HikvisionBaseDataTaskTest.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.utils.DatasourceUtil;
  10. import com.xjrsoft.module.hikvision.util.ApiUtil;
  11. import com.xjrsoft.module.hikvision.util.DataUtil;
  12. import com.xjrsoft.module.organization.entity.Department;
  13. import com.xjrsoft.module.schedule.entity.JianyueData;
  14. import com.xjrsoft.module.teacher.entity.XjrUser;
  15. import org.junit.jupiter.api.Test;
  16. import org.junit.runner.RunWith;
  17. import org.springframework.boot.test.context.SpringBootTest;
  18. import org.springframework.test.context.junit4.SpringRunner;
  19. import javax.sql.DataSource;
  20. import java.sql.SQLException;
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.HashSet;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Set;
  27. import static org.junit.jupiter.api.Assertions.*;
  28. /**
  29. * @author dzx
  30. * @date 2024/5/14
  31. */
  32. @RunWith(SpringRunner.class)
  33. @SpringBootTest(classes = XjrSoftApplication.class)
  34. class HikvisionBaseDataTaskTest {
  35. @Test
  36. void test() throws Exception {
  37. DataSource datasource = DatasourceUtil.getDataSource(GlobalConstant.DEFAULT_DATASOURCE_KEY);
  38. Db use = Db.use(datasource);
  39. String sql = "SELECT distinct table_name FROM hikvision_data WHERE 1 = 1";
  40. List<JianyueData> query = use.query(sql, JianyueData.class);
  41. Set<String> tables = new HashSet<>();
  42. for (JianyueData jianyueData : query) {
  43. tables.add(jianyueData.getTableName());
  44. }
  45. sql = "SELECT * FROM hikvision_data WHERE 0 = 0";
  46. List<JianyueData> list = use.query(sql, JianyueData.class);
  47. Map<String, Map<Long, String>> dataMap = new HashMap<>();
  48. for (String table : tables) {
  49. Map<Long, String> tableData = new HashMap<>();
  50. for (JianyueData jianyueData : list) {
  51. if(!table.equals(jianyueData.getTableName())){
  52. continue;
  53. }
  54. tableData.put(jianyueData.getSourceId(), jianyueData.getJianyueId());
  55. }
  56. dataMap.put(table, tableData);
  57. }
  58. DataUtil dataUtil = new DataUtil();
  59. String tableName = "xjr_department";
  60. Map<Long, String> department = dataUtil.insertDepartment(use, tableName, null);
  61. //System.out.println(department.toString());
  62. // selectOrg(use, tableName);
  63. // deleteOrg(use, tableName);
  64. // selecAllPersonById(use);
  65. // selectResource(use);
  66. }
  67. /**
  68. * 查询资源信息
  69. * @param db
  70. * @throws SQLException
  71. */
  72. void selectResource(Db db) throws SQLException {
  73. String apiPath = "/api/irds/v2/resource/resourcesByParams";
  74. JsonObject jsonObject = new JsonObject();
  75. jsonObject.addProperty("pageNo", 1);
  76. jsonObject.addProperty("pageSize", 500);
  77. jsonObject.addProperty("resourceType", "door");
  78. ApiUtil apiUtil = new ApiUtil();
  79. String result = apiUtil.doPost(apiPath, jsonObject.toString(), null, null);
  80. System.out.println("------------------------------------------------------------------------");
  81. System.out.println(result);
  82. }
  83. void insertRecord(Db db, String tableName, Map<Long, String> idsMap) throws SQLException {
  84. if(idsMap.isEmpty()){
  85. return;
  86. }
  87. List<String> sqls = new ArrayList<>();
  88. for (Long sourceId : idsMap.keySet()) {
  89. String sql = "INSERT INTO hikvision_data(create_date,table_name,source_id,hikvision_id) value(now(),'"
  90. + tableName + "'," + sourceId + ",'" + idsMap.get(sourceId) + "')";
  91. sqls.add(sql);
  92. }
  93. db.executeBatch(sqls);
  94. };
  95. void selecAllPersonById(Db db) throws SQLException {
  96. String apiPath = "/api/resource/v2/person/advance/personList";
  97. JsonObject jsonObject = new JsonObject();
  98. jsonObject.addProperty("pageNo", 1);
  99. jsonObject.addProperty("pageSize", 500);
  100. jsonObject.addProperty("personIds", "b9d90bdd018447f2951dadfaffd1e411");
  101. ApiUtil apiUtil = new ApiUtil();
  102. String result = apiUtil.doPost(apiPath, jsonObject.toString(), null, null);
  103. }
  104. void selecAllPerson(Db db) throws SQLException {
  105. String apiPath = "/api/resource/v2/person/personList";
  106. JsonObject jsonObject = new JsonObject();
  107. jsonObject.addProperty("pageNo", 1);
  108. jsonObject.addProperty("pageSize", 500);
  109. ApiUtil apiUtil = new ApiUtil();
  110. String result = apiUtil.doPost(apiPath, jsonObject.toString(), null, null);
  111. System.out.println(result);
  112. String sql = "SELECT t1.* FROM xjr_user t1" +
  113. " INNER JOIN base_teacher t2 ON t1.id = t2.user_id" +
  114. " WHERE t1.delete_mark = 0 AND t2.delete_mark = 0";
  115. List<XjrUser> dataList = db.query(sql, XjrUser.class);
  116. Map<String, Long> phoneMap = new HashMap<>();
  117. for (XjrUser xjrUser : dataList) {
  118. phoneMap.put(xjrUser.getMobile(), xjrUser.getId());
  119. }
  120. JsonParser jsonParser = new JsonParser();
  121. JsonObject resultJson = jsonParser.parse(result).getAsJsonObject();
  122. JsonArray array = resultJson.get("data").getAsJsonObject().get("list").getAsJsonArray();
  123. Map<Long, String> idMap = new HashMap<>();
  124. for (JsonElement jsonElement : array) {
  125. JsonObject element = jsonElement.getAsJsonObject();
  126. if(!element.has("phoneNo") || element.get("phoneNo").isJsonNull()){
  127. System.out.println(element.toString());
  128. continue;
  129. }
  130. String phoneNo = element.get("phoneNo").getAsString();
  131. idMap.put(phoneMap.get(phoneNo), element.get("personId").getAsString());
  132. }
  133. insertRecord(db, "base_teacher", idMap);
  134. }
  135. void selectOrg(Db db, String tableName) throws SQLException {
  136. String sql = "SELECT * FROM " + tableName + " WHERE delete_mark = 0";
  137. List<Department> dataList = db.query(sql, Department.class);
  138. String apiPath = "/api/resource/v1/org/orgList";
  139. JsonObject jsonObject = new JsonObject();
  140. jsonObject.addProperty("pageNo", 1);
  141. jsonObject.addProperty("pageSize", 500);
  142. ApiUtil apiUtil = new ApiUtil();
  143. String result = apiUtil.doPost(apiPath, jsonObject.toString(), null);
  144. System.out.println("-----------------------------------------------------");
  145. System.out.println(result);
  146. }
  147. void deleteOrg(Db db, String tableName) throws SQLException {
  148. String sql = "SELECT * FROM " + tableName + " WHERE delete_mark = 0 ORDER BY LENGTH(hierarchy)";
  149. List<Department> dataList = db.query(sql, Department.class);
  150. String apiPath = "/api/resource/v1/org/batch/delete";
  151. Map<Long, String> idMap = new HashMap<>();
  152. JsonParser jsonParser = new JsonParser();
  153. ApiUtil apiUtil = new ApiUtil();
  154. JsonArray dataArray = new JsonArray();
  155. for (Department department : dataList) {
  156. dataArray.add(department.getId().toString());
  157. }
  158. JsonObject paramJson = new JsonObject();
  159. paramJson.add("indexCodes", dataArray);
  160. Map<String, String> headerMap = new HashMap<>();
  161. headerMap.put("tagId", "deleteOrg");
  162. String result = apiUtil.doPost(apiPath, paramJson.toString(), null, headerMap);
  163. System.out.println(result);
  164. }
  165. }