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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
import BaseController from "../baseController";
import { Post } from "egg-shell-decorators";
import { SaleOrder } from "../../entity/outbound/sale/saleOrder";
import { In } from "typeorm";
import { BaseExpressCorp } from "../../entity/basicInfo/base/baseExpressCorp";
import { BasePrintTemplate } from "../../entity/sys/print/basePrintTemplate";
import { TemplateSendBill } from "../../entity/outbound/template/templateSendBill";
import { TemplateSendBillColumnInfo } from "../../entity/outbound/template/templateSendBillColumnInfo";
import { TMSWayBill } from "../../entity/express/tms/tmsWayBill";
import { TMSWayBillList } from "../../entity/express/tms/tmsWayBillList";
import { ExpressYTOrder } from "../../entity/express/yuantong/expressYTOrder";
import { ExpressSFOrder } from "../../entity/express/sf/expressSFOrder";
import { TMSWayBillOther } from "../../entity/express/tms/tmsWayBillOther";
import { TMSWayBillOtherList } from "../../entity/express/tms/tmsWayBillOtherList";
import { vSaleOuterCase } from "../../entity/outbound/stat/vSaleOuterCase";
import { vSaleOuterCaseList } from "../../entity/outbound/stat/vSaleOuterCaseList";
import { BaseProductPlaceHolder } from "../../entity/storage/storage/baseProductPlaceHolder";
import { vSaleOrderList } from "../../entity/outbound/stat/vSaleOrderList";
import { PurchaseOrder } from '../../entity/inbound/purchase/purchaseOrder';
import { vPurchaseOrderList } from '../../entity/inbound/stat/vPurchaseOrderList';
import moment = require("moment");
 
/**
 * 打印
 */
export default class PrintController extends BaseController {
  //#region orderList
  /// <summary>
  /// 出库单打印
  /// </summary>
  @Post()
  public async orderList() {
    try {
      let userInfo = await this.userInfo;
      let ids = this.body.ids;
      let type = this.body.type;
      if (!Array.isArray(ids)) {
        ids = ids.split(",");
      }
      let cases: Array<vSaleOuterCase> = [];
      // case为装箱单打印
      if (type == "case") {
        cases = await this.dbRead.find(vSaleOuterCase, {
          caseNumber: In(ids)
        });
        ids = cases.map(item => item.order_Id);
      }
      // 获得出库单打印模板
      var orderList = await this.dbRead.find(SaleOrder, {
        order_Id: In(ids)
      });
      if (!orderList.length) {
        this.info.data = [];
        this.info.result = true;
        return;
      }
      // 快递公司
      let expressList = await this.dbRead.find(BaseExpressCorp, {
        expressCorp_Id: In(orderList.map(item => item.expressCorp_Id))
      });
      // 打印模板
      let templateList = [];
      if (this.body.type == "detail") {
        templateList = await this.dbRead.find(BasePrintTemplate, {
          printTemplate_Id: In(expressList.map(item => item.orderDetailTemplateID))
        });
      } else {
        templateList = await this.dbRead.find(BasePrintTemplate, {
          printTemplate_Id: In(expressList.map(item => item.faceBillTemplateID))
        });
      }
      // 波次次配货位
      let sql = `SELECT DISTINCT allotPositionName, orderCode, 
        (SELECT P.OrderCount FROM dbo.Sale_OrderPrint P WHERE P.OrderPrint_Id=L.OrderPrint_Id) AS orderCount FROM Sale_OrderPrintList L Where order_Id IN(${ids.join(
        ","
      )})`;
      let allotPositions = await this.dbRead.query(sql);
      var dataList = [];
      if (this.body.type == "case") {
        // 装箱打印
        for (let caseInfo of cases) {
          let orderInfo = orderList.find(item => item.order_Id == caseInfo.order_Id);
          orderInfo.expressCode = caseInfo.extendField10; // 当前装箱单对应的快递单号
 
          // 获取配货位
          let orderMacthInfo = allotPositions.find(item => item.orderCode === orderInfo.orderCode);
          if (orderMacthInfo) {
            orderInfo["配货位"] = orderMacthInfo.allotPositionName;
            orderInfo["orderCount"] = orderMacthInfo.orderCount;
          }
          let expressCorpInfo = expressList.find(item => item.expressCorp_Id === orderInfo.expressCorp_Id);
          // 获得打印模板
          let printTemplate_Id = expressCorpInfo.faceBillTemplateID || 0;
          let templateInfo = templateList.find(item => item.printTemplate_Id == printTemplate_Id);
 
          let detailList = await this.dbRead.find(vSaleOuterCaseList, {
            userProduct_Id: caseInfo.userProduct_Id,
            caseNumber: caseInfo.caseNumber
          });
 
          dataList.push({
            mainInfo: Object.assign({}, orderInfo),
            detailList: detailList,
            vueData: templateInfo != null ? templateInfo.vueData : null
          });
        }
      } else {
        // 订单明细
        let orderDetails = await this.dbRead.find(vSaleOrderList, {
          order_Id: In(ids)
        });
        for (let detail of orderDetails) {
          // 处理主表扩展字段
          if (detail["expandFields"]) {
            let expandFields = JSON.parse(detail["expandFields"]);
            Object.assign(detail, expandFields);
          }
          // 处理明细表扩展字段
          if (detail["detailExpandFields"]) {
            let expandFields = JSON.parse(detail["detailExpandFields"]);
            Object.assign(detail, expandFields);
          }
        }
 
        // 占位数据
        let holderList = await this.dbRead.find(BaseProductPlaceHolder, {
          mainID: In(ids),
          className: "销售订单",
          userProduct_Id: userInfo.userProduct_Id
        });
 
        // 常规打印
        for (let orderInfo of orderList) {
          // 处理扩展字段
          if (orderInfo["expandFields"]) {
            let expandFields = JSON.parse(orderInfo["expandFields"]);
            Object.assign(orderInfo, expandFields);
          }
          let expressCorpInfo = expressList.find(item => item.expressCorp_Id === orderInfo.expressCorp_Id);
          let printTemplate_Id = 0;
          if (this.body.type == "detail" && expressCorpInfo != null) {
            printTemplate_Id = expressCorpInfo.orderDetailTemplateID || 0;
          } else if (["express", "case"].indexOf(this.body.type) >= 0 && expressCorpInfo != null) {
            printTemplate_Id = expressCorpInfo.faceBillTemplateID || 0;
          }
          let templateInfo = templateList.find(item => item.printTemplate_Id == printTemplate_Id);
          let detailList = orderDetails.filter(item => item.order_Id === orderInfo.order_Id);
 
          // 获取配货位
          let orderMacthInfo = allotPositions.find(item => item.orderCode === orderInfo.orderCode);
          if (orderMacthInfo) {
            orderInfo["配货位"] = orderMacthInfo.allotPositionName;
            orderInfo["orderCount"] = orderMacthInfo.orderCount;
          }
          dataList.push({
            mainInfo: orderInfo,
            detailList: detailList.map(item => {
              // 获取货位
              item["positionName"] = holderList
                .filter(h => h.mainID == item.order_Id)
                .map(row => row.positionName)
                .join(",");
              return item;
            }),
            vueData: templateInfo != null ? templateInfo.vueData : null
          });
        }
      }
 
      this.info.data = dataList;
      this.info.result = true;
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "保存失败," + ex.message;
    }
 
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region PackingList
  /// <summary>
  /// 装箱清单打印
  /// </summary>
  /// <param name="this.body">保存请求参数</param>
  @Post()
  public async packingList() {
    try {
      var dataInfo = this.body.dataInfo;
 
      // 获得出库单打印模板
      var orderList = await this.dbRead.find(SaleOrder, {
        order_Id: dataInfo.order_Id
      });
      var dataList = await orderList.map(async s => {
        var expressCorpInfo = await this.dbRead.findOne(BaseExpressCorp, s.expressCorp_Id);
        var printTemplate_Id = 0;
        if (this.body.type == "detail" && expressCorpInfo != null) {
          printTemplate_Id = expressCorpInfo.orderDetailTemplateID || 0;
        } else if (this.body.type == "express" && expressCorpInfo != null) {
          // printTemplate_Id = expressCorpInfo.packingDetailTemplateID || 0;
        }
        var templateInfo = await this.dbRead.findOne(BasePrintTemplate, printTemplate_Id);
 
        var detailList = await this.dbRead.find(vSaleOrderList, {
          orderList_Id: dataInfo.orderList_Id
        });
        for (var item of detailList) {
          item.quantityOrder = dataInfo.finishedQuantity;
        }
 
        //var detailList = Sale_OrderListRepository.Instance.GetMany("order_Id=" + s.order_Id).ToList();
        s.remark = dataInfo.caseNumber;
        return {
          mainInfo: s,
          detailList: detailList,
          vueData: templateInfo != null ? templateInfo.vueData : null
        };
      });
 
      this.info.data = await dataList;
      this.info.result = true;
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "保存失败," + ex.message;
    }
 
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region 获取LODOP模板
  /// <summary>
  /// 获取LODOP模板
  /// </summary>
  @Post()
  public async getLodopTemplate() {
    let userInfo = await this.userInfo;
    var printInfo = await this.dbRead.findOne(TemplateSendBill, {
      sendBillName: this.body.sendBillName,
      userProduct_Id: userInfo.userProduct_Id
    });
 
    if (printInfo != null) {
      this.info.result = true;
      this.info.data = {
        designConent: printInfo.designConent
      };
    } else {
      this.info.result = false;
      this.info.msg = "模板不存在";
    }
 
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region 获取模板字段
  /**
   * 获取模板字段
   */
  @Post()
  public async getColumnList() {
    var colList = await this.dbRead.find(TemplateSendBillColumnInfo, {
      select: ["columnComment", "columnName"],
      where: {
        enable: 1
      }
    });
 
    this.info.result = true;
    this.info.data = colList;
 
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region 保存LODOP模板
  /// <summary>
  /// 保存LODOP模板
  /// </summary>
  /// <param name="this.body"></param>
  /// <returns></returns>
  @Post()
  public async saveLodopTemplate() {
    let userInfo = await this.userInfo;
    var printInfo = await this.dbRead.findOne(TemplateSendBill, {
      sendBillName: this.body.sendBillName,
      userProduct_Id: userInfo.userProduct_Id
    });
 
    if (printInfo != null) {
      await this.dbWrite.update(TemplateSendBill, printInfo.sendBill_Id, {
        designConent: this.body.designConent
      });
      this.info.result = true;
      this.info.msg = "修改保存成功";
    } else {
      printInfo = new TemplateSendBill();
      printInfo.sendBillName = this.body.sendBillName;
      printInfo.designConent = this.body.designConent;
      this.setAccountInfo(printInfo);
      await this.dbWrite.save(printInfo);
 
      this.info.result = true;
      this.info.msg = "初始化保存成功";
    }
 
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region 打印快递面单
  @Post()
  public async printWayBill() {
    try {
      var orderPrintList = await this.dbRead.find(TMSWayBill, {
        wayBill_Id: In(this.body.ids)
      });
 
      var dataList = [];
      for (let printInfo of orderPrintList) {
        //获得明细数据
        var details = await this.dbRead
          .createQueryBuilder(TMSWayBillList, "t")
          .select(["productName", "quantityOrder", "productSpec", "wayBill_Id"])
          .addSelect(
            `(SELECT TOP 1 CIQNameCn FROM dbo.TMS_ProductInfo_Port WHERE Product_Id=t.Product_Id AND port_Id=${printInfo.port_Id || 0})`,
            "cIQNameCn"
          )
          .where({
            wayBill_Id: printInfo.wayBill_Id
          })
          .getRawMany();
 
        var otherData = [];
        if (printInfo.expressCorpType == "5") {
          //圆通
          var ytOrder = await this.dbRead.findOne(ExpressYTOrder, {
            txLogisticID: printInfo.wayBillCode
          });
          if (ytOrder != null) {
            otherData.push({ bigPen: ytOrder.bigPen });
          }
        }
 
        if (printInfo.expressCorpType == "19") {
          //泉州顺丰
          var QZSFOrder = await this.dbRead.findOne(ExpressSFOrder, {
            orderCode: printInfo.wayBillCode,
            expressCorpName: "泉州顺丰"
          });
          if (QZSFOrder != null) {
            otherData.push({ codingMapping: QZSFOrder.codingMapping });
            otherData.push({ codingMappingOut: QZSFOrder.codingMappingOut });
            otherData.push({ abFlag: QZSFOrder.abFlag });
            otherData.push({ pro_code: QZSFOrder.pro_code });
          }
        }
 
        let groupList = details.reduce((all: Array<any>, next) => (all.some(item => item.wayBill_Id == next.wayBill_Id) ? all : [...all, next]), []);
        dataList.push({
          mainInfo: printInfo,
          detaiList: {
            total: details.length,
            rows: details,
            orderCount: groupList.length
          },
          otherData: otherData
        });
      }
      this.info.result = true;
      this.info.data = dataList;
 
      //#region 记录打印次数
      //where = "OrderPrint_Id in(Select Col from dbo.f_split(@ids, ','))";
      //string sql = "Update Sale_OrderPrint Set PickingPrint=isnull(PickingPrint,0)+1 Where " + where;
      //paramList = new List<SqlParameter>();
      //paramList.Add(new SqlParameter("@IDs", this.body.IDs));
      //Sale_OrderPrintRepository.Instance.ExeSql(sql, paramList.ToArray());
      //#endregion
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "加载数据失败:" + ex.message;
    }
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region 打印快递面单Other
  @Post()
  public async printWayBillOther() {
    try {
      var orderPrintList = await this.dbRead.find(TMSWayBillOther, {
        wayBillOther_Id: In(this.body.ids)
      });
 
      let dataList = [];
      for (let printInfo of orderPrintList) {
        //获得明细数据
        var printDetails = await this.dbRead.find(TMSWayBillOtherList, {
          wayBillOther_Id: printInfo.wayBillOther_Id
        });
        var otherData = [];
        var wayInfo = await this.dbRead.findOne(TMSWayBill, {
          wayBillCode: printInfo.wayBillCode
        });
        if (wayInfo != null) {
          otherData.push({ bigPen: wayInfo.bigPen });
        }
 
        let groupList = printDetails.reduce(
          (all: Array<any>, next) => (all.some(item => item.wayBillOther_Id == next.wayBillOther_Id) ? all : [...all, next]),
          []
        );
 
        dataList.push({
          mainInfo: printInfo,
          detaiList: {
            total: printDetails.length,
            rows: printDetails,
            orderCount: groupList.length
          },
          otherData: otherData
        });
        return true;
      }
      this.info.result = true;
      this.info.data = dataList;
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "加载数据失败:" + ex.message;
    }
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region 入库标签打印 purchaseOrderList
  /// <summary>
  /// 入库标签打印
  /// </summary>
  @Post()
  public async purchaseOrderList() {
    try {
      let ids = this.body.ids;
      if (!Array.isArray(ids)) {
        ids = ids.split(",");
      }
      // 获得预到货单数据
      var orderList = await this.dbRead.find(PurchaseOrder, {
        order_Id: In(ids)
      });
      if (!orderList.length) {
        this.info.data = [];
        this.info.result = true;
        return;
      }
      var dataList = [];
      // 订单明细
      let orderDetails = await this.dbRead.find(vPurchaseOrderList, {
        order_Id: In(ids)
      });
      for (let detail of orderDetails) {
        // 处理主表扩展字段
        if (detail["expandFields"]) {
          let expandFields = JSON.parse(detail["expandFields"]);
          Object.assign(detail, expandFields);
        }
        // 处理明细表扩展字段
        if (detail["detailExpandFields"]) {
          let expandFields = JSON.parse(detail["detailExpandFields"]);
          Object.assign(detail, expandFields);
        }
      }
 
      // 常规打印
      for (let orderInfo of orderList) {
        // 处理扩展字段
        if (orderInfo["expandFields"]) {
          let expandFields = JSON.parse(orderInfo["expandFields"]);
          Object.assign(orderInfo, expandFields);
        }
        // orderInfo["xxxxxx"] = 3333;
        let menu_Id = 20001;
        let templateInfo = await this.dbRead.findOne(BasePrintTemplate, {
          menu_Id: menu_Id
        });
 
        let detailList = orderDetails.filter(item => item.order_Id === orderInfo.order_Id);
        if (detailList.length) {
          orderInfo["batchNumber"] = detailList[0].batchNumber;
          //  orderInfo["produceDate"] = detailList[0].produceDate;
          orderInfo["produceDate"] = moment(detailList[0].produceDate).format("YYYY-MM-DD")
        }
 
        dataList.push({
          mainInfo: orderInfo,
          detailList: detailList,
          vueData: templateInfo != null ? templateInfo.vueData : null
        });
      }
 
      this.info.data = dataList;
      this.info.result = true;
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "保存失败," + ex.message;
    }
 
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region EU入库标签打印 eupurchaseOrderList
  /// <summary>
  /// EU入库标签打印
  /// </summary>
  @Post()
  public async eupurchaseOrderList() {
    try {
      let ids = this.body.ids;
      if (!Array.isArray(ids)) {
        ids = ids.split(",");
      }
      // 获得预到货单数据
      var orderList = await this.dbRead.find(PurchaseOrder, {
        order_Id: In(ids)
      });
      if (!orderList.length) {
        this.info.data = [];
        this.info.result = true;
        return;
      }
      var dataList = [];
      // 订单明细
      let orderDetails = await this.dbRead.find(vPurchaseOrderList, {
        order_Id: In(ids)
      });
      for (let detail of orderDetails) {
        // 处理主表扩展字段
        if (detail["expandFields"]) {
          let expandFields = JSON.parse(detail["expandFields"]);
          Object.assign(detail, expandFields);
        }
        // 处理明细表扩展字段
        if (detail["detailExpandFields"]) {
          let expandFields = JSON.parse(detail["detailExpandFields"]);
          Object.assign(detail, expandFields);
        }
      }
 
      // 常规打印
      for (let orderInfo of orderList) {
        // 处理扩展字段
        if (orderInfo["expandFields"]) {
          let expandFields = JSON.parse(orderInfo["expandFields"]);
          Object.assign(orderInfo, expandFields);
        }
        // orderInfo["xxxxxx"] = 3333;
        let menu_Id = 20002;
        let templateInfo = await this.dbRead.findOne(BasePrintTemplate, {
          menu_Id: menu_Id
        });;
        let detailList = orderDetails.filter(item => item.order_Id === orderInfo.order_Id);
 
        dataList.push({
          mainInfo: orderInfo,
          detailList: detailList,
          vueData: templateInfo != null ? templateInfo.vueData : null
        });
      }
 
      this.info.data = dataList;
      this.info.result = true;
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "保存失败," + ex.message;
    }
 
    this.ctx.body = this.info;
  }
  //#endregion
 
 
}