SmartDataClassController.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package com.template.controller;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.template.annotation.DESRespondSecret;
  4. import com.template.api.SmartDataClassControllerAPI;
  5. import com.template.common.utils.CommonUtil;
  6. import com.template.common.utils.paramUtils;
  7. import com.template.model.pojo.SmartDataClass;
  8. import com.template.model.pojo.SmartDataSource;
  9. import com.template.model.pojo.SmartDataSourceLog;
  10. import com.template.model.result.CommonResult;
  11. import com.template.model.result.PageUtils;
  12. import com.template.services.SmartDataClassService;
  13. import com.template.services.SmartDataSourceLogService;
  14. import com.template.services.SmartDataSourceService;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.http.HttpRequest;
  17. import org.springframework.validation.BindingResult;
  18. import org.springframework.web.bind.annotation.RequestMapping;
  19. import org.springframework.web.bind.annotation.RestController;
  20. import javax.servlet.http.HttpServletRequest;
  21. import java.net.InetAddress;
  22. import java.net.UnknownHostException;
  23. import java.util.Map;
  24. /**
  25. * <p>
  26. * 数据源类别 前端控制器
  27. * </p>
  28. *
  29. * @author ceshi
  30. * @since 2023-12-05
  31. */
  32. @RestController
  33. //返回参数加密注解
  34. @DESRespondSecret
  35. public class SmartDataClassController implements SmartDataClassControllerAPI {
  36. @Autowired
  37. private SmartDataClassService smartDataClassService;
  38. @Autowired
  39. private SmartDataSourceService smartDataSourceService;
  40. @Autowired
  41. private SmartDataSourceLogService smartDataSourceLogService;
  42. /**
  43. * 新增 数据源分类
  44. *
  45. * @param smartDataClass
  46. * @param bindingResult
  47. * @return
  48. */
  49. @Override
  50. @DESRespondSecret(validated = true)
  51. public CommonResult insertSmartDataClass(SmartDataClass smartDataClass, HttpServletRequest httpServletRequest, BindingResult bindingResult) {
  52. if (bindingResult.hasErrors()) {
  53. String st = paramUtils.getParamError(bindingResult);
  54. return CommonResult.fail(st);
  55. }
  56. if (smartDataClass.getDsClsName() == null) {
  57. return CommonResult.fail("数据源分类名称为空!");
  58. }
  59. if (smartDataClass.getDsClsDriver() == null) {
  60. return CommonResult.fail("数据源驱动为空!");
  61. }
  62. // 检测是否有重复名称
  63. QueryWrapper<SmartDataClass> smartDataClassQueryWrapper = new QueryWrapper<>();
  64. smartDataClassQueryWrapper.eq("ds_cls_name", smartDataClass.getDsClsName());
  65. smartDataClassQueryWrapper.eq("ds_cls_driver", smartDataClass.getDsClsDriver());
  66. int count = smartDataClassService.count(smartDataClassQueryWrapper);
  67. if (count > 0) {
  68. return CommonResult.fail("数据源分类名称和驱动有重复,新增失败!");
  69. }
  70. SmartDataSourceLog smartDataSourceLog =
  71. CommonUtil.generateLog("新增数据源分类|【数据交换中心】→【数据源分类管理】|新增【数据源分类】|create", httpServletRequest);
  72. int result = smartDataClassService.insertSmartDataClass(smartDataClass);
  73. if (result > 0) {
  74. smartDataSourceLog.setLogActionStatus(1);
  75. smartDataSourceLogService.insertSmartDataSourceLog(smartDataSourceLog);
  76. return CommonResult.ok("添加成功");
  77. } else {
  78. smartDataSourceLog.setLogActionStatus(0);
  79. smartDataSourceLogService.insertSmartDataSourceLog(smartDataSourceLog);
  80. return CommonResult.fail("添加失败");
  81. }
  82. }
  83. /**
  84. * 更新 数据源分类
  85. *
  86. * @param smartDataClass
  87. * @param bindingResult
  88. * @return
  89. */
  90. @Override
  91. @DESRespondSecret(validated = true)
  92. public CommonResult updateSmartDataClassById(SmartDataClass smartDataClass, HttpServletRequest httpServletRequest, BindingResult bindingResult) {
  93. if (bindingResult.hasErrors()) {
  94. String st = paramUtils.getParamError(bindingResult);
  95. return CommonResult.fail(st);
  96. }
  97. if (smartDataClass.getDsClsName() == null) {
  98. return CommonResult.fail("数据源分类名称为空!");
  99. }
  100. if (smartDataClass.getDsClsDriver() == null) {
  101. return CommonResult.fail("数据源驱动为空!");
  102. }
  103. // 检测是否有重复名称
  104. QueryWrapper<SmartDataClass> smartDataClassQueryWrapper = new QueryWrapper<>();
  105. smartDataClassQueryWrapper.eq("ds_cls_name", smartDataClass.getDsClsName());
  106. smartDataClassQueryWrapper.eq("ds_cls_driver", smartDataClass.getDsClsDriver());
  107. int count = smartDataClassService.count(smartDataClassQueryWrapper);
  108. if (count > 0) {
  109. return CommonResult.fail("数据源分类名称和驱动有重复,更新失败!");
  110. }
  111. SmartDataSourceLog smartDataSourceLog =
  112. CommonUtil.generateLog("修改数据源分类|【数据交换中心】→【数据源分类管理】|修改【数据源分类】|update", httpServletRequest);
  113. int result = smartDataClassService.updateSmartDataClass(smartDataClass);
  114. if (result > 0) {
  115. smartDataSourceLog.setLogActionStatus(1);
  116. smartDataSourceLogService.insertSmartDataSourceLog(smartDataSourceLog);
  117. return CommonResult.ok("修改成功");
  118. } else {
  119. smartDataSourceLog.setLogActionStatus(0);
  120. smartDataSourceLogService.insertSmartDataSourceLog(smartDataSourceLog);
  121. return CommonResult.fail("修改失败");
  122. }
  123. }
  124. /**
  125. * 数据源分类 分页数据查询
  126. *
  127. * @param currentPage 当前页数
  128. * @param pageCount 一页数据条数
  129. * @param name 查询名称
  130. * @return
  131. */
  132. @Override
  133. @DESRespondSecret(validated = true)
  134. public CommonResult queryPageSmartDataClass(int currentPage, int pageCount, String name) {
  135. PageUtils<SmartDataClass> result = smartDataClassService.queryPageSmartDataClasss(currentPage, pageCount, name);
  136. return CommonResult.ok(result);
  137. }
  138. @Override
  139. @DESRespondSecret(validated = true)
  140. public CommonResult deleteSmartDataClassById(int id, HttpServletRequest httpServletRequest) {
  141. SmartDataClass data = smartDataClassService.getSmartById(id);
  142. if (data == null) {
  143. return CommonResult.fail("当前数据源类别不存在,删除失败!");
  144. }
  145. // 检测当前类别下是否有数据源,有则不能删除,否则可以删除
  146. QueryWrapper<SmartDataSource> smartDataSourceQueryWrapper = new QueryWrapper<>();
  147. smartDataSourceQueryWrapper.eq("ds_cls_id", id);
  148. int count = smartDataSourceService.count(smartDataSourceQueryWrapper);
  149. if (count > 0) {
  150. return CommonResult.fail("当前类别下有数据源,不能删除,删除失败!");
  151. }
  152. SmartDataSourceLog smartDataSourceLog =
  153. CommonUtil.generateLog("删除数据源分类|【数据交换中心】→【数据源分类管理】|删除【数据源分类】|delete", httpServletRequest);
  154. int result = smartDataClassService.deleteSmartDataClassById(id);
  155. if (result > 0) {
  156. smartDataSourceLog.setLogActionStatus(1);
  157. smartDataSourceLogService.insertSmartDataSourceLog(smartDataSourceLog);
  158. return CommonResult.ok("删除成功");
  159. } else {
  160. smartDataSourceLog.setLogActionStatus(0);
  161. smartDataSourceLogService.insertSmartDataSourceLog(smartDataSourceLog);
  162. return CommonResult.fail("删除失败");
  163. }
  164. }
  165. }