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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import { default as BaseController } from "../baseController";
import { Post } from "egg-shell-decorators";
import { BaseProductInfo } from "../../entity/basicInfo/base/baseProductInfo";
import { BaseEmptyEquipment } from "../../entity/inbound/purchase/baseEmptyEquipment";
import { BasePlate } from "../../entity/basicInfo/base/basePlate";
import { BasePlateType } from "../../entity/basicInfo/base/basePlateType";
import { BaseEmptyEquipmentList } from "../../entity/inbound/purchase/baseEmptyEquipmentList";
import { BaseProductPosition } from "../../entity/storage/product/baseProductPosition";
import { MoreThan, Not, Raw } from "typeorm";
import { vBaseProductPosition } from "../../entity/storage/product/vBaseProductPosition";
 
/**
 * 空器具出库
 */
export default class EmptyEquipmentController extends BaseController {
  //#region 空器具出库详情
  @Post()
  public async getquipmentList() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    try {
      let sql = ` SELECT emptyEquipment_Id,emptyEquipmentCode,statusText,consignorName,totalQuantity,storageName, checkout, destinationName, (SELECT COUNT(*) AS unfinishedNub FROM dbo.Base_EmptyEquipmentList WHERE EmptyEquipment_Id = B.EmptyEquipment_Id) AS muber
       FROM Base_EmptyEquipment B 
       WHERE   B.emptyEquipment_Id = @0 and  B.userProduct_Id=@1`;
      let quipment = await this.dbRead.query(sql, [body.emptyEquipmentCode, userInfo.userProduct_Id]);
      var emptyEquipment_Id = quipment[0].emptyEquipment_Id;
      sql = `  SELECT plateCode,plateName,currentLocation,productName,quantity,currentArea FROM Base_EmptyEquipmentList AS A  LEFT JOIN Base_EmptyEquipment AS B ON A.EmptyEquipment_Id =B.EmptyEquipment_Id 
      WHERE  B.emptyEquipment_Id = @0 and B.userProduct_Id =@1 `;
      let quipmentList = await this.dbRead.query(sql, [emptyEquipment_Id, userInfo.userProduct_Id]);
 
      this.info.data = quipment;
      this.info.data2 = quipmentList;
    } catch (error) {
      this.info.result = false;
      this.info.msg = "错误:" + error.message;
    }
    this.info.result = true;
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 获取物料信息关联器具种类数量
  @Post()
  public async onChangeProductCode() {
    let { ctx } = this;
    let body = ctx.request.body;
    let product_Id = body.product_Id;
    try {
      let prodInfo = await this.dbRead.findOne(BaseProductInfo, {
        where: {
          product_Id: product_Id
        }
      });
      // 空器具数量
      let count = await this.dbRead.count(BaseProductPosition, {
        where: {
          plateTypeCode: prodInfo.plateTypeCode,
          product_Id: 0, // 0为空器具
          productStorage: MoreThan(0),
          // 排除已经分配给其他空器具任务中的器具
          productCode: Raw(() => {
            return `NOT EXISTS(Select 1 
              From Base_EmptyEquipment E INNER JOIN Base_EmptyEquipmentList L ON E.emptyEquipment_Id=L.emptyEquipment_Id
              Where BaseProductPosition.plateCode=L.plateCode And E.StatusText IN('新建', '部分出库'))`;
          })
        }
      });
      this.info.result = true;
      this.info.data = { count: count };
    } catch (error) {
      this.info.result = false;
      this.info.msg = "错误:" + error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#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 (!masterData.demandQuantity || masterData.demandQuantity <= 0) {
        this.info.result = false;
        this.info.msg = `物料编号${masterData.productCode}数量必须大于0`;
        this.ctx.body = this.info;
        return;
      }
      let where = {
        product_Id: masterData.product_Id,
        userProduct_Id: userInfo.userProduct_Id,
        statusText: "新建"
      };
      // 编辑时排除自己
      if (masterData.emptyEquipment_Id) {
        where["emptyEquipment_Id"] = Not(masterData.emptyEquipment_Id);
      }
      let orderInfo = await this.dbRead.findOne(BaseEmptyEquipment, where);
      if (orderInfo) {
        this.info.result = false;
        this.info.msg = `物料编号${masterData.productCode}已创建,不可重复创建!`;
        this.ctx.body = this.info;
        return;
      }
 
      let productInfo = await this.dbRead.findOne(BaseProductInfo, {
        where: {
          product_Id: masterData.product_Id
        }
      });
      let plateType = await this.dbRead.findOne(BasePlateType, {
        where: {
          plateType: productInfo.plateType
        }
      });
 
      let mainInfo = new BaseEmptyEquipment(); // 主表数据
      // 必须做这边操作,编辑时才执行更新操作
      mainInfo = Object.assign(mainInfo, masterData);
      // 新建时,生成单据编号
      if (!mainInfo.emptyEquipment_Id) {
        let code = await ctx.service.common.getCodeRegular(1808);
        mainInfo.emptyEquipmentCode = code;
      }
 
      if (plateType) {
        mainInfo.storageName = plateType.storageName;
        mainInfo.storage_Id = plateType.storage_Id;
      }
      // 获取空器具库存
      let ppList = [];
 
      // 当明细为空时,自动添加从库存中添加器具
      if (!detailList.length) {
        ppList = await this.dbRead.find(vBaseProductPosition, {
          where: {
            product_Id: 0,
            plateTypeCode: productInfo.plateTypeCode,
            productStorage: MoreThan(0),
            productCode: Raw(() => {
              return `NOT EXISTS(Select 1 
              From Base_EmptyEquipment E INNER JOIN Base_EmptyEquipmentList L ON E.emptyEquipment_Id=L.emptyEquipment_Id
              Where vBaseProductPosition.plateCode=L.plateCode And E.StatusText IN('新建', '部分出库', '下架中'))`;
            })
          },
          take: masterData.demandQuantity
        });
        if (ppList.length < masterData.demandQuantity) {
          this.info.result = false;
          this.info.msg = `库存不足!`;
          this.ctx.body = this.info;
          return;
        }
      }
      // 状态默认为新建
      if (!mainInfo.statusText) {
        mainInfo.statusText = "新建";
      }
      mainInfo.orderType = "空器具出库";
      mainInfo.product_Id = productInfo.product_Id;
      mainInfo.productCode = productInfo.productCode;
      mainInfo.productName = productInfo.productName + "(空器具)";
      mainInfo.createDate = new Date();
      mainInfo.createID = userInfo.user_Id;
      mainInfo.creator = userInfo.userTrueName;
      mainInfo.totalQuantity = masterData.totalQuantity;
      mainInfo.demandQuantity = masterData.demandQuantity;
      mainInfo.consignor_Id = 30;
      mainInfo.consignorCode = "GX30";
      mainInfo.consignorName = "广州西门子";
      await this.setAccountInfo(mainInfo);
      await this.dbWrite.save(mainInfo); // 保存主表信息,编辑和添加
 
      // 当明细为空时,自动添加从库存中添加器具
      if (!this.body.detailList.length) {
        // 清空明细
        await this.dbWrite.delete(BaseEmptyEquipmentList, {
          emptyEquipment_Id: mainInfo.emptyEquipment_Id
        });
 
        for (let item of ppList) {
          let detailInfo = new BaseEmptyEquipmentList();
          // 必须做这边操作,编辑时才执行更新操作
          detailInfo = Object.assign(detailInfo, item);
          detailInfo.emptyEquipment_Id = mainInfo.emptyEquipment_Id;
          detailInfo.status = "新建";
 
          await this.setAccountInfo(detailInfo);
          await this.dbWrite.save(detailInfo); // 保存明细数据
          details.push(detailInfo);
        }
      } else {
        // 保存手工添加的器具
        for (let item of this.body.detailList) {
          let detailInfo = new BaseEmptyEquipmentList();
          // 必须做这边操作,编辑时才执行更新操作
          detailInfo = Object.assign(detailInfo, item);
          detailInfo.emptyEquipment_Id = mainInfo.emptyEquipment_Id;
          detailInfo.status = "新建";
 
          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 confirm() {
    let { ctx } = this;
    let body = ctx.request.body;
    let emptyEquipment_Id = body.emptyEquipment_Id;
    try {
      let equipmentList = await this.dbRead.find(BaseEmptyEquipmentList, {
        emptyEquipment_Id: emptyEquipment_Id,
        status: "新建"
      });
      if (!equipmentList.length) {
        this.info.result = false;
        this.info.msg = "没有可用的器具编号";
        ctx.body = this.info;
        return;
      }
      for (let equipInfo of equipmentList) {
        let taskInfo = await this.ctx.service.wcs.createTaskQueueEmptyOut(equipInfo);
        if (taskInfo === "-1") {
          this.info.result = false;
          this.info.msg = equipInfo.plateCode + "出库指令下发失败, 输送线不存在!";
          ctx.body = this.info;
          return;
        }
        if (taskInfo === "-2") {
          this.info.result = false;
          this.info.msg = `器具编号${equipInfo.plateCode}库存不存在!`;
          ctx.body = this.info;
          return;
        }
      }
      // 更新单据状态为下架中
      await this.dbWrite.update(
        BaseEmptyEquipment,
        {
          emptyEquipment_Id: emptyEquipment_Id
        },
        {
          statusText: "下架中"
        }
      );
 
      this.info.msg = "出库指令成功!";
      this.info.result = true;
    } catch (error) {
      this.info.result = false;
      this.info.msg = error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region  空器具出库任务 器具编号获取详情
  @Post()
  public async getappplateCode() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    try {
      let plateInfo = await this.dbRead.findOne(BasePlate, {
        plateCode: body.plateCode,
        userProduct_Id: userInfo.userProduct_Id
      });
      this.info.data = plateInfo;
    } catch (error) {
      this.info.result = false;
      this.info.msg = "错误:" + error.message;
    }
    this.info.result = true;
    ctx.body = this.info;
  }
  //#endregion
}