Files
SupplierSystem/server/src/services/purchaseCategoryPurchaserMapService.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

104 lines
3.6 KiB
TypeScript

import { pool } from '../db';
import type { Pool, PoolClient } from 'pg';
import { listBranchPurchasers } from './purchasePurchaserAssignService';
export type CategoryPurchaserBinding = {
category_code: string;
purchaser_code: string;
};
export type CategoryPurchaserMapConfig = {
bindings: CategoryPurchaserBinding[];
};
type Db = Pool | PoolClient;
const CONFIG_KEY = 'purchase_category_purchaser_map';
const CONFIG_GROUP = 'purchase';
const CONFIG_DESCRIPTION = '自采分类固定采购员:category_code → purchaser_code;与默认分配 mode 解耦';
export function parseCategoryPurchaserMap(raw: unknown): CategoryPurchaserMapConfig {
if (raw == null || typeof raw !== 'object') return { bindings: [] };
const obj = raw as Record<string, unknown>;
const list = Array.isArray(obj.bindings) ? obj.bindings : [];
const seen = new Set<string>();
const bindings: CategoryPurchaserBinding[] = [];
for (const item of list) {
if (!item || typeof item !== 'object') continue;
const row = item as Record<string, unknown>;
const category_code = String(row.category_code || '').trim();
const purchaser_code = String(row.purchaser_code || '').trim();
if (!category_code || !purchaser_code || seen.has(category_code)) continue;
seen.add(category_code);
bindings.push({ category_code, purchaser_code });
}
return { bindings };
}
export async function getCategoryPurchaserMap(
branchCode?: string | null,
db: Db = pool
): Promise<CategoryPurchaserMapConfig> {
const code = String(branchCode || '').trim();
if (!code) return { bindings: [] };
const branchRow = await db.query(
`SELECT config_value FROM system_configs
WHERE config_key = $1 AND branch_code = $2
LIMIT 1`,
[CONFIG_KEY, code]
);
if (branchRow.rows[0]?.config_value != null) {
return parseCategoryPurchaserMap(branchRow.rows[0].config_value);
}
return { bindings: [] };
}
export async function setCategoryPurchaserMap(
branchCode: string,
input: { bindings?: unknown },
db: Db = pool
): Promise<CategoryPurchaserMapConfig> {
const branch = String(branchCode || '').trim();
if (!branch) {
throw Object.assign(new Error('缺少分公司编码'), { status: 400, code: 'invalid_request' });
}
const parsed = parseCategoryPurchaserMap({ bindings: input.bindings });
const purchasers = await listBranchPurchasers(db, branch);
const allowed = new Set(purchasers.map((p) => p.purchaser_code));
for (const row of parsed.bindings) {
if (!allowed.has(row.purchaser_code)) {
throw Object.assign(new Error(`采购员 ${row.purchaser_code} 不在本分公司启用列表`), {
status: 400,
code: 'purchaser_required',
});
}
}
const payload = JSON.stringify(parsed);
const updated = await db.query(
`UPDATE system_configs
SET config_value = $1::jsonb, updated_at = NOW(), description = $4
WHERE config_key = $2 AND branch_code = $3`,
[payload, CONFIG_KEY, branch, CONFIG_DESCRIPTION]
);
if (!updated.rowCount) {
await db.query(
`INSERT INTO system_configs (config_key, config_value, config_group, description, branch_code)
VALUES ($1, $2::jsonb, $4, $5, $3)`,
[CONFIG_KEY, payload, branch, CONFIG_GROUP, CONFIG_DESCRIPTION]
);
}
return parsed;
}
export async function lookupMappedPurchaserCode(
branchCode: string | null | undefined,
categoryCode: string | null | undefined,
db: Db = pool
): Promise<string | null> {
const cat = String(categoryCode || '').trim();
if (!cat) return null;
const map = await getCategoryPurchaserMap(branchCode, db);
const hit = map.bindings.find((b) => b.category_code === cat);
return hit?.purchaser_code || null;
}