| import BaseController from "../baseController"; | 
| import { Post } from "egg-shell-decorators"; | 
| import { PurchaseOrder } from "../../entity/inbound/purchase/purchaseOrder"; | 
| import { BasePlate } from "../../entity/basicInfo/base/basePlate"; | 
| import { In, Not } from "typeorm"; | 
| import { PurchaseOrderList } from "../../entity/inbound/purchase/purchaseOrderList"; | 
|   | 
| /** | 
|  * 焊装返修回库 | 
|  */ | 
| export default class OrderFxhkController 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(["返修单"]) | 
|       }; | 
|       // 编辑时排除自己 | 
|       if (masterData.order_Id) { | 
|         where["order_Id"] = Not(masterData.order_Id); | 
|       } | 
|       let orderInfo = await this.dbRead.findOne(PurchaseOrder, 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 PurchaseOrder(); // 主表数据 | 
|       // 必须做这边操作,编辑时才执行更新操作 | 
|       mainInfo = Object.assign(mainInfo, masterData); | 
|       // 新建时,生成单据编号 | 
|       if (!mainInfo.order_Id) { | 
|         let code = await ctx.service.common.getCodeRegular(102); | 
|         mainInfo.orderCode = code; | 
|       } | 
|   | 
|       // 主表求和字段计算 | 
|       let total: any = detailList.reduce( | 
|         (totalItem: any, currItem) => { | 
|           totalItem.totalQuantity += currItem.quantity; | 
|           return totalItem; | 
|         }, | 
|         { totalQuantity: 0 } | 
|       ); | 
|   | 
|       // 状态默认为新建 | 
|       if (!mainInfo.statusText) { | 
|         mainInfo.statusText = "焊装待返修"; | 
|         mainInfo.partStatus = "焊装待返修"; | 
|       } | 
|       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.totalQuantity = total.totalQuantity; | 
|       mainInfo.partStatus = masterData.partStatus; | 
|       mainInfo.consignor_Id = 30; | 
|       mainInfo.consignorCode = "GX30"; | 
|       mainInfo.consignorName = "广州西门子"; | 
|       await this.setAccountInfo(mainInfo); | 
|       await this.dbWrite.save(mainInfo); // 保存主表信息,编辑和添加 | 
|   | 
|       for (let item of detailList) { | 
|         let detailInfo = new PurchaseOrderList(); | 
|         // 必须做这边操作,编辑时才执行更新操作 | 
|         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 = masterData.batchNumber; | 
|         detailInfo.plateType = item.plateType; | 
|         detailInfo.quantity = item.quantity; | 
|         // detailInfo.validQty = item.validQty; | 
|   | 
|         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 | 
| } |