Browse Source

车辆信息推送

brealinxx 10 months ago
parent
commit
305f7465c0

+ 75 - 8
src/main/java/com/xjrsoft/module/hikvision/util/DataUtil.java

@@ -1,13 +1,14 @@
 package com.xjrsoft.module.hikvision.util;
 
 import cn.hutool.db.Db;
-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.module.organization.entity.Department;
+import com.xjrsoft.module.personnel.entity.CarMessageApply;
 import lombok.extern.slf4j.Slf4j;
+import lombok.var;
 
 import java.sql.SQLException;
 import java.util.ArrayList;
@@ -16,6 +17,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ThreadLocalRandom;
 
 /**
  * @author dzx
@@ -82,16 +84,81 @@ public class DataUtil {
         return idMap;
     }
 
-    void insertRecord(Db db, String tableName, Map<Long, String> idsMap) throws SQLException {
-        if(idsMap.isEmpty()){
+    /*同步车辆数据*/
+    public Map<String, String> insertCar(Db db, String tableName, Map<Long, String> tableData) throws Exception {
+        String sql = "SELECT * FROM " + tableName + " WHERE delete_mark = 0 and status = 1";
+        List<CarMessageApply> dataList = db.query(sql, CarMessageApply.class);
+        String apiPath = "/api/resource/v1/vehicle/batch/add";
+        Map<String, String> idMap = new HashMap<>();
+        JsonParser jsonParser = new JsonParser();
+        ApiUtil apiUtil = new ApiUtil();
+
+        Map<String, String> idCodeMap = new HashMap<>();
+        idCodeMap.put("tagId", "frs");
+
+        JsonArray dataArray = new JsonArray();
+        String result = null;
+        for (CarMessageApply carMessageApply : dataList) {
+            long currentTimestamp = System.currentTimeMillis();
+            int randomSuffix = ThreadLocalRandom.current().nextInt(10000);
+            String uniqueClientIdStr = String.format("%03d%04d", currentTimestamp % 1000, randomSuffix);
+            long uniqueClientId = Long.parseLong(uniqueClientIdStr);
+
+            JsonObject paramJson = new JsonObject();
+            paramJson.addProperty("clientId", uniqueClientId);
+            paramJson.addProperty("plateNo", carMessageApply.getCarNumber());
+            paramJson.addProperty("PersonId",carMessageApply.getUserId());
+
+            int vehicleTypeNum;
+            switch (carMessageApply.getVehicleType()) {
+                case "小型车":
+                    vehicleTypeNum = 1;
+                    break;
+                case "大型车":
+                    vehicleTypeNum = 2;
+                    break;
+                case "摩托车":
+                    vehicleTypeNum = 3;
+                    break;
+                default:
+                    vehicleTypeNum = 0;
+                    break;
+            }
+            paramJson.addProperty("vehicleType", vehicleTypeNum);
+
+            dataArray.add(paramJson);
+        }
+        result = apiUtil.doPost(apiPath, dataArray.toString(), idCodeMap, null);
+
+        JsonElement parse = jsonParser.parse(result);
+
+        JsonObject resultJson = parse.getAsJsonObject();
+        if (resultJson.get("code").getAsInt() == 0 && "success".equals(resultJson.get("msg").getAsString())) {
+            JsonArray array = resultJson.get("data").getAsJsonObject().get("successes").getAsJsonArray();
+            for (JsonElement jsonElement : array) {
+                JsonObject jsonObject = jsonElement.getAsJsonObject();
+                idMap.put(jsonObject.get("clientId").getAsString(), jsonObject.get("vehicleId").getAsString());
+            }
+        } else {
+            System.err.println("API call failed: " + resultJson.get("msg").getAsString());
+        }
+
+        insertRecord(db, tableName, idMap);
+        return idMap;
+    }
+
+    void insertRecord(Db db, String tableName, Map<?, String> idsMap) throws SQLException {
+        if (idsMap.isEmpty()) {
             return;
         }
-        List<String> sqls = new ArrayList<>();
-        for (Long sourceId : idsMap.keySet()) {
-            String sql = "INSERT INTO hikvision_data(create_date,table_name,source_id,hikvision_id) value(now(),'"
-                    + tableName + "'," + sourceId + ",'" + idsMap.get(sourceId) + "')";
+        var sqls = new ArrayList<String>();
+        for (var entry : idsMap.entrySet()) {
+            var sourceId = entry.getKey();
+            var sql = "INSERT INTO hikvision_data(create_date, table_name, source_id, hikvision_id) " +
+                    "VALUES(now(), '" + tableName + "', " + sourceId + ", '" + entry.getValue() + "')";
             sqls.add(sql);
         }
         db.executeBatch(sqls);
-    };
+    }
+
 }

+ 4 - 0
src/main/java/com/xjrsoft/module/job/HikvisionBaseDataTask.java

@@ -71,6 +71,10 @@ public class HikvisionBaseDataTask {
 //            tableName = "base_student";
 //            dataUtil.insertStudent(use, tableName, grade, dataMap.get(tableName));
 
+            //推送车辆
+            String carTableName = "car_message_apply";
+            dataUtil.insertCar(use, carTableName, null);
+
             log.info("数据推送完成");
         } catch (Exception e) {
             log.error(e.getMessage(), e);

+ 4 - 0
src/main/resources/mapper/teacher/TeacherFaceImportMapper.xml

@@ -10,6 +10,7 @@
         <if test="id != null">
             where t.id = #{id}
         </if>
+        and t.status = 1
     </select>
 
     <select id="GetTeacherHikvisionImgById" parameterType="long" resultType="java.lang.String">
@@ -19,6 +20,7 @@
         <if test="id != null">
             where t.id = #{id}
         </if>
+        and t.status = 1
     </select>
 
     <select id="GetStudentHikvisionIdById" parameterType="long" resultType="java.lang.String">
@@ -28,6 +30,7 @@
         <if test="id != null">
             where t.id = #{id}
         </if>
+        and t.status = 1
     </select>
 
     <select id="GetStudentHikvisionImgById" parameterType="long" resultType="java.lang.String">
@@ -37,5 +40,6 @@
         <if test="id != null">
             where t.id = #{id}
         </if>
+        and t.status = 1
     </select>
 </mapper>

+ 6 - 2
src/test/java/com/xjrsoft/module/job/HikvisionBaseDataTaskTest.java

@@ -62,8 +62,12 @@ class HikvisionBaseDataTaskTest {
             dataMap.put(table, tableData);
         }
         DataUtil dataUtil = new DataUtil();
-        String tableName = "xjr_department";
-        Map<Long, String> department = dataUtil.insertDepartment(use, tableName, null);
+//        String tableName = "xjr_department";
+//        Map<Long, String> department = dataUtil.insertDepartment(use, tableName, null);
+
+        String tableNameCar = "car_message_apply";
+        Map<String, String> carmessage = dataUtil.insertCar(use, tableNameCar, null);
+        System.out.println(carmessage);
         //System.out.println(department.toString());
 
 //        selectOrg(use, tableName);