SysUserController.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package easydo.technology.system.rest;
  2. import easydo.technology.components.JdbcClient;
  3. import easydo.technology.config.RsaProperties;
  4. import easydo.technology.exception.BadRequestException;
  5. import easydo.technology.exception.BizException;
  6. import easydo.technology.system.model.*;
  7. import easydo.technology.utils.RsaUtils;
  8. import easydo.technology.utils.SecurityUtils;
  9. import org.springframework.http.HttpStatus;
  10. import org.springframework.http.ResponseEntity;
  11. import org.springframework.security.crypto.password.PasswordEncoder;
  12. import org.springframework.web.bind.annotation.RequestBody;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RestController;
  15. import javax.annotation.Resource;
  16. import javax.sql.DataSource;
  17. import java.sql.Connection;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.stream.Collectors;
  22. @RestController
  23. @RequestMapping("/sysUser")
  24. public class SysUserController {
  25. @Resource
  26. JdbcClient jdbcClient;
  27. @Resource
  28. DataSource dataSource;
  29. @Resource
  30. PasswordEncoder passwordEncoder;
  31. @RequestMapping(value = "/getList")
  32. public Object getList(@RequestBody Map<String, Object> map) throws Exception {
  33. Connection connection = dataSource.getConnection();
  34. try {
  35. Map<String, Object> paramMap = new HashMap<>();
  36. List<SysUser> list = jdbcClient.getJdbcList(map, SysUser.class);
  37. for (SysUser model : list) {
  38. // 1) 二次查询部门详情并回填到返回对象
  39. SysDept dept = new SysDept();
  40. dept.setId(model.getDeptId());
  41. dept = jdbcClient.getJdbcModel(dept, connection);
  42. model.setDept(dept);
  43. // 2) 二次查询用户角色并回填 roleList
  44. SysUsersRoles usersRoles = new SysUsersRoles();
  45. usersRoles.setUserId(model.getId());
  46. List<SysUsersRoles> rolesList = jdbcClient.getJdbcList(usersRoles, connection);
  47. List<Long> roleIdList = rolesList.stream().map(SysUsersRoles::getRoleId).collect(Collectors.toList());
  48. paramMap.put("idIn", roleIdList);
  49. List<SysRole> roleList = jdbcClient.getJdbcList(paramMap, SysRole.class, connection);
  50. model.setRoleList(roleList);
  51. // 3) 二次查询租户详情并回填 tenant(对齐 Go 版本 userGetList)
  52. if (model.getTenantId() != null) {
  53. Tenant tenant = new Tenant();
  54. tenant.setId(model.getTenantId());
  55. tenant = jdbcClient.getJdbcModelById(tenant, connection);
  56. model.setTenant(tenant);
  57. }
  58. }
  59. return new ResponseEntity<>(list, HttpStatus.OK);
  60. } catch (Exception e) {
  61. throw new BizException(e.getMessage());
  62. } finally {
  63. connection.close();
  64. }
  65. }
  66. @RequestMapping(value = "/getPage")
  67. public Object getPage(@RequestBody Map<String, Object> map) throws Exception {
  68. Connection connection = dataSource.getConnection();
  69. try {
  70. Map<String, Object> paramMap = new HashMap<>();
  71. Map<String, Object> recordsPage = jdbcClient.getJdbcPage(map, SysUser.class, connection);
  72. List<SysUser> records = (List<SysUser>) recordsPage.get("records");
  73. for (SysUser model : records) {
  74. SysDept dept = new SysDept();
  75. dept.setId(model.getDeptId());
  76. dept = jdbcClient.getJdbcModel(dept, connection);
  77. model.setDept(dept);
  78. SysUsersRoles usersRoles = new SysUsersRoles();
  79. usersRoles.setUserId(model.getId());
  80. List<SysUsersRoles> rolesList = jdbcClient.getJdbcList(usersRoles, connection);
  81. List<Long> roleIdList = rolesList.stream().map(SysUsersRoles::getRoleId).collect(Collectors.toList());
  82. paramMap.put("idIn", roleIdList);
  83. List<SysRole> roleList = jdbcClient.getJdbcList(paramMap, SysRole.class, connection);
  84. model.setRoleList(roleList);
  85. if (model.getTenantId() != null) {
  86. Tenant tenant = new Tenant();
  87. tenant.setId(model.getTenantId());
  88. tenant = jdbcClient.getJdbcModelById(tenant, connection);
  89. model.setTenant(tenant);
  90. }
  91. }
  92. return new ResponseEntity<>(recordsPage, HttpStatus.OK);
  93. } catch (Exception e) {
  94. throw new BizException(e.getMessage());
  95. } finally {
  96. connection.close();
  97. }
  98. }
  99. @RequestMapping(value = "/save")
  100. public Object add(@RequestBody SysUser model) throws Exception {
  101. Connection connection = dataSource.getConnection();
  102. try {
  103. connection.setAutoCommit(false);
  104. SysUser user = new SysUser();
  105. user.setUsername(model.getUsername());
  106. int count = jdbcClient.getJdbcCount(user, connection);
  107. if (count > 0) {
  108. throw new BizException("用户名已存在");
  109. }
  110. model.setPassword(passwordEncoder.encode("123456"));
  111. jdbcClient.jdbcInsert(model, connection);
  112. List<SysRole> roleList = model.getRoleList();
  113. for (SysRole rmodel : roleList) {
  114. SysUsersRoles usersRoles = new SysUsersRoles();
  115. usersRoles.setUserId(model.getId());
  116. usersRoles.setRoleId(rmodel.getId());
  117. jdbcClient.jdbcInsert(usersRoles, connection);
  118. }
  119. connection.commit();
  120. return new ResponseEntity<>(model, HttpStatus.OK);
  121. } catch (Exception e) {
  122. connection.rollback();
  123. throw new BizException(e.getMessage());
  124. } finally {
  125. connection.close();
  126. }
  127. }
  128. @RequestMapping(value = "/update")
  129. public Object update(@RequestBody SysUser model) throws Exception {
  130. Connection connection = dataSource.getConnection();
  131. try {
  132. connection.setAutoCommit(false);
  133. jdbcClient.jdbcUpdateById(model, connection);
  134. SysUsersRoles usersRoles = new SysUsersRoles();
  135. usersRoles.setUserId(model.getId());
  136. jdbcClient.jdbcRemove(usersRoles, connection);
  137. List<SysRole> roleList = model.getRoleList();
  138. for (SysRole rmodel : roleList) {
  139. usersRoles = new SysUsersRoles();
  140. usersRoles.setUserId(model.getId());
  141. usersRoles.setRoleId(rmodel.getId());
  142. jdbcClient.jdbcInsert(usersRoles, connection);
  143. }
  144. connection.commit();
  145. return new ResponseEntity<>(HttpStatus.OK);
  146. } catch (Exception e) {
  147. connection.rollback();
  148. throw new BizException(e.getMessage());
  149. } finally {
  150. connection.close();
  151. }
  152. }
  153. @RequestMapping(value = "/remove")
  154. public Object deleteProgram(@RequestBody SysUser model) throws Exception {
  155. Connection connection = dataSource.getConnection();
  156. try {
  157. connection.setAutoCommit(false);
  158. jdbcClient.jdbcRemoveById(model, connection);
  159. SysUsersRoles usersRoles = new SysUsersRoles();
  160. usersRoles.setUserId(model.getId());
  161. jdbcClient.jdbcRemove(usersRoles, connection);
  162. connection.commit();
  163. return new ResponseEntity<>(HttpStatus.OK);
  164. } catch (Exception e) {
  165. connection.rollback();
  166. throw new BizException(e.getMessage());
  167. } finally {
  168. connection.close();
  169. }
  170. }
  171. @RequestMapping(value = "/updatePass")
  172. public ResponseEntity<Object> updatePass(@RequestBody Map<String, Object> map) throws Exception {
  173. Connection connection = dataSource.getConnection();
  174. try {
  175. String oldPass = RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, map.get("oldPass").toString());
  176. String newPass = RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, map.get("newPass").toString());
  177. SysUser user = new SysUser();
  178. user.setUsername(SecurityUtils.getCurrentUsername());
  179. user = jdbcClient.getJdbcModel(user, connection);
  180. if (!passwordEncoder.matches(oldPass, user.getPassword())) {
  181. throw new BadRequestException("修改失败,旧密码错误");
  182. }
  183. if (passwordEncoder.matches(newPass, user.getPassword())) {
  184. throw new BadRequestException("新密码不能与旧密码相同");
  185. }
  186. user.setPassword(passwordEncoder.encode(newPass));
  187. jdbcClient.jdbcUpdateById(user, connection);
  188. return new ResponseEntity<>(HttpStatus.OK);
  189. } catch (Exception e) {
  190. throw new BizException(e.getMessage());
  191. } finally {
  192. connection.close();
  193. }
  194. }
  195. @RequestMapping(value = "/resetPass")
  196. public Object resetPass(@RequestBody SysUser model) throws Exception {
  197. model.setPassword(passwordEncoder.encode("123456"));
  198. jdbcClient.jdbcUpdateById(model);
  199. return new ResponseEntity<>(HttpStatus.OK);
  200. }
  201. }