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
import * as sql from "mssql";
import { Post } from "egg-shell-decorators";
import { BaseConsignorAddress } from "../../../entity/basicInfo/consignor/baseConsignorAddress";
import { BaseProductInfo } from "../../../entity/basicInfo/base/baseProductInfo";
import BaseController from "../../baseController";
import { BaseCity } from "../../../entity/basicInfo/base/baseCity";
import { BaseConsignor } from "../../../entity/basicInfo/consignor/baseConsignor";
import { TMSWayBill } from "../../../entity/express/tms/tmsWayBill";
import { TMSWayBillReceive } from "../../../entity/express/tms/tmsWayBillReceive";
import { TMSIdCard } from "../../../entity/express/tms/tmsIdCard";
import { TMSWayBillList } from "../../../entity/express/tms/tmsWayBillList";
import { In } from "typeorm";
 
/**
 * 客户端运单管理
 */
export default class BillController extends BaseController {
  //#region 获取省信息
  /**
   * 获取省信息
   */
  @Post()
  public async getCity() {
    try {
      let cityList = await this.dbRead.find(BaseCity, {
        parentId: this.body.parentId
      });
      this.info.data = cityList;
      this.info.result = true;
      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
 
  //#region 根据手机号调取收件地址信息
  /**
   * 根据手机号调取收件地址信息
   */
  @Post()
  public async getConsignorAddress() {
    let userInfo = await this.userInfo;
    try {
      let where =
        " CHARINDEX(:mobile,mobile)>0 and userProduct_Id=:userProduct_Id  and  enable=1 and linkmanAttr=:linkmanAttr and consignor_Id=:consignor_Id";
      let consignorAddressList = await this.dbRead
        .createQueryBuilder(BaseConsignorAddress, "t")
        .select(["mobile"])
        .where(where, {
          mobile: this.body.mobile,
          userProduct_Id: userInfo.userProduct_Id,
          linkmanAttr: this.body.type,
          consignor_Id: userInfo.consignor_Id
        })
        .take(30)
        .getRawMany();
      this.info.data = consignorAddressList;
      this.info.result = true;
      this.ctx.body = this.info;
    } catch (ex) {
      let msg = "异常错误信息:" + ex.message;
      this.info.result = false;
      this.info.msg = msg;
      this.ctx.body = this.info;
    }
  }
  @Post()
  public async getConsignorAddressInfo() {
    let userInfo = await this.userInfo;
    try {
      let consignorAddressList = await this.dbRead.findOne(BaseConsignorAddress, {
        mobile: this.body.mobile,
        userProduct_Id: userInfo.userProduct_Id,
        consignor_Id: userInfo.consignor_Id,
        enable: 1,
        linkmanAttr: "寄件人"
      });
      this.info.data = consignorAddressList;
      this.info.result = true;
      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
 
  //#region 获取寄件人信息
  @Post()
  public async getConsignorAddressBill() {
    let userInfo = await this.userInfo;
    try {
      if (!this.body.mobile) {
        this.info.result = false;
        this.info.msg = "手机号不能为空";
        this.ctx.body = this.info;
      }
      let dataList = await this.dbRead.find(BaseConsignorAddress, {
        mobile: this.body.mobile,
        userProduct_Id: userInfo.userProduct_Id,
        consignor_Id: userInfo.consignor_Id,
        enable: 1,
        linkmanAttr: "寄件人"
      });
      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 根据关键词获取订单可选物料信息
  /**
   * 根据关键词获取订单可选物料信息
   */
  @Post()
  public async getChooseBaseProductInfo() {
    let userInfo = await this.userInfo;
    try {
      let where = `(CHARINDEX(:searchText,productModel)>0 or CHARINDEX(:searchText,productName)>0 or CHARINDEX(:searchText,BrandName)>0 or CHARINDEX(:searchText,TypeName)>0 )and userProduct_Id=:userProduct_Id and Auditing=2 `;
      let dataList = await this.dbRead
        .createQueryBuilder(BaseProductInfo, "t")
        .where(where, {
          userProduct_Id: userInfo.userProduct_Id,
          searchText: this.body.searchText
        })
        .take(50)
        .getMany();
      this.info.data = dataList;
      this.info.result = true;
      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
 
  //#region 客户端 运单录入
  /// <summary>
  /// 客户端 运单录入
  /// </summary>
  /// <returns>运单录入</returns>
  ///
  @Post()
  public async saveWayBillInfo() {
    let userInfo = await this.userInfo;
    try {
      if (!this.body.wayBillInfo.consignor_Id) {
        this.info.result = false;
        this.info.msg = "账号信息不存在";
        return;
      }
      let conInfo = await this.dbRead.findOne(BaseConsignor, {
        consignor_Id: this.body.wayBillInfo.consignor_Id
      });
      let code = "";
      if (conInfo != null) {
        // 获得编码
        const connection: any = await this.dbWrite.connection;
        let request = new sql.Request(connection.driver.master);
        request.input("consignor_Id", conInfo.consignor_Id);
        request.input("user_Id", userInfo.user_Id);
        request.output("code", sql.NVarChar(2000));
        let result = await request.execute("sp_GetCodePlateRule");
        code = result.output.code;
 
        if (code && this.body.productLine == "标准线") {
          code = code + "B";
        } else if (code && this.body.productLine == "专线") {
          code = code + "E";
        }
      }
      //查询运单号是否已揽收
      //添加运单表
      let wayBillInfo = new TMSWayBill();
      wayBillInfo.consignorName = this.body.wayBillInfo.consignorName;
      wayBillInfo.consignorCode = this.body.wayBillInfo.consignorCode;
      wayBillInfo.consignor_Id = this.body.wayBillInfo.consignor_Id;
      wayBillInfo.wayBillCode = code;
      wayBillInfo.consigneeName = this.body.wayBillInfo.consigneeName;
      wayBillInfo.provinceName = this.body.wayBillInfo.ProvinceName;
      wayBillInfo.province_Id = this.body.wayBillInfo.province_Id;
      wayBillInfo.cityName = this.body.wayBillInfo.cityName;
      wayBillInfo.city_Id = this.body.wayBillInfo.city_Id;
      wayBillInfo.regionName = this.body.wayBillInfo.regionName;
      wayBillInfo.region_Id = this.body.wayBillInfo.region_Id;
      wayBillInfo.street = this.body.wayBillInfo.street;
      wayBillInfo.consigneeAddress = this.body.wayBillInfo.consigneeAddress;
      wayBillInfo.consigneePostCode = this.body.wayBillInfo.consigneePostCode;
      wayBillInfo.consigneeMobile = this.body.wayBillInfo.consigneeMobile;
      wayBillInfo.consigneeIdcard = this.body.wayBillInfo.consigneeIdcard;
      //weight = this.body.wayBillInfo.weight;
      wayBillInfo.grossWeight = this.body.wayBillInfo.grossWeight;
      wayBillInfo.isStoreMate = 0;
      wayBillInfo.orderType = this.body.wayBillInfo.orderType;
      wayBillInfo.storage_Id = this.body.wayBillInfo.storage_Id;
      wayBillInfo.storageName = this.body.wayBillInfo.storageName;
      wayBillInfo.remark = this.body.wayBillInfo.remark;
      wayBillInfo.billingName = this.body.wayBillInfo.billingName;
      wayBillInfo.billingMobile = this.body.wayBillInfo.billingMobile;
      wayBillInfo.billingAddress = this.body.wayBillInfo.billingAddress;
      wayBillInfo.expressCorp_Id = null;
      wayBillInfo.expressCorpName = null;
      wayBillInfo.port_Id = null;
      wayBillInfo.portName = null;
      this.setAccountInfo(wayBillInfo);
      wayBillInfo.orderStatus = "新建";
      wayBillInfo.makeWay = "OMS录入";
      wayBillInfo.collectStatus = null;
 
      let wayBillReceiveInfo = await this.dbRead.findOne(TMSWayBillReceive, {
        wayBillCode: wayBillInfo.wayBillCode
      });
 
      // 揽收身份证不为空,以揽收为准
      if (wayBillReceiveInfo != null && wayBillReceiveInfo.consigneeName && wayBillReceiveInfo.consigneeIdcard) {
        wayBillInfo.consigneeName = wayBillReceiveInfo.consigneeName;
        wayBillInfo.consigneeIdcard = wayBillReceiveInfo.consigneeIdcard;
      }
      await this.dbWrite.save(wayBillInfo);
 
      //#region 更新收货人手机到身份证库
      if (this.body.wayBillInfo.consigneeIdcard) {
        let idcard = await this.dbRead.findOne(TMSIdCard, {
          idCardCode: this.body.wayBillInfo.consigneeIdcard
        });
        if (idcard != null) {
          var flag = true;
          if (!idcard.mobile) {
            idcard.mobile = wayBillInfo.consigneeMobile;
            flag = false;
          } else if (!idcard.mobile2 && flag && idcard.mobile != idcard.mobile2) {
            idcard.mobile2 = wayBillInfo.consigneeMobile;
            flag = false;
          } else if (!idcard.mobile3 && flag && idcard.mobile != idcard.mobile3 && idcard.mobile2 != idcard.mobile3) {
            idcard.mobile3 = wayBillInfo.consigneeMobile;
            flag = false;
          }
          await this.dbWrite.save(idcard);
        }
      }
      //#endregion
 
      //#region 运单明细处理
      let quantityOrder = 0; //数量
      let rowTotal = 0; //金额
      for (let detailInfo of this.body.wayBillDetails) {
        let product_Id = detailInfo.product_Id; //物料ID
        let dataInfo = await this.dbRead.findOne(BaseProductInfo, product_Id); //查询物料信息
        //添加到TMS_WayBillList表
        let listInfo = new TMSWayBillList();
        listInfo.wayBill_Id = wayBillInfo.wayBill_Id;
        listInfo.product_Id = product_Id;
        listInfo.productName = detailInfo.productName;
        listInfo.productCode = dataInfo.productCode;
        listInfo.productModel = detailInfo.productModel;
        listInfo.productSpec = detailInfo.productSpec;
        listInfo.quantityOrder = detailInfo.quantityOrder;
        listInfo.smallUnit = detailInfo.smallUnit;
        listInfo.salePrice = detailInfo.salePrice || 0;
        listInfo.rowTotal = detailInfo.quantityOrder * (detailInfo.salePrice || 0);
        listInfo.weight = detailInfo.weight;
        listInfo.grossWeight = detailInfo.weight + 0.1; //以前是加100
 
        quantityOrder = quantityOrder + listInfo.quantityOrder;
        rowTotal = rowTotal + listInfo.rowTotal;
        await this.dbWrite.save(listInfo);
      }
      //#endregion
 
      //#region 修改运单合计数量和合计金额
      let sqlwhere = `Update TMS_WayBill Set GrandTotal=${rowTotal},TotalQuantityOrder=${quantityOrder},
                    weight =(select sum(weight) from TMS_WayBillList where wayBill_Id =TMS_WayBill.wayBill_Id)
                    Where wayBill_Id =${wayBillInfo.wayBill_Id}`;
      await this.dbWrite.query(sqlwhere);
      //#endregion
 
      if (this.body.saveAddress) {
        //查收收件人手机
        let addrInfo = await this.dbRead.findOne(BaseConsignorAddress, {
          userProduct_Id: userInfo.userProduct_Id,
          mobile: this.body.wayBillInfo.consigneeMobile,
          enable: 1,
          linkmanAttr: "收件人"
        });
        if (addrInfo == null) {
          addrInfo = new BaseConsignorAddress();
        }
        addrInfo.consignee = this.body.wayBillInfo.consigneeName;
        addrInfo.consigneeIdcard = this.body.wayBillInfo.consigneeIdcard;
        addrInfo.province_Id = this.body.wayBillInfo.province_Id;
        addrInfo.provinceName = this.body.wayBillInfo.provinceName;
        addrInfo.city_Id = this.body.wayBillInfo.city_Id;
        addrInfo.cityName = this.body.wayBillInfo.cityName;
        addrInfo.region_Id = this.body.wayBillInfo.region_Id;
        addrInfo.regionName = this.body.wayBillInfo.regionName;
        addrInfo.consignorName = this.body.wayBillInfo.consignorName;
        addrInfo.consignorCode = this.body.wayBillInfo.consignorCode;
        addrInfo.consignor_Id = this.body.wayBillInfo.consignor_Id;
        addrInfo.street = this.body.wayBillInfo.street;
        addrInfo.detailAddress = this.body.wayBillInfo.consigneeAddress;
        addrInfo.mobile = this.body.wayBillInfo.consigneeMobile;
        addrInfo.enable = 1;
        addrInfo.linkmanAttr = "收件人";
        this.setAccountInfo(addrInfo);
        await this.dbWrite.save(addrInfo);
        //查询寄件人手机
        let addressInfo = await this.dbRead.findOne(BaseConsignorAddress, {
          userProduct_Id: userInfo.userProduct_Id,
          mobile: this.body.wayBillInfo.billingMobile
        });
        if (addressInfo == null) {
          addressInfo = new BaseConsignorAddress();
        }
        addressInfo.consignee = this.body.wayBillInfo.billingName;
        //addressInfo.consigneeIdcard = this.body.wayBillInfo.consigneeIdcard;
        //addressInfo.province_Id = this.body.wayBillInfo.province_Id;
        //addressInfo.provinceName = this.body.wayBillInfo.provinceName;
        //addressInfo.city_Id = this.body.wayBillInfo.city_Id;
        //addressInfo.cityName = this.body.wayBillInfo.cityName;
        //addressInfo.region_Id = this.body.wayBillInfo.region_Id;
        //addressInfo.regionName = this.body.wayBillInfo.regionName;
        addressInfo.consignorName = this.body.wayBillInfo.consignorName;
        addressInfo.consignorCode = this.body.wayBillInfo.consignorCode;
        addressInfo.consignor_Id = this.body.wayBillInfo.consignor_Id;
        //addressInfo.street = this.body.wayBillInfo.street;
        addressInfo.detailAddress = this.body.wayBillInfo.billingAddress;
        addressInfo.mobile = this.body.wayBillInfo.billingMobile;
        addressInfo.enable = 1;
        addressInfo.linkmanAttr = "寄件人";
        this.setAccountInfo(addressInfo);
        await this.dbWrite.save(addressInfo);
      }
 
      //添加运单追踪记录
      this.ctx.service.tms.wayBillHelper.setStatusHistory(wayBillInfo, "客户端录入", "客户端录入成功");
 
      this.info.msg = wayBillInfo.orderStatus;
      this.info.result = true;
      this.info.data = wayBillInfo;
    } catch (ex) {
      let msg = "异常错误信息:" + ex.message;
      this.info.result = false;
      this.info.msg = msg;
    }
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region 改变打印状态
  @Post()
  public async changePrintStatus() {
    try {
      let wayBill_Ids = this.body.wayBill_Ids;
      if (!Array.isArray(wayBill_Ids) || !wayBill_Ids.length) {
        this.info.result = false;
        this.info.msg = "更新打印状态没有执行的数据";
        return;
      }
      await this.dbWrite.update(
        TMSWayBill,
        {
          wayBill_Id: In(wayBill_Ids)
        },
        {
          printStatus: "已打印"
        }
      );
      this.info.result = true;
    } catch (ex) {
      this.info.msg = "更新打印状态错误:" + ex.message;
      this.info.result = false;
    }
    this.ctx.body = this.info;
  }
  //#endregion
}