StundentFaceProcessController.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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.IdUtil;
  6. import cn.hutool.core.util.ObjectUtil;
  7. import cn.hutool.core.util.StrUtil;
  8. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  9. import com.baomidou.mybatisplus.core.metadata.IPage;
  10. import com.baomidou.mybatisplus.core.toolkit.IdWorker;
  11. import com.baomidou.mybatisplus.core.toolkit.StringPool;
  12. import com.github.yulichang.toolkit.MPJWrappers;
  13. import com.github.yulichang.wrapper.MPJLambdaWrapper;
  14. import com.google.gson.JsonArray;
  15. import com.google.gson.JsonObject;
  16. import com.google.gson.JsonParser;
  17. import com.xjrsoft.common.enums.DeleteMark;
  18. import com.xjrsoft.common.model.result.RT;
  19. import com.xjrsoft.common.page.ConventPage;
  20. import com.xjrsoft.common.page.PageOutput;
  21. import com.xjrsoft.common.utils.FileZipUtil;
  22. import com.xjrsoft.common.utils.ImageUtil;
  23. import com.xjrsoft.common.utils.UploadUtil;
  24. import com.xjrsoft.common.utils.VoToColumnUtil;
  25. import com.xjrsoft.module.base.entity.BaseClass;
  26. import com.xjrsoft.module.base.service.IBaseClassService;
  27. import com.xjrsoft.module.base.vo.StudentClassVo;
  28. import com.xjrsoft.module.hikvision.mapper.HikvisionDataMapper;
  29. import com.xjrsoft.module.hikvision.util.ApiUtil;
  30. import com.xjrsoft.module.personnel.dto.AddStundentFaceProcessDto;
  31. import com.xjrsoft.module.personnel.dto.StundentFaceProcessPageDto;
  32. import com.xjrsoft.module.personnel.dto.UpdateStundentFaceProcessDto;
  33. import com.xjrsoft.module.personnel.entity.FaceManagement;
  34. import com.xjrsoft.module.personnel.entity.StundentFaceProcess;
  35. import com.xjrsoft.module.personnel.entity.TeacherFaceProcess;
  36. import com.xjrsoft.module.personnel.service.IFaceManagementService;
  37. import com.xjrsoft.module.personnel.service.IStundentFaceProcessService;
  38. import com.xjrsoft.module.personnel.vo.StundentFaceProcessPageVo;
  39. import com.xjrsoft.module.personnel.vo.StundentFaceProcessVo;
  40. import com.xjrsoft.module.student.entity.BaseStudent;
  41. import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
  42. import com.xjrsoft.module.student.entity.BaseStudentUser;
  43. import com.xjrsoft.module.student.service.IStudentManagerService;
  44. import com.xjrsoft.module.system.entity.DictionaryDetail;
  45. import com.xjrsoft.module.system.entity.File;
  46. import com.xjrsoft.module.system.service.IFileService;
  47. import com.xjrsoft.module.teacher.entity.XjrUser;
  48. import io.swagger.annotations.Api;
  49. import io.swagger.annotations.ApiOperation;
  50. import lombok.AllArgsConstructor;
  51. import org.apache.commons.lang3.StringUtils;
  52. import org.springframework.web.bind.annotation.DeleteMapping;
  53. import org.springframework.web.bind.annotation.GetMapping;
  54. import org.springframework.web.bind.annotation.PostMapping;
  55. import org.springframework.web.bind.annotation.PutMapping;
  56. import org.springframework.web.bind.annotation.RequestBody;
  57. import org.springframework.web.bind.annotation.RequestMapping;
  58. import org.springframework.web.bind.annotation.RequestParam;
  59. import org.springframework.web.bind.annotation.RestController;
  60. import org.springframework.web.multipart.MultipartFile;
  61. import javax.validation.Valid;
  62. import java.io.ByteArrayOutputStream;
  63. import java.io.InputStream;
  64. import java.time.LocalDateTime;
  65. import java.util.Base64;
  66. import java.util.Date;
  67. import java.util.Enumeration;
  68. import java.util.HashMap;
  69. import java.util.List;
  70. import java.util.Map;
  71. import java.util.zip.ZipEntry;
  72. import java.util.zip.ZipFile;
  73. /**
  74. * @title: 学生人脸信息审核
  75. * @Author dzx
  76. * @Date: 2024-05-10
  77. * @Version 1.0
  78. */
  79. @RestController
  80. @RequestMapping("/personnel" + "/stundentFaceProcess")
  81. @Api(value = "/personnel" + "/stundentFaceProcess",tags = "学生人脸信息审核代码")
  82. @AllArgsConstructor
  83. public class StundentFaceProcessController {
  84. private final IStundentFaceProcessService stundentFaceProcessService;
  85. private final IStudentManagerService studentManagerService;
  86. private final IFileService fileService;
  87. private final IFaceManagementService faceManagementService;
  88. private final IBaseClassService classService;
  89. private final HikvisionDataMapper hikvisionDataMapper;
  90. @GetMapping(value = "/page")
  91. @ApiOperation(value="学生人脸信息审核列表(分页)")
  92. @SaCheckPermission("stundentfaceprocess:detail")
  93. public RT<PageOutput<StundentFaceProcessPageVo>> page(@Valid StundentFaceProcessPageDto dto){
  94. if(ObjectUtil.isNull(dto.getStatus())){
  95. dto.setStatus(1);
  96. }
  97. IPage<StundentFaceProcessPageVo> page=stundentFaceProcessService.selectJoinListPage(ConventPage.getPage(dto),StundentFaceProcessPageVo.class,
  98. MPJWrappers.<StundentFaceProcess>lambdaJoin()
  99. .orderByDesc(StundentFaceProcess::getId)
  100. .disableSubLogicDel()
  101. .like(StrUtil.isNotBlank(dto.getName()), XjrUser::getName,dto.getName())
  102. .eq(StrUtil.isNotBlank(dto.getStduyStatus()),BaseStudentSchoolRoll::getStduyStatus,dto.getStduyStatus())
  103. .like(StrUtil.isNotBlank(dto.getMobile()),XjrUser::getMobile,dto.getMobile())
  104. .like(StrUtil.isNotBlank(dto.getStudentId()),BaseStudent::getStudentId,dto.getStudentId())
  105. .eq(ObjectUtil.isNotNull(dto.getClassId())&&dto.getClassId()!=0,StundentFaceProcess::getClassId,dto.getClassId())
  106. .eq(StundentFaceProcess::getExamStatus,dto.getStatus())
  107. .select(StundentFaceProcess::getId,StundentFaceProcess::getUserId,StundentFaceProcess::getClassId,StundentFaceProcess::getFacePhoto)
  108. .innerJoin(XjrUser.class,XjrUser::getId, StundentFaceProcess::getUserId)
  109. .leftJoin(File.class,File::getFolderId,StundentFaceProcess::getFacePhoto)
  110. .leftJoin(FaceManagement.class,FaceManagement::getFileId, StundentFaceProcess::getFacePhoto)
  111. .leftJoin(DictionaryDetail.class,DictionaryDetail::getCode,StundentFaceProcess::getGender, ext->ext.selectAs(DictionaryDetail::getName, StundentFaceProcessPageVo::getGender))
  112. .leftJoin(BaseStudentSchoolRoll.class,BaseStudentSchoolRoll::getUserId,StundentFaceProcess::getUserId)
  113. .leftJoin(DictionaryDetail.class,DictionaryDetail::getCode,BaseStudentSchoolRoll::getStduyStatus, ext->ext.selectAs(DictionaryDetail::getName, StundentFaceProcessPageVo::getStduyStatus))
  114. .leftJoin(BaseClass.class,BaseClass::getId,StundentFaceProcess::getClassId,ext->ext.selectAs(BaseClass::getName,StundentFaceProcessPageVo::getClassCn))
  115. .leftJoin(XjrUser.class,XjrUser::getId,StundentFaceProcess::getTeacherId, ext->ext.selectAs(XjrUser::getName, StundentFaceProcessPageVo::getTeacherCn))
  116. .selectAs(XjrUser::getName,StundentFaceProcess::getName)
  117. .select("t2.file_url AS face_photo_url,t1.user_name AS user_name,t1.mobile")
  118. .leftJoin(BaseStudent.class,BaseStudent::getUserId,StundentFaceProcess::getUserId, ext->ext.selectAs(BaseStudent::getStudentId, StundentFaceProcessPageVo::getStudentId))
  119. );
  120. PageOutput<StundentFaceProcessPageVo> pageOutput = ConventPage.getPageOutput(page, StundentFaceProcessPageVo.class);
  121. return RT.ok(pageOutput);
  122. }
  123. @GetMapping(value = "/info")
  124. @ApiOperation(value="根据id查询学生人脸信息审核信息")
  125. @SaCheckPermission("stundentfaceprocess:detail")
  126. public RT<StundentFaceProcessVo> info(@RequestParam Long id){
  127. StundentFaceProcess stundentFaceProcess = stundentFaceProcessService.getById(id);
  128. if (stundentFaceProcess == null) {
  129. return RT.error("找不到此数据!");
  130. }
  131. return RT.ok(BeanUtil.toBean(stundentFaceProcess, StundentFaceProcessVo.class));
  132. }
  133. @PostMapping
  134. @ApiOperation(value = "新增学生人脸信息审核")
  135. @SaCheckPermission("stundentfaceprocess:add")
  136. public RT<Boolean> add(@Valid @RequestBody AddStundentFaceProcessDto dto){
  137. StundentFaceProcess stundentFaceProcess = BeanUtil.toBean(dto, StundentFaceProcess.class);
  138. boolean isSuccess = stundentFaceProcessService.save(stundentFaceProcess);
  139. return RT.ok(isSuccess);
  140. }
  141. @PutMapping
  142. @ApiOperation(value = "修改学生人脸信息审核")
  143. @SaCheckPermission("stundentfaceprocess:edit")
  144. public RT<Boolean> update(@Valid @RequestBody UpdateStundentFaceProcessDto dto){
  145. StundentFaceProcess stundentFaceProcess = BeanUtil.toBean(dto, StundentFaceProcess.class);
  146. return RT.ok(stundentFaceProcessService.updateById(stundentFaceProcess));
  147. }
  148. @DeleteMapping
  149. @ApiOperation(value = "删除学生人脸信息审核")
  150. @SaCheckPermission("stundentfaceprocess:delete")
  151. public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
  152. return RT.ok(stundentFaceProcessService.removeByIds(ids));
  153. }
  154. @PostMapping(value = "/batch-import")
  155. @ApiOperation(value = "批量新增学生人脸")
  156. @SaCheckPermission("stundentfaceprocess:batch-upload")
  157. public RT<Boolean> batchUpload(@RequestParam("file") MultipartFile file) throws Exception {
  158. List<BaseStudentUser> list = studentManagerService.list(
  159. new MPJLambdaWrapper<BaseStudentUser>().distinct()
  160. .select(BaseStudentUser::getId)
  161. .select(BaseStudentUser.class, user -> !user.getColumn().equals("avatar"))
  162. .select(BaseStudentUser.class, x -> VoToColumnUtil.fieldsToColumns(BaseStudentUser.class).contains(x.getProperty()))
  163. .leftJoin(BaseStudent.class, BaseStudent::getUserId, BaseStudentUser::getId)
  164. .eq(BaseStudent::getDeleteMark, DeleteMark.NODELETE.getCode())
  165. .eq(BaseStudentUser::getDeleteMark, DeleteMark.NODELETE.getCode())
  166. );
  167. Map<String, BaseStudentUser> studentMap = new HashMap<>();
  168. for (BaseStudentUser baseStudentUser : list) {
  169. studentMap.put(baseStudentUser.getCredentialNumber(), baseStudentUser);
  170. }
  171. List<StudentClassVo> studentClass = classService.getStudentClass();
  172. Map<Long, StudentClassVo> studentClassMap = new HashMap<>();
  173. for (StudentClassVo classVo : studentClass) {
  174. studentClassMap.put(classVo.getUserId(), classVo);
  175. }
  176. List<StundentFaceProcess> faceList = stundentFaceProcessService.list(
  177. new QueryWrapper<StundentFaceProcess>().lambda()
  178. .eq(StundentFaceProcess::getDeleteMark, DeleteMark.NODELETE.getCode())
  179. );
  180. Map<Long, StundentFaceProcess> faceMap = new HashMap<>();
  181. for (StundentFaceProcess faceProcess : faceList) {
  182. faceMap.put(faceProcess.getUserId(), faceProcess);
  183. }
  184. List<FaceManagement> faceManagementList = faceManagementService.list(
  185. new QueryWrapper<FaceManagement>().lambda()
  186. .select(FaceManagement.class, face -> !face.getColumn().equals("registerBase64"))
  187. .select(FaceManagement.class, face -> !face.getColumn().equals("register_base64"))
  188. .eq(FaceManagement::getDeleteMark, DeleteMark.NODELETE.getCode())
  189. .eq(FaceManagement::getUserType, 2)
  190. );
  191. Map<Long, FaceManagement> faceManagementMap = new HashMap<>();
  192. for (FaceManagement management : faceManagementList) {
  193. faceManagementMap.put(management.getUserId(), management);
  194. }
  195. JsonParser parser = new JsonParser();
  196. ApiUtil apiUtil = new ApiUtil();
  197. ZipFile zipFile = FileZipUtil.convertToZipFile(file);
  198. Enumeration<? extends ZipEntry> entries = zipFile.entries();
  199. while (entries.hasMoreElements()){
  200. ZipEntry entry = entries.nextElement();
  201. String filename = entry.getName();
  202. InputStream inputStream = zipFile.getInputStream(entry); //读取文件内容
  203. String[] split = filename.split("\\.");
  204. String idNumber = split[0].substring(split[0].length() - 18);
  205. BaseStudentUser studentUser = studentMap.get(idNumber);
  206. if(studentUser == null){
  207. continue;
  208. }
  209. //将照片转换成base64
  210. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  211. byte[] buffer = new byte[4096];
  212. int bytesRead;
  213. while ((bytesRead = inputStream.read(buffer)) != -1) {
  214. outputStream.write(buffer, 0, bytesRead);
  215. }
  216. byte[] imageBytes = outputStream.toByteArray();
  217. //压缩到200k
  218. imageBytes = ImageUtil.compressUnderSize(imageBytes, 204800);
  219. String base64String = Base64.getEncoder().encodeToString(imageBytes);
  220. inputStream.close();
  221. outputStream.close();
  222. //保存到云服务器
  223. String filePath = UploadUtil.uploadFileByte(filename, imageBytes);
  224. //存入数据库
  225. long fileId = IdUtil.getSnowflakeNextId();
  226. String suffix = StringUtils.substringAfterLast(filename, StringPool.DOT);
  227. Long folderId = IdWorker.getId();
  228. File fileEntity = new File();
  229. fileEntity.setId(fileId);
  230. fileEntity.setFolderId(folderId);
  231. fileEntity.setFileName(filename);
  232. fileEntity.setCreateDate(LocalDateTime.now());
  233. fileEntity.setCreateUserId(StpUtil.getLoginIdAsLong());
  234. fileEntity.setDeleteMark(0);
  235. fileEntity.setFileUrl(filePath);
  236. fileEntity.setFileSize(file.getSize());
  237. fileEntity.setFileSuffiex(StringPool.DOT + suffix);
  238. fileEntity.setFileType(suffix);
  239. fileService.save(fileEntity);
  240. StundentFaceProcess process = faceMap.get(studentUser.getId());
  241. if(process == null){
  242. StudentClassVo classVo = studentClassMap.get(studentUser.getId());
  243. if(classVo == null){
  244. continue;
  245. }
  246. long baseFaceId = IdUtil.getSnowflakeNextId();
  247. FaceManagement baseFace = new FaceManagement();
  248. baseFace.setId(baseFaceId);
  249. baseFace.setRegisterBase64(base64String);
  250. baseFace.setUserId(studentUser.getId());
  251. baseFace.setSex(studentUser.getGender());
  252. baseFace.setName(studentUser.getName());
  253. baseFace.setIdno(studentUser.getCredentialNumber());
  254. baseFace.setVerifyStatus(1);
  255. baseFace.setCreateDate(LocalDateTime.now());
  256. baseFace.setCreateUserId(StpUtil.getLoginIdAsLong());
  257. baseFace.setDeleteMark(0);
  258. baseFace.setStatus(1);
  259. baseFace.setFileId(folderId);
  260. baseFace.setUserType(2L);
  261. faceManagementService.save(baseFace);
  262. StundentFaceProcess studentFace = new StundentFaceProcess();
  263. studentFace.setStatus(1);
  264. studentFace.setCreateDate(new Date());
  265. studentFace.setCreateUserId(StpUtil.getLoginIdAsLong());
  266. studentFace.setDeleteMark(0);
  267. studentFace.setGender(studentUser.getGender());
  268. studentFace.setFacePhoto(folderId);
  269. studentFace.setName(studentUser.getName());
  270. studentFace.setClassId(classVo.getId());
  271. studentFace.setClassCn(classVo.getName());
  272. studentFace.setTeacherId(classVo.getTeacherId());
  273. studentFace.setIdentityCard(studentUser.getCredentialNumber());
  274. studentFace.setStatus(1);
  275. studentFace.setUserId(studentUser.getId());
  276. studentFace.setExamStatus(1);
  277. //将人脸上传海康
  278. JsonObject paramJson = new JsonObject();
  279. paramJson.addProperty("personId", hikvisionDataMapper.getStudentHikvisionId(studentUser.getId()));
  280. paramJson.addProperty("faceData", base64String);
  281. Map<String, String> querys = new HashMap<>();
  282. querys.put("tagId", "frs");
  283. String apiPath = "/api/resource/v1/face/single/add";
  284. String response = apiUtil.doPost(apiPath, String.valueOf(paramJson), querys);
  285. studentFace.setHikvisionResult(response);
  286. stundentFaceProcessService.save(studentFace);
  287. }else{
  288. process.setStatus(1);
  289. process.setExamStatus(1);
  290. process.setModifyDate(new Date());
  291. process.setModifyUserId(StpUtil.getLoginIdAsLong());
  292. process.setDeleteMark(0);
  293. process.setFacePhoto(folderId);
  294. String faceId = null;
  295. if(process.getHikvisionResult() != null && process.getHikvisionResult().startsWith("{") && process.getHikvisionResult().endsWith("}")){
  296. JsonObject object = parser.parse(process.getHikvisionResult()).getAsJsonObject();
  297. if("0".equals(object.get("code").getAsString()) && "success".equals(object.get("msg").getAsString())){
  298. faceId = object.get("data").getAsJsonObject().get("faceId").getAsString();
  299. }
  300. }
  301. if(faceId == null){
  302. String apiUrl = "/api/resource/v1/person/condition/personInfo";
  303. JsonObject paramsJson = new JsonObject();
  304. paramsJson.addProperty("paramName", "personId");
  305. JsonArray array = new JsonArray();
  306. array.add(studentUser.getId());
  307. paramsJson.add("paramValue", array);
  308. String personInfoRes = apiUtil.doPost(apiUrl, paramsJson.toString(), null);
  309. JsonObject personInfoData = parser.parse(personInfoRes).getAsJsonObject();
  310. if("0".equals(personInfoData.get("code").getAsString()) && "success".equals(personInfoData.get("msg").getAsString())){
  311. JsonObject dataJson = personInfoData.get("data").getAsJsonObject();
  312. if(dataJson.get("total").getAsInt() > 0){
  313. JsonArray personPhoto = dataJson.get("list").getAsJsonArray().get(0)
  314. .getAsJsonObject().get("personPhoto").getAsJsonArray();
  315. if(personPhoto.size() > 0){
  316. faceId = personPhoto.get(0).getAsJsonObject().get("personPhotoIndexCode").getAsString();
  317. }
  318. }
  319. }
  320. }
  321. //更新海康数据
  322. JsonObject paramJson = new JsonObject();
  323. String apiPath;
  324. if(faceId != null){
  325. apiPath = "/api/resource/v1/face/single/update";
  326. paramJson.addProperty("faceId", faceId);
  327. paramJson.addProperty("faceData", base64String);
  328. }else{
  329. apiPath = "/api/resource/v1/face/single/add";
  330. paramJson.addProperty("personId", studentUser.getId());
  331. paramJson.addProperty("faceData", base64String);
  332. }
  333. String result = apiUtil.doPost(apiPath, paramJson.toString(), null);
  334. if(result.startsWith("{") && result.endsWith("}")){
  335. JsonObject object = parser.parse(result).getAsJsonObject();
  336. if("0".equals(object.get("code").getAsString()) && "success".equals(object.get("msg").getAsString())){
  337. process.setHikvisionResult(result);
  338. }
  339. }
  340. stundentFaceProcessService.updateById(process);
  341. FaceManagement faceManagement = faceManagementMap.get(studentUser.getId());
  342. faceManagement.setVerifyStatus(2);
  343. faceManagement.setRegisterBase64(base64String);
  344. faceManagement.setFileId(folderId);
  345. faceManagementService.update(faceManagement);
  346. }
  347. }
  348. return RT.ok(true);
  349. }
  350. }