import { default as BaseController } from "../baseController";
|
import { Post } from "egg-shell-decorators";
|
import { StorageEnter } from "../../entity/storage/operation/storageEnter";
|
import { StorageEnterList } from "../../entity/storage/operation/storageEnterList";
|
import { BasePosition } from "../../entity/basicInfo/base/basePosition";
|
import * as sql from "mssql";
|
import * as path from "path";
|
import * as XLSX from "xlsx";
|
import moment = require("moment");
|
import { isNumber } from "util";
|
import { BaseProductInfo } from "../../entity/basicInfo/base/baseProductInfo";
|
|
export default class EnterController extends BaseController {
|
//#region 其他入库审核
|
@Post()
|
public async singleConfirm() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let userInfo = await ctx.helper.userInfo();
|
let enter_Ids = body.enter_Ids;
|
try {
|
for (let data of enter_Ids) {
|
let storageEnterInfo = await this.dbRead.findOne(StorageEnter, {
|
enter_Id: data
|
});
|
let checkStorageEnterList = await this.dbRead.find(StorageEnterList, {
|
enter_Id: data
|
});
|
let errorMsg = "";
|
for (let mInfo of checkStorageEnterList) {
|
let where: any = {
|
storage_Id: storageEnterInfo.enter_Id,
|
positionName: mInfo.positionName,
|
enable: 1
|
};
|
let basePosition = await this.dbRead.find(BasePosition, where);
|
if (!basePosition) {
|
errorMsg += mInfo.positionName + "货位不正确或已经停用无法操作<br/>";
|
}
|
}
|
|
if (errorMsg) {
|
this.info.result = false;
|
this.info.msg = errorMsg;
|
ctx.body = this.info;
|
}
|
const connection: any = await this.dbWrite.connection;
|
let request = new sql.Request(connection.driver.master);
|
request.input("enter_Id", data);
|
request.input("modifier", userInfo.userTrueName);
|
request.output("outMsg", sql.NVarChar(2000));
|
let result = await request.execute("sp_Storage_Enter_Check");
|
let msg = result.output.outMsg;
|
|
let enterinfo = await this.dbRead.findOne(StorageEnter, {
|
enter_Id: data
|
});
|
if (!msg) {
|
this.info.result = true;
|
this.info.msg = "确认成功!";
|
} else {
|
this.info.result = false;
|
await this.dbWrite.update(StorageEnter, enterinfo.enter_Id, {
|
auditor: userInfo.userTrueName,
|
auditing: 3,
|
auditDate: new Date()
|
});
|
this.info.msg = "执行失败!" + msg;
|
}
|
}
|
ctx.body = this.info;
|
} catch (error) {
|
this.info.result = false;
|
this.info.msg = "执行失败!" + error.message;
|
ctx.body = this.info;
|
}
|
}
|
// #endregion
|
|
//#region 导入Excel
|
@Post()
|
public async importExcel() {
|
setTimeout(async () => {
|
await this.importExcelWork();
|
}, 0);
|
|
this.info.result = true;
|
this.ctx.body = this.info;
|
}
|
|
private async importExcelWork() {
|
let userInfo = await this.userInfo;
|
let startDate = new Date();
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let redis = ctx.app.redis.clients.get("common"); // 将消息放入redis缓存
|
let fileUrl = body.url;
|
redis.expire(body.key, 5 * 60);
|
if (!body.key) {
|
this.setMsg(body.key, "上传key不存在");
|
return;
|
}
|
if (!fileUrl) {
|
redis.rpush(body.key, "上传文件不存在");
|
return;
|
}
|
try {
|
let rootPath = path.resolve(); // 获得根目录
|
let filePath = rootPath + path.sep + fileUrl.replace(/\//gi, path.sep); // 上传文件路径
|
var workbook = XLSX.readFile(filePath); //整个 excel 文档
|
var sheetNames = workbook.SheetNames; //获取所有工作薄名
|
var sheet1 = workbook.Sheets[sheetNames[0]]; //根据工作薄名获取工作薄
|
let dataList: Array<any> = XLSX.utils.sheet_to_json(sheet1); // 获得当前sheet表单数据转为json格式
|
|
//#region 验证数据正确性
|
this.info.result = true;
|
if (!dataList.length) {
|
this.setMsg(`没有可导入的数据`, "red");
|
return;
|
}
|
this.isMsgError = false;
|
let msg = "";
|
|
let excelDT = new sql.Table();
|
excelDT.columns.add("ConsignorName", sql.NVarChar(50));
|
excelDT.columns.add("StorageName", sql.NVarChar(50));
|
excelDT.columns.add("ProviderShortName", sql.NVarChar(50));
|
excelDT.columns.add("ProductCode", sql.NVarChar(50));
|
excelDT.columns.add("ProductName", sql.NVarChar(50));
|
excelDT.columns.add("Quantity", sql.Int);
|
excelDT.columns.add("PositionName ", sql.NVarChar(50));
|
excelDT.columns.add("PlateCode", sql.NVarChar(50));
|
excelDT.columns.add("BatchNumber", sql.NVarChar(50));
|
excelDT.columns.add("ProduceDate", sql.DateTime);
|
excelDT.columns.add("ProductSpec", sql.NVarChar(50));
|
excelDT.columns.add("userTrueName", sql.NVarChar(50));
|
excelDT.columns.add("weight", sql.Decimal(12, 4));
|
excelDT.columns.add("totalWeight", sql.Decimal(12, 4));
|
|
let i = 0;
|
for (var dataItem of dataList) {
|
i++;
|
if (!isNumber(dataItem["数量"])) {
|
this.setMsg(`${i}、第${i + 1}行,数量值【${dataItem["数量"]}】格式错误!`, "red");
|
}
|
|
// 日期处理,日期格式为
|
let produceDate = dataItem["生产日期"];
|
if (produceDate) {
|
if (!isNumber(produceDate)) {
|
this.setMsg(`${i}、第${i + 1}行,生产日期值【${produceDate}】格式错误!`, "red");
|
}
|
let d = moment(new Date(1900, 0, produceDate - 1));
|
produceDate = d.toDate();
|
}
|
let productCode = dataItem["物料编号"];
|
let prodInfo = await this.dbRead.findOne(BaseProductInfo, {
|
productCode: productCode,
|
userProduct_Id: userInfo.userProduct_Id
|
});
|
if (!prodInfo) {
|
this.setMsg(`${i}、第${i + 1}行,物料编号【${productCode}】不存在!`, "red");
|
}
|
let weight = dataItem["单位毛重"] || prodInfo.weight;
|
let totalWeight = weight * dataItem["数量"];
|
excelDT.rows.add(
|
dataItem["货主名称"],
|
dataItem["仓库名称"],
|
dataItem["供应商"],
|
dataItem["物料编号"],
|
dataItem["物料名称"],
|
dataItem["数量"],
|
dataItem["货位名称"],
|
dataItem["拍号"],
|
dataItem["批次号"],
|
produceDate,
|
dataItem["物料规格"] || prodInfo.productSpec,
|
dataItem["经手人"],
|
weight,
|
totalWeight
|
);
|
}
|
if (this.isMsgError) {
|
this.setMsg(`导入数据有错误,请处理好重新导入`, "red");
|
this.setMsg("-1");
|
return;
|
}
|
|
const connection: any = await this.dbWrite.connection;
|
let request = new sql.Request(connection.driver.master);
|
request.input("user_Id", userInfo.user_Id);
|
request.input("excelDT", excelDT);
|
request.output("outMsg", sql.NVarChar(2000));
|
let result = await request.execute("sp_Storage_Enter_Import");
|
let outMsg = result.output.outMsg;
|
if (outMsg) {
|
this.setMsg(outMsg, "red");
|
} else {
|
outMsg = "入库单导入成功";
|
this.setMsg(outMsg, "blue");
|
}
|
|
let endDate = moment(Date.now());
|
let totalSeconds = endDate.diff(startDate, "seconds");
|
msg = `导入完成,共耗时${totalSeconds}秒`;
|
this.setMsg(msg);
|
} catch (ex) {
|
this.setMsg("出现异常:" + ex.message, "red");
|
}
|
|
this.setMsg("-1");
|
}
|
//#endregion
|
}
|