AdminController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. package com.template.controller;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.metadata.IPage;
  4. import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.template.api.AdminAPI;
  7. import com.template.common.utils.RSAUtils;
  8. import com.template.config.MySecurity;
  9. import com.template.model.dto.SaveAdminDto;
  10. import com.template.model.dto.UpdateAdminDto;
  11. import com.template.model.dto.UsersPageDto;
  12. import com.template.model.pojo.*;
  13. import com.template.model.result.CommonResult;
  14. import com.template.model.vo.AdminVo;
  15. import com.template.model.vo.UsersVo;
  16. import com.template.services.*;
  17. import org.springframework.beans.BeanUtils;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.web.bind.annotation.RestController;
  20. import java.util.ArrayList;
  21. import java.util.Arrays;
  22. import java.util.List;
  23. /**
  24. * <p>
  25. * 前端控制器
  26. * </p>
  27. *
  28. * @author ceshi
  29. * @since 2023-12-13
  30. */
  31. @RestController
  32. public class AdminController implements AdminAPI {
  33. @Autowired
  34. AdminService adminService;
  35. @Autowired
  36. AdminMenuService adminMenuService;
  37. @Autowired
  38. PermissionSettingService permissionSettingService;
  39. @Autowired
  40. UsersService usersService;
  41. @Autowired
  42. MySecurity mySecurity;
  43. @Autowired
  44. OrganizationService organizationService;
  45. @Override
  46. public CommonResult getListPage(int permissionSettingId, int page, int size, String keyWord, String startTime, String endTime, String status) {
  47. PermissionSetting permissionSetting = permissionSettingService.getById(permissionSettingId);
  48. if (ObjectUtils.isEmpty(permissionSetting)) {
  49. return CommonResult.fail("非法进入");
  50. }
  51. // 判断该用户是否拥有权限
  52. // 判断该用户是否拥有权限
  53. // String accountManagement = permissionSetting.getAccountManagement();
  54. // if (!"0".equals(accountManagement) && !accountManagement.contains("4")) {
  55. // return CommonResult.fail("此账号暂无该权限");
  56. // }
  57. if (ObjectUtils.isEmpty(page) && page <= 0) {
  58. page = 1;
  59. }
  60. if (ObjectUtils.isEmpty(size) && size <= 0) {
  61. size = 10;
  62. }
  63. LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
  64. wrapper.between(ObjectUtils.isNotEmpty(startTime) && ObjectUtils.isNotEmpty(endTime), Admin::getCreateTime, startTime, endTime)
  65. .eq(ObjectUtils.isNotEmpty(status), Admin::getStatus, status)
  66. .like(ObjectUtils.isNotEmpty(keyWord), Admin::getName, keyWord)
  67. .orderByDesc(Admin::getCreateTime);
  68. IPage<Admin> page1 = adminService.page(new Page<>(page, size), wrapper);
  69. Page<AdminVo> adminVoPage = new Page<>();
  70. List<Admin> records = page1.getRecords();
  71. ArrayList<AdminVo> adminVos = new ArrayList<>();
  72. for (Admin record : records) {
  73. AdminVo adminVo = new AdminVo();
  74. BeanUtils.copyProperties(record,adminVo);
  75. String adminMenuId = record.getAdminMenuId();
  76. AdminMenu adminMenu = adminMenuService.getById(adminMenuId);
  77. adminVo.setAdminMenu(adminMenu);
  78. Users byId = usersService.getById(record.getUsersId());
  79. if (ObjectUtils.isNotEmpty(byId)) {
  80. adminVo.setCardNumber(byId.getCardNumber());
  81. }
  82. List<Organization> checkOrgList= new ArrayList<>();
  83. if (ObjectUtils.isNotEmpty(record.getCheckOrg())) {
  84. List<String> checkOrg= Arrays.asList(record.getCheckOrg().split(","));
  85. for(int i=0;i<checkOrg.size();i++){
  86. Organization organization=organizationService.getById(checkOrg.get(i));
  87. if (organization!=null){
  88. checkOrgList.add(organization);
  89. }
  90. }
  91. adminVo.setCheckOrg(checkOrgList);
  92. }
  93. // 解密
  94. try {
  95. String decrypt = RSAUtils.decrypt(record.getPassWord(), RSAUtils.getPrivateKey(mySecurity.privateKey));
  96. adminVo.setPassWord(decrypt);
  97. } catch (Exception e) {
  98. throw new RuntimeException(e);
  99. }
  100. adminVos.add(adminVo);
  101. }
  102. BeanUtils.copyProperties(page1,adminVoPage);
  103. adminVoPage.setRecords(adminVos);
  104. return CommonResult.ok(adminVoPage);
  105. }
  106. @Override
  107. public CommonResult save(SaveAdminDto saveAdminDto) {
  108. String permissionSettingId = saveAdminDto.getPermissionSettingId();
  109. PermissionSetting permissionSetting = permissionSettingService.getById(permissionSettingId);
  110. if (ObjectUtils.isEmpty(permissionSetting)) {
  111. return CommonResult.fail("非法进入");
  112. }
  113. // 判断该用户是否拥有权限
  114. // 判断该用户是否拥有权限
  115. String accountManagement = permissionSetting.getAccountManagement();
  116. if (!"0".equals(accountManagement) && !accountManagement.contains("1")) {
  117. return CommonResult.fail("此账号暂无该权限");
  118. }
  119. String userNumber = saveAdminDto.getUserNumber();
  120. Admin result = adminService.getAdminByAccount(userNumber);
  121. if (ObjectUtils.isNotEmpty(result)) {
  122. return CommonResult.fail("该账号已存在");
  123. }
  124. // 通过cardNumber找到对应的用户
  125. String cardNumber = saveAdminDto.getCardNumber();
  126. LambdaQueryWrapper<Users> wrapper=new LambdaQueryWrapper<>();
  127. wrapper.eq(Users::getCardNumber,cardNumber);
  128. Users users = usersService.getOne(wrapper);
  129. if (ObjectUtils.isEmpty(users)) {
  130. return CommonResult.fail("暂无该用户");
  131. }
  132. // 通过用户id找管理端用户,如果有就不能在绑定
  133. // LambdaQueryWrapper<Admin> wrapperA=new LambdaQueryWrapper<>();
  134. // wrapperA.eq(Admin::getUsersId,users.getId());
  135. // Admin one = adminService.getOne(wrapperA);
  136. // if (ObjectUtils.isNotEmpty(one)) {
  137. // return CommonResult.fail("该用户已绑定,请更换绑定用户");
  138. // }
  139. // 密码加密
  140. String passWord = saveAdminDto.getPassWord();
  141. try {
  142. Admin admin = new Admin();
  143. String encrypt = RSAUtils.encrypt(passWord, RSAUtils.getPublicKey(mySecurity.publicKey));
  144. BeanUtils.copyProperties(saveAdminDto, admin);
  145. admin.setPassWord(encrypt);
  146. admin.setStatus("1");
  147. // admin.setCheckOrg(saveAdminDto.getCheckOrg().toString());
  148. admin.setUsersId(users.getId()+"");
  149. adminService.save(admin);
  150. return CommonResult.ok();
  151. } catch (Exception e) {
  152. throw new RuntimeException(e);
  153. }
  154. }
  155. @Override
  156. public CommonResult update(UpdateAdminDto updateAdminDto) {
  157. String permissionSettingId = updateAdminDto.getPermissionSettingId();
  158. PermissionSetting permissionSetting = permissionSettingService.getById(permissionSettingId);
  159. if (ObjectUtils.isEmpty(permissionSetting)) {
  160. return CommonResult.fail("非法进入");
  161. }
  162. // 判断该用户是否拥有权限
  163. // 判断该用户是否拥有权限
  164. String accountManagement = permissionSetting.getAccountManagement();
  165. if (!"0".equals(accountManagement) && !accountManagement.contains("2")) {
  166. return CommonResult.fail("此账号暂无该权限");
  167. }
  168. // 通过cardNumber找到对应的用户
  169. String cardNumber = updateAdminDto.getCardNumber();
  170. LambdaQueryWrapper<Users> wrapper=new LambdaQueryWrapper<>();
  171. wrapper.eq(Users::getCardNumber,cardNumber);
  172. Users users = usersService.getOne(wrapper);
  173. if (ObjectUtils.isEmpty(users)) {
  174. return CommonResult.fail("暂无该用户");
  175. }
  176. Admin admin = updateAdminDto.getAdmin();
  177. Admin byId = adminService.getById(admin.getId());
  178. if (ObjectUtils.isEmpty(byId)) {
  179. return CommonResult.fail("该管理员不存在");
  180. }
  181. String userNumber = admin.getUserNumber();
  182. String userNumber1 = byId.getUserNumber();
  183. if (!userNumber.equals(userNumber1)) {
  184. Admin result = adminService.getAdminByAccount(userNumber);
  185. if (ObjectUtils.isNotEmpty(result)) {
  186. return CommonResult.fail("该账号已存在");
  187. }
  188. }
  189. // 当前可以重复
  190. // 用户不能重复绑定
  191. // String usersId = admin.getUsersId();
  192. // String usersId1 = byId.getUsersId();
  193. // if (!usersId.equals(usersId1)) {
  194. // LambdaQueryWrapper<Admin> wrapperA=new LambdaQueryWrapper<>();
  195. // wrapperA.eq(Admin::getUsersId,users.getId());
  196. // Admin one = adminService.getOne(wrapperA);
  197. // if (ObjectUtils.isNotEmpty(one)) {
  198. // return CommonResult.fail("该用户已绑定,请更换绑定用户");
  199. // }
  200. // }
  201. try {
  202. String passWord = admin.getPassWord();
  203. String encrypt = RSAUtils.encrypt(passWord, RSAUtils.getPublicKey(mySecurity.publicKey));
  204. admin.setPassWord(encrypt);
  205. admin.setUsersId(users.getId()+"");
  206. boolean update = adminService.updateById(admin);
  207. if (update) {
  208. return CommonResult.ok();
  209. }
  210. return CommonResult.fail();
  211. } catch (Exception e) {
  212. throw new RuntimeException(e);
  213. }
  214. }
  215. @Override
  216. public CommonResult delete(int permissionSettingId, int id) {
  217. PermissionSetting permissionSetting = permissionSettingService.getById(permissionSettingId);
  218. if (ObjectUtils.isEmpty(permissionSetting)) {
  219. return CommonResult.fail("非法进入");
  220. }
  221. // 判断该用户是否拥有权限
  222. // 判断该用户是否拥有权限
  223. String accountManagement = permissionSetting.getAccountManagement();
  224. if (!"0".equals(accountManagement) && !accountManagement.contains("3")) {
  225. return CommonResult.fail("此账号暂无该权限");
  226. }
  227. boolean b = adminService.removeById(id);
  228. if (b) {
  229. return CommonResult.ok();
  230. }
  231. return CommonResult.fail();
  232. }
  233. @Override
  234. public CommonResult getUsersPage(UsersPageDto usersPageDto) {
  235. int size = usersPageDto.getSize();
  236. Integer page = usersPageDto.getPage();
  237. Integer permissionSettingId = usersPageDto.getPermissionSettingId();
  238. String keyWord = usersPageDto.getKeyWord();
  239. if (ObjectUtils.isEmpty(page) && page <= 0) {
  240. page = 1;
  241. }
  242. if (ObjectUtils.isEmpty(size) && size <= 0) {
  243. size = 10;
  244. }
  245. PermissionSetting permissionSetting = permissionSettingService.getById(permissionSettingId);
  246. if (ObjectUtils.isEmpty(permissionSetting)) {
  247. return CommonResult.fail("非法进入");
  248. }
  249. // 判断该用户是否拥有权限
  250. // 判断该用户是否拥有权限
  251. // String accountManagement = permissionSetting.getAccountManagement();
  252. // if (!"0".equals(accountManagement) && !accountManagement.contains("1")) {
  253. // return CommonResult.fail("此账号暂无该权限");
  254. // }
  255. LambdaQueryWrapper<Users> wrapperU = new LambdaQueryWrapper<>();
  256. wrapperU.like(ObjectUtils.isNotEmpty(keyWord), Users::getCardNumber, keyWord)
  257. .or()
  258. .like(ObjectUtils.isNotEmpty(keyWord), Users::getUserName, keyWord);
  259. IPage<Users> page1 = usersService.page(new Page<>(page, size), wrapperU);
  260. IPage<UsersVo> page2 = new Page<>();
  261. BeanUtils.copyProperties(page1,page2);
  262. List<Users> records = page1.getRecords();
  263. ArrayList<UsersVo> usersVos = new ArrayList<>();
  264. for (Users record : records) {
  265. UsersVo usersVo = new UsersVo();
  266. BeanUtils.copyProperties(record,usersVo);
  267. String department = record.getDepartment();
  268. if ("0".equals(department)) {
  269. usersVo.setDepartmentName("其他");
  270. }else if ("1".equals(department)){
  271. usersVo.setDepartmentName("学生");
  272. }else if ("4".equals(department)){
  273. usersVo.setDepartmentName("教职工");
  274. }else if ("5".equals(department)){
  275. usersVo.setDepartmentName("校友");
  276. }else if ("6".equals(department)){
  277. usersVo.setDepartmentName("访客");
  278. }else if ("7".equals(department)){
  279. usersVo.setDepartmentName("临时人员");
  280. }
  281. usersVos.add(usersVo);
  282. }
  283. page2.setRecords(usersVos);
  284. return CommonResult.ok(page2);
  285. }
  286. @Override
  287. public CommonResult userList(String adminMenuId) {
  288. LambdaQueryWrapper<Admin> wrapper=new LambdaQueryWrapper<>();
  289. wrapper.eq(Admin::getAdminMenuId,adminMenuId);
  290. List<Admin> list = adminService.list(wrapper);
  291. return CommonResult.ok(list);
  292. }
  293. }