FaceManagementController.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package com.xjrsoft.module.personnel.controller;
  2. import cn.dev33.satoken.annotation.SaCheckPermission;
  3. import cn.dev33.satoken.stp.StpUtil;
  4. import cn.hutool.core.bean.BeanUtil;
  5. import cn.hutool.core.util.ObjectUtil;
  6. import cn.hutool.core.util.StrUtil;
  7. import com.baomidou.mybatisplus.core.metadata.IPage;
  8. import com.baomidou.mybatisplus.core.toolkit.StringPool;
  9. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  10. import com.github.yulichang.wrapper.MPJLambdaWrapper;
  11. import com.xjrsoft.common.enums.DeleteMark;
  12. import com.xjrsoft.common.enums.EnabledMark;
  13. import com.xjrsoft.common.exception.MyException;
  14. import com.xjrsoft.common.model.result.RT;
  15. import com.xjrsoft.common.page.ConventPage;
  16. import com.xjrsoft.common.page.PageOutput;
  17. import com.xjrsoft.common.utils.ImageUtil;
  18. import com.xjrsoft.common.utils.VoToColumnUtil;
  19. import com.xjrsoft.module.personnel.dto.AddFaceManagementDto;
  20. import com.xjrsoft.module.personnel.dto.FaceManagementPageDto;
  21. import com.xjrsoft.module.personnel.dto.UpdateFaceManagementDto;
  22. import com.xjrsoft.module.personnel.dto.UpdateFaceManagementStatusDto;
  23. import com.xjrsoft.module.personnel.entity.FaceManagement;
  24. import com.xjrsoft.module.personnel.entity.StundentFaceProcess;
  25. import com.xjrsoft.module.personnel.entity.TeacherFaceProcess;
  26. import com.xjrsoft.module.personnel.service.IFaceManagementService;
  27. import com.xjrsoft.module.personnel.service.IStundentFaceProcessService;
  28. import com.xjrsoft.module.personnel.service.ITeacherFaceProcessService;
  29. import com.xjrsoft.module.personnel.vo.FaceManagementVo;
  30. import com.xjrsoft.module.system.service.ICodeRuleService;
  31. import io.swagger.annotations.Api;
  32. import io.swagger.annotations.ApiOperation;
  33. import lombok.AllArgsConstructor;
  34. import org.apache.commons.lang3.StringUtils;
  35. import org.jetbrains.annotations.NotNull;
  36. import org.springframework.web.bind.annotation.DeleteMapping;
  37. import org.springframework.web.bind.annotation.GetMapping;
  38. import org.springframework.web.bind.annotation.PostMapping;
  39. import org.springframework.web.bind.annotation.PutMapping;
  40. import org.springframework.web.bind.annotation.RequestBody;
  41. import org.springframework.web.bind.annotation.RequestMapping;
  42. import org.springframework.web.bind.annotation.RequestParam;
  43. import org.springframework.web.bind.annotation.RestController;
  44. import org.springframework.web.multipart.MultipartFile;
  45. import javax.validation.Valid;
  46. import java.io.IOException;
  47. import java.util.Arrays;
  48. import java.util.HashMap;
  49. import java.util.List;
  50. import java.util.Map;
  51. @RestController
  52. @RequestMapping("/personnel" + "/facemanager")
  53. @Api(value = "/personnel" + "/facemanager", tags = "人脸管理")
  54. @AllArgsConstructor
  55. public class FaceManagementController {
  56. private final IFaceManagementService faceManagementService;
  57. private ICodeRuleService codeRuleService;
  58. private final ITeacherFaceProcessService teacherFaceProcessService;
  59. private final IStundentFaceProcessService stundentFaceProcessService;
  60. @GetMapping(value = "/page")
  61. @ApiOperation(value = "人脸列表(分页)")
  62. @SaCheckPermission("facemanager:detail")
  63. public RT<PageOutput<FaceManagementVo>> Page(@Valid FaceManagementPageDto dto) {
  64. MPJLambdaWrapper<FaceManagement> queryWrapper = new MPJLambdaWrapper<>();
  65. queryWrapper
  66. .like(StrUtil.isNotBlank(dto.getFaceCode()), FaceManagement::getFaceCode, dto.getFaceCode())
  67. .like(ObjectUtil.isNotNull(dto.getName()), FaceManagement::getName, dto.getName())
  68. .select(FaceManagement.class, x -> VoToColumnUtil.fieldsToColumns(FaceManagementVo.class).contains(x.getProperty()))
  69. .select(FaceManagement::getId);
  70. IPage<FaceManagement> page = faceManagementService.page(ConventPage.getPage(dto), queryWrapper);
  71. PageOutput<FaceManagementVo> pageOutput = ConventPage.getPageOutput(page, FaceManagementVo.class);
  72. return RT.ok(pageOutput);
  73. }
  74. @GetMapping(value = "/info")
  75. @ApiOperation(value = "根据id查询人脸信息")
  76. @SaCheckPermission("facemanager:detail")
  77. public RT<FaceManagementVo> info(@RequestParam Long id) {
  78. FaceManagement faceManagement = faceManagementService.getByIdDeep(id);
  79. if (faceManagement == null) {
  80. return RT.error("找不到此数据!");
  81. }
  82. return RT.ok(BeanUtil.toBean(faceManagement, FaceManagementVo.class));
  83. }
  84. @GetMapping(value = "/detail")
  85. @ApiOperation(value = "根据当前用户查询人脸信息")
  86. @SaCheckPermission("facemanager:detail")
  87. public RT<FaceManagementVo> detail() {
  88. FaceManagement faceManagement = faceManagementService.getOneDeep(Wrappers.lambdaQuery(FaceManagement.class).eq(FaceManagement::getDeleteMark, DeleteMark.NODELETE.getCode()).eq(FaceManagement::getUserId, StpUtil.getLoginIdAsLong()));
  89. if (faceManagement == null) {
  90. return RT.error("找不到此数据!");
  91. }
  92. faceManagement.setVerifyStatus(1);
  93. if(faceManagement.getUserType()==1) {
  94. // 查询 teacher_face_process 审核通过
  95. MPJLambdaWrapper<TeacherFaceProcess> queryWrapper = new MPJLambdaWrapper<>();
  96. queryWrapper.eq(TeacherFaceProcess::getUserId, faceManagement.getUserId())
  97. .eq(TeacherFaceProcess::getFacePhoto, faceManagement.getFileId());
  98. if (teacherFaceProcessService.count(queryWrapper) > 0) {
  99. faceManagement.setVerifyStatus(2);
  100. }
  101. }
  102. if(faceManagement.getUserType()==2){
  103. // 查询 teacher_face_process 审核通过
  104. MPJLambdaWrapper<StundentFaceProcess> queryWrapper = new MPJLambdaWrapper<>();
  105. queryWrapper.eq(StundentFaceProcess::getUserId, faceManagement.getUserId())
  106. .eq(StundentFaceProcess::getFacePhoto, faceManagement.getFileId());
  107. if (stundentFaceProcessService.count(queryWrapper) > 0) {
  108. faceManagement.setVerifyStatus(2);
  109. }
  110. }
  111. return RT.ok(BeanUtil.toBean(faceManagement, FaceManagementVo.class));
  112. }
  113. @PostMapping(value = "/add")
  114. @ApiOperation(value = "新增人脸")
  115. @SaCheckPermission("facemanager:add")
  116. public RT<Boolean> add(AddFaceManagementDto dto, @RequestParam("file") MultipartFile file) {
  117. FaceManagement faceManagement = BeanUtil.toBean(dto, FaceManagement.class);
  118. try {
  119. faceManagement.setRegisterBase64(ImageHandler(file));
  120. } catch (Exception e) {
  121. return RT.error(e.getMessage());
  122. }
  123. boolean isSuccess = faceManagementService.add(faceManagement);
  124. codeRuleService.useEncode("FACECODE");
  125. return RT.ok(isSuccess);
  126. }
  127. @PostMapping(value = "/update")
  128. @ApiOperation(value = "修改人脸")
  129. @SaCheckPermission("facemanager:edit")
  130. public RT<Boolean> update(UpdateFaceManagementDto dto, @RequestParam(name = "file",required = false) MultipartFile file) {
  131. FaceManagement faceManagement = BeanUtil.toBean(dto, FaceManagement.class);
  132. if (file != null) {
  133. try {
  134. faceManagement.setRegisterBase64(ImageHandler(file));
  135. } catch (Exception e) {
  136. return RT.error(e.getMessage());
  137. }
  138. }
  139. return RT.ok(faceManagementService.update(faceManagement));
  140. }
  141. @PutMapping("/status")
  142. @ApiOperation(value = "修改人脸状态")
  143. @SaCheckPermission("facemanager:edit-status")
  144. public RT<Boolean> updateEnabled(@Valid @RequestBody UpdateFaceManagementStatusDto dto) {
  145. //根据id修改表单enabledMark
  146. return RT.ok(faceManagementService.update(Wrappers.<FaceManagement>update().lambda().set(FaceManagement::getEnabledMark, dto.getEnabledMark()).eq(FaceManagement::getId, dto.getId())));
  147. }
  148. @DeleteMapping
  149. @ApiOperation(value = "删除")
  150. @SaCheckPermission("facemanager:delete")
  151. public RT<Boolean> delete(@Valid @RequestBody List<Long> ids) {
  152. return RT.ok(faceManagementService.delete(ids));
  153. }
  154. @DeleteMapping("/delete")
  155. @ApiOperation(value = "删除")
  156. @SaCheckPermission("facemanager:delete-current")
  157. public RT<Boolean> delete() {
  158. return RT.ok(faceManagementService.remove(Wrappers.<FaceManagement>query().lambda().eq(FaceManagement::getUserId,StpUtil.getLoginIdAsLong())));
  159. }
  160. // 图片处理
  161. public String ImageHandler(@NotNull MultipartFile file) throws IOException {
  162. String filename = file.getOriginalFilename();
  163. String suffix = StringUtils.substringAfterLast(filename, StringPool.DOT);
  164. String[] imgSuffix = new String[]{"png", "jpg", "jpeg"};
  165. if (!Arrays.asList(imgSuffix).contains(suffix)) {
  166. throw new MyException("图片格式不正确!");
  167. }
  168. Map<String, String> map = new HashMap<String, String>() {
  169. {
  170. put("jpg", "data:image/jpg;base64,");
  171. put("jpeg", "data:image/jpeg;base64,");
  172. put("png", "data:image/png;base64,");
  173. }
  174. };
  175. Long maxSize = 1048576L; // 最大文件1M
  176. byte[] resultImg = ImageUtil.compressUnderSize(file.getBytes(), maxSize);
  177. return map.get(suffix) + ImageUtil.bytesEncode2Base64(resultImg);
  178. }
  179. }