来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
193 lines
6.6 KiB
TypeScript
193 lines
6.6 KiB
TypeScript
import { pool } from '../db';
|
|
import { renameFreeCostType, listFreeCostTypes, labelForFreeSubtype } from './freeCostService';
|
|
import {
|
|
renameFixedCostType,
|
|
listFixedCostTypes,
|
|
labelForFixedCostType,
|
|
} from './fixedCostTypeService';
|
|
|
|
const COST_DISPLAY_NAMES_KEY = 'cost_display_names';
|
|
|
|
const DEFAULT_DISPLAY_NAMES: Record<string, string> = {
|
|
product: '采购成本',
|
|
};
|
|
|
|
function normalizeNameMap(raw: unknown): Record<string, string> {
|
|
let obj: unknown = raw;
|
|
if (typeof raw === 'string') {
|
|
try {
|
|
obj = JSON.parse(raw);
|
|
} catch {
|
|
return { ...DEFAULT_DISPLAY_NAMES };
|
|
}
|
|
}
|
|
if (!obj || typeof obj !== 'object' || Array.isArray(obj)) {
|
|
return { ...DEFAULT_DISPLAY_NAMES };
|
|
}
|
|
const out: Record<string, string> = { ...DEFAULT_DISPLAY_NAMES };
|
|
for (const [k, v] of Object.entries(obj as Record<string, unknown>)) {
|
|
const key = String(k || '')
|
|
.trim()
|
|
.toLowerCase()
|
|
.slice(0, 64);
|
|
const label = String(v || '').trim().slice(0, 40);
|
|
if (!key || !label) continue;
|
|
out[key] = label;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
export async function getCostDisplayNames(branchCode?: string | null): Promise<Record<string, string>> {
|
|
const branch = String(branchCode || '').trim();
|
|
if (branch) {
|
|
const byBranch = await pool.query(
|
|
`SELECT config_value FROM system_configs
|
|
WHERE config_key = $1 AND branch_code = $2 LIMIT 1`,
|
|
[COST_DISPLAY_NAMES_KEY, branch]
|
|
);
|
|
if (byBranch.rowCount) return normalizeNameMap(byBranch.rows[0].config_value);
|
|
}
|
|
const global = await pool.query(
|
|
`SELECT config_value FROM system_configs
|
|
WHERE config_key = $1 AND (branch_code IS NULL OR TRIM(branch_code) = '')
|
|
ORDER BY updated_at DESC NULLS LAST
|
|
LIMIT 1`,
|
|
[COST_DISPLAY_NAMES_KEY]
|
|
);
|
|
if (global.rowCount) return normalizeNameMap(global.rows[0].config_value);
|
|
return { ...DEFAULT_DISPLAY_NAMES };
|
|
}
|
|
|
|
async function saveCostDisplayNames(names: Record<string, string>, branchCode?: string | null) {
|
|
const branch = String(branchCode || '').trim() || null;
|
|
const value = JSON.stringify(names);
|
|
if (branch) {
|
|
const updated = await pool.query(
|
|
`UPDATE system_configs
|
|
SET config_value = $2::jsonb, updated_at = NOW()
|
|
WHERE config_key = $1 AND branch_code = $3`,
|
|
[COST_DISPLAY_NAMES_KEY, value, branch]
|
|
);
|
|
if (updated.rowCount) return;
|
|
await pool.query(
|
|
`INSERT INTO system_configs (config_key, config_value, config_group, description, branch_code)
|
|
VALUES ($1, $2::jsonb, 'finance', '成本结构显示名称覆盖(商品成本/账本类型等)', $3)`,
|
|
[COST_DISPLAY_NAMES_KEY, value, branch]
|
|
);
|
|
return;
|
|
}
|
|
const updated = await pool.query(
|
|
`UPDATE system_configs
|
|
SET config_value = $2::jsonb, updated_at = NOW()
|
|
WHERE config_key = $1 AND (branch_code IS NULL OR TRIM(branch_code) = '')`,
|
|
[COST_DISPLAY_NAMES_KEY, value]
|
|
);
|
|
if (updated.rowCount) return;
|
|
await pool.query(
|
|
`INSERT INTO system_configs (config_key, config_value, config_group, description, branch_code)
|
|
VALUES ($1, $2::jsonb, 'finance', '成本结构显示名称覆盖(商品成本/账本类型等)', NULL)`,
|
|
[COST_DISPLAY_NAMES_KEY, value]
|
|
);
|
|
}
|
|
|
|
export async function setCostDisplayName(
|
|
key: string,
|
|
label: string,
|
|
branchCode?: string | null
|
|
): Promise<Record<string, string>> {
|
|
const nameKey = String(key || '')
|
|
.trim()
|
|
.toLowerCase()
|
|
.slice(0, 64);
|
|
const name = String(label || '').trim().slice(0, 40);
|
|
if (!nameKey) throw new Error('缺少名称键');
|
|
if (!name) throw new Error('请填写显示名称');
|
|
const names = await getCostDisplayNames(branchCode);
|
|
names[nameKey] = name;
|
|
await saveCostDisplayNames(names, branchCode);
|
|
return names;
|
|
}
|
|
|
|
/**
|
|
* 按成本结构 cat_key 改显示名:
|
|
* - product → cost_display_names
|
|
* - fixed:code → fixed_cost_types
|
|
* - free:code → free_cost_types
|
|
* - type:code → cost_display_names[`type:code`]
|
|
*/
|
|
export async function renameCostBucketLabel(
|
|
catKey: string,
|
|
label: string,
|
|
branchCode?: string | null
|
|
): Promise<{ cat_key: string; label: string }> {
|
|
const key = String(catKey || '').trim();
|
|
const name = String(label || '').trim().slice(0, 40);
|
|
if (!key) throw new Error('缺少成本类别');
|
|
if (!name) throw new Error('请填写显示名称');
|
|
|
|
if (key === 'product') {
|
|
await setCostDisplayName('product', name, branchCode);
|
|
return { cat_key: key, label: name };
|
|
}
|
|
if (key.startsWith('fixed:')) {
|
|
const code = key.slice('fixed:'.length).trim().toLowerCase();
|
|
if (!code) throw new Error('缺少固定成本科目编码');
|
|
await renameFixedCostType(code, name, branchCode);
|
|
return { cat_key: key, label: name };
|
|
}
|
|
if (key.startsWith('free:')) {
|
|
const code = key.slice('free:'.length).trim().toLowerCase() || 'misc';
|
|
await renameFreeCostType(code, name, branchCode);
|
|
return { cat_key: key, label: name };
|
|
}
|
|
if (key.startsWith('type:')) {
|
|
const typeKey = `type:${key.slice('type:'.length).trim().toLowerCase()}`;
|
|
await setCostDisplayName(typeKey, name, branchCode);
|
|
return { cat_key: key, label: name };
|
|
}
|
|
await setCostDisplayName(key, name, branchCode);
|
|
return { cat_key: key, label: name };
|
|
}
|
|
|
|
/** 同步解析概览桶显示名(调用方已拉齐配置时使用) */
|
|
export function resolveCostBucketLabelSync(
|
|
catKey: string,
|
|
opts: {
|
|
displayNames: Record<string, string>;
|
|
fixedTypes: Array<{ code: string; label: string }>;
|
|
freeTypes: Array<{ code: string; label: string }>;
|
|
costTypeLabels?: Record<string, string>;
|
|
}
|
|
): string {
|
|
const key = String(catKey || '').trim();
|
|
const display = opts.displayNames || {};
|
|
if (key === 'product') return display.product || DEFAULT_DISPLAY_NAMES.product;
|
|
|
|
if (key.startsWith('fixed:')) {
|
|
const code = key.slice('fixed:'.length);
|
|
if (display[`fixed:${code}`]) return display[`fixed:${code}`];
|
|
return labelForFixedCostType(code, opts.fixedTypes);
|
|
}
|
|
if (key.startsWith('free:')) {
|
|
const code = key.slice('free:'.length) || 'misc';
|
|
const sub = labelForFreeSubtype(code, opts.freeTypes);
|
|
return sub && sub !== 'misc' ? `其他成本 · ${sub}` : '其他成本';
|
|
}
|
|
if (key.startsWith('type:')) {
|
|
const tk = key.slice('type:'.length);
|
|
if (display[`type:${tk}`]) return display[`type:${tk}`];
|
|
if (display[tk]) return display[tk];
|
|
return opts.costTypeLabels?.[tk] || tk;
|
|
}
|
|
return display[key] || key;
|
|
}
|
|
|
|
export async function loadCostLabelContext(branchCode?: string | null) {
|
|
const [displayNames, fixedTypes, freeTypes] = await Promise.all([
|
|
getCostDisplayNames(branchCode),
|
|
listFixedCostTypes(branchCode),
|
|
listFreeCostTypes(branchCode),
|
|
]);
|
|
return { displayNames, fixedTypes, freeTypes };
|
|
}
|