RepairAdminController.java 6.3 KB

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