HkAuthConfigSearchLocalV3U.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. package biz.kq;
  2. import com.google.gson.Gson;
  3. import com.google.gson.GsonBuilder;
  4. import com.google.gson.JsonObject;
  5. import core.co.haikang.HkApiClient;
  6. import core.co.haikang.HkProtocolC;
  7. import org.apache.commons.lang3.StringUtils;
  8. import ss.db.Db;
  9. import ss.db.SqlCmd;
  10. import java.io.PrintWriter;
  11. import java.io.StringWriter;
  12. import java.lang.reflect.Method;
  13. import java.sql.Timestamp;
  14. import java.time.LocalDateTime;
  15. import java.time.OffsetDateTime;
  16. import java.time.ZoneId;
  17. import java.util.ArrayList;
  18. import java.util.LinkedHashMap;
  19. import java.util.LinkedHashSet;
  20. import java.util.List;
  21. import java.util.Map;
  22. /**
  23. * 本地 JSP 手工排查用。
  24. * 不接入现有 serv 路由,只提供“按学生姓名实时查询海康 auth_config/search”的独立能力。
  25. */
  26. public final class HkAuthConfigSearchLocalV3U {
  27. private static final HkApiClient HK_API_CLIENT = new HkApiClient();
  28. private static final Gson GSON = new GsonBuilder()
  29. .setPrettyPrinting()
  30. .disableHtmlEscaping()
  31. .create();
  32. private static final String DEF_TAG_ID = "frs";
  33. private HkAuthConfigSearchLocalV3U() {
  34. }
  35. public static String queryByStudentNameJson(String studentName) {
  36. try {
  37. return GSON.toJson(queryByStudentName(studentName));
  38. } catch (Exception e) {
  39. Map<String, Object> errorMap = new LinkedHashMap<>();
  40. errorMap.put("ok", false);
  41. errorMap.put("implVersion", "2026-06-18-v3");
  42. errorMap.put("queryStudentName", StringUtils.trimToNull(studentName));
  43. errorMap.put("errorType", e.getClass().getName());
  44. errorMap.put("error", e.getMessage());
  45. errorMap.put("stackTrace", makeStackTrace(e));
  46. return GSON.toJson(errorMap);
  47. }
  48. }
  49. public static Map<String, Object> queryByStudentName(String studentName) throws Exception {
  50. String name = StringUtils.trimToNull(studentName);
  51. Map<String, Object> root = new LinkedHashMap<>();
  52. root.put("implVersion", "2026-06-18-v3");
  53. root.put("queryStudentName", name);
  54. root.put("queryTime", OffsetDateTime.now().toString());
  55. Map<String, Object> hkConnParm = makeHkConnParm();
  56. root.put("hkConnSummary", makeHkConnSummary(hkConnParm));
  57. if (StringUtils.isBlank(name)) {
  58. root.put("ok", false);
  59. root.put("msg", "缺少学生姓名,请使用 ?xm=姓名 或 ?学生姓名=姓名 访问。");
  60. root.put("studentCount", 0);
  61. root.put("studentList", List.of());
  62. return root;
  63. }
  64. try (Db gdba = Db.makeConn()) {
  65. if (gdba == null) {
  66. root.put("ok", false);
  67. root.put("msg", "数据库连接失败,Db.makeConn() 返回 null。");
  68. root.put("studentCount", 0);
  69. root.put("studentList", List.of());
  70. return root;
  71. }
  72. List<Map<String, Object>> studentList = loadStudentList(gdba, name);
  73. String matchMode = "exact";
  74. if (studentList.isEmpty()) {
  75. studentList = loadStudentListLike(gdba, name);
  76. matchMode = "like";
  77. }
  78. for (Map<String, Object> student : studentList) {
  79. long ryid = rdLong(student.get("ryid"));
  80. List<Map<String, Object>> itemList = loadLeaveAuthItemList(gdba, ryid);
  81. for (Map<String, Object> item : itemList)
  82. fillAuthConfigSearchResult(hkConnParm, item);
  83. student.put("leaveAuthItemCount", itemList.size());
  84. student.put("leaveAuthItemList", itemList);
  85. }
  86. root.put("ok", true);
  87. root.put("matchMode", matchMode);
  88. root.put("studentCount", studentList.size());
  89. root.put("studentList", studentList);
  90. root.put("msg", studentList.isEmpty() ? "未查询到匹配学生。" : "查询成功。");
  91. return root;
  92. }
  93. }
  94. private static Map<String, Object> makeHkConnParm() {
  95. Map<String, Object> hkConnParm = new LinkedHashMap<>(HkConnU.makeHkConnParm());
  96. String tagId = rdCfg("hk.leaveAuth.tagId", "HK_LEAVE_AUTH_TAG_ID");
  97. hkConnParm.put("tagId", StringUtils.defaultIfBlank(tagId, DEF_TAG_ID));
  98. return hkConnParm;
  99. }
  100. private static Map<String, Object> makeHkConnSummary(Map<String, Object> hkConnParm) {
  101. Map<String, Object> row = new LinkedHashMap<>();
  102. row.put("host", hkConnParm.get("host"));
  103. row.put("port", hkConnParm.get("port"));
  104. row.put("appKey", hkConnParm.get("appKey"));
  105. row.put("tagId", hkConnParm.get("tagId"));
  106. return row;
  107. }
  108. private static List<Map<String, Object>> loadStudentList(Db gdba, String studentName) throws Exception {
  109. String sql = ""
  110. + "select ryid,xm,ryh,rylbm,jlztm,bmid "
  111. + "from ry where xm=:xm order by ryid desc";
  112. SqlCmd sqlCmd = new SqlCmd(sql);
  113. sqlCmd.wrStrParm("xm", studentName);
  114. try {
  115. return gdba.doSelMapListByC(sqlCmd);
  116. } catch (Exception e) {
  117. throw new RuntimeException("loadStudentList 执行失败,xm=" + studentName, e);
  118. }
  119. }
  120. private static List<Map<String, Object>> loadStudentListLike(Db gdba, String studentName) throws Exception {
  121. String sql = ""
  122. + "select ryid,xm,ryh,rylbm,jlztm,bmid "
  123. + "from ry where xm like :xm order by ryid desc";
  124. SqlCmd sqlCmd = new SqlCmd(sql);
  125. sqlCmd.wrStrParm("xm", "%" + studentName + "%");
  126. try {
  127. return gdba.doSelMapListByC(sqlCmd);
  128. } catch (Exception e) {
  129. throw new RuntimeException("loadStudentListLike 执行失败,xm=" + studentName, e);
  130. }
  131. }
  132. private static List<Map<String, Object>> loadLeaveAuthItemList(Db gdba, long ryid) throws Exception {
  133. String sql = ""
  134. + "select * from ("
  135. + " select rc.rcid,rc.rclbm,rc.kssj,rc.jssj,"
  136. + " i.item_id,i.item_type,i.item_name,i.resource_scope,i.resource_keys,i.person_id,"
  137. + " i.auth_beg,i.auth_end,"
  138. + " i.sync_stage,i.platform_config_confirmed,i.config_search_matched,"
  139. + " i.delete_confirmed,i.config_task_id,i.delete_task_id,"
  140. + " i.delete_time,i.updated_at "
  141. + " from rc rc"
  142. + " join rchkla_item i on i.rcid=rc.rcid"
  143. + " where rc.fzryid=:ryid"
  144. + " order by i.updated_at desc nulls last,i.item_id desc"
  145. + " ) where rownum<=20";
  146. SqlCmd sqlCmd = new SqlCmd(sql);
  147. sqlCmd.wrLongParm("ryid", ryid);
  148. try {
  149. return gdba.doSelMapListByC(sqlCmd);
  150. } catch (Exception e) {
  151. throw new RuntimeException("loadLeaveAuthItemList 执行失败,ryid=" + ryid, e);
  152. }
  153. }
  154. private static void fillAuthConfigSearchResult(Map<String, Object> hkConnParm,
  155. Map<String, Object> item) {
  156. String personId = rdStr(item.get("person_id"));
  157. String resourceKeys = rdStr(item.get("resource_keys"));
  158. item.put("resourceKeyCount", makeAuthConfigResourceInfoList(resourceKeys).size());
  159. if (StringUtils.isAnyBlank(personId, resourceKeys)) {
  160. item.put("authConfigSearchSkipReason", "personId 或 resourceKeys 为空,跳过海康查询。");
  161. return;
  162. }
  163. Map<String, Object> req = makeAuthConfigSearchReq(personId, resourceKeys);
  164. item.put("authConfigSearchReq", req);
  165. try {
  166. Map<String, Object> resp = searchAuthConfigRespMapWithRaw(hkConnParm, req, item);
  167. item.put("authConfigSearchResp", resp);
  168. item.put("authConfigSearchRespBrief", makeSearchRespBrief(resp));
  169. item.put("currentWindowCount", countAuthConfigWindow(
  170. resp,
  171. rdStr(item.get("auth_beg")),
  172. rdStr(item.get("auth_end")),
  173. true));
  174. item.put("foreignWindowCount", countAuthConfigWindow(
  175. resp,
  176. rdStr(item.get("auth_beg")),
  177. rdStr(item.get("auth_end")),
  178. false));
  179. } catch (Exception e) {
  180. item.put("authConfigSearchError", e.getMessage());
  181. }
  182. }
  183. private static Map<String, Object> searchAuthConfigRespMapWithRaw(Map<String, Object> hkConnParm,
  184. Map<String, Object> req,
  185. Map<String, Object> item) throws Exception {
  186. Method method = HkApiClient.class.getDeclaredMethod("postHkJson", Map.class, String.class, Map.class);
  187. method.setAccessible(true);
  188. JsonObject respJson = (JsonObject) method.invoke(HK_API_CLIENT, hkConnParm, HkProtocolC.HK_AUTH_CONFIG_SEARCH_API_PATH, req);
  189. item.put("authConfigSearchRespRawJson", respJson == null ? null : respJson.toString());
  190. @SuppressWarnings("unchecked")
  191. Map<String, Object> respMap = GSON.fromJson(respJson, Map.class);
  192. return respMap;
  193. }
  194. private static Map<String, Object> makeAuthConfigSearchReq(String personId, String resourceKeys) {
  195. List<Map<String, Object>> resourceInfoList = makeAuthConfigResourceInfoList(resourceKeys);
  196. Map<String, Object> req = new LinkedHashMap<>();
  197. req.put("personDataIds", List.of(personId));
  198. req.put("personDataType", "person");
  199. req.put("resourceInfos", resourceInfoList);
  200. req.put("resourceDataType", "resource");
  201. req.put("pageNo", 1);
  202. req.put("pageSize", Math.max(20, Math.min(200, resourceInfoList.size())));
  203. return req;
  204. }
  205. private static List<Map<String, Object>> makeAuthConfigResourceInfoList(String resourceKeys) {
  206. LinkedHashSet<String> keySet = new LinkedHashSet<>();
  207. if (resourceKeys != null) {
  208. for (String part : resourceKeys.split("\\|")) {
  209. String key = rdStr(part);
  210. if (key != null)
  211. keySet.add(key);
  212. }
  213. }
  214. List<Map<String, Object>> out = new ArrayList<>();
  215. for (String key : keySet) {
  216. int idx = key.lastIndexOf('#');
  217. if (idx <= 0 || idx >= key.length() - 1)
  218. continue;
  219. String resourceIndexCode = key.substring(0, idx).trim();
  220. int channelNo = rdChannelNo(key.substring(idx + 1));
  221. Map<String, Object> row = new LinkedHashMap<>();
  222. row.put("resourceIndexCode", resourceIndexCode);
  223. row.put("resourceType", "acsDevice");
  224. row.put("channelNos", List.of(channelNo));
  225. out.add(row);
  226. }
  227. return out;
  228. }
  229. private static Map<String, Object> makeSearchRespBrief(Map<String, Object> resp) {
  230. Map<String, Object> row = new LinkedHashMap<>();
  231. row.put("code", resp == null ? null : resp.get("code"));
  232. row.put("msg", resp == null ? null : resp.get("msg"));
  233. Map<String, Object> dataMap = castMap(resp == null ? null : resp.get("data"));
  234. if (dataMap != null) {
  235. row.put("total", dataMap.get("total"));
  236. row.put("pageNo", dataMap.get("pageNo"));
  237. row.put("pageSize", dataMap.get("pageSize"));
  238. }
  239. return row;
  240. }
  241. private static int countAuthConfigWindow(Map<String, Object> resp,
  242. String authBeg,
  243. String authEnd,
  244. boolean currentWindow) {
  245. Map<String, Object> dataMap = castMap(resp == null ? null : resp.get("data"));
  246. if (dataMap == null)
  247. return 0;
  248. Object listObj = dataMap.get("list");
  249. if (!(listObj instanceof List<?> list))
  250. return 0;
  251. int count = 0;
  252. for (Object itemObj : list) {
  253. Map<String, Object> row = castMap(itemObj);
  254. if (row == null)
  255. continue;
  256. boolean same = sameTime(rdStr(row.get("startTime")), authBeg)
  257. && sameTime(rdStr(row.get("endTime")), authEnd);
  258. if (same == currentWindow)
  259. count++;
  260. }
  261. return count;
  262. }
  263. private static boolean sameTime(String hkText, String dbText) {
  264. LocalDateTime hkTime = parseHkTime(hkText);
  265. LocalDateTime dbTime = parseDbTime(dbText);
  266. return hkTime != null && dbTime != null && hkTime.equals(dbTime);
  267. }
  268. private static LocalDateTime parseHkTime(String text) {
  269. try {
  270. return OffsetDateTime.parse(text)
  271. .atZoneSameInstant(ZoneId.of("Asia/Shanghai"))
  272. .toLocalDateTime();
  273. } catch (Exception ignored) {
  274. return null;
  275. }
  276. }
  277. private static LocalDateTime parseDbTime(String text) {
  278. try {
  279. return Timestamp.valueOf(text).toLocalDateTime();
  280. } catch (Exception ignored) {
  281. return null;
  282. }
  283. }
  284. @SuppressWarnings("unchecked")
  285. private static Map<String, Object> castMap(Object obj) {
  286. if (obj instanceof Map<?, ?> map)
  287. return (Map<String, Object>) map;
  288. return null;
  289. }
  290. private static int rdChannelNo(String text) {
  291. try {
  292. return Math.max(1, (int) Math.round(Double.parseDouble(StringUtils.trim(text))));
  293. } catch (Exception ignored) {
  294. return 1;
  295. }
  296. }
  297. private static long rdLong(Object value) {
  298. if (value == null)
  299. return 0L;
  300. if (value instanceof Number number)
  301. return number.longValue();
  302. try {
  303. return Long.parseLong(String.valueOf(value).trim());
  304. } catch (Exception ignored) {
  305. return 0L;
  306. }
  307. }
  308. private static String rdStr(Object value) {
  309. if (value == null)
  310. return null;
  311. String text = String.valueOf(value).trim();
  312. return text.isEmpty() ? null : text;
  313. }
  314. private static String rdCfg(String propName, String envName) {
  315. String value = System.getProperty(propName);
  316. if (StringUtils.isNotBlank(value))
  317. return StringUtils.trim(value);
  318. value = System.getenv(envName);
  319. if (StringUtils.isNotBlank(value))
  320. return StringUtils.trim(value);
  321. return null;
  322. }
  323. private static String makeStackTrace(Throwable e) {
  324. if (e == null)
  325. return null;
  326. StringWriter sw = new StringWriter();
  327. PrintWriter pw = new PrintWriter(sw);
  328. e.printStackTrace(pw);
  329. pw.flush();
  330. return sw.toString();
  331. }
  332. }