import { default as BaseController } from "../baseController";
|
import { Post } from "egg-shell-decorators";
|
import * as sql from "mssql";
|
import * as XLSX from "xlsx";
|
import * as path from "path";
|
// import moment = require("moment");
|
|
import { BasePosition } from "../../entity/basicInfo/base/basePosition";
|
import { StorageValidAdjust } from "../../entity/storage/validadjust/storageValidAdjust";
|
import { BaseProductInfo } from "../../entity/basicInfo/base/baseProductInfo";
|
import { StorageValidAdjustList } from "../../entity/storage/validadjust/storageValidAdjustList";
|
import { BaseProductPosition } from "../../entity/storage/product/baseProductPosition";
|
import moment = require("moment");
|
|
export default class StorageValidAdjustController extends BaseController {
|
//#region GetData
|
@Post()
|
public async getData() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
this.info = await ctx.service.storage.positionTransfer.getProductPositionGroupData(
|
body.originalPositionName,
|
body.targetPositionName,
|
body.storage_Id
|
);
|
if (!this.info.result) {
|
this.info.result = false;
|
this.info.msg = this.info.msg;
|
} else {
|
this.info.result = true;
|
this.info.dynamic = this.info.data;
|
}
|
ctx.body = this.info;
|
}
|
// #endregion
|
|
//#region 转移保存
|
|
@Post()
|
public async save() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let userInfo = await ctx.helper.userInfo();
|
let where: any = {};
|
let storage_Id = body.storage_Id;
|
var productModelList = body.productModelList;
|
|
let msg = "";
|
for (let mInfo of productModelList) {
|
(where.storage_Id = body.storage_Id), (where.originalPositionName = mInfo.originalPositionName);
|
let p = this.dbRead.find(BasePosition, { where });
|
if (p == null) {
|
msg += mInfo.originalPositionName + "原货位不正确无法操作<br/>";
|
}
|
(where.storage_Id = body.storage_Id), (where.originalPositionName = mInfo.originalPositionName);
|
where.Enable = 1;
|
p = this.dbRead.find(BasePosition, { where });
|
if (p == null) {
|
msg += mInfo.targetPositionName + "目标货位不正确或已经停用无法操作<br/>";
|
}
|
}
|
if (msg) {
|
this.info.result = false;
|
this.info.msg = msg;
|
ctx.body = this.info;
|
}
|
|
this.info = await ctx.service.storage.positionTransfer.storagePositionTransferSave(storage_Id, productModelList, userInfo.user_Id);
|
if (!this.info.result) {
|
this.info.result = false;
|
this.info.msg = this.info.msg;
|
ctx.body = this.info;
|
} else {
|
this.info.result = true;
|
this.info.msg = this.info.msg;
|
ctx.body = this.info;
|
}
|
}
|
// #endregion
|
|
//#region ConfirmAdjust 效期确认修改
|
@Post()
|
public async confirmAdjust() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let userInfo = await ctx.helper.userInfo();
|
let id = body.id;
|
|
try {
|
if (!id) {
|
this.info.result = false;
|
this.info.msg = "未找到信息,id为0";
|
return this.info;
|
}
|
|
let adjustInfo = await this.dbRead.findOne(StorageValidAdjust, id);
|
|
const connection: any = await this.dbWrite.connection;
|
let request = new sql.Request(connection.driver.master);
|
request.input("ValidAdjust_Id", body.id);
|
request.input("User_Id", userInfo.user_Id);
|
request.output("outMsg", sql.NVarChar(2000));
|
let result = await request.execute("sp_Storage_ValidAdjust_Confirm");
|
let outMsg = result.output.outMsg;
|
if (outMsg) {
|
this.info.result = false;
|
this.info.msg = "【" + adjustInfo.validAdjustCode + "】调整失败";
|
} else {
|
this.info.result = true;
|
this.info.msg = "【" + adjustInfo.validAdjustCode + "】调整成功";
|
}
|
} catch (ex) {
|
this.info.result = false;
|
this.info.msg = "效期调整失败,原因:" + ex.message.ToString();
|
}
|
ctx.body = this.info;
|
}
|
//#endregion
|
//#region 效期信息调整明细导出
|
/// <summary>
|
/// 效期信息调整明细导出
|
/// </summary>
|
/// <param name="body"></param>
|
/// <returns></returns>
|
@Post()
|
public async exportList() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
|
try {
|
let sql = "";
|
sql = `SELECT positionName AS '货位名称',productStorage AS '库存量',placeholderStorage AS '占位量',
|
productCode AS '物料编号',productName AS '物料名称',productSpec AS '物料规格',productModel AS '物料条码',
|
weight AS '单位毛重', totalWeight AS '毛重合计',shelfLifeDay AS '保质期天数',produceDate AS '原生产日期',
|
limitDate AS '原到期日期',produceDate_Target AS '目标生产日期',limitDate_Target AS '目标到期日期',batchNumber AS '原批次号',batchNumber_Target AS '目标批次号'
|
FROM Storage_ValidAdjustList
|
WHERE validAdjustList_Id IN(select col from dbo.split(@0, ','))`;
|
|
let transferList: Array<any> = await this.dbRead.query(sql, [body.ids]);
|
|
let root = path.resolve();
|
let url = "/download/效期信息调整明细数据.xlsx";
|
let fileName = root + url.replace(/\//g, path.sep);
|
let pathToCreate = fileName.substring(0, fileName.lastIndexOf(path.sep));
|
ctx.helper.mkdir(pathToCreate);
|
|
let jsonWorkSheet = XLSX.utils.json_to_sheet(transferList);
|
// 构造workBook
|
let workBook = {
|
SheetNames: ["数据"],
|
Sheets: {
|
数据: jsonWorkSheet
|
}
|
};
|
XLSX.writeFile(workBook, fileName);
|
|
this.info.result = true;
|
this.info.data = {
|
url: url
|
};
|
this.info.msg = "导出成功!";
|
} catch (ex) {
|
this.info.result = false;
|
this.info.msg = "错误信息:" + ex.message;
|
}
|
ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region 导入库存调整Excel
|
/// <summary>
|
/// 导入Excel
|
/// </summary>
|
/// <returns></returns>
|
@Post()
|
public async importExel() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let userInfo = await ctx.helper.userInfo();
|
let redis = ctx.app.redis.clients.get("common"); // 将消息放入redis缓存
|
let fileUrl = body.url;
|
redis.expire(body.key, 5 * 60);
|
// if (!body.key) {
|
// redis.rpush(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 = XLSX.utils.sheet_to_json(sheet1); // 获得当前sheet表单数据转为json格式
|
|
//#region 验证数据正确性
|
this.info.result = true;
|
if (!dataList.length) {
|
redis.rpush(body.key, "没有可导入的数据");
|
return;
|
}
|
|
let msg = "";
|
let i = 0;
|
for (let row of dataList) {
|
i++;
|
let positionName = row["货位名称"];
|
let productStorage = row["库存量"];
|
let productCode = row["物料编号"];
|
let productModel = row["物料条码"];
|
let produceDate = row["原生产日期"];
|
let limitDate = row["原到期日期"];
|
let produceDate_Target = row["目标生产日期"];
|
let limitDate_Target = row["目标到期日期"];
|
if (!positionName) {
|
msg += `${i}、货位名称不能为空`;
|
redis.rpush(body.key, msg);
|
this.info.result = false;
|
}
|
if (!productCode) {
|
msg += `${i}、物料编号不能为空`;
|
redis.rpush(body.key, msg);
|
this.info.result = false;
|
}
|
if (!productStorage) {
|
msg += `${i}、库存量不能为空`;
|
redis.rpush(body.key, msg);
|
this.info.result = false;
|
}
|
if (!productModel) {
|
msg += `${i}、条形码不能为空`;
|
redis.rpush(body.key, msg);
|
this.info.result = false;
|
}
|
//主表
|
let dataInfo = await this.dbRead.findOne(StorageValidAdjust, {
|
validAdjust_Id: body.validAdjust_Id,
|
userProduct_Id: userInfo.userProduct_Id
|
});
|
//查询物料信息
|
let productinfo = await this.dbRead.findOne(BaseProductInfo, {
|
productModel: productModel,
|
productCode: productCode,
|
userProduct_Id: userInfo.userProduct_Id
|
});
|
if (!productinfo) {
|
msg += `${i}、物料信息不存在`;
|
redis.rpush(body.key, msg);
|
this.info.result = false;
|
}
|
//查询货位信息
|
let postiontinfo = await this.dbRead.findOne(BasePosition, {
|
userProduct_Id: userInfo.userProduct_Id,
|
storageName: dataInfo.storageName,
|
positionName: positionName
|
});
|
if (!postiontinfo) {
|
msg += `${i}、货位不正确,请确认是否属于本单仓库下的货位`;
|
redis.rpush(body.key, msg);
|
this.info.result = false;
|
}
|
// 时间格式转换
|
let pDate = moment(new Date(1900, 0, produceDate - 1));
|
produceDate = pDate.toDate();
|
// 时间格式转换
|
limitDate = moment(new Date(1900, 0, limitDate - 1));
|
limitDate = limitDate.toDate();
|
// 时间格式转换
|
produceDate_Target = moment(new Date(1900, 0, produceDate_Target - 1));
|
produceDate_Target = produceDate_Target.toDate();
|
// 时间格式转换
|
limitDate_Target = moment(new Date(1900, 0, limitDate_Target - 1));
|
limitDate_Target = limitDate_Target.toDate();
|
// 查询库存ID
|
let ppInfo = await this.dbRead.findOne(BaseProductPosition, {
|
userProduct_Id: userInfo.userProduct_Id,
|
storageName: dataInfo.storageName,
|
positionName: positionName,
|
productCode: productCode,
|
produceDate: pDate.format("YYYY-MM-DD HH:mm:ss"),
|
productModel: productModel
|
});
|
if (!ppInfo) {
|
msg += `${i}、物料库存不正确`;
|
redis.rpush(body.key, msg);
|
this.info.result = false;
|
}
|
|
if (msg) {
|
// #endregion
|
this.info.result = false;
|
this.info.msg = msg;
|
} else {
|
let _detail = new StorageValidAdjustList();
|
_detail.validAdjust_Id = body.validAdjust_Id;
|
_detail.createID = userInfo.user_Id;
|
_detail.creator = userInfo.userTrueName;
|
_detail.positionName = row["货位名称"];
|
_detail.product_Id = productinfo.product_Id;
|
_detail.productCode = productinfo.productCode;
|
_detail.productName = productinfo.productName;
|
_detail.productModel = productinfo.productModel;
|
_detail.productSpec = productinfo.productSpec;
|
_detail.weight = row["单位毛重"];
|
_detail.placeholderStorage = row["占位量"];
|
_detail.shelfLifeDay = row["保质期天数"];
|
_detail.produceDate = produceDate;
|
_detail.limitDate = limitDate;
|
_detail.produceDate_Target = produceDate_Target;
|
_detail.limitDate_Target = limitDate_Target;
|
_detail.batchNumber = row["原批次号"];
|
_detail.batchNumber_Target = row["目标批次号"];
|
_detail.remark = row["备注"];
|
_detail.productPosition_Id = ppInfo.productPosition_Id;
|
// _detail.productPosition_Id = 0;
|
await this.dbWrite.save(StorageValidAdjustList, _detail);
|
this.info.result = true;
|
this.info.msg = "导入成功";
|
}
|
}
|
} catch (ex) {
|
this.info.msg = "出现异常:" + ex.message;
|
this.info.result = false;
|
}
|
|
ctx.body = this.info;
|
}
|
|
// #endregion
|
}
|