HikvisionBaseDataTaskTest.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. package com.xjrsoft.module.job;
  2. import cn.hutool.db.Db;
  3. import com.google.gson.JsonArray;
  4. import com.google.gson.JsonElement;
  5. import com.google.gson.JsonObject;
  6. import com.google.gson.JsonParser;
  7. import com.xjrsoft.XjrSoftApplication;
  8. import com.xjrsoft.common.constant.GlobalConstant;
  9. import com.xjrsoft.common.mybatis.SqlRunnerAdapter;
  10. import com.xjrsoft.common.utils.DatasourceUtil;
  11. import com.xjrsoft.common.utils.SqlRunnerAdapterUtil;
  12. import com.xjrsoft.module.base.entity.BaseClass;
  13. import com.xjrsoft.module.evaluate.controller.EvaluateItemController;
  14. import com.xjrsoft.module.hikvision.entity.HikvisionData;
  15. import com.xjrsoft.module.hikvision.util.ApiUtil;
  16. import com.xjrsoft.module.hikvision.util.DataUtil;
  17. import com.xjrsoft.module.hikvision.util.OutInRecordUtil;
  18. import com.xjrsoft.module.organization.entity.Department;
  19. import com.xjrsoft.module.schedule.entity.JianyueData;
  20. import com.xjrsoft.module.teacher.entity.XjrUser;
  21. import com.xjrsoft.module.teacher.mapper.FaceImportMapper;
  22. import com.yomahub.liteflow.util.JsonUtil;
  23. import lombok.var;
  24. import org.junit.jupiter.api.Test;
  25. import org.junit.runner.RunWith;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.boot.test.context.SpringBootTest;
  28. import org.springframework.test.context.junit4.SpringRunner;
  29. import javax.sql.DataSource;
  30. import java.sql.SQLException;
  31. import java.util.ArrayList;
  32. import java.util.HashMap;
  33. import java.util.HashSet;
  34. import java.util.List;
  35. import java.util.Map;
  36. import java.util.Set;
  37. /**
  38. * @author dzx
  39. * @date 2024/5/14
  40. */
  41. @RunWith(SpringRunner.class)
  42. @SpringBootTest(classes = XjrSoftApplication.class)
  43. class HikvisionBaseDataTaskTest {
  44. @Autowired
  45. private FaceImportMapper faceImportMapper;
  46. @Autowired
  47. EvaluateItemController evaluateItemController;
  48. OutInRecordUtil outInRecordUtil = new OutInRecordUtil();
  49. @Test
  50. void test2(){
  51. String sql = "select * from base_class";
  52. List<Map<String, Object>> maps = SqlRunnerAdapter.db().selectList(sql, BaseClass.class);
  53. for (Map<String, Object> map : maps) {
  54. BaseClass baseClass = SqlRunnerAdapterUtil.convertMapToEntity(map, BaseClass.class);
  55. System.out.println(JsonUtil.toJsonString(baseClass));
  56. }
  57. sql = "SELECT DISTINCT LENGTH(hierarchy) FROM xjr_department WHERE delete_mark = 0";
  58. List<Map<String, Object>> list = SqlRunnerAdapter.db().selectList(sql);
  59. Set<Integer> levelSet = new HashSet<>();
  60. for (Map<String, Object> strings : list) {
  61. Integer[] strings1 = SqlRunnerAdapterUtil.convertMapToIntegerArray(strings);
  62. System.out.println(strings1.toString());
  63. levelSet.add(strings1[0]);
  64. }
  65. }
  66. @Test
  67. void test() throws Exception {
  68. DataSource datasource = DatasourceUtil.getDataSource(GlobalConstant.DEFAULT_DATASOURCE_KEY);
  69. Db use = Db.use(datasource);
  70. // String sql = "SELECT distinct table_name FROM hikvision_data WHERE 1 = 1";
  71. // List<JianyueData> query = use.query(sql, JianyueData.class);
  72. // Set<String> tables = new HashSet<>();
  73. // for (JianyueData jianyueData : query) {
  74. // tables.add(jianyueData.getTableName());
  75. // }
  76. // sql = "SELECT * FROM hikvision_data WHERE 0 = 0";
  77. // List<HikvisionData> list = use.query(sql, HikvisionData.class);
  78. // Map<String, Map<String, String>> dataMap = new HashMap<>();
  79. // for (String table : tables) {
  80. // Map<String, String> tableData = new HashMap<>();
  81. // for (HikvisionData hikvisiondata : list) {
  82. // if(!table.equals(hikvisiondata.getTableName())){
  83. // continue;
  84. // }
  85. // tableData.put(hikvisiondata.getSourceId(), hikvisiondata.getHikvisionId());
  86. // }
  87. // dataMap.put(table, tableData);
  88. // }
  89. // DataUtil dataUtil = new DataUtil();
  90. // String tableName = "xjr_department";
  91. // Map<String, String> department = dataUtil.insertDepartment(use, tableName, dataMap.get(tableName));
  92. // if(department.isEmpty() && dataMap.get(tableName) != null){
  93. // department = dataMap.get(tableName);
  94. // }
  95. //
  96. // //推送教职工
  97. // tableName = "base_teacher";
  98. // dataUtil.insertTeacher(use, tableName, dataMap.get(tableName), department);
  99. //
  100. //推送车辆
  101. // String carTableName = "car_message_apply";
  102. // Map<String, String> baseClass = dataMap.get(carTableName);
  103. // dataUtil.insertCar(use, carTableName,baseClass, faceImportMapper);
  104. // tableName = "base_class";
  105. // Map<String, String> baseClass = dataMap.get(tableName);
  106. // Map<String, String> classes = dataUtil.insertClass(use, tableName, baseClass);
  107. // tableName = "base_student";
  108. // Map<String, String> baseStudent = dataMap.get(tableName);
  109. // Map<String, String> student = dataUtil.insertStudentOne(use, tableName, baseStudent);
  110. // selectOrg(use, "base_class");
  111. //查询车辆
  112. // String carTableName = "car_message_apply";
  113. // selectCar(use, carTableName);
  114. // outInRecordUtil.GetVehicleRecord(use,faceImportMapper);
  115. outInRecordUtil.GetTeacherAndStudentRecords(use,faceImportMapper);
  116. //// selecAllPersonById(use);
  117. // selectResource(use);
  118. }
  119. public static void selectCar(Db db, String tableName) throws SQLException {
  120. String sql = "SELECT * FROM " + tableName + " WHERE delete_mark = 0";
  121. List<Department> dataList = db.query(sql, Department.class);
  122. String apiPath = "/api/resource/v2/vehicle/advance/vehicleList";
  123. JsonObject jsonObject = new JsonObject();
  124. jsonObject.addProperty("pageNo", 1);
  125. jsonObject.addProperty("pageSize", 500);
  126. ApiUtil apiUtil = new ApiUtil();
  127. String result = apiUtil.doPost(apiPath, jsonObject.toString(), null);
  128. System.out.println("-----------------------------------------------------");
  129. System.out.println(result);
  130. }
  131. public static void insertRecord(Db db, String tableName, Map<?, String> idsMap) throws SQLException {
  132. if (idsMap.isEmpty()) {
  133. return;
  134. }
  135. var sqls = new ArrayList<String>();
  136. for (var entry : idsMap.entrySet()) {
  137. var sourceId = entry.getKey();
  138. var sql = "INSERT INTO hikvision_data(create_date, table_name, source_id, hikvision_id) " +
  139. "VALUES(now(), '" + tableName + "', '" + sourceId + "', '" + entry.getValue() + "')";
  140. sqls.add(sql);
  141. }
  142. db.executeBatch(sqls);
  143. }
  144. public Map<String, String> insertStudentOne(Db db, String tableName, Map<String, String> tableData) throws Exception {
  145. String sql = "SELECT t1.id,t1.name,CONCAT(t3.id,'-',t4.name,'-',t3.class_type,'-',t2.stduy_status)," +
  146. " t1.user_name,t1.gender,DATE_FORMAT(t1.birth_date, '%Y-%m-%d'),t1.mobile,t1.email,t1.credential_type,t1.credential_number FROM xjr_user t1" +
  147. " INNER JOIN base_student_school_roll t2 ON t1.id = t2.user_id" +
  148. " INNER JOIN base_class t3 ON t2.class_id = t3.id" +
  149. " INNER JOIN base_grade t4 ON t3.grade_id = t4.id" +
  150. " WHERE t1.delete_mark = 0 AND t2.delete_mark = 0" +
  151. " AND t3.class_type IS NOT NULL" +
  152. " AND t1.id NOT IN (SELECT source_id FROM hikvision_data WHERE table_name = 'base_student'\n" +
  153. ") AND t4.name NOT IN ('2021级','2020级')";
  154. List<String[]> list = db.query(sql, String[].class);
  155. Map<Integer, String> clientMap = new HashMap<>();
  156. String apiPath = "/api/resource/v2/person/single/add";
  157. Map<String, String> idMap = new HashMap<>();
  158. JsonParser jsonParser = new JsonParser();
  159. ApiUtil apiUtil = new ApiUtil();
  160. Map<String, String> header = new HashMap<>();
  161. header.put("tagId", "insert_student");
  162. for(int i = 0; i < list.size(); i ++){
  163. String[] el = list.get(i);
  164. if(tableData != null && tableData.containsKey(el[0])){
  165. continue;
  166. }
  167. JsonObject paramJson = new JsonObject();
  168. paramJson.addProperty("clientId", i);
  169. paramJson.addProperty("personId", el[0]);
  170. paramJson.addProperty("personName", el[1]);
  171. paramJson.addProperty("orgIndexCode", el[2]);
  172. paramJson.addProperty("phoneNo", el[6]);
  173. paramJson.addProperty("jobNo", el[3]);
  174. paramJson.addProperty("birthday", el[5]);
  175. paramJson.addProperty("personType", 2);
  176. int gender;
  177. switch (el[4]) {
  178. case "SB10001":
  179. gender = 1;
  180. break;
  181. case "SB10002":
  182. gender = 2;
  183. break;
  184. default:
  185. gender = 0;
  186. break;
  187. }
  188. paramJson.addProperty("gender", gender);
  189. clientMap.put(i, el[0]);
  190. System.out.println("请求参数:" + paramJson.toString());
  191. String result = apiUtil.doPost(apiPath, paramJson.toString(), null, header);
  192. System.out.println("返回结果:" + result);
  193. JsonElement parse = jsonParser.parse(result);
  194. JsonObject resultJson = parse.getAsJsonObject();
  195. if("0".equals(resultJson.get("code").getAsString()) && "success".equals(resultJson.get("msg").getAsString())){
  196. JsonObject array = resultJson.get("data").getAsJsonObject();
  197. idMap.put(el[0], array.get("personId").getAsString());
  198. }
  199. }
  200. insertRecord(db, tableName, idMap);
  201. //插入记录表
  202. return idMap;
  203. }
  204. void deleteClass(Db db) throws SQLException {
  205. String apiPath = "/api/resource/v1/org/orgList";
  206. JsonObject jsonObject = new JsonObject();
  207. jsonObject.addProperty("pageNo", 1);
  208. jsonObject.addProperty("pageSize", 500);
  209. ApiUtil apiUtil = new ApiUtil();
  210. String result = apiUtil.doPost(apiPath, jsonObject.toString(), null);
  211. String sql = "SELECT * FROM hikvision_data WHERE table_name = 'base_class' order by length(hikvision_id) desc";
  212. List<HikvisionData> dataList = db.query(sql, HikvisionData.class);
  213. apiPath = "/api/resource/v1/org/batch/delete";
  214. Map<Long, String> idMap = new HashMap<>();
  215. JsonParser jsonParser = new JsonParser();
  216. JsonArray dataArray = new JsonArray();
  217. for (HikvisionData department : dataList) {
  218. dataArray.add(department.getHikvisionId());
  219. }
  220. JsonObject paramJson = new JsonObject();
  221. paramJson.add("indexCodes", dataArray);
  222. Map<String, String> headerMap = new HashMap<>();
  223. headerMap.put("tagId", "deleteOrg");
  224. result = apiUtil.doPost(apiPath, paramJson.toString(), null, headerMap);
  225. System.out.println(result);
  226. }
  227. /**
  228. * 查询资源信息
  229. * @param db
  230. * @throws SQLException
  231. */
  232. void selectResource(Db db) throws SQLException {
  233. String apiPath = "/api/irds/v2/resource/resourcesByParams";
  234. JsonObject jsonObject = new JsonObject();
  235. jsonObject.addProperty("pageNo", 1);
  236. jsonObject.addProperty("pageSize", 500);
  237. jsonObject.addProperty("resourceType", "door");
  238. ApiUtil apiUtil = new ApiUtil();
  239. String result = apiUtil.doPost(apiPath, jsonObject.toString(), null, null);
  240. System.out.println("------------------------------------------------------------------------");
  241. System.out.println(result);
  242. }
  243. void selecAllPersonById(Db db) throws SQLException {
  244. String apiPath = "/api/resource/v2/person/advance/personList";
  245. JsonObject jsonObject = new JsonObject();
  246. jsonObject.addProperty("pageNo", 1);
  247. jsonObject.addProperty("pageSize", 500);
  248. jsonObject.addProperty("personIds", "654321987655613");
  249. ApiUtil apiUtil = new ApiUtil();
  250. String result = apiUtil.doPost(apiPath, jsonObject.toString(), null, null);
  251. }
  252. void selecAllPerson(Db db) throws SQLException {
  253. String apiPath = "/api/resource/v2/person/personList";
  254. JsonObject jsonObject = new JsonObject();
  255. jsonObject.addProperty("pageNo", 1);
  256. jsonObject.addProperty("pageSize", 500);
  257. ApiUtil apiUtil = new ApiUtil();
  258. String result = apiUtil.doPost(apiPath, jsonObject.toString(), null, null);
  259. System.out.println(result);
  260. String sql = "SELECT t1.* FROM xjr_user t1" +
  261. " INNER JOIN base_teacher t2 ON t1.id = t2.user_id" +
  262. " WHERE t1.delete_mark = 0 AND t2.delete_mark = 0";
  263. List<XjrUser> dataList = db.query(sql, XjrUser.class);
  264. Map<String, Long> phoneMap = new HashMap<>();
  265. for (XjrUser xjrUser : dataList) {
  266. phoneMap.put(xjrUser.getMobile(), xjrUser.getId());
  267. }
  268. JsonParser jsonParser = new JsonParser();
  269. JsonObject resultJson = jsonParser.parse(result).getAsJsonObject();
  270. JsonArray array = resultJson.get("data").getAsJsonObject().get("list").getAsJsonArray();
  271. Map<Long, String> idMap = new HashMap<>();
  272. for (JsonElement jsonElement : array) {
  273. JsonObject element = jsonElement.getAsJsonObject();
  274. if(!element.has("phoneNo") || element.get("phoneNo").isJsonNull()){
  275. System.out.println(element.toString());
  276. continue;
  277. }
  278. String phoneNo = element.get("phoneNo").getAsString();
  279. idMap.put(phoneMap.get(phoneNo), element.get("personId").getAsString());
  280. }
  281. insertRecord(db, "base_teacher", idMap);
  282. }
  283. void selectOrg(Db db, String tableName) throws SQLException {
  284. String sql = "SELECT * FROM " + tableName + " WHERE delete_mark = 0";
  285. List<Department> dataList = db.query(sql, Department.class);
  286. String apiPath = "/api/resource/v1/org/orgList";
  287. JsonObject jsonObject = new JsonObject();
  288. jsonObject.addProperty("pageNo", 1);
  289. jsonObject.addProperty("pageSize", 500);
  290. ApiUtil apiUtil = new ApiUtil();
  291. String result = apiUtil.doPost(apiPath, jsonObject.toString(), null);
  292. System.out.println("-----------------------------------------------------");
  293. System.out.println(result);
  294. }
  295. void deleteOrg(Db db, String tableName) throws SQLException {
  296. String sql = "SELECT * FROM " + tableName + " WHERE delete_mark = 0 ORDER BY LENGTH(hierarchy)";
  297. List<Department> dataList = db.query(sql, Department.class);
  298. String apiPath = "/api/resource/v1/org/batch/delete";
  299. Map<Long, String> idMap = new HashMap<>();
  300. JsonParser jsonParser = new JsonParser();
  301. ApiUtil apiUtil = new ApiUtil();
  302. JsonArray dataArray = new JsonArray();
  303. for (Department department : dataList) {
  304. dataArray.add(department.getId().toString());
  305. }
  306. JsonObject paramJson = new JsonObject();
  307. paramJson.add("indexCodes", dataArray);
  308. Map<String, String> headerMap = new HashMap<>();
  309. headerMap.put("tagId", "deleteOrg");
  310. String result = apiUtil.doPost(apiPath, paramJson.toString(), null, headerMap);
  311. System.out.println(result);
  312. }
  313. }