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
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
import BaseController from "../baseController";
import { Post } from "egg-shell-decorators";
import { BasePlate } from "../../entity/basicInfo/base/basePlate";
import { In, Not, MoreThan } from "typeorm";
import { SaleOrder } from "../../entity/outbound/sale/saleOrder";
import { SaleOrderList } from "../../entity/outbound/sale/saleOrderList";
import { vBaseProductPosition } from "../../entity/storage/product/vBaseProductPosition";
import * as mssql from "mssql";
import { SaleSortingRulePosition } from "../../entity/outbound/sortingrule/saleSortingRulePosition";
import { BaseProductPlaceHolder } from "../../entity/storage/storage/baseProductPlaceHolder";
 
/**
 * EU箱出库任务
 */
export default class OrderEuController 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([masterData.orderType])
      };
      // 编辑时排除自己
      if (masterData.order_Id) {
        where["order_Id"] = Not(masterData.order_Id);
      }
      let orderInfo = await this.dbRead.findOne(SaleOrder, 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 SaleOrder(); // 主表数据
      // 必须做这边操作,编辑时才执行更新操作
      mainInfo = Object.assign(mainInfo, masterData);
      // 新建时,生成单据编号
      if (!mainInfo.order_Id) {
        let code = await ctx.service.common.getCodeRegular(112);
        mainInfo.orderCode = code;
      }
 
      // 主表求和字段计算
      let total: any = detailList.reduce(
        (totalItem: any, currItem) => {
          totalItem.totalQuantity += currItem.quantityOrder;
          return totalItem;
        },
        { totalQuantity: 0 }
      );
 
      // 状态默认为新建
      if (!mainInfo.statusText) {
        mainInfo.statusText = "新建";
      }
      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.totalQuantityOrder = total.totalQuantity;
      mainInfo.consignor_Id = 30;
      mainInfo.consignorCode = "GX30";
      mainInfo.consignorName = "广州西门子";
      mainInfo.destinationName = masterData.destinationName;
      mainInfo.destination_Id = masterData.destination_Id;
      await this.setAccountInfo(mainInfo);
      await this.dbWrite.save(mainInfo); // 保存主表信息,编辑和添加
 
      for (let item of detailList) {
        let detailInfo = new SaleOrderList();
        // 必须做这边操作,编辑时才执行更新操作
        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 = item.batchNumber;
        detailInfo.plateType = item.plateType;
        detailInfo.quantityOrder = item.quantityOrder;
 
        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 scanCheckData() {
    let { ctx } = this;
    let body = ctx.request.body;
 
    try {
      if (!body.plateCode) {
        this.info.result = false;
        this.info.msg = `扫描标签不能为空!`;
        this.ctx.body = this.info;
        return;
      }
      if (!body.order_Id) {
        this.info.result = false;
        this.info.msg = `单号不能为空!`;
        this.ctx.body = this.info;
        return;
      }
      if (!body.orderList_Id) {
        this.info.result = false;
        this.info.msg = `明细不能为空!`;
        this.ctx.body = this.info;
        return;
      }
 
      let orderInfo = await this.dbRead.findOne(SaleOrder, body.order_Id);
      if (!orderInfo) {
        this.info.result = false;
        this.info.msg = `单号不存在!`;
        this.ctx.body = this.info;
        return;
      }
      let orderDetail = await this.dbRead.findOne(SaleOrderList, body.orderList_Id);
 
      // 没有分拣成功需要,判断库存是否充足
      if (orderInfo.sortingStatus !== 2) {
        let ppInfo = await this.dbRead.findOne(vBaseProductPosition, {
          plateCode: body.plateCode,
          validQty: MoreThan(0)
        });
 
        if (!ppInfo) {
          this.info.result = false;
          this.info.msg = `没有可用库存!`;
          this.ctx.body = this.info;
          return;
        }
 
        if (ppInfo.validQty < orderDetail.quantityOrder) {
          this.info.result = false;
          this.info.msg = `有效库为${ppInfo.validQty},库存不足!`;
          this.ctx.body = this.info;
          return;
        }
      } else {
        let holderInfo = await this.dbRead.findOne(BaseProductPlaceHolder, {
          mainID: body.order_Id
        });
        if (!holderInfo) {
          this.info.result = false;
          this.info.msg = `未分配库存!`;
          this.ctx.body = this.info;
          return;
        }
        if (holderInfo.plateCode !== body.plateCode) {
          this.info.result = false;
          this.info.msg = `当前已分配库存${holderInfo.plateCode},扫描不正确!`;
          this.ctx.body = this.info;
          return;
        }
      }
 
      // 先清空现有规则
      await this.dbWrite.delete(SaleSortingRulePosition, {
        order_Id: body.order_Id
      });
 
      // 添加分拣规则
      let regInfo = new SaleSortingRulePosition();
      regInfo.order_Id = orderDetail.order_Id;
      regInfo.orderList_Id = orderDetail.orderList_Id;
      regInfo.plateCode = body.plateCode;
      regInfo.product_Id = orderDetail.product_Id;
      regInfo.productCode = orderDetail.productCode;
      await this.dbWrite.save(regInfo);
 
      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 checkOut() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await this.userInfo;
    if (!body.order_Id) {
      this.info.result = false;
      this.info.msg = `单号不能为空!`;
      this.ctx.body = this.info;
      return;
    }
 
    try {
      const connection: any = await this.dbWrite.connection;
      let request = new mssql.Request(connection.driver.master);
      request.input("order_Id", body.order_Id);
      request.input("user_Id", userInfo.user_Id);
 
      request.output("outMsg", mssql.NVarChar(2000));
      let result = await request.execute("sp_Sale_Order_AutoOut");
      let outMsg = result.output.outMsg;
      if (outMsg != null && outMsg) {
        this.info.msg = outMsg;
        this.info.result = false;
      } else {
        this.info.msg = "确认出库成功";
        this.info.result = true;
      }
    } catch (error) {
      this.info.result = false;
      this.info.msg = "错误:" + error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
}