257 lines
7.3 KiB
JavaScript
257 lines
7.3 KiB
JavaScript
'use strict';
|
|
|
|
const OrderBaseService = require('./base');
|
|
|
|
class RecheckOrderService extends OrderBaseService {
|
|
/**
|
|
* 初始化复核订单系统方法免登录、免权限、免日志与免鉴权配置。
|
|
* @param {Egg.Context} ctx 请求上下文。
|
|
*/
|
|
constructor(ctx) {
|
|
super(ctx);
|
|
const _noNeedLogin = '';
|
|
const _noNeedRight = 'scanWaybill,confirmRecheck';
|
|
const _noNeedLog = 'scanWaybill,confirmRecheck';
|
|
const _noNeedAuth = 'scanWaybill,confirmRecheck';
|
|
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
|
|
}
|
|
|
|
/**
|
|
* 扫描运单号并返回订单头与商品明细。
|
|
* @param {Object} data 请求体。
|
|
* @param {string} [data.waybill_no] 运单号。
|
|
* @param {string} [data.waybillCode] 运单号别名。
|
|
* @param {string} [data.waybill_code] 运单号别名。
|
|
* @return {Promise<Object>} 扫描结果。
|
|
*/
|
|
async scanWaybill(data = {}) {
|
|
const warehouseId = this.getWarehouseId();
|
|
const waybillNo = this.resolveWaybillNo(data);
|
|
const orderId = await this.pickOrderIdByWaybill(waybillNo);
|
|
const orderModel = await this.getOrderModelByTableType('current', warehouseId);
|
|
const orderWhere = this.buildOrderWhere([ orderId ], warehouseId);
|
|
const order = await orderModel.findOne({
|
|
where: orderWhere,
|
|
raw: true,
|
|
});
|
|
|
|
if (!order) {
|
|
throw new Error('未找到对应订单');
|
|
}
|
|
|
|
if (String(order.print_status || '') !== 'fin') {
|
|
throw new Error('运单状态必须已打印');
|
|
}
|
|
|
|
const detailResult = await this.detail({
|
|
id: orderId,
|
|
});
|
|
|
|
return {
|
|
order_id: orderId,
|
|
waybill_no: waybillNo,
|
|
order: detailResult.detail || {},
|
|
items: Array.isArray(detailResult.items) ? detailResult.items : [],
|
|
expresses: Array.isArray(detailResult.expresses) ? detailResult.expresses : [],
|
|
logs: Array.isArray(detailResult.logs) ? detailResult.logs : [],
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 确认订单已复核,并写入复核任务记录。
|
|
* @param {Object} data 请求体。
|
|
* @param {number|string} [data.order_id] 订单ID。
|
|
* @param {number|string} [data.id] 订单ID别名。
|
|
* @param {number|string} [data.orderId] 订单ID别名。
|
|
* @return {Promise<Object>} 复核确认结果。
|
|
*/
|
|
async confirmRecheck(data = {}) {
|
|
const { ctx } = this;
|
|
const warehouseId = this.getWarehouseId();
|
|
const orderId = this.resolveOrderId(data);
|
|
const orderModel = await this.getOrderModelByTableType('current', warehouseId);
|
|
const orderWhere = this.buildOrderWhere([ orderId ], warehouseId);
|
|
const order = await orderModel.findOne({
|
|
where: orderWhere,
|
|
raw: true,
|
|
});
|
|
|
|
if (!order) {
|
|
throw new Error('未找到对应订单');
|
|
}
|
|
|
|
if (String(order.print_status || '') !== 'fin') {
|
|
throw new Error('运单状态必须已打印');
|
|
}
|
|
|
|
const expresses = await this.getOrderExpressesByOrderId(orderId);
|
|
const waybillNo = String(expresses?.[0]?.waybill_code || '').trim();
|
|
|
|
const now = new Date();
|
|
const updateValues = {
|
|
recheck_status: 'fin',
|
|
recheck_time: now,
|
|
updated_at: now,
|
|
};
|
|
const updateResult = await orderModel.update(updateValues, {
|
|
where: orderWhere,
|
|
});
|
|
const affectedRows = Array.isArray(updateResult) ? Number(updateResult[0] || 0) : Number(updateResult || 0);
|
|
|
|
if (affectedRows <= 0) {
|
|
throw new Error('未找到对应订单');
|
|
}
|
|
|
|
const operator = this.getOrderLogOperator();
|
|
const reviewNo = this.buildRecheckReviewNo(orderId, now);
|
|
const orderNo = String(order.delivery_order_code || order.pre_delivery_order_code || '').trim();
|
|
const operatorNo = String(operator.id || '').trim();
|
|
const operatorName = String(operator.name || '').trim();
|
|
|
|
await ctx.model.query(
|
|
`
|
|
INSERT INTO wms_recheck_task (
|
|
review_no,
|
|
warehouse_id,
|
|
order_id,
|
|
order_no,
|
|
waybill_no,
|
|
task_status,
|
|
scan_status,
|
|
review_emp_no,
|
|
review_emp_name,
|
|
scan_emp_no,
|
|
scan_emp_name,
|
|
scan_time,
|
|
finish_time,
|
|
remark
|
|
)
|
|
VALUES (
|
|
:reviewNo,
|
|
:warehouseId,
|
|
:orderId,
|
|
:orderNo,
|
|
:waybillNo,
|
|
:taskStatus,
|
|
:scanStatus,
|
|
:reviewEmpNo,
|
|
:reviewEmpName,
|
|
:scanEmpNo,
|
|
:scanEmpName,
|
|
:scanTime,
|
|
:finishTime,
|
|
:remark
|
|
)
|
|
`,
|
|
{
|
|
replacements: {
|
|
reviewNo,
|
|
warehouseId,
|
|
orderId,
|
|
orderNo,
|
|
waybillNo,
|
|
taskStatus: 2,
|
|
scanStatus: 1,
|
|
reviewEmpNo: operatorNo,
|
|
reviewEmpName: operatorName,
|
|
scanEmpNo: operatorNo,
|
|
scanEmpName: operatorName,
|
|
scanTime: now,
|
|
finishTime: now,
|
|
remark: '系统复核完成',
|
|
},
|
|
type: ctx.model.QueryTypes.INSERT,
|
|
}
|
|
);
|
|
|
|
return {
|
|
order_id: orderId,
|
|
waybill_no: waybillNo,
|
|
recheck_status: 'fin',
|
|
recheck_time: now,
|
|
review_no: reviewNo,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 从多种兼容字段中解析订单ID。
|
|
* @param {Object} data 请求体。
|
|
* @return {number} 规范化后的订单ID。
|
|
*/
|
|
resolveOrderId(data = {}) {
|
|
const requestData = this.isPlainObject(data) ? data : {};
|
|
const {
|
|
order_id: orderId,
|
|
id,
|
|
orderId: orderIdAlias,
|
|
} = requestData;
|
|
const normalizedOrderId = Number(orderId || id || orderIdAlias || 0);
|
|
|
|
if (!Number.isFinite(normalizedOrderId) || normalizedOrderId <= 0) {
|
|
throw new Error('订单ID不能为空');
|
|
}
|
|
|
|
return normalizedOrderId;
|
|
}
|
|
|
|
/**
|
|
* 从多种兼容字段中解析运单号。
|
|
* @param {Object} data 请求体。
|
|
* @return {string} 规范化后的运单号。
|
|
*/
|
|
resolveWaybillNo(data = {}) {
|
|
const requestData = this.isPlainObject(data) ? data : {};
|
|
const {
|
|
waybill_no: waybillNo,
|
|
waybillCode,
|
|
waybill_code: waybillCodeAlias,
|
|
} = requestData;
|
|
const normalizedWaybillNo = String(waybillNo || waybillCode || waybillCodeAlias || '').trim();
|
|
|
|
if (!normalizedWaybillNo) {
|
|
throw new Error('运单号不能为空');
|
|
}
|
|
|
|
return normalizedWaybillNo;
|
|
}
|
|
|
|
/**
|
|
* 根据运单号解析唯一订单ID。
|
|
* @param {string} waybillNo 运单号。
|
|
* @return {Promise<number>} 订单ID。
|
|
*/
|
|
async pickOrderIdByWaybill(waybillNo) {
|
|
const warehouseId = this.getWarehouseId();
|
|
const orderIds = await this.findOrderIdsByExpressNumbers([ waybillNo ], warehouseId);
|
|
|
|
if (!Array.isArray(orderIds) || orderIds.length === 0) {
|
|
throw new Error('未找到对应订单');
|
|
}
|
|
|
|
const normalizedOrderIds = orderIds
|
|
.map(item => Number(item))
|
|
.filter(orderId => Number.isFinite(orderId) && orderId > 0)
|
|
.sort((a, b) => a - b);
|
|
|
|
if (normalizedOrderIds.length === 0) {
|
|
throw new Error('未找到对应订单');
|
|
}
|
|
|
|
return normalizedOrderIds[0];
|
|
}
|
|
|
|
/**
|
|
* 生成复核单号。
|
|
* @param {number} orderId 订单ID。
|
|
* @param {Date} now 当前时间。
|
|
* @return {string} 复核单号。
|
|
*/
|
|
buildRecheckReviewNo(orderId, now = new Date()) {
|
|
const timestamp = Number(now.getTime());
|
|
const paddedOrderId = String(orderId).padStart(6, '0');
|
|
|
|
return `RC${timestamp}${paddedOrderId}`;
|
|
}
|
|
}
|
|
|
|
module.exports = RecheckOrderService; |