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
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
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 { SaleOrderPrintList } from "../../entity/outbound/manufacture/saleOrderPrintList";
import { SaleOrderPicking } from "../../entity/outbound/manufacture/saleOrderPicking";
 
/**
 * 拣货下架
 */
export default class OrderPickingController extends BaseController {
  //#region GetData
  /**
   * 回车获取数据
   */
  @Post()
  public async getData() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    let batchCode = body.batchCode;
 
    try {
      // let where = "";
 
      if (!batchCode) {
        this.info.msg = "波次不能为空!";
        ctx.body = this.info;
        return;
      }
      let orderPrintCode = batchCode;
      if (batchCode.indexOf("-") > 0) {
        let arr = batchCode.split("-");
        orderPrintCode = arr[0];
      }
      let orderPrint = await this.dbRead.findOne(SaleOrderPrint, {
        orderPrintCode: orderPrintCode,
        userProduct_Id: userInfo.userProduct_Id
      });
      if (orderPrint == null) {
        this.info.msg = "波次不存在!";
        ctx.body = this.info;
        return;
      }
 
      let model: any = {};
      model.orderPrint_Id = orderPrint.orderPrint_Id;
      model.orderPrintCode = orderPrint.orderPrintCode;
 
      if (["待拣货", "拣货中", "部分拣货", "波次完成", "等待打包"].indexOf(orderPrint.statusText) < 0) {
        this.info.msg = `波次单必须是"待拣货", "拣货中", "部分拣货", "波次完成", "等待打包"才允许下架操作,当前状态是[${orderPrint.statusText}]`;
        ctx.body = this.info;
        return;
      }
      let query = `
                SELECT  l.productModel ,
                                l.productCode ,
                                MAX(l.productName) AS productName ,
                                SUM(l.QuantityOrderOrign) AS quantityOrder ,
                                SUM(l.pickQuantity) as pickQuantity,
                                SUM(placeholderStorage) AS placeholderStorage,
                                MAX(v.salePrice) AS salePrice ,
                                MAX(v.Remark) AS Remark ,
                                0 AS orderList_Id ,
                                0 AS orderPrintList_Id ,
                                l.plateCode ,
                                l.positionName,
                                l.productSpec,
                                MAX(v.SmallUnit) AS smallUnit,
                                v.relationCode, v.relationCode2, v.relationCode3, v.relationCode4, v.relationCode5,
                                v.middleBarcode, v.middleUnitConvert, v.bigBarcode, v.unitConvert,MAX(l.weight)AS weight,MAX(l.totalWeight) AS totalWeight,
                max(l.order_Id) as order_Id
                FROM    dbo.Sale_OrderPrint m
                                INNER JOIN dbo.Sale_OrderPrintList l ON m.orderPrint_Id = l.orderPrint_Id
                                INNER JOIN vBase_ProductInfo_SKU v ON l.Product_Id = v.Product_Id
                                INNER JOIN dbo.Base_ProductPlaceHolder h ON l.Placeholder_Id = h.Placeholder_Id
        WHERE   m.orderPrint_Id =@0 AND PositionType IS NULL
        ${orderPrint.subBatch ? ` And l.SubOrderPrintCode='${batchCode}'` : ""}
                GROUP BY 
                                l.productModel ,
                                l.productCode ,
                                l.plateCode ,
                                l.positionName,
                l.productSpec,
                                v.relationCode, v.relationCode2, v.relationCode3, v.relationCode4, v.relationCode5,
                                v.middleBarcode, v.middleUnitConvert, v.bigBarcode, v.unitConvert
                ORDER BY l.positionName ,
                                l.productModel;
                `;
 
      let dataList = await this.dbRead.query(query, [orderPrint.orderPrint_Id]);
      if (dataList.length == 0) {
        this.info.msg = "未找到对应的订单明细数据";
      } else {
        dataList = dataList.filter(o => o.quantityOrder > o.pickQuantity);
        this.info.result = true;
        model.dataList = dataList;
        model.isSupportPlate = false;
        //创建拣货记录
        await ctx.service.outbound.outScanService.pickingStart(orderPrintCode, orderPrint, dataList);
 
        model.offPositionList = await ctx.service.outbound.outScanService.getOffPosition(orderPrint.storage_Id);
        this.info.data = model;
      }
    } catch (error) {
      this.info.result = false;
      this.info.msg = "错误:" + error.message;
    }
 
    ctx.body = this.info;
  }
  //#endregion
 
  //#region  保存扫描结果
  /**
   * 拣货完成
   */
  @Post()
  public async saveScan() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
 
    try {
      let batchCode = body.batchCode; //波次号
      let cartCode = body.cartCode; //车号
      let dataList = body.dataList;
      let orderPrintCode = batchCode;
 
      //#region 校验
      if (!batchCode) {
        this.info.msg = "波次不能为空!";
        ctx.body = this.info;
        return;
      }
 
      if (!dataList || dataList.length == 0) {
        this.info.msg = "请先扫描条码!";
        ctx.body = this.info;
        return;
      }
 
      if (batchCode.indexOf("-") > 0) {
        let arr = batchCode.split("-");
        orderPrintCode = arr[0];
      }
      let orderPrint: SaleOrderPrint = await this.dbRead.findOne(SaleOrderPrint, {
        orderPrintCode: orderPrintCode
      });
 
      let printListModel = await this.dbRead.find(SaleOrderPrintList, {
        orderPrint_Id: orderPrint.orderPrint_Id
      });
 
      if (orderPrint == null) {
        this.info.msg = "波次不存在!";
        ctx.body = this.info;
        return;
      }
      let orderPicking = await this.dbRead.find(SaleOrderPicking, {
        orderPrintCode: orderPrintCode,
        user_Id: userInfo.user_Id
      });
      if (orderPicking == null) {
        this.info.msg = "拣货记录不存在!";
        ctx.body = this.info;
        return;
      }
 
      //#endregion
 
      //#region 存储过程
      let data = new sql.Table();
      data.columns.add("productModel", sql.NVarChar(50));
      data.columns.add("productCode", sql.NVarChar(50));
      data.columns.add("positionName", sql.NVarChar(50));
      data.columns.add("offPosition", sql.NVarChar(50));
      data.columns.add("plateCode", sql.NVarChar(50));
      data.columns.add("scanCount", sql.BigInt);
      data.columns.add("weight", sql.Decimal(14, 4));
      data.columns.add("totalWeight", sql.Decimal(14, 4));
      for (var _detail of dataList) {
        data.rows.add(
          _detail.productModel,
          _detail.productCode,
          _detail.positionName,
          _detail.offPosition,
          _detail.plateCode,
          _detail.scanCount,
          _detail.weight,
          _detail.totalWeight
        );
      }
      const connection: any = await this.dbWrite.connection;
      let request = new sql.Request(connection.driver.master);
      request.input("batchCode", orderPrintCode);
      request.input("cartCode", cartCode);
      request.input("user_Id", userInfo.user_Id);
      request.input("userProduct_Id", userInfo.userProduct_Id);
      request.input("data", data);
      request.output("msg", sql.NVarChar(2000));
      let result = await request.execute("sp_ScanPickingSave");
      let msg = result.output.msg;
 
      if (msg && msg != "部分拣货") {
        this.info.result = false;
        this.info.msg = msg;
        ctx.body = this.info;
        return;
      } else {
        this.info.result = true;
        this.info.msg = "拣货完成";
      }
      //#endregion
 
      if (msg == "部分拣货") {
        this.info.result = true;
        this.info.state = "部分下架";
        this.info.msg = "提交成功,已部分下架成功!";
      } else {
        // 对单据进行分组
        let groupData = printListModel.reduce(
          (all: Array<any>, next) => (all.some(item => item["order_Id"] == next["order_Id"]) ? all : [...all, next]),
          []
        );
 
        for (var g of groupData) {
          ctx.service.outbound.orderHelper.setStatusHistory(g.order_Id, 5, 6, "订单状态", "订单拣货");
        }
      }
 
      ctx.body = this.info;
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
}