import BaseService from "./baseService";
|
import { BaseProductInfo } from "../entity/basicInfo/base/baseProductInfo";
|
import { TMSWayBill } from "../entity/express/tms/tmsWayBill";
|
import { TMSIdCard } from "../entity/express/tms/tmsIdCard";
|
import moment = require("moment");
|
import { TMSWayBillList } from "../entity/express/tms/tmsWayBillList";
|
|
/**
|
* 作业任务服务
|
*/
|
export default class TaskService extends BaseService {
|
//#region redisInitProduct 初始化物料信息
|
/**
|
* redisInitProduct 初始化物料信息
|
*/
|
public async redisInitProduct() {
|
let { ctx } = this;
|
let redis = ctx.app.redis.clients.get("product");
|
var clear = ctx.request.query.clear;
|
//初始化Redis
|
if (clear == "1") {
|
await redis.flushdb();
|
clear = "全部清除缓存并重新缓存";
|
} else {
|
clear = "只追加缓存";
|
}
|
|
// 更新点击量
|
var sql = `UPDATE dbo.Base_ProductInfo SET ClickCount=(SELECT COUNT(1) FROM dbo.TMS_WayBillList
|
WHERE ProductCode=dbo.Base_ProductInfo.ProductCode)
|
`;
|
await this.dbWrite.query(sql);
|
var dataList = await this.dbWrite.find(BaseProductInfo, {
|
select: [
|
"product_Id",
|
"productCode",
|
"productModel",
|
"productName",
|
"consignor_Id",
|
"consignorCode",
|
"consignorName",
|
"brand_Id",
|
"brandName",
|
"type_Id",
|
"typeName",
|
"productSpec",
|
"weight",
|
"netWeight",
|
"salePrice",
|
"smallUnit",
|
"clickCount",
|
"relationCode2",
|
"relationCode3",
|
"relationCode4"
|
]
|
});
|
for (var item of dataList) {
|
item.clickCount = parseInt("" + item.clickCount) || 0;
|
let data = JSON.stringify(item);
|
redis.set(item.productCode.toLocaleLowerCase(), data);
|
redis.set(item.productModel.toLocaleLowerCase(), data);
|
redis.set(item.productName.toLocaleLowerCase(), data);
|
if (item.relationCode2) {
|
redis.set(item.relationCode2.toLocaleLowerCase(), data);
|
}
|
if (item.relationCode3) {
|
redis.set(item.relationCode3.toLocaleLowerCase(), data);
|
}
|
if (item.relationCode4) {
|
redis.set(item.relationCode4.toLocaleLowerCase(), data);
|
}
|
}
|
|
this.info.result = true;
|
this.info.msg = "执行完成," + clear;
|
return this.info;
|
}
|
//#endregion
|
|
//#region 运单收货人电话更新到身份证库中
|
/**
|
* 运单收货人电话更新到身份证库中
|
*/
|
public async updateIdCardTel() {
|
let log = this.ctx.getLogger("taskLogger");
|
log.info(`更新手机号到身份证库开始`);
|
try {
|
let where = `exists
|
(
|
Select 1 from TMS_WayBillReceive Where WayBillCode=t.WayBillCode And ConsigneeIdcard IS NOT NULL AND CollectStatus='已揽收' And
|
CreateDate>=DATEADD(DAY, -5, GETDATE())
|
)`;
|
//where = "wayBillCode='2002505096'";
|
where = `
|
NOT EXISTS(SELECT 1 FROM dbo.TMS_IdCard C WHERE C.Mobile=t.ConsigneeMobile OR C.Mobile2=t.ConsigneeMobile
|
OR C.Mobile3=t.ConsigneeMobile)
|
and t.ConsigneeMobile IS NOT NULL
|
and ConsigneeIdcard is NOT NULL
|
`;
|
let wayBillList = await this.dbRead
|
.createQueryBuilder(TMSWayBill, "t")
|
.select(["wayBillCode", "orderStatus", "consigneeName", "consigneeMobile", "consigneeIdcard"])
|
.where(where)
|
.getRawMany();
|
for (let wayBillInfo of wayBillList) {
|
//#region 身份证处理
|
let _idCard = await this.dbRead.findOne(TMSIdCard, {
|
idCardCode: wayBillInfo.consigneeIdcard
|
});
|
let mobile = wayBillInfo.consigneeMobile;
|
log.info(`${wayBillInfo.wayBillCode}更新手机号${mobile}到身份证库${wayBillInfo.consigneeIdcard}中开始`);
|
if (_idCard) {
|
// 手机号已存在不再更新
|
if (_idCard.mobile === mobile || _idCard.mobile2 === mobile || _idCard.mobile3 === mobile) {
|
log.info(`${wayBillInfo.wayBillCode}更新手机号${mobile}到身份证库${wayBillInfo.consigneeIdcard}中已存在`);
|
continue;
|
}
|
|
if (!_idCard.mobile) {
|
// 第一个手机为空先更新到第一个手机号
|
_idCard.mobile = mobile;
|
log.info(`${wayBillInfo.wayBillCode}更新手机号${mobile}到身份证库${wayBillInfo.consigneeIdcard}中第1个字段`);
|
} else if (!_idCard.mobile2) {
|
// 第2个手机为空更新到第2个手机号
|
_idCard.mobile2 = mobile;
|
log.info(`${wayBillInfo.wayBillCode}更新手机号${mobile}到身份证库${wayBillInfo.consigneeIdcard}中第2个字段`);
|
} else if (!_idCard.mobile3 != mobile) {
|
// 第3个手机为空更新到第3个手机号
|
_idCard.mobile3 = mobile;
|
log.info(`${wayBillInfo.wayBillCode}更新手机号${mobile}到身份证库${wayBillInfo.consigneeIdcard}中第3个字段`);
|
}
|
await this.dbWrite.save(_idCard);
|
}
|
}
|
let msg = `更新手机号到身份证库结束`;
|
log.info(msg);
|
this.info.result = true;
|
this.info.msg = msg;
|
//#endregion
|
} catch (error) {
|
let msg = `更新手机号到身份证库失败:${error.message}`;
|
this.info.result = false;
|
this.info.msg = msg;
|
log.info(msg);
|
}
|
|
return this.info;
|
}
|
//#endregion
|
|
//#region checkWayBill
|
public async checkWayBill() {
|
let startDate = new Date();
|
let isExistModify = false;
|
let db = this.app.mongodb;
|
let collWayBill = db.collection("tmsWayBill");
|
let collWayBillCheck = db.collection("tmsWayBillCheck");
|
|
//#region 校验订单,将不正确的订单明细矫正过来
|
try {
|
let currDate = moment(new Date()).add("M", -5).format("YYYY-MM-DD HH:mm:ss");
|
let wayBillList = await collWayBill
|
.find({
|
state: "审核成功",
|
finished: null,
|
createDate: { $lt: currDate } // 查找5分钟前的数据
|
})
|
.limit(50)
|
.toArray();
|
for (let wayInfo of wayBillList) {
|
let msgList = [];
|
let wayBillCode = wayInfo.wayBillUploadInfo.wayBillCode;
|
if (!wayBillCode) {
|
collWayBill.update(
|
{
|
_id: wayInfo._id
|
},
|
{
|
$set: {
|
finished: "已验证,单号不存在"
|
}
|
}
|
);
|
continue;
|
}
|
let _wayBillInfo = await this.dbRead.findOne(TMSWayBill, {
|
wayBillCode: wayBillCode
|
});
|
if (_wayBillInfo != null) {
|
let _detailList = await this.dbRead.find(TMSWayBillList, {
|
wayBill_Id: _wayBillInfo.wayBill_Id
|
});
|
|
// 明细不存在的
|
for (let a of _detailList) {
|
let exist = wayInfo.wayBillDetails.some(y => y.productCode == a.productCode && y.quantityOrder == a.quantityOrder);
|
if (!exist) {
|
msgList.push(wayBillCode + ":多余明细,条码" + a.productModel);
|
this.dbWrite.delete(TMSWayBillList, a.wayBillList_Id);
|
isExistModify = true;
|
}
|
}
|
|
// 未增加的
|
for (let a of wayInfo.wayBillDetails) {
|
if (!a.productCode) {
|
msgList.push(wayBillCode + ":新增明细,条码不存在");
|
continue;
|
}
|
|
let exist = _detailList.some(y => y.productCode == a.productCode && y.quantityOrder == a.quantityOrder);
|
if (!exist) {
|
//查询物料信息
|
let dataInfo = await this.dbRead.findOne(BaseProductInfo, {
|
where: [
|
{
|
productCode: a.productCode
|
},
|
{ productModel: a.productCode }
|
]
|
});
|
|
//添加到TMS_WayBillList表
|
let listInfo = new TMSWayBillList();
|
listInfo.wayBill_Id = _wayBillInfo.wayBill_Id;
|
listInfo.product_Id = dataInfo.product_Id;
|
listInfo.productName = dataInfo.productName;
|
listInfo.productCode = dataInfo.productCode;
|
listInfo.productModel = dataInfo.productModel;
|
listInfo.productSpec = dataInfo.productSpec;
|
listInfo.quantityOrder = a.quantityOrder;
|
listInfo.smallUnit = a.smallUnit;
|
listInfo.salePrice = a.salePrice || 0;
|
listInfo.rowTotal = a.quantityOrder * (a.salePrice || 0);
|
listInfo.weight = a.weight;
|
listInfo.grossWeight = a.weight + 0.1; //以前是加100
|
await this.dbWrite.insert(TMSWayBillList, listInfo);
|
msgList.push(wayBillCode + ":新增明细,条码" + dataInfo.productModel);
|
|
isExistModify = true;
|
}
|
}
|
|
if (isExistModify) {
|
let sql = `UPDATE dbo.TMS_WayBill
|
SET TotalQuantityOrder=(SELECT SUM(L.quantityOrder) FROM dbo.TMS_WayBillList L WHERE L.wayBill_Id=TMS_WayBill.wayBill_Id)
|
WHERE TMS_WayBill.wayBill_Id=${_wayBillInfo.wayBill_Id}`;
|
await this.dbWrite.query(sql);
|
msgList.push(wayBillCode + ":重新修改数量");
|
}
|
} else {
|
msgList.push(wayBillCode + ":在系统中不存在");
|
}
|
|
collWayBill.update(
|
{
|
_id: wayInfo._id
|
},
|
{
|
$set: {
|
finished: "已验证"
|
}
|
}
|
);
|
|
// 记录日志
|
if (msgList.length) {
|
await collWayBillCheck.insertOne({
|
wayBillCode: wayBillCode,
|
createDate: new Date(),
|
msgList: msgList
|
});
|
}
|
}
|
let seconds = moment(new Date()).diff(startDate, "seconds");
|
let msg = "重新校验运单共执行:" + wayBillList.length + ",执行时长(s):" + seconds;
|
this.info.result = true;
|
this.info.msg = msg;
|
} catch (ex) {
|
let msg = "重新校验运单共执行失败:" + ex.message;
|
this.info.result = false;
|
this.info.msg = msg;
|
}
|
//#endregion
|
return this.info;
|
}
|
//#endregion
|
}
|