import { default as BaseController } from "../baseController";
|
import { Post } from "egg-shell-decorators";
|
import { BaseProductInfo } from "../../entity/basicInfo/base/baseProductInfo";
|
import { BaseEmptyEquipment } from "../../entity/inbound/purchase/baseEmptyEquipment";
|
import { BasePlate } from "../../entity/basicInfo/base/basePlate";
|
import { BasePlateType } from "../../entity/basicInfo/base/basePlateType";
|
import { BaseEmptyEquipmentList } from "../../entity/inbound/purchase/baseEmptyEquipmentList";
|
import { BaseProductPosition } from "../../entity/storage/product/baseProductPosition";
|
import { MoreThan, Not, Raw } from "typeorm";
|
import { vBaseProductPosition } from "../../entity/storage/product/vBaseProductPosition";
|
|
/**
|
* 空器具出库
|
*/
|
export default class EmptyEquipmentController extends BaseController {
|
//#region 空器具出库详情
|
@Post()
|
public async getquipmentList() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let userInfo = await ctx.helper.userInfo();
|
try {
|
let sql = ` SELECT emptyEquipment_Id,emptyEquipmentCode,statusText,consignorName,totalQuantity,storageName, checkout, destinationName, (SELECT COUNT(*) AS unfinishedNub FROM dbo.Base_EmptyEquipmentList WHERE EmptyEquipment_Id = B.EmptyEquipment_Id) AS muber
|
FROM Base_EmptyEquipment B
|
WHERE B.emptyEquipment_Id = @0 and B.userProduct_Id=@1`;
|
let quipment = await this.dbRead.query(sql, [body.emptyEquipmentCode, userInfo.userProduct_Id]);
|
var emptyEquipment_Id = quipment[0].emptyEquipment_Id;
|
sql = ` SELECT plateCode,plateName,currentLocation,productName,quantity,currentArea FROM Base_EmptyEquipmentList AS A LEFT JOIN Base_EmptyEquipment AS B ON A.EmptyEquipment_Id =B.EmptyEquipment_Id
|
WHERE B.emptyEquipment_Id = @0 and B.userProduct_Id =@1 `;
|
let quipmentList = await this.dbRead.query(sql, [emptyEquipment_Id, userInfo.userProduct_Id]);
|
|
this.info.data = quipment;
|
this.info.data2 = quipmentList;
|
} catch (error) {
|
this.info.result = false;
|
this.info.msg = "错误:" + error.message;
|
}
|
this.info.result = true;
|
ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region 获取物料信息关联器具种类数量
|
@Post()
|
public async onChangeProductCode() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let product_Id = body.product_Id;
|
try {
|
let prodInfo = await this.dbRead.findOne(BaseProductInfo, {
|
where: {
|
product_Id: product_Id
|
}
|
});
|
// 空器具数量
|
let count = await this.dbRead.count(BaseProductPosition, {
|
where: {
|
plateTypeCode: prodInfo.plateTypeCode,
|
product_Id: 0, // 0为空器具
|
productStorage: MoreThan(0),
|
// 排除已经分配给其他空器具任务中的器具
|
productCode: Raw(() => {
|
return `NOT EXISTS(Select 1
|
From Base_EmptyEquipment E INNER JOIN Base_EmptyEquipmentList L ON E.emptyEquipment_Id=L.emptyEquipment_Id
|
Where BaseProductPosition.plateCode=L.plateCode And E.StatusText IN('新建', '部分出库'))`;
|
})
|
}
|
});
|
this.info.result = true;
|
this.info.data = { count: count };
|
} catch (error) {
|
this.info.result = false;
|
this.info.msg = "错误:" + error.message;
|
}
|
ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region save 保存数据
|
@Post()
|
public async save() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let userInfo = await ctx.helper.userInfo();
|
let details = []; // 记录明细保存的数据
|
let masterData = body.masterData;
|
let detailList = body.detailList;
|
|
try {
|
if (!masterData.demandQuantity || masterData.demandQuantity <= 0) {
|
this.info.result = false;
|
this.info.msg = `物料编号${masterData.productCode}数量必须大于0`;
|
this.ctx.body = this.info;
|
return;
|
}
|
let where = {
|
product_Id: masterData.product_Id,
|
userProduct_Id: userInfo.userProduct_Id,
|
statusText: "新建"
|
};
|
// 编辑时排除自己
|
if (masterData.emptyEquipment_Id) {
|
where["emptyEquipment_Id"] = Not(masterData.emptyEquipment_Id);
|
}
|
let orderInfo = await this.dbRead.findOne(BaseEmptyEquipment, where);
|
if (orderInfo) {
|
this.info.result = false;
|
this.info.msg = `物料编号${masterData.productCode}已创建,不可重复创建!`;
|
this.ctx.body = this.info;
|
return;
|
}
|
|
let productInfo = await this.dbRead.findOne(BaseProductInfo, {
|
where: {
|
product_Id: masterData.product_Id
|
}
|
});
|
let plateType = await this.dbRead.findOne(BasePlateType, {
|
where: {
|
plateType: productInfo.plateType
|
}
|
});
|
|
let mainInfo = new BaseEmptyEquipment(); // 主表数据
|
// 必须做这边操作,编辑时才执行更新操作
|
mainInfo = Object.assign(mainInfo, masterData);
|
// 新建时,生成单据编号
|
if (!mainInfo.emptyEquipment_Id) {
|
let code = await ctx.service.common.getCodeRegular(1808);
|
mainInfo.emptyEquipmentCode = code;
|
}
|
|
if (plateType) {
|
mainInfo.storageName = plateType.storageName;
|
mainInfo.storage_Id = plateType.storage_Id;
|
}
|
// 获取空器具库存
|
let ppList = [];
|
|
// 当明细为空时,自动添加从库存中添加器具
|
if (!detailList.length) {
|
ppList = await this.dbRead.find(vBaseProductPosition, {
|
where: {
|
product_Id: 0,
|
plateTypeCode: productInfo.plateTypeCode,
|
productStorage: MoreThan(0),
|
productCode: Raw(() => {
|
return `NOT EXISTS(Select 1
|
From Base_EmptyEquipment E INNER JOIN Base_EmptyEquipmentList L ON E.emptyEquipment_Id=L.emptyEquipment_Id
|
Where vBaseProductPosition.plateCode=L.plateCode And E.StatusText IN('新建', '部分出库', '下架中'))`;
|
})
|
},
|
take: masterData.demandQuantity
|
});
|
if (ppList.length < masterData.demandQuantity) {
|
this.info.result = false;
|
this.info.msg = `库存不足!`;
|
this.ctx.body = this.info;
|
return;
|
}
|
}
|
// 状态默认为新建
|
if (!mainInfo.statusText) {
|
mainInfo.statusText = "新建";
|
}
|
mainInfo.orderType = "空器具出库";
|
mainInfo.product_Id = productInfo.product_Id;
|
mainInfo.productCode = productInfo.productCode;
|
mainInfo.productName = productInfo.productName + "(空器具)";
|
mainInfo.createDate = new Date();
|
mainInfo.createID = userInfo.user_Id;
|
mainInfo.creator = userInfo.userTrueName;
|
mainInfo.totalQuantity = masterData.totalQuantity;
|
mainInfo.demandQuantity = masterData.demandQuantity;
|
mainInfo.consignor_Id = 30;
|
mainInfo.consignorCode = "GX30";
|
mainInfo.consignorName = "广州西门子";
|
await this.setAccountInfo(mainInfo);
|
await this.dbWrite.save(mainInfo); // 保存主表信息,编辑和添加
|
|
// 当明细为空时,自动添加从库存中添加器具
|
if (!this.body.detailList.length) {
|
// 清空明细
|
await this.dbWrite.delete(BaseEmptyEquipmentList, {
|
emptyEquipment_Id: mainInfo.emptyEquipment_Id
|
});
|
|
for (let item of ppList) {
|
let detailInfo = new BaseEmptyEquipmentList();
|
// 必须做这边操作,编辑时才执行更新操作
|
detailInfo = Object.assign(detailInfo, item);
|
detailInfo.emptyEquipment_Id = mainInfo.emptyEquipment_Id;
|
detailInfo.status = "新建";
|
|
await this.setAccountInfo(detailInfo);
|
await this.dbWrite.save(detailInfo); // 保存明细数据
|
details.push(detailInfo);
|
}
|
} else {
|
// 保存手工添加的器具
|
for (let item of this.body.detailList) {
|
let detailInfo = new BaseEmptyEquipmentList();
|
// 必须做这边操作,编辑时才执行更新操作
|
detailInfo = Object.assign(detailInfo, item);
|
detailInfo.emptyEquipment_Id = mainInfo.emptyEquipment_Id;
|
detailInfo.status = "新建";
|
|
await this.setAccountInfo(detailInfo);
|
await this.dbWrite.save(detailInfo); // 保存明细数据
|
details.push(detailInfo);
|
}
|
}
|
|
this.info.data = mainInfo;
|
this.info.data2 = details;
|
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 confirm() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let emptyEquipment_Id = body.emptyEquipment_Id;
|
try {
|
let equipmentList = await this.dbRead.find(BaseEmptyEquipmentList, {
|
emptyEquipment_Id: emptyEquipment_Id,
|
status: "新建"
|
});
|
if (!equipmentList.length) {
|
this.info.result = false;
|
this.info.msg = "没有可用的器具编号";
|
ctx.body = this.info;
|
return;
|
}
|
for (let equipInfo of equipmentList) {
|
let taskInfo = await this.ctx.service.wcs.createTaskQueueEmptyOut(equipInfo);
|
if (taskInfo === "-1") {
|
this.info.result = false;
|
this.info.msg = equipInfo.plateCode + "出库指令下发失败, 输送线不存在!";
|
ctx.body = this.info;
|
return;
|
}
|
if (taskInfo === "-2") {
|
this.info.result = false;
|
this.info.msg = `器具编号${equipInfo.plateCode}库存不存在!`;
|
ctx.body = this.info;
|
return;
|
}
|
}
|
// 更新单据状态为下架中
|
await this.dbWrite.update(
|
BaseEmptyEquipment,
|
{
|
emptyEquipment_Id: emptyEquipment_Id
|
},
|
{
|
statusText: "下架中"
|
}
|
);
|
|
this.info.msg = "出库指令成功!";
|
this.info.result = true;
|
} catch (error) {
|
this.info.result = false;
|
this.info.msg = error.message;
|
}
|
ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region 空器具出库任务 器具编号获取详情
|
@Post()
|
public async getappplateCode() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let userInfo = await ctx.helper.userInfo();
|
try {
|
let plateInfo = await this.dbRead.findOne(BasePlate, {
|
plateCode: body.plateCode,
|
userProduct_Id: userInfo.userProduct_Id
|
});
|
this.info.data = plateInfo;
|
} catch (error) {
|
this.info.result = false;
|
this.info.msg = "错误:" + error.message;
|
}
|
this.info.result = true;
|
ctx.body = this.info;
|
}
|
//#endregion
|
}
|