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
import BaseController from "../baseController";
import { Post } from "egg-shell-decorators";
import moment = require("moment");
import * as path from "path";
import * as fs from "fs";
import { TMSWayBillReceiveTemp } from "../../entity/express/tms/tmsWayBillReceiveTemp";
import * as mssql from "mssql";
import * as sendToWormhole from "stream-wormhole";
const sendToWormhole = require("stream-wormhole");
const pump = require("mz-modules/pump");
 
export default class PdaController extends BaseController {
  //#region uploadWayBillReceive 运单揽收上传
  @Post()
  public async uploadWayBillReceive() {
    let { ctx } = this;
    try {
      //获取参数信息
      let user_Id = this.body.user_Id;
      let userTrueName = this.body.userTrueName;
      let rootPath = path.resolve();
 
      //保存文件
      let fileName;
      let dirPath;
      let filePath;
      const parts = ctx.multipart();
 
      try {
        let part;
        while ((part = await parts()) != null) {
          if (part.length) {
            if (part[0] == "user_Id") {
              user_Id = part[1];
            }
            if (part[0] == "userTrueName") {
              userTrueName = part[1];
            }
          } else {
            try {
              if (!part.filename) {
                continue;
              }
              fileName = `${userTrueName}_${user_Id}_${moment(new Date()).format("YYYYMMDDHHmmss")}.json`;
              dirPath = rootPath + `${path.sep}upload${path.sep}pda`;
              filePath = `${dirPath}${path.sep}${fileName}`;
              ctx.helper.mkdir(dirPath);
 
              // otherwise, it's a stream
              let writeStream = fs.createWriteStream(filePath);
              await pump(part, writeStream);
            } catch (error) {
              await sendToWormhole(part);
            }
          }
        }
        ctx.cleanupRequestFiles();
 
        var jsonData = await fs.readFileSync(filePath);
        var fileData = JSON.parse(jsonData.toString());
        var msg = "";
        var idList = new Array<number>();
        let total = 0; // 共计
        let existTotal = 0; //已经上传的
        let newTotal = 0; //已经上传的
        for (var dataInfo of fileData.dataList) {
          total++;
          newTotal++;
          let wayBillCode: string = dataInfo.wayBillCode;
          wayBillCode = wayBillCode.trim();
          var receive = await this.dbRead.findOne(TMSWayBillReceiveTemp, {
            wayBillCode: wayBillCode
          });
          if (receive == null) {
            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.consignorCode = dataInfo.consignorCode;
            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);
        }
 
        const connection: any = await this.dbWrite.connection;
        let request = new mssql.Request(connection.driver.master);
        request.input("userTrueName", userTrueName);
        request.input("user_Id", user_Id);
        await request.execute("sp_PdaUploadData");
 
        this.info.result = true;
        this.info.msg = msg;
        this.info.dynamic = idList;
        this.info.data = {
          total: total,
          existTotal: existTotal,
          newTotal: newTotal
        };
 
        // 删除垃圾文件
        ctx.helper.deldir(dirPath, 5);
      } catch (ex) {
        this.info.result = false;
        this.info.msg = ex.message;
      }
    } catch (ex) {
      this.info.result = false;
      this.info.msg = ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
}