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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { default as BaseController } from "../baseController";
import { Post } from "egg-shell-decorators";
import { BaseForklift } from "../../entity/basicInfo/workshop/baseForklift";
import { BaseEmptyEquipmentReturn } from "../../entity/outbound/sale/baseEmptyEquipmentReturn";
import { BaseDestination } from "../../entity/basicInfo/workshop/baseDestination";
import { BasePlate } from "../../entity/basicInfo/base/basePlate";
import { BaseEmptyEquipmentReturnList } from "../../entity/outbound/sale/baseEmptyEquipmentReturnList";
import { Not, Like } from "typeorm";
// import { BaseEmptyEquipment } from '../../entity/inbound/purchase/baseEmptyEquipment';
 
/**
 * 空器具出库
 */
export default class quipmentReturnController extends BaseController {
  //#region 空器具出库详情
  @Post()
  public async save() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    try {
      if (!body.destination_Id) {
        this.info.result = false;
        this.info.msg = "所在工位不能为空!";
        ctx.body = this.info;
        return;
      }
      let destInfo = await this.dbRead.findOne(BaseDestination, body.destination_Id);
      if (!destInfo) {
        this.info.result = false;
        this.info.msg = "所在工位不能为空!";
        ctx.body = this.info;
        return;
      }
      if (!body.storage_Id) {
        this.info.result = false;
        this.info.msg = "仓库不能为空!";
        ctx.body = this.info;
        return;
      }
 
      if (!body.totalQuantity) {
        this.info.result = false;
        this.info.msg = "数量不能为空!";
        ctx.body = this.info;
        return;
      }
 
      let quipmentReturn = new BaseEmptyEquipmentReturn();
      quipmentReturn.emptyEquipmentReturnCode = await this.ctx.service.common.getCodeRegular(1807);
      quipmentReturn.orderType = "空器具回库";
      quipmentReturn.statusText = "新建";
      quipmentReturn.totalQuantity = body.totalQuantity;
      quipmentReturn.destination_Id = destInfo.destination_Id;
      quipmentReturn.destinationName = destInfo.destinationName;
      quipmentReturn.storage_Id = body.storage_Id;
      quipmentReturn.storageName = body.storageName;
      quipmentReturn.consignor_Id = 30;
      quipmentReturn.consignorCode = "GX30";
      quipmentReturn.consignorName = "广州西门子";
 
      // 获取到叉车为空闲中并选中目的地和叉车目的地相同的叉车
      let forklift = await this.dbRead.findOne(BaseForklift, {
        forkliftStatus: "空闲中",
        userProduct_Id: userInfo.userProduct_Id,
        destination_Id: Like("%" + this.body.destination_Id + "%")
      });
 
      if (forklift) {
        // 已分配的几号叉车
        quipmentReturn.allocatedForklift = forklift.forkliftName;
        // 叉车司机
        quipmentReturn.relationDrivers = forklift.relationDrivers;
      }
 
      quipmentReturn.createDate = new Date();
      quipmentReturn.createID = userInfo.user_Id;
      quipmentReturn.creator = userInfo.userTrueName;
      await this.setAccountInfo(quipmentReturn);
      await this.dbWrite.save(quipmentReturn);
      if (forklift) {
        await this.dbWrite.update(
          BaseForklift,
          {
            forklift_Id: forklift.forklift_Id
          },
          {
            forkliftStatus: "任务中"
          }
        );
      }
 
      this.info.msg = "保存成功";
      this.info.result = true;
    } catch (error) {
      this.info.result = false;
      this.info.msg = "错误:" + error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region scanSave 扫描保存数据
  @Post()
  public async scanSave() {
    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;
      }
      if (!masterData.storage_Id) {
        this.info.result = false;
        this.info.msg = `仓库不能为空`;
        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;
      }
 
      // 判断是否重复创建
      for (let item of detailList) {
        let where = {
          plateCode: item.plateCode,
          returnStatus: "新建"
        };
        // 编辑时排除自己
        if (masterData.emptyEquipmentReturn_Id) {
          where["emptyEquipmentReturn_Id"] = Not(masterData.emptyEquipmentReturn_Id);
        }
        let detailInfo = await this.dbRead.findOne(BaseEmptyEquipmentReturnList, where);
        if (detailInfo) {
          let mainInfo = await this.dbRead.findOne(BaseEmptyEquipmentReturn, detailInfo.emptyEquipmentReturn_Id);
          this.info.result = false;
          this.info.msg = `器具编号${item.plateCode}已在${mainInfo.emptyEquipmentReturnCode}中创建,不可重复创建!`;
          this.ctx.body = this.info;
          return;
        }
      }
 
      let mainInfo = new BaseEmptyEquipmentReturn(); // 主表数据
      // 必须做这边操作,编辑时才执行更新操作
      mainInfo = Object.assign(mainInfo, masterData);
      // 新建时,生成单据编号
      if (!mainInfo.emptyEquipmentReturn_Id) {
        let code = await ctx.service.common.getCodeRegular(1807);
        mainInfo.emptyEquipmentReturnCode = code;
      }
 
      // 状态默认为新建
      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.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 BaseEmptyEquipmentReturnList();
        // 必须做这边操作,编辑时才执行更新操作
        detailInfo = Object.assign(detailInfo, item);
 
        detailInfo.emptyEquipmentReturn_Id = mainInfo.emptyEquipmentReturn_Id;
        detailInfo.productCode = item.plateCode;
        detailInfo.product_Id = 0;
        detailInfo.productName = item.plateName;
        detailInfo.plateCode = item.plateCode;
        detailInfo.plateType = item.plateType;
        detailInfo.returnStatus = "新建";
 
        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
}