schangxiang@126.com
2025-09-09 3d8966ba2c81e7e0365c8b123e861d18ee4f94f5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//#region import
import { default as BaseController } from "../baseController";
import * as sql from "mssql";
import { Post } from "egg-shell-decorators";
import { SaleOrderPrint } from "../../entity/outbound/manufacture/saleOrderPrint";
import { vSaleOrderPrintListGroup } from "../../entity/outbound/manufacture/vSaleOrderPrintListGroup";
import { SaleOrder } from "../../entity/outbound/sale/saleOrder";
//#endregion
 
/**
 * 闪电发货校验
 */
export default class OrderSendBatchController extends BaseController {
  //#region GetData
  /**
   * 获取波次下的订单数据
   */
  @Post()
  public async getData() {
    let userInfo = await this.userInfo;
    let { ctx } = this;
    let body = ctx.request.body;
 
    let sql = `
        SELECT    L.order_Id, L.orderCode,L.expressCorpName, O.statusText,
                SUM(L.quantityOrder) AS totalQuantityOrder,
                SUM(O.weight) AS weight
                FROM dbo.Sale_OrderPrint M INNER JOIN dbo.Sale_OrderPrintList L ON M.orderPrint_Id=L.orderPrint_Id
        INNER JOIN dbo.Sale_Order O ON O.order_Id = L.order_Id
                WHERE M.orderPrintCode=@0 And M.userProduct_Id=@1
                AND O.statusText IN('波次完成', '拣货中','等待配货', '配货中', '等待打包', '部分打包', '部分发运')
        GROUP BY L.order_Id, L.orderCode, L.expressCorpName, O.statusText;`;
 
    var table = await this.dbRead.query(sql, [body.orderPrintCode, userInfo.userProduct_Id]);
    this.info.data = table;
    this.info.result = true;
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 保存闪电发货
  /**
   * 保存闪电发货
   */
  @Post()
  public async saveScan() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    let orderPrintCode = body.orderPrintCode;
    let dataList = body.dataList;
 
    try {
      for (var model of dataList) {
        let orderCode = model.orderCode;
        //#region 验证
        if (!orderPrintCode) {
          this.info.msg = "波次不能为空!";
          ctx.body = this.info;
          return;
        }
        if (!orderCode) {
          this.info.msg = "快递单号不能为空!";
          ctx.body = this.info;
          return;
        }
        let orderPrint = await this.dbRead.findOne(SaleOrderPrint, {
          orderPrintCode: body.orderPrintCode,
          userProduct_Id: userInfo.userProduct_Id
        });
        if (orderPrint == null) {
          this.info.msg = "波次不存在!";
          ctx.body = this.info;
          return;
        }
        let where = "orderPrint_Id=:orderPrint_Id AND (expressCode=:expressCode OR orderCode=:expressCode)";
        let printList = await this.dbRead
          .createQueryBuilder(vSaleOrderPrintListGroup, "t")
          .where(where, {
            orderPrint_Id: orderPrint.orderPrint_Id,
            expressCode: orderCode
          })
          .getMany();
        if (printList == null) {
          this.info.msg = "该波次下不存在该快递单!";
          ctx.body = this.info;
          return;
        }
        //#endregion
 
        for (let printDetail of printList) {
          if ((printDetail.quantityOuter || 0) - (printDetail.quantityOrder || 0) - (printDetail.freezeQuanity || 0) >= 0) {
            this.info.msg = "该订单已经打包完成!";
            ctx.body = this.info;
            return;
          }
          if ((printDetail.quantityOuter || 0) > 0) {
            this.info.msg = "该订单已经部分打包,不能使用完全出库,请继续使用部分打包!";
            ctx.body = this.info;
            return;
          }
          // let outerModel = await this.dbRead.findOne(SaleOuter, {
          //   expressCode: orderCode
          // });
          // if (outerModel != null) {
          //   this.info.msg = "该快递单号[" + orderCode + "]在[打包校验]已经使用过!";
          //   ctx.body = this.info;
          //   return;
          // }
          //验证耗材是否正确
          let wrapperBarcode = model.wrapperBarcode;
          if (wrapperBarcode) {
            var wrapperBarcodeArray = wrapperBarcode.split("\n").filter(item => item);
            if (wrapperBarcodeArray.Length > 0) {
              let sql = `SELECT TOP 1 v.col FROM  [dbo].[Func_SplitStrToTable]${wrapperBarcodeArray.Join(",")}) AS v
          WHERE NOT EXISTS(
            SELECT * FROM dbo.vBase_ProductInfo_SKU WHERE ProductModel=v.col AND userProduct_Id=${userInfo.userProduct_Id}
          )`;
              let notExistsSku = await this.dbRead.query(sql);
              if (notExistsSku.length) {
                this.info.msg = "包材[" + notExistsSku[0].col + "]在物料信息中不存在,请先维护好基础数据!";
                ctx.body = this.info;
                return;
              }
            }
          }
          let orderInfo = await this.dbRead.findOne(SaleOrder, printDetail.order_Id);
          if (!orderInfo) {
            this.info.msg = "波次中的明细订单在销售订单中已经不存在了,订单ID:" + printDetail.order_Id;
            ctx.body = this.info;
            return;
          }
          let weight = model.weight || orderInfo.weight || body.weight;
 
          //#region 快速发货
          let outerCode = await ctx.service.common.getCodeRegular(268);
 
          const connection: any = await this.dbWrite.connection;
          let request = new sql.Request(connection.driver.master);
          request.input("outerCode", outerCode);
          request.input("order_Id", orderInfo.order_Id);
          request.input("wrapperBarcode", wrapperBarcode);
          request.input("user_Id", userInfo.user_Id);
          request.input("weight", weight);
          request.input("orderPrint_Id", orderPrint.orderPrint_Id);
          request.input("wayBillCode", await ctx.service.common.getCodeRegular(499));
          request.output("outMsg", sql.NVarChar(2000));
          let result = await request.execute("sp_Sale_SendFast");
          let outMsg = result.output.outMsg;
 
          var newOrderInfo = await this.dbRead.findOne(SaleOrder, {
            select: ["statusID"],
            where: {
              order_Id: printDetail.order_Id
            }
          });
          await ctx.service.outbound.orderHelper.setStatusHistory(
            orderInfo.order_Id,
            orderInfo.statusID,
            newOrderInfo.statusID,
            "订单状态",
            "闪电发货"
          );
          if (body.driver) {
            await this.dbWrite.update(SaleOrder, orderInfo.order_Id, {
              driver: body.driver
            });
          }
 
          if (outMsg) {
            this.info.result = false;
            this.info.msg = outMsg;
          } else {
            this.info.result = true;
            this.info.msg = orderPrint.orderPrint_Id + "," + orderInfo.order_Id;
            //扫描成功生成揽收信息
            await ctx.service.tms.wayBillHelper.createWayBillReceive(orderCode, weight);
          }
          //#endregion
        }
      }
      this.info.msg = "发货成功!";
      this.info.result = true;
    } catch (error) {
      this.info.msg = "发货失败:" + error.message;
      this.info.result = false;
    }
 
    ctx.body = this.info;
  }
  //#endregion
}