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
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
import { default as BaseController } from "../baseController";
import { Post } from "egg-shell-decorators";
import * as mssql 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";
import { BaseStorage } from "../../entity/basicInfo/base/baseStorage";
import { StorageOuter } from "../../entity/storage/operation/storageOuter";
import { In } from "typeorm";
 
export default class OuterController extends BaseController {
  //#region 开始分拣
  @Post()
  public async startSorting() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
 
    try {
      if (body.outer_Id) {
        const connection: any = await this.dbWrite.connection;
        let request = new mssql.Request(connection.driver.master);
        request.input("outer_Id", body.outer_Id);
        request.input("user_Id", userInfo.user_Id);
        request.output("outMsg", mssql.NVarChar(2000));
        let result = await request.execute("sp_Storage_Outer_Sorting");
        let outMsg = result.output.outMsg;
        if (outMsg) {
          this.info.msg = outMsg;
          this.info.result = false;
          this.ctx.body = this.info;
          return;
        }
      }
      this.info.msg = "分拣完毕";
      this.info.result = true;
      this.ctx.body = this.info;
    } catch (ex) {
      this.info.msg = ex.message + "数据分拣失败请重试!";
      this.info.result = false;
      this.ctx.body = this.info;
    }
  }
  //#endregion
 
  //#region 确认出库
  @Post()
  public async onConfirm() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
 
    try {
      if (body.outer_Id) {
        const connection: any = await this.dbWrite.connection;
        let request = new mssql.Request(connection.driver.master);
 
        request.input("outer_Id", body.outer_Id);
        request.input("user_Id", userInfo.user_Id);
        request.input("userTrueName", userInfo.userTrueName);
        await request.execute("sp_Storage_Outer_Check");
      }
      this.info.msg = "审核成功";
      this.info.result = true;
      this.ctx.body = this.info;
    } catch (ex) {
      this.info.msg = ex.message + "数据分拣失败请重试!";
      this.info.result = false;
      this.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;
  }
 
  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;
    let startDate = new Date();
    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: Array<any> = XLSX.utils.sheet_to_json(sheet1); // 获得当前sheet表单数据转为json格式
 
      //#region 验证数据正确性
      this.info.result = true;
      if (!dataList.length) {
        this.setMsg(`没有可导入的数据`, "red");
        return;
      }
      //#endregion
 
      this.isMsgError = false;
      let msg = "";
 
      let excelDT = new mssql.Table();
      excelDT.columns.add("ConsignorName", mssql.NVarChar(50));
      excelDT.columns.add("StorageName", mssql.NVarChar(50));
      excelDT.columns.add("ClientShortName", mssql.NVarChar(50));
      excelDT.columns.add("ProductCode", mssql.NVarChar(50));
      excelDT.columns.add("ProductName", mssql.NVarChar(50));
      excelDT.columns.add("Quantity", mssql.Int);
      excelDT.columns.add("PositionName ", mssql.NVarChar(50));
      excelDT.columns.add("PlateCode", mssql.NVarChar(50));
      excelDT.columns.add("BatchNumber", mssql.NVarChar(50));
      excelDT.columns.add("ProductSpec", mssql.NVarChar(50));
      excelDT.columns.add("userTrueName", mssql.NVarChar(50));
      excelDT.columns.add("weight", mssql.Decimal(12, 4));
      excelDT.columns.add("totalWeight", mssql.Decimal(12, 4));
 
      let i = 0;
      for (var dataItem of dataList) {
        i++;
        if (!isNumber(dataItem["数量"])) {
          this.setMsg(`${i}、第${i + 1}行,数量值【${dataItem["数量"]}】格式错误!`, "red");
          return;
        }
        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");
          return;
        }
        //#region 校验数据
        var storageName = dataItem["仓库名称"];
        let typeInfo = await this.dbRead.findOne(BaseStorage, {
          userProduct_Id: userInfo.userProduct_Id,
          storageName: storageName
        });
        if (!typeInfo) {
          this.setMsg(`${i}、第${i}行、${storageName}仓库不存在,请核实信息!`, "red");
          return;
        }
        //#endregion
 
        excelDT.rows.add(
          dataItem["货主名称"],
          dataItem["仓库名称"],
          dataItem["客户名称"],
          dataItem["物料编号"],
          dataItem["物料名称"],
          dataItem["数量"],
          dataItem["出库货位"],
          dataItem["拍号"],
          dataItem["批次号"],
          dataItem["物料规格"] || prodInfo.productSpec,
          dataItem["经手人"],
          dataItem["单位毛重"],
          dataItem["小计毛重"]
        );
      }
      if (this.isMsgError) {
        this.setMsg(`导入数据有错误,请处理好重新导入`, "red");
        this.setMsg("-1");
        return;
      }
 
      const connection: any = await this.dbWrite.connection;
      let request = new mssql.Request(connection.driver.master);
      request.input("user_Id", userInfo.user_Id);
      request.input("excelDT", excelDT);
      request.output("outMsg", mssql.NVarChar(2000));
      let result = await request.execute("sp_Storage_Outer_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
 
  //#region 记录打印次数
  @Post()
  public async print() {
    let { ctx } = this;
    let body = ctx.request.body;
    if (!Array.isArray(body.ids)) {
      this.info.result = false;
      this.info.msg = "数据ID不正确";
      this.ctx.body = this.info;
      return;
    }
 
    try {
      // 对字段printCount进行加1
      for (let id of body.ids) {
        let outerInfo = await this.dbRead.findOne(StorageOuter, {
          outer_Id: id
        });
        if (outerInfo) {
          await this.dbWrite.update(
            StorageOuter,
            {
              outer_Id: In(body.ids)
            },
            { printCount: (outerInfo.printCount || 0) + 1 }
          );
        }
      }
      this.info.result = true;
      this.ctx.body = this.info;
    } catch (ex) {
      this.info.msg = ex.message + "数据分拣失败请重试!";
      this.info.result = false;
      this.ctx.body = this.info;
    }
  }
  //#endregion
}