Pārlūkot izejas kodu

海康数据推送调整

dzx 1 mēnesi atpakaļ
vecāks
revīzija
859eeda19e

+ 81 - 12
src/main/java/com/xjrsoft/module/hikvision/util/DataUtil.java

@@ -1,19 +1,23 @@
 package com.xjrsoft.module.hikvision.util;
 
+import cn.hutool.db.Entity;
 import com.google.gson.JsonArray;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
 import com.xjrsoft.common.mybatis.SqlRunnerAdapter;
 import com.xjrsoft.common.utils.SqlRunnerAdapterUtil;
-import com.xjrsoft.module.base.entity.BaseClass;
 import com.xjrsoft.module.base.entity.BaseGrade;
+import com.xjrsoft.module.hikvision.entity.HikvisionData;
 import com.xjrsoft.module.hikvision.mapper.HikvisionDataMapper;
 import com.xjrsoft.module.organization.entity.Department;
 import com.xjrsoft.module.personnel.entity.CarMessageApply;
 import lombok.extern.slf4j.Slf4j;
 import lombok.var;
 
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -254,31 +258,40 @@ public class DataUtil {
         return idMap;
     }
 
-    public Map<String, String> insertStudentOne(String tableName, Map<String, String> tableData){
+    public Map<String, String> insertStudentOne(String tableName, Map<String, String> tableData, List<HikvisionData> studentList) throws InterruptedException {
         String sql = "SELECT t1.id,t1.name,CONCAT(t3.id,'-',replace(t4.name,'级',''),'-',t3.class_type,'-',t2.stduy_status) as orgIndexCode," +
                 " t1.user_name,t1.gender,DATE_FORMAT(t1.birth_date, '%Y-%m-%d') as birthday,t1.mobile,t1.email,t1.credential_type,t1.credential_number FROM xjr_user t1" +
                 " INNER JOIN base_student_school_roll t2 ON t1.id = t2.user_id" +
                 " INNER JOIN base_class t3 ON t2.class_id = t3.id" +
                 " INNER JOIN base_grade t4 ON t3.grade_id = t4.id" +
                 " WHERE t1.delete_mark = 0 AND t2.delete_mark = 0" +
-                " AND t3.class_type IS NOT NULL";
+                " AND t3.class_type IS NOT NULL AND t3.is_graduate = 1";
         List<Map<String, Object>> list = SqlRunnerAdapter.db().selectList(sql, String[].class);
         Map<Integer, String> clientMap = new HashMap<>();
+        List<Entity> insertList = new ArrayList<>();
 
 
+        Map<String, String> historyMap = new HashMap<>();
+        for (HikvisionData hikvisionData : studentList) {
+            historyMap.put(hikvisionData.getSourceId(), hikvisionData.getSendData());
+        }
+
         Map<String, String> idMap = new HashMap<>();
         JsonParser jsonParser = new JsonParser();
         ApiUtil apiUtil = new ApiUtil();
         Map<String, String> header = new HashMap<>();
         header.put("tagId", "insert_student");
 
+        List<JsonObject> addList = new ArrayList<>();
+        List<JsonObject> updateList = new ArrayList<>();
+
         for(int i = 0; i < list.size(); i ++){
             Map<String, Object> objectMap = list.get(i);
-            String apiPath = "/api/resource/v2/person/single/add";
+            String id = objectMap.get("id").toString();
 
             JsonObject paramJson = new JsonObject();
             paramJson.addProperty("clientId", i);
-            paramJson.addProperty("personId", objectMap.get("id").toString());
+            paramJson.addProperty("personId", id);
             paramJson.addProperty("personName", objectMap.get("name").toString());
             paramJson.addProperty("orgIndexCode", objectMap.get("orgIndexCode").toString());
             paramJson.addProperty("phoneNo", objectMap.get("mobile")==null?"":objectMap.get("mobile").toString());
@@ -301,24 +314,80 @@ public class DataUtil {
             }
             paramJson.addProperty("gender", gender);
 
-            if(tableData != null && tableData.containsKey(objectMap.get("id").toString())){
-                apiPath = "/api/resource/v1/person/single/update";
-                apiUtil.doPost(apiPath, paramJson.toString(), null, header);
+            if(historyMap.containsKey(id)){
+                if(!paramJson.toString().equals(historyMap.get(id))){
+                    updateList.add(paramJson);
+                }
                 continue;
             }
 
-            clientMap.put(i, objectMap.get("id").toString());
+            addList.add(paramJson);
+
+//            if(tableData != null && tableData.containsKey(id)){
+//                apiPath = "/api/resource/v1/person/single/update";
+//                apiUtil.doPost(apiPath, paramJson.toString(), null, header);
+//                continue;
+//            }
+//
+//            clientMap.put(i, id);
+//            String result = apiUtil.doPost(apiPath, paramJson.toString(), null, header);
+//            JsonElement parse = jsonParser.parse(result);
+//            JsonObject resultJson = parse.getAsJsonObject();
+//            if("0".equals(resultJson.get("code").getAsString()) && "success".equals(resultJson.get("msg").getAsString())){
+//                JsonObject array = resultJson.get("data").getAsJsonObject();
+//                idMap.put(id, array.get("personId").getAsString());
+//            }
+        }
+
+        String apiPath = "/api/resource/v1/person/single/add";
+        SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
+        for (JsonObject paramJson : addList) {
+            String result = apiUtil.doPost(apiPath, paramJson.toString(), null, header);
+            JsonElement parse = jsonParser.parse(result);
+            JsonObject resultJson = parse.getAsJsonObject();
+            if("0".equals(resultJson.get("code").getAsString()) && "success".equals(resultJson.get("msg").getAsString())){
+                if("0".equals(resultJson.get("code").getAsString()) && "success".equals(resultJson.get("msg").getAsString())){
+                    Entity entity = Entity.create(tableName);
+                    entity.set("table_name", tableName);
+                    entity.set("create_date", sdf.format(new Date()));
+                    entity.set("source_id", clientMap.get(paramJson.get("clientId").getAsInt()));
+                    entity.set("hikvision_id", paramJson.get("personId").getAsString());
+                    entity.set("send_data", paramJson.toString());
+                    insertList.add(entity);
+                }else if("0x00052102".equals(resultJson.get("code").getAsString()) && "PersonId Already In Db".equals(resultJson.get("msg").getAsString())){
+                    Entity entity = Entity.create(tableName);
+                    entity.set("table_name", tableName);
+                    entity.set("create_date", sdf.format(new Date()));
+                    entity.set("source_id", paramJson.get("personId").getAsString());
+                    entity.set("hikvision_id", paramJson.get("personId").getAsString());
+                    entity.set("send_data", paramJson.toString());
+                    insertList.add(entity);
+                }
+            }
+        }
+
+        if(!insertList.isEmpty()){
+            SqlRunnerAdapter.db().dynamicInsertBatch(tableName, insertList);
+        }
+
+        tableName = "hikvision_data";
+        apiPath = "/api/resource/v1/person/single/update";
+        for (JsonObject paramJson : updateList) {
+            Thread.sleep(500);
             String result = apiUtil.doPost(apiPath, paramJson.toString(), null, header);
             JsonElement parse = jsonParser.parse(result);
             JsonObject resultJson = parse.getAsJsonObject();
             if("0".equals(resultJson.get("code").getAsString()) && "success".equals(resultJson.get("msg").getAsString())){
-                JsonObject array = resultJson.get("data").getAsJsonObject();
-                idMap.put(objectMap.get("id").toString(), array.get("personId").getAsString());
+                Entity entity = Entity.create(tableName);
+                entity.set("send_data", paramJson.toString());
+                Entity where = Entity.create(tableName);
+                where.set("source_id", paramJson.get("personId").getAsString());
+                SqlRunnerAdapter.db().dynamicUpdate(tableName, entity, where);
             }
         }
 
         //插入记录表
-        insertRecord(tableName, idMap);
+        //insertRecord(tableName, idMap);
         return idMap;
     }
 

+ 6 - 1
src/main/java/com/xjrsoft/module/job/HikvisionBaseDataTask.java

@@ -1,8 +1,10 @@
 package com.xjrsoft.module.job;
 
 import cn.hutool.extra.spring.SpringUtil;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.xjrsoft.common.mybatis.SqlRunnerAdapter;
 import com.xjrsoft.common.utils.SqlRunnerAdapterUtil;
+import com.xjrsoft.module.hikvision.entity.HikvisionData;
 import com.xjrsoft.module.hikvision.mapper.HikvisionDataMapper;
 import com.xjrsoft.module.hikvision.util.DataUtil;
 import lombok.extern.slf4j.Slf4j;
@@ -104,8 +106,11 @@ public class HikvisionBaseDataTask {
 
 
             tableName = "base_student";
+            List<HikvisionData> studentList = hikvisionDataMapper.selectList(
+                    new QueryWrapper<HikvisionData>().lambda().eq(HikvisionData::getTableName, tableName)
+            );
             Map<String, String> baseStudent = dataMap.get(tableName);
-            dataUtil.insertStudentOne(tableName, baseStudent);
+            dataUtil.insertStudentOne(tableName, baseStudent, studentList);
 
             log.info("数据推送完成");
         } catch (Exception e) {

+ 1 - 1
src/main/java/com/xjrsoft/module/job/HikvisionLeaveTask.java

@@ -46,7 +46,7 @@ public class HikvisionLeaveTask {
 
     @Scheduled(cron = "0 */15 * * * ?")
     public void execute() {
-        //doExecute();
+        doExecute();
     }
     public void doExecute() {
         String active = SpringUtil.getActiveProfile();

+ 126 - 29
src/test/java/com/xjrsoft/module/job/HikvisionBaseDataTaskTest.java

@@ -1,6 +1,8 @@
 package com.xjrsoft.module.job;
 
 import cn.hutool.db.Db;
+import cn.hutool.db.Entity;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.google.gson.JsonArray;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
@@ -13,6 +15,7 @@ import com.xjrsoft.common.utils.SqlRunnerAdapterUtil;
 import com.xjrsoft.module.base.entity.BaseClass;
 import com.xjrsoft.module.evaluate.controller.EvaluateItemController;
 import com.xjrsoft.module.hikvision.entity.HikvisionData;
+import com.xjrsoft.module.hikvision.mapper.HikvisionDataMapper;
 import com.xjrsoft.module.hikvision.util.ApiUtil;
 import com.xjrsoft.module.hikvision.util.DataUtil;
 import com.xjrsoft.module.hikvision.util.OutInRecordUtil;
@@ -30,7 +33,9 @@ import org.springframework.test.context.junit4.SpringRunner;
 
 import javax.sql.DataSource;
 import java.sql.SQLException;
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -52,6 +57,9 @@ class HikvisionBaseDataTaskTest {
 
     OutInRecordUtil outInRecordUtil = new OutInRecordUtil();
 
+    @Autowired
+    private HikvisionDataMapper hikvisionDataMapper;
+
     @Test
     void test2(){
         String sql = "select * from base_class";
@@ -75,6 +83,51 @@ class HikvisionBaseDataTaskTest {
         }
     }
 
+    @Test
+    void selectOrg(){
+        String apiPath = "/api/resource/v1/org/orgList";
+        ApiUtil apiUtil = new ApiUtil();
+
+        JsonObject jsonObject = new JsonObject();
+        jsonObject.addProperty("pageNo", 1);
+        jsonObject.addProperty("pageSize", 500);
+
+        String doPost = apiUtil.doPost(apiPath, jsonObject.toString(), null);
+        System.out.println(doPost);
+
+        String sql = "SELECT * FROM hikvision_data WHERE 1 = 1 and table_name = 'base_class'";
+        List<Map<String, Object>> maps = SqlRunnerAdapter.db().selectList(sql);
+        List<String> sourceIds = new ArrayList<>();
+        for (Map<String, Object> map : maps) {
+            sourceIds.add(map.get("source_id").toString());
+        }
+        JsonParser jsonParser = new JsonParser();
+        JsonElement parse = jsonParser.parse(doPost);
+        JsonObject resultJson = parse.getAsJsonObject();
+
+        String tableName = "hikvision_data";
+        SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
+        if("0".equals(resultJson.get("code").getAsString()) && "success".equals(resultJson.get("msg").getAsString())){
+            JsonArray array = resultJson.get("data").getAsJsonObject().get("list").getAsJsonArray();
+            for (JsonElement jsonElement : array) {
+                JsonObject json = jsonElement.getAsJsonObject();
+                String orgIndexCode = json.get("orgIndexCode").getAsString();
+                if(sourceIds.contains(orgIndexCode)){
+                    continue;
+                }
+
+                Entity entity = Entity.create(tableName);
+                entity.set("source_id", orgIndexCode);
+                entity.set("table_name", "base_class");
+                entity.set("hikvision_id", orgIndexCode);
+                entity.set("create_date", sdf.format(new Date()));
+                SqlRunnerAdapter.db().dynamicInsert(tableName, entity);
+            }
+        }
+
+
+    }
+
 
     @Test
     void test() throws Exception {
@@ -123,8 +176,11 @@ class HikvisionBaseDataTaskTest {
 
 
         tableName = "base_student";
+        List<HikvisionData> studentList = hikvisionDataMapper.selectList(
+                new QueryWrapper<HikvisionData>().lambda().eq(HikvisionData::getTableName, tableName)
+        );
         Map<String, String> baseStudent = dataMap.get(tableName);
-        Map<String, String> student = dataUtil.insertStudentOne(tableName, baseStudent);
+        Map<String, String> student = insertStudentOne(tableName, baseStudent, studentList);
 
 //        selectOrg(use, "base_class");
 
@@ -178,17 +234,24 @@ class HikvisionBaseDataTaskTest {
         db.executeBatch(sqls);
     }
 
-    public Map<String, String> insertStudentOne(String tableName, Map<String, String> tableData) throws Exception {
+    public Map<String, String> insertStudentOne(String tableName, Map<String, String> tableData, List<HikvisionData> studentList) throws Exception {
         String sql = "SELECT t1.id,t1.name,CONCAT(t3.id,'-',replace(t4.name,'级',''),'-',t3.class_type,'-',t2.stduy_status) as orgIndexCode," +
                 " t1.user_name,t1.gender,DATE_FORMAT(t1.birth_date, '%Y-%m-%d') as birthday,t1.mobile,t1.email,t1.credential_type,t1.credential_number FROM xjr_user t1" +
                 " INNER JOIN base_student_school_roll t2 ON t1.id = t2.user_id" +
                 " INNER JOIN base_class t3 ON t2.class_id = t3.id" +
                 " INNER JOIN base_grade t4 ON t3.grade_id = t4.id" +
                 " WHERE t1.delete_mark = 0 AND t2.delete_mark = 0" +
-                " AND t3.class_type IS NOT NULL" +
-                " AND t1.name IN ('罗一钦','刘嘉靖')";
+                " AND t3.class_type IS NOT NULL AND t3.is_graduate = 1" +
+                " AND t3.id = 1828994300931256321";
         List<Map<String, Object>> list = SqlRunnerAdapter.db().selectList(sql, String[].class);
         Map<Integer, String> clientMap = new HashMap<>();
+        List<Entity> insertList = new ArrayList<>();
+
+
+        Map<String, String> historyMap = new HashMap<>();
+        for (HikvisionData hikvisionData : studentList) {
+            historyMap.put(hikvisionData.getSourceId(), hikvisionData.getSendData());
+        }
 
         Map<String, String> idMap = new HashMap<>();
         JsonParser jsonParser = new JsonParser();
@@ -196,13 +259,16 @@ class HikvisionBaseDataTaskTest {
         Map<String, String> header = new HashMap<>();
         header.put("tagId", "insert_student");
 
+        List<JsonObject> addList = new ArrayList<>();
+        List<JsonObject> updateList = new ArrayList<>();
+
         for(int i = 0; i < list.size(); i ++){
             Map<String, Object> objectMap = list.get(i);
-            String apiPath = "/api/resource/v2/person/single/add";
+            String id = objectMap.get("id").toString();
 
             JsonObject paramJson = new JsonObject();
             paramJson.addProperty("clientId", i);
-            paramJson.addProperty("personId", objectMap.get("id").toString());
+            paramJson.addProperty("personId", id);
             paramJson.addProperty("personName", objectMap.get("name").toString());
             paramJson.addProperty("orgIndexCode", objectMap.get("orgIndexCode").toString());
             paramJson.addProperty("phoneNo", objectMap.get("mobile")==null?"":objectMap.get("mobile").toString());
@@ -210,8 +276,9 @@ class HikvisionBaseDataTaskTest {
             paramJson.addProperty("birthday", objectMap.get("birthday")==null?"":objectMap.get("birthday").toString());
             paramJson.addProperty("personType", 2);
 
-            int gender;
-            switch (objectMap.get("gender").toString()) {
+            int gender = 0;
+            String genderData = objectMap.get("gender") == null ? "" : objectMap.get("gender").toString();
+            switch (genderData) {
                 case "SB10001":
                     gender = 1;
                     break;
@@ -224,19 +291,57 @@ class HikvisionBaseDataTaskTest {
             }
             paramJson.addProperty("gender", gender);
 
-            if(tableData != null && tableData.containsKey(objectMap.get("id").toString())){
-                apiPath = "/api/resource/v1/person/single/update";
-                apiUtil.doPost(apiPath, paramJson.toString(), null, header);
+            if(historyMap.containsKey(id)){
+                if(!paramJson.toString().equals(historyMap.get(id))){
+                    updateList.add(paramJson);
+                }
                 continue;
             }
 
-            clientMap.put(i, objectMap.get("id").toString());
+            addList.add(paramJson);
+        }
+
+        String apiPath = "/api/resource/v1/person/single/add";
+        SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
+        for (JsonObject paramJson : addList) {
             String result = apiUtil.doPost(apiPath, paramJson.toString(), null, header);
             JsonElement parse = jsonParser.parse(result);
             JsonObject resultJson = parse.getAsJsonObject();
             if("0".equals(resultJson.get("code").getAsString()) && "success".equals(resultJson.get("msg").getAsString())){
-                JsonObject array = resultJson.get("data").getAsJsonObject();
-                idMap.put(objectMap.get("id").toString(), array.get("personId").getAsString());
+                Entity entity = Entity.create(tableName);
+                entity.set("table_name", tableName);
+                entity.set("create_date", sdf.format(new Date()));
+                entity.set("source_id", clientMap.get(paramJson.get("clientId").getAsInt()));
+                entity.set("hikvision_id", paramJson.get("personId").getAsString());
+                entity.set("send_data", paramJson.toString());
+                insertList.add(entity);
+            }else if("0x00052102".equals(resultJson.get("code").getAsString()) && "PersonId Already In Db".equals(resultJson.get("msg").getAsString())){
+                Entity entity = Entity.create(tableName);
+                entity.set("table_name", tableName);
+                entity.set("create_date", sdf.format(new Date()));
+                entity.set("source_id", paramJson.get("personId").getAsString());
+                entity.set("hikvision_id", paramJson.get("personId").getAsString());
+                entity.set("send_data", paramJson.toString());
+                insertList.add(entity);
+            }
+        }
+        tableName = "hikvision_data";
+        if(!insertList.isEmpty()){
+            SqlRunnerAdapter.db().dynamicInsertBatch(tableName, insertList);
+        }
+
+        apiPath = "/api/resource/v1/person/single/update";
+        for (JsonObject paramJson : updateList) {
+            Thread.sleep(500);
+            String result = apiUtil.doPost(apiPath, paramJson.toString(), null, header);
+            JsonElement parse = jsonParser.parse(result);
+            JsonObject resultJson = parse.getAsJsonObject();
+            if("0".equals(resultJson.get("code").getAsString()) && "success".equals(resultJson.get("msg").getAsString())){
+                Entity entity = Entity.create(tableName);
+                entity.set("send_data", paramJson.toString());
+                Entity where = Entity.create(tableName);
+                where.set("source_id", paramJson.get("personId").getAsString());
+                SqlRunnerAdapter.db().dynamicUpdate(tableName, entity, where);
             }
         }
         //插入记录表
@@ -245,30 +350,21 @@ class HikvisionBaseDataTaskTest {
 
 
 
-    void deleteClass(Db db) throws SQLException {
+    @Test
+    void deleteClass() {
 
-        String apiPath = "/api/resource/v1/org/orgList";
+        String apiPath = "/api/resource/v1/org/batch/delete";
         JsonObject jsonObject = new JsonObject();
-        jsonObject.addProperty("pageNo", 1);
-        jsonObject.addProperty("pageSize", 500);
         ApiUtil apiUtil = new ApiUtil();
-        String result = apiUtil.doPost(apiPath, jsonObject.toString(), null);
 
-        String sql = "SELECT * FROM hikvision_data WHERE table_name = 'base_class' order by length(hikvision_id) desc";
-        List<HikvisionData> dataList = db.query(sql, HikvisionData.class);
-        apiPath = "/api/resource/v1/org/batch/delete";
-        Map<Long, String> idMap = new HashMap<>();
-        JsonParser jsonParser = new JsonParser();
         JsonArray dataArray = new JsonArray();
-        for (HikvisionData department : dataList) {
-            dataArray.add(department.getHikvisionId());
-        }
-
+        dataArray.add("1828723343029219330-2024-job_class-FB3001");
+        dataArray.add("1828723343029219330-2024-job_class-FB3002");
         JsonObject paramJson = new JsonObject();
         paramJson.add("indexCodes", dataArray);
         Map<String, String> headerMap = new HashMap<>();
         headerMap.put("tagId", "deleteOrg");
-        result = apiUtil.doPost(apiPath, paramJson.toString(), null, headerMap);
+        String result = apiUtil.doPost(apiPath, paramJson.toString(), null, headerMap);
         System.out.println(result);
     }
 
@@ -385,6 +481,7 @@ class HikvisionBaseDataTaskTest {
     }
 
 
+    @Test
     void deleteOrg(Db db, String tableName) throws SQLException {
         String sql = "SELECT * FROM " + tableName + " WHERE delete_mark = 0 ORDER BY LENGTH(hierarchy)";
         List<Department> dataList = db.query(sql, Department.class);