RepairAdminController.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package com.template.controller;
  2. import com.template.api.RepairAdminControllerAPI;
  3. import com.template.common.utils.AesUtils;
  4. import com.template.common.utils.paramUtils;
  5. import com.template.model.enumModel.eIsSuper;
  6. import com.template.model.enumModel.eStatu;
  7. import com.template.model.pojo.RepairAdmin;
  8. import com.template.model.result.CommonResult;
  9. import com.template.model.result.PageUtils;
  10. import com.template.services.RepairAdminService;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.validation.BindingResult;
  13. import org.springframework.web.bind.annotation.*;
  14. /**
  15. * <p>
  16. * 前端控制器
  17. * </p>
  18. *
  19. * @author ceshi
  20. * @since 2023-07-05
  21. */
  22. @RestController
  23. public class RepairAdminController implements RepairAdminControllerAPI {
  24. @Autowired
  25. private RepairAdminService repairAdminService;
  26. /**
  27. * 添加账号数据
  28. *
  29. * @param ra account 账号
  30. * password 密码
  31. * username 用户名称
  32. * phone 手机号
  33. * @return
  34. */
  35. @Override
  36. public CommonResult InsertRepairAdmin(@RequestBody RepairAdmin ra, BindingResult bindingResult) {
  37. if (bindingResult.hasErrors()) {
  38. String st = paramUtils.getParamError(bindingResult);
  39. return CommonResult.fail(st);
  40. }
  41. int result = 0; // 帮我们自动生成id
  42. try {
  43. result = repairAdminService.insertRepairAdmin(ra);
  44. } catch (Exception e) {
  45. if (e.getCause().getMessage().contains("'repair_admin.account_un'")) {
  46. return CommonResult.fail("该账号已存在!");
  47. }
  48. return CommonResult.fail("系统异常,添加失败!");
  49. }
  50. return result > 0 ? CommonResult.ok() : CommonResult.fail();
  51. }
  52. /**
  53. * 查询账户列表数据
  54. *
  55. * @param currentPage 当前页
  56. * @param pageCount 一页数据条数
  57. * @param account 账号
  58. * @param phone 手机号
  59. * @param userName 账号名称
  60. * @return
  61. */
  62. @Override
  63. public CommonResult queryPageRepairAdmins(@RequestParam int currentPage, @RequestParam int pageCount, String account, String phone, String userName) {
  64. PageUtils<RepairAdmin> result = repairAdminService.queryPageList(currentPage, pageCount, account, phone, userName);
  65. return CommonResult.ok(result);
  66. }
  67. /**
  68. * 根据ID冻结账号账号数据
  69. *
  70. * @param frar id 数据ID
  71. * statu 状态
  72. * 正常:0;冻结:1
  73. * @return
  74. */
  75. @Override
  76. public CommonResult freezeRepairAdminById(@RequestBody com.repair.model.request.freezeRepairAdminRequest frar, BindingResult bindingResult) {
  77. if (bindingResult.hasErrors()) {
  78. String st = paramUtils.getParamError(bindingResult);
  79. return CommonResult.fail(st);
  80. }
  81. RepairAdmin data = repairAdminService.getRepairById(frar.getId());
  82. if (data == null) {
  83. return CommonResult.fail("当前数据不存在,解冻失败!");
  84. }
  85. if (data.getStatu() == frar.getStatu()) {
  86. String Message = frar.getStatu() == eStatu.Freeze.getValue() ? "当前处于冻结状态,请勿重复操作!" : "当前处于未冻结状态,请勿重复操作!";
  87. return CommonResult.fail(Message);
  88. }
  89. String success = frar.getStatu() == eStatu.Freeze.getValue() ? "冻结成功!" : "解冻成功";
  90. String fail = frar.getStatu() == eStatu.Freeze.getValue() ? "冻结失败!" : "解冻失败";
  91. RepairAdmin ra = new RepairAdmin();
  92. ra.setId(frar.getId());
  93. ra.setStatu(frar.getStatu());
  94. int result = repairAdminService.updateRepairAdmin(ra);
  95. System.out.println(result);
  96. return result > 0 ? CommonResult.ok(success) : CommonResult.fail(fail);
  97. }
  98. /**
  99. * 根据ID删除账号数据
  100. *
  101. * @param id id 数据ID
  102. * @return
  103. */
  104. @Override
  105. public CommonResult deleteRepairAdminById(@RequestParam String id) {
  106. RepairAdmin data = repairAdminService.getRepairById(id);
  107. if (data == null) {
  108. return CommonResult.fail("当前数据不存在,删除失败!");
  109. }
  110. int result = repairAdminService.deleteRepairAdminById(id);
  111. return result > 0 ? CommonResult.ok() : CommonResult.fail();
  112. }
  113. /**
  114. * 编辑账号
  115. *
  116. * @param ra id 数据ID
  117. * username 账号名称
  118. * phone 手机号
  119. * password 密码
  120. * @return
  121. */
  122. @Override
  123. public CommonResult updateRepairAdminById(com.repair.model.request.updateRepairAdminRequest ra, @RequestHeader("user_head") String userhead, BindingResult bindingResult) {
  124. if (bindingResult.hasErrors()) {
  125. String st = paramUtils.getParamError(bindingResult);
  126. return CommonResult.fail(st);
  127. }
  128. RepairAdmin data = repairAdminService.getRepairById(ra.getId());
  129. if (data == null) {
  130. return CommonResult.fail("当前数据不存在,编辑失败!");
  131. }
  132. //只有操作管理员才能编辑他人的密码
  133. String userID = AesUtils.decrypt(userhead);
  134. if (ra.getPassword() != null && !userID.equals(ra.getId())) {
  135. RepairAdmin operateData = repairAdminService.getRepairById(userID);
  136. if(operateData == null){
  137. return CommonResult.fail("操作身份不合法,编辑失败!");
  138. }
  139. if(!data.getPassword().equals(AesUtils.encrypt(ra.getPassword())) && operateData.getIsSuper() != eIsSuper.Super.getValue()){
  140. return CommonResult.fail("只有超级管理员才能更改密码!");
  141. }
  142. }
  143. int result = repairAdminService.updateRepaiRadmin(ra);
  144. return result > 0 ? CommonResult.ok("编辑成功") : CommonResult.fail("编辑失败");
  145. }
  146. }