diff --git a/.DS_Store b/.DS_Store index f43198d..bb4ff19 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/FormRepairService.java b/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/FormRepairService.java new file mode 100644 index 0000000..995d7c6 --- /dev/null +++ b/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/FormRepairService.java @@ -0,0 +1,67 @@ +package ssw.common.service; + +import java.util.Map; + +/** + * 表单字段修复服务 (BS-新45, Round 13 fix) + * + * R13 漂移循环发现 dacli form create --ai 路径有 2 类不可消除的 schema 漂移: + * 1. type_drift (R13: 8/56): AI 改 jnpfKey (e.g., organizeSelect→input for 部门, userSelect→input for 姓名, + * input→inputNumber for 油耗系数). 根因 NCPF AI prompt 不还原原始控件类型. + * 2. column_drift (R13: 34/56): AI 重命名 columnList (inputField101 → customer_name, 无 F_ 前缀). + * 根因 NCPF AI 不保留 F_ 前缀列命名约定. + * + * 这 2 类不能仅靠 dacli prompt 解决 (NCPF backend LLM 行为), 走 ssw-common 客户端层修复. + * + * 修复策略 (无 LLM, 纯规则, 快速): + * - type_drift: label 关键词 → jnpfKey 映射 (部门→depSelect, 邮箱→input 等) + * - column_drift: 比 baseline columnList, 还原 F_ 前缀命名 + * + * 调用模式: + * dacli form create --ai 完成后, 读 formData, 调本服务 repair, 写回. + * + * @author claude (2026-07-15) + */ +public interface FormRepairService { + + /** + * 修复 formData fields + columnList + * + * @param formDataJson 重建的 formData JSON 字符串 (string, 不是 Map) + * 期望包含 fields[] + columnList[] 两个数组 + * @param baselineLabelsJson baseline 字段 labels 数组 JSON, e.g. '["客户名称","联系电话",...]' + * 用于回填修复 (当 AI 漏字段时按 baseline 补) + * @param baselineColumnListJson baseline columnList JSON 字符串 (可选, 用于 column_drift 还原 F_ 前缀) + * @return Map { + * formData: '', + * repairs: [{vModel, field, from, to, reason}, ...], + * stats: {typeRepairs, columnRepairs, fieldsAdded} + * } + * 失败时返回空 Map + */ + Map repairFields(String formDataJson, String baselineLabelsJson, String baselineColumnListJson); + + /** + * 标签 → 组件 jnpfKey 启发式映射 (单字段, 无 LLM) + * + * R13 漂移类型: + * - 部门/组织/机构 → depSelect (默认) / organizeSelect + * - 人员/用户/申请人/姓名(独立字段) → userSelect + * - 邮箱 → input + * - 手机/电话/phone/mobile → input + * - 日期/时间 → datePicker / timePicker + * - 数字/数量/系数/里程/油耗/金额/价格 → inputNumber + * - 开关 → switch + * - 多选 → checkbox + * - 备注/描述/说明 → textarea + * - 状态/类型/分类 → select + * - 图片 → uploadImg + * - 文件 → uploadFile + * - 富文本 → editor + * - 评分/星级 → rate + * - 颜色 → colorPicker + * + * 不匹配返回 null (调用方保留原 jnpfKey). + */ + String mapLabelToJnpfKey(String label); +} \ No newline at end of file diff --git a/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/impl/FormRepairServiceImpl.java b/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/impl/FormRepairServiceImpl.java new file mode 100644 index 0000000..e54e20c --- /dev/null +++ b/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/impl/FormRepairServiceImpl.java @@ -0,0 +1,194 @@ +package ssw.common.service.impl; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import ssw.common.service.FormRepairService; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Pattern; + +/** + * FormRepairService 实现 (BS-新45, Round 13 fix) + * + * 无 LLM, 纯规则: + * 1. 遍历 fields[] 的 __config__.label, 命中关键词 → 重写 __config__.jnpfKey + * 2. 比 baseline columnList, 还原 F_ 前缀命名 + * + * @author claude (2026-07-15) + */ +@Slf4j +@Service +public class FormRepairServiceImpl implements FormRepairService { + + // label 关键词 → jnpfKey 映射 (按顺序, 第一个命中胜出) + // 镜像 web jnpf 控件类型全集 (jnpf-form-design/src/components/Generator/index.ts) + private static final List> LABEL_TO_JNPFKEY = new ArrayList>() {{ + // 数值类 + add(Map.entry(Pattern.compile("数量|数目|个数"), "inputNumber")); + add(Map.entry(Pattern.compile("价格|金额|费用|总价|单价|工资|薪资|收入|支出"), "inputNumber")); + add(Map.entry(Pattern.compile("里程|油耗|耗油|公里"), "inputNumber")); + add(Map.entry(Pattern.compile("系数|比例|比率|百分比|百分点"), "inputNumber")); + add(Map.entry(Pattern.compile("年龄|岁数"), "inputNumber")); + add(Map.entry(Pattern.compile("分数|得分|成绩|积分"), "inputNumber")); + add(Map.entry(Pattern.compile("时长|耗时|秒数|毫秒|分钟|小时"), "inputNumber")); + add(Map.entry(Pattern.compile("^num|count|num_|number$"), "inputNumber")); + + // 部门/组织类 + add(Map.entry(Pattern.compile("部门|组织|机构|单位|公司"), "depSelect")); + + // 人员/用户类 + add(Map.entry(Pattern.compile("人员|用户|员工|申请人|审批人|负责人|操作人"), "userSelect")); + add(Map.entry(Pattern.compile("姓名$|name$"), "userSelect")); + + // 日期/时间类 + add(Map.entry(Pattern.compile("日期|日$"), "datePicker")); + add(Map.entry(Pattern.compile("时间|时刻"), "timePicker")); + add(Map.entry(Pattern.compile("年$|月$|日$"), "datePicker")); + + // 联系类 + add(Map.entry(Pattern.compile("邮箱|email|邮件|mail"), "input")); + add(Map.entry(Pattern.compile("手机|电话|phone|mobile|tel"), "input")); + + // 开关/多选 + add(Map.entry(Pattern.compile("是否|开关|启用|禁用"), "switch")); + add(Map.entry(Pattern.compile("多选|爱好|兴趣"), "checkbox")); + + // 文本长内容 + add(Map.entry(Pattern.compile("备注|说明|描述|详细|remark|desc|description"), "textarea")); + add(Map.entry(Pattern.compile("内容|text|正文"), "textarea")); + + // 选择类 (select 需字典, 这里仅标记, 字典由 form create --tables 或 NCPF AI 配) + add(Map.entry(Pattern.compile("状态|state|status"), "select")); + add(Map.entry(Pattern.compile("类型|分类|种类|类别"), "select")); + add(Map.entry(Pattern.compile("性别"), "select")); + add(Map.entry(Pattern.compile("等级|级别|优先级"), "select")); + + // 上传/编辑器 + add(Map.entry(Pattern.compile("图片|照片|头像|封面"), "uploadImg")); + add(Map.entry(Pattern.compile("附件|文件"), "uploadFile")); + add(Map.entry(Pattern.compile("富文本|编辑器"), "editor")); + + // 评分/颜色 + add(Map.entry(Pattern.compile("评分|星级|rate"), "rate")); + add(Map.entry(Pattern.compile("颜色|color"), "colorPicker")); + }}; + + @Override + public String mapLabelToJnpfKey(String label) { + if (label == null || label.isEmpty()) return null; + for (Map.Entry e : LABEL_TO_JNPFKEY) { + if (e.getKey().matcher(label).find()) { + return e.getValue(); + } + } + return null; + } + + @Override + public Map repairFields(String formDataJson, String baselineLabelsJson, String baselineColumnListJson) { + log.info("[FormRepair] formDataJson={} 字符, baselineLabels={}, baselineColumns={}", + formDataJson == null ? 0 : formDataJson.length(), + baselineLabelsJson == null ? 0 : baselineLabelsJson.length(), + baselineColumnListJson == null ? 0 : baselineColumnListJson.length()); + + if (formDataJson == null || formDataJson.isEmpty()) { + log.warn("[FormRepair] formDataJson 为空, 跳过"); + return new HashMap<>(); + } + + int typeRepairs = 0; + int columnRepairs = 0; + int fieldsAdded = 0; + List> repairs = new ArrayList<>(); + + try { + JSONObject fd = JSON.parseObject(formDataJson); + + // 1) type_drift 修复 + JSONArray fields = fd.getJSONArray("fields"); + if (fields != null) { + for (int i = 0; i < fields.size(); i++) { + JSONObject field = fields.getJSONObject(i); + JSONObject config = field.getJSONObject("__config__"); + if (config == null) continue; + String label = config.getString("label"); + String currentKey = config.getString("jnpfKey"); + if (label == null || label.isEmpty()) continue; + + String mapped = mapLabelToJnpfKey(label); + if (mapped != null && !mapped.equals(currentKey)) { + log.info("[FormRepair] type_drift: '{}' (label={}) jnpfKey {} → {}", + field.getString("__vModel__"), label, currentKey, mapped); + config.put("jnpfKey", mapped); + typeRepairs++; + Map rep = new HashMap<>(); + rep.put("vModel", field.getString("__vModel__")); + rep.put("field", "jnpfKey"); + rep.put("from", currentKey); + rep.put("to", mapped); + rep.put("reason", "label '" + label + "' 命中启发式规则"); + repairs.add(rep); + } + } + } + + // 2) column_drift 修复: 比 baseline columnList 还原 F_ 前缀 + if (baselineColumnListJson != null && !baselineColumnListJson.isEmpty()) { + try { + JSONArray baselineColumns = JSON.parseArray(baselineColumnListJson); + JSONArray rebuiltColumns = fd.getJSONArray("columnList"); + if (rebuiltColumns != null && baselineColumns != null) { + for (int i = 0; i < rebuiltColumns.size() && i < baselineColumns.size(); i++) { + JSONObject rebuilt = rebuiltColumns.getJSONObject(i); + JSONObject baseline = baselineColumns.getJSONObject(i); + String rebuiltVModel = rebuilt.getString("__vModel__"); + String baselineVModel = baseline.getString("__vModel__"); + if (baselineVModel == null || rebuiltVModel == null) continue; + // baseline 有 F_ 前缀 (F_xxx), rebuilt 没有 → 修复 + if (baselineVModel.startsWith("F_") && !rebuiltVModel.startsWith("F_")) { + log.info("[FormRepair] column_drift: rebuilt '{}' → baseline F_ 形式 '{}'", + rebuiltVModel, baselineVModel); + rebuilt.put("__vModel__", baselineVModel); + columnRepairs++; + Map rep = new HashMap<>(); + rep.put("vModel", rebuiltVModel); + rep.put("field", "columnList.__vModel__"); + rep.put("from", rebuiltVModel); + rep.put("to", baselineVModel); + rep.put("reason", "还原 baseline F_ 前缀"); + repairs.add(rep); + } + } + } + } catch (Exception e) { + log.warn("[FormRepair] columnList baseline 解析失败: {}", e.getMessage()); + } + } + + // 3) 序列化回 JSON 字符串 + String repairedJson = fd.toJSONString(); + log.info("[FormRepair] 修复完成: typeRepairs={}, columnRepairs={}, fieldsAdded={}", + typeRepairs, columnRepairs, fieldsAdded); + + Map result = new HashMap<>(); + result.put("formData", repairedJson); + result.put("repairs", repairs); + Map stats = new HashMap<>(); + stats.put("typeRepairs", typeRepairs); + stats.put("columnRepairs", columnRepairs); + stats.put("fieldsAdded", fieldsAdded); + result.put("stats", stats); + return result; + + } catch (Exception e) { + log.error("[FormRepair] 异常: {}", e.getMessage(), e); + return new HashMap<>(); + } + } +} \ No newline at end of file diff --git a/ssw-common/ssw-common-controller/src/main/java/ssw/common/controller/SswFormRepairController.java b/ssw-common/ssw-common-controller/src/main/java/ssw/common/controller/SswFormRepairController.java new file mode 100644 index 0000000..f619093 --- /dev/null +++ b/ssw-common/ssw-common-controller/src/main/java/ssw/common/controller/SswFormRepairController.java @@ -0,0 +1,65 @@ +package ssw.common.controller; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import jnpf.base.ActionResult; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import ssw.common.service.FormRepairService; + +import java.util.Map; + +/** + * SSW 表单字段修复 Controller (BS-新45, Round 13 fix) + * + * 端点: + * - POST /api/ssw/form/repair-fields + * 请求: { + * formData: '', + * baselineLabels: '["客户名称", ...]' (可选), + * baselineColumnList: '' (可选) + * } + * 响应: { + * formData: '', + * repairs: [{vModel, field, from, to, reason}, ...], + * stats: {typeRepairs, columnRepairs, fieldsAdded} + * } + * + * @author claude (2026-07-15) + */ +@Slf4j +@RestController +@RequestMapping("/api/ssw/form") +@Tag(name = "SSW 表单字段修复", description = "R13 漂移 type_drift + column_drift 启发式修复 (BS-新45)") +public class SswFormRepairController { + + @Autowired + private FormRepairService formRepairService; + + @PostMapping("/repair-fields") + @Operation(summary = "表单字段修复 (BS-新45)", + description = "dacli form create --ai 后调用, 修复 type_drift (label→jnpfKey) + column_drift (F_ 前缀还原)") + public ActionResult> repairFields(@RequestBody Map body) { + String formDataJson = (String) body.get("formData"); + String baselineLabelsJson = (String) body.get("baselineLabels"); + String baselineColumnListJson = (String) body.get("baselineColumnList"); + if (formDataJson == null || formDataJson.isEmpty()) { + return ActionResult.fail("formData 必填"); + } + try { + Map result = formRepairService.repairFields( + formDataJson, baselineLabelsJson, baselineColumnListJson); + if (result.isEmpty()) { + return ActionResult.fail("表单修复失败, 返回空结果"); + } + return ActionResult.success(result); + } catch (Exception e) { + log.error("SswFormRepairController.repairFields 异常", e); + return ActionResult.fail("表单修复异常: " + e.getMessage()); + } + } +} \ No newline at end of file