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
import * as sql from "mssql";
import BaseController from "../baseController";
import { Post } from "egg-shell-decorators";
import { SaleOrderPrint } from "../../entity/outbound/manufacture/saleOrderPrint";
import { SaleOrderPicking } from "../../entity/outbound/manufacture/saleOrderPicking";
import { SaleOrderPrintList } from "../../entity/outbound/manufacture/saleOrderPrintList";
/**
 * 扫拍下架
 */
export default class OrderPickingController extends BaseController {
  //#region app-根据波次单号获取下架理货位
  @Post()
  public async getPickingOrderInfo() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    let orderPrintCode = body.orderPrintCode; // 盘点单号
    try {
      if (!orderPrintCode) {
        this.info.result = false;
        this.info.msg = "波次单号不能为空!";
        ctx.body = this.info;
        return;
      }
      // 对主表数据进行检验
      let dataInfo = await this.dbRead
        .createQueryBuilder(SaleOrderPrint, "t")
        .where("orderPrintCode=:orderPrintCode", {
          orderPrintCode: orderPrintCode
        })
        .getOne();
      if (!dataInfo) {
        this.info.msg = "未找到波次数据!";
        this.info.result = false;
        ctx.body = this.info;
        return;
      }
      if (dataInfo.user_Id != userInfo.user_Id) {
        this.info.msg = "该波次单已被其他用户占有!";
        this.info.result = false;
        ctx.body = this.info;
        return;
      }
      // 获取波次对应的明细信息
      let info = await ctx.service.outbound.pickingPaiScanService.getData(orderPrintCode, userInfo);
      if (info.result) {
        // 更新这个波次单为当前用户所有
        let sql = `UPDATE dbo.Sale_OrderPrint 
                  SET User_Id=u.User_Id,UserTrueName=u.UserTrueName,PickingStatus='拣货中',StatusID=5,StatusText='拣货中'
                  FROM dbo.Sale_OrderPrint s,dbo.Sys_User u 
                  WHERE s.OrderPrint_Id=@0 AND u.User_Id=@1`;
        await this.dbRead.query(sql, [dataInfo.orderPrint_Id, userInfo.user_Id]);
        let sqlOne = `UPDATE dbo.Sys_User SET PDAOperateDate=GETDATE() WHERE User_Id= @0`;
        await this.dbRead.query(sqlOne, [userInfo.user_Id]);
      }
      this.info.msg = info.msg;
      this.info.data = info.data;
      this.info.result = info.result;
    } catch (error) {
      this, (this.info.result = false);
      this.info.msg = "错误:" + error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region app-根据波次号和拍号获取数据
  @Post()
  public async getPlateStorage() {
    let { ctx } = this;
    let body = ctx.request.body;
    let orderPrintCode = body.orderPrintCode; // 盘点单号
    let plateCode = body.plateCode; // 拍号
    try {
      if (!orderPrintCode) {
        this.info.result = false;
        this.info.msg = "波次单号不能为空!";
        ctx.body = this.info;
        return;
      }
      if (!plateCode) {
        this.info.result = false;
        this.info.msg = "拍号不能为空!";
        ctx.body = this.info;
        return;
      }
      // 获取波次对应的明细信息
      this.info = await ctx.service.outbound.pickingPaiScanService.getHoldingStorageByPlateCode(
        orderPrintCode,
        plateCode
      );
    } catch (error) {
      this, (this.info.result = false);
      this.info.msg = "错误:" + error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region  保存扫描结果
  /**
   * 接货完成
   */
  @Post()
  public async saveScan() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    try {
      let batchCode = body.batchCode; //波次号
      let cartCode = body.cartCode; //车号
      let dataList = body.dataList;
      let orderPrint: SaleOrderPrint = await this.dbRead.findOne(SaleOrderPrint, {
        orderPrintCode: batchCode
      });
 
      let printListModel = await this.dbRead.find(SaleOrderPrintList, {
        orderPrint_Id: orderPrint.orderPrint_Id
      });
 
      //#region 校验
 
      if (!batchCode) {
        this.info.msg = "波次不能为空!";
        ctx.body = this.info;
        return;
      }
      if (!dataList || dataList.length == 0) {
        this.info.msg = "请先扫描条码!";
        ctx.body = this.info;
        return;
      }
      if (orderPrint == null) {
        this.info.msg = "波次不存在!";
        ctx.body = this.info;
        return;
      }
      if (dataList.PickingStatus == "等待配货") {
        this.info.msg = "波次单[" + dataList.orderPrintCode + "]已经完成拣货,不能再拣货!";
        ctx.body = this.info;
        return;
      }
      let orderPicking = await this.dbRead.find(SaleOrderPicking, {
        orderPrintCode: batchCode,
        user_Id: userInfo.user_Id
      });
      if (orderPicking == null) {
        this.info.msg = "拣货记录不存在!";
        ctx.body = this.info;
        return;
      }
 
      //#endregion
 
      //#region 存储过程
      let data = new sql.Table();
      data.columns.add("productModel", sql.NVarChar(50));
      data.columns.add("productCode", sql.NVarChar(50));
      data.columns.add("positionName", sql.NVarChar(50));
      data.columns.add("offPosition", sql.NVarChar(50));
      data.columns.add("plateCode", sql.NVarChar(50));
      data.columns.add("scanCount", sql.BigInt);
      data.columns.add("weight", sql.Decimal(14, 4));
      data.columns.add("totalWeight", sql.Decimal(14, 4));
      for (var _detail of dataList) {
        data.rows.add(
          _detail.productModel,
          _detail.productCode,
          _detail.positionName,
          _detail.offPosition,
          _detail.plateCode,
          _detail.scanCount,
          _detail.weight,
          _detail.totalWeight
        );
      }
      const connection: any = await this.dbWrite.connection;
      let request = new sql.Request(connection.driver.master);
      request.input("batchCode", batchCode);
      request.input("cartCode", cartCode);
      request.input("user_Id", userInfo.user_Id);
      request.input("userProduct_Id", userInfo.userProduct_Id);
      request.input("data", data);
      request.output("msg", sql.NVarChar(2000));
      let result = await request.execute("sp_ScanPickingSave");
      let msg = result.output.msg;
 
      if (msg && msg != "部分拣货") {
        this.info.result = false;
        this.info.msg = msg;
        ctx.body = this.info;
        return;
      } else {
        this.info.result = true;
      }
      //#endregion
 
      if (msg == "部分拣货") {
        this.info.result = true;
        this.info.state = "部分下架";
        this.info.msg = "提交成功,已部分下架成功!";
      } else {
        // 对单据进行分组
        let groupData = printListModel.reduce(
          (all: Array<any>, next) => (all.some(item => item["order_Id"] == next["order_Id"]) ? all : [...all, next]),
          []
        );
 
        for (var g of groupData) {
          ctx.service.outbound.orderHelper.setStatusHistory(g.order_Id, 5, 6, "订单状态", "订单拣货");
        }
      }
 
      ctx.body = this.info;
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
}