TenantController.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package easydo.technology.system.rest;
  2. import easydo.technology.components.JdbcClient;
  3. import easydo.technology.system.model.SysDept;
  4. import easydo.technology.system.model.Tenant;
  5. import org.springframework.http.HttpStatus;
  6. import org.springframework.http.ResponseEntity;
  7. import org.springframework.web.bind.annotation.PostMapping;
  8. import org.springframework.web.bind.annotation.RequestBody;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import javax.annotation.Resource;
  12. import javax.sql.DataSource;
  13. import java.sql.Connection;
  14. import java.util.HashMap;
  15. import java.util.List;
  16. import java.util.Map;
  17. @RestController
  18. @RequestMapping("/tenant")
  19. public class TenantController {
  20. @Resource
  21. private JdbcClient jdbcClient;
  22. @Resource
  23. private DataSource dataSource;
  24. @PostMapping(value = "/getList")
  25. public Object getList(@RequestBody(required = false) Map<String, Object> map) {
  26. try {
  27. if (map == null) {
  28. map = new HashMap<>();
  29. }
  30. List<Tenant> list = jdbcClient.getJdbcList(map, Tenant.class);
  31. return new ResponseEntity<>(list, HttpStatus.OK);
  32. } catch (Exception e) {
  33. return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
  34. }
  35. }
  36. @PostMapping(value = "/getPage")
  37. public Object getPage(@RequestBody(required = false) Map<String, Object> map) {
  38. try {
  39. if (map == null) {
  40. map = new HashMap<>();
  41. }
  42. Map<String, Object> page = jdbcClient.getJdbcPage(map, Tenant.class);
  43. Object records = page.get("records");
  44. if (records == null) {
  45. page.put("records", new java.util.ArrayList<>());
  46. }
  47. return new ResponseEntity<>(page, HttpStatus.OK);
  48. } catch (Exception e) {
  49. return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
  50. }
  51. }
  52. @PostMapping(value = "/save")
  53. public Object save(@RequestBody Tenant model) {
  54. Connection connection = null;
  55. try {
  56. connection = dataSource.getConnection();
  57. connection.setAutoCommit(false); // 开启事务
  58. model.setStatus("enable"); // 强制设置状态为启用
  59. jdbcClient.jdbcInsert(model, connection);
  60. // 同步创建关联部门
  61. SysDept dept = new SysDept();
  62. dept.setName(model.getName());
  63. dept.setPid(1L); // 顶级部门
  64. dept.setFirmFunctionary(model.getManagerName());
  65. dept.setFirmFunctionaryPhone(model.getManagerPhone());
  66. dept.setDeptSort(1);
  67. dept.setTenantId(model.getId());
  68. jdbcClient.jdbcInsert(dept, connection);
  69. connection.commit(); // 提交事务
  70. return new ResponseEntity<>(model, HttpStatus.OK);
  71. } catch (Exception e) {
  72. if (connection != null) {
  73. try {
  74. connection.rollback(); // 回滚事务
  75. } catch (Exception ignored) {
  76. }
  77. }
  78. return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
  79. } finally {
  80. if (connection != null) {
  81. try {
  82. connection.close();
  83. } catch (Exception ignored) {
  84. }
  85. }
  86. }
  87. }
  88. @PostMapping(value = "/update")
  89. public Object update(@RequestBody Tenant model) {
  90. Connection connection = null;
  91. try {
  92. connection = dataSource.getConnection();
  93. connection.setAutoCommit(false); // 开启事务
  94. jdbcClient.jdbcUpdateById(model, connection);
  95. // 同步更新关联部门信息
  96. SysDept deptQuery = new SysDept();
  97. deptQuery.setPid(1L);
  98. deptQuery.setTenantId(model.getId());
  99. SysDept dept = jdbcClient.getJdbcModel(deptQuery, connection);
  100. if (dept != null) {
  101. dept.setName(model.getName());
  102. dept.setFirmFunctionary(model.getManagerName());
  103. dept.setFirmFunctionaryPhone(model.getManagerPhone());
  104. jdbcClient.jdbcUpdateById(dept, connection);
  105. }
  106. connection.commit(); // 提交事务
  107. return new ResponseEntity<>(model, HttpStatus.OK);
  108. } catch (Exception e) {
  109. if (connection != null) {
  110. try {
  111. connection.rollback(); // 回滚事务
  112. } catch (Exception ignored) {
  113. }
  114. }
  115. return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
  116. } finally {
  117. if (connection != null) {
  118. try {
  119. connection.close();
  120. } catch (Exception ignored) {
  121. }
  122. }
  123. }
  124. }
  125. @PostMapping(value = "/remove")
  126. public Object remove(@RequestBody Tenant model) {
  127. Connection connection = null;
  128. try {
  129. connection = dataSource.getConnection();
  130. connection.setAutoCommit(false);
  131. jdbcClient.jdbcRemoveById(model, connection);
  132. // Go 版本此处删除部门逻辑被注释掉,这里保留同样逻辑(默认不执行)
  133. // SysDept dept = new SysDept();
  134. // dept.setPid(1L);
  135. // dept.setTenantId(model.getId());
  136. // jdbcClient.jdbcRemove(dept, connection);
  137. connection.commit();
  138. return new ResponseEntity<>(model, HttpStatus.OK);
  139. } catch (Exception e) {
  140. if (connection != null) {
  141. try {
  142. connection.rollback();
  143. } catch (Exception ignored) {
  144. }
  145. }
  146. return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
  147. } finally {
  148. if (connection != null) {
  149. try {
  150. connection.close();
  151. } catch (Exception ignored) {
  152. }
  153. }
  154. }
  155. }
  156. }