StundentFaceProcessController.java 22 KB

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