| import BaseController from "../baseController"; | 
| import { Post } from "egg-shell-decorators"; | 
| import { BasePlate } from "../../entity/basicInfo/base/basePlate"; | 
| import { In, Not, MoreThan } from "typeorm"; | 
| import { SaleOrder } from "../../entity/outbound/sale/saleOrder"; | 
| import { SaleOrderList } from "../../entity/outbound/sale/saleOrderList"; | 
| import { vBaseProductPosition } from "../../entity/storage/product/vBaseProductPosition"; | 
| import * as mssql from "mssql"; | 
| import { SaleSortingRulePosition } from "../../entity/outbound/sortingrule/saleSortingRulePosition"; | 
| import { BaseProductPlaceHolder } from "../../entity/storage/storage/baseProductPlaceHolder"; | 
|   | 
| /** | 
|  * EU箱出库任务 | 
|  */ | 
| export default class OrderEuController extends BaseController { | 
|   //#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 (!Array.isArray(detailList) || !detailList.length) { | 
|         this.info.result = false; | 
|         this.info.msg = `明细不能为空!`; | 
|         this.ctx.body = this.info; | 
|         return; | 
|       } | 
|       let where = { | 
|         plateCode: masterData.plateCode, | 
|         userProduct_Id: userInfo.userProduct_Id, | 
|         statusText: "新建", | 
|         orderType: In([masterData.orderType]) | 
|       }; | 
|       // 编辑时排除自己 | 
|       if (masterData.order_Id) { | 
|         where["order_Id"] = Not(masterData.order_Id); | 
|       } | 
|       let orderInfo = await this.dbRead.findOne(SaleOrder, where); | 
|       if (orderInfo) { | 
|         this.info.result = false; | 
|         this.info.msg = `器具种类${masterData.plateCode}已创建,不可重复创建!`; | 
|         this.ctx.body = this.info; | 
|         return; | 
|       } | 
|   | 
|       let plateInfo = await this.dbRead.findOne(BasePlate, { | 
|         plateCode: masterData.plateCode | 
|       }); | 
|       if (!plateInfo) { | 
|         this.info.result = false; | 
|         this.info.msg = `${masterData.plateCode}器具编号不存在`; | 
|         ctx.body = this.info; | 
|         return; | 
|       } | 
|   | 
|       let mainInfo = new SaleOrder(); // 主表数据 | 
|       // 必须做这边操作,编辑时才执行更新操作 | 
|       mainInfo = Object.assign(mainInfo, masterData); | 
|       // 新建时,生成单据编号 | 
|       if (!mainInfo.order_Id) { | 
|         let code = await ctx.service.common.getCodeRegular(112); | 
|         mainInfo.orderCode = code; | 
|       } | 
|   | 
|       // 主表求和字段计算 | 
|       let total: any = detailList.reduce( | 
|         (totalItem: any, currItem) => { | 
|           totalItem.totalQuantity += currItem.quantityOrder; | 
|           return totalItem; | 
|         }, | 
|         { totalQuantity: 0 } | 
|       ); | 
|   | 
|       // 状态默认为新建 | 
|       if (!mainInfo.statusText) { | 
|         mainInfo.statusText = "新建"; | 
|       } | 
|       mainInfo.orderType = masterData.orderType; | 
|       mainInfo.storage_Id = masterData.storage_Id; | 
|       mainInfo.storageName = masterData.storageName; | 
|       mainInfo.plateType = plateInfo.plateType; | 
|       mainInfo.plateCode = masterData.plateCode; | 
|       mainInfo.createDate = new Date(); | 
|       mainInfo.createID = userInfo.user_Id; | 
|       mainInfo.creator = userInfo.userTrueName; | 
|       mainInfo.totalQuantityOrder = total.totalQuantity; | 
|       mainInfo.consignor_Id = 30; | 
|       mainInfo.consignorCode = "GX30"; | 
|       mainInfo.consignorName = "广州西门子"; | 
|       mainInfo.destinationName = masterData.destinationName; | 
|       mainInfo.destination_Id = masterData.destination_Id; | 
|       await this.setAccountInfo(mainInfo); | 
|       await this.dbWrite.save(mainInfo); // 保存主表信息,编辑和添加 | 
|   | 
|       for (let item of detailList) { | 
|         let detailInfo = new SaleOrderList(); | 
|         // 必须做这边操作,编辑时才执行更新操作 | 
|         detailInfo = Object.assign(detailInfo, item); | 
|   | 
|         detailInfo.order_Id = mainInfo.order_Id; | 
|         detailInfo.productCode = item.productCode; | 
|         detailInfo.product_Id = item.product_Id; | 
|         detailInfo.productName = item.productName; | 
|         detailInfo.productModel = item.productModel; | 
|         detailInfo.produceDate = item.produceDate; | 
|         detailInfo.productSpec = item.productSpec; | 
|         detailInfo.produceDate = item.produceDate; | 
|         detailInfo.plateCode = item.plateCode; | 
|         detailInfo.batchNumber = item.batchNumber; | 
|         detailInfo.plateType = item.plateType; | 
|         detailInfo.quantityOrder = item.quantityOrder; | 
|   | 
|         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 scanCheckData() { | 
|     let { ctx } = this; | 
|     let body = ctx.request.body; | 
|   | 
|     try { | 
|       if (!body.plateCode) { | 
|         this.info.result = false; | 
|         this.info.msg = `扫描标签不能为空!`; | 
|         this.ctx.body = this.info; | 
|         return; | 
|       } | 
|       if (!body.order_Id) { | 
|         this.info.result = false; | 
|         this.info.msg = `单号不能为空!`; | 
|         this.ctx.body = this.info; | 
|         return; | 
|       } | 
|       if (!body.orderList_Id) { | 
|         this.info.result = false; | 
|         this.info.msg = `明细不能为空!`; | 
|         this.ctx.body = this.info; | 
|         return; | 
|       } | 
|   | 
|       let orderInfo = await this.dbRead.findOne(SaleOrder, body.order_Id); | 
|       if (!orderInfo) { | 
|         this.info.result = false; | 
|         this.info.msg = `单号不存在!`; | 
|         this.ctx.body = this.info; | 
|         return; | 
|       } | 
|       let orderDetail = await this.dbRead.findOne(SaleOrderList, body.orderList_Id); | 
|   | 
|       // 没有分拣成功需要,判断库存是否充足 | 
|       if (orderInfo.sortingStatus !== 2) { | 
|         let ppInfo = await this.dbRead.findOne(vBaseProductPosition, { | 
|           plateCode: body.plateCode, | 
|           validQty: MoreThan(0) | 
|         }); | 
|   | 
|         if (!ppInfo) { | 
|           this.info.result = false; | 
|           this.info.msg = `没有可用库存!`; | 
|           this.ctx.body = this.info; | 
|           return; | 
|         } | 
|   | 
|         if (ppInfo.validQty < orderDetail.quantityOrder) { | 
|           this.info.result = false; | 
|           this.info.msg = `有效库为${ppInfo.validQty},库存不足!`; | 
|           this.ctx.body = this.info; | 
|           return; | 
|         } | 
|       } else { | 
|         let holderInfo = await this.dbRead.findOne(BaseProductPlaceHolder, { | 
|           mainID: body.order_Id | 
|         }); | 
|         if (!holderInfo) { | 
|           this.info.result = false; | 
|           this.info.msg = `未分配库存!`; | 
|           this.ctx.body = this.info; | 
|           return; | 
|         } | 
|         if (holderInfo.plateCode !== body.plateCode) { | 
|           this.info.result = false; | 
|           this.info.msg = `当前已分配库存${holderInfo.plateCode},扫描不正确!`; | 
|           this.ctx.body = this.info; | 
|           return; | 
|         } | 
|       } | 
|   | 
|       // 先清空现有规则 | 
|       await this.dbWrite.delete(SaleSortingRulePosition, { | 
|         order_Id: body.order_Id | 
|       }); | 
|   | 
|       // 添加分拣规则 | 
|       let regInfo = new SaleSortingRulePosition(); | 
|       regInfo.order_Id = orderDetail.order_Id; | 
|       regInfo.orderList_Id = orderDetail.orderList_Id; | 
|       regInfo.plateCode = body.plateCode; | 
|       regInfo.product_Id = orderDetail.product_Id; | 
|       regInfo.productCode = orderDetail.productCode; | 
|       await this.dbWrite.save(regInfo); | 
|   | 
|       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 checkOut() { | 
|     let { ctx } = this; | 
|     let body = ctx.request.body; | 
|     let userInfo = await this.userInfo; | 
|     if (!body.order_Id) { | 
|       this.info.result = false; | 
|       this.info.msg = `单号不能为空!`; | 
|       this.ctx.body = this.info; | 
|       return; | 
|     } | 
|   | 
|     try { | 
|       const connection: any = await this.dbWrite.connection; | 
|       let request = new mssql.Request(connection.driver.master); | 
|       request.input("order_Id", body.order_Id); | 
|       request.input("user_Id", userInfo.user_Id); | 
|   | 
|       request.output("outMsg", mssql.NVarChar(2000)); | 
|       let result = await request.execute("sp_Sale_Order_AutoOut"); | 
|       let outMsg = result.output.outMsg; | 
|       if (outMsg != null && outMsg) { | 
|         this.info.msg = outMsg; | 
|         this.info.result = false; | 
|       } else { | 
|         this.info.msg = "确认出库成功"; | 
|         this.info.result = true; | 
|       } | 
|     } catch (error) { | 
|       this.info.result = false; | 
|       this.info.msg = "错误:" + error.message; | 
|     } | 
|     ctx.body = this.info; | 
|   } | 
|   //#endregion | 
| } |