来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
164 lines
5.1 KiB
TypeScript
164 lines
5.1 KiB
TypeScript
import { pool } from '../db';
|
|
import type { TenantContext } from './tenantScope';
|
|
|
|
export type SupplierDirectoryRow = {
|
|
supplier_code: string;
|
|
company_name: string;
|
|
contact_name: string | null;
|
|
status: number | null;
|
|
skip_inbound: boolean;
|
|
};
|
|
|
|
export function resolveSupplierSkipInboundFilter(raw: unknown): string {
|
|
if (raw === undefined || raw === null) return '';
|
|
const value = String(raw).trim();
|
|
if (value === '' || value === 'all') return '';
|
|
if (value === '0' || value === '1') return value;
|
|
throw Object.assign(new Error('skip_inbound 只能是 0、1、all 或空'), {
|
|
status: 400,
|
|
code: 'invalid_request',
|
|
});
|
|
}
|
|
|
|
export function resolveSupplierListStatusFilter(raw: unknown): string {
|
|
const status = String(raw || '').trim();
|
|
if (!status) return '';
|
|
if (!/^(10|20|30|40)$/.test(status)) {
|
|
throw Object.assign(new Error('status 只能是 10、20、30、40 或空'), {
|
|
status: 400,
|
|
code: 'invalid_request',
|
|
});
|
|
}
|
|
return status;
|
|
}
|
|
|
|
export function resolveSupplierCompositeFilter(raw: unknown): string {
|
|
const value = String(raw || '').trim();
|
|
if (!value) return '';
|
|
if (value !== 'has' && value !== 'none') {
|
|
throw Object.assign(new Error('composite 只能是 has、none 或空'), {
|
|
status: 400,
|
|
code: 'invalid_request',
|
|
});
|
|
}
|
|
return value;
|
|
}
|
|
|
|
function categoryMatchSql(branchParam: number, filterParam: number): string {
|
|
return `EXISTS (
|
|
SELECT 1
|
|
FROM supplier_categories sc
|
|
LEFT JOIN product_catalog pc
|
|
ON pc.item_type = 'category'
|
|
AND pc.code = sc.category_code
|
|
AND pc.branch_code = $${branchParam}
|
|
LEFT JOIN product_catalog filt
|
|
ON filt.item_type = 'category'
|
|
AND filt.code = $${filterParam}
|
|
AND filt.branch_code = $${branchParam}
|
|
WHERE sc.supplier_code = s.supplier_code
|
|
AND (
|
|
sc.category_code = $${filterParam}
|
|
OR COALESCE(pc.parent_code, '') = $${filterParam}
|
|
OR (
|
|
COALESCE(filt.parent_code, '') <> ''
|
|
AND sc.category_code = filt.parent_code
|
|
)
|
|
)
|
|
)`;
|
|
}
|
|
|
|
export async function listSuppliersForManagement(input: {
|
|
ctx: TenantContext;
|
|
search?: string;
|
|
status?: unknown;
|
|
skip_inbound?: unknown;
|
|
category?: string;
|
|
sku?: string;
|
|
composite?: unknown;
|
|
limit?: number;
|
|
}): Promise<{
|
|
rows: SupplierDirectoryRow[];
|
|
total: number;
|
|
status: string;
|
|
skip_inbound: string;
|
|
category: string;
|
|
sku: string;
|
|
composite: string;
|
|
}> {
|
|
const branchCode = String(input.ctx.branchCode || '').trim();
|
|
if (!branchCode) {
|
|
throw Object.assign(new Error('请选择当前操作分公司'), { status: 400, code: 'invalid_request' });
|
|
}
|
|
const status = resolveSupplierListStatusFilter(input.status);
|
|
const skipInbound = resolveSupplierSkipInboundFilter(input.skip_inbound);
|
|
const composite = resolveSupplierCompositeFilter(input.composite);
|
|
const sku = String(input.sku || '').trim();
|
|
const categoryArg = sku ? '' : String(input.category || '').trim();
|
|
let categoryForSql = categoryArg;
|
|
if (sku) {
|
|
const skuRow = await pool.query(
|
|
`SELECT parent_code FROM product_catalog
|
|
WHERE item_type = 'product' AND branch_code = $1 AND code = $2
|
|
LIMIT 1`,
|
|
[branchCode, sku]
|
|
);
|
|
categoryForSql = String(skuRow.rows[0]?.parent_code || '').trim();
|
|
}
|
|
const params: unknown[] = [branchCode];
|
|
const where = [`s.branch_code = $1`];
|
|
if (status) {
|
|
params.push(Number(status));
|
|
where.push(`s.status = $${params.length}`);
|
|
}
|
|
if (skipInbound) {
|
|
where.push(`COALESCE(s.skip_inbound, false) = ${skipInbound === '1' ? 'true' : 'false'}`);
|
|
}
|
|
const search = String(input.search || '').trim();
|
|
if (search) {
|
|
params.push(`%${search}%`);
|
|
where.push(`(s.company_name ILIKE $${params.length} OR s.supplier_code ILIKE $${params.length})`);
|
|
}
|
|
if (composite === 'has' || composite === 'none') {
|
|
const existsSnap = `EXISTS (
|
|
SELECT 1 FROM supplier_rating_snapshots snap
|
|
WHERE snap.supplier_code = s.supplier_code
|
|
AND snap.branch_code = $1
|
|
AND snap.composite_score IS NOT NULL
|
|
)`;
|
|
where.push(composite === 'has' ? existsSnap : `NOT ${existsSnap}`);
|
|
}
|
|
if (categoryForSql) {
|
|
params.push(categoryForSql);
|
|
where.push(categoryMatchSql(1, params.length));
|
|
}
|
|
const limit = Math.min(Math.max(Number(input.limit) || 50, 1), 50);
|
|
params.push(limit);
|
|
const result = await pool.query(
|
|
`SELECT s.supplier_code, s.company_name, s.contact_name, s.status,
|
|
COALESCE(s.skip_inbound, false) AS skip_inbound,
|
|
COUNT(*) OVER()::int AS total
|
|
FROM suppliers s
|
|
WHERE ${where.join(' AND ')}
|
|
ORDER BY s.created_at DESC
|
|
LIMIT $${params.length}`,
|
|
params
|
|
);
|
|
const rows = result.rows.map((row) => ({
|
|
supplier_code: String(row.supplier_code || ''),
|
|
company_name: String(row.company_name || ''),
|
|
contact_name: row.contact_name == null ? null : String(row.contact_name),
|
|
status: row.status == null ? null : Number(row.status),
|
|
skip_inbound: row.skip_inbound === true,
|
|
}));
|
|
return {
|
|
rows,
|
|
total: Number(result.rows[0]?.total) || rows.length,
|
|
status,
|
|
skip_inbound: skipInbound,
|
|
category: categoryArg,
|
|
sku,
|
|
composite,
|
|
};
|
|
}
|