Răsfoiți Sursa

新增: 本地海康权限查询JSP排查页

1. 新增 web/auth_config_search.jsp,支持通过 localhost:8082/auth_config_search.jsp?xm=学生姓名 直接触发查询,返回 JSON 结果。

2. 新增 src/biz/kq/HkAuthConfigSearchLocalU.java,独立实现按学生姓名查询本地 RCHKLA_ITEM,并实时调用海康 auth_config/search 返回请求体、返回摘要、当前时间窗命中数与异窗命中数。

3. 新增 docs/2026-06-18_localhost_auth_config_search_jsp使用说明.md,说明访问方式、连接参数和返回内容。验证:已执行 javac 编译新增类通过,未改动现有 serv/xml 路由与业务代码。
“gaohp” 5 luni în urmă
părinte
comite
123bb96a71

+ 60 - 0
docs/2026-06-18_localhost_auth_config_search_jsp使用说明.md

@@ -0,0 +1,60 @@
+# localhost:8082/auth_config_search.jsp 使用说明
+
+## 1. 目的
+
+- 本功能用于本地启动当前项目后,直接通过 JSP 按学生姓名查询海康 `auth_config/search` 结果。
+- 为了避免影响现有业务链路,本次实现采用独立 JSP + 独立工具类方式,不接入现有 `serv/xml` 路由。
+
+## 2. 新增文件
+
+- `web/auth_config_search.jsp`
+- `src/biz/kq/HkAuthConfigSearchLocalU.java`
+
+## 3. 访问方式
+
+本地启动项目后,可直接访问:
+
+```text
+http://localhost:8082/auth_config_search.jsp?xm=麦云依
+```
+
+也支持:
+
+```text
+http://localhost:8082/auth_config_search.jsp?学生姓名=麦云依
+http://localhost:8082/auth_config_search.jsp?name=麦云依
+```
+
+## 4. 返回内容
+
+返回为 `application/json`,主要包含:
+
+- 查询时间
+- 当前海康连接摘要
+- 命中的学生列表
+- 每个学生最近的海康请假权限条目
+- 每个条目的:
+  - `authConfigSearchReq`
+  - `authConfigSearchResp`
+  - `authConfigSearchRespBrief`
+  - `currentWindowCount`
+  - `foreignWindowCount`
+
+## 5. 海康连接说明
+
+- 默认复用项目现有海康连接参数读取逻辑:`biz.kq.HkConnU`
+- 若本地未显式配置,则回退到项目当前默认环境:
+  - `host=192.168.68.3`
+  - `port=443`
+  - `appKey=25723525`
+- `tagId` 默认使用:
+  - `frs`
+
+## 6. 备注
+
+- 该页面仅用于本地排查,不影响现有请假自动开门、定时补偿、权限回收等业务代码。
+- 查询逻辑会:
+  1. 先按学生姓名精确查询
+  2. 精确无结果时,再按 `like` 查询
+  3. 对命中的学生,读取最近 20 条 `RCHKLA_ITEM`
+  4. 对每条记录实时调用海康 `auth_config/search`

+ 331 - 0
src/biz/kq/HkAuthConfigSearchLocalU.java

@@ -0,0 +1,331 @@
+package biz.kq;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import core.co.haikang.HkApiClient;
+import org.apache.commons.lang3.StringUtils;
+import ss.db.Db;
+import ss.db.SqlCmd;
+
+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 HkAuthConfigSearchLocalU {
+
+    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 HkAuthConfigSearchLocalU() {
+    }
+
+    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("queryStudentName", StringUtils.trimToNull(studentName));
+            errorMap.put("error", e.getMessage());
+            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("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) {
+                List<Map<String, Object>> itemList = loadLeaveAuthItemList(gdba, rdLong(student.get("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);
+        return gdba.doSelMapListByC(sqlCmd);
+    }
+
+    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 + "%");
+        return gdba.doSelMapListByC(sqlCmd);
+    }
+
+    private static List<Map<String, Object>> loadLeaveAuthItemList(Db gdba, long ryid) throws Exception {
+        String sql = ""
+                + "select * from ("
+                + " select rc.rcid,rc.rclbm,to_char(rc.kssj,'yyyy-mm-dd hh24:mi:ss') kssj,"
+                + " to_char(rc.jssj,'yyyy-mm-dd hh24:mi:ss') jssj,"
+                + " i.item_id,i.item_type,i.item_name,i.resource_scope,i.resource_keys,i.person_id,"
+                + " to_char(i.auth_beg,'yyyy-mm-dd hh24:mi:ss') auth_beg,"
+                + " to_char(i.auth_end,'yyyy-mm-dd hh24:mi:ss') auth_end,"
+                + " i.sync_stage,i.platform_config_confirmed,i.config_search_matched,"
+                + " i.delete_confirmed,i.config_task_id,i.delete_task_id,"
+                + " to_char(i.delete_time,'yyyy-mm-dd hh24:mi:ss') delete_time,"
+                + " to_char(i.updated_at,'yyyy-mm-dd hh24:mi:ss') 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);
+        return gdba.doSelMapListByC(sqlCmd);
+    }
+
+    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 = HK_API_CLIENT.searchAuthConfig(hkConnParm, req);
+            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> 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;
+    }
+}

+ 15 - 0
web/auth_config_search.jsp

@@ -0,0 +1,15 @@
+<%@ page contentType="application/json; charset=UTF-8" pageEncoding="UTF-8"%>
+<%@ page import="biz.kq.HkAuthConfigSearchLocalU" %>
+<%@ page import="org.apache.commons.lang3.StringUtils" %>
+<%
+request.setCharacterEncoding("UTF-8");
+response.setCharacterEncoding("UTF-8");
+
+String studentName = request.getParameter("xm");
+if (StringUtils.isBlank(studentName))
+    studentName = request.getParameter("学生姓名");
+if (StringUtils.isBlank(studentName))
+    studentName = request.getParameter("name");
+
+out.write(HkAuthConfigSearchLocalU.queryByStudentNameJson(studentName));
+%>