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
import BaseService from "../baseService";
import { SysUser } from "../../entity/sys/core/sysUser";
import { LoginInfo } from "../../entity/sys/loginInfo";
import * as sql from "mssql";
import moment = require("moment");
import { StorageReplenishment } from "../../entity/storage/replenishment/storageReplenishment";
import { In } from "typeorm";
import { BaseProductPlaceHolder } from "../../entity/storage/storage/baseProductPlaceHolder";
/**
 * 产品入库单Service
 */
export default class StorageReplenishmentService extends BaseService {
  //  #region  获取补货数据
  /// <summary>
  /// 获取补货数据
  /// </summary>
  /// <returns></returns>
  public async getReplenishmentGroupData(replenishmentCode) {
    let { ctx } = this;
    let userInfo = await ctx.helper.userInfo();
    let sql = "";
    if (!replenishmentCode) {
      this.info.result = false;
      this.info.msg = "请扫描补货单号!";
      return this.info;
    }
    sql = `SELECT  S.product_Id, S.productCode, S.productModel, S.productName,ISNULL(S.ProductSpec,'') AS productSpec,
          SUM(S.PlaceholderStorage) AS placeholderStorage,S. positionName, S.storageName,
                   ISNULL(S.BatchNumber,'') AS batchNumber,ISNULL( S.ProduceDate,'') AS produceDate,
                   ISNULL(S.PlateCode,'') AS plateCode, L.positionName AS targetPositionName,S.consignor_Id,
                  (SELECT TOP 1 manufacturer FROM dbo.vBase_ProductInfo_SKU nolock WHERE Product_Id=S.Product_Id) AS  manufacturer
          FROM Base_ProductPlaceHolder AS S INNER JOIN dbo.Storage_ReplenishmentList AS L
                  ON S.DetailID=L.ReplenishmentList_Id AND S.MainID=L.Replenishment_Id
          WHERE  ClassName='补货出库' AND S.PlaceholderStorage>0 AND
          Replenishment_Id IN(SELECT TOP 1 Replenishment_Id  FROM dbo.Storage_Replenishment WHERE ReplenishmentCode=@0 AND UserProduct_Id=@1)
          AND S.UserProduct_Id=@1
          GROUP BY S.Product_Id,S.ProductCode, S.ProductModel, S.ProductName,S.ProductSpec,S. PositionName,S.StorageName,
          S.BatchNumber,S.ProduceDate,L.PositionName,S.PlateCode,S.Consignor_Id;`;
    let orderList = await this.dbWrite.query(sql, [replenishmentCode, userInfo.userProduct_Id]);
    if (!orderList || orderList.length <= 0) {
      this.info.result = false;
      this.info.msg = "未找到需要补货出库的数据";
      return this.info;
    } else {
      this.info.result = true;
      this.info.msg = "获取补货数据成功";
      this.info.data = orderList;
      return this.info;
    }
  }
  // #endregion
 
  //#region ScanSvae  确认补货出库
  /// <summary>
  /// 确认补货出库
  /// </summary>
  /// <returns></returns>
  public async storageReplenishmentSave(replenishmentCode, productModelList: Array<any>, user_Id) {
    let userInfo = new LoginInfo();
    //#region  验证用户信息
    let table = await this.dbRead.findOne(SysUser, {
      user_Id: user_Id
    });
    if (table) {
      userInfo.user_Id = table.user_Id;
      userInfo.userName = table.userName;
      userInfo.userTrueName = table.userTrueName;
    }
 
    if (!productModelList) {
      this.info.msg = "没有确认补货出库数据";
      this.info.result = false;
      return this.info;
    }
    // #endregion
    try {
      var excelDT = new sql.Table();
      excelDT.columns.add("product_Id", sql.Int);
      excelDT.columns.add("finishedQuantity", sql.Int);
      excelDT.columns.add("originalPositionName", sql.NVarChar(50));
      excelDT.columns.add("targetPositionName", sql.NVarChar(50));
      excelDT.columns.add("batchNumber", sql.NVarChar(50));
      excelDT.columns.add("produceDate", sql.DateTime);
      excelDT.columns.add("plateCode", sql.NVarChar(50));
      excelDT.columns.add("consignor_Id", sql.Int);
      for (var dataItem of productModelList) {
        excelDT.rows.add(
          dataItem.product_Id,
          dataItem.finishedQuantity,
          dataItem.originalPositionName,
          dataItem.targetPositionName,
          dataItem.batchNumber,
          moment(dataItem.produceDate).toDate(),
          dataItem.plateCode,
          dataItem.consignor_Id
        );
      }
      const connection: any = await this.dbWrite.connection;
      let request = new sql.Request(connection.driver.master);
      request.input("replenishmentCode", replenishmentCode);
      request.input("user_Id", user_Id);
      request.input("execelDT", excelDT);
      request.output("outMsg", sql.NVarChar(2000));
      let result = await request.execute("sp_Storage_Replenishment_ConfirmOut");
      let outMsg = result.output.outMsg;
      if (outMsg) {
        this.info.msg = outMsg;
        this.info.result = false;
      } else {
        this.info.msg = "补货出库成功";
        this.info.result = true;
      }
      return this.info;
    } catch (ex) {
      this.info.msg = "出库失败," + ex.message;
      this.info.result = false;
      return this.info;
    }
  }
  //#endregion
 
  //#region Stop
  public async stop() {
    // 删除占位
    await this.dbRead.delete(BaseProductPlaceHolder, {
      className: "补货出库",
      mainID: In(this.body.idValues)
    });
    let stList = await this.dbRead.find(StorageReplenishment, {
      replenishment_Id: In(this.body.idValues)
    });
    for (let sInfo of stList) {
      await this.dbRead.update(StorageReplenishment, sInfo.replenishment_Id, {
        billStatus: "终止"
      });
    }
    this.info.result = true;
    this.info.msg = "终止成功!";
 
    return this.info;
  }
  //#endregion
 
  //#region Open
  public async open() {
    let stList = await this.dbRead.find(StorageReplenishment, {
      replenishment_Id: In(this.body.idValues)
    });
    for (let sInfo of stList) {
      await this.dbRead.update(StorageReplenishment, sInfo.replenishment_Id, {
        billStatus: "新建",
        auditing: 0,
        auditor: null,
        auditDate: null,
        sortingStatus: 1,
        sortingDate: null
      });
    }
    this.info.result = true;
    this.info.msg = "开启成功!";
 
    return this.info;
  }
  //#endregion
}