schangxiang@126.com
2025-09-09 3d8966ba2c81e7e0365c8b123e861d18ee4f94f5
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
}