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

95 lines
3.2 KiB
TypeScript

import { pool } from '../db';
import {
DEFAULT_ALLOCATION_RULES,
type AllocationRules,
} from './allocationRules';
const CONFIG_KEY = 'allocation_rules';
function parseRules(raw: unknown): AllocationRules {
if (!raw || typeof raw !== 'object') return { ...DEFAULT_ALLOCATION_RULES };
const obj = raw as Record<string, unknown>;
return {
noSkuToPurchaser:
obj.noSkuToPurchaser === undefined
? DEFAULT_ALLOCATION_RULES.noSkuToPurchaser
: Boolean(obj.noSkuToPurchaser),
uncategorizedToPurchaser:
obj.uncategorizedToPurchaser === undefined
? DEFAULT_ALLOCATION_RULES.uncategorizedToPurchaser
: Boolean(obj.uncategorizedToPurchaser),
categoryMatchToSupplier:
obj.categoryMatchToSupplier === undefined
? DEFAULT_ALLOCATION_RULES.categoryMatchToSupplier
: Boolean(obj.categoryMatchToSupplier),
noCategorySupplierToPurchaser:
obj.noCategorySupplierToPurchaser === undefined
? DEFAULT_ALLOCATION_RULES.noCategorySupplierToPurchaser
: Boolean(obj.noCategorySupplierToPurchaser),
preferPrimarySupplier:
obj.preferPrimarySupplier === undefined
? DEFAULT_ALLOCATION_RULES.preferPrimarySupplier
: Boolean(obj.preferPrimarySupplier),
autoSwitchOnFulfillmentRisk:
obj.autoSwitchOnFulfillmentRisk === undefined
? DEFAULT_ALLOCATION_RULES.autoSwitchOnFulfillmentRisk
: Boolean(obj.autoSwitchOnFulfillmentRisk),
};
}
export async function getAllocationRulesConfig(
branchCode?: string | null
): Promise<AllocationRules> {
const code = String(branchCode || '').trim();
if (code) {
const branchRow = await pool.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 parseRules(branchRow.rows[0].config_value);
}
}
const globalRow = await pool.query(
`SELECT config_value FROM system_configs
WHERE config_key = $1 AND branch_code IS NULL
LIMIT 1`,
[CONFIG_KEY]
);
if (globalRow.rows[0]?.config_value != null) {
return parseRules(globalRow.rows[0].config_value);
}
return { ...DEFAULT_ALLOCATION_RULES };
}
export async function setAllocationRulesConfig(
branchCode: string,
rules: Partial<AllocationRules>
): Promise<AllocationRules> {
const branch = String(branchCode || '').trim();
if (!branch) {
throw Object.assign(new Error('缺少分公司编码'), { status: 400 });
}
const current = await getAllocationRulesConfig(branch);
const next = parseRules({ ...current, ...rules });
const payload = JSON.stringify(next);
const updated = await pool.query(
`UPDATE system_configs
SET config_value = $1::jsonb, updated_at = NOW()
WHERE config_key = $2 AND branch_code = $3`,
[payload, CONFIG_KEY, branch]
);
if (!updated.rowCount) {
await pool.query(
`INSERT INTO system_configs (config_key, config_value, config_group, description, branch_code)
VALUES ($1, $2::jsonb, 'allocation',
'智能分配规则(无SKU/分类供应商匹配等)',
$3)`,
[CONFIG_KEY, payload, branch]
);
}
return next;
}