import { default as BaseController } from "../baseController";
|
import { Post } from "egg-shell-decorators";
|
import { StorageAllocateApply } from "../../entity/storage/allocate/storageAllocateApply";
|
import * as sql from "mssql";
|
import * as XLSX from "xlsx";
|
import * as path from "path";
|
import { StorageAllocateApplyList } from "../../entity/storage/allocate/storageAllocateApplyList";
|
export default class AllocateApplyController extends BaseController {
|
//#region auditing
|
@Post()
|
public async auditing() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let userInfo = await ctx.helper.userInfo();
|
|
try {
|
let result = await this.dbWrite.update(StorageAllocateApply, body.allocateApply_Id, {
|
statusID: 2,
|
statusText: "确认调拨",
|
auditor: userInfo.userTrueName,
|
auditing: body.auditing,
|
auditDate: new Date()
|
});
|
if (result) {
|
this.info.result = true;
|
this.info.msg = "审核成功!";
|
} else {
|
this.info.result = false;
|
this.info.msg = "审核失败!";
|
}
|
} catch (error) {
|
this.info.result = false;
|
this.info.msg = "审核失败:" + error.message;
|
}
|
ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region Stop
|
@Post()
|
public async stop() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
try {
|
let sql = `
|
delete from Base_ProductPlaceHolder where ClassName='调拨出库' And MainID=@0;
|
Update Storage_AllocateApply Set StatusID=5, StatusText='终止', SortingStatus=1, SortingDate=null Where AllocateApply_Id=@0`;
|
await this.dbWrite.query(sql, [body.idValue]);
|
this.info.result = true;
|
this.info.msg = "终止成功!";
|
} catch (error) {
|
this.info.result = false;
|
this.info.msg = "终止失败:" + error.message;
|
}
|
ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region Open
|
@Post()
|
public async open() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
try {
|
let reVal = await this.dbWrite.update(StorageAllocateApply, body.idValue, {
|
statusID: 1,
|
statusText: "新建"
|
});
|
this.info.data = reVal;
|
this.info.result = true;
|
this.info.msg = "开启成功";
|
} catch (error) {
|
this.info.result = false;
|
this.info.msg = "开启失败!" + error.message;
|
}
|
|
ctx.body = this.info;
|
}
|
//#endregion
|
//#region 开始分拣
|
@Post()
|
public async startSorting() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let userInfo = await ctx.helper.userInfo();
|
if (!body.selectIDs) {
|
this.info.result = false;
|
this.info.msg = "数据不存在";
|
ctx.body = this.info;
|
return;
|
}
|
try {
|
// 执行分拣
|
const connection: any = await this.dbWrite.connection;
|
let request = new sql.Request(connection.driver.master);
|
request.input("allocateApply_Id", body.selectIDs);
|
request.input("user_Id", userInfo.user_Id);
|
request.output("outMsg", sql.NVarChar(2000));
|
let result = await request.execute("sp_Storage_AllocateApply_Sorting");
|
let outMsg = result.output.outMsg;
|
//在outMsg中没有值时则返回true "分练成功"
|
if (outMsg !== null && outMsg) {
|
this.info.msg = outMsg;
|
this.info.result = false;
|
ctx.body = this.info;
|
return;
|
}
|
this.info.msg = "分拣完成";
|
this.info.result = true;
|
} catch (ex) {
|
this.info.result = false;
|
this.info.msg = "错误信息:" + ex.message;
|
}
|
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
|
ProductCode AS '物料编号',ProductName AS '物料名称',ProductModel AS '条形码',BatchNumber AS '批次号',ProductSpec AS '物料规格',
|
Quantity AS '调入数量',PurchasePrice AS '成本价',PurchaseMoney AS '成本额',LackStorage AS '缺货数量',SmallUnit AS '小单位',
|
BigUnit AS '大单位',Remark AS '备注'
|
FROM Storage_AllocateApplyList
|
WHERE AllocateApplyList_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 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 quantity = row["调入数量"];
|
let product_Id = row["物料ID"];
|
let productCode = row["物料编号"];
|
let productName = row["物料名称"];
|
let productModel = row["条形码"];
|
// let productCode = row["物料编号"];
|
if (!quantity) {
|
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;
|
}
|
if (!productModel) {
|
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(StorageAllocateApplyList, {
|
allocateApply_Id: body.allocateApply_Id,
|
createID: userInfo.user_Id,
|
creator: userInfo.userTrueName,
|
quantity: row["调入数量"],
|
product_Id: row["物料ID"],
|
productCode: row["物料编号"],
|
productName: row["物料名称"],
|
productModel: row["条形码"],
|
productSpec: row["物料规格"],
|
purchasePrice: row["成本价"],
|
purchaseMoney: row["成本额"],
|
lackStorage: row["缺货数量"],
|
smallUnit: row["小单位"],
|
bigUnit: row["大单位"],
|
remark: row["备注"]
|
});
|
|
this.info.result = true;
|
this.info.msg = "导入成功";
|
}
|
}
|
} catch (ex) {
|
this.info.msg = "出现异常:" + ex.message;
|
this.info.result = false;
|
}
|
|
ctx.body = this.info;
|
}
|
|
// #endregion
|
}
|