333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//#region
import { default as BaseController } from "../baseController";
import { Post } from "egg-shell-decorators";
import { PurchaseOrder } from "../../entity/inbound/purchase/purchaseOrder";
import { PurchaseOrderList } from "../../entity/inbound/purchase/purchaseOrderList";
//#endregion
 
/**
 * 外协件入库
 */
export default class OrderOutController 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;
 
    let mainInfo = new PurchaseOrder(); // 主表数据
    // 必须做这边操作,编辑时才执行更新操作
    mainInfo = Object.assign(mainInfo, masterData);
 
    try {
      // 新建时,生成单据编号
      if (!mainInfo.order_Id) {
        let orderCode = await ctx.service.common.getCodeRegular(102);
        mainInfo.orderCode = orderCode;
      }
 
      // 主表求和字段计算
      let total: any = detailList.reduce(
        (totalItem: any, currItem) => {
          totalItem.totalQuantity += currItem.quantity;
          return totalItem;
        },
        { totalQuantity: 0 }
      );
 
      // 状态默认为新建
      if (!mainInfo.statusText) {
        mainInfo.statusText = "新建";
      }
      mainInfo.orderType = "外协件入库";
      mainInfo.storage_Id = masterData.storage_Id;
      mainInfo.storageName = masterData.storageName;
      mainInfo.createDate = new Date();
      mainInfo.createID = userInfo.user_Id;
      mainInfo.creator = userInfo.userTrueName;
      mainInfo.totalQuantity = total.totalQuantity;
      mainInfo.applyDate = new Date();
      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.quantity = item.quantity;
 
        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
}