'use strict'; const MainService = require('../main'); class orderServer extends MainService { constructor(ctx) { super(ctx); // 初始化公共变量 this.orderType = { JYCK: { method: 'ORDER', name: '一般交易出库单', }, HHCK: { method: 'ORDER', name: '换货出库', }, BFCK: { method: 'ORDER', name: '补发出库', }, PTCK: { method: 'CK', name: '普通出库单', }, DBCK: { method: 'CK', name: '调拨出库', }, B2BCK: { method: 'CK', name: 'B2B出库', }, QTCK: { method: 'CK', name: '其他出库', }, CGTH: { method: 'CK', name: '采购退货出库单', }, B2BRK: { method: 'RK', name: 'B2B入库', }, SCRK: { method: 'RK', name: '生产入库', }, LYRK: { method: 'RK', name: '领用入库', }, CCRK: { method: 'RK', name: '残次品入库', }, CGRK: { method: 'RK', name: '采购入库', }, DBRK: { method: 'RK', name: '调拨入库', }, QTRK: { method: 'RK', name: '其他入库', }, XTRK: { method: 'BACKORDER', name: '销退入库', }, THRK: { method: 'BACKORDER', name: '退货入库', }, HHRK: { method: 'BACKORDER', name: '换货入库', }, CNJG: { method: 'JG', name: '仓内加工单', }, }; } async getInterceptExpressInfo(orderId) { const { ctx } = this; const normalizedOrderId = Number(orderId || 0); if (!Number.isFinite(normalizedOrderId) || normalizedOrderId <= 0) { return []; } const orderExpressModel = ctx.model && ctx.model.OrderExpress; if (!orderExpressModel || typeof orderExpressModel.findAll !== 'function') { return []; } const rows = await orderExpressModel.findAll({ where: { order_id: normalizedOrderId, }, attributes: [ 'waybill_code', 'logistics_code', 'logistics_name', 'logistics_interface_id' ], order: [[ 'id', 'DESC' ]], raw: true, }); return (Array.isArray(rows) ? rows : []).map(row => ({ waybill_code: String(row && row.waybill_code || '').trim(), logistics_code: String(row && row.logistics_code || '').trim(), logistics_name: String(row && row.logistics_name || '').trim(), logistics_interface_id: row && row.logistics_interface_id == null ? null : Number(row.logistics_interface_id), })); } async sendCancelInterceptNotice({ orderCode, existingOrder, info }) { const { ctx } = this; try { const express = await this.getInterceptExpressInfo(existingOrder && existingOrder.id); await ctx.service.systems.notice.send({ event: 'order_cancel_intercepted', businessName: 'order', warehouseId: info.warehouseId, storeId: info.storeId, subject: { id: info.storeId, type: 'store', }, businessId: orderCode, messageType: 'text', title: `订单取消触发拦截:${orderCode}`, content: `订单 ${orderCode} 因拣货/打印状态未完成,已标记为拦截执行。`, sender: { id: ctx.user?.employees_id || ctx.user?.id, name: ctx.user?.employees_name || '', }, metadata: { orderCode, orderId: existingOrder.id, pickStatus: String(existingOrder.pick_status || 'none').trim(), printStatus: String(existingOrder.print_status || 'none').trim(), interceptStatus: String(existingOrder.intercept_status || 'none').trim(), express, }, }); } catch (error) { ctx.logger.warn(`取消订单拦截通知发送失败: ${error.message}`); } } async cancel(data) { const { ctx } = this; const info = this.getInfo(); const orderType = this.orderType[data.request.orderType]; switch (orderType.method) { case 'ORDER': { const orderCode = String(data?.request?.orderCode || '').trim(); if (!orderCode) { return this.fail('订单号不能为空'); } const DeliveryModel = await ctx.model.OrderDeliveryFactory.getCurrentModel(info.warehouseId); const transaction = await ctx.model.transaction(); try { const existingOrder = await DeliveryModel.findOne({ where: { delivery_order_code: orderCode, warehouse_id: info.warehouseId, store_id: info.storeId, }, transaction, lock: transaction.LOCK.UPDATE, }); if (!existingOrder) { await transaction.rollback(); return this.fail('未找到对应的订单'); } const pickStatus = String(existingOrder.pick_status || 'none').trim(); const printStatus = String(existingOrder.print_status || 'none').trim(); if (pickStatus !== 'none' || printStatus !== 'none') { await existingOrder.update({ intercept_status: 'intercepting', updated_at: new Date(), }, { transaction, }); await transaction.commit(); await this.sendCancelInterceptNotice({ orderCode, existingOrder, info, }); return this.fail('取消失败,已通知拦截执行'); } await existingOrder.update({ status: 'cancel', outbound_status: 'fin', step: 4, updated_at: new Date(), }, { transaction, }); await transaction.commit(); return this.success('取消订单成功'); } catch (error) { try { await transaction.rollback(); } catch (rollbackError) { ctx.logger.error('取消订单回滚失败', rollbackError); } ctx.logger.error(`取消订单失败: ${error.message}`); return this.fail(error.message || '取消订单失败'); } } case 'RK': { const existingOrder = await ctx.model.EntryOrder.findOne({ where: { entry_order_code: data.request.orderCode, warehouse_id: info.warehouseId, store_id: info.storeId, }, }); if (existingOrder) { await existingOrder.update({ order_status: 'CANCELLED', updated_at: new Date(), }); return this.success('取消入库单成功'); } return this.fail('未找到对应的入库单'); } case 'CK': return this.fail('取消出库单未开发完成'); case 'JG': return this.fail('取消加工单未开发完成'); case 'BACKORDER': return this.fail('取消退货单未开发完成'); default: return this.fail(`未识别的作业类型${orderType.method}`, {}); } } async pending(data) { const orderType = this.orderType[data.request.orderType]; switch (orderType.method) { case 'ORDER': return this.fail('锁定订单未开发完成'); case 'RK': return this.fail('锁定入库单未开发完成'); case 'CK': return this.fail('锁定出库单未开发完成'); case 'JG': return this.fail('锁定加工单未开发完成'); case 'BACKORDER': return this.fail('锁定退货单未开发完成'); default: return this.fail(`未识别的作业类型${orderType.method}`, {}); } } } module.exports = orderServer;