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
import * as fs from "fs";
import * as path from "path";
import BaseController from "../baseController";
import { Post } from "egg-shell-decorators";
import { BaseConsignor } from "../../entity/basicInfo/consignor/baseConsignor";
import { BaseStorage } from "../../entity/basicInfo/base/baseStorage";
import { TMSWayBillReceive } from "../../entity/express/tms/tmsWayBillReceive";
import { TMSWayBill } from "../../entity/express/tms/tmsWayBill";
import moment = require("moment");
import { TMSWayBillReceiveTemp } from "../../entity/express/tms/tmsWayBillReceiveTemp";
import { TMSWayBillUpload } from "../../entity/express/tms/tmsWayBillUpload";
import { In } from "typeorm";
 
/**
 * 运单揽收
 */
export default class WayBillReceiveController extends BaseController {
  //#region  揽收导入
  /**
   * 揽收导入
   */
  @Post()
  public async wayBillSave() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    try {
      var consignorInfo = await this.dbRead.findOne(BaseConsignor, {
        consignor_Id: body.consignor_Id
      });
      if (consignorInfo == null) {
        this.info.result = false;
        this.info.msg = "货主不存在";
        ctx.body = this.info;
        return;
      }
      var storageInfo = await this.dbRead.findOne(BaseStorage, {
        storage_Id: body.storage_Id
      });
      if (storageInfo == null) {
        this.info.result = false;
        this.info.msg = "仓库不存在";
        ctx.body = this.info;
        return;
      }
      let wayBillCodes = body.wayBillCodes;
      var wayBillList = wayBillCodes.replace(/\r/gi, "").split(/,|,|、|:| |\n/);
      //var Code = "";d
      var ishave = false;
      var errMsg = "";
      for (var wayBillCode of wayBillList) {
        var wayReceiveInfo = await this.dbRead.findOne(TMSWayBillReceive, {
          wayBillCode: wayBillCode,
          collectStatus: "已揽收"
        });
        if (wayReceiveInfo != null) {
          ishave = true;
          errMsg = "运单号" + wayBillCode + "已存在且已揽收";
        }
      }
      if (ishave) {
        this.info.result = false;
        this.info.msg = errMsg;
        ctx.body = this.info;
        return;
      }
      for (var wayBillCode of wayBillList) {
        var wayReceiveInfo = await this.dbRead.findOne(TMSWayBillReceive, {
          wayBillCode: wayBillCode
        });
        if (wayReceiveInfo != null) {
          wayReceiveInfo.storage_Id = storageInfo.storage_Id;
          wayReceiveInfo.storageName = storageInfo.storageName;
          wayReceiveInfo.consignor_Id = consignorInfo.consignor_Id;
          wayReceiveInfo.consignorName = consignorInfo.consignorName;
          wayReceiveInfo.consignorCode = consignorInfo.consignorCode;
          wayReceiveInfo.storeName = consignorInfo.consignorName;
          wayReceiveInfo.store_Id = consignorInfo.consignor_Id;
          wayReceiveInfo.orderStatus = "新建";
          wayReceiveInfo.collectStatus = "已揽收";
          wayReceiveInfo.user_Id = userInfo.user_Id;
          wayReceiveInfo.userTrueName = userInfo.userTrueName;
          wayReceiveInfo.createID = userInfo.user_Id;
          wayReceiveInfo.creator = userInfo.userTrueName;
          wayReceiveInfo.platUser_Id = userInfo.platUser_Id;
          wayReceiveInfo.platUserName = userInfo.platUserName;
          wayReceiveInfo.platUserCode = userInfo.platUserCode;
          wayReceiveInfo.platCorpName = userInfo.platCorpName;
          wayReceiveInfo.userProduct_Id = userInfo.userProduct_Id;
          wayReceiveInfo.userProductCode = userInfo.userProductCode;
          wayReceiveInfo.userProductAlias = userInfo.userProductAlias;
        } else {
          wayReceiveInfo = new TMSWayBillReceive();
          wayReceiveInfo.wayBillCode = wayBillCode;
          wayReceiveInfo.storage_Id = storageInfo.storage_Id;
          wayReceiveInfo.storageName = storageInfo.storageName;
          wayReceiveInfo.consignor_Id = consignorInfo.consignor_Id;
          wayReceiveInfo.consignorName = consignorInfo.consignorName;
          wayReceiveInfo.consignorCode = consignorInfo.consignorCode;
          wayReceiveInfo.storeName = consignorInfo.consignorName;
          wayReceiveInfo.store_Id = consignorInfo.consignor_Id;
          wayReceiveInfo.orderStatus = "新建";
          wayReceiveInfo.collectStatus = "已揽收";
          wayReceiveInfo.user_Id = userInfo.user_Id;
          wayReceiveInfo.userTrueName = userInfo.userTrueName;
          wayReceiveInfo.createID = userInfo.user_Id;
          wayReceiveInfo.creator = userInfo.userTrueName;
          wayReceiveInfo.platUser_Id = userInfo.platUser_Id;
          wayReceiveInfo.platUserName = userInfo.platUserName;
          wayReceiveInfo.platUserCode = userInfo.platUserCode;
          wayReceiveInfo.platCorpName = userInfo.platCorpName;
          wayReceiveInfo.userProduct_Id = userInfo.userProduct_Id;
          wayReceiveInfo.userProductCode = userInfo.userProductCode;
          wayReceiveInfo.userProductAlias = userInfo.userProductAlias;
        }
        await this.dbWrite.save(wayReceiveInfo);
 
        var billInfo = await this.dbRead.findOne(TMSWayBill, {
          wayBillCode: wayReceiveInfo.wayBillCode
        });
        if (billInfo != null) {
          await this.dbWrite.update(TMSWayBill, billInfo.wayBill_Id, {
            collectStatus: "已揽收"
          });
        } else {
          billInfo = new TMSWayBill();
          billInfo.wayBill_Id = wayReceiveInfo.wayBillReceive_Id;
          billInfo.wayBillCode = wayReceiveInfo.wayBillCode;
          billInfo.orderStatus = null;
          billInfo.collectStatus = "已揽收";
        }
        await ctx.service.tms.wayBillHelper.setStatusHistory(
          billInfo,
          "揽收导入",
          "揽收导入",
          wayReceiveInfo.storageName + "已成功收件,等待理货运输"
        );
      }
      this.info.result = true;
      this.info.msg = "导入成功;";
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "异常" + ex.message;
      ctx.body = this.info;
      return;
    }
    ctx.body = this.info;
    return;
  }
  //#endregion
 
  //#region UploadWayBillReceive 运单揽收上传
  /**
   * 运单揽收
   */
  @Post()
  public async uploadWayBillReceive() {
    let { ctx } = this;
    let userInfo = await ctx.helper.userInfo();
 
    try {
      //获取参数信息
      let user_Id = userInfo.user_Id;
      let userTrueName = userInfo.userTrueName;
 
      //保存文件
      let rootPath = path.resolve();
      let fileName = `${userTrueName}_${user_Id}_${moment(new Date()).format("YYYYMMDDHHmmss")}.json`;
      let filePath =
        rootPath +
        path.sep +
        "upload" +
        path.sep +
        "PDA" +
        path.sep +
        moment(new Date()).format("YYYYMMDD") +
        path.sep +
        fileName;
      let pathToCreate = filePath.substring(0, filePath.lastIndexOf(path.sep));
      ctx.helper.mkdir(pathToCreate);
 
      // file not required
      const stream = await ctx.getFileStream({ requireFile: false });
      if (stream.filename) {
        let writeStrem = fs.createWriteStream(filePath);
        await stream.pipe(writeStrem);
      } else {
        const sendToWormhole = require("stream-wormhole");
        // must consume the empty stream
        await sendToWormhole(stream);
      }
 
      try {
        var jsonData = fs.readFileSync(filePath);
        var fileData = JSON.parse(jsonData.toString());
        var msg = "";
        var idList: Array<number> = [];
        let total = 0; // 共计
        let existTotal = 0; //已经上传的
        let newTotal = 0; //已经上传的
        for (var dataInfo of fileData.dataList) {
          total++;
          newTotal++;
          let wayBillCode = dataInfo.wayBillCode;
          var receive = await this.dbRead.findOne(TMSWayBillReceiveTemp, {
            wayBillCode: wayBillCode
          });
          if (!receive) {
            receive = new TMSWayBillReceiveTemp();
            receive.collectStatus = "已揽收";
            receive.wayBillCode = wayBillCode;
            receive.storage_Id = dataInfo.storage_Id;
            receive.storageName = dataInfo.storageName;
            receive.consignor_Id = dataInfo.consignor_Id;
            receive.consignorName = dataInfo.consignorName;
            receive.collectDate = dataInfo.createDate;
            receive.createDate = new Date();
            receive.user_Id = user_Id;
            receive.userTrueName = userTrueName;
            receive.status = "未处理";
            await this.dbWrite.save(receive);
          }
 
          idList.push(dataInfo.rowId);
        }
 
        var sql = "EXEC dbo.sp_PdaUploadData @user_Id = " + user_Id + ", @userTrueName = N'" + userTrueName + "'";
        await this.dbWrite.query(sql);
 
        this.info.result = true;
        this.info.msg = msg;
        this.info.dynamic = idList;
        this.info.data = {
          total: total,
          existTotal: existTotal,
          newTotal: newTotal
        };
      } catch (ex) {
        this.info.result = false;
        this.info.msg = ex.message;
      }
 
      ctx.body = this.info;
      return;
    } catch (ex) {
      this.info.result = false;
      this.info.msg = ex.message;
 
      ctx.body = this.info;
      return;
    }
  }
  //#endregion
 
  //#region  Return
  /**
   * 揽收直接退货
   */
  @Post()
  public async return() {
    let { ctx } = this;
    let body = ctx.request.body;
 
    if (!Array.isArray(body.wayBillCodes) || !body.wayBillCodes.length) {
      this.info.result = false;
      this.info.msg = "无可执行的数据";
      ctx.body = this.info;
      return;
    }
    try {
      await this.dbWrite.update(
        TMSWayBillUpload,
        {
          wayBillCode: In(body.wayBillCodes)
        },
        {
          orderStatus: "已退货"
        }
      );
      await this.dbWrite.update(
        TMSWayBillReceive,
        {
          wayBillCode: In(body.wayBillCodes)
        },
        {
          orderStatus: "已退货",
          collectStatus: "已退货"
        }
      );
 
      var receiveList = await this.dbRead.find(TMSWayBillReceive, {
        wayBillCode: In(body.wayBillCodes)
      });
      for (var receiveInfo of receiveList) {
        var wayInfo = await this.dbRead.findOne(TMSWayBill, {
          wayBillCode: receiveInfo.wayBillCode
        });
        if (wayInfo != null) {
          //添加运单追踪记录
          await ctx.service.tms.wayBillHelper.setStatusHistory(wayInfo, "运单退货", "已退货", "已退货");
        } else {
          wayInfo = new TMSWayBill();
          wayInfo = Object.assign(wayInfo, receiveInfo);
          wayInfo.orderStatus = receiveInfo.collectStatus;
 
          //添加运单追踪记录
          await ctx.service.tms.wayBillHelper.setStatusHistory(wayInfo, "运单退货", "已退货", "已退货");
        }
      }
 
      this.info.result = true;
      this.info.msg = "退货成功;";
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "异常" + ex;
    }
    ctx.body = this.info;
    return;
  }
  //#endregion
}