来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
125 lines
4.0 KiB
TypeScript
125 lines
4.0 KiB
TypeScript
import { pool } from '../db';
|
|
|
|
export type WeighStationLabelPrintKind = 'inbound' | 'picking';
|
|
|
|
export interface WeighStationLabelPrintConfig {
|
|
enabled: boolean;
|
|
}
|
|
|
|
const KEYS: Record<WeighStationLabelPrintKind, string> = {
|
|
inbound: 'weigh_station_inbound_label_print',
|
|
picking: 'weigh_station_picking_label_print',
|
|
};
|
|
|
|
const DEFAULTS: Record<WeighStationLabelPrintKind, WeighStationLabelPrintConfig> = {
|
|
inbound: { enabled: false },
|
|
picking: { enabled: true },
|
|
};
|
|
|
|
const GROUPS: Record<WeighStationLabelPrintKind, string> = {
|
|
inbound: 'inspection',
|
|
picking: 'picking',
|
|
};
|
|
|
|
const DESCRIPTIONS: Record<WeighStationLabelPrintKind, string> = {
|
|
inbound:
|
|
'安卓入库台 USB 标签打印:enabled=true 时验收入库与独立库存入库成功后自动打标;false 关闭自动打印并禁用重打',
|
|
picking:
|
|
'安卓分拣台 USB 标签打印:enabled=true 时装框成功后自动打标;false 关闭自动打印并禁用重打',
|
|
};
|
|
|
|
export function parseLabelPrintConfigValue(
|
|
raw: unknown,
|
|
kind: WeighStationLabelPrintKind
|
|
): WeighStationLabelPrintConfig {
|
|
const fallback = DEFAULTS[kind];
|
|
if (raw == null) return { ...fallback };
|
|
if (typeof raw === 'boolean') return { enabled: raw };
|
|
if (typeof raw === 'object') {
|
|
const obj = raw as Record<string, unknown>;
|
|
if (typeof obj.enabled === 'boolean') return { enabled: obj.enabled };
|
|
}
|
|
return { ...fallback };
|
|
}
|
|
|
|
export function defaultLabelPrintConfig(kind: WeighStationLabelPrintKind): WeighStationLabelPrintConfig {
|
|
return { ...DEFAULTS[kind] };
|
|
}
|
|
|
|
export async function getWeighStationLabelPrintConfig(
|
|
kind: WeighStationLabelPrintKind,
|
|
branchCode?: string | null
|
|
): Promise<WeighStationLabelPrintConfig> {
|
|
const key = KEYS[kind];
|
|
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`,
|
|
[key, code]
|
|
);
|
|
if (branchRow.rows[0]?.config_value != null) {
|
|
return parseLabelPrintConfigValue(branchRow.rows[0].config_value, kind);
|
|
}
|
|
}
|
|
const globalRow = await pool.query(
|
|
`SELECT config_value FROM system_configs
|
|
WHERE config_key = $1 AND branch_code IS NULL
|
|
LIMIT 1`,
|
|
[key]
|
|
);
|
|
if (globalRow.rows[0]?.config_value != null) {
|
|
return parseLabelPrintConfigValue(globalRow.rows[0].config_value, kind);
|
|
}
|
|
return defaultLabelPrintConfig(kind);
|
|
}
|
|
|
|
export async function setWeighStationLabelPrintConfig(
|
|
kind: WeighStationLabelPrintKind,
|
|
branchCode: string,
|
|
enabled: boolean
|
|
): Promise<WeighStationLabelPrintConfig> {
|
|
const branch = String(branchCode || '').trim();
|
|
if (!branch) {
|
|
throw Object.assign(new Error('缺少分公司编码'), { status: 400 });
|
|
}
|
|
if (typeof enabled !== 'boolean') {
|
|
throw Object.assign(new Error('请传入 enabled: true/false'), { status: 400 });
|
|
}
|
|
const next: WeighStationLabelPrintConfig = { enabled };
|
|
const payload = JSON.stringify(next);
|
|
const key = KEYS[kind];
|
|
const desc = DESCRIPTIONS[kind];
|
|
const group = GROUPS[kind];
|
|
const updated = await pool.query(
|
|
`UPDATE system_configs
|
|
SET config_value = $1::jsonb, updated_at = NOW(), description = $4
|
|
WHERE config_key = $2 AND branch_code = $3`,
|
|
[payload, key, branch, desc]
|
|
);
|
|
if (!updated.rowCount) {
|
|
await pool.query(
|
|
`INSERT INTO system_configs (config_key, config_value, config_group, description, branch_code)
|
|
VALUES ($1, $2::jsonb, $4, $5, $3)`,
|
|
[key, payload, branch, group, desc]
|
|
);
|
|
}
|
|
return next;
|
|
}
|
|
|
|
/** 合并进 mobile pending/config 的打印开关字段 */
|
|
export async function resolveWeighStationLabelPrintFlags(branchCode?: string | null): Promise<{
|
|
inbound_label_print_enabled: boolean;
|
|
picking_label_print_enabled: boolean;
|
|
}> {
|
|
const [inbound, picking] = await Promise.all([
|
|
getWeighStationLabelPrintConfig('inbound', branchCode),
|
|
getWeighStationLabelPrintConfig('picking', branchCode),
|
|
]);
|
|
return {
|
|
inbound_label_print_enabled: inbound.enabled === true,
|
|
picking_label_print_enabled: picking.enabled === true,
|
|
};
|
|
}
|