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
//#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";
import { SaleSendList } from "../../entity/outbound/sale/saleSendList";
import { SaleOuter } from "../../entity/outbound/sale/saleOuter";
//#endregion
 
/**
 * 收货 - 上架
 */
export default class OrderSendFastController extends BaseController {
  //#region  部分发运
  @Post()
  public async partialSaveScan() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
 
    let batchCode = body.batchCode;
    let expressCode = body.expressCode;
    let wrapperBarcode = body.wrapperBarcode;
    let newExpressCode = body.newExpressCode;
    let weight = body.weight || 0;
    var _expressCode = expressCode || newExpressCode;
 
    //#region 校验
    if (!batchCode) {
      this.info.msg = "波次不能为空!";
      ctx.body = this.info;
      return;
    }
    if (expressCode.IsNullOrEmpty()) {
      this.info.msg = "快递单号不能为空!";
      ctx.body = this.info;
      return;
    }
    var wrapperBarcodeArray = wrapperBarcode.split("\n").filters(item => item);
 
    if (body.dataList.length == 0) {
      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) {
      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: expressCode
      })
      .getOne();
    if (printList == null) {
      this.info.msg = "该波次下不存在该快递单!";
      ctx.body = this.info;
    }
    if ((printList.quantityOuter || 0) - (printList.quantityOrder || 0) - (printList.freezeQuanity || 0) >= 0) {
      this.info.msg = "该订单已经打包完成!";
      ctx.body = this.info;
      return;
    }
    let outerModel = await this.dbRead.findOne(SaleOuter, {
      expressCode: expressCode
    });
    if (outerModel) {
      this.info.msg = "该快递单号[" + _expressCode + "]在[打包校验]已经使用过!";
      ctx.body = this.info;
      return;
    }
    //验证耗材是否正确
    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 order = await this.dbRead.findOne(SaleOrder, printList.order_Id);
    if (order == null) {
      this.info.msg = "波次中的明细订单在销售订单中已经不存在了,订单ID:" + printList.order_Id;
      ctx.body = this.info;
      return;
    }
 
    let sendListModel = await this.dbRead.findOne(SaleSendList, {
      expressCode: expressCode
    });
    if (sendListModel != null) {
      this.info.msg = _expressCode + "快递单号已存在,不允许重复使用!";
      ctx.body = this.info;
    }
    //判断出库的物料数量是否大于可以出库的数量
    let isExistsSql = "";
    for (var model of body.dataList) {
      var scanCount = model.scanCount;
      if (isExistsSql) {
        isExistsSql += "  UNION  ";
      }
      isExistsSql += ` SELECT productCode,(quantityOrder-ISNULL(quantityOuter,0)) AS enableQty,
        ${scanCount} AS currentQty FROM dbo.Sale_OrderPrintList WHERE orderPrint_Id=${orderPrint.orderPrint_Id} 
        AND orderList_Id=" + orderList_Id + @" AND (quantityOrder-ISNULL(quantityOuter,0)-" + scanCount + ")<0 `;
    }
    isExistsSql = `SELECT productCode,enableQty,currentQty FROM (${isExistsSql}) AS v`;
    let table = await this.dbRead.query(isExistsSql);
    if (table && table.length > 0) {
      let row = table[0];
      this.info.msg = `物料[${row["productCode"]}]的可出库数量[${row["enableQty"]}]小于当前的出库数量[${row["currentQty"]}]!`;
      ctx.body = this.info;
    }
    //#endregion
 
    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", order.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.output("outMsg", sql.NVarChar(2000));
    let result = await request.execute("sp_Sale_SendFast");
    let outMsg = result.output.outMsg;
 
    ctx.service.outbound.orderHelper.setStatusHistory(order.order_Id, order.statusID, 12, "订单状态", "闪电发货");
    this.info.result = true;
    if (outMsg) {
      this.info.result = false;
      this.info.msg = outMsg;
    }
 
    ctx.body = this.info;
  }
  //#endregion
}