ProposeWriteTool.java 27.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
package com.xly.tool;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xly.agent.AgentIdentity;
import com.xly.service.ErpClient;
import com.xly.service.FormResolverService;
import com.xly.service.OpService;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * ProposeWrite 工具(写操作,人在环)。
 *
 * <p>**只提议并暂存,绝不立即执行**:把「改某条记录的某字段」解析成具体的 表/记录id/列/新值,
 * 写一条 draft 到 ai_op_queue,返回一个提议。真正执行发生在用户点【确认】后的确定性端点里
 * (见 OpController),不经过 LLM。
 */
public class ProposeWriteTool {

    private final ErpClient erp;
    private final JdbcTemplate jdbc;
    private final OpService ops;
    private final ObjectMapper mapper;
    private final AgentIdentity identity;
    private final FormResolverService resolver;

    public ProposeWriteTool(ErpClient erp, JdbcTemplate jdbc, OpService ops, ObjectMapper mapper,
                            AgentIdentity identity, FormResolverService resolver) {
        this.erp = erp;
        this.jdbc = jdbc;
        this.ops = ops;
        this.mapper = mapper;
        this.identity = identity;
        this.resolver = resolver;
    }

    @Tool("提议一次写操作(人在环:只生成待确认提议、绝不立即执行;用户点【确认】才写入,你绝不能声称已完成)。"
            + "用 action 指定动作:create=新增;update=改某字段;invalid=作废(业务单据要“删除/取消”一律用它,可复原);"
            + "cancelInvalid=复原/取消作废;examine=审核;cancelExamine=销审/反审核;delete=物理删除(明细行/极少用,业务单据别用)。"
            + "按 action 传参:create 用 fieldsJson;update 用 recordKeyword+fieldChinese+newValue;"
            + "invalid/cancelInvalid/examine/cancelExamine/delete 用 recordKeyword 定位单据。本工具自行定位主表,无需先 findForms。")
    public String proposeWrite(
            @P("动作:create|update|invalid|cancelInvalid|examine|cancelExamine|delete") String action,
            @P("实体/单据类型,如 报价 / 客户 / 销售订单") String entityKeyword,
            @P(value = "记录名称或单号关键词(create 不需要)", required = false) String recordKeyword,
            @P(value = "要改的字段中文名(仅 update)", required = false) String fieldChinese,
            @P(value = "新值(仅 update)", required = false) String newValue,
            @P(value = "字段 JSON 中文名->值(仅 create)", required = false) String fieldsJson) {
        if (isBlank(action)) {
            return err("缺少 action。");
        }
        switch (action.trim().toLowerCase()) {
            case "create":        return doCreate(entityKeyword, fieldsJson);
            case "update":        return doUpdate(entityKeyword, recordKeyword, fieldChinese, newValue);
            case "invalid":       return doInvalidate(entityKeyword, recordKeyword, false);
            case "cancelinvalid": return doInvalidate(entityKeyword, recordKeyword, true);
            case "examine":       return doExamineOp(entityKeyword, recordKeyword, 1);
            case "cancelexamine": return doExamineOp(entityKeyword, recordKeyword, 0);
            case "delete":        return doDelete(entityKeyword, recordKeyword);
            default:
                return err("未知 action:" + action + "(支持 create/update/invalid/cancelInvalid/examine/cancelExamine/delete)");
        }
    }

    /** 作废(cancel=false)/复原·取消作废(cancel=true)一条记录 → ERP updatebInvalid(Sp_Invalidation)。业务单据的“删除”走这里。 */
    private String doInvalidate(String entityKeyword, String recordKeyword, boolean cancel) {
        String verb = cancel ? "复原" : "作废";
        if (isBlank(entityKeyword) || isBlank(recordKeyword)) {
            return err("缺少实体类型或记录名称。");
        }
        Located l = locateRecord(entityKeyword, recordKeyword, verb);
        if (l.error != null) {
            return l.error;
        }
        String description = verb + "【" + l.recordName + "】(" + entityKeyword + ")";
        // sNewValue 存 handleType:toVoid=作废 / cancel=复原;执行走 ERP updatebInvalid
        String opId = ops.createDraft(identity.userId(), "invalid", l.formId, l.moduleId, l.table, l.billId,
                null, null, null, cancel ? "cancel" : "toVoid", description);

        Map<String, Object> out = new LinkedHashMap<>();
        out.put("opId", opId);
        out.put("summary", description);
        out.put("message", "已为你生成一条待确认的" + verb + ",请在下方点【确认】执行、或【取消】。");
        return toJson(out);
    }

    /** 修改某条记录的某个字段。见 {@link #proposeWrite}。 */
    private String doUpdate(String entityKeyword, String recordKeyword, String fieldChinese, String newValue) {

        if (isBlank(entityKeyword) || isBlank(recordKeyword) || isBlank(fieldChinese)) {
            return err("缺少信息:需要实体类型、记录名称关键词、字段中文名、新值。");
        }

        // 1) 定位主表 + 唯一记录
        Located l = locateRecord(entityKeyword, recordKeyword, "修改");
        if (l.error != null) {
            return l.error;
        }

        // 2) 字段中文名 -> 技术列名
        String field = queryOne(
                "SELECT sField FROM viw_kg_field_dict WHERE sTable=? AND sChinese=? ORDER BY iFormUses DESC LIMIT 1",
                l.table, fieldChinese.trim());
        if (field == null) {
            field = queryOne(
                    "SELECT sField FROM viw_kg_field_dict WHERE sTable=? AND sChinese LIKE ? ORDER BY iFormUses DESC LIMIT 1",
                    l.table, "%" + fieldChinese.trim() + "%");
        }
        if (field == null) {
            return err("在该表单里找不到叫「" + fieldChinese + "」的字段,请换个字段名或先查看该表单有哪些字段。");
        }
        String oldValue = l.rec.path(field).asText("");

        // 3) 暂存 draft(不执行)
        String description = "将【" + l.recordName + "】的【" + fieldChinese + "】"
                + (oldValue.isBlank() ? "" : ("由「" + oldValue + "」")) + "改为「" + newValue + "」";
        String opId = ops.createDraft(identity.userId(), "update", l.formId, l.moduleId, l.table, l.billId,
                field, fieldChinese, oldValue, newValue, description);

        Map<String, Object> out = new LinkedHashMap<>();
        out.put("opId", opId);
        out.put("summary", description);
        out.put("message", "已为你生成一条待确认的修改,请在下方点【确认】执行、或【取消】。");
        return toJson(out);
    }

    /** 物理删除一条记录(罕用;业务单据请用 doInvalidate 作废)。见 {@link #proposeWrite}。 */
    private String doDelete(String entityKeyword, String recordKeyword) {

        if (isBlank(entityKeyword) || isBlank(recordKeyword)) {
            return err("缺少实体类型或记录名称。");
        }
        Located l = locateRecord(entityKeyword, recordKeyword, "删除");
        if (l.error != null) {
            return l.error;
        }
        String description = "删除【" + l.recordName + "】(" + entityKeyword + ")";
        String opId = ops.createDraft(identity.userId(), "delete", l.formId, l.moduleId, l.table, l.billId,
                null, null, l.recordName, null, description);

        Map<String, Object> out = new LinkedHashMap<>();
        out.put("opId", opId);
        out.put("summary", description);
        out.put("message", "已为你生成一条待确认的删除,请在下方点【确认】执行、或【取消】。删除不可恢复,请谨慎。");
        return toJson(out);
    }

    /** 新增一条记录(报价走多表 proposeQuote)。见 {@link #proposeWrite}。 */
    private String doCreate(String entityKeyword, String fieldsJson) {

        if (isBlank(entityKeyword)) {
            return err("缺少实体类型。");
        }
        Map<String, Object> form = resolveForm(entityKeyword.trim());
        if (form == null) {
            return err("找不到「" + entityKeyword + "」对应的可新增主表。");
        }
        String formId = str(form.get("sFormId"));
        String moduleId = str(form.get("sModuleId"));
        String table = str(form.get("sDataSource"));
        if (!identity.canAccessModule(moduleId)) {
            return err("你没有新增「" + entityKeyword + "」的权限。");
        }

        // 报价是跨表主-从单据 → 走多表创建(主表 + 印刷/部件从表 + 多数量)
        if ("quoquotationmaster".equalsIgnoreCase(table)) {
            return proposeQuote(fieldsJson, formId, moduleId, table);
        }

        Map<String, String> types = resolver.columnTypes(table);
        // 权威 label -> {col, fk} 映射(与 collectForm 同源:策展/字段字典),保证前端标签能被正确回映射
        Map<String, Map<String, Object>> labelMap = new LinkedHashMap<>();
        for (Map<String, Object> bfm : resolver.businessFields(table, 40)) {
            labelMap.put(normLabel(String.valueOf(bfm.get("label"))), bfm);
        }
        Map<String, Object> col = new LinkedHashMap<>();
        List<String> descParts = new ArrayList<>();
        try {
            if (!isBlank(fieldsJson)) {
                JsonNode fj = mapper.readTree(fieldsJson.trim());
                Iterator<Map.Entry<String, JsonNode>> it = fj.fields();
                while (it.hasNext()) {
                    Map.Entry<String, JsonNode> e = it.next();
                    String zh = e.getKey();
                    String v = e.getValue().asText("");
                    if (isBlank(v)) {
                        continue;
                    }
                    Map<String, Object> fm = labelMap.get(normLabel(zh));
                    if (fm == null) {
                        fm = resolveColumn(table, zh); // 回退字段字典
                    }
                    if (fm == null) {
                        continue; // 该实体没有这个字段,忽略
                    }
                    String colName = str(fm.get("col"));
                    // 别让 LLM/用户直填系统列(制单人/单据日期/租户/单号…),交给 ERP 与下方系统列处理
                    if (resolver.isSystemColumn(colName)) {
                        continue;
                    }
                    String fk = str(fm.get("fk"));
                    if (fk != null && !fk.isBlank()) {
                        String id = resolver.resolveFk(fk, v); // 外键:名称 -> id
                        if (id == null) {
                            String ent = zh.replace("名称", "");
                            return err("「" + v + "」不是系统里已有的" + ent + "。请让用户从下拉里选真实存在的"
                                    + ent + ";**不要**因此新建" + ent + "。");
                        }
                        col.put(colName, id);
                    } else {
                        Object cv = resolver.coerce(types.get(colName), v); // 按列类型强转
                        if (cv != null) {
                            col.put(colName, cv);
                        }
                    }
                    descParts.add(zh + "=" + v);
                }
            }
        } catch (Exception ex) {
            return err("字段解析失败:" + ex.getMessage());
        }
        if (descParts.isEmpty()) {
            return err("请至少提供一个有效字段(如客户名称)。");
        }

        // 自动补齐 NOT-NULL 无默认列(系统列跳过;其余按类型给默认 0/'')
        for (String rc : requiredCols(table)) {
            if (col.containsKey(rc) || resolver.isSystemColumn(rc)) {
                continue;
            }
            col.put(rc, resolver.typeDefault(rc, types.get(rc)));
        }
        // 系统列:主键 + 表单id + 单号(租户/制单人/日期由 ERP 注入,不填)
        col.put("sId", erp.newUuid(identity.token()));
        col.put("sFormId", formId);
        String billNo = resolver.nextBillNo(table, identity.brandsId());
        if (billNo != null) {
            col.put("sBillNo", billNo);
        }

        String payload;
        try {
            payload = mapper.writeValueAsString(col);
        } catch (Exception e) {
            return err("内部错误:" + e.getMessage());
        }
        String description = "新增【" + entityKeyword + "】:" + String.join(",", descParts);
        String opId = ops.createDraftPayload(identity.userId(), "create", formId, moduleId, table, payload, description);

        Map<String, Object> out = new LinkedHashMap<>();
        out.put("opId", opId);
        out.put("summary", description);
        out.put("message", "已为你生成一条待确认的新增,请在下方点【确认】执行、或【取消】。");
        return toJson(out);
    }

    /** 审核(iFlag=1)/反审核·销审(iFlag=0)一条单据 → ERP doExamine。见 {@link #proposeWrite}。 */
    private String doExamineOp(String entityKeyword, String recordKeyword, int iFlag) {

        if (isBlank(entityKeyword) || isBlank(recordKeyword)) {
            return err("缺少单据类型或单号/名称。");
        }
        String verb = iFlag == 1 ? "审核" : "反审核";
        Located l = locateRecord(entityKeyword, recordKeyword, verb);
        if (l.error != null) {
            return l.error;
        }
        String description = verb + "【" + l.recordName + "】(" + entityKeyword + ")";
        // examine:sNewValue 存 iFlag(1=审核,0=反审核/销审);执行走 ERP doExamine(存储过程驱动)
        String opId = ops.createDraft(identity.userId(), "examine", l.formId, l.moduleId, l.table, l.billId,
                null, null, null, String.valueOf(iFlag), description);

        Map<String, Object> out = new LinkedHashMap<>();
        out.put("opId", opId);
        out.put("summary", description);
        out.put("message", "已为你生成一条待确认的" + verb + ",请在下方点【确认】执行、或【取消】。审核有业务后果,请谨慎。");
        return toJson(out);
    }

    /**
     * 报价多表创建:主表(quoquotationmaster) + 印刷/部件从表(quoquotationslave) + 多数量(quoquotationmanyqtys)。
     * 字段按策展的 target 表分流;印刷/颜色/单双面无独立列 → 合进从表 sMaterialsMemo;多数量按逗号拆多行。
     * 价格由 ERP【核价】计算,本工具只落主-从明细。生成 __tables__ 结构化 payload,确认端点做多表写入。
     */
    private String proposeQuote(String fieldsJson, String formId, String moduleId, String table) {
        Map<String, String> masterTypes = resolver.columnTypes(table);
        Map<String, String> slaveTypes = resolver.columnTypes("quoquotationslave");
        Map<String, Map<String, Object>> labelMap = new LinkedHashMap<>();
        for (Map<String, Object> bfm : resolver.businessFields(table, 40)) {
            labelMap.put(normLabel(String.valueOf(bfm.get("label"))), bfm);
        }

        Map<String, Object> masterCol = new LinkedHashMap<>();
        Map<String, Object> slaveCol = new LinkedHashMap<>();
        List<String> notes = new ArrayList<>();
        List<String> manyQtys = new ArrayList<>();
        List<String> descParts = new ArrayList<>();
        String custId = null;
        try {
            if (!isBlank(fieldsJson)) {
                JsonNode fj = mapper.readTree(fieldsJson.trim());
                Iterator<Map.Entry<String, JsonNode>> it = fj.fields();
                while (it.hasNext()) {
                    Map.Entry<String, JsonNode> e = it.next();
                    String zh = e.getKey();
                    String v = e.getValue().asText("");
                    if (isBlank(v)) {
                        continue;
                    }
                    Map<String, Object> fm = labelMap.get(normLabel(zh));
                    if (fm == null) {
                        continue;
                    }
                    String colName = str(fm.get("col"));
                    String tgt = fm.get("table") == null ? "master" : str(fm.get("table"));
                    String fk = str(fm.get("fk"));
                    if ("note".equals(tgt)) {
                        notes.add(zh + "=" + v);
                        descParts.add(zh + "=" + v);
                        continue;
                    }
                    if ("manyqtys".equals(tgt)) {
                        for (String q : v.split("[,,、\\s]+")) {
                            String n = q.replaceAll("[^0-9.]", "");
                            if (!n.isEmpty()) {
                                manyQtys.add(n);
                            }
                        }
                        descParts.add(zh + "=" + v);
                        continue;
                    }
                    Object value;
                    if (fk != null && !fk.isBlank()) {
                        String id = resolver.resolveFk(fk, v);
                        if (id == null) {
                            String ent = zh.replace("名称", "");
                            return err("「" + v + "」不是系统里已有的" + ent + "。请用 collectForm 让用户从下拉里选真实存在的"
                                    + ent + ";**不要**因此新建" + ent + ",也不要把产品/规格当成客户。");
                        }
                        value = id;
                        if ("sCustomerId".equals(colName)) {
                            custId = id;
                        }
                    } else {
                        String dt = "slave".equals(tgt) ? slaveTypes.get(colName) : masterTypes.get(colName);
                        value = resolver.coerce(dt, v);
                    }
                    if (value != null) {
                        if ("slave".equals(tgt)) {
                            slaveCol.put(colName, value);
                        } else {
                            masterCol.put(colName, value);
                        }
                    }
                    descParts.add(zh + "=" + v);
                }
            }
        } catch (Exception ex) {
            return err("字段解析失败:" + ex.getMessage());
        }
        if (descParts.isEmpty()) {
            return err("请至少填写一个字段(如客户名称/产品名称/数量)。");
        }

        // 主表:补必填(非系统) + 主键 + 表单id + 单号
        for (String rc : requiredCols(table)) {
            if (masterCol.containsKey(rc) || resolver.isSystemColumn(rc)) {
                continue;
            }
            masterCol.put(rc, resolver.typeDefault(rc, masterTypes.get(rc)));
        }
        String masterId = erp.newUuid(identity.token());
        masterCol.put("sId", masterId);
        masterCol.put("sFormId", formId);
        String billNo = resolver.nextBillNo(table, identity.brandsId());
        if (billNo != null) {
            masterCol.put("sBillNo", billNo);
        }

        List<Map<String, Object>> tables = new ArrayList<>();
        tables.add(tableItem("quoquotationmaster", "master", masterCol));

        // 印刷/部件从表:有从表字段或印刷备注就建一行(sCustomerId 从表 NOT-NULL)
        if (!slaveCol.isEmpty() || !notes.isEmpty()) {
            if (!notes.isEmpty()) {
                String memo = String.join(";", notes);
                Object exist = slaveCol.get("sMaterialsMemo");
                slaveCol.put("sMaterialsMemo", exist == null ? memo : (exist + ";" + memo));
            }
            slaveCol.put("sId", erp.newUuid(identity.token()));
            slaveCol.put("sParentId", masterId);
            slaveCol.put("sCustomerId", custId == null ? "" : custId);
            tables.add(tableItem("quoquotationslave", "slave", slaveCol));
        }
        // 多数量报价:每个数量一行
        for (String q : manyQtys) {
            Map<String, Object> mq = new LinkedHashMap<>();
            mq.put("sId", erp.newUuid(identity.token()));
            mq.put("sParentId", masterId);
            mq.put("dManyQty", q);
            tables.add(tableItem("quoquotationmanyqtys", "slave", mq));
        }

        Map<String, Object> wrap = new LinkedHashMap<>();
        wrap.put("__tables__", tables);
        String payload;
        try {
            payload = mapper.writeValueAsString(wrap);
        } catch (Exception e) {
            return err("内部错误:" + e.getMessage());
        }
        String description = "新增【报价】:" + String.join(",", descParts)
                + "(含印刷/多数量明细;价格请在 ERP 点【核价】计算)";
        String opId = ops.createDraftPayload(identity.userId(), "create", formId, moduleId, table, payload, description);

        Map<String, Object> out = new LinkedHashMap<>();
        out.put("opId", opId);
        out.put("summary", description);
        out.put("message", "已为你生成一条待确认的报价新增(主表+印刷明细+多数量),请点【确认】执行、或【取消】。价格在 ERP 里点【核价】计算。");
        return toJson(out);
    }

    private Map<String, Object> tableItem(String sTable, String name, Map<String, Object> column) {
        Map<String, Object> m = new LinkedHashMap<>();
        m.put("sTable", sTable);
        m.put("name", name);
        m.put("column", column);
        return m;
    }

    /** 中文名 -> {col, fk}(字段字典,先精确合并模糊,取使用度最高的一列)。 */
    private Map<String, Object> resolveColumn(String table, String zh) {
        try {
            List<Map<String, Object>> r = jdbc.queryForList(
                    "SELECT sField col, MAX(sFkTable) fk FROM viw_kg_field_dict " +
                            "WHERE sTable=? AND (sChinese=? OR sChinese LIKE ?) " +
                            "GROUP BY sField ORDER BY SUM(iFormUses) DESC LIMIT 1",
                    table, zh, "%" + zh + "%");
            return r.isEmpty() ? null : r.get(0);
        } catch (Exception e) {
            return null;
        }
    }

    /** 目标表的 NOT-NULL 无默认列(排除 ERP 会自动注入的租户/制单人)。 */
    private List<String> requiredCols(String table) {
        List<String> out = new ArrayList<>();
        try {
            List<Map<String, Object>> rows = jdbc.queryForList(
                    "SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=? " +
                            "AND IS_NULLABLE='NO' AND COLUMN_DEFAULT IS NULL AND EXTRA NOT LIKE '%auto_increment%'", table);
            Set<String> skip = Set.of("sBrandsId", "sSubsidiaryId", "sMakePerson");
            for (Map<String, Object> r : rows) {
                String c = str(r.get("COLUMN_NAME"));
                if (c != null && !skip.contains(c)) {
                    out.add(c);
                }
            }
        } catch (Exception ignore) {
        }
        return out;
    }

    /** 定位实体的可写主表——统一走 FormResolverService(与 collectForm 同源,含从属/参数表排除)。 */
    private Map<String, Object> resolveForm(String entityKeyword) {
        return resolver.resolveMasterForm(entityKeyword);
    }

    /** {@link #locateRecord} 的结果:error 非空表示失败(已是 err() JSON);否则携带定位到的主表与唯一记录。 */
    private static final class Located {
        String error;
        String formId;
        String moduleId;
        String table;
        JsonNode rec;
        String billId;
        String recordName;
    }

    /** 定位主表 + 唯一记录(update/invalid/examine/delete 共用):解析主表 → 权限 → 名称字段模糊匹配 → 0 条/多条/缺主键报错。 */
    private Located locateRecord(String entityKeyword, String recordKeyword, String verb) {
        Located l = new Located();
        Map<String, Object> form = resolveForm(entityKeyword.trim());
        if (form == null) {
            l.error = err("找不到「" + entityKeyword + "」对应的可操作主表。");
            return l;
        }
        l.formId = str(form.get("sFormId"));
        l.moduleId = str(form.get("sModuleId"));
        l.table = str(form.get("sDataSource"));
        if (!identity.canAccessModule(l.moduleId)) {
            l.error = err("你没有" + verb + "「" + entityKeyword + "」的权限。");
            return l;
        }
        String nameField = resolver.searchField(l.table, recordKeyword);
        JsonNode root;
        try {
            root = erp.readForm(identity.token(), l.formId, l.moduleId, 1, 5, nameField, recordKeyword.trim());
        } catch (Exception e) {
            l.error = err("定位记录失败:" + e.getMessage());
            return l;
        }
        if (root.path("code").asInt(0) < 0) {
            l.error = err("定位记录失败:" + root.path("msg").asText("未知错误"));
            return l;
        }
        JsonNode rows = root.path("dataset").path("rows");
        JsonNode data = (rows.isArray() && rows.size() > 0) ? rows.get(0).path("dataSet") : null;
        int n = (data != null && data.isArray()) ? data.size() : 0;
        if (n == 0) {
            l.error = err("没有找到名称含「" + recordKeyword + "」的记录。");
            return l;
        }
        if (n > 1) {
            StringBuilder names = new StringBuilder();
            for (int i = 0; i < data.size() && i < 5; i++) {
                if (i > 0) names.append("、");
                names.append(nameField == null ? "" : data.get(i).path(nameField).asText(""));
            }
            l.error = err("匹配到多条记录(" + names + "),请提供更精确的名称,只" + verb + "其中一条。");
            return l;
        }
        l.rec = data.get(0);
        l.billId = l.rec.path("sId").asText(null);
        if (isBlank(l.billId)) {
            l.error = err("定位到的记录缺少主键 sId,无法" + verb + "。");
            return l;
        }
        l.recordName = nameField == null ? recordKeyword : l.rec.path(nameField).asText(recordKeyword);
        return l;
    }

    private static String str(Object o) {
        return o == null ? null : o.toString();
    }

    private String queryOne(String sql, Object... args) {
        try {
            List<Map<String, Object>> r = jdbc.queryForList(sql, args);
            if (!r.isEmpty()) {
                Object v = r.get(0).values().iterator().next();
                return v == null ? null : v.toString();
            }
        } catch (Exception ignore) {
        }
        return null;
    }

    private static boolean isBlank(String s) {
        return s == null || s.isBlank();
    }

    /** 归一化标签用于匹配:去掉括号提示与空白("多数量(逗号分隔)" ⇄ "多数量")。 */
    private static String normLabel(String s) {
        if (s == null) {
            return "";
        }
        return s.replaceAll("[((].*?[))]", "").replaceAll("\\s+", "").trim();
    }

    private String err(String msg) {
        Map<String, Object> m = new LinkedHashMap<>();
        m.put("error", msg);
        return toJson(m);
    }

    private String toJson(Map<String, Object> m) {
        try {
            return mapper.writeValueAsString(m);
        } catch (Exception e) {
            return "{\"error\":\"内部错误\"}";
        }
    }
}