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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//#region import
import { default as BaseController } from "../baseController";
import { Post } from "egg-shell-decorators";
import { vSaleOrderPrintListGroup } from "../../entity/outbound/manufacture/vSaleOrderPrintListGroup";
import { In, Raw } from "typeorm";
import { SaleOrderPrint } from "../../entity/outbound/manufacture/saleOrderPrint";
import { SaleOrder } from "../../entity/outbound/sale/saleOrder";
import { BaseProductPlaceHolder } from "../../entity/storage/storage/baseProductPlaceHolder";
import { SaleOrderPrintList } from "../../entity/outbound/manufacture/saleOrderPrintList";
import moment = require("moment");
import { SaleOrderList } from "../../entity/outbound/sale/saleOrderList";
import { vSaleOrderPrintList } from "../../entity/outbound/manufacture/vSaleOrderPrintList";
import { vSaleOrderPickToLight } from "../../entity/outbound/manufacture/vSaleOrderPickToLight";
import { IDXPickToLight } from "../../entity/outbound/manufacture/iDXPickToLight";
import { SaleOrderPrintSub } from "../../entity/outbound/manufacture/saleOrderPrintSub";
//#endregion
 
/**
 * 订单打印
 */
export default class OrderPrintController extends BaseController {
  //#region 根据订单号、快递单号、波次单号、波次单ID获得出库单ID集合
  /**
   * 根据订单号、快递单号、波次单号、波次单ID获得出库单ID集合
   */
  @Post()
  public async getOrderIds() {
    let { ctx } = this;
    let body = ctx.request.body;
    let type = body.type;
    let userInfo = await this.userInfo;
 
    try {
      if (type === "order" || type === "express" || type === "orderprint") {
        let where = {};
        if (type === "order") {
          where = {
            userProduct_Id: userInfo.userProduct_Id,
            orderCode: In(body.code)
          };
        } else if (type === "orderprint") {
          let codes = "'" + body.code.join("','") + "'";
          where = {
            userProduct_Id: userInfo.userProduct_Id,
            order_Id: Raw(() => {
              return ` order_Id In(SELECT Order_Id FROM dbo.Sale_OrderPrint M INNER JOIN dbo.Sale_OrderPrintList L ON L.OrderPrint_Id = M.OrderPrint_Id 
              WHERE M.OrderPrintCode In(${codes}))`;
            })
          };
        } else {
          where = {
            userProduct_Id: userInfo.userProduct_Id,
            expressCode: In(body.code)
          };
        }
        let orderPrintDetails = await this.dbRead.find(SaleOrder, {
          select: ["order_Id"],
          where: where
        });
 
        let idArr = orderPrintDetails
          .reduce((all: Array<any>, next) => (all.some(item => item["order_Id"] == next["order_Id"]) ? all : [...all, next]), [])
          .map(item => item.order_Id);
 
        if (idArr.length) {
          this.info.result = true;
          this.info.data = idArr;
        } else {
          this.info.result = false;
          this.info.msg = "未获得订单信息";
        }
 
        ctx.body = this.info;
      } else {
        let orderPrintDetails = await this.dbRead.find(vSaleOrderPrintListGroup, {
          orderPrint_Id: In(body.ids)
        });
 
        let idArr = orderPrintDetails
          .reduce((all: Array<any>, next) => (all.some(item => item["order_Id"] == next["order_Id"]) ? all : [...all, next]), [])
          .map(item => item.order_Id);
 
        if (idArr.length) {
          this.info.result = true;
          this.info.data = idArr;
        } else {
          this.info.result = false;
          this.info.msg = "未获得订单信息";
        }
        ctx.body = this.info;
      }
    } catch (error) {
      this.info.result = false;
      this.info.msg = "未获得订单信息";
      ctx.body = this.info;
    }
  }
  //#endregion
 
  //#region AddOrder 添加订单
  @Post()
  public async addOrder() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
 
    let orderPrint_Id = body.orderPrint_Id;
    let orderCode = body.orderCode;
    let orderPrintInfo = await this.dbRead.findOne(SaleOrderPrint, orderPrint_Id);
 
    let orderInfo = await this.dbRead.findOne(SaleOrder, {
      orderCode: orderCode,
      userProduct_Id: userInfo.userProduct_Id
    });
    if (orderInfo == null) {
      this.info.result = false;
      this.info.msg = orderCode + "订单号不存在!";
      ctx.body = this.info;
      return;
    }
    if (orderInfo.statusText != "审核成功") {
      this.info.result = false;
      this.info.msg = orderCode + "订单未审核成功,不允许添加!";
      ctx.body = this.info;
      return;
    }
    if (orderInfo.sortingStatus != 2) {
      this.info.result = false;
      this.info.msg = orderCode + "订单为分配成功 ,不允许添加!";
      ctx.body = this.info;
      return;
    }
    if (orderInfo.orderPrintCode) {
      this.info.result = false;
      this.info.msg = orderCode + "订单已经在" + orderInfo.orderPrintCode + "波次单下面,不允许添加!";
      ctx.body = this.info;
      return;
    }
 
    let holderList = await this.dbRead.find(BaseProductPlaceHolder, {
      code: orderCode,
      userProduct_Id: userInfo.userProduct_Id
    });
    let existsPrintDetail = await this.dbRead.findOne(SaleOrderPrintList, {
      where: { orderPrint_Id: orderPrint_Id },
      order: { allotPositionName: "DESC" }
    });
    let allotPositionName = existsPrintDetail.allotPositionName;
    let allot = parseInt(allotPositionName) + 1;
    allotPositionName = "" + (allot.toString().length == 1 ? "0" + allot : allot);
 
    for (var item of holderList) {
      let detailInfo = new SaleOrderPrintList();
      detailInfo.allotPositionName = allotPositionName;
      detailInfo.createDate = new Date();
      detailInfo.createID = userInfo.user_Id;
      detailInfo.creator = userInfo.userTrueName;
 
      detailInfo.finishedQuanity = 0;
      detailInfo.freezeQuanity = 0;
      detailInfo.order_Id = item.mainID;
      detailInfo.orderCode = item.code;
      detailInfo.orderList_Id = item.detailID;
      detailInfo.orderPrint_Id = orderPrint_Id;
 
      detailInfo.pickQuantity = 0;
      detailInfo.placeholder_Id = item.placeholder_Id;
      detailInfo.plateCode = item.plateCode;
      detailInfo.expressCorp_Id = orderInfo.expressCorp_Id;
      detailInfo.expressCorpName = orderInfo.expressCorpName;
      detailInfo.expressCode = orderInfo.expressCode;
 
      detailInfo.positionName = item.positionName;
      detailInfo.produceDate = item.produceDate ? moment(item.produceDate).format("YYYY-MM-DD") : null;
      detailInfo.product_Id = item.product_Id;
      detailInfo.productCode = item.productCode;
      detailInfo.productModel = item.productModel;
      detailInfo.productName = item.productName;
      detailInfo.productSpec = item.productSpec;
 
      let orderDetail = await this.dbRead.findOne(SaleOrderList, item.detailID);
      detailInfo.weight = orderDetail.weight;
      detailInfo.totalWeight = orderDetail.totalWeight;
      detailInfo.quantityOrder = orderDetail.quantityOrder;
      detailInfo.quantityOrderOrign = orderDetail.quantityOrder;
      detailInfo.quantityOuter = 0;
      detailInfo.storeOrderCode = item.storeOrderCode;
      await this.dbWrite.save(detailInfo);
    }
 
    //更新订单波次号
    await this.dbWrite.update(SaleOrder, orderInfo.order_Id, {
      orderPrint_Id: orderPrintInfo.orderPrint_Id,
      orderPrintCode: orderPrintInfo.orderPrintCode,
      userProduct_Id: userInfo.userProduct_Id
    });
 
    let sql = `SET XACT_ABORT on;Begin Tran
      Update Sale_Order Set StatusID=4, statusText='波次完成' Where orderCode='${orderCode}' and userProduct_Id=${userInfo.userProduct_Id};
      Update Sale_OrderList set batchQuantity=quantityOrder WHERE Order_Id='${orderInfo.order_Id}'
      Update Sale_OrderPrint set 
        TotalWeight=(SELECT SUM(totalWeight) FROM dbo.Sale_OrderPrintList WHERE orderPrint_Id=Sale_OrderPrint.orderPrint_Id AND ISNULL(Sale_OrderPrintList.QuantityOrder,0)>0),
                OrderCount=(SELECT COUNT(DISTINCT order_Id) FROM dbo.Sale_OrderPrintList WHERE orderPrint_Id=Sale_OrderPrint.orderPrint_Id AND ISNULL(Sale_OrderPrintList.QuantityOrder,0)>0),
                UnFinishedCount=(SELECT COUNT(DISTINCT order_Id) FROM dbo.Sale_OrderPrintList WHERE orderPrint_Id=Sale_OrderPrint.orderPrint_Id AND ISNULL(Sale_OrderPrintList.QuantityOrder,0)>0),
                TotalQuanity=(SELECT SUM(quantityOrder) FROM dbo.Sale_OrderPrintList WHERE orderPrint_Id=Sale_OrderPrint.orderPrint_Id AND ISNULL(Sale_OrderPrintList.QuantityOrder,0)>0)
        Where orderPrint_Id=${orderPrint_Id} and userProduct_Id=${userInfo.userProduct_Id}
      If @@Error <> 0 RollBack Tran Else Commit Tran`;
 
    await this.dbWrite.query(sql);
 
    this.info.result = true;
    this.info.msg = "添加成功!";
 
    ctx.body = this.info;
  }
  //#endregion
 
  //#region OnFrozenOrder 剔除订单
  @Post()
  public async onFrozenOrder() {
    let { ctx } = this;
    let body = ctx.request.body;
 
    try {
      let orderPrint_Id = body.orderPrint_Id;
      let orderCode = body.orderCode;
      let orderInfo = await this.dbRead.find(SaleOrderPrintList, {
        orderPrint_Id: orderPrint_Id,
        orderCode: orderCode
      });
      if (orderInfo == null || orderInfo.length == 0) {
        this.info.result = false;
        this.info.msg = orderCode + "订单号不存在当前波次单中!";
        ctx.body = this.info;
        return this.info;
      }
      if (orderInfo.filter(a => a.quantityOuter > 0).length) {
        this.info.result = false;
        this.info.msg = orderCode + "订单号已经存在出库明细,不允许剔除!";
        ctx.body = this.info;
        return this.info;
      }
 
      let sql = `SET XACT_ABORT on;Begin Tran
      delete from Sale_OrderPrintList Where orderCode='${orderCode}' And orderPrint_Id=${orderPrint_Id};
      Update Sale_OrderPrint set 
                OrderCount=(SELECT COUNT(DISTINCT order_Id) FROM dbo.Sale_OrderPrintList WHERE orderPrint_Id=Sale_OrderPrint.orderPrint_Id),
                UnFinishedCount=(SELECT COUNT(DISTINCT order_Id) FROM dbo.Sale_OrderPrintList WHERE orderPrint_Id=Sale_OrderPrint.orderPrint_Id),
                TotalQuanity=(SELECT SUM(quantityOrder) FROM dbo.Sale_OrderPrintList WHERE orderPrint_Id=Sale_OrderPrint.orderPrint_Id)
        Where orderPrint_Id=${orderPrint_Id}
      Update Sale_OrderPrint set 
                FreezeQuantity=
                (
                    Select Sum(freezeQuanity) from Sale_OrderPrintList Where orderPrint_Id=${orderPrint_Id}
                ),
                UnFinishedCount = 
                (
                    SELECT COUNT(DISTINCT order_Id)
                    FROM     Sale_OrderPrintList L
                    WHERE    orderPrint_Id = ${orderPrint_Id} AND L.quantityOrder>L.freezeQuanity
        ) Where orderPrint_Id=${orderPrint_Id};
 
            --更新波次数量
            UPDATE dbo.Sale_OrderList SET BatchQuantity=
        ISNULL((
          SELECT SUM(L.quantityOrder) FROM dbo.Sale_OrderPrintList L
          WHERE orderList_Id=Sale_OrderList.orderList_Id
        ), 0)
        Where order_Id IN (Select order_Id from Sale_Order Where orderPrint_Id=${orderPrint_Id});
            --更新状态
      Update Sale_Order Set StatusID=2, statusText='审核成功', orderPrint_Id=NULL, orderPrintCode=NULL Where orderCode='${orderCode}';
      If @@Error <> 0 RollBack Tran Else Commit Tran`;
 
      await this.dbWrite.query(sql);
 
      this.info.result = true;
      this.info.msg = "剔除订单成功!";
    } catch (error) {
      this.info.result = false;
      this.info.msg = "剔除失败:" + error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region printPickBill2 打印拣配单
  @Post()
  public async printPickBill2() {
    let userInfo = await this.userInfo;
    let { ctx } = this;
    let body = ctx.request.body;
 
    try {
      let dataList = [];
      var orderPrints = await this.dbRead.find(SaleOrderPrint, {
        orderPrint_Id: In(body.ids)
      });
 
      for (let printInfo of orderPrints) {
        if (printInfo.subBatch) {
          // 存在子波次
          let subs = await this.dbRead.find(SaleOrderPrintSub, {
            orderPrint_Id: printInfo.orderPrint_Id
          });
          let groupList = subs.reduce(
            (all: Array<SaleOrderPrintSub>, next) => (all.some(item => item.areaCode == next.areaCode) ? all : [...all, next]),
            []
          );
          for (let subInfo of groupList) {
            let ids = subs.filter(item => item.areaCode === subInfo.areaCode).map(item => item.orderPrintList_Id);
            var orderPrintDetails = await this.dbRead.find(vSaleOrderPrintList, {
              where: {
                orderPrintList_Id: In(ids),
                positionName: Raw(() => {
                  return `positionName NOT IN(SELECT positionName FROM base_position Where userProduct_Id=${userInfo.userProduct_Id} And positionType=5 And storage_Id=${printInfo.storage_Id})`;
                })
              },
              order: { positionName: "ASC", productModel: "ASC" }
            });
 
            // 子波次号
            printInfo.orderPrintCode = subInfo.subOrderPrintCode;
            printInfo["areaCode"] = subInfo.areaCode;
 
            let dataInfo = {
              printInfo: this.ctx.helper.copyObject(printInfo),
              details: orderPrintDetails
            };
            dataList.push(dataInfo);
          }
        } else {
          // 常规波次
          var orderPrintDetails = await this.dbRead.find(vSaleOrderPrintList, {
            where: {
              orderPrint_Id: printInfo.orderPrint_Id,
              positionName: Raw(() => {
                return `positionName NOT IN(SELECT positionName FROM base_position Where userProduct_Id=${userInfo.userProduct_Id} And positionType=5 And storage_Id=${printInfo.storage_Id})`;
              })
            },
            order: { positionName: "ASC", productModel: "ASC" }
          });
 
          let dataInfo = {
            printInfo: printInfo,
            details: orderPrintDetails
          };
          dataList.push(dataInfo);
        }
      }
 
      this.info.result = true;
      this.info.data = dataList;
 
      //#region 记录打印次数
      await this.dbWrite.increment(
        SaleOrderPrint,
        {
          orderPrint_Id: In(body.ids)
        },
        "pickingPrint",
        1
      );
      //#endregion
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "加载数据失败:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 更新打印次数
  @Post()
  public async updatePrintCount() {
    try {
      await this.dbWrite.increment(
        SaleOrderPrint,
        {
          orderPrint_Id: In(this.body.ids)
        },
        this.body.type,
        1
      );
      this.info.result = true;
      this.info.msg = "打印次数更新成功";
    } catch (error) {
      this.info.result = false;
      this.info.msg = "打印次数更新失败," + error.message;
    }
 
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region LightPicking灯光拣选
  @Post()
  public async lightPicking() {
    let userInfo = await this.userInfo;
    try {
      if (!this.body.orderPrintCode) {
        this.info.result = false;
        this.info.msg = "波次号不能为空";
        this.ctx.body = this.info;
        return;
      }
      let orderPrintInfo = await this.dbRead.findOne(SaleOrderPrint, {
        orderPrintCode: this.body.orderPrintCode,
        userProduct_Id: (await this.userInfo).userProduct_Id
      });
      if (!orderPrintInfo) {
        this.info.result = false;
        this.info.msg = "波次号不存在";
        this.ctx.body = this.info;
        return;
      }
      if (["波次完成", "拣货中"].indexOf(orderPrintInfo.statusText) < 0) {
        this.info.result = false;
        this.info.msg = "只有状态为“波次完成、拣货中”才允许转入灯光接口数据中";
        this.ctx.body = this.info;
        return;
      }
      let plList = await this.dbRead.find(vSaleOrderPickToLight, {
        where: {
          orderPrintCode: this.body.orderPrintCode,
          userProduct_Id: userInfo.userProduct_Id
        }
      });
      if (plList.length == 0) {
        this.info.result = false;
        this.info.msg = "没有对应波次单!";
        this.ctx.body = this.info;
        return;
      }
 
      for (var item of plList) {
        let holderInfo = await this.dbRead.findOne(IDXPickToLight, {
          placeholder_Id: item.placeholder_Id,
          orderPrint_Id: item.orderPrint_Id
        });
        if (holderInfo) {
          this.info.result = false;
          this.info.msg = "已推送,不可重复推送";
          this.ctx.body = this.info;
          return;
        }
 
        let pl = new IDXPickToLight();
        // pl.createDate = item.createDate;
        // pl.fMLocation = item.fMLocation;
        pl.isUpload = 0;
        pl.orderCode = item.orderCode;
        pl.orderList_Id = item.orderList_Id;
        pl.orderPrintCode = item.orderPrintCode;
        pl.orderPrintList_Id = item.orderPrintList_Id;
        pl.orderPrint_Id = item.orderPrint_Id;
        pl.order_Id = item.order_Id;
        pl.placeholder_Id = item.placeholder_Id;
 
        pl.positionName = item.positionName;
        pl.productCode = item.productCode;
        pl.productModel = item.productModel;
        pl.productName = item.productName;
        pl.productSpec = item.productSpec;
        pl.product_Id = item.product_Id;
        pl.quantityOrder = item.quantityOrder;
        pl.orignHolderStorage = 0 + item.orignHolderStorage;
 
        pl.smallUnit = item.smallUnit;
        pl.bigUnit = item.bigUnit;
 
        pl.taskProcess = item.taskProcess;
        pl.taskType = item.taskType;
        pl.userProduct_Id = item.userProduct_Id;
        pl.storage_Id = item.storage_Id;
        pl.storageName = item.storageName;
 
        await this.dbWrite.save(pl);
      }
      // 更新备注
      await this.dbWrite.update(SaleOrderPrint, orderPrintInfo.orderPrint_Id, {
        remark: "已推送到灯光系统"
      });
      this.info.result = true;
      this.info.msg = "灯光拣选成功";
    } catch (ex) {
      this.info.msg = ex.message;
      this.info.result = false;
    }
    this.ctx.body = this.info;
  }
  //#endregion
}