SmartIdentityController.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package com.template.controller;
  2. import com.template.annotation.DESRespondSecret;
  3. import com.template.api.SmartIdentityControllerAPI;
  4. import com.template.common.utils.paramUtils;
  5. import com.template.model.pojo.SmartApply;
  6. import com.template.model.pojo.SmartIdentity;
  7. import com.template.model.pojo.SmartScreenshot;
  8. import com.template.model.request.insertIdentityRequest;
  9. import com.template.model.request.updateIdentityRequest;
  10. import com.template.model.result.CommonResult;
  11. import com.template.model.result.PageUtils;
  12. import com.template.model.vo.ApplysVo;
  13. import com.template.model.vo.IdentityApplyVo;
  14. import com.template.model.vo.SemesterVo;
  15. import com.template.services.SmartApplyService;
  16. import com.template.services.SmartIdentityService;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.validation.BindingResult;
  20. import org.springframework.web.bind.annotation.RestController;
  21. import java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.List;
  24. import java.util.Optional;
  25. import java.util.stream.Collectors;
  26. /**
  27. * <p>
  28. * 前端控制器
  29. * </p>
  30. *
  31. * @author ceshi
  32. * @since 2023-12-04
  33. */
  34. @RestController
  35. //返回参数加密注解
  36. @DESRespondSecret
  37. public class SmartIdentityController implements SmartIdentityControllerAPI {
  38. @Autowired
  39. private SmartApplyService smartApplyService;
  40. @Autowired
  41. private SmartIdentityService smartIdentityService;
  42. /**
  43. * 新增身份
  44. *
  45. * @param smartIdentity 身份数据
  46. * @param bindingResult
  47. * @return
  48. */
  49. @Override
  50. @DESRespondSecret(validated = true)
  51. public CommonResult insertSmartIdentity(insertIdentityRequest smartIdentity, BindingResult bindingResult) {
  52. if (bindingResult.hasErrors()) {
  53. String st = paramUtils.getParamError(bindingResult);
  54. return CommonResult.fail(st);
  55. }
  56. SmartIdentity data = smartIdentityService.queryIdentityByName(smartIdentity.getName());
  57. if (data != null) {
  58. return CommonResult.fail("该身份已存在");
  59. }
  60. SmartIdentity identity = new SmartIdentity();
  61. identity.setName(smartIdentity.getName());
  62. identity.setApplyId(StringUtils.join(smartIdentity.getApplyIds(), ','));
  63. int result = smartIdentityService.insertSmartIdentity(identity);
  64. return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败");
  65. }
  66. /**
  67. * 更新身份
  68. *
  69. * @param sa 身份数据
  70. * @param bindingResult
  71. * @return
  72. */
  73. @Override
  74. @DESRespondSecret(validated = true)
  75. public CommonResult updateSmartIdentityById(updateIdentityRequest sa, BindingResult bindingResult) {
  76. if (bindingResult.hasErrors()) {
  77. String st = paramUtils.getParamError(bindingResult);
  78. return CommonResult.fail(st);
  79. }
  80. SmartIdentity oldData = smartIdentityService.getSmartById(sa.getId());
  81. if (oldData == null) {
  82. return CommonResult.fail("身份数据无效,修改失败");
  83. }
  84. if (sa.getName() != null && !sa.getName().equals(oldData.getName())) {
  85. //查看是否存在相同身份的数据
  86. SmartIdentity data = smartIdentityService.queryIdentityByName(sa.getName());
  87. if (data != null) {
  88. return CommonResult.fail("该身份已存在");
  89. }
  90. }
  91. SmartIdentity identity = new SmartIdentity();
  92. identity.setId(sa.getId());
  93. identity.setName(sa.getName());
  94. identity.setApplyId(StringUtils.join(sa.getApplyIds(), ','));
  95. int result = smartIdentityService.updateSmartIdentity(identity);
  96. return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
  97. }
  98. /**
  99. * 身份分页数据查询
  100. *
  101. * @param currentPage 当前页数
  102. * @param pageCount 一页数据条数
  103. * @param name 查询名称
  104. * @return
  105. */
  106. @Override
  107. @DESRespondSecret(validated = true)
  108. public CommonResult queryPageSmartIdentitys(int currentPage, int pageCount, String name) {
  109. PageUtils<SmartIdentity> result = smartIdentityService.queryPageSmartIdentitys(currentPage, pageCount, name);
  110. for (SmartIdentity data : result.getList()) {
  111. List<String> lists = Arrays.asList(data.getApplyId().split(","));
  112. data.setApplyIds(lists);
  113. }
  114. return CommonResult.ok(result);
  115. }
  116. @Override
  117. @DESRespondSecret(validated = true)
  118. public CommonResult deleteSmartIdentityById(int id) {
  119. SmartIdentity data = smartIdentityService.getSmartById(id);
  120. if (data == null) {
  121. return CommonResult.fail("当前数据不存在,删除失败!");
  122. }
  123. int result = smartIdentityService.deleteSmartIdentityById(id);
  124. return result > 0 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败");
  125. }
  126. @Override
  127. @DESRespondSecret(validated = true)
  128. public CommonResult querySmartIdentitys() {
  129. List<SmartIdentity> datas = smartIdentityService.list(null);
  130. List<SemesterVo> result = new ArrayList<>();
  131. for (SmartIdentity data : datas) {
  132. SemesterVo model = new SemesterVo();
  133. model.setId(data.getId());
  134. model.setName(data.getName());
  135. result.add(model);
  136. }
  137. return CommonResult.ok(result);
  138. }
  139. @Override
  140. @DESRespondSecret(validated = true)
  141. public CommonResult queryIdentityApplyById(int id) {
  142. List<ApplysVo> result = new ArrayList<>();
  143. SmartIdentity identity = smartIdentityService.getSmartById(id);
  144. if (identity == null) {
  145. return CommonResult.fail("当前身份不合法,无应用权限");
  146. }
  147. if (identity.getApplyId() == null) {
  148. return CommonResult.fail("当前身份信息无应用权限");
  149. }
  150. List<SmartApply> applys = smartApplyService.queryAppletApplys();
  151. if (applys == null) {
  152. return CommonResult.fail("应用管理数据为空");
  153. }
  154. List<String> applyIds = Arrays.asList(identity.getApplyId().split(","));
  155. for (String applyId : applyIds) {
  156. Optional<SmartApply> applyData = applys.stream().filter(e -> e.getId().equals(Integer.valueOf(applyId))).findFirst();
  157. if (applyData != null && applyData.isPresent()) {
  158. ApplysVo data = new ApplysVo();
  159. data.setId(applyData.get().getId());
  160. data.setPath(applyData.get().getRoute());
  161. data.setTitle(applyData.get().getName());
  162. data.setUrl(applyData.get().getLogo());
  163. data.setAppid(applyData.get().getAppid());
  164. data.setCategory(applyData.get().getCategory());
  165. result.add(data);
  166. }
  167. }
  168. return CommonResult.ok(result);
  169. }
  170. }