333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
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
import { default as BaseController } from "../baseController";
import { Post } from "egg-shell-decorators";
import * as XLSX from "xlsx";
import * as path from "path";
import { BasePosition } from "../../entity/basicInfo/base/basePosition";
import { BaseStorage } from "../../entity/basicInfo/base/baseStorage";
 
/**
 * 货位管理
 */
export default class providerController extends BaseController {
  //#region 批量导入
  /**
   * 批量导入入口,异步处理
   */
  @Post()
  public async import() {
    setTimeout(async () => {
      await this.importWork();
    }, 0);
 
    this.info.result = true;
    this.ctx.body = this.info;
  }
  /**
   * 批量导入业务处理
   */
  private async importWork() {
    let { ctx } = this;
    // let startDate = new Date();
    let body = ctx.request.body;
    // let userInfo = await ctx.helper.userInfo();
    let fileUrl = body.fileUrl;
    // let details = [];
    if (!fileUrl) {
      this.setMsg(`上传文件不存在`, "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 cangku = await this.dbRead.find(BaseStorage);
      let index = 0;
      for (let item of dataList) {
        let _index = index + 1;
        // let providerCode = ""+ ["供应商编号"];
        let positionName = "" + item["货位名称"];
        let storageName = "" + item["所属仓库"];
 
        if (_index % 20 === 1) {
          this.setMsg(`第${_index}行开始校验`, "blue");
        }
        if (!positionName || !storageName) {
          this.setMsg(`第${_index}、标黄[供应商简称、供应商全称、货主名称]部分数据不能为空]`, "red");
        }
        let Provider = await this.dbRead.findOne(BasePosition, {
          positionName: positionName
        });
        if (Provider) {
          // this.setMsg(`第${_index}行,供应商简称在供应商表中已存在,请修改后在导入!`);
          this.setMsg(`第${_index}行,货位已存在,请修改后在导入!`);
          this.setMsg("-1");
          return;
        }
        let consigoninfo = await this.dbRead.findOne(BaseStorage, {
          storageName: storageName
        });
        // let Isconsig = cangku.find(item => { item.storageName === storageName; });
        if (!consigoninfo) {
          this.setMsg(`第${_index}行,货主信息不存在,请修改后在导入!`);
        }
        index++;
      }
      if (this.isMsgError) {
        this.setMsg(`导入数据有错误,请处理好重新导入`, "red");
        this.setMsg("-1");
        return;
      }
      let count = 0;
      for (let row of dataList) {
        count += 1;
        let dates = await this.dbRead.findOne(BaseStorage, {
          storageName: row["所属仓库"]
        });
 
        let Position = new BasePosition();
        // Position.providerCode = await ctx.service.common.getCodeRegular(256);
        Position.positionName = row["货位名称"];
        Position.storageName = row["所属仓库"];
        Position.storageName = dates.storageName;
        Position.storage_Id = dates.storage_Id;
        // Position.storageCode = dates.storageCode;
 
        Position.maxCapacity = row["最大容量"];
        Position.positionType = row["货位类型"];
        Position.isLocked = row["是否锁定"];
        Position.isMixProduct = row["是否混物料"];
        Position.enable = row["是否可用"];
        Position.minCapacity = row["最低库存"];
        Position.areaCode = row["库区"];
        Position.rowCode = row["温层"];
        Position.enable = 1;
        await this.setAccountInfo(Position);
        await this.dbWrite.save(Position);
      }
      this.setMsg(`导入成功,共导入${count}条`, "blue");
      //#endregion
    } catch (ex) {
      this.setMsg("出现异常:" + ex.message, "red");
    }
    this.setMsg("-1");
  }
  //#endregion
}