Files
Ubuntu deb3e72750
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
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库: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 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

415 lines
14 KiB
JavaScript
Raw Permalink 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.
function buildCategoryPickerOptions(categories) {
const mapped = (categories || []).map((item) => ({
category_code: item.category_code || '',
category_name: item.category_name || item.name || '',
label: item.label || item.category_name || item.name || '未分类',
parent_code: item.parent_code ?? null,
level: item.level != null ? Number(item.level) : item.parent_code ? 2 : 1,
sort_order: item.sort_order != null ? Number(item.sort_order) : 0,
}));
return [
{
category_code: '',
category_name: '未分类',
label: '未分类',
parent_code: null,
level: 1,
sort_order: 999999,
},
...mapped,
];
}
function buildCategoryPickerLabels(categoryOptions) {
return (categoryOptions || []).map((item) => item.label || item.category_name || '未分类');
}
function isSameAsCategoryCode(text, categoryCode) {
const value = String(text || '').trim();
const code = String(categoryCode || '').trim();
return Boolean(value && code && value === code);
}
function pickCategoryText(...candidates) {
for (const candidate of candidates) {
const text = String(candidate || '').trim();
if (text && text !== '未分类') return text;
}
return '';
}
function getCategoryDisplay(categoryOptions, categoryCode) {
const code = String(categoryCode || '').trim();
if (!code) return '未分类';
const found = (categoryOptions || []).find((item) => item.category_code === code);
if (found) return found.label || found.category_name || '未分类';
return '未分类';
}
function resolveCategoryLabel(item, categoryOptions) {
const code = String(item?.category_code || '').trim();
const fromCatalog = code ? getCategoryDisplay(categoryOptions, code) : '未分类';
if (fromCatalog && fromCatalog !== '未分类') return fromCatalog;
const rawLabel = String(item?.category_label || '').trim();
const rawName = String(item?.category_name || '').trim();
if (!isSameAsCategoryCode(rawLabel, code) && rawLabel) return rawLabel;
if (!isSameAsCategoryCode(rawName, code) && rawName) return rawName;
return code ? '未分类' : '未分类';
}
function enrichItemCategoryFields(item, categoryOptions) {
const code = String(item.category_code || '').trim();
const label = resolveCategoryLabel(item, categoryOptions);
return {
...item,
category_name: code
? pickCategoryText(label !== '未分类' ? label : '', item.category_name) || ''
: pickCategoryText(item.category_name, label) || '',
category_label: label,
};
}
function enrichItemsCategoryFields(items, categoryOptions) {
return (items || []).map((item) => enrichItemCategoryFields(item, categoryOptions));
}
/** 商品行分类展示文案 */
function categoryDisplayText(item) {
const label = String(item?.category_label || '').trim();
const name = String(item?.category_name || '').trim();
const code = String(item?.category_code || '').trim();
if (label && label !== '未分类' && label !== code) return label;
if (name && name !== '未分类' && name !== code) return name;
return '选择分类';
}
function categoryHasValue(item) {
return categoryDisplayText(item) !== '选择分类';
}
function applyManualCategory(item, option) {
const code = option?.category_code || '';
const label = pickCategoryText(option?.label, option?.category_name) || '未分类';
const name = pickCategoryText(option?.category_name, option?.label) || label;
const displayName = name !== '未分类' ? name : (label !== '未分类' ? label : '');
return {
...item,
category_code: code,
category_name: displayName,
category_label: label !== '未分类' ? label : (displayName || '未分类'),
category_manually_set: Boolean(code),
library_category_code: item.library_category_code || code || '',
};
}
function toHalfWidthNumber(value) {
if (typeof value === 'number') return Number.isFinite(value) ? value : NaN;
const text = String(value ?? '')
.trim()
.replace(/[\uFF10-\uFF19]/g, (ch) => String.fromCharCode(ch.charCodeAt(0) - 0xfee0))
.replace(/[\uFF0E\u3002.,]/g, '.')
.replace(/\s+/g, '');
const match = text.match(/^(\d+(?:\.\d+)?)/);
return match ? Number.parseFloat(match[1]) : Number(text);
}
function mapParsedItem(row, smartClassifyEnabled, categoryOptions) {
const qty = toHalfWidthNumber(row.quantity);
const entryName = String(
row.originalName || row.original_name || row.item_name || ''
).trim();
const catalogName = String(
row.catalog_sku_name || row.sku_name || row.skuName || ''
).trim();
const base = {
item_name: entryName,
catalog_sku_name: catalogName,
sku_code: row.sku_code || row.skuCode || '',
quantity: Number.isFinite(qty) && qty > 0 ? Math.round(qty * 1000) / 1000 : 1,
unit: row.unit || '斤',
price: Number(row.price) || 0,
amount: Number(row.amount) || 0,
processing_method: row.processing_method || row.processingMethod || '原材料',
processing_owner: row.processing_owner || row.processingOwner || null,
process_unit_price: Number(row.process_unit_price || row.processUnitPrice) || 0,
remark: String(row.remark || '').trim().slice(0, 500),
category_manually_set: false,
library_category_code: '',
match_layer: row.match_layer ?? row.matchLayer ?? null,
in_customer_library: Boolean(row.in_customer_library ?? row.inCustomerLibrary),
name_mismatch: Boolean(
entryName &&
catalogName &&
String(row.sku_code || row.skuCode || '').trim() &&
entryName !== catalogName
),
};
if (!smartClassifyEnabled) {
return enrichItemCategoryFields({
...base,
category_code: '',
category_name: '',
}, categoryOptions);
}
const categoryCode = row.category_code || row.categoryCode || '';
const fromParseCategory = Boolean(categoryCode);
return enrichItemCategoryFields({
...base,
category_code: categoryCode,
category_name: row.category_name || row.categoryName || '',
library_category_code: categoryCode,
category_manually_set: fromParseCategory,
}, categoryOptions);
}
function stripAutoCategoriesUnlessManual(item, categoryOptions) {
if (item.category_manually_set) {
return enrichItemCategoryFields(item, categoryOptions);
}
return enrichItemCategoryFields({
...item,
category_code: '',
category_name: '',
category_manually_set: false,
library_category_code: '',
}, categoryOptions);
}
function findCategoryOptionByName(name, categoryOptions) {
const normalized = String(name || '').trim().toLowerCase();
if (!normalized || normalized === '未分类') return null;
return (categoryOptions || []).find((item) => {
const label = String(item.label || item.category_name || '').trim().toLowerCase();
const code = String(item.category_code || '').trim().toLowerCase();
const cname = String(item.category_name || '').trim().toLowerCase();
return label === normalized || cname === normalized || code === normalized;
}) || null;
}
/** 按名称/编码模糊筛选分类(供输入框联想) */
function filterCategoryOptions(query, categoryOptions, limit = 10) {
const list = (categoryOptions || []).filter((item) => item.category_code);
const q = String(query || '').trim().toLowerCase();
if (!q) return list.slice(0, limit);
return list
.filter((item) => {
const label = String(item.label || item.category_name || '').toLowerCase();
const code = String(item.category_code || '').toLowerCase();
const cname = String(item.category_name || '').toLowerCase();
return label.includes(q) || cname.includes(q) || code.includes(q);
})
.slice(0, limit);
}
/** 分类输入框展示值(未分类时留空以显示 placeholder) */
function categoryInputValue(item) {
const label = String(item.category_label || item.category_name || '').trim();
if (!item.category_code && (!label || label === '未分类')) return '';
return label === '未分类' ? '' : label;
}
function applyCategoryInput(item, name, categoryOptions) {
const trimmed = String(name || '').trim();
if (!trimmed) {
return {
...item,
category_code: '',
category_name: '',
category_label: '未分类',
category_manually_set: false,
};
}
const matched = findCategoryOptionByName(trimmed, categoryOptions);
if (matched?.category_code) {
return applyManualCategory(item, matched);
}
return {
...item,
category_code: '',
category_name: trimmed,
category_label: trimmed,
category_manually_set: true,
library_category_code: item.library_category_code || '',
};
}
function resolveItemsForSubmit(items, categoryOptions, smartClassifyEnabled) {
return (items || []).map((item) => {
const withCategory = applyCategoryInput(
item,
item.category_name || item.category_label,
categoryOptions
);
return buildSubmitItem(withCategory, smartClassifyEnabled);
});
}
function buildSubmitItem(item, smartClassifyEnabled) {
const remark = String(item.remark || '').trim().slice(0, 500);
const processingMethod = String(item.processing_method || '').trim() || '原材料';
const {
normalizeProcessingOwner,
resolveProcessUnitPrice,
} = require('./processingOwner');
const processingOwner = normalizeProcessingOwner(processingMethod, item.processing_owner);
return {
item_name: String(item.item_name || '').trim(),
sku_code: item.sku_code || undefined,
quantity: Number(item.quantity || 0) || 1,
unit: item.unit || '斤',
price: Number(item.price) || 0,
processing_method: processingMethod,
processing_owner: processingOwner,
process_unit_price: resolveProcessUnitPrice(processingOwner, item.process_unit_price),
category_code: item.category_code || undefined,
category_name: item.category_name || undefined,
category_manually_set: Boolean(item.category_manually_set),
library_category_code: item.library_category_code || item.category_code || undefined,
in_customer_library: Boolean(item.in_customer_library),
smart_classify_enabled: smartClassifyEnabled,
remark: remark || null,
};
}
function validateItemsCategories(items) {
const missing = (items || []).filter((item) => {
const name = String(item.item_name || '').trim();
if (!name) return false;
const catName = String(item.category_name || item.category_label || '').trim();
const catCode = String(item.category_code || '').trim();
return !catName && !catCode;
});
if (missing.length) {
return `请为 ${missing.length} 个商品填写分类后再提交`;
}
return '';
}
function emptyCategoryFields() {
return {
category_code: '',
category_name: '',
category_label: '未分类',
category_manually_set: false,
library_category_code: '',
match_layer: null,
in_customer_library: false,
};
}
function matchLayerLabel(layer) {
if (layer === 0) return '映射库';
if (layer === 1) return '客户别名';
if (layer === 3) return '商品库';
return '';
}
function mappingStatusText(item) {
if (item.create_product && !item.sku_code) return '待新建';
if (item.in_customer_library) return '已在映射库';
if (item.sku_code) return '提交后入库';
return '新商品';
}
function parseLevel1NameFromLabel(label) {
const text = String(label || '').trim();
if (!text || text === '未分类') return '未分类';
const splitIndex = text.indexOf(' / ');
if (splitIndex > 0) return text.slice(0, splitIndex).trim();
return text;
}
function resolveLevel1CategoryCode(categoryCode, flat) {
const code = String(categoryCode || '').trim();
if (!code) return '';
const map = new Map((flat || []).filter((row) => row.category_code).map((row) => [row.category_code, row]));
let current = code;
const visited = new Set();
while (current && !visited.has(current)) {
visited.add(current);
const row = map.get(current);
if (!row) break;
if (Number(row.level) === 1 || !row.parent_code) return current;
current = row.parent_code;
}
return code;
}
function resolveLevel1CategoryInfo(item, flat) {
const code = String(item?.category_code || '').trim();
if (code && flat && flat.length) {
const level1Code = resolveLevel1CategoryCode(code, flat);
const level1Row = flat.find((row) => row.category_code === level1Code);
const name = level1Row?.category_name
|| parseLevel1NameFromLabel(item.category_label || item.category_name)
|| '未分类';
return { code: level1Code || '', name };
}
const name = parseLevel1NameFromLabel(item.category_label || item.category_name) || '未分类';
return { code: '', name };
}
function buildCategorySummary(items, categoryFlat) {
const map = new Map();
(items || []).forEach((item) => {
const { code, name } = resolveLevel1CategoryInfo(item, categoryFlat);
const key = code || name;
const prev = map.get(key) || { name, code, count: 0, amount: 0 };
prev.count += 1;
prev.amount += Number(item.amount) || 0;
map.set(key, prev);
});
const columns = Array.from(map.values()).map((row) => ({
...row,
amountText: row.amount.toFixed(2),
}));
const totalCount = (items || []).length;
const totalAmount = (items || []).reduce((sum, item) => sum + (Number(item.amount) || 0), 0);
return {
categoryColumns: columns,
totalCount,
totalAmountText: totalAmount.toFixed(2),
};
}
function buildItemGroupsByLevel1(items, categoryFlat) {
const groupMap = new Map();
(items || []).forEach((item, index) => {
const { code, name } = resolveLevel1CategoryInfo(item, categoryFlat);
const key = code || name;
const prev = groupMap.get(key) || { code, name, items: [] };
prev.items.push({ ...item, _itemIndex: index });
groupMap.set(key, prev);
});
return Array.from(groupMap.values());
}
module.exports = {
buildCategorySummary,
buildItemGroupsByLevel1,
resolveLevel1CategoryInfo,
resolveLevel1CategoryCode,
buildCategoryPickerOptions,
buildCategoryPickerLabels,
getCategoryDisplay,
categoryDisplayText,
categoryHasValue,
enrichItemCategoryFields,
enrichItemsCategoryFields,
applyManualCategory,
applyCategoryInput,
resolveItemsForSubmit,
findCategoryOptionByName,
filterCategoryOptions,
categoryInputValue,
mapParsedItem,
stripAutoCategoriesUnlessManual,
buildSubmitItem,
validateItemsCategories,
emptyCategoryFields,
matchLayerLabel,
mappingStatusText,
};