333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import * as sql from "mssql";
import { SaleOrderPrint } from "../../entity/outbound/manufacture/saleOrderPrint";
import { Post } from "egg-shell-decorators";
import BaseController from "../baseController";
import { SaleOrder } from "../../entity/outbound/sale/saleOrder";
import { SaleOrderPicking } from "../../entity/outbound/manufacture/saleOrderPicking";
import { SaleOrderMatching } from "../../entity/outbound/manufacture/saleOrderMatching";
 
/**
 * 拣货下架
 */
export default class OrderMatchingController extends BaseController {
  //#region 出库单配货
  /**
        /// 出库单配货
        */
  @Post()
  public async getOrderMatchingData() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    let orderPrintCode = body.orderPrintCode;
    let subOrderPrintCode = orderPrintCode;
    let scanMain = true; // 扫描主波次
 
    // 转为主波次单和
    if (orderPrintCode.indexOf("-") > 0) {
      let arr = orderPrintCode.split("-");
      orderPrintCode = arr[0];
      scanMain = false;
    }
 
    var orderPrintInfo = await this.dbRead.findOne(SaleOrderPrint, {
      orderPrintCode: orderPrintCode,
      userProduct_Id: userInfo.userProduct_Id
    });
    if (orderPrintInfo == null) {
      this.info.result = false;
      this.info.msg = body.orderPrintCode + "波次号不存在!";
      ctx.body = this.info;
      return;
    }
    var reqInfo: any = {};
 
    reqInfo.orderPrintCode = orderPrintCode;
    reqInfo.user_Id = userInfo.user_Id;
    reqInfo.userTrueName = userInfo.userTrueName;
    reqInfo.storage_Id = orderPrintInfo.storage_Id || 0;
    reqInfo.storageName = orderPrintInfo.storageName;
 
    //#region 校验
    if (reqInfo == null) {
      this.info.msg = "请求参数不正确";
      ctx.body = this.info;
      return;
    }
    if (reqInfo.user_Id == 0) {
      this.info.msg = "用户ID都不能为空!";
      ctx.body = this.info;
      return;
    }
    if (!reqInfo.orderPrintCode) {
      this.info.msg = "波次单号不能为空";
      ctx.body = this.info;
      return;
    }
 
    // if (orderPrintInfo.statusText != "等待配货" && orderPrintInfo.statusText != "配货中") {
    //   this.info.result = false;
    //   this.info.msg = "波次单状态为[" + orderPrintInfo.statusText + "],不允许操作";
    //   ctx.body = this.info;
    //   return;
    // }
 
    let dataInfo: any = {};
    dataInfo.orderPrint_Id = orderPrintInfo.orderPrint_Id;
    dataInfo.orderPrintCode = orderPrintInfo.orderPrintCode;
 
    let matchInfo = await this.dbRead.findOne(SaleOrderMatching, {
      orderPrintCode: orderPrintInfo.orderPrintCode,
      userProduct_Id: userInfo.userProduct_Id
    });
    if (matchInfo != null && matchInfo.matchStatus == "配货完成") {
      this.info.result = false;
      this.info.msg = "波次单" + orderPrintInfo.orderPrintCode + "已经配货完成!";
      ctx.body = this.info;
      return;
    }
    //#endregion
 
    try {
      //明细数据
      let sql = `
                SELECT T.*, I.relationCode, I.relationCode2, I.relationCode3, I.relationCode4, I.relationCode5,
                    middleBarcode, middleUnitConvert, bigBarcode, I.unitConvert
                FROM
                (
                    SELECT allotPositionName,orderCode,order_Id, product_Id, productCode,productModel,
                        max(productName) as productName,
                        SUM(quantityOrder) AS quantityOrder, 
                        SUM(isnull(finishedQuanity,0)) as finishedQuanity,MAX(weight) AS weight,MAX(totalWeight) AS totalWeight
                    FROM dbo.Sale_OrderPrintList
                    WHERE orderPrint_Id=@0
          ${orderPrintInfo.subBatch && !scanMain ? ` And SubOrderPrintCode='${subOrderPrintCode}'` : ""}
                    GROUP BY allotPositionName,orderCode,order_Id,product_Id, productCode,productModel 
                    HAVING SUM(quantityOrder) - sum(isnull(finishedQuanity,0))>0
        ) T INNER JOIN dbo.vBase_ProductInfo_SKU I ON T.product_Id=I.product_Id
                ORDER BY allotPositionName
                `;
 
      var matchOrderInfoList = await this.dbRead.query(sql, [orderPrintInfo.orderPrint_Id]);
 
      if (matchOrderInfoList != null && matchOrderInfoList.length > 0) {
        if (matchInfo != null && (matchInfo.matchStatus == "部分配货" || matchInfo.matchStatus == "配货中")) {
          this.info.result = true;
          this.info.msg = "波次单" + orderPrintInfo.orderPrintCode + "正在配货!";
          dataInfo.dataList = matchOrderInfoList;
          this.info.data = dataInfo;
          this.info.dynamic = orderPrintInfo.storage_Id + "," + orderPrintInfo.storageName;
 
          // 更新配货状态
          await this.dbWrite.update(SaleOrderMatching, matchInfo.orderMatching_Id, {
            matchStatus: "配货中"
          });
 
          ctx.body = this.info;
          return;
        }
 
        dataInfo.dataList = matchOrderInfoList;
        this.info.result = true;
        this.info.data = dataInfo;
        this.info.msg = "已成功获取扫描数据!";
        this.info.dynamic = orderPrintInfo.storage_Id + "," + orderPrintInfo.storageName;
      } else {
        this.info.result = false;
        this.info.msg = "没有可以拣货的明细";
      }
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "获取数据时出错:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region  确认配货
  @Post()
  public async saveOrderMatch() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    let orderPrintCode = body.orderPrintCode;
 
    //#region 校验
    if (!orderPrintCode) {
      this.info.msg = "波次不能为空!";
      ctx.body = this.info;
      return;
    }
 
    if (!body.dataList || body.dataList.length == 0) {
      this.info.msg = "请先扫描条码!";
      ctx.body = this.info;
      return;
    }
 
    // 转为主波次单和
    if (orderPrintCode.indexOf("-") > 0) {
      let arr = orderPrintCode.split("-");
      orderPrintCode = arr[0];
    }
 
    let orderPrint = await this.dbRead.findOne(SaleOrderPrint, {
      orderPrintCode: orderPrintCode
    });
    if (!orderPrint) {
      this.info.msg = "波次不存在!";
      ctx.body = this.info;
      return;
    }
 
    let orderPicking = await this.dbRead.findOne(SaleOrderPicking, {
      orderPrintCode: orderPrintCode,
      userProduct_Id: userInfo.userProduct_Id
    });
    if (orderPicking == null) {
      this.info.msg = "拣货记录不存在!";
      ctx.body = this.info;
      return;
    }
 
    //#endregion
 
    //#region 存储过程
    let data = new sql.Table();
    data.columns.add("storage_Id", sql.Int);
    data.columns.add("storageName", sql.NVarChar(50));
    data.columns.add("product_Id", sql.BigInt);
    data.columns.add("productCode", sql.NVarChar(50));
    data.columns.add("quantityOrder", sql.Int);
    data.columns.add("finishedQuantity", sql.Int);
    data.columns.add("allotPositionName", sql.NVarChar(50));
    data.columns.add("orderCode", sql.NVarChar(50));
    data.columns.add("weight", sql.Decimal(14, 2));
    data.columns.add("totalWeight", sql.Decimal(14, 2));
 
    for (var _detail of body.dataList) {
      data.rows.add(
        orderPicking.storage_Id,
        orderPicking.storageName,
        _detail.product_Id,
        _detail.productCode,
        _detail.quantityOrder,
        _detail.finishedQuantity,
        _detail.allotPositionName,
        _detail.orderCode,
        _detail.weight,
        _detail.totalWeight
      );
    }
    const connection: any = await this.dbWrite.connection;
    let request = new sql.Request(connection.driver.master);
    request.input("orderPrintCode", orderPrintCode);
    request.input("user_Id", userInfo.user_Id);
    request.input("menu_Id", 250);
    request.input("excelDT", data);
    request.output("outMsg", sql.NVarChar(2000));
    let result = await request.execute("sp_Sale_Order_Matching");
    let outMsg = result.output.outMsg;
 
    if (outMsg && outMsg != "部分配货" && outMsg != "配货完成") {
      this.info.result = false;
      this.info.msg = outMsg;
      ctx.body = this.info;
      return;
    } else {
      this.info.result = true;
      this.info.msg = outMsg;
      if (!outMsg) {
        //#region 更新状态和插入记录
        var orderList = await this.dbRead.find(SaleOrder, {
          orderPrintCode: body.orderPrintCode
        });
        for (var orderInfo of orderList) {
          ctx.service.outbound.orderHelper.setStatusHistory(orderInfo.order_Id, 6, 7, "订单状态", "订单配货");
        }
        //#endregion
      }
    }
    //#endregion
 
    ctx.body = this.info;
    return;
  }
 
  //#endregion
}