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
import { default as BaseController } from "../baseController";
import { Post } from "egg-shell-decorators";
import { TMSPDAPrint } from "../../entity/express/tms/tmsPDAPrint";
import { TMSWayBill } from "../../entity/express/tms/tmsWayBill";
import { TMSPortTemplate } from "../../entity/express/tms/tmsPortTemplate";
import { BasePrintTemplate } from "../../entity/sys/print/basePrintTemplate";
import { TMSWayBillList } from "../../entity/express/tms/tmsWayBillList";
import { ExpressYTOrder } from "../../entity/express/yuantong/expressYTOrder";
import { ExpressSFOrder } from "../../entity/express/sf/expressSFOrder";
 
/**
 * 打印模板
 */
export default class PrintTemplateController extends BaseController {
  //#region 获得打印机IP和端口
  /// <summary>
  /// 清关
  /// </summary>
  /// <param name="body"></param>
  /// <returns></returns>
  @Post()
  public async getIP() {
    let { ctx } = this;
    let body = ctx.request.body;
    try {
      var printInfo = await this.dbRead.findOne(TMSPDAPrint, {
        user_Id: body.user_Id
      });
      if (printInfo != null) {
        let dataInfo: any = {
          pdaPrintIP: printInfo.iP,
          pdaPrintPort: printInfo.port || "8000"
        };
        this.info.result = true;
        this.info.data = dataInfo;
      } else {
        this.info.result = false;
      }
    } catch (ex) {
      this.info.msg = ex.message;
      this.info.result = false;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 获取运单信息和打印机名称
  @Post()
  public async getWayBillPrinter() {
    let { ctx } = this;
    let body = ctx.request.body;
    try {
      console.log(body.Code + " 打印请求:" + JSON.stringify(body));
 
      var wayInfo = await this.dbRead.findOne(TMSWayBill, {
        wayBillCode: body.wayBillCode
      });
      if (wayInfo != null) {
        if (!wayInfo.portName) {
          this.info.result = false;
          this.info.msg = "未设置口岸信息";
          ctx.body = this.info;
        }
 
        //#region 获取快递模板Id
        let template = new TMSPortTemplate();
        var protTemplateName = body.protTemplateName; //定向口岸
        if (protTemplateName && protTemplateName != "系统分配") {
          template = await this.dbRead.findOne(TMSPortTemplate, {
            protTemplateName: protTemplateName
          });
        } else {
          template = await this.dbRead.findOne(TMSPortTemplate, {
            portName: wayInfo.portName
          });
        }
        if (template == null) {
          this.info.result = false;
          this.info.msg = "口岸未设置快递模板";
          ctx.body = this.info;
        }
 
        let btemp = await this.dbRead.findOne(BasePrintTemplate, {
          printTemplate_Id: template.faceBillTemplateID
        });
        if (btemp == null) {
          this.info.result = false;
          this.info.msg = "快递模板不存在";
          ctx.body = this.info;
        }
        //#endregion
 
        //#region 获得口岸信息
        var printInfo = await this.dbRead.findOne(TMSPDAPrint, {
          portName: wayInfo.portName
        });
        if (printInfo == null) {
          this.info.result = false;
          this.info.msg = wayInfo.wayBillCode + wayInfo.portName + "未设置打印机";
          ctx.body = this.info;
        }
        //#endregion
 
        // 获得模板VueData
        var templateInfo = await this.dbRead.findOne(BasePrintTemplate, {
          menu_Id: body.menu_Id
        });
        if (templateInfo == null) {
          this.info.result = false;
          this.info.msg = wayInfo.wayBillCode + "打印模板不存在";
          ctx.body = this.info;
        }
 
        //#region 获得打印数据
        //获得明细数据
        // sql =
        // SELECT L.ProductName, L.QuantityOrder, L.ProductSpec,
        //     (SELECT TOP 1 CIQNameCn FROM dbo.TMS_ProductInfo_Port WHERE Product_Id=L.Product_Id AND port_Id="
        //         + (wayInfo.port_Id ?? 0) + @") AS CIQNameCn
        // FROM dbo.TMS_WayBillList L
        // WHERE L.wayBill_Id=" + wayInfo.wayBill_Id;
        var detailDataSet = await this.dbRead
          .createQueryBuilder(TMSWayBillList, "t")
          .addSelect(
            `(SELECT TOP 1 CIQNameCn FROM dbo.TMS_ProductInfo_Port WHERE Product_Id=L.Product_Id AND port_Id=${wayInfo.port_Id ||
              0})`,
            "cIQNameCn"
          )
          .where("wayBill_Id=:wayBill_Id", {
            wayBill_Id: wayInfo.wayBill_Id
          });
 
        var printDetails = await this.dbRead.find(TMSWayBillList, {
          wayBill_Id: wayInfo.wayBill_Id
        });
        var otherData: any = {};
        if (wayInfo.expressCorpType == "5") {
          //圆通
          var ytOrder = await this.dbRead.findOne(ExpressYTOrder, {
            txLogisticID: wayInfo.wayBillCode
          });
          if (ytOrder != null) {
            otherData["BigPen"] = ytOrder.bigPen;
          }
        }
 
        if (wayInfo.expressCorpType == "19") {
          //泉州顺丰
          var QZSFOrder = await this.dbRead.findOne(ExpressSFOrder, {
            orderCode: wayInfo.wayBillCode,
            expressCorpName: "泉州顺丰"
          });
          if (QZSFOrder != null) {
            otherData["codingMapping"] = QZSFOrder.codingMapping;
            otherData["codingMappingOut"] = QZSFOrder.codingMappingOut;
            otherData["abFlag"] = QZSFOrder.abFlag;
            otherData["pro_code"] = QZSFOrder.pro_code;
          }
        }
 
        let groupList = printDetails.reduce(
          (all: Array<any>, next) =>
            all.some(item => item["wayBill_Id"] == next["wayBill_Id"]) ? all : [...all, next],
          []
        );
 
        var billDataInfo = {
          mainInfo: wayInfo,
          detaiList: {
            total: printDetails.length,
            rows: detailDataSet,
            orderCount: groupList.length
          },
          otherData: otherData
        };
 
        //#endregion
 
        // 更新打印次数
        try {
          await this.dbWrite.update(TMSWayBill, wayInfo.wayBill_Id, {
            printNum: (wayInfo.printNum || 0) + 1
          });
          await this.dbWrite.increment(TMSWayBill, wayInfo.wayBill_Id, "printNum", wayInfo.printNum || 0);
          console.log(wayInfo.wayBillCode + " 更新打印次数:" + ((wayInfo.printNum || 0) + 1));
        } catch (Exception) {}
 
        this.info.result = true;
        this.info.data = {
          wayInfo: {
            wayBill_Id: wayInfo.wayBill_Id,
            wayBillCode: wayInfo.wayBillCode
          },
          printInfo: {
            printerName: printInfo.printerName,
            portName: printInfo.portName
          },
          // 模板ID
          menu_Id: btemp.menu_Id,
          // 打印模板VueData
          vueData: templateInfo.vueData,
          // 打印数据
          billDataInfo: billDataInfo
        };
      } else {
        this.info.result = false;
        this.info.msg = "运单号不存在";
      }
    } catch (ex) {
      this.info.msg = ex.message;
      this.info.result = false;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 获取打印模板
  @Post()
  public async getPrintTemplate() {
    let { ctx } = this;
    let body = ctx.request.body;
    try {
      // 获得模板VueData
      var templateInfo = await this.dbRead.findOne(BasePrintTemplate, {
        menu_Id: body.menu_Id
      });
      if (!templateInfo) {
        this.info.result = false;
        //this.info.msg = "打印模板不存在";
        ctx.body = this.info;
      }
 
      this.info.result = true;
      this.info.data = JSON.parse(templateInfo.vueData);
    } catch (ex) {
      this.info.msg = ex.message;
      this.info.result = false;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region getTemplateList
  /// <summary>
  /// 新增项
  /// </summary>
  /// <param name="this.body">保存请求参数</param>
  @Post()
  public async getTemplateList() {
    let userInfo = await this.userInfo;
    try {
      var where =
        "userProduct_Id=:userProduct_Id And parentId in(Select printTemplate_Id From Base_PrintTemplate Where templateName=:templateName)";
      var dataList = await this.dbRead
        .createQueryBuilder(BasePrintTemplate, "t")
        .select(["printTemplate_Id", "menu_Id", "templateName"])
        .where(where, {
          userProduct_Id: userInfo.userProduct_Id,
          templateName: this.body.templateName
        })
        .getRawMany();
 
      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 getPortList 获取口岸模板列表
  @Post()
  public async getPortList() {
    let { ctx } = this;
    try {
      var portList = await this.dbRead.find(TMSPortTemplate);
 
      this.info.result = true;
      this.info.data = portList.map(s => {
        return {
          portTemplate_Id: s.portTemplate_Id,
          protTemplateName: s.protTemplateName
        };
      });
    } catch (ex) {
      this.info.msg = ex.message;
      this.info.result = false;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region AddItem
  /// <summary>
  /// 新增项
  /// </summary>
  @Post()
  public async addItem() {
    try {
      var tempInfo = new BasePrintTemplate();
      tempInfo.templateName = this.body.templateName;
      tempInfo.parentId = this.body.parentId;
      tempInfo.vueData = `{"dataOptions":{
                    "menu_Id": 0,
                    "projectName": null,
                    "tableView": null,
                    "idField": null,
                    "router": null,
                    "title": "${this.body.value}",
                    "paddingTop": 30,
                    "paddingBottom": 30,
                    "paddingLeft": 30,
                    "paddingRight": 30,
                    "width": 830,
                    "height": 500
        },"fields":[]}`;
      await this.setAccountInfo(tempInfo);
      await this.dbWrite.save(tempInfo);
 
      this.info.result = true;
      this.info.msg = "新增成功";
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "保存失败," + ex.message;
    }
 
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region CopyItem
  /// <summary>
  /// 复制项
  /// </summary>
  @Post()
  public async copyItem() {
    try {
      var tempInfo = await this.dbRead.findOne(BasePrintTemplate, this.body.id);
      if (tempInfo != null) {
        tempInfo.printTemplate_Id = 0;
        tempInfo.templateName = this.body.value;
        await this.dbWrite.insert(BasePrintTemplate, tempInfo);
        this.info.result = true;
        this.info.msg = "复制成功";
      }
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "保存失败," + ex.message;
    }
 
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region UpdateTitle
  /// <summary>
  /// 修改名称
  /// </summary>
  @Post()
  public async updateTitle() {
    try {
      var tempInfo = await this.dbRead.findOne(BasePrintTemplate, this.body.id);
      if (tempInfo != null) {
        tempInfo.templateName = this.body.value;
        await this.dbWrite.update(BasePrintTemplate, tempInfo.printTemplate_Id, {
          templateName: this.body.value
        });
      }
 
      this.info.result = true;
      this.info.msg = "保存成功";
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "保存失败," + ex.message;
    }
 
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region DeleteItem
  /// <summary>
  /// 上次项
  /// </summary>
  /// <param name="DeleteItem">保存请求参数</param>
  @Post()
  public async deleteItem() {
    try {
      var tempInfo = await this.dbRead.findOne(BasePrintTemplate, this.body.id);
      if (tempInfo != null) {
        tempInfo.templateName = this.body.value;
        await this.dbWrite.delete(BasePrintTemplate, tempInfo.printTemplate_Id);
        this.info.result = true;
        this.info.msg = "删除成功";
      }
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "保存失败," + ex.message;
    }
 
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region MoveItem
  /// <summary>
  /// 移动项
  /// </summary>
  /// <param name="this.body">保存请求参数</param>
  @Post()
  public async moveItem() {
    try {
      var tempInfo = await this.dbRead.findOne(BasePrintTemplate, this.body.id);
      if (tempInfo != null) {
        var parentId = this.body.value;
        if (this.body.id == this.body.value) {
          parentId = 0;
        }
        await this.dbWrite.update(BasePrintTemplate, tempInfo.printTemplate_Id, {
          parentId: parentId
        });
        this.info.result = true;
        this.info.msg = "移动成功";
      }
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "保存失败," + ex.message;
    }
 
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region savePrintTemplate 创建BaseVue页面
  /// <summary>
  /// 创建Vue页面
  /// </summary>
  ///
  @Post()
  public async savePrintTemplate() {
    try {
      var printInfo = await this.dbRead.findOne(BasePrintTemplate, this.body.printTemplate_Id);
      if (printInfo != null) {
        printInfo.vueData = this.body.vueData;
        printInfo.templateName = this.body.templateName;
        printInfo.templateType = this.body.templateType;
        printInfo.menu_Id = this.body.menu_Id;
        await this.dbWrite.save(BasePrintTemplate, printInfo);
      } else {
        printInfo = new BasePrintTemplate();
        await this.setAccountInfo(printInfo);
 
        printInfo.printTemplate_Id = this.body.printTemplate_Id;
        printInfo.templateName = this.body.templateName;
        printInfo.templateType = this.body.templateType;
        printInfo.menu_Id = this.body.menu_Id;
        printInfo.vueData = this.body.vueData;
        await this.dbWrite.save(BasePrintTemplate, printInfo);
      }
 
      this.info.result = true;
      this.info.msg = "保存成功";
 
      this.ctx.body = this.info;
    } catch (ex) {
      let msg = "保存失败:" + ex.message;
      this.info.result = false;
      this.info.msg = msg;
      this.ctx.body = this.info;
    }
  }
  //#endregion
}