Files
SupplierSystem/server/src/utils/shelfLife.ts
T
雨天andCursor e3ae7ee6e7
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
feat(库存入库): 按行入库 + 保质期年/月/日选择 + 弹层抬层
小程序识别结果逐行入库;保质期拆数值与单位(AI 三选一或人手选)落库原文;选择商品弹层 root-portal 防 tab 遮挡,并预填 AI 品名。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-15 10:43:47 +08:00

121 lines
5.1 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.
/**
* 保质期原文 → 有效期(`inventory.expiry_date`)的**共用口径**。
*
* 背景(开发者口径):保质期以**字符串原文**存储(`inventory.shelf_life`),单位可能是
* 天 / 月 / 年(如「7天」「6个月」「1年」);有效期**一律由「生产日期 + 保质期」推算**,
* 不再让人直接填一个日期(库存管理「修改」弹窗与入库链路同一套)。
*
* ⚠️ 换算口径:**月按 30 天、年按 360 天**推进(不是日历月/年)—— 沿用入库链路既有口径。
* ⚠️ 推不出(生产日期缺失 / 保质期识别不出)**返回 null** —— 调用方不得瞎写日期。
*
* 2026-10-15 补充(AI 侧口径):入库多商品识别的提示词要求模型把保质期拆成
* `shelf_life_value`(数字)+ `shelf_life_unit`(**只能年/月/日,识别不出填 null**),
* 落库前用 `composeShelfLifeText` 组装回原文 —— 存的是原文、算的是同一套解析,两侧不漂。
*/
function trimText(value: unknown): string {
return String(value ?? '').trim();
}
function toYmd(d: Date): string {
const pad = (n: number) => String(n).padStart(2, '0');
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
}
/**
* 识别出的日期归一为 YYYY-MM-DD;只有年月时补 01;否则 null。
* 兼容模型常吐的纯数字(`20261014` / `202610`)与中文「2026年10月14日」。
*/
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;
}
/**
* 数字 + 单位(年/月/日)→ 落库原文;也可吃旧模型整串「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}`;
}
/** 保质期原文 → 单位与数量(支持「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 };
}
/**
* 由「生产日期 + 保质期原文」推算有效期(`YYYY-MM-DD`);推不出返回 null。
*
* - 保质期本身是个**完整日期**时(有些标签直接印截止日)⇒ 直接用它;
* - 否则 = 生产日期 + 保质期(天/月/年,月=30天、年=360天);
* - 生产日期缺失或保质期识别不出 ⇒ null(**不写**)。
*/
export function deriveExpiryDate(productionDate: unknown, shelfLife: unknown): string | null {
const shelf = trimText(shelfLife);
if (!shelf) return null;
const absShelf = normalizeRecognizedDate(shelf);
if (absShelf && /^\d{4}-\d{2}-\d{2}$/.test(absShelf) && /\d{4}[-/.]\d{1,2}[-/.]\d{1,2}/.test(shelf)) {
return absShelf;
}
const baseYmd = normalizeRecognizedDate(productionDate);
if (!baseYmd) return null;
const parsed = parseShelfLife(shelf);
if (!parsed) return null;
const base = new Date(`${baseYmd}T00:00:00`);
if (Number.isNaN(base.getTime())) return null;
const d = new Date(base.getTime());
if (parsed.unit === 'day') d.setDate(d.getDate() + parsed.n);
else if (parsed.unit === 'month') d.setDate(d.getDate() + parsed.n * 30);
else d.setDate(d.getDate() + parsed.n * 360);
return toYmd(d);
}