|
@@ -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);
|
|
|
- };
|
|
|
+ }
|
|
|
+
|
|
|
}
|