来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
141 lines
4.8 KiB
TypeScript
141 lines
4.8 KiB
TypeScript
import { pool } from '../db';
|
||
|
||
export type WeighMatchToleranceMode = 'absolute' | 'percent';
|
||
|
||
export interface WeighStationToleranceConfig {
|
||
weigh_match_tolerance_mode: WeighMatchToleranceMode;
|
||
/** absolute: 与商品数量同单位(多为斤);percent: 如 10 表示 10% */
|
||
weigh_match_tolerance_value: number;
|
||
}
|
||
|
||
const CONFIG_KEY = 'picking_weigh_station_tolerance';
|
||
const DEFAULT_CONFIG: WeighStationToleranceConfig = {
|
||
weigh_match_tolerance_mode: 'percent',
|
||
weigh_match_tolerance_value: 10,
|
||
};
|
||
|
||
function normalizeMode(raw: unknown): WeighMatchToleranceMode {
|
||
return String(raw || '').trim() === 'absolute' ? 'absolute' : 'percent';
|
||
}
|
||
|
||
function normalizeValue(mode: WeighMatchToleranceMode, raw: unknown): number {
|
||
const n = Number(raw);
|
||
if (!Number.isFinite(n) || n < 0) {
|
||
return mode === 'percent' ? DEFAULT_CONFIG.weigh_match_tolerance_value : 0.05;
|
||
}
|
||
if (mode === 'percent') {
|
||
return Math.min(100, Math.round(n * 1000) / 1000);
|
||
}
|
||
return Math.round(n * 10000) / 10000;
|
||
}
|
||
|
||
function parseConfigValue(raw: unknown): WeighStationToleranceConfig {
|
||
if (raw == null || typeof raw !== 'object') return { ...DEFAULT_CONFIG };
|
||
const obj = raw as Record<string, unknown>;
|
||
const mode = normalizeMode(obj.weigh_match_tolerance_mode ?? obj.mode);
|
||
const value = normalizeValue(
|
||
mode,
|
||
obj.weigh_match_tolerance_value ?? obj.value ?? obj.tolerance
|
||
);
|
||
return { weigh_match_tolerance_mode: mode, weigh_match_tolerance_value: value };
|
||
}
|
||
|
||
export async function getWeighStationToleranceConfig(
|
||
branchCode?: string | null
|
||
): Promise<WeighStationToleranceConfig> {
|
||
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 setWeighStationToleranceConfig(
|
||
branchCode: string,
|
||
input: { mode?: string | null; value?: number | null }
|
||
): Promise<WeighStationToleranceConfig> {
|
||
const branch = String(branchCode || '').trim();
|
||
if (!branch) {
|
||
throw Object.assign(new Error('缺少分公司编码'), { status: 400 });
|
||
}
|
||
const mode = normalizeMode(input.mode);
|
||
const value = normalizeValue(mode, input.value);
|
||
const next: WeighStationToleranceConfig = {
|
||
weigh_match_tolerance_mode: mode,
|
||
weigh_match_tolerance_value: value,
|
||
};
|
||
const payload = JSON.stringify(next);
|
||
const desc =
|
||
'分拣台称重匹配阈值:mode=absolute|percent;absolute 与商品单位同口径(多为斤);percent 为允许误差百分比;value≤0 关闭校验;>0 超阈值禁止提交';
|
||
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, CONFIG_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, 'picking', $4, $3)`,
|
||
[CONFIG_KEY, payload, branch, desc]
|
||
);
|
||
}
|
||
return next;
|
||
}
|
||
|
||
/** 校验秤重相对应拣量是否在阈值内;返回 allowed 与说明。value<=0 表示关闭校验。 */
|
||
export function assertWeighWithinTolerance(input: {
|
||
requiredQty: number;
|
||
actualQty: number;
|
||
config: WeighStationToleranceConfig;
|
||
}): { ok: true; diff: number; allowed: number } | never {
|
||
const required = Number(input.requiredQty) || 0;
|
||
const actual = Number(input.actualQty) || 0;
|
||
const diff = Math.abs(actual - required);
|
||
const mode = input.config.weigh_match_tolerance_mode;
|
||
const value = Number(input.config.weigh_match_tolerance_value);
|
||
if (!Number.isFinite(value) || value <= 0) {
|
||
return { ok: true, diff, allowed: Number.POSITIVE_INFINITY };
|
||
}
|
||
let allowed = 0;
|
||
if (mode === 'absolute') {
|
||
allowed = value;
|
||
} else {
|
||
allowed = Math.abs(required) * (value / 100);
|
||
}
|
||
if (diff > allowed + 1e-9) {
|
||
const tip =
|
||
mode === 'absolute'
|
||
? `误差 ${diff.toFixed(3)} 超过允许 ${allowed.toFixed(3)}(绝对阈值)`
|
||
: `误差 ${diff.toFixed(3)} 超过允许 ${allowed.toFixed(3)}(${value}%)`;
|
||
throw Object.assign(new Error(tip), {
|
||
status: 400,
|
||
code: 'weigh_tolerance_exceeded',
|
||
diff,
|
||
allowed,
|
||
mode,
|
||
value,
|
||
required_qty: required,
|
||
actual_qty: actual,
|
||
});
|
||
}
|
||
return { ok: true, diff, allowed };
|
||
}
|