import { default as BaseController } from "../baseController";
|
import { Post } from "egg-shell-decorators";
|
import * as mssql 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";
|
import { BaseStorage } from "../../entity/basicInfo/base/baseStorage";
|
import { StorageOuter } from "../../entity/storage/operation/storageOuter";
|
import { In } from "typeorm";
|
|
export default class OuterController extends BaseController {
|
//#region 开始分拣
|
@Post()
|
public async startSorting() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let userInfo = await ctx.helper.userInfo();
|
|
try {
|
if (body.outer_Id) {
|
const connection: any = await this.dbWrite.connection;
|
let request = new mssql.Request(connection.driver.master);
|
request.input("outer_Id", body.outer_Id);
|
request.input("user_Id", userInfo.user_Id);
|
request.output("outMsg", mssql.NVarChar(2000));
|
let result = await request.execute("sp_Storage_Outer_Sorting");
|
let outMsg = result.output.outMsg;
|
if (outMsg) {
|
this.info.msg = outMsg;
|
this.info.result = false;
|
this.ctx.body = this.info;
|
return;
|
}
|
}
|
this.info.msg = "分拣完毕";
|
this.info.result = true;
|
this.ctx.body = this.info;
|
} catch (ex) {
|
this.info.msg = ex.message + "数据分拣失败请重试!";
|
this.info.result = false;
|
this.ctx.body = this.info;
|
}
|
}
|
//#endregion
|
|
//#region 确认出库
|
@Post()
|
public async onConfirm() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let userInfo = await ctx.helper.userInfo();
|
|
try {
|
if (body.outer_Id) {
|
const connection: any = await this.dbWrite.connection;
|
let request = new mssql.Request(connection.driver.master);
|
|
request.input("outer_Id", body.outer_Id);
|
request.input("user_Id", userInfo.user_Id);
|
request.input("userTrueName", userInfo.userTrueName);
|
await request.execute("sp_Storage_Outer_Check");
|
}
|
this.info.msg = "审核成功";
|
this.info.result = true;
|
this.ctx.body = this.info;
|
} catch (ex) {
|
this.info.msg = ex.message + "数据分拣失败请重试!";
|
this.info.result = false;
|
this.ctx.body = this.info;
|
}
|
}
|
//#endregion
|
|
//#region 导入Excel
|
@Post()
|
public async importExcel() {
|
setTimeout(async () => {
|
await this.importExcelWork();
|
}, 0);
|
|
this.info.result = true;
|
this.info.msg = "开始上传...";
|
this.ctx.body = this.info;
|
}
|
|
private async importExcelWork() {
|
let { ctx } = this;
|
let userInfo = await this.userInfo;
|
let body = ctx.request.body;
|
let redis = ctx.app.redis.clients.get("common"); // 将消息放入redis缓存
|
let fileUrl = body.fileUrl;
|
let startDate = new Date();
|
redis.expire(body.key, 5 * 60);
|
if (!fileUrl) {
|
this.setMsg("上传文件不存在", "red");
|
return;
|
}
|
if (!body.key) {
|
this.setMsg("上传key不存在", "red");
|
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;
|
}
|
//#endregion
|
|
this.isMsgError = false;
|
let msg = "";
|
|
let excelDT = new mssql.Table();
|
excelDT.columns.add("ConsignorName", mssql.NVarChar(50));
|
excelDT.columns.add("StorageName", mssql.NVarChar(50));
|
excelDT.columns.add("ClientShortName", mssql.NVarChar(50));
|
excelDT.columns.add("ProductCode", mssql.NVarChar(50));
|
excelDT.columns.add("ProductName", mssql.NVarChar(50));
|
excelDT.columns.add("Quantity", mssql.Int);
|
excelDT.columns.add("PositionName ", mssql.NVarChar(50));
|
excelDT.columns.add("PlateCode", mssql.NVarChar(50));
|
excelDT.columns.add("BatchNumber", mssql.NVarChar(50));
|
excelDT.columns.add("ProductSpec", mssql.NVarChar(50));
|
excelDT.columns.add("userTrueName", mssql.NVarChar(50));
|
excelDT.columns.add("weight", mssql.Decimal(12, 4));
|
excelDT.columns.add("totalWeight", mssql.Decimal(12, 4));
|
|
let i = 0;
|
for (var dataItem of dataList) {
|
i++;
|
if (!isNumber(dataItem["数量"])) {
|
this.setMsg(`${i}、第${i + 1}行,数量值【${dataItem["数量"]}】格式错误!`, "red");
|
return;
|
}
|
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");
|
return;
|
}
|
//#region 校验数据
|
var storageName = dataItem["仓库名称"];
|
let typeInfo = await this.dbRead.findOne(BaseStorage, {
|
userProduct_Id: userInfo.userProduct_Id,
|
storageName: storageName
|
});
|
if (!typeInfo) {
|
this.setMsg(`${i}、第${i}行、${storageName}仓库不存在,请核实信息!`, "red");
|
return;
|
}
|
//#endregion
|
|
excelDT.rows.add(
|
dataItem["货主名称"],
|
dataItem["仓库名称"],
|
dataItem["客户名称"],
|
dataItem["物料编号"],
|
dataItem["物料名称"],
|
dataItem["数量"],
|
dataItem["出库货位"],
|
dataItem["拍号"],
|
dataItem["批次号"],
|
dataItem["物料规格"] || prodInfo.productSpec,
|
dataItem["经手人"],
|
dataItem["单位毛重"],
|
dataItem["小计毛重"]
|
);
|
}
|
if (this.isMsgError) {
|
this.setMsg(`导入数据有错误,请处理好重新导入`, "red");
|
this.setMsg("-1");
|
return;
|
}
|
|
const connection: any = await this.dbWrite.connection;
|
let request = new mssql.Request(connection.driver.master);
|
request.input("user_Id", userInfo.user_Id);
|
request.input("excelDT", excelDT);
|
request.output("outMsg", mssql.NVarChar(2000));
|
let result = await request.execute("sp_Storage_Outer_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
|
|
//#region 记录打印次数
|
@Post()
|
public async print() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
if (!Array.isArray(body.ids)) {
|
this.info.result = false;
|
this.info.msg = "数据ID不正确";
|
this.ctx.body = this.info;
|
return;
|
}
|
|
try {
|
// 对字段printCount进行加1
|
for (let id of body.ids) {
|
let outerInfo = await this.dbRead.findOne(StorageOuter, {
|
outer_Id: id
|
});
|
if (outerInfo) {
|
await this.dbWrite.update(
|
StorageOuter,
|
{
|
outer_Id: In(body.ids)
|
},
|
{ printCount: (outerInfo.printCount || 0) + 1 }
|
);
|
}
|
}
|
this.info.result = true;
|
this.ctx.body = this.info;
|
} catch (ex) {
|
this.info.msg = ex.message + "数据分拣失败请重试!";
|
this.info.result = false;
|
this.ctx.body = this.info;
|
}
|
}
|
//#endregion
|
}
|