Files
SupplierSystem/src/utils/shelfLife.ts
T
Ubuntu fb51b16c84
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
fix(库存入库Web): 补齐入库识别「日期两列」——生产日期归一 + 保质期「数值+年/月/日」
开发者指令(2026-10-15):「日期让 AI 解析了一下,AI 返回**数字 + 年/月/日**;小程序方面已经修改了,
现在 Web 界面上还未修改」。

现状差异(已逐处核对):
- 提示词 server/src/prompts/defaults.ts:441 起按行给 `production_date`(YYYY-MM-DD 或 null)
  + **`shelf_life_value`(数字)+ `shelf_life_unit`(年|月|日)**,并写明天→日、个月→月、半年→6+月;
- 小程序已按这对字段解析(miniprogram/utils/shelfLife.js 的 applyShelfLifeFromAi,含旧字段兼容);
- **Web 仍只读旧 `shelf_life`(整串文本)⇒ 保质期空白**;且 `production_date` 把 AI 原文**直接塞进
  `<input type="date">`**(浏览器只认 YYYY-MM-DD,中文「2026年10月15日」/纯数字 `20261015` 会显示空)。
  服务端提交时虽会归一(stockInboundBatchService.ts:453),但界面先显示空 ⇒ 操作员要重打一遍。

修复(与既有实现同口径,不新造规则、不加任何文案):
- 新增 src/utils/shelfLife.ts:镜像 server/src/utils/shelfLife.ts 的
  normalizeRecognizedDate / shelfLifeUnitSuffix / parseShelfLife / composeShelfLifeText,
  并加页面用的 resolveRecognizedDateFields(item):
  新字段优先(数字+单位→`N年`/`N个月`/`N天`)→ 旧字段整串兼容 → 只有数字填数字留人补单位 → 都没有留空(不猜)
- StockInboundPanel.tsx:AI 结果→行的映射改为 `...resolveRecognizedDateFields(item)`
- workflowApi.ts:识别结果 DTO 补 shelf_life_value / shelf_life_unit(旧 shelf_life 标 @deprecated 仅兜底)

验证:
- 新增 src/utils/shelfLife.parity.test.ts 8/8 通过,含**前端与服务端四个函数逐值对拍**(同一边界输入两侧输出相等)
  + 「新字段/旧字段/只有数字/垃圾数据」四种输入形态的行为用例 + 接线与 DTO 断言
- 前端 npx tsc --noEmit 0 错误
- 前端全量 vitest 3 文件/6 用例失败 = 与基线一致(同名三文件);后端全量 26 文件/33 用例失败 = 基线一致、无新增
- 设计门禁 run-all.sh --verify:通过 8 / 未通过 1(check-ai-cost 既有红)/ 未执行 1 = 基线

⚠️ 生效条件:前端需**重新构建并部署**;服务端无改动、无迁移。
设计:docs/design/20261015-入库识别日期两列Web补齐(生产日期归一与保质期数值+单位).md
2026-10-15 15:53:46 +08:00

125 lines
5.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 入库识别(AI)日期/保质期的**前端同口径**工具 —— 与 `server/src/utils/shelfLife.ts`
* 同算法、同输出(由 `src/utils/shelfLife.parity.test.ts` 逐值对拍钉住,不许各写一套漂移)。
*
* 背景(2026-10-15 开发者指令):「日期让 AI 解析了一下,AI 返回**数字 + 年/月/日**;
* 小程序已经改了,Web 还没改」——小程序按 `shelf_life_value`(数字)+ `shelf_life_unit`(年/月/日)
* 解析(`miniprogram/utils/shelfLife.js` 的 `applyShelfLifeFromAi`),而 Web 仍在读旧的
* `shelf_life`(整串文本)⇒ **保质期空白**;`production_date` 也把 AI 原文直接塞进
* `<input type="date">`(浏览器只认 `YYYY-MM-DD`,中文/纯数字写法会显示空)⇒ 本文件补齐这两件事。
*
* 口径(与服务端一致,勿改):
* · 生产日期:`YYYY-MM-DD` / `YYYY-MM`(补 01)/ 纯数字 `20261014`、`202610` / 中文 `2026年10月14日`、`2026年10月`
* · 保质期:数字 + 单位(年/月/日;`天`/`日`→`天`、`个月`/`月`→`个月`)→ 落库原文 `N天`/`N个月`/`N年`
* · 识别不出**一律不用**(返回 null / 空串),不瞎猜日期
*/
function trimText(value: unknown): string {
return String(value ?? '').trim();
}
/** 识别出的日期归一为 YYYY-MM-DD;只有年月时补 01;否则 null */
export function normalizeRecognizedDate(value: unknown): string | null {
const t = trimText(value);
if (!t) return null;
const full = t.match(/^(\d{4})[-/.](\d{1,2})[-/.](\d{1,2})$/);
if (full) {
return `${full[1]}-${full[2].padStart(2, '0')}-${full[3].padStart(2, '0')}`;
}
const ym = t.match(/^(\d{4})[-/.](\d{1,2})$/);
if (ym) return `${ym[1]}-${ym[2].padStart(2, '0')}-01`;
// 纯数字 / 中文写法(20261014、202610、2026年10月14日、2026年10月)
if (!/[-/.]/.test(t)) {
const digits = t.replace(/\D/g, '');
if (/^\d{8}$/.test(digits)) {
const mm = Number(digits.slice(4, 6));
const dd = Number(digits.slice(6, 8));
if (mm >= 1 && mm <= 12 && dd >= 1 && dd <= 31) {
return `${digits.slice(0, 4)}-${digits.slice(4, 6)}-${digits.slice(6, 8)}`;
}
}
if (/^\d{6}$/.test(digits)) {
const mm = Number(digits.slice(4, 6));
if (mm >= 1 && mm <= 12) {
return `${digits.slice(0, 4)}-${digits.slice(4, 6)}-01`;
}
}
}
return null;
}
/** 保质期单位展示字 → 落库原文后缀(parseShelfLife 可解析) */
export function shelfLifeUnitSuffix(unit: unknown): '天' | '个月' | '年' | null {
const u = trimText(unit);
if (u === '日' || u === '天') return '天';
if (u === '月' || u === '个月') return '个月';
if (u === '年') return '年';
return null;
}
/** 保质期原文 → 单位与数量(支持「7天」「180天」「6个月」「1年」);识别不出返回 null */
export function parseShelfLife(text: unknown): { unit: 'day' | 'month' | 'year'; n: number } | null {
const m = trimText(text).match(/(\d+)\s*(天|日|个月|月|年)/);
if (!m) return null;
const n = Number(m[1]);
if (!Number.isFinite(n) || n <= 0) return null;
const u = m[2];
if (u === '天' || u === '日') return { unit: 'day', n };
if (u === '个月' || u === '月') return { unit: 'month', n };
return { unit: 'year', n };
}
/**
* 数字 + 单位(年/月/日)→ 落库原文;也可吃旧模型整串「12个月」。推不出返回 null。
*/
export function composeShelfLifeText(value: unknown, unit?: unknown): string | null {
const raw = trimText(value);
if (!raw) return null;
// 已是可解析原文(含单位)
if (parseShelfLife(raw)) return raw;
const n = Number(raw);
if (!Number.isFinite(n) || n <= 0) return null;
const suffix = shelfLifeUnitSuffix(unit);
if (!suffix) return null;
return `${Math.trunc(n)}${suffix}`;
}
/**
* AI 识别结果的**日期两列** → 页面行字段(Web 入库面板用)。
*
* 兼容三种输入(新提示词优先,旧字段兜底):
* ① 新:`shelf_life_value`(数字)+ `shelf_life_unit`(年/月/日) ⇒ 组装成「N年/N个月/N天」
* ② 旧:`shelf_life`(整串文本,如「6个月」) ⇒ 归一后原样用作原文
* ③ 都没有 ⇒ 空串(留空给人填,不猜)
* 生产日期一律走 `normalizeRecognizedDate`(`<input type="date">` 只认 YYYY-MM-DD)。
*/
export function resolveRecognizedDateFields(item: {
production_date?: unknown;
shelf_life?: unknown;
shelf_life_value?: unknown;
shelf_life_unit?: unknown;
} | null): { production_date: string; shelf_life: string } {
const productionDate = normalizeRecognizedDate(item?.production_date) || '';
const composed = composeShelfLifeText(item?.shelf_life_value, item?.shelf_life_unit);
if (composed) return { production_date: productionDate, shelf_life: composed };
// 旧字段:整串文本(可能带「个月/天/年」)
const legacy = trimText(item?.shelf_life);
if (legacy && legacy.toLowerCase() !== 'null') {
const parsed = parseShelfLife(legacy);
const normalized = parsed
? composeShelfLifeText(String(parsed.n), parsed.unit === 'day' ? '日' : parsed.unit === 'month' ? '月' : '年')
: null;
return { production_date: productionDate, shelf_life: normalized || legacy };
}
// 新字段只有数字没单位:仍把数字填进原文格(单位留给人补,与服务端「识别不出不推算」同精神)
const valueOnly = trimText(item?.shelf_life_value);
if (valueOnly && valueOnly.toLowerCase() !== 'null' && Number(valueOnly) > 0) {
return { production_date: productionDate, shelf_life: String(Math.trunc(Number(valueOnly))) };
}
return { production_date: productionDate, shelf_life: '' };
}