import { default as BaseController } from "../baseController";
|
import { Post } from "egg-shell-decorators";
|
import { BasePosition } from "../../entity/basicInfo/base/basePosition";
|
import * as XLSX from "xlsx";
|
import * as path from "path";
|
import * as mssql from "mssql";
|
import { StoragePositionTransferList } from "../../entity/storage/check/storagePositionTransferList";
|
|
export default class PositionTransferController 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 storage_Id = body.storage_Id;
|
var productModelList = body.productModelList;
|
var originalPositionName = body.originalPositionName; // 原货位
|
var targetPositionName = body.targetPositionName; // 目标货位
|
|
if (!originalPositionName || !targetPositionName) {
|
this.info.msg = "原货位或目标货位不能为空<br/>";
|
this.info.result = false;
|
ctx.body = this.info;
|
return this.info;
|
}
|
//查询原货位是否存在
|
let pInfo = await this.dbRead.findOne(BasePosition, {
|
userProduct_Id: userInfo.userProduct_Id,
|
storage_Id: body.storage_Id,
|
positionName: originalPositionName
|
});
|
if (!pInfo) {
|
this.info.msg = originalPositionName + "原货位不正确无法操作<br/>";
|
this.info.result = false;
|
ctx.body = this.info;
|
return this.info;
|
}
|
// 查询目标货位是否存在
|
let bInfo = await this.dbRead.findOne(BasePosition, {
|
userProduct_Id: userInfo.userProduct_Id,
|
storage_Id: body.storage_Id,
|
positionName: targetPositionName
|
});
|
if (!bInfo) {
|
this.info.msg = targetPositionName + "目标货位不正确或已经停用无法操作<br/>";
|
this.info.result = false;
|
ctx.body = this.info;
|
return 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;
|
}
|
return 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
|
ConsignorCode AS '货主编号',ConsignorName AS '货主名称',positionName AS '货位名称',ProductCode AS '物料编号',ProductName AS '物料名称',
|
ProductModel AS '条形码',ProductSpec AS '物料规格',TransferQuantity AS '转移数量',LackStorage AS '缺货数量',PurchasePrice AS '进货价',
|
SubTotalMoney AS '金额',PositionName_In AS '转移货位',BatchNumber AS '批次号',ProduceDate AS '生产日期',Remark AS '备注',StorageStatus AS '库存状态'
|
FROM Storage_PositionTransferList
|
WHERE TransferList_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);
|
|
// wayBillList = wayBillList.splice(0, 0, {
|
// "分运单号":"xxxxxx"
|
// }, {"申报类型": "xxxxsssss"}, {});
|
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 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 product_Id = row["物料ID"];
|
let productCode = row["物料编号"];
|
let productName = row["物料名称"];
|
// let productCode = row["物料编号"];
|
if (!positionName) {
|
msg += `${i}、货位名称不能为空`;
|
redis.rpush(body.key, msg);
|
this.info.result = false;
|
}
|
if (!product_Id) {
|
msg += `${i}、物料ID不能为空`;
|
redis.rpush(body.key, msg);
|
this.info.result = false;
|
}
|
if (!productCode) {
|
msg += `${i}、物料编号不能为空`;
|
redis.rpush(body.key, msg);
|
this.info.result = false;
|
}
|
if (!productName) {
|
msg += `${i}、物料名称不能为空`;
|
redis.rpush(body.key, msg);
|
this.info.result = false;
|
}
|
// #endregion
|
|
if (msg) {
|
this.info.result = false;
|
this.info.msg = msg;
|
} else {
|
await this.dbWrite.save(StoragePositionTransferList, {
|
transfer_Id: body.transfer_Id,
|
consignorCode: row["货主编号"],
|
consignorName: row["货主名称"],
|
positionName: row["货位名称"],
|
product_Id: row["物料ID"],
|
productCode: row["物料编号"],
|
productName: row["物料名称"],
|
productModel: row["条形码"],
|
productSpec: row["物料规格"],
|
transferQuantity: row["转移数量"],
|
lackStorage: row["缺货数量"],
|
purchasePrice: row["进货价"],
|
subTotalMoney: row["金额"],
|
positionName_In: row["转移货位"],
|
batchNumber: row["批次号"],
|
produceDate: row["生产日期"],
|
remark: row["备注"],
|
storageStatus: row["库存状态"]
|
});
|
|
this.info.result = true;
|
this.info.msg = "导入成功";
|
}
|
}
|
} catch (ex) {
|
this.info.msg = "出现异常:" + ex.message;
|
this.info.result = false;
|
}
|
|
ctx.body = this.info;
|
}
|
|
// #endregion
|
|
// #region 审核
|
/// <summary>
|
/// 货位转移审核
|
/// </summary>
|
/// <param name="loadInfo"></param>
|
/// <returns></returns>
|
@Post()
|
public async auditing() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let userInfo = await ctx.helper.userInfo();
|
let outMsgs = null;
|
try {
|
if (body.transfer_Id.length > 0) {
|
const connection: any = await this.dbWrite.connection;
|
let request = new mssql.Request(connection.driver.master);
|
request.input("Transfer_Id", body.transfer_Id);
|
request.input("User_Id", userInfo.user_Id);
|
request.input("Menu_Id", 450);
|
request.output("outMsg", mssql.NVarChar(2000));
|
let result = await request.execute("sp_Storage_PositionTransfer_Check");
|
outMsgs = result.output.outMsg;
|
if (outMsgs != null && outMsgs) {
|
this.info.result = false;
|
this.info.msg = outMsgs;
|
} else {
|
this.info.result = true;
|
this.info.msg = "确认转移成功";
|
}
|
} else {
|
this.info.result = false;
|
this.info.msg = "信息不存在";
|
}
|
} catch (ex) {
|
this.info.msg = ex.message + "审核失败!";
|
this.info.result = false;
|
this.ctx.body = this.info;
|
}
|
|
ctx.body = this.info;
|
}
|
// #endregion
|
}
|