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
|
}
|