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
import BaseController from "../baseController";
import { Post } from "egg-shell-decorators";
import * as sql from "mssql";
import { RequireAllocationList } from "../../entity/outbound/require/requireAllocationList";
import { RequireAllocation } from "../../entity/outbound/require/requireAllocation";
import { In } from "typeorm";
import { vBaseProductPlaceHolder } from "../../entity/storage/storage/vBaseProductPlaceHolder";
import * as XLSX from "xlsx";
import * as path from "path";
import { isNumber } from "util";
import { BaseStorage } from "../../entity/basicInfo/base/baseStorage";
import { BaseProductInfo } from "../../entity/basicInfo/base/baseProductInfo";
import { BasePosition } from "../../entity/basicInfo/base/basePosition";
// import { BaseProductPosition } from '../../entity/storage/product/baseProductPosition';
/**
 * 调拨单管理
 */
export default class AllocationController extends BaseController {
  //#region 批量分拣
  @Post()
  public async batchSort() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    let allocation_Ids = body.allocation_Ids;
    let msg = [];
    if (!allocation_Ids) {
      this.info.result = false;
      this.info.msg = "信息不存在";
      this.ctx.body = this.info;
      return;
    }
    try {
      let allocations = await this.dbRead.find(RequireAllocation, {
        allocation_Id: In(allocation_Ids)
      });
      for (let allocationInfo of allocations) {
        let Allocationlist = await this.dbRead.find(RequireAllocationList, {
          allocation_Id: allocationInfo.allocation_Id
        });
        if (allocationInfo.allocationText != "审核成功") {
          this.info.msg = allocationInfo.allocationCode + "只有审核成功的单子才允许确认入库";
          this.info.result = false;
          this.ctx.body = this.info;
          return;
        }
        if (!allocationInfo.storage_Id_In) {
          this.info.msg = "调入仓库不能为空!";
          this.info.result = false;
          this.ctx.body = this.info;
          return;
        }
        if (Allocationlist.length <= 0) {
          this.info.msg = "明细为空不能分拣!";
          this.info.result = false;
          this.ctx.body = this.info;
          return;
        }
        for (let detail of Allocationlist) {
          if (!detail.positionName) {
            this.info.msg = "明细货位不能为空!";
            this.info.result = false;
            this.ctx.body = this.info;
            return;
          }
        }
 
        const connection: any = await this.dbWrite.connection;
        let request = new sql.Request(connection.driver.master);
        request.input("User_Id", userInfo.user_Id);
        request.input("allocation_Id", allocationInfo.allocation_Id);
        request.output("outMsg", sql.NVarChar(2000));
        let result = await request.execute("sp_Require_Allocation_Sorting");
        let outMsg = result.output.outMsg;
 
        if (outMsg) {
          this.info.msg = outMsg;
          this.info.result = false;
          ctx.body = this.info;
          return;
        }
        msg.push(allocationInfo.allocationCode + ",分拣完成!");
      }
      this.info.result = true;
      this.info.msg = msg.join(",");
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 确认调拨
  @Post()
  public async confirmAllocation() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    let allocation_Ids = body.allocation_Ids;
    let msg = [];
 
    if (!allocation_Ids) {
      this.info.result = false;
      this.info.msg = "信息不存在";
      this.ctx.body = this.info;
      return;
    }
    try {
      let allocations = await this.dbRead.find(RequireAllocation, {
        allocation_Id: In(allocation_Ids)
      });
      for (let allocationInfo of allocations) {
        let Allocationlist = await this.dbRead.find(RequireAllocationList, {
          allocation_Id: allocationInfo.allocation_Id
        });
        if (allocationInfo.allocationText != "审核成功") {
          this.info.msg = allocationInfo.allocationCode + "只有审核成功的单子才允许确认入库";
          this.info.result = false;
          this.ctx.body = this.info;
          return;
        }
        if (!allocationInfo.storage_Id_In) {
          this.info.msg = "调入仓库不能为空!";
          this.info.result = false;
          this.ctx.body = this.info;
          return;
        }
        if (Allocationlist.length <= 0) {
          this.info.msg = "明细为空不能分拣!";
          this.info.result = false;
          this.ctx.body = this.info;
          return;
        }
        for (let detail of Allocationlist) {
          if (!detail.positionName) {
            this.info.msg = "明细货位不能为空!";
            this.info.result = false;
            this.ctx.body = this.info;
            return;
          }
        }
 
        //执行确认操作
        const connection: any = await this.dbWrite.connection;
        let request = new sql.Request(connection.driver.master);
        request = new sql.Request(connection.driver.master);
        request.input("modifier", userInfo.userTrueName);
        request.input("allocation_Id", allocationInfo.allocation_Id);
        request.output("outMsg", sql.NVarChar(2000));
        try {
          let result = await request.execute("sp_Require_Allocation_Check");
          let outMsg = result.output.outMsg;
          if (outMsg) {
            msg.push(allocationInfo.allocationCode + "," + outMsg);
            this.info.msg = outMsg;
            this.info.result = false;
            ctx.body = this.info;
            continue;
          }
        } catch (e) {
          msg.push(allocationInfo.allocationCode + "执行错误:" + e.message);
          continue;
        }
        msg.push(allocationInfo.allocationCode + ",确认入库成功!");
      }
      this.info.result = true;
      this.info.msg = msg.join(",");
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 获得打印拣配单数据
  @Post()
  public async pickBillPrint() {
    let { ctx } = this;
    let body = ctx.request.body;
    let allocation_Ids = body.allocation_Ids;
    let userInfo = await ctx.helper.userInfo();
 
    if (!allocation_Ids) {
      this.info.result = false;
      this.info.msg = "信息不存在";
      this.ctx.body = this.info;
      return;
    }
    try {
      let allocations = await this.dbRead.find(RequireAllocation, {
        allocation_Id: In(allocation_Ids)
      });
      let allocationlist = await this.dbRead.find(RequireAllocationList, {
        allocation_Id: In(allocation_Ids)
      });
      let holderList = await this.dbRead.find(vBaseProductPlaceHolder, {
        userProduct_Id: userInfo.userProduct_Id,
        className: "要货调拨出库",
        mainID: In(allocation_Ids)
      });
      this.info.result = true;
      this.info.data = {
        allocations: allocations,
        allocationlist: allocationlist,
        holderList: holderList
      };
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 货主导入Excel
  @Post()
  public async importExcel() {
    setTimeout(async () => {
      await this.importExcelWork();
    }, 0);
 
    this.info.result = true;
    this.info.msg = "开始上传...";
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region importExcelWork
  private async importExcelWork() {
    let { ctx } = this;
    // let userInfo = await this.userInfo;
    let userInfo = await ctx.helper.userInfo();
    let body = ctx.request.body;
    let redis = ctx.app.redis.clients.get("common"); // 将消息放入redis缓存
    let fileUrl = body.fileUrl;
    redis.expire(body.key, 5 * 60);
    if (!fileUrl) {
      this.setMsg("上传文件不存在", "red");
      return;
    }
    if (!body.key) {
      this.setMsg("上传key不存在", "red");
      return;
    }
 
    try {
      let rootPath = path.resolve(); // 获得根目录
      let filePath = rootPath + path.sep + fileUrl.replace(/\//gi, path.sep); // 上传文件路径
      var workbook = XLSX.readFile(filePath); //整个 excel 文档
      var sheetNames = workbook.SheetNames; //获取所有工作薄名
      var sheet1 = workbook.Sheets[sheetNames[0]]; //根据工作薄名获取工作薄
      let dataList = XLSX.utils.sheet_to_json(sheet1); // 获得当前sheet表单数据转为json格式
 
      //#region 验证数据正确性
      this.info.result = true;
      if (!dataList.length) {
        this.setMsg("没有可导入的数据", "red");
        return;
      }
      //#endregion
      let storageList = await this.dbRead.find(BaseStorage, {
        userProduct_Id: userInfo.userProduct_Id
      });
      let productinfo = await this.dbRead.find(BaseProductInfo, {
        productCode: In(dataList.map(item => "" + item["物料编号"])),
        userProduct_Id: userInfo.userProduct_Id
      });
      let position = await this.dbRead.find(BasePosition, {
        positionName: In(dataList.map(item => "" + item["入库货位"])),
        userProduct_Id: userInfo.userProduct_Id
      });
      let i = 0;
      // let dataRows = []; // 数据集合
      for (let row of dataList) {
        i += 1;
        if (i % 20 === 1) {
          this.setMsg(`第${i}行开始校验`, "blue");
        }
        let outstorage = storageList.find(item => item.storageName === row["调出仓库"]);
        if (!outstorage) {
          this.setMsg(`${i}、调出仓库不存在`, "red");
          this.info.result = false;
        }
        let instorage = storageList.find(item => item.storageName === row["调入仓库"]);
        if (!instorage) {
          this.setMsg(`${i}、调入仓库不存在`, "red");
          this.info.result = false;
        }
        if (!row["调拨日期"]) {
          this.setMsg(`${i}、调出仓库不能为空`, "red");
          this.info.result = false;
        }
        let productname = productinfo.find(item => item.productCode === row["物料编号"]);
        if (!productname) {
          this.setMsg(`${i}、产品编号不能为空`, "red");
          this.info.result = false;
        }
        if (!row["产品名称"]) {
          this.setMsg(`${i}、产品名称不能为空`, "red");
          this.info.result = false;
        }
        if (!row["条形码"]) {
          this.setMsg(`${i}、条形码不能为空`, "red");
          this.info.result = false;
        }
 
        let quantity = row["要货数量"];
        if (quantity && !isNumber(quantity)) {
          this.setMsg(`${i}要货数量[${quantity}]数量请输入正整数!`, "red");
        }
        let inhuowei = position.find(item => item.positionName === row["入库货位"]);
        if (!inhuowei) {
          this.setMsg(`${i}、入库货位不存在`, "red");
          this.info.result = false;
        }
        // if (!row["要货单号"]) {
        //   this.setMsg(`${i}、要货单号不能为空`, "red");
        //   this.info.result = false;
        // }
      }
      if (this.isMsgError) {
        this.setMsg(`导入数据有错误,请处理好重新导入`, "red");
        this.setMsg("-1");
        return;
      }
 
      // 分组
      let groupList = dataList.reduce(
        (all: Array<any>, next) =>
          all.some(item => item["调出仓库"] == next["调出仓库"] && item["调入仓库"] == next["调入仓库"] && item["调拨日期"] == next["调拨日期"])
            ? all
            : [...all, next],
        []
      );
      i = 0;
      // 循环处理分组数据,groupList对应的是主表数据
      for (let item of groupList) {
        i++;
        if (i % 20 === 1) {
          this.setMsg(`第${i}行开始导入`, "blue");
        }
        let outstorage = storageList.find(row => row.storageName === item["调出仓库"]);
        let instorage = storageList.find(row => row.storageName === item["调入仓库"]);
 
        let Allocation = new RequireAllocation();
 
        let allocationCode = await ctx.service.common.getCodeRegular(1742);
        Allocation.allocationCode = allocationCode;
        Allocation.allocationText = "新建";
        Allocation.storageName = item["调出仓库"];
        Allocation.storage_Id = outstorage.storage_Id;
        Allocation.storageName_In = item["调入仓库"];
        Allocation.storage_Id_In = instorage.storage_Id;
        Allocation.sortingStatus = 1;
        Allocation.sortingDate = null;
        Allocation.allocationDate = new Date();
        await this.setAccountInfo(Allocation);
        // 明细数据
        let detailList = dataList.filter(
          row => row["调出仓库"] === item["调出仓库"] && row["调入仓库"] === item["调入仓库"] && row["调拨日期"] === item["调拨日期"]
        );
        // 主表求和字段计算
        let total: any = detailList.reduce(
          (totalItem: any, currItem) => {
            totalItem.totalQuantity += currItem["要货数量"];
            totalItem.ProductMoney += currItem["物料金额"];
            return totalItem;
          },
          { totalQuantity: 0, purchaseMoney: 0 }
        );
        Allocation.totalQuantity = total.totalQuantity;
        Allocation.productMoney = total.purchaseMoney;
        // 保存主表
        await this.dbWrite.save(Allocation);
        // let allocationInfo = await this.dbRead.findOne()
        // 处理明细,将明细挂载到主表下
        for (let detail of detailList) {
          let productCode = detail["物料编号"];
          let product = await this.dbRead.findOne(BaseProductInfo, {
            productCode: productCode
          });
          let AllocationList = new RequireAllocationList();
          AllocationList.productCode = productCode;
          AllocationList.product_Id = product.product_Id;
          AllocationList.productModel = product.productModel;
          AllocationList.productName = product.productName;
          AllocationList.productSpec = product.productSpec;
          AllocationList.goodsQuantity = detail["要货数量"];
          AllocationList.sortingQuantity = 0;
          AllocationList.positionName = detail["入库货位"];
          AllocationList.productPrice = product.purchasePrice;
          AllocationList.productTotal = Number(detail["要货数量"]) * (product.purchasePrice || 0);
          AllocationList.goodsCode = detail["要货单号"];
 
          AllocationList.createDate = new Date();
          AllocationList.creator = userInfo.userTrueName;
          AllocationList.createID = userInfo.user_Id;
          // //建立关系
          AllocationList.requireAllocation = Allocation;
          await this.dbWrite.save(AllocationList);
        }
        this.setMsg(`第${i}行导入成功`, "blue");
      }
      // redis.rpush(body.key, `${i}、第${i}行导入成功`);
      // this.setMsg(`${i}、第${i}行导入成功`, "blue");
      // this.setMsg(`第${i}行导入成功`, "blue");
    } catch (ex) {
      this.setMsg("出现异常:" + ex.message, "red");
      this.info.result = false;
    }
    this.setMsg("-1");
  }
  //#endregion
}