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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import BaseController from "../baseController";
import { Post } from "egg-shell-decorators";
import { vBaseProductPosition } from "../../entity/storage/product/vBaseProductPosition";
import { SaleOrderPicking } from "../../entity/outbound/manufacture/saleOrderPicking";
import { SaleOrderPickingList } from "../../entity/outbound/manufacture/saleOrderPickingList";
import { BaseProductPosition } from "../../entity/storage/product/baseProductPosition";
import { MoreThan } from "typeorm";
import { SaleOrder } from "../../entity/outbound/sale/saleOrder";
import { BaseForklift } from "../../entity/basicInfo/workshop/baseForklift";
 
export default class OrderJxController extends BaseController {
  //#region 出库分拣区扫描获取数据
  @Post()
  public async getScanData() {
    let { ctx } = this;
    // 前台传过来的数据
    let body = ctx.request.body;
    // 登录名等信息
    let userInfo = await ctx.helper.userInfo();
    try {
      // 分拣根据托盘号 extendField02出库数量  extendField03为1出库完成待分拣的数据
      let datalist = null;
      if (body.isall) {
        datalist = await this.dbRead.find(vBaseProductPosition, {
          userProduct_Id: userInfo.userProduct_Id,
          plateCode: body.plateCode,
          productStorage: MoreThan(0),
          //extendField02: MoreThan("0"), //还原为原始版本 【Editby shaocx,2024-02-29】
          extendField03: "1"
        });
      } else {
        datalist = await this.dbRead.find(vBaseProductPosition, {
          userProduct_Id: userInfo.userProduct_Id,
          plateCode: body.plateCode,
          productStorage: MoreThan(0),
          extendField02: MoreThan("0"),
          extendField03: "1"
        });
      }
 
      // 查到数据就返回  否则就不存在
      if (datalist.length) {
        this.info.data = datalist;
        this.info.result = true;
      } else {
        this.info.result = false;
        this.info.msg = `${body.plateCode}不存在`;
      }
    } catch (error) {
      this.info.result = false;
      this.info.msg = "错误:" + error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 扫描获取数据
  @Post()
  public async endOrder() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    try {
      if (!body.plateCode) {
        this.info.result = false;
        this.info.msg = `器具编号不能为空`;
        ctx.body = this.info;
        return;
      }
      let saleOrderInfo = await this.dbRead.findOne(SaleOrder, {
        where: {
          userProduct_Id: userInfo.userProduct_Id,
          statusText: "已完成",
          plateCode: body.plateCode
        },
        order: { createDate: "DESC" }
      });
      if (saleOrderInfo) {
        // 修改叉车司机状态为空闲中
        await this.dbWrite.update(
          BaseForklift,
          {
            relationDrivers: saleOrderInfo.relationDrivers
          },
          {
            forkliftStatus: "空闲中"
          }
        );
        saleOrderInfo.statusText = "出库口取货完成";
        await this.dbWrite.save(saleOrderInfo); // 保存主表信息
        this.info.data = saleOrderInfo;
        this.info.result = true;
      } else {
        this.info.result = false;
        this.info.msg = `${body.plateCode}已完成出库单不存在`;
      }
    } 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;
 
    let mainInfo = new SaleOrderPicking(); // 主表数据
    // 必须做这边操作,编辑时才执行更新操作
    mainInfo = Object.assign(mainInfo, masterData);
 
    try {
      // 新建时,生成单据编号
      if (!mainInfo.orderPicking_Id) {
        let orderPickingCode = await ctx.service.common.getCodeRegular(116);
        mainInfo.orderPickingCode = orderPickingCode;
      }
 
      // 主表求和字段计算
      let total: any = detailList.reduce(
        (totalItem: any, currItem) => {
          totalItem.totalQuanity += currItem.quantity;
          return totalItem;
        },
        { totalQuanity: 0 }
      );
 
      // 状态默认为新建
      if (!mainInfo.statusText) {
        mainInfo.statusText = "待上架";
      }
      mainInfo.orderType = body.orderType;
      mainInfo.storage_Id = masterData.storage_Id;
      mainInfo.storageName = masterData.storageName;
      mainInfo.plateType = masterData.plateType;
      mainInfo.plateCode = masterData.plateCode;
      mainInfo.createDate = new Date();
      mainInfo.createID = userInfo.user_Id;
      mainInfo.creator = userInfo.userTrueName;
      mainInfo.totalQuanity = total.totalQuanity;
      await this.setAccountInfo(mainInfo);
      await this.dbWrite.save(mainInfo); // 保存主表信息
 
      let detailInfo = new SaleOrderPickingList();
      for (let item of detailList) {
        // 必须做这边操作,编辑时才执行更新操作
        detailInfo = Object.assign(detailInfo, item);
 
        detailInfo.orderPicking_Id = mainInfo.orderPicking_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.plateType = item.plateType;
        detailInfo.quantity = item.quantityOrder;
        detailInfo.validQty = item.validQty;
 
        await this.setAccountInfo(detailInfo);
        await this.dbWrite.save(detailInfo); // 保存明细数据
        details.push(detailInfo);
      }
      // 库存校验
      for (let detail of details) {
        let ppInfo = await this.dbRead.findOne(BaseProductPosition, {
          plateCode: masterData.plateCode,
          product_Id: detail.product_Id,
          productStorage: MoreThan(0)
        });
        if (!ppInfo || ppInfo.productStorage < detail.quantity) {
          this.info.result = false;
          this.info.msg = `${masterData.plateCode}器具没有库存`;
          ctx.body = this.info;
          return;
        }
      }
 
      // 扣减库存
      for (let detail of details) {
        let ppInfo = await this.dbRead.findOne(BaseProductPosition, {
          plateCode: mainInfo.plateCode,
          product_Id: detail.product_Id,
          productStorage: MoreThan(0)
        });
 
        // 将库存设置为0
        await this.dbWrite.update(
          BaseProductPosition,
          {
            productPosition_Id: ppInfo.productPosition_Id
          },
          {
            productStorage: ppInfo.productStorage - detail.quantity,
            storageStatus: "余料"
          }
        );
      }
 
      // 将单据状态修改为确认完成
      mainInfo.statusText = "已出库";
      await this.dbWrite.save(mainInfo);
      this.info.result = true;
      this.info.msg = "确认出库成功";
 
      this.info.data = mainInfo;
      this.info.data2 = details;
    } catch (error) {
      this.info.result = false;
      this.info.msg = "错误:" + error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region save 确认出库
  @Post()
  public async confirm() {
    let { ctx } = this;
    let body = ctx.request.body;
    let id = body.id;
    let masterData = await this.dbRead.findOne(SaleOrderPicking, id);
 
    try {
      let details = await this.dbRead.find(SaleOrderPickingList, {
        orderPicking_Id: id
      });
      // 库存校验
      for (let detail of details) {
        let ppInfo = await this.dbRead.findOne(BaseProductPosition, {
          plateCode: masterData.plateCode,
          product_Id: detail.product_Id,
          productStorage: MoreThan(0)
        });
        if (!ppInfo || ppInfo.productStorage < detail.quantity) {
          this.info.result = false;
          this.info.msg = `${masterData.plateCode}器具没有库存`;
          ctx.body = this.info;
          return;
        }
      }
 
      // 扣减库存
      for (let detail of details) {
        let ppInfo = await this.dbRead.findOne(BaseProductPosition, {
          plateCode: masterData.plateCode,
          product_Id: detail.product_Id,
          productStorage: MoreThan(0)
        });
 
        // 将库存设置为0
        await this.dbWrite.update(
          BaseProductPosition,
          {
            productPosition_Id: ppInfo.productPosition_Id
          },
          {
            productStorage: ppInfo.productStorage - detail.quantity,
            storageStatus: "余料"
          }
        );
      }
 
      // 将单据状态修改为确认完成
      masterData.statusText = "已出库";
      await this.dbWrite.save(masterData);
 
      this.info.result = true;
      this.info.msg = "确认出库成功";
    } catch (error) {
      this.info.result = false;
      this.info.msg = "错误:" + error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
}