SmartDataSourceServiceImpl.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package com.template.services.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.baomidou.mybatisplus.core.metadata.IPage;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  6. import com.template.common.utils.CommonUtil;
  7. import com.template.mapper.SmartDataClassMapper;
  8. import com.template.mapper.SmartDataSourceMapper;
  9. import com.template.model.pojo.SmartDataClass;
  10. import com.template.model.pojo.SmartDataSource;
  11. import com.template.model.result.PageUtils;
  12. import com.template.services.SmartDataSourceService;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Service;
  15. import org.springframework.util.StringUtils;
  16. import java.util.List;
  17. import java.util.Map;
  18. /**
  19. * <p>
  20. * 数据源配置 服务实现类
  21. * </p>
  22. *
  23. * @author ceshi
  24. * @since 2023-12-05
  25. */
  26. @Service
  27. public class SmartDataSourceServiceImpl extends ServiceImpl<SmartDataSourceMapper, SmartDataSource> implements SmartDataSourceService {
  28. @Autowired
  29. private SmartDataSourceMapper smartDataSourceMapper;
  30. @Autowired
  31. private SmartDataClassMapper smartDataClassMapper;
  32. @Override
  33. public Map<String, String> insertSmartDataSource(SmartDataSource smartDataSource) {
  34. // 检测参数,还有是否存在重复记录
  35. Map<String, String> stringStringMap = validateParams(smartDataSource, "insert");
  36. if (stringStringMap != null) {
  37. return stringStringMap;
  38. }
  39. // 必填的参数不为null,则进行新增操作
  40. int result = smartDataSourceMapper.insert(smartDataSource);
  41. if (result > 0) {
  42. return CommonUtil.getReturnMap(String.valueOf(result), "数据源添加成功!");
  43. } else {
  44. return CommonUtil.getReturnMap(String.valueOf(result), "数据源添加失败!");
  45. }
  46. }
  47. // 检测验证参数,和数据库中是否存在重复记录
  48. private Map<String, String> validateParams(SmartDataSource smartDataSource, String action) {
  49. // 看是否有相同的记录
  50. QueryWrapper<SmartDataSource> queryWrapper = new QueryWrapper<>();
  51. if ("update".equals(action)) {
  52. queryWrapper.like(smartDataSource.getDsId() != null, "ds_id", smartDataSource.getDsId());
  53. }
  54. queryWrapper.like(smartDataSource.getDsClsId() != null, "ds_cls_id", smartDataSource.getDsClsId());
  55. queryWrapper.like(StringUtils.hasText(smartDataSource.getDsName()), "ds_name", smartDataSource.getDsName());
  56. queryWrapper.like(StringUtils.hasText(smartDataSource.getDsUrl()), "ds_url", smartDataSource.getDsUrl());
  57. queryWrapper.like(StringUtils.hasText(smartDataSource.getDsUser()), "ds_user", smartDataSource.getDsUser());
  58. queryWrapper.like(StringUtils.hasText(smartDataSource.getDsPassword()), "ds_password", smartDataSource.getDsPassword());
  59. queryWrapper.like(smartDataSource.getDsStatus() != null, "ds_status", smartDataSource.getDsStatus());
  60. queryWrapper.like(StringUtils.hasText(smartDataSource.getDsDescrition()), "ds_descrition", smartDataSource.getDsDescrition());
  61. List<SmartDataSource> smartDataSources = smartDataSourceMapper.selectList(queryWrapper);
  62. if (smartDataSources.size() > 0) {
  63. if ("update".equals(action)) {
  64. return CommonUtil.getReturnMap("0", "数据未修改,请修改后再提交!");
  65. } else {
  66. return CommonUtil.getReturnMap("0", "有重复记录!");
  67. }
  68. }
  69. // 检测必要参数是否为null
  70. if ("update".equals(action)) {
  71. if (smartDataSource.getDsId() == null) {
  72. return CommonUtil.getReturnMap("0", "【数据源id】不能为空!");
  73. }
  74. SmartDataSource sdc = smartDataSourceMapper.selectById(smartDataSource.getDsId());
  75. if (sdc == null) {
  76. return CommonUtil.getReturnMap("0", "要修改的【数据源】不存在!");
  77. }
  78. }
  79. // 检测必要参数是否为null
  80. if (smartDataSource.getDsClsId() == null) {
  81. return CommonUtil.getReturnMap("0", "【数据源类型id】不能为空!");
  82. }
  83. SmartDataClass smartDataClass = smartDataClassMapper.selectById(smartDataSource.getDsClsId());
  84. if (smartDataClass == null) {
  85. return CommonUtil.getReturnMap("0", "选择的【数据源类型】不存在!");
  86. }
  87. if (smartDataSource.getDsName() == null) {
  88. return CommonUtil.getReturnMap("0", "【数据源名称】不能为空!");
  89. }
  90. if (smartDataSource.getDsUrl() == null) {
  91. return CommonUtil.getReturnMap("0", "【数据源连接地址】不能为空!");
  92. }
  93. if (smartDataSource.getDsUser() == null) {
  94. return CommonUtil.getReturnMap("0", "【数据源用户】不能为空!");
  95. }
  96. if (smartDataSource.getDsPassword() == null) {
  97. return CommonUtil.getReturnMap("0", "【数据源密码】不能为空!");
  98. }
  99. if (smartDataSource.getDsDescrition() == null) {
  100. return CommonUtil.getReturnMap("0", "【数据源描述】不能为空!");
  101. }
  102. return null;
  103. }
  104. @Override
  105. public Map<String, String> updateSmartDataSource(SmartDataSource smartDataSource) {
  106. // 检测参数,还有是否存在重复记录
  107. Map<String, String> stringStringMap = validateParams(smartDataSource, "insert");
  108. if (stringStringMap != null) {
  109. return stringStringMap;
  110. }
  111. int result = smartDataSourceMapper.updateById(smartDataSource);
  112. if (result > 0) {
  113. return CommonUtil.getReturnMap(String.valueOf(result), "数据源修改成功!");
  114. } else {
  115. return CommonUtil.getReturnMap(String.valueOf(result), "数据源修改失败!");
  116. }
  117. }
  118. @Override
  119. public PageUtils<SmartDataSource> queryPageSmartDataSources(int currentPage, int pageCount, SmartDataSource smartDataSource) {
  120. Page<SmartDataSource> page = new Page<>(currentPage, pageCount);
  121. QueryWrapper<SmartDataSource> queryWrapper = new QueryWrapper<>();
  122. queryWrapper.like(StringUtils.hasText(smartDataSource.getDsName()), "ds_name", smartDataSource.getDsName());
  123. queryWrapper.like(StringUtils.hasText(smartDataSource.getDsUrl()), "ds_url", smartDataSource.getDsUrl());
  124. queryWrapper.like(smartDataSource.getDsStatus() != null, "ds_status", smartDataSource.getDsStatus());
  125. queryWrapper.like(StringUtils.hasText(smartDataSource.getDsDescrition()), "ds_descrition", smartDataSource.getDsDescrition());
  126. queryWrapper.orderByDesc("ds_update_time");
  127. IPage<SmartDataSource> result = smartDataSourceMapper.selectPage(page, queryWrapper);
  128. return new PageUtils<>(result);
  129. }
  130. @Override
  131. public int deleteSmartDataSourceById(int id) {
  132. int result = smartDataSourceMapper.deleteById(id);
  133. return result;
  134. }
  135. @Override
  136. public SmartDataSource getSmartById(int id) {
  137. SmartDataSource result = smartDataSourceMapper.selectById(id);
  138. return result;
  139. }
  140. }