SmartIdentityController.java 6.4 KB

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