liufumingren 9c2b254206 1
Co-authored-by: Copilot <copilot@github.com>
2026-05-11 08:44:27 +08:00

222 lines
6.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const MainService = require('../main');
class PickService extends MainService {
/**
* 初始化 pick 模块鉴权配置。
*
* @param {Egg.Context} ctx 上下文对象。
*/
constructor(ctx) {
super(ctx);
const _noNeedLogin = '';
const _noNeedRight = 'query,wavePickTaskDetail';
const _noNeedLog = 'query';
const _noNeedAuth = 'query,wavePickTaskDetail';
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
}
/**
* 将入参标准化为布尔值。
*
* 兼容 `true/false`、`1/0`、`yes/no`、`on/off` 以及大小写字符串。
*
* @param {*} value 原始值。
* @param {boolean} defaultValue 默认值。
* @return {boolean} 解析后的布尔值。
*/
normalizeBoolean(value, defaultValue = false) {
if (value === undefined || value === null || value === '') {
return defaultValue;
}
if (typeof value === 'boolean') {
return value;
}
const normalized = String(value).trim().toLowerCase();
if ([ '1', 'true', 'yes', 'y', 'on' ].includes(normalized)) {
return true;
}
if ([ '0', 'false', 'no', 'n', 'off' ].includes(normalized)) {
return false;
}
return defaultValue;
}
/**
* 按仓库上下文获取订单分表模型。
*
* @param {number} warehouseId 仓库ID。
* @return {Promise<Object>} 订单模型。
*/
async getOrderModelByWarehouse(warehouseId) {
const { ctx } = this;
const factory = ctx.model && ctx.model.OrderDeliveryFactory;
if (!factory || typeof factory.getCurrentModel !== 'function') {
throw new Error('订单分表模型工厂不可用');
}
const model = await factory.getCurrentModel(warehouseId);
if (!model || typeof model.findOne !== 'function') {
throw new Error('订单分表模型不可用');
}
return model;
}
/**
* 根据快递号查询订单所在波次的拣货汇总信息。
*
* 入参:
* - 快递号:`waybill_code`
* - 完整显示开关:`full` / `full_display` / `is_full`
*
* 仓库上下文仅取当前登录用户 `ctx.user.store_id`(兼容 `ctx.state.user.store_id`),
* 不依赖外部传入 `warehouse_id`,用于避免跨仓越界访问。
*
* @param {Object} data 请求参数。
* @return {Promise<Object>} 统一 success/fail 包装结果。
*/
async query(data = {}) {
const { service, ctx } = this;
const requestData = this.isPlainObject(data) ? data : {};
const {
waybill_code,
full,
full_display,
is_full,
} = requestData;
const normalizedWaybillCode = String(waybill_code || '').trim();
if (!normalizedWaybillCode) {
return this.fail('请传入快递号', {}, 400);
}
const userWarehouseId = ctx && ctx.user ? ctx.user.store_id : undefined;
const stateWarehouseId = ctx && ctx.state && ctx.state.user ? ctx.state.user.store_id : undefined;
const normalizedWarehouseId = Number(userWarehouseId || stateWarehouseId || 0);
if (!Number.isFinite(normalizedWarehouseId) || normalizedWarehouseId <= 0) {
return this.fail('缺少当前用户仓库上下文 store_id', {}, 400);
}
const orderExpress = await ctx.model.OrderExpress.findOne({
where: {
warehouse_id: normalizedWarehouseId,
waybill_code: normalizedWaybillCode,
},
attributes: [ 'order_id' ],
raw: true,
});
const orderId = Number(orderExpress && orderExpress.order_id || 0);
if (!orderExpress || orderId <= 0) {
return this.fail('快递号不存在或不属于当前仓库', {}, 404);
}
let orderModel = null;
try {
orderModel = await this.getOrderModelByWarehouse(normalizedWarehouseId);
} catch (error) {
return this.fail(error && error.message ? error.message : '订单模型不可用', {}, 500);
}
const order = await orderModel.findOne({
where: {
id: orderId,
warehouse_id: normalizedWarehouseId,
},
attributes: [ 'id', 'store_id', 'wave_no', 'wave_id' ],
raw: true,
});
if (!order) {
return this.fail('订单不存在', {}, 404);
}
const waveNo = String(order.wave_no || '').trim();
if (!waveNo) {
return this.fail('订单未分配波次号,暂无拣货信息', {}, 400);
}
const fullDisplay = this.normalizeBoolean(full ?? full_display ?? is_full, false);
try {
const result = await service.systems.pick.queryPickingInfo({
orderId,
waveNo,
waveId: Number(order.wave_id || 0),
storeId: Number(order.store_id || 0),
warehouseId: normalizedWarehouseId,
fullDisplay,
});
return this.success('ok', {
data: result,
code: 200,
});
} catch (error) {
const code = Number(error && error.code);
if (Number.isFinite(code) && code >= 400 && code < 600) {
return this.fail(error.message || '查询失败', {}, code);
}
ctx.logger.error('[PickService.query] 查询波次拣货信息失败: %s', error && error.message ? error.message : error);
return this.fail('查询失败', {}, 500);
}
}
/**
* 查询拣货任务详情。
*
* 仅允许任务创建人查看任务详情,包含主任务、波次、订单与框信息。
*
* @param {Object} data 请求参数。
* @param {number|string} data.task_id 拣货任务ID。
* @return {Promise<Object>} 统一 success/fail 包装结果。
*/
async wavePickTaskDetail(data = {}) {
const { service, ctx } = this;
const requestData = this.isPlainObject(data) ? data : {};
const { task_id } = requestData;
const taskId = Number(task_id);
if (!Number.isInteger(taskId) || taskId <= 0) {
return this.fail('task_id 必填且必须为正整数', {}, 400);
}
const stateUser = this.isPlainObject(ctx && ctx.state && ctx.state.user) ? ctx.state.user : null;
const ctxUser = this.isPlainObject(ctx && ctx.user) ? ctx.user : null;
const operatorSource = stateUser || ctxUser || {};
const rawOperatorId = operatorSource.employees_id ?? operatorSource.operator_id ?? operatorSource.id;
const operatorId = Number(rawOperatorId || 0);
if (!Number.isInteger(operatorId) || operatorId <= 0) {
return this.fail('未获取到操作人上下文', {}, 401);
}
try {
const result = await service.systems.pick.wavePickTaskDetail({
taskId,
operatorId,
});
return this.success('ok', {
data: result,
code: 200,
});
} catch (error) {
const code = Number(error && error.code);
if (Number.isInteger(code) && code >= 400 && code < 600) {
return this.fail(error.message || '查询失败', {}, code);
}
ctx.logger.error('[PickService.wavePickTaskDetail] 查询拣货任务详情失败: %s', error && error.message ? error.message : error);
return this.fail('查询失败', {}, 500);
}
}
}
module.exports = PickService;