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