Przeglądaj źródła

学生人脸批量导入调整

dzx 1 rok temu
rodzic
commit
a18b482720

+ 1 - 1
src/main/java/com/xjrsoft/common/utils/UploadUtil.java

@@ -42,7 +42,7 @@ public class UploadUtil {
         try {
             OkHttpClient client = new OkHttpClient();
             Request req = new Request.Builder().url(path).build();
-            InputStream inputStream=null;
+            InputStream inputStream = null;
                 okhttp3.Response resp = client.newCall(req).execute();
                 if (resp.isSuccessful()) {
                     ResponseBody body = resp.body();

+ 108 - 46
src/main/java/com/xjrsoft/module/personnel/controller/StundentFaceProcessController.java

@@ -6,12 +6,14 @@ import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.util.IdUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.IdWorker;
 import com.baomidou.mybatisplus.core.toolkit.StringPool;
 import com.github.yulichang.toolkit.MPJWrappers;
 import com.github.yulichang.wrapper.MPJLambdaWrapper;
 import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
 import com.xjrsoft.common.enums.DeleteMark;
 import com.xjrsoft.common.model.result.RT;
 import com.xjrsoft.common.page.ConventPage;
@@ -182,6 +184,26 @@ public class StundentFaceProcessController {
             studentClassMap.put(classVo.getUserId(), classVo);
         }
 
+        List<StundentFaceProcess> faceList = stundentFaceProcessService.list(
+            new QueryWrapper<StundentFaceProcess>().lambda()
+            .eq(StundentFaceProcess::getDeleteMark, DeleteMark.NODELETE.getCode())
+        );
+        Map<Long, StundentFaceProcess> faceMap = new HashMap<>();
+        for (StundentFaceProcess faceProcess : faceList) {
+            faceMap.put(faceProcess.getUserId(), faceProcess);
+        }
+
+        List<FaceManagement> faceManagementList = faceManagementService.list(
+            new QueryWrapper<FaceManagement>().lambda()
+            .eq(FaceManagement::getDeleteMark, DeleteMark.NODELETE.getCode())
+            .eq(FaceManagement::getUserType, 2)
+        );
+        Map<Long, FaceManagement> faceManagementMap = new HashMap<>();
+        for (FaceManagement management : faceManagementList) {
+            faceManagementMap.put(management.getUserId(), management);
+        }
+        JsonParser parser = new JsonParser();
+
         ApiUtil apiUtil = new ApiUtil();
         Long folderId = IdWorker.getId();
         ZipFile zipFile = FileZipUtil.convertToZipFile(file);
@@ -228,52 +250,92 @@ public class StundentFaceProcessController {
             fileEntity.setFileType(suffix);
             fileService.save(fileEntity);
 
-            long baseFaceId = IdUtil.getSnowflakeNextId();
-            FaceManagement baseFace = new FaceManagement();
-            baseFace.setId(baseFaceId);
-            baseFace.setRegisterBase64(base64String);
-            baseFace.setUserId(studentUser.getId());
-            baseFace.setSex(studentUser.getGender());
-            baseFace.setName(studentUser.getName());
-            baseFace.setIdno(studentUser.getCredentialNumber());
-            baseFace.setVerifyStatus(1);
-            baseFace.setCreateDate(LocalDateTime.now());
-            baseFace.setCreateUserId(StpUtil.getLoginIdAsLong());
-            baseFace.setDeleteMark(0);
-            baseFace.setStatus(1);
-            baseFace.setFileId(fileId);
-            baseFace.setUserType(2L);
-            faceManagementService.save(baseFace);
-
-            StudentClassVo classVo = studentClassMap.get(studentUser.getId());
-            StundentFaceProcess studentFace = new StundentFaceProcess();
-            studentFace.setStatus(1);
-            studentFace.setCreateDate(new Date());
-            studentFace.setCreateUserId(StpUtil.getLoginIdAsLong());
-            studentFace.setDeleteMark(0);
-            studentFace.setGender(studentUser.getGender());
-            studentFace.setFacePhoto(fileId);
-            studentFace.setName(studentUser.getName());
-            studentFace.setClassId(classVo.getId());
-            studentFace.setClassCn(classVo.getName());
-            studentFace.setTeacherId(classVo.getTeacherId());
-            studentFace.setIdentityCard(studentUser.getCredentialNumber());
-            studentFace.setStatus(1);
-            studentFace.setUserId(studentUser.getId());
-            studentFace.setExamStatus(1);
-
-
-            //将人脸上传海康
-            JsonObject paramJson = new JsonObject();
-            paramJson.addProperty("personId", studentUser.getId());
-            paramJson.addProperty("faceData", base64String);
-
-            Map<String, String> querys = new HashMap<>();
-            querys.put("tagId", "frs");
-
-            String apiPath = "/api/resource/v1/face/single/add";
-            String response = apiUtil.doPost(apiPath, String.valueOf(paramJson), querys);
-            stundentFaceProcessService.save(studentFace);
+            StundentFaceProcess process = faceMap.get(studentUser.getId());
+            if(process == null){
+                long baseFaceId = IdUtil.getSnowflakeNextId();
+                FaceManagement baseFace = new FaceManagement();
+                baseFace.setId(baseFaceId);
+                baseFace.setRegisterBase64(base64String);
+                baseFace.setUserId(studentUser.getId());
+                baseFace.setSex(studentUser.getGender());
+                baseFace.setName(studentUser.getName());
+                baseFace.setIdno(studentUser.getCredentialNumber());
+                baseFace.setVerifyStatus(1);
+                baseFace.setCreateDate(LocalDateTime.now());
+                baseFace.setCreateUserId(StpUtil.getLoginIdAsLong());
+                baseFace.setDeleteMark(0);
+                baseFace.setStatus(1);
+                baseFace.setFileId(folderId);
+                baseFace.setUserType(2L);
+                faceManagementService.save(baseFace);
+
+                StudentClassVo classVo = studentClassMap.get(studentUser.getId());
+                StundentFaceProcess studentFace = new StundentFaceProcess();
+                studentFace.setStatus(1);
+                studentFace.setCreateDate(new Date());
+                studentFace.setCreateUserId(StpUtil.getLoginIdAsLong());
+                studentFace.setDeleteMark(0);
+                studentFace.setGender(studentUser.getGender());
+                studentFace.setFacePhoto(folderId);
+                studentFace.setName(studentUser.getName());
+                studentFace.setClassId(classVo.getId());
+                studentFace.setClassCn(classVo.getName());
+                studentFace.setTeacherId(classVo.getTeacherId());
+                studentFace.setIdentityCard(studentUser.getCredentialNumber());
+                studentFace.setStatus(1);
+                studentFace.setUserId(studentUser.getId());
+                studentFace.setExamStatus(1);
+
+                //将人脸上传海康
+                JsonObject paramJson = new JsonObject();
+                paramJson.addProperty("personId", studentUser.getId());
+                paramJson.addProperty("faceData", base64String);
+
+                Map<String, String> querys = new HashMap<>();
+                querys.put("tagId", "frs");
+
+                String apiPath = "/api/resource/v1/face/single/add";
+                String response = apiUtil.doPost(apiPath, String.valueOf(paramJson), querys);
+                studentFace.setHikvisionResult(response);
+                stundentFaceProcessService.save(studentFace);
+            }else{
+                process.setStatus(1);
+                process.setExamStatus(1);
+                process.setModifyDate(new Date());
+                process.setModifyUserId(StpUtil.getLoginIdAsLong());
+                process.setDeleteMark(0);
+                process.setFacePhoto(folderId);
+
+                String faceId = null;
+                if(process.getHikvisionResult() == null){
+                    JsonObject object = parser.parse(process.getHikvisionResult()).getAsJsonObject();
+                    if("0".equals(object.get("code").getAsString()) && "success".equals(object.get("msg").getAsString())){
+                        faceId = object.get("data").getAsJsonObject().get("faceId").getAsString();
+                    }
+                }
+
+                //更新海康数据
+                JsonObject paramJson = new JsonObject();
+                String apiPath;
+                if(faceId != null){
+                    apiPath = "api/resource/v1/face/single/update";
+                    paramJson.addProperty("faceId", faceId);
+                    paramJson.addProperty("faceData", base64String);
+
+                }else{
+                    apiPath = "api/resource/v1/face/single/add";
+                    paramJson.addProperty("personId", studentUser.getId());
+                    paramJson.addProperty("faceData", base64String);
+                }
+                String result = apiUtil.doPost(apiPath, paramJson.toString(), null);
+                process.setHikvisionResult(result);
+                stundentFaceProcessService.updateById(process);
+
+                FaceManagement faceManagement = faceManagementMap.get(studentUser.getId());
+                faceManagement.setVerifyStatus(2);
+                faceManagement.setRegisterBase64(base64String);
+                faceManagementService.update(faceManagement);
+            }
         }
         return RT.ok(true);
     }

+ 3 - 0
src/main/java/com/xjrsoft/module/personnel/entity/StundentFaceProcess.java

@@ -121,4 +121,7 @@ public class StundentFaceProcess implements Serializable {
 
     @ApiModelProperty("审批状态(0:未通过 1:通过)")
     private Integer examStatus;
+
+    @ApiModelProperty("海康接口返回内容")
+    private String hikvisionResult;
 }

+ 3 - 0
src/main/java/com/xjrsoft/module/personnel/entity/TeacherFaceProcess.java

@@ -121,4 +121,7 @@ public class TeacherFaceProcess implements Serializable {
 
     @ApiModelProperty("审批状态(0:未通过 1:通过)")
     private Integer examStatus;
+
+    @ApiModelProperty("海康接口返回内容")
+    private String hikvisionResult;
 }

+ 55 - 0
src/test/java/com/xjrsoft/module/personnel/controller/StundentFaceProcessControllerTest.java

@@ -0,0 +1,55 @@
+package com.xjrsoft.module.personnel.controller;
+
+import com.xjrsoft.XjrSoftApplication;
+import com.xjrsoft.common.utils.UploadUtil;
+import org.junit.jupiter.api.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.mock.web.MockMultipartFile;
+import org.springframework.test.context.junit4.SpringRunner;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+/**
+ * @author dzx
+ * @date 2024/5/22
+ */
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = XjrSoftApplication.class)
+class StundentFaceProcessControllerTest {
+
+    @Test
+    void test() throws Exception {
+        String filePath = "C:\\Users\\14263\\Downloads\\face.rar";
+        File file = new File(filePath);
+        FileInputStream input = new FileInputStream(file);
+        MultipartFile multipartFile = new MockMultipartFile("file",
+                file.getName(), "text/plain", input);
+        String uploadedFile = UploadUtil.uploadFile(multipartFile);
+
+        InputStream download = UploadUtil.download(uploadedFile);
+
+        readZipEntries(download);
+    }
+
+    public static void readZipEntries(InputStream inputStream) throws IOException {
+        try (ZipInputStream zipInputStream = new ZipInputStream(inputStream)) {
+            ZipEntry zipEntry;
+            while ((zipEntry = zipInputStream.getNextEntry()) != null) {
+                // 处理每个zip条目
+                System.out.println("Zip Entry: " + zipEntry.getName());
+                zipInputStream.closeEntry();
+            }
+        }
+    }
+}