|
|
@@ -1,11 +1,15 @@
|
|
|
package com.xjrsoft.module.hikvision.util;
|
|
|
|
|
|
import cn.hutool.db.Db;
|
|
|
+import com.google.gson.JsonArray;
|
|
|
+import com.google.gson.JsonElement;
|
|
|
import com.google.gson.JsonObject;
|
|
|
import com.google.gson.JsonParser;
|
|
|
-import com.xjrsoft.module.base.entity.BaseClass;
|
|
|
-import com.xjrsoft.module.schedule.util.ScheduleUtil;
|
|
|
+import com.xjrsoft.module.organization.entity.Department;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
@@ -14,48 +18,60 @@ import java.util.Map;
|
|
|
* @author dzx
|
|
|
* @date 2024/5/10
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
public class DataUtil {
|
|
|
-
|
|
|
- public Map<Long, String> insertDepartment(Db db, String tableName, Map<Long, String> gradeMap,
|
|
|
- Map<Long, String> teacherMap, String semesterSerialNo, Map<Long, String> ids, Map<Long, String> classroomMap) throws Exception {
|
|
|
- String sql = "SELECT * FROM " + tableName + " WHERE delete_mark = 0 and is_graduate = 1";
|
|
|
- List<BaseClass> dataList = db.query(sql, BaseClass.class);
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 同步组织数据
|
|
|
+ */
|
|
|
+ public Map<Long, String> insertDepartment(Db db, String tableName) throws Exception {
|
|
|
+ String 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();
|
|
|
- for (BaseClass baseClass : dataList) {
|
|
|
- String url = ScheduleUtil.apiUrl + "class/create";
|
|
|
+ ApiUtil apiUtil = new ApiUtil();
|
|
|
+ JsonArray dataArray = new JsonArray();
|
|
|
+
|
|
|
+ Map<String, Long> idCodeMap = new HashMap<>();
|
|
|
+ for (Department department : dataList) {
|
|
|
JsonObject paramJson = new JsonObject();
|
|
|
- paramJson.addProperty("teacherSerialNo", teacherMap.get(baseClass.getTeacherId()));
|
|
|
- paramJson.addProperty("name", baseClass.getName());
|
|
|
-
|
|
|
- paramJson.addProperty("semesterSerialNo", semesterSerialNo);
|
|
|
- paramJson.addProperty("eduYearSerialNo", gradeMap.get(baseClass.getGradeId()));
|
|
|
- paramJson.addProperty("extendId", baseClass.getId());
|
|
|
- paramJson.addProperty("classRoomSerialNo", classroomMap.get(baseClass.getClassroomId()));
|
|
|
- if(ids != null && ids.get(baseClass.getId()) != null){
|
|
|
- url = ScheduleUtil.apiUrl + "class/update";
|
|
|
- paramJson.addProperty("serialNo", ids.get(baseClass.getId()));
|
|
|
- long timestamp = System.currentTimeMillis();
|
|
|
- //生成签名
|
|
|
- String sign = ScheduleUtil.createSign(timestamp);
|
|
|
- String result = ScheduleUtil.doPost(url, paramJson.toString(), sign, timestamp);
|
|
|
- continue;
|
|
|
- }
|
|
|
+ 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());
|
|
|
+ }
|
|
|
|
|
|
- //获取时间戳
|
|
|
- long timestamp = System.currentTimeMillis();
|
|
|
- //生成签名
|
|
|
- String sign = ScheduleUtil.createSign(timestamp);
|
|
|
- String result = ScheduleUtil.doPost(url, paramJson.toString(), sign, timestamp);
|
|
|
- if(result == null){
|
|
|
- continue;
|
|
|
+ String result = apiUtil.doPost(apiPath, dataArray.toString(), 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());
|
|
|
}
|
|
|
- JsonObject resultJson = jsonParser.parse(result).getAsJsonObject();
|
|
|
- idMap.put(baseClass.getId(), resultJson.get("data").getAsString());
|
|
|
+ //插入记录表
|
|
|
+ insertRecord(db, tableName, idMap);
|
|
|
+ }else{
|
|
|
+
|
|
|
}
|
|
|
- //插入记录表
|
|
|
-// insertRecord(db, tableName, idMap);
|
|
|
+
|
|
|
return idMap;
|
|
|
}
|
|
|
+
|
|
|
+ void insertRecord(Db db, String tableName, Map<Long, String> idsMap) throws SQLException {
|
|
|
+ if(idsMap.isEmpty()){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<String> sqls = new ArrayList<>();
|
|
|
+ for (Long sourceId : idsMap.keySet()) {
|
|
|
+ String sql = "INSERT INTO jianyue_data(create_date,table_name,source_id,jianyue_id) value(now(),'"
|
|
|
+ + tableName + "'," + sourceId + ",'" + idsMap.get(sourceId) + "')";
|
|
|
+ sqls.add(sql);
|
|
|
+ }
|
|
|
+ db.executeBatch(sqls);
|
|
|
+ };
|
|
|
}
|