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
import 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 pdaPrintController extends BaseController {
  //#region 获得打印机IP和端口
  @Post()
  public async getIP() {
    try {
      var printInfo = await this.dbRead.findOne(TMSPDAPrint, {
        user_Id: this.body.id
      });
      if (printInfo != null) {
        this.info.result = true;
        this.info.data = {
          PDA_PrintIP: printInfo.iP,
          PDA_PrintPort: printInfo.port || "8000"
        };
      } else {
        this.info.result = false;
      }
    } catch (ex) {
      this.info.msg = ex.message;
      this.info.result = false;
    }
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region 获取运单信息和打印机名称
  @Post()
  public async getWayBillPrinter() {
    try {
      this.ctx.logger.info(this.body.Code + " 打印请求:" + JSON.stringify(this.body));
 
      var wayInfo = await this.dbRead.findOne(TMSWayBill, {
        wayBillCode: this.body.wayBillCode
      });
      if (wayInfo != null) {
        if (!wayInfo.portName) {
          this.info.result = false;
          this.info.msg = "未设置口岸信息";
          this.ctx.body = this.info;
          return;
        }
 
        //#region 获取快递模板Id
        let template = new TMSPortTemplate();
        var protTemplateName = this.body.protTemplateName; //定向口岸
        if (protTemplateName && protTemplateName != "系统分配") {
          template = await this.dbRead.findOne(TMSPortTemplate, {
            protTemplateName: protTemplateName
          });
        } else {
          template = await this.dbRead.findOne(TMSPortTemplate, {
            portName: wayInfo.portName,
            expressCorp_Id: wayInfo.expressCorp_Id
          });
        }
        if (template == null) {
          this.info.result = false;
          this.info.msg = "口岸未设置快递模板";
          this.ctx.body = this.info;
          return;
        }
 
        let btemp = await this.dbRead.findOne(BasePrintTemplate, {
          printTemplate_Id: template.faceBillTemplateID
        });
        if (btemp == null) {
          this.info.result = false;
          this.info.msg = "快递模板不存在";
          this.ctx.body = this.info;
          return;
        }
        //#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 + "未设置打印机";
          this.ctx.body = this.info;
          return;
        }
        //#endregion
 
        // 获得模板VueData
        var templateInfo = await this.dbRead.findOne(BasePrintTemplate, {
          menu_Id: btemp.menu_Id
        });
        if (templateInfo == null) {
          this.info.result = false;
          this.info.msg = wayInfo.wayBillCode + "打印模板不存在";
          this.ctx.body = this.info;
          return;
        }
 
        //#region 获得打印数据
        //获得明细数据
        let 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.query(sql);
 
        var printDetails = await this.dbRead.find(TMSWayBillList, {
          wayBill_Id: wayInfo.wayBill_Id
        });
        var otherData = [];
        if (wayInfo.expressCorpType == "5") {
          //圆通
          var ytOrder = await this.dbRead.findOne(ExpressYTOrder, {
            txLogisticID: wayInfo.wayBillCode
          });
          if (ytOrder != null) {
            otherData.push({ bigPen: ytOrder.bigPen });
          }
        }
 
        if (wayInfo.expressCorpType == "19") {
          //泉州顺丰
          var QZSFOrder = await this.dbRead.findOne(ExpressSFOrder, {
            orderCode: wayInfo.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 = 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 {
          let sql = "Update TMS_WayBill Set printNum=isnull(printNum, 0) + 1 where wayBill_Id=" + wayInfo.wayBill_Id;
          await this.dbWrite.query(sql);
          this.ctx.logger.info(
            wayInfo.wayBillCode + " 更新打印次数:" + ((wayInfo.printNum || 0) + 1) + ", SQL=" + sql
          );
        } 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;
    }
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region 获取打印模板
  @Post()
  public async getPrintTemplate() {
    try {
      this.ctx.logger.info(this.body.Code + " 打印请求:" + JSON.stringify(this.body));
 
      // 获得模板VueData
      var templateInfo = await this.dbRead.findOne(BasePrintTemplate, {
        menu_Id: this.body.menu_Id
      });
      if (templateInfo == null) {
        this.info.result = false;
        //this.info.msg = "打印模板不存在";
        this.ctx.body = this.info;
        return;
      }
 
      this.info.result = true;
      this.info.data = JSON.parse(templateInfo.vueData);
    } catch (ex) {
      this.info.msg = ex.message;
      this.info.result = false;
    }
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region 获取口岸列表
  @Post()
  public async getPortList() {
    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;
    }
    this.ctx.body = this.info;
  }
  //#endregion
}