来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
114 lines
3.3 KiB
TypeScript
114 lines
3.3 KiB
TypeScript
/** 客户商品映射库行(customer_skus) */
|
||
export interface CustomerMappingRow {
|
||
sku_code: string;
|
||
customer_sku_name: string | null;
|
||
category_code: string | null;
|
||
processing_method?: string | null;
|
||
processing_owner?: string | null;
|
||
customer_unit?: string | null;
|
||
}
|
||
|
||
/** 仅从客户映射库解析分类(不读 skus 表默认分类) */
|
||
export function resolveCategoryFromCustomerMapping(
|
||
rows: CustomerMappingRow[],
|
||
displayName: string,
|
||
skuCode?: string
|
||
): { categoryCode: string; fromMapping: boolean } {
|
||
const normalizedName = displayName.trim();
|
||
|
||
if (skuCode) {
|
||
const bySku = rows.find((row) => row.sku_code === skuCode && row.category_code);
|
||
if (bySku?.category_code) {
|
||
return { categoryCode: bySku.category_code, fromMapping: true };
|
||
}
|
||
}
|
||
|
||
if (normalizedName) {
|
||
const byName = rows.find(
|
||
(row) =>
|
||
row.customer_sku_name &&
|
||
row.customer_sku_name.trim() === normalizedName &&
|
||
row.category_code
|
||
);
|
||
if (byName?.category_code) {
|
||
return { categoryCode: byName.category_code, fromMapping: true };
|
||
}
|
||
}
|
||
|
||
return { categoryCode: '', fromMapping: false };
|
||
}
|
||
|
||
export function isInCustomerMappingLibrary(
|
||
rows: CustomerMappingRow[],
|
||
displayName: string,
|
||
skuCode?: string
|
||
): boolean {
|
||
if (skuCode && rows.some((row) => row.sku_code === skuCode)) {
|
||
return true;
|
||
}
|
||
const normalizedName = displayName.trim();
|
||
if (!normalizedName) return false;
|
||
return rows.some(
|
||
(row) => row.customer_sku_name && row.customer_sku_name.trim() === normalizedName
|
||
);
|
||
}
|
||
|
||
export interface SmartClassificationRule {
|
||
keyword: string;
|
||
category_code: string;
|
||
match_type?: string | null;
|
||
priority?: number | null;
|
||
}
|
||
|
||
/** 从智能分类规则匹配(录单同步写入的规则,映射库无 SKU 时的兜底) */
|
||
export function resolveCategoryFromSmartRules(
|
||
itemName: string,
|
||
rules: SmartClassificationRule[]
|
||
): string | undefined {
|
||
const normalized = itemName.trim().toLowerCase();
|
||
if (!normalized || !rules.length) return undefined;
|
||
|
||
const ordered = [...rules].sort((a, b) => {
|
||
const priorityDiff = (b.priority ?? 0) - (a.priority ?? 0);
|
||
if (priorityDiff !== 0) return priorityDiff;
|
||
return b.keyword.length - a.keyword.length;
|
||
});
|
||
|
||
for (const rule of ordered) {
|
||
const keyword = rule.keyword.trim().toLowerCase();
|
||
if (!keyword) continue;
|
||
if (rule.match_type === 'exact') {
|
||
if (normalized === keyword) return rule.category_code;
|
||
continue;
|
||
}
|
||
if (normalized.includes(keyword)) return rule.category_code;
|
||
}
|
||
|
||
return undefined;
|
||
}
|
||
|
||
/** 录单/订单分类解析:商品管理(SKU分类) → 解析建议 → 客户映射 → 智能规则 */
|
||
export function resolveOrderItemCategory(params: {
|
||
displayName: string;
|
||
skuCode?: string;
|
||
customerSkus: CustomerMappingRow[];
|
||
smartRules: SmartClassificationRule[];
|
||
parseCategoryCode?: string;
|
||
skuCategoryCode?: string;
|
||
}): string {
|
||
if (params.skuCategoryCode) return params.skuCategoryCode;
|
||
if (params.parseCategoryCode) return params.parseCategoryCode;
|
||
|
||
const mapping = resolveCategoryFromCustomerMapping(
|
||
params.customerSkus,
|
||
params.displayName,
|
||
params.skuCode
|
||
);
|
||
if (mapping.categoryCode) return mapping.categoryCode;
|
||
|
||
const fromRules = resolveCategoryFromSmartRules(params.displayName, params.smartRules);
|
||
if (fromRules) return fromRules;
|
||
|
||
return '';
|
||
}
|