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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//#region import
import { default as BaseController } from "../baseController";
import * as sql from "mssql";
import { Post } from "egg-shell-decorators";
import { SaleOrder } from "../../entity/outbound/sale/saleOrder";
import { In } from "typeorm";
import { vBaseProductPlaceHolder } from "../../entity/storage/storage/vBaseProductPlaceHolder";
import { SaleOrderList } from "../../entity/outbound/sale/saleOrderList";
//#endregion
 
/**
 * 出库 - 生成批次
 */
export default class OrderBatchController extends BaseController {
  //#region 预览或者保存 Handle
  /**
   * 预览或者保存
   */
  @Post()
  public async createOrderPrint() {
    let { ctx } = this;
    let body = ctx.request.body;
 
    try {
      let finalWhere = "";
      let opName = this.getOpName(body.op);
      //#region 生成where条件
      switch (body.op) {
        case "order":
          finalWhere = await this.getOrderWhere(body.where, body.selectIDs);
          break;
      }
      //#endregion
 
      //将数据保存到临时表
      this.info = await this.previewTmp(finalWhere, body.op, opName, body.count);
      if (this.info.result && body.type == "save") {
        this.info = await this.save();
      }
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region getOpName
  private getOpName(op): string {
    let opName = "";
    switch (op) {
      case "diqu":
        opName = "地区";
        break;
      case "qudao":
        opName = "渠道";
        break;
      case "kuqu":
        opName = "库区";
        break;
      case "huozhu":
        opName = "货主";
        break;
      case "danpin":
        opName = "单品";
        break;
      case "danjian":
        opName = "单件";
        break;
      case "duopin":
        opName = "多品";
        break;
      case "kuaidi":
        opName = "快递";
        break;
      case "order":
        opName = "订单";
        break;
      default:
        opName = "无";
        break;
    }
    return opName;
  }
  //#endregion
 
  //#region getOrderWhere
  /**
   * 将选中的订单生成波次
   * 2016/3/1
   */
  private async getOrderWhere(where, orderIds) {
    let { ctx } = this;
    let userInfo = await ctx.helper.userInfo();
 
    if (!userInfo.isAdministrator) {
      if (where) where += " And ";
      where += " consignor_Id in(SELECT Node_Id FROM dbo.Sys_RoleAuthData where DataType_Id=1 AND user_Id=" + userInfo.user_Id + ")";
    }
    if (where) where += " And ";
    where += " Order_Id IN(" + orderIds + ")";
    return where;
  }
  //#endregion
 
  //#region 根据条件把数据存入到临时表
  /**
   * 先根据条件把波次单数据创建到临时表(通用)
   */
  private async previewTmp(where, op, opName, orderCount: number) {
    let { ctx } = this;
    let userInfo = await ctx.helper.userInfo();
    let sessionId = userInfo.user_Id;
 
    const connection: any = await this.dbWrite.connection;
    let request = new sql.Request(connection.driver.master);
    request.input("where", where);
    request.input("sessionId", sessionId);
    request.input("user_Id", userInfo.user_Id);
    request.input("op", op);
    request.input("opName", opName);
    request.input("orderCount", orderCount);
    request.output("msg", sql.NVarChar(2000));
    let result = await request.execute("sp_SaleOrderBatch_SaveTmpData");
    let msg = result.output.msg;
 
    if (msg) {
      this.info.result = false;
      this.info.msg = msg;
    } else {
      this.info.result = true;
    }
    return this.info;
  }
  //#endregion
 
  //#region 保存-通过sessionid读取临时表中的数据
  private async save() {
    let { ctx } = this;
    let userInfo = await ctx.helper.userInfo();
    let sessionId = userInfo.user_Id;
 
    const connection: any = await this.dbWrite.connection;
    let request = new sql.Request(connection.driver.master);
    request.input("sessionId", sessionId);
    request.input("user_Id", userInfo.user_Id);
    request.input("subBatch", this.body.subBatch);
    request.output("msg", sql.NVarChar(2000));
    let result = await request.execute("sp_SaleOrderBatch_Save");
    let msg = result.output.msg;
 
    this.info.result = true;
    this.info.msg = "生成成功,波次单号:[" + msg + "]";
    return this.info;
  }
  //#endregion
 
  //#region getDropdownList 获得下拉框数据
  @Post()
  public async getDropdownList() {
    let userInfo = await this.userInfo;
    let fixedWhere = this.body.fixedWhere;
    let where = await this.ctx.service.common.parseWhere(fixedWhere);
    where = Object.assign(where, {
      userProduct_Id: userInfo.userProduct_Id,
      sortingStatus: 2,
      statusText: "审核成功"
    });
    let orderList = await this.dbRead.find(SaleOrder, {
      select: [
        "order_Id",
        "storage_Id",
        "storageName",
        "consignor_Id",
        "consignorCode",
        "consignorName",
        "clientShortName",
        "expressCorp_Id",
        "expressCode",
        "expressCorpName",
        "orderChannel",
        "shippingName",
        "mobile",
        "provinceName",
        "cityName",
        "regionName"
      ],
      where: where
    });
    // 仓库分组
    let storageList = orderList.reduce(
      (all: Array<SaleOrder>, next) => (all.some(item => item.storage_Id === next.storage_Id) ? all : [...all, next]),
      []
    );
    // 货主分组
    let consignorList = orderList.reduce(
      (all: Array<SaleOrder>, next) => (all.some(item => item.consignor_Id === next.consignor_Id) ? all : [...all, next]),
      []
    );
    let ids = orderList.map(item => item.order_Id);
    if (!ids.length) {
      ids = [0];
    }
 
    // 获得占位库区
    let holderList = await this.dbRead.find(vBaseProductPlaceHolder, {
      select: ["areaCode"],
      where: {
        userProduct_Id: userInfo.userProduct_Id,
        mainID: In(ids)
      }
    });
    // // 库区分组
    let storageAreaList = holderList.reduce(
      (all: Array<vBaseProductPlaceHolder>, next) => (all.some(item => item.areaCode === next.areaCode) ? all : [...all, next]),
      []
    );
 
    // 客户分组
    let clientList = orderList.reduce(
      (all: Array<SaleOrder>, next) => (all.some(item => item.clientShortName === next.clientShortName) ? all : [...all, next]),
      []
    );
 
    // 快递公司分组
    let expressList = orderList.reduce(
      (all: Array<SaleOrder>, next) => (all.some(item => item.expressCorp_Id === next.expressCorp_Id) ? all : [...all, next]),
      []
    );
 
    // 渠道分组
    let channelList = orderList.reduce(
      (all: Array<SaleOrder>, next) => (all.some(item => item.orderChannel === next.orderChannel) ? all : [...all, next]),
      []
    );
 
    // 收货人分组
    let shippingNameList = orderList.reduce(
      (all: Array<SaleOrder>, next) => (all.some(item => item.shippingName === next.shippingName) ? all : [...all, next]),
      []
    );
 
    // 电话分组
    let mobileList = orderList.reduce((all: Array<SaleOrder>, next) => (all.some(item => item.mobile === next.mobile) ? all : [...all, next]), []);
 
    // 省分组
    let provinceList = orderList.reduce(
      (all: Array<SaleOrder>, next) => (all.some(item => item.provinceName === next.provinceName) ? all : [...all, next]),
      []
    );
 
    // 市分组
    let cityList = orderList.reduce((all: Array<SaleOrder>, next) => (all.some(item => item.cityName === next.cityName) ? all : [...all, next]), []);
 
    // 区分组
    let regionList = orderList.reduce(
      (all: Array<SaleOrder>, next) => (all.some(item => item.regionName === next.regionName) ? all : [...all, next]),
      []
    );
 
    // 获得物料数据
    let productList = await this.dbRead.find(SaleOrderList, {
      select: ["product_Id", "productCode", "productModel", "productName", "order_Id", "quantityOrder"],
      where: {
        order_Id: In(ids)
      }
    });
 
    // 物料名称
    let productNameList = productList.reduce(
      (all: Array<SaleOrderList>, next) => (all.some(item => item.productName === next.productName) ? all : [...all, next]),
      []
    );
 
    // 物料编号分组
    let productCodeList = productList.reduce(
      (all: Array<SaleOrderList>, next) => (all.some(item => item.productCode === next.productCode) ? all : [...all, next]),
      []
    );
 
    // 明细订单分组
    let qtyList = productList.reduce(
      (all: Array<SaleOrderList>, next) => (all.some(item => item.order_Id === next.order_Id) ? all : [...all, next]),
      []
    );
 
    // 物料条码分组
    let productModelList = productList.reduce(
      (all: Array<SaleOrderList>, next) => (all.some(item => item.productModel === next.productModel) ? all : [...all, next]),
      []
    );
 
    // 获得每个订单数量
    let detailQtyList = qtyList.map(item => {
      let total = productList
        .filter(row => row.order_Id === item.order_Id)
        .reduce(
          (totalItem: any, currItem) => {
            totalItem.totalQuantityOrder += currItem.quantityOrder;
            return totalItem;
          },
          { totalQuantityOrder: 0 }
        );
      return {
        totalQuantityOrder: total.totalQuantityOrder
      };
    });
    // 获得每个订单明细条数
    let detailCountList = qtyList.map(item => {
      let count = productList.filter(row => row.order_Id === item.order_Id).length;
      return {
        count: count
      };
    });
 
    // 构建下拉框数据
    let dropdownList: any = {
      // 仓库
      storageList: storageList.map(item => {
        return {
          value: item.storage_Id,
          label: item.storageName
        };
      }),
      // 货主
      consignorList: consignorList.map((item: SaleOrder) => {
        return {
          value: item.consignor_Id,
          label: item.consignorName
        };
      }),
      // 库区
      storageAreaList: storageAreaList.map(item => {
        return {
          value: item.areaCode,
          label: item.areaCode
        };
      }),
      // 客户
      clientList: clientList.map(item => {
        return {
          value: item.clientShortName,
          label: item.clientShortName
        };
      }),
      // 快递公司
      expressList: expressList.map(item => {
        return {
          value: item.expressCorp_Id,
          label: item.expressCorpName
        };
      }),
      // 快递公司
      channelList: channelList.map(item => {
        return {
          value: item.orderChannel,
          label: item.orderChannel
        };
      }),
      // 收货人
      shippingNameList: shippingNameList.map(item => {
        return {
          value: item.shippingName,
          label: item.shippingName
        };
      }),
      // 电话
      mobileList: mobileList.map(item => {
        return {
          value: item.mobile,
          label: item.mobile
        };
      }),
      // 省
      provinceList: provinceList.map(item => {
        return {
          value: item.provinceName,
          label: item.provinceName
        };
      }),
      // 市
      cityList: cityList.map(item => {
        return {
          value: item.cityName,
          label: item.cityName
        };
      }),
      // 区
      regionList: regionList.map(item => {
        return {
          value: item.regionName,
          label: item.regionName
        };
      }),
      // 物料名称
      productNameList: productNameList.map(item => {
        return {
          value: item.productName,
          label: item.productName
        };
      }),
      // 物料编号
      productCodeList: productCodeList.map(item => {
        return {
          value: item.productCode,
          label: item.productCode
        };
      }),
      // 物料条码
      productModelList: productModelList.map(item => {
        return {
          value: item.productModel,
          label: item.productModel
        };
      }),
      // 物料数量
      detailQtyList: detailQtyList
        .reduce((all: Array<any>, next) => (all.some(item => item.totalQuantityOrder == next.totalQuantityOrder) ? all : [...all, next]), [])
        .map(item => {
          return {
            value: item.totalQuantityOrder,
            label: item.totalQuantityOrder
          };
        }),
      // 物料条数
      detailCountList: detailCountList
        .reduce((all: Array<any>, next) => (all.some(item => item.count == next.count) ? all : [...all, next]), [])
        .map(item => {
          return {
            value: item.count,
            label: item.count
          };
        })
    };
 
    this.info.result = true;
    this.info.data = dropdownList;
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region 获得子波次数据
  /**
   * 获得子波次数据
   */
  @Post()
  public async getSubBatch() {
    let { ctx } = this;
    let body = ctx.request.body;
 
    try {
      let sql = `SELECT   S.sub_Id, S.subOrderPrintCode, S.orderPrint_Id, S.orderPrintList_Id, S.Remark, 
        S.createID, S.creator, S.createDate, S.areaCode, L.allotPositionName, 
        L.positionName, L.product_Id, L.productCode, L.productName, L.productModel, L.productSpec, L.quantityOrder, 
        L.storeOrderCode, L.orderCode
        FROM      Sale_OrderPrintSub AS S INNER JOIN
        Sale_OrderPrintList AS L ON S.OrderPrintList_Id = L.OrderPrintList_Id
        where L.orderPrint_Id=@0`;
      let dataList = await this.dbRead.query(sql, [body.orderPrint_Id]);
 
      this.info.data = dataList;
      this.info.result = true;
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
}