import BaseController from "../../baseController";
|
import { Post } from "egg-shell-decorators";
|
import { In, Like } from "typeorm";
|
import { BasePlate } from "../../../entity/basicInfo/base/basePlate";
|
import { BasePlateType } from "../../../entity/basicInfo/base/basePlateType";
|
import { BaseStorageArea } from "../../../entity/basicInfo/base/baseStorageArea";
|
import * as path from "path";
|
import * as XLSX from "xlsx";
|
|
export default class PlateController extends BaseController {
|
//#region getListPlateType 获得器具种类
|
/**
|
* 获得器具种类
|
*/
|
@Post()
|
public async getListPlateType() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let userInfo = await ctx.helper.userInfo();
|
|
try {
|
let whereStr = "";
|
var where: any = {};
|
|
// 超级管理员不需要走仓库权限
|
if (userInfo.isAdministrator || body.isAll) {
|
whereStr = `userProduct_Id=:userProduct_Id`;
|
where = {
|
userProduct_Id: userInfo.userProduct_Id
|
};
|
} else if (userInfo.userType === "consignor") {
|
// 前端货主登录权限
|
whereStr = `userProduct_Id=:userProduct_Id`;
|
where = {
|
userProduct_Id: userInfo.userProduct_Id
|
};
|
} else {
|
whereStr = `userProduct_Id=:userProduct_Id`;
|
where = {
|
userProduct_Id: userInfo.userProduct_Id
|
};
|
}
|
// 关键词查询条件
|
// if (body.name) {
|
// let name = this.ctx.helper.sqlSecurity(body.name);
|
// whereStr += ` AND (productName LIKE '%' + :name + '%' OR productCode LIKE '%' + :name + '%')`;
|
// where.name = name;
|
// }
|
let fields = ["plateType", "plateType_Id"];
|
// 自定义查询字段
|
if (body.searchFields) {
|
fields = body.searchFields;
|
}
|
if (body.appendField === "*") {
|
fields = [];
|
}
|
let builder = this.dbRead.createQueryBuilder(BasePlateType, "t").where(whereStr, where);
|
if (fields.length) {
|
builder.select(fields);
|
}
|
builder.take(200);
|
let dataList = [];
|
if (body.appendField === "*") {
|
dataList = await builder.getMany();
|
} else {
|
dataList = await builder.getRawMany();
|
}
|
|
this.info.result = true;
|
this.info.data = dataList;
|
|
ctx.body = this.info;
|
} catch (ex) {
|
let msg = "异常错误信息:" + ex.message;
|
this.info.result = false;
|
this.info.msg = msg;
|
ctx.body = this.info;
|
}
|
}
|
//#endregion
|
|
//#region getListPlate 获得物料列表
|
/**
|
* 获得物料列表
|
*/
|
@Post()
|
public async getListPlate() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let userInfo = await ctx.helper.userInfo();
|
|
try {
|
let whereStr = "";
|
var where: any = {};
|
|
// 超级管理员不需要走仓库权限
|
if (userInfo.isAdministrator || body.isAll) {
|
whereStr = `userProduct_Id=:userProduct_Id`;
|
where = {
|
userProduct_Id: userInfo.userProduct_Id
|
};
|
} else if (userInfo.userType === "consignor") {
|
// 前端货主登录权限
|
whereStr = `userProduct_Id=:userProduct_Id`;
|
where = {
|
userProduct_Id: userInfo.userProduct_Id
|
};
|
} else {
|
whereStr = `userProduct_Id=:userProduct_Id`;
|
where = {
|
userProduct_Id: userInfo.userProduct_Id
|
};
|
}
|
// 关键词查询条件
|
// if (body.name) {
|
// let name = this.ctx.helper.sqlSecurity(body.name);
|
// whereStr += ` AND (productName LIKE '%' + :name + '%' OR productCode LIKE '%' + :name + '%')`;
|
// where.name = name;
|
// }
|
let fields = ["plateCode", "plate_Id"];
|
// 自定义查询字段
|
if (body.searchFields) {
|
fields = body.searchFields;
|
}
|
if (body.appendField === "*") {
|
fields = [];
|
}
|
let builder = this.dbRead.createQueryBuilder(BasePlate, "t").where(whereStr, where);
|
if (fields.length) {
|
builder.select(fields);
|
}
|
builder.take(200);
|
let dataList = [];
|
if (body.appendField === "*") {
|
dataList = await builder.getMany();
|
} else {
|
dataList = await builder.getRawMany();
|
}
|
|
this.info.result = true;
|
this.info.data = dataList;
|
|
ctx.body = this.info;
|
} catch (ex) {
|
let msg = "异常错误信息:" + ex.message;
|
this.info.result = false;
|
this.info.msg = msg;
|
ctx.body = this.info;
|
}
|
}
|
//#endregion
|
|
//#region getListStorage
|
/**
|
* 根据仓库名称获得TableSelect获得器具信息
|
*/
|
@Post()
|
public async getListStorage() {
|
let userInfo = await this.userInfo;
|
let { ctx } = this;
|
let body = ctx.request.body;
|
var dataList = await this.dbRead.find(BaseStorageArea, {
|
where: {
|
userProduct_Id: userInfo.userProduct_Id,
|
storageName: body.name
|
}
|
});
|
if (dataList.length == 0) {
|
this.info.result = false;
|
this.info.data = dataList;
|
} else {
|
this.info.result = true;
|
this.info.data = dataList;
|
}
|
|
this.ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region getListPlates
|
/**
|
* getListPlates获得母托盘信息
|
*/
|
@Post()
|
public async getListPlates() {
|
let userInfo = await this.userInfo;
|
let { ctx } = this;
|
let body = ctx.request.body;
|
var dataList = await this.dbRead.find(BasePlate, {
|
where: {
|
userProduct_Id: userInfo.userProduct_Id,
|
plateCode: body.plateCode
|
}
|
});
|
if (dataList.length == 0) {
|
this.info.result = false;
|
this.info.msg = body.plateCode + "器具编号不存在";
|
} else {
|
this.info.result = true;
|
this.info.data = dataList;
|
}
|
|
this.ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region getListCode
|
/**
|
* 获得TableSelect获得器具信息
|
*/
|
@Post()
|
public async getListCode() {
|
let userInfo = await this.userInfo;
|
let { ctx } = this;
|
|
let body = ctx.request.body;
|
await this.dbWrite.update(
|
BasePlate,
|
{
|
userProduct_Id: userInfo.userProduct_Id,
|
|
plateCode: body.plateCode
|
},
|
{
|
plateState: "已组盘"
|
}
|
);
|
this.info.result = true;
|
// this.info.msg = "修改成功!";
|
this.ctx.body = this.info;
|
return;
|
}
|
//#endregion
|
|
//#region getList
|
/**
|
* 获得TableSelect获得器具信息
|
*/
|
@Post()
|
public async getList() {
|
let userInfo = await this.userInfo;
|
let where: any = {
|
userProduct_Id: userInfo.userProduct_Id
|
};
|
if (this.body.name) {
|
where.plateCode = this.body.name;
|
}
|
if (this.body.ids) {
|
where.plate_Id = In(this.body.ids);
|
}
|
var dataList = await this.dbRead.find(BasePlate, {
|
where: where
|
});
|
if (dataList.length == 0) {
|
this.info.result = false;
|
this.info.data = dataList;
|
} else {
|
this.info.result = true;
|
this.info.data = dataList;
|
}
|
|
this.ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region getListType
|
/**
|
* 获得TableSelect获得器具信息
|
*/
|
@Post()
|
public async getListType() {
|
let userInfo = await this.userInfo;
|
var dataList = await this.dbRead.find(BasePlateType, {
|
where: {
|
userProduct_Id: userInfo.userProduct_Id,
|
plateType: Like("%" + this.body.name + "%")
|
// plateType_Id: Like("%" + this.body.id + "%")
|
}
|
});
|
if (dataList.length == 0) {
|
this.info.result = false;
|
this.info.data = dataList;
|
} else {
|
this.info.result = true;
|
this.info.data = dataList;
|
}
|
|
this.ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region getPlateInfo 器具编号获取数据
|
@Post()
|
public async getPlateInfo() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
try {
|
let plateInfo = await this.dbRead.findOne(BasePlate, { plateCode: body.plateCode });
|
if (!plateInfo) {
|
this.info.result = false;
|
this.info.msg = body.plateCode + "器具编号不存在";
|
return;
|
}
|
this.info.result = true;
|
this.info.data = plateInfo;
|
} catch (error) {
|
this.info.result = false;
|
this.info.msg = "错误:" + error.message;
|
}
|
ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region 启用容器
|
/**
|
* 启用容器
|
*/
|
@Post()
|
public async plateOpen() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
if (!Array.isArray(body.ids) || !body.ids.length) {
|
this.info.result = false;
|
this.info.msg = "没有可执行的单号";
|
return;
|
}
|
try {
|
// 更新启用为1
|
await this.dbWrite.update(
|
BasePlate,
|
{
|
plate_Id: In(body.ids)
|
},
|
{
|
enable: 1
|
}
|
);
|
|
this.info.result = true;
|
this.info.msg = "容器启用成功";
|
} catch (error) {
|
this.info.result = false;
|
this.info.msg = "容器启用失败";
|
}
|
this.ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region 停止容器
|
/**
|
* 停止容器
|
*/
|
@Post()
|
public async plateStop() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
if (!Array.isArray(body.ids) || !body.ids.length) {
|
this.info.result = false;
|
this.info.msg = "没有可执行的单号";
|
return;
|
}
|
try {
|
// 更新启用为1
|
await this.dbWrite.update(
|
BasePlate,
|
{
|
plate_Id: In(body.ids)
|
},
|
{
|
enable: 0
|
}
|
);
|
|
this.info.result = true;
|
this.info.msg = "容器停止成功";
|
} catch (error) {
|
this.info.result = false;
|
this.info.msg = "容器停止失败";
|
}
|
this.ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region 生成容器号
|
@Post()
|
public async createPlantCode() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
try {
|
let plateInfo = await this.dbRead.findOne(BasePlate, {
|
where: {
|
plateCode: Like(body.data.codePrefix + "%")
|
},
|
order: {
|
plate_Id: "DESC"
|
}
|
});
|
let lastCode = 0; // 最后一条编码
|
if (plateInfo) {
|
lastCode = Number(plateInfo.plateCode.substring(2));
|
}
|
for (let index = 0; index < body.data.generate; index++) {
|
lastCode++;
|
let newPlateInfo = new BasePlate();
|
let plateCode = "000000" + lastCode;
|
newPlateInfo.plateCode = body.data.codePrefix + plateCode.substring(plateCode.length - 6);
|
newPlateInfo.storage_Id = body.data.storage_Id;
|
newPlateInfo.storageName = body.data.storageName;
|
newPlateInfo.plateType = body.data.plateType;
|
newPlateInfo.enable = 1;
|
await this.setAccountInfo(newPlateInfo, "plate_Id");
|
this.dbWrite.save(BasePlate, newPlateInfo);
|
}
|
this.info.result = true;
|
this.info.msg = "生成成功";
|
} catch (error) {
|
this.info.result = false;
|
this.info.msg = "生成失败";
|
}
|
this.ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region 更新图片 updateImages
|
/**
|
* 更新图片 updateImages
|
*/
|
@Post()
|
public async updateImages() {
|
let { ctx } = this;
|
let plate_Id = Number(this.body.plate_Id);
|
if (!plate_Id) {
|
this.info.msg = "器具不存在";
|
this.info.result = false;
|
ctx.body = this.info;
|
return;
|
}
|
|
try {
|
await this.dbWrite.update(BasePlate, this.body.plate_Id, {
|
photo: this.body.photo
|
});
|
|
this.info.result = true;
|
this.info.msg = "图片更新成功";
|
} catch (ex) {
|
this.info.msg = ex.message;
|
this.info.result = false;
|
}
|
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;
|
}
|
//#endregion
|
|
//#region importExcelWork
|
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;
|
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 = XLSX.utils.sheet_to_json(sheet1); // 获得当前sheet表单数据转为json格式
|
|
//#region 验证数据正确性
|
this.info.result = true;
|
if (!dataList.length) {
|
this.setMsg("没有可导入的数据", "red");
|
return;
|
}
|
let i = 1,
|
successCount = 0,
|
updateCount = 0;
|
|
// 母托盘集合
|
let plateList = await this.dbRead.find(BasePlate, {
|
userProduct_Id: userInfo.userProduct_Id,
|
plateCode: In(dataList.map(item => "" + item["器具编号"]))
|
});
|
let dataRows = []; //数据集合
|
for (let row of dataList) {
|
i++;
|
|
// let plateInfo = await this.dbRead.findOne(BasePlate, {
|
// plateTypeCode: plateTypeCode
|
// });
|
let data = {
|
// productName: "" + row["物料名称"],
|
plateCode: row["母托盘编号"],
|
plateType: row["母托盘种类"],
|
plateTypeCode: row["种类编号"],
|
plateName: row["母托盘名称"],
|
areaCode: row["库区名称"],
|
plateState: "空器具",
|
length: row["长"],
|
width: row["宽"],
|
height: row["高"],
|
currentLocation: row["当前位置"],
|
plateWeight: row["母托盘自重kg"],
|
packingQuantity: row["装箱数量"],
|
remark: row["备注"]
|
};
|
|
// #region 校验数据
|
if (!data.plateCode) {
|
this.setMsg(`第${i}行、母托盘编号为空`, "red");
|
updateCount++;
|
}
|
if (!data.plateType) {
|
this.setMsg(`第${i}行、母托盘种类为空`, "red");
|
updateCount++;
|
}
|
if (!data.plateTypeCode) {
|
this.setMsg(`第${i}行、种类编号为空`, "red");
|
updateCount++;
|
}
|
if (!data.plateName) {
|
this.setMsg(`第${i}行、母托盘名称为空`, "red");
|
updateCount++;
|
}
|
dataRows.push(data);
|
}
|
|
if (this.isMsgError) {
|
this.setMsg(`导入数据有错误,请处理好重新导入`, "red");
|
this.setMsg("-1");
|
return;
|
}
|
//#endregion
|
|
i = 1;
|
successCount = 0;
|
updateCount = 0;
|
let rows = []; // 准备更新或者新增的数据
|
for (let data of dataRows) {
|
i++;
|
// 根据物料编号查询物料是否存在
|
let dataInfo = plateList.find(item => item.plateCode === data.plateCode);
|
let isadd = true;
|
if (!dataInfo) {
|
dataInfo = new BasePlate(); // 新建数据
|
isadd = false;
|
}
|
|
dataInfo = Object.assign(dataInfo, data); // 合并数据
|
await this.setAccountInfo(dataInfo, "plate_Id");
|
if (isadd == true) {
|
this.setMsg(`第${i}行、${data.plateCode}已存在,正在更新...`);
|
updateCount++;
|
// 物料审核状态
|
data.auditing = 0;
|
rows.push(dataInfo);
|
} else {
|
this.setMsg(`第${i}行、${data.plateCode}正在导入...`);
|
successCount++;
|
rows.push(dataInfo);
|
}
|
if (i % 50 === 0) {
|
await this.dbWrite.save(BasePlate, rows);
|
rows = [];
|
}
|
}
|
if (rows.length > 0) {
|
await this.dbWrite.save(BasePlate, rows);
|
rows = [];
|
}
|
|
this.setMsg(`导入成功,新增${successCount}条,更新${updateCount}条`, "blue");
|
} catch (ex) {
|
this.setMsg("出现异常:" + ex.message, "red");
|
this.info.result = false;
|
}
|
this.setMsg("-1");
|
}
|
//#endregion
|
}
|