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
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
import { default as BaseController } from "../baseController";
import { Post } from "egg-shell-decorators";
import { StorageEnter } from "../../entity/storage/operation/storageEnter";
import { StorageEnterList } from "../../entity/storage/operation/storageEnterList";
import { BasePosition } from "../../entity/basicInfo/base/basePosition";
import * as sql from "mssql";
import * as path from "path";
import * as XLSX from "xlsx";
import moment = require("moment");
import { isNumber } from "util";
import { BaseProductInfo } from "../../entity/basicInfo/base/baseProductInfo";
 
export default class EnterController extends BaseController {
  //#region 其他入库审核
  @Post()
  public async singleConfirm() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    let enter_Ids = body.enter_Ids;
    try {
      for (let data of enter_Ids) {
        let storageEnterInfo = await this.dbRead.findOne(StorageEnter, {
          enter_Id: data
        });
        let checkStorageEnterList = await this.dbRead.find(StorageEnterList, {
          enter_Id: data
        });
        let errorMsg = "";
        for (let mInfo of checkStorageEnterList) {
          let where: any = {
            storage_Id: storageEnterInfo.enter_Id,
            positionName: mInfo.positionName,
            enable: 1
          };
          let basePosition = await this.dbRead.find(BasePosition, where);
          if (!basePosition) {
            errorMsg += mInfo.positionName + "货位不正确或已经停用无法操作<br/>";
          }
        }
 
        if (errorMsg) {
          this.info.result = false;
          this.info.msg = errorMsg;
          ctx.body = this.info;
        }
        const connection: any = await this.dbWrite.connection;
        let request = new sql.Request(connection.driver.master);
        request.input("enter_Id", data);
        request.input("modifier", userInfo.userTrueName);
        request.output("outMsg", sql.NVarChar(2000));
        let result = await request.execute("sp_Storage_Enter_Check");
        let msg = result.output.outMsg;
 
        let enterinfo = await this.dbRead.findOne(StorageEnter, {
          enter_Id: data
        });
        if (!msg) {
          this.info.result = true;
          this.info.msg = "确认成功!";
        } else {
          this.info.result = false;
          await this.dbWrite.update(StorageEnter, enterinfo.enter_Id, {
            auditor: userInfo.userTrueName,
            auditing: 3,
            auditDate: new Date()
          });
          this.info.msg = "执行失败!" + msg;
        }
      }
      ctx.body = this.info;
    } catch (error) {
      this.info.result = false;
      this.info.msg = "执行失败!" + error.message;
      ctx.body = this.info;
    }
  }
  //    #endregion
 
  //#region 导入Excel
  @Post()
  public async importExcel() {
    setTimeout(async () => {
      await this.importExcelWork();
    }, 0);
 
    this.info.result = true;
    this.ctx.body = this.info;
  }
 
  private async importExcelWork() {
    let userInfo = await this.userInfo;
    let startDate = new Date();
    let { ctx } = this;
    let body = ctx.request.body;
    let redis = ctx.app.redis.clients.get("common"); // 将消息放入redis缓存
    let fileUrl = body.url;
    redis.expire(body.key, 5 * 60);
    if (!body.key) {
      this.setMsg(body.key, "上传key不存在");
      return;
    }
    if (!fileUrl) {
      redis.rpush(body.key, "上传文件不存在");
      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: Array<any> = XLSX.utils.sheet_to_json(sheet1); // 获得当前sheet表单数据转为json格式
 
      //#region 验证数据正确性
      this.info.result = true;
      if (!dataList.length) {
        this.setMsg(`没有可导入的数据`, "red");
        return;
      }
      this.isMsgError = false;
      let msg = "";
 
      let excelDT = new sql.Table();
      excelDT.columns.add("ConsignorName", sql.NVarChar(50));
      excelDT.columns.add("StorageName", sql.NVarChar(50));
      excelDT.columns.add("ProviderShortName", sql.NVarChar(50));
      excelDT.columns.add("ProductCode", sql.NVarChar(50));
      excelDT.columns.add("ProductName", sql.NVarChar(50));
      excelDT.columns.add("Quantity", sql.Int);
      excelDT.columns.add("PositionName ", sql.NVarChar(50));
      excelDT.columns.add("PlateCode", sql.NVarChar(50));
      excelDT.columns.add("BatchNumber", sql.NVarChar(50));
      excelDT.columns.add("ProduceDate", sql.DateTime);
      excelDT.columns.add("ProductSpec", sql.NVarChar(50));
      excelDT.columns.add("userTrueName", sql.NVarChar(50));
      excelDT.columns.add("weight", sql.Decimal(12, 4));
      excelDT.columns.add("totalWeight", sql.Decimal(12, 4));
 
      let i = 0;
      for (var dataItem of dataList) {
        i++;
        if (!isNumber(dataItem["数量"])) {
          this.setMsg(`${i}、第${i + 1}行,数量值【${dataItem["数量"]}】格式错误!`, "red");
        }
 
        // 日期处理,日期格式为
        let produceDate = dataItem["生产日期"];
        if (produceDate) {
          if (!isNumber(produceDate)) {
            this.setMsg(`${i}、第${i + 1}行,生产日期值【${produceDate}】格式错误!`, "red");
          }
          let d = moment(new Date(1900, 0, produceDate - 1));
          produceDate = d.toDate();
        }
        let productCode = dataItem["物料编号"];
        let prodInfo = await this.dbRead.findOne(BaseProductInfo, {
          productCode: productCode,
          userProduct_Id: userInfo.userProduct_Id
        });
        if (!prodInfo) {
          this.setMsg(`${i}、第${i + 1}行,物料编号【${productCode}】不存在!`, "red");
        }
        let weight = dataItem["单位毛重"] || prodInfo.weight;
        let totalWeight = weight * dataItem["数量"];
        excelDT.rows.add(
          dataItem["货主名称"],
          dataItem["仓库名称"],
          dataItem["供应商"],
          dataItem["物料编号"],
          dataItem["物料名称"],
          dataItem["数量"],
          dataItem["货位名称"],
          dataItem["拍号"],
          dataItem["批次号"],
          produceDate,
          dataItem["物料规格"] || prodInfo.productSpec,
          dataItem["经手人"],
          weight,
          totalWeight
        );
      }
      if (this.isMsgError) {
        this.setMsg(`导入数据有错误,请处理好重新导入`, "red");
        this.setMsg("-1");
        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("excelDT", excelDT);
      request.output("outMsg", sql.NVarChar(2000));
      let result = await request.execute("sp_Storage_Enter_Import");
      let outMsg = result.output.outMsg;
      if (outMsg) {
        this.setMsg(outMsg, "red");
      } else {
        outMsg = "入库单导入成功";
        this.setMsg(outMsg, "blue");
      }
 
      let endDate = moment(Date.now());
      let totalSeconds = endDate.diff(startDate, "seconds");
      msg = `导入完成,共耗时${totalSeconds}秒`;
      this.setMsg(msg);
    } catch (ex) {
      this.setMsg("出现异常:" + ex.message, "red");
    }
 
    this.setMsg("-1");
  }
  //#endregion
}