|
@@ -1,6 +1,7 @@
|
|
|
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;
|
|
@@ -11,8 +12,10 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import java.sql.SQLException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.HashSet;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
|
|
|
/**
|
|
|
* @author dzx
|
|
@@ -22,43 +25,60 @@ import java.util.Map;
|
|
|
public class DataUtil {
|
|
|
/**
|
|
|
* 同步组织数据
|
|
|
+ * 只有批量添加接口,需要一个层级一个层级的添加
|
|
|
*/
|
|
|
- public Map<Long, String> insertDepartment(Db db, String tableName) throws Exception {
|
|
|
- String sql = "SELECT * FROM " + tableName + " WHERE delete_mark = 0 ORDER BY LENGTH(hierarchy)";
|
|
|
+ public Map<Long, String> insertDepartment(Db db, String tableName, Map<Long, String> tableData) throws Exception {
|
|
|
+ String sql = "SELECT DISTINCT LENGTH(hierarchy) FROM " + tableName + " WHERE delete_mark = 0";
|
|
|
+ List<Integer[]> list = db.query(sql, Integer[].class);
|
|
|
+ Set<Integer> levelSet = new HashSet<>();
|
|
|
+ for (Integer[] strings : list) {
|
|
|
+ levelSet.add(strings[0]);
|
|
|
+ }
|
|
|
+
|
|
|
+ sql = "SELECT * FROM " + tableName + " WHERE delete_mark = 0 ORDER BY LENGTH(hierarchy)";
|
|
|
List<Department> dataList = db.query(sql, Department.class);
|
|
|
String apiPath = "/api/resource/v1/org/batch/add";
|
|
|
Map<Long, String> idMap = new HashMap<>();
|
|
|
JsonParser jsonParser = new JsonParser();
|
|
|
ApiUtil apiUtil = new ApiUtil();
|
|
|
- JsonArray dataArray = new JsonArray();
|
|
|
|
|
|
Map<String, Long> idCodeMap = new HashMap<>();
|
|
|
- for (Department department : dataList) {
|
|
|
- JsonObject paramJson = new JsonObject();
|
|
|
- paramJson.addProperty("clientId", department.getCode());
|
|
|
- paramJson.addProperty("orgIndexCode", department.getId().toString());
|
|
|
- paramJson.addProperty("orgName", department.getName());
|
|
|
- paramJson.addProperty("parentIndexCode", department.getParentId().toString());
|
|
|
- paramJson.addProperty("orgCode", department.getCode());
|
|
|
- dataArray.add(paramJson);
|
|
|
- idCodeMap.put(department.getCode(), department.getId());
|
|
|
- }
|
|
|
-
|
|
|
- String result = apiUtil.doPost(apiPath, dataArray.toString(), null, 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(idCodeMap.get(jsonObject.get("clientId").getAsString()), jsonObject.get("orgIndexCode").getAsString());
|
|
|
+ for (Integer level : levelSet) {
|
|
|
+ JsonArray dataArray = new JsonArray();
|
|
|
+ for (Department department : dataList) {
|
|
|
+ if(tableData.containsKey(department.getId())){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if(department.getHierarchy().length() == level){
|
|
|
+ JsonObject paramJson = new JsonObject();
|
|
|
+ paramJson.addProperty("clientId", department.getCode());
|
|
|
+ paramJson.addProperty("orgIndexCode", department.getId().toString());
|
|
|
+ paramJson.addProperty("orgName", department.getName());
|
|
|
+ paramJson.addProperty("parentIndexCode", department.getParentId().toString());
|
|
|
+ if(department.getParentId() == 0){
|
|
|
+ paramJson.addProperty("parentIndexCode", "root000000");
|
|
|
+ }
|
|
|
+ paramJson.addProperty("orgCode", department.getCode());
|
|
|
+ dataArray.add(paramJson);
|
|
|
+ idCodeMap.put(department.getCode(), department.getId());
|
|
|
+ }
|
|
|
}
|
|
|
- //插入记录表
|
|
|
- insertRecord(db, tableName, idMap);
|
|
|
- }else{
|
|
|
+ String result = apiUtil.doPost(apiPath, dataArray.toString(), null, 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(idCodeMap.get(jsonObject.get("clientId").getAsString()), jsonObject.get("orgIndexCode").getAsString());
|
|
|
+ }
|
|
|
+ }else{
|
|
|
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //插入记录表
|
|
|
+ insertRecord(db, tableName, idMap);
|
|
|
return idMap;
|
|
|
}
|
|
|
|