Files
SupplierSystem/src/utils/purchaseCategoryOrder.ts
T
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

190 lines
6.3 KiB
TypeScript

import {
findCategoryRowByCode,
getCategoryDisplayLabel,
resolveLevel1CategoryCode,
type CategoryRow,
} from './categories';
const UNCATEGORIZED_SORT = 999_999;
export type PurchaseCategoryOrderInfo = {
categoryCode: string;
/** 展示用:一级 / 二级 */
label: string;
l1Code: string;
l1Name: string;
l1Sort: number;
l2Code: string;
l2Name: string;
l2Sort: number;
/** 复制行内二级名(无二级时用一级名) */
lineCategoryName: string;
isUncategorized: boolean;
};
function sortOrderValue(raw: number | null | undefined): number {
if (raw == null || !Number.isFinite(Number(raw))) return 0;
return Number(raw);
}
/** 去掉「一级 / 二级」展示串中的一级前缀,便于按末级名匹配 */
function leafCategoryNameHint(raw: string): string {
const name = String(raw || '').trim();
if (!name) return '';
if (name.includes(' / ')) {
const parts = name.split(' / ').map((p) => p.trim()).filter(Boolean);
return parts[parts.length - 1] || name;
}
if (name.includes('/')) {
const parts = name.split('/').map((p) => p.trim()).filter(Boolean);
return parts[parts.length - 1] || name;
}
return name;
}
/** 按分类名在目录中精确匹配;同名时优先二级 */
export function findCategoryRowByName(
categoryRows: CategoryRow[],
categoryName?: string | null
): CategoryRow | undefined {
const raw = String(categoryName || '').trim();
if (!raw || raw === '未分类' || raw === '-') return undefined;
const candidates = [raw, leafCategoryNameHint(raw)].filter(
(name, index, arr) => name && arr.indexOf(name) === index
);
for (const name of candidates) {
const matches = categoryRows.filter((row) => row.category_name === name);
if (matches.length === 0) continue;
const l2 = matches.find((row) => Boolean(row.parent_code) || row.level === 2);
return l2 || matches[0];
}
return undefined;
}
function infoFromCatalogRow(
categoryRows: CategoryRow[],
row: CategoryRow
): PurchaseCategoryOrderInfo {
const l1Code = resolveLevel1CategoryCode(row.category_code, categoryRows) || row.category_code;
const l1Row = findCategoryRowByCode(categoryRows, l1Code);
const isL2 = Boolean(row.parent_code) || row.level === 2;
const l2Row = isL2 ? row : null;
const l1Name = l1Row?.category_name || row.category_name || '未分类';
const l2Name = l2Row?.category_name || '';
const label =
getCategoryDisplayLabel(categoryRows, row.category_code) || row.category_name || '未分类';
return {
categoryCode: row.category_code,
label,
l1Code,
l1Name,
l1Sort: sortOrderValue(l1Row?.sort_order),
l2Code: l2Row?.category_code || '',
l2Name,
l2Sort: l2Row ? sortOrderValue(l2Row.sort_order) : 0,
lineCategoryName: l2Name || l1Name,
isUncategorized: false,
};
}
/** 解析自采行分类,供排序 / 展示 / 复制 */
export function resolvePurchaseCategoryOrder(
categoryRows: CategoryRow[],
categoryCode?: string | null,
fallbackName?: string | null
): PurchaseCategoryOrderInfo {
const code = String(categoryCode || '').trim();
const fallback = String(fallbackName || '').trim();
if (!code && !fallback) {
return {
categoryCode: '',
label: '未分类',
l1Code: '',
l1Name: '未分类',
l1Sort: UNCATEGORIZED_SORT,
l2Code: '',
l2Name: '',
l2Sort: UNCATEGORIZED_SORT,
lineCategoryName: '未分类',
isUncategorized: true,
};
}
const byCode = code ? findCategoryRowByCode(categoryRows, code) : undefined;
const row = byCode || findCategoryRowByName(categoryRows, fallback) || findCategoryRowByName(categoryRows, code);
if (row) {
return infoFromCatalogRow(categoryRows, row);
}
const name = fallback || code || '未分类';
const uncategorized = name === '未分类';
return {
categoryCode: code,
label: name,
l1Code: code,
l1Name: leafCategoryNameHint(name) || name,
l1Sort: uncategorized ? UNCATEGORIZED_SORT : 500_000,
l2Code: '',
l2Name: '',
l2Sort: 0,
lineCategoryName: leafCategoryNameHint(name) || name,
isUncategorized: uncategorized,
};
}
export type PurchaseCategorySortable = {
createdDay?: string;
categoryCode?: string | null;
categoryName?: string | null;
productName?: string;
pendingCount?: number;
key?: string;
};
/** 配送日升序(先采在前)→ L1 → L2 → 商品名(与列表/复制共用) */
export function comparePurchaseCategoryThenName(
categoryRows: CategoryRow[],
a: PurchaseCategorySortable,
b: PurchaseCategorySortable
): number {
const dayA = String(a.createdDay || '');
const dayB = String(b.createdDay || '');
const byDay = dayA.localeCompare(dayB);
if (byDay !== 0) return byDay;
const infoA = resolvePurchaseCategoryOrder(categoryRows, a.categoryCode, a.categoryName);
const infoB = resolvePurchaseCategoryOrder(categoryRows, b.categoryCode, b.categoryName);
if (infoA.l1Sort !== infoB.l1Sort) return infoA.l1Sort - infoB.l1Sort;
const byL1Name = infoA.l1Name.localeCompare(infoB.l1Name, 'zh-CN');
if (byL1Name !== 0) return byL1Name;
if (infoA.l2Sort !== infoB.l2Sort) return infoA.l2Sort - infoB.l2Sort;
const byL2Name = infoA.l2Name.localeCompare(infoB.l2Name, 'zh-CN');
if (byL2Name !== 0) return byL2Name;
const byProduct = String(a.productName || '').localeCompare(String(b.productName || ''), 'zh-CN');
if (byProduct !== 0) return byProduct;
const pendingA = Number(a.pendingCount) || 0;
const pendingB = Number(b.pendingCount) || 0;
if (pendingA !== pendingB) return pendingB - pendingA;
return String(a.key || '').localeCompare(String(b.key || ''), 'zh-CN');
}
export function compareCategorySummaryOrder(
categoryRows: CategoryRow[],
a: { categoryCode?: string | null; name: string },
b: { categoryCode?: string | null; name: string }
): number {
const infoA = resolvePurchaseCategoryOrder(categoryRows, a.categoryCode, a.name);
const infoB = resolvePurchaseCategoryOrder(categoryRows, b.categoryCode, b.name);
if (infoA.l1Sort !== infoB.l1Sort) return infoA.l1Sort - infoB.l1Sort;
const byL1 = infoA.l1Name.localeCompare(infoB.l1Name, 'zh-CN');
if (byL1 !== 0) return byL1;
if (infoA.l2Sort !== infoB.l2Sort) return infoA.l2Sort - infoB.l2Sort;
return infoA.l2Name.localeCompare(infoB.l2Name, 'zh-CN') || a.name.localeCompare(b.name, 'zh-CN');
}