| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- package biz.kq;
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- import com.google.gson.JsonObject;
- import core.co.haikang.HkApiClient;
- import core.co.haikang.HkProtocolC;
- import org.apache.commons.lang3.StringUtils;
- import ss.db.Db;
- import ss.db.SqlCmd;
- import java.io.PrintWriter;
- import java.io.StringWriter;
- import java.lang.reflect.Method;
- import java.sql.Timestamp;
- import java.time.LocalDateTime;
- import java.time.OffsetDateTime;
- import java.time.ZoneId;
- import java.util.ArrayList;
- import java.util.LinkedHashMap;
- import java.util.LinkedHashSet;
- import java.util.List;
- import java.util.Map;
- /**
- * 本地 JSP 手工排查用。
- * 不接入现有 serv 路由,只提供“按学生姓名实时查询海康 auth_config/search”的独立能力。
- */
- public final class HkAuthConfigSearchLocalV3U {
- private static final HkApiClient HK_API_CLIENT = new HkApiClient();
- private static final Gson GSON = new GsonBuilder()
- .setPrettyPrinting()
- .disableHtmlEscaping()
- .create();
- private static final String DEF_TAG_ID = "frs";
- private HkAuthConfigSearchLocalV3U() {
- }
- public static String queryByStudentNameJson(String studentName) {
- try {
- return GSON.toJson(queryByStudentName(studentName));
- } catch (Exception e) {
- Map<String, Object> errorMap = new LinkedHashMap<>();
- errorMap.put("ok", false);
- errorMap.put("implVersion", "2026-06-18-v3");
- errorMap.put("queryStudentName", StringUtils.trimToNull(studentName));
- errorMap.put("errorType", e.getClass().getName());
- errorMap.put("error", e.getMessage());
- errorMap.put("stackTrace", makeStackTrace(e));
- return GSON.toJson(errorMap);
- }
- }
- public static Map<String, Object> queryByStudentName(String studentName) throws Exception {
- String name = StringUtils.trimToNull(studentName);
- Map<String, Object> root = new LinkedHashMap<>();
- root.put("implVersion", "2026-06-18-v3");
- root.put("queryStudentName", name);
- root.put("queryTime", OffsetDateTime.now().toString());
- Map<String, Object> hkConnParm = makeHkConnParm();
- root.put("hkConnSummary", makeHkConnSummary(hkConnParm));
- if (StringUtils.isBlank(name)) {
- root.put("ok", false);
- root.put("msg", "缺少学生姓名,请使用 ?xm=姓名 或 ?学生姓名=姓名 访问。");
- root.put("studentCount", 0);
- root.put("studentList", List.of());
- return root;
- }
- try (Db gdba = Db.makeConn()) {
- if (gdba == null) {
- root.put("ok", false);
- root.put("msg", "数据库连接失败,Db.makeConn() 返回 null。");
- root.put("studentCount", 0);
- root.put("studentList", List.of());
- return root;
- }
- List<Map<String, Object>> studentList = loadStudentList(gdba, name);
- String matchMode = "exact";
- if (studentList.isEmpty()) {
- studentList = loadStudentListLike(gdba, name);
- matchMode = "like";
- }
- for (Map<String, Object> student : studentList) {
- long ryid = rdLong(student.get("ryid"));
- List<Map<String, Object>> itemList = loadLeaveAuthItemList(gdba, ryid);
- for (Map<String, Object> item : itemList)
- fillAuthConfigSearchResult(hkConnParm, item);
- student.put("leaveAuthItemCount", itemList.size());
- student.put("leaveAuthItemList", itemList);
- }
- root.put("ok", true);
- root.put("matchMode", matchMode);
- root.put("studentCount", studentList.size());
- root.put("studentList", studentList);
- root.put("msg", studentList.isEmpty() ? "未查询到匹配学生。" : "查询成功。");
- return root;
- }
- }
- private static Map<String, Object> makeHkConnParm() {
- Map<String, Object> hkConnParm = new LinkedHashMap<>(HkConnU.makeHkConnParm());
- String tagId = rdCfg("hk.leaveAuth.tagId", "HK_LEAVE_AUTH_TAG_ID");
- hkConnParm.put("tagId", StringUtils.defaultIfBlank(tagId, DEF_TAG_ID));
- return hkConnParm;
- }
- private static Map<String, Object> makeHkConnSummary(Map<String, Object> hkConnParm) {
- Map<String, Object> row = new LinkedHashMap<>();
- row.put("host", hkConnParm.get("host"));
- row.put("port", hkConnParm.get("port"));
- row.put("appKey", hkConnParm.get("appKey"));
- row.put("tagId", hkConnParm.get("tagId"));
- return row;
- }
- private static List<Map<String, Object>> loadStudentList(Db gdba, String studentName) throws Exception {
- String sql = ""
- + "select ryid,xm,ryh,rylbm,jlztm,bmid "
- + "from ry where xm=:xm order by ryid desc";
- SqlCmd sqlCmd = new SqlCmd(sql);
- sqlCmd.wrStrParm("xm", studentName);
- try {
- return gdba.doSelMapListByC(sqlCmd);
- } catch (Exception e) {
- throw new RuntimeException("loadStudentList 执行失败,xm=" + studentName, e);
- }
- }
- private static List<Map<String, Object>> loadStudentListLike(Db gdba, String studentName) throws Exception {
- String sql = ""
- + "select ryid,xm,ryh,rylbm,jlztm,bmid "
- + "from ry where xm like :xm order by ryid desc";
- SqlCmd sqlCmd = new SqlCmd(sql);
- sqlCmd.wrStrParm("xm", "%" + studentName + "%");
- try {
- return gdba.doSelMapListByC(sqlCmd);
- } catch (Exception e) {
- throw new RuntimeException("loadStudentListLike 执行失败,xm=" + studentName, e);
- }
- }
- private static List<Map<String, Object>> loadLeaveAuthItemList(Db gdba, long ryid) throws Exception {
- String sql = ""
- + "select * from ("
- + " select rc.rcid,rc.rclbm,rc.kssj,rc.jssj,"
- + " i.item_id,i.item_type,i.item_name,i.resource_scope,i.resource_keys,i.person_id,"
- + " i.auth_beg,i.auth_end,"
- + " i.sync_stage,i.platform_config_confirmed,i.config_search_matched,"
- + " i.delete_confirmed,i.config_task_id,i.delete_task_id,"
- + " i.delete_time,i.updated_at "
- + " from rc rc"
- + " join rchkla_item i on i.rcid=rc.rcid"
- + " where rc.fzryid=:ryid"
- + " order by i.updated_at desc nulls last,i.item_id desc"
- + " ) where rownum<=20";
- SqlCmd sqlCmd = new SqlCmd(sql);
- sqlCmd.wrLongParm("ryid", ryid);
- try {
- return gdba.doSelMapListByC(sqlCmd);
- } catch (Exception e) {
- throw new RuntimeException("loadLeaveAuthItemList 执行失败,ryid=" + ryid, e);
- }
- }
- private static void fillAuthConfigSearchResult(Map<String, Object> hkConnParm,
- Map<String, Object> item) {
- String personId = rdStr(item.get("person_id"));
- String resourceKeys = rdStr(item.get("resource_keys"));
- item.put("resourceKeyCount", makeAuthConfigResourceInfoList(resourceKeys).size());
- if (StringUtils.isAnyBlank(personId, resourceKeys)) {
- item.put("authConfigSearchSkipReason", "personId 或 resourceKeys 为空,跳过海康查询。");
- return;
- }
- Map<String, Object> req = makeAuthConfigSearchReq(personId, resourceKeys);
- item.put("authConfigSearchReq", req);
- try {
- Map<String, Object> resp = searchAuthConfigRespMapWithRaw(hkConnParm, req, item);
- item.put("authConfigSearchResp", resp);
- item.put("authConfigSearchRespBrief", makeSearchRespBrief(resp));
- item.put("currentWindowCount", countAuthConfigWindow(
- resp,
- rdStr(item.get("auth_beg")),
- rdStr(item.get("auth_end")),
- true));
- item.put("foreignWindowCount", countAuthConfigWindow(
- resp,
- rdStr(item.get("auth_beg")),
- rdStr(item.get("auth_end")),
- false));
- } catch (Exception e) {
- item.put("authConfigSearchError", e.getMessage());
- }
- }
- private static Map<String, Object> searchAuthConfigRespMapWithRaw(Map<String, Object> hkConnParm,
- Map<String, Object> req,
- Map<String, Object> item) throws Exception {
- Method method = HkApiClient.class.getDeclaredMethod("postHkJson", Map.class, String.class, Map.class);
- method.setAccessible(true);
- JsonObject respJson = (JsonObject) method.invoke(HK_API_CLIENT, hkConnParm, HkProtocolC.HK_AUTH_CONFIG_SEARCH_API_PATH, req);
- item.put("authConfigSearchRespRawJson", respJson == null ? null : respJson.toString());
- @SuppressWarnings("unchecked")
- Map<String, Object> respMap = GSON.fromJson(respJson, Map.class);
- return respMap;
- }
- private static Map<String, Object> makeAuthConfigSearchReq(String personId, String resourceKeys) {
- List<Map<String, Object>> resourceInfoList = makeAuthConfigResourceInfoList(resourceKeys);
- Map<String, Object> req = new LinkedHashMap<>();
- req.put("personDataIds", List.of(personId));
- req.put("personDataType", "person");
- req.put("resourceInfos", resourceInfoList);
- req.put("resourceDataType", "resource");
- req.put("pageNo", 1);
- req.put("pageSize", Math.max(20, Math.min(200, resourceInfoList.size())));
- return req;
- }
- private static List<Map<String, Object>> makeAuthConfigResourceInfoList(String resourceKeys) {
- LinkedHashSet<String> keySet = new LinkedHashSet<>();
- if (resourceKeys != null) {
- for (String part : resourceKeys.split("\\|")) {
- String key = rdStr(part);
- if (key != null)
- keySet.add(key);
- }
- }
- List<Map<String, Object>> out = new ArrayList<>();
- for (String key : keySet) {
- int idx = key.lastIndexOf('#');
- if (idx <= 0 || idx >= key.length() - 1)
- continue;
- String resourceIndexCode = key.substring(0, idx).trim();
- int channelNo = rdChannelNo(key.substring(idx + 1));
- Map<String, Object> row = new LinkedHashMap<>();
- row.put("resourceIndexCode", resourceIndexCode);
- row.put("resourceType", "acsDevice");
- row.put("channelNos", List.of(channelNo));
- out.add(row);
- }
- return out;
- }
- private static Map<String, Object> makeSearchRespBrief(Map<String, Object> resp) {
- Map<String, Object> row = new LinkedHashMap<>();
- row.put("code", resp == null ? null : resp.get("code"));
- row.put("msg", resp == null ? null : resp.get("msg"));
- Map<String, Object> dataMap = castMap(resp == null ? null : resp.get("data"));
- if (dataMap != null) {
- row.put("total", dataMap.get("total"));
- row.put("pageNo", dataMap.get("pageNo"));
- row.put("pageSize", dataMap.get("pageSize"));
- }
- return row;
- }
- private static int countAuthConfigWindow(Map<String, Object> resp,
- String authBeg,
- String authEnd,
- boolean currentWindow) {
- Map<String, Object> dataMap = castMap(resp == null ? null : resp.get("data"));
- if (dataMap == null)
- return 0;
- Object listObj = dataMap.get("list");
- if (!(listObj instanceof List<?> list))
- return 0;
- int count = 0;
- for (Object itemObj : list) {
- Map<String, Object> row = castMap(itemObj);
- if (row == null)
- continue;
- boolean same = sameTime(rdStr(row.get("startTime")), authBeg)
- && sameTime(rdStr(row.get("endTime")), authEnd);
- if (same == currentWindow)
- count++;
- }
- return count;
- }
- private static boolean sameTime(String hkText, String dbText) {
- LocalDateTime hkTime = parseHkTime(hkText);
- LocalDateTime dbTime = parseDbTime(dbText);
- return hkTime != null && dbTime != null && hkTime.equals(dbTime);
- }
- private static LocalDateTime parseHkTime(String text) {
- try {
- return OffsetDateTime.parse(text)
- .atZoneSameInstant(ZoneId.of("Asia/Shanghai"))
- .toLocalDateTime();
- } catch (Exception ignored) {
- return null;
- }
- }
- private static LocalDateTime parseDbTime(String text) {
- try {
- return Timestamp.valueOf(text).toLocalDateTime();
- } catch (Exception ignored) {
- return null;
- }
- }
- @SuppressWarnings("unchecked")
- private static Map<String, Object> castMap(Object obj) {
- if (obj instanceof Map<?, ?> map)
- return (Map<String, Object>) map;
- return null;
- }
- private static int rdChannelNo(String text) {
- try {
- return Math.max(1, (int) Math.round(Double.parseDouble(StringUtils.trim(text))));
- } catch (Exception ignored) {
- return 1;
- }
- }
- private static long rdLong(Object value) {
- if (value == null)
- return 0L;
- if (value instanceof Number number)
- return number.longValue();
- try {
- return Long.parseLong(String.valueOf(value).trim());
- } catch (Exception ignored) {
- return 0L;
- }
- }
- private static String rdStr(Object value) {
- if (value == null)
- return null;
- String text = String.valueOf(value).trim();
- return text.isEmpty() ? null : text;
- }
- private static String rdCfg(String propName, String envName) {
- String value = System.getProperty(propName);
- if (StringUtils.isNotBlank(value))
- return StringUtils.trim(value);
- value = System.getenv(envName);
- if (StringUtils.isNotBlank(value))
- return StringUtils.trim(value);
- return null;
- }
- private static String makeStackTrace(Throwable e) {
- if (e == null)
- return null;
- StringWriter sw = new StringWriter();
- PrintWriter pw = new PrintWriter(sw);
- e.printStackTrace(pw);
- pw.flush();
- return sw.toString();
- }
- }
|