来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
95 lines
3.1 KiB
TypeScript
95 lines
3.1 KiB
TypeScript
import { pool } from '../db';
|
|
|
|
export type MarketComparePriceBasis = 'cost' | 'sale';
|
|
|
|
export interface MarketComparePriceBasisConfig {
|
|
/** cost=报价成本;sale=报价销售价 */
|
|
basis: MarketComparePriceBasis;
|
|
}
|
|
|
|
const CONFIG_KEY = 'market_compare_price_basis';
|
|
const DEFAULT_CONFIG: MarketComparePriceBasisConfig = { basis: 'cost' };
|
|
|
|
export function normalizeMarketComparePriceBasis(raw: unknown): MarketComparePriceBasis {
|
|
const s = String(raw || '')
|
|
.trim()
|
|
.toLowerCase();
|
|
if (s === 'sale' || s === 'sales' || s === 'sell' || s === '销售价') return 'sale';
|
|
return 'cost';
|
|
}
|
|
|
|
function parseConfigValue(raw: unknown): MarketComparePriceBasisConfig {
|
|
if (!raw || typeof raw !== 'object') return { ...DEFAULT_CONFIG };
|
|
const obj = raw as Record<string, unknown>;
|
|
return { basis: normalizeMarketComparePriceBasis(obj.basis ?? obj.price_basis) };
|
|
}
|
|
|
|
export async function getMarketComparePriceBasisConfig(
|
|
branchCode?: string | null
|
|
): Promise<MarketComparePriceBasisConfig> {
|
|
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 parseConfigValue(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 parseConfigValue(globalRow.rows[0].config_value);
|
|
}
|
|
return { ...DEFAULT_CONFIG };
|
|
}
|
|
|
|
/** 该分公司是否已有独立比价基准配置行(不含全局回退) */
|
|
export async function hasMarketComparePriceBasisConfig(branchCode?: string | null): Promise<boolean> {
|
|
const code = String(branchCode || '').trim();
|
|
if (!code) return false;
|
|
const row = await pool.query(
|
|
`SELECT 1 FROM system_configs
|
|
WHERE config_key = $1 AND branch_code = $2
|
|
LIMIT 1`,
|
|
[CONFIG_KEY, code]
|
|
);
|
|
return (row.rowCount || 0) > 0;
|
|
}
|
|
|
|
export async function setMarketComparePriceBasisConfig(
|
|
branchCode: string,
|
|
basis: MarketComparePriceBasis | string
|
|
): Promise<MarketComparePriceBasisConfig> {
|
|
const branch = String(branchCode || '').trim();
|
|
if (!branch) {
|
|
throw Object.assign(new Error('缺少分公司编码'), { status: 400 });
|
|
}
|
|
const normalized = normalizeMarketComparePriceBasis(basis);
|
|
const payload = JSON.stringify({ basis: normalized });
|
|
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, 'market',
|
|
'市场比价「我们的价格」基准:cost=报价成本价,sale=报价销售价',
|
|
$3)`,
|
|
[CONFIG_KEY, payload, branch]
|
|
);
|
|
}
|
|
return { basis: normalized };
|
|
}
|