来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
132 lines
4.3 KiB
TypeScript
132 lines
4.3 KiB
TypeScript
/**
|
|
* 小程序分拣下发称台标签。设计:docs/design/20260920-mp-picking-remote-print.md
|
|
* 未完成单只走 updatePickingItemProgress,禁止调用分拣完成。
|
|
*/
|
|
import { pool } from '../db';
|
|
import { updatePickingItemProgress } from './pickingService';
|
|
import {
|
|
createPrintJobs,
|
|
listMyOnlineWeighDevices,
|
|
listOnlineWeighDevices,
|
|
listWeighDevices,
|
|
normalizeDeviceId,
|
|
type WeighDeviceRow,
|
|
} from './printJobService';
|
|
|
|
function biz(message: string, status = 400, extra?: Record<string, unknown>) {
|
|
return Object.assign(new Error(message), { status, ...extra });
|
|
}
|
|
|
|
export function isNeedDeviceError(
|
|
error: unknown
|
|
): error is Error & { status: number; code: string; devices: WeighDeviceRow[] } {
|
|
return Boolean(error && typeof error === 'object' && (error as { code?: string }).code === 'need_device');
|
|
}
|
|
|
|
export async function resolveMobilePrintDeviceId(input: {
|
|
branchCode: string;
|
|
userId: string | null;
|
|
deviceId?: string | null;
|
|
}): Promise<string> {
|
|
const requested = String(input.deviceId || '').trim();
|
|
if (requested) {
|
|
return normalizeDeviceId(requested);
|
|
}
|
|
const devices = await listWeighDevices(input.branchCode);
|
|
const mine = listMyOnlineWeighDevices(devices, input.userId);
|
|
if (mine.length === 1) return mine[0].device_id;
|
|
if (mine.length > 1) {
|
|
throw biz('请选择秤', 400, { code: 'need_device', devices: mine });
|
|
}
|
|
const online = listOnlineWeighDevices(devices);
|
|
if (online.length === 1) return online[0].device_id;
|
|
if (online.length === 0) {
|
|
throw biz('请先在称上打开分拣工作台');
|
|
}
|
|
throw biz('请选择秤', 400, { code: 'need_device', devices: online });
|
|
}
|
|
|
|
function progressRowsFromItems(
|
|
rows: Array<{
|
|
id: string;
|
|
required_qty: unknown;
|
|
picked_qty: unknown;
|
|
picked_weight: unknown;
|
|
unit: unknown;
|
|
}>
|
|
) {
|
|
return rows
|
|
.map((row) => {
|
|
const required = Number(row.required_qty) || 0;
|
|
const currentQty = Number(row.picked_qty) || 0;
|
|
const currentWeight = Number(row.picked_weight) || 0;
|
|
const pickedQty = Math.max(currentQty, required);
|
|
const pickedWeight = currentWeight > 0 ? currentWeight : required;
|
|
return {
|
|
id: String(row.id),
|
|
required_qty: required,
|
|
picked_qty: pickedQty,
|
|
picked_weight: pickedWeight,
|
|
unit: row.unit != null ? String(row.unit) : '',
|
|
diff_reason: '',
|
|
};
|
|
})
|
|
.filter((row) => (Number(row.picked_weight) || Number(row.picked_qty) || 0) > 0);
|
|
}
|
|
|
|
export async function issueMobilePickingPrintJobs(input: {
|
|
pickCode: string;
|
|
branchCode: string;
|
|
userId: string | null;
|
|
deviceId?: string | null;
|
|
copies?: number;
|
|
pickingItemId?: string | null;
|
|
}) {
|
|
const pickCode = String(input.pickCode || '').trim();
|
|
if (!pickCode) throw biz('缺少拣货单号');
|
|
const deviceId = await resolveMobilePrintDeviceId({
|
|
branchCode: input.branchCode,
|
|
userId: input.userId,
|
|
deviceId: input.deviceId,
|
|
});
|
|
|
|
const task = await pool.query(
|
|
`SELECT pick_code, pick_status, branch_code FROM picking_tasks WHERE pick_code = $1`,
|
|
[pickCode]
|
|
);
|
|
if (!task.rows[0]) throw biz('拣货单不存在', 404);
|
|
if (task.rows[0].branch_code && task.rows[0].branch_code !== input.branchCode) {
|
|
throw biz('拣货单不属于当前分公司', 403);
|
|
}
|
|
|
|
const onlyId = String(input.pickingItemId || '').trim();
|
|
const items = await pool.query(
|
|
onlyId
|
|
? `SELECT id, required_qty, picked_qty, picked_weight, unit
|
|
FROM picking_items WHERE pick_code = $1 AND id = $2::uuid`
|
|
: `SELECT id, required_qty, picked_qty, picked_weight, unit
|
|
FROM picking_items WHERE pick_code = $1`,
|
|
onlyId ? [pickCode, onlyId] : [pickCode]
|
|
);
|
|
const ids = items.rows.map((row) => String(row.id));
|
|
if (!ids.length) throw biz(onlyId ? '拣货行不存在' : '拣货单没有明细');
|
|
|
|
if (Number(task.rows[0].pick_status) < 30) {
|
|
const progressItems = progressRowsFromItems(items.rows);
|
|
if (progressItems.length) {
|
|
await updatePickingItemProgress({ pickCode, rows: progressItems });
|
|
}
|
|
}
|
|
|
|
return createPrintJobs({
|
|
pickCode,
|
|
pickingItemIds: ids,
|
|
deviceId,
|
|
branchCode: input.branchCode,
|
|
userId: input.userId,
|
|
copies: input.copies,
|
|
// 行上「打印」即补打:取消未完成任务后再插一条,见 docs/design/20260922-mp-print-reprint.md
|
|
force: true,
|
|
});
|
|
}
|