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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
import BaseController from "../../baseController";
import { Post } from "egg-shell-decorators";
import { In, Like } from "typeorm";
import { BasePlate } from "../../../entity/basicInfo/base/basePlate";
import { BasePlateType } from "../../../entity/basicInfo/base/basePlateType";
import { BaseStorageArea } from "../../../entity/basicInfo/base/baseStorageArea";
import * as path from "path";
import * as XLSX from "xlsx";
 
export default class PlateController extends BaseController {
  //#region getListPlateType 获得器具种类
  /**
   * 获得器具种类
   */
  @Post()
  public async getListPlateType() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
 
    try {
      let whereStr = "";
      var where: any = {};
 
      // 超级管理员不需要走仓库权限
      if (userInfo.isAdministrator || body.isAll) {
        whereStr = `userProduct_Id=:userProduct_Id`;
        where = {
          userProduct_Id: userInfo.userProduct_Id
        };
      } else if (userInfo.userType === "consignor") {
        // 前端货主登录权限
        whereStr = `userProduct_Id=:userProduct_Id`;
        where = {
          userProduct_Id: userInfo.userProduct_Id
        };
      } else {
        whereStr = `userProduct_Id=:userProduct_Id`;
        where = {
          userProduct_Id: userInfo.userProduct_Id
        };
      }
      // 关键词查询条件
      // if (body.name) {
      //   let name = this.ctx.helper.sqlSecurity(body.name);
      //   whereStr += ` AND (productName LIKE '%' + :name + '%' OR productCode LIKE '%' + :name + '%')`;
      //   where.name = name;
      // }
      let fields = ["plateType", "plateType_Id"];
      // 自定义查询字段
      if (body.searchFields) {
        fields = body.searchFields;
      }
      if (body.appendField === "*") {
        fields = [];
      }
      let builder = this.dbRead.createQueryBuilder(BasePlateType, "t").where(whereStr, where);
      if (fields.length) {
        builder.select(fields);
      }
      builder.take(200);
      let dataList = [];
      if (body.appendField === "*") {
        dataList = await builder.getMany();
      } else {
        dataList = await builder.getRawMany();
      }
 
      this.info.result = true;
      this.info.data = dataList;
 
      ctx.body = this.info;
    } catch (ex) {
      let msg = "异常错误信息:" + ex.message;
      this.info.result = false;
      this.info.msg = msg;
      ctx.body = this.info;
    }
  }
  //#endregion
 
  //#region getListPlate 获得物料列表
  /**
   * 获得物料列表
   */
  @Post()
  public async getListPlate() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
 
    try {
      let whereStr = "";
      var where: any = {};
 
      // 超级管理员不需要走仓库权限
      if (userInfo.isAdministrator || body.isAll) {
        whereStr = `userProduct_Id=:userProduct_Id`;
        where = {
          userProduct_Id: userInfo.userProduct_Id
        };
      } else if (userInfo.userType === "consignor") {
        // 前端货主登录权限
        whereStr = `userProduct_Id=:userProduct_Id`;
        where = {
          userProduct_Id: userInfo.userProduct_Id
        };
      } else {
        whereStr = `userProduct_Id=:userProduct_Id`;
        where = {
          userProduct_Id: userInfo.userProduct_Id
        };
      }
      // 关键词查询条件
      // if (body.name) {
      //   let name = this.ctx.helper.sqlSecurity(body.name);
      //   whereStr += ` AND (productName LIKE '%' + :name + '%' OR productCode LIKE '%' + :name + '%')`;
      //   where.name = name;
      // }
      let fields = ["plateCode", "plate_Id"];
      // 自定义查询字段
      if (body.searchFields) {
        fields = body.searchFields;
      }
      if (body.appendField === "*") {
        fields = [];
      }
      let builder = this.dbRead.createQueryBuilder(BasePlate, "t").where(whereStr, where);
      if (fields.length) {
        builder.select(fields);
      }
      builder.take(200);
      let dataList = [];
      if (body.appendField === "*") {
        dataList = await builder.getMany();
      } else {
        dataList = await builder.getRawMany();
      }
 
      this.info.result = true;
      this.info.data = dataList;
 
      ctx.body = this.info;
    } catch (ex) {
      let msg = "异常错误信息:" + ex.message;
      this.info.result = false;
      this.info.msg = msg;
      ctx.body = this.info;
    }
  }
  //#endregion
 
  //#region getListStorage
  /**
   * 根据仓库名称获得TableSelect获得器具信息
   */
  @Post()
  public async getListStorage() {
    let userInfo = await this.userInfo;
    let { ctx } = this;
    let body = ctx.request.body;
    var dataList = await this.dbRead.find(BaseStorageArea, {
      where: {
        userProduct_Id: userInfo.userProduct_Id,
        storageName: body.name
      }
    });
    if (dataList.length == 0) {
      this.info.result = false;
      this.info.data = dataList;
    } else {
      this.info.result = true;
      this.info.data = dataList;
    }
 
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region getListPlates
  /**
   * getListPlates获得母托盘信息
   */
  @Post()
  public async getListPlates() {
    let userInfo = await this.userInfo;
    let { ctx } = this;
    let body = ctx.request.body;
    var dataList = await this.dbRead.find(BasePlate, {
      where: {
        userProduct_Id: userInfo.userProduct_Id,
        plateCode: body.plateCode
      }
    });
    if (dataList.length == 0) {
      this.info.result = false;
      this.info.msg = body.plateCode + "器具编号不存在";
    } else {
      this.info.result = true;
      this.info.data = dataList;
    }
 
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region getListCode
  /**
   * 获得TableSelect获得器具信息
   */
  @Post()
  public async getListCode() {
    let userInfo = await this.userInfo;
    let { ctx } = this;
 
    let body = ctx.request.body;
    await this.dbWrite.update(
      BasePlate,
      {
        userProduct_Id: userInfo.userProduct_Id,
 
        plateCode: body.plateCode
      },
      {
        plateState: "已组盘"
      }
    );
    this.info.result = true;
    // this.info.msg = "修改成功!";
    this.ctx.body = this.info;
    return;
  }
  //#endregion
 
  //#region getList
  /**
   * 获得TableSelect获得器具信息
   */
  @Post()
  public async getList() {
    let userInfo = await this.userInfo;
    let where: any = {
      userProduct_Id: userInfo.userProduct_Id
    };
    if (this.body.name) {
      where.plateCode = this.body.name;
    }
    if (this.body.ids) {
      where.plate_Id = In(this.body.ids);
    }
    var dataList = await this.dbRead.find(BasePlate, {
      where: where
    });
    if (dataList.length == 0) {
      this.info.result = false;
      this.info.data = dataList;
    } else {
      this.info.result = true;
      this.info.data = dataList;
    }
 
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region getListType
  /**
   * 获得TableSelect获得器具信息
   */
  @Post()
  public async getListType() {
    let userInfo = await this.userInfo;
    var dataList = await this.dbRead.find(BasePlateType, {
      where: {
        userProduct_Id: userInfo.userProduct_Id,
        plateType: Like("%" + this.body.name + "%")
        // plateType_Id: Like("%" + this.body.id + "%")
      }
    });
    if (dataList.length == 0) {
      this.info.result = false;
      this.info.data = dataList;
    } else {
      this.info.result = true;
      this.info.data = dataList;
    }
 
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region getPlateInfo 器具编号获取数据
  @Post()
  public async getPlateInfo() {
    let { ctx } = this;
    let body = ctx.request.body;
    try {
      let plateInfo = await this.dbRead.findOne(BasePlate, { plateCode: body.plateCode });
      if (!plateInfo) {
        this.info.result = false;
        this.info.msg = body.plateCode + "器具编号不存在";
        return;
      }
      this.info.result = true;
      this.info.data = plateInfo;
    } catch (error) {
      this.info.result = false;
      this.info.msg = "错误:" + error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 启用容器
  /**
   * 启用容器
   */
  @Post()
  public async plateOpen() {
    let { ctx } = this;
    let body = ctx.request.body;
    if (!Array.isArray(body.ids) || !body.ids.length) {
      this.info.result = false;
      this.info.msg = "没有可执行的单号";
      return;
    }
    try {
      // 更新启用为1
      await this.dbWrite.update(
        BasePlate,
        {
          plate_Id: In(body.ids)
        },
        {
          enable: 1
        }
      );
 
      this.info.result = true;
      this.info.msg = "容器启用成功";
    } catch (error) {
      this.info.result = false;
      this.info.msg = "容器启用失败";
    }
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region 停止容器
  /**
   * 停止容器
   */
  @Post()
  public async plateStop() {
    let { ctx } = this;
    let body = ctx.request.body;
    if (!Array.isArray(body.ids) || !body.ids.length) {
      this.info.result = false;
      this.info.msg = "没有可执行的单号";
      return;
    }
    try {
      // 更新启用为1
      await this.dbWrite.update(
        BasePlate,
        {
          plate_Id: In(body.ids)
        },
        {
          enable: 0
        }
      );
 
      this.info.result = true;
      this.info.msg = "容器停止成功";
    } catch (error) {
      this.info.result = false;
      this.info.msg = "容器停止失败";
    }
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region 生成容器号
  @Post()
  public async createPlantCode() {
    let { ctx } = this;
    let body = ctx.request.body;
    try {
      let plateInfo = await this.dbRead.findOne(BasePlate, {
        where: {
          plateCode: Like(body.data.codePrefix + "%")
        },
        order: {
          plate_Id: "DESC"
        }
      });
      let lastCode = 0; // 最后一条编码
      if (plateInfo) {
        lastCode = Number(plateInfo.plateCode.substring(2));
      }
      for (let index = 0; index < body.data.generate; index++) {
        lastCode++;
        let newPlateInfo = new BasePlate();
        let plateCode = "000000" + lastCode;
        newPlateInfo.plateCode = body.data.codePrefix + plateCode.substring(plateCode.length - 6);
        newPlateInfo.storage_Id = body.data.storage_Id;
        newPlateInfo.storageName = body.data.storageName;
        newPlateInfo.plateType = body.data.plateType;
        newPlateInfo.enable = 1;
        await this.setAccountInfo(newPlateInfo, "plate_Id");
        this.dbWrite.save(BasePlate, newPlateInfo);
      }
      this.info.result = true;
      this.info.msg = "生成成功";
    } catch (error) {
      this.info.result = false;
      this.info.msg = "生成失败";
    }
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region 更新图片 updateImages
  /**
   * 更新图片 updateImages
   */
  @Post()
  public async updateImages() {
    let { ctx } = this;
    let plate_Id = Number(this.body.plate_Id);
    if (!plate_Id) {
      this.info.msg = "器具不存在";
      this.info.result = false;
      ctx.body = this.info;
      return;
    }
 
    try {
      await this.dbWrite.update(BasePlate, this.body.plate_Id, {
        photo: this.body.photo
      });
 
      this.info.result = true;
      this.info.msg = "图片更新成功";
    } catch (ex) {
      this.info.msg = ex.message;
      this.info.result = false;
    }
    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 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;
      }
      let i = 1,
        successCount = 0,
        updateCount = 0;
 
      // 母托盘集合
      let plateList = await this.dbRead.find(BasePlate, {
        userProduct_Id: userInfo.userProduct_Id,
        plateCode: In(dataList.map(item => "" + item["器具编号"]))
      });
      let dataRows = []; //数据集合
      for (let row of dataList) {
        i++;
 
        // let plateInfo = await this.dbRead.findOne(BasePlate, {
        //   plateTypeCode: plateTypeCode
        // });
        let data = {
          // productName: "" + row["物料名称"],
          plateCode: row["母托盘编号"],
          plateType: row["母托盘种类"],
          plateTypeCode: row["种类编号"],
          plateName: row["母托盘名称"],
          areaCode: row["库区名称"],
          plateState: "空器具",
          length: row["长"],
          width: row["宽"],
          height: row["高"],
          currentLocation: row["当前位置"],
          plateWeight: row["母托盘自重kg"],
          packingQuantity: row["装箱数量"],
          remark: row["备注"]
        };
 
        // #region 校验数据
        if (!data.plateCode) {
          this.setMsg(`第${i}行、母托盘编号为空`, "red");
          updateCount++;
        }
        if (!data.plateType) {
          this.setMsg(`第${i}行、母托盘种类为空`, "red");
          updateCount++;
        }
        if (!data.plateTypeCode) {
          this.setMsg(`第${i}行、种类编号为空`, "red");
          updateCount++;
        }
        if (!data.plateName) {
          this.setMsg(`第${i}行、母托盘名称为空`, "red");
          updateCount++;
        }
        dataRows.push(data);
      }
 
      if (this.isMsgError) {
        this.setMsg(`导入数据有错误,请处理好重新导入`, "red");
        this.setMsg("-1");
        return;
      }
      //#endregion
 
      i = 1;
      successCount = 0;
      updateCount = 0;
      let rows = []; // 准备更新或者新增的数据
      for (let data of dataRows) {
        i++;
        // 根据物料编号查询物料是否存在
        let dataInfo = plateList.find(item => item.plateCode === data.plateCode);
        let isadd = true;
        if (!dataInfo) {
          dataInfo = new BasePlate(); // 新建数据
          isadd = false;
        }
 
        dataInfo = Object.assign(dataInfo, data); // 合并数据
        await this.setAccountInfo(dataInfo, "plate_Id");
        if (isadd == true) {
          this.setMsg(`第${i}行、${data.plateCode}已存在,正在更新...`);
          updateCount++;
          // 物料审核状态
          data.auditing = 0;
          rows.push(dataInfo);
        } else {
          this.setMsg(`第${i}行、${data.plateCode}正在导入...`);
          successCount++;
          rows.push(dataInfo);
        }
        if (i % 50 === 0) {
          await this.dbWrite.save(BasePlate, rows);
          rows = [];
        }
      }
      if (rows.length > 0) {
        await this.dbWrite.save(BasePlate, rows);
        rows = [];
      }
 
      this.setMsg(`导入成功,新增${successCount}条,更新${updateCount}条`, "blue");
    } catch (ex) {
      this.setMsg("出现异常:" + ex.message, "red");
      this.info.result = false;
    }
    this.setMsg("-1");
  }
  //#endregion
}