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
import BaseService from "../baseService";
import * as sql from "mssql";
import { ResultInfo } from "../../public/commonInterface";
import { StoragePositionTransfer } from "../../entity/storage/check/storagePositionTransfer";
import { In } from "typeorm";
export default class PositionTransferService extends BaseService {
  //#region  获取货位数据
 
  /// <summary>
  /// 获取货位数据
  /// </summary>
  /// <returns></returns>
  public async getProductPositionGroupData(originalPositionName: string, targetPositionName: string, storage_Id) {
    let { ctx } = this;
    let body = ctx.request.body;
    let sql = "";
    if (!originalPositionName) {
      this.info.result = false;
      this.info.msg = "原货位不能为空!";
      return this.info;
    }
    if (storage_Id <= 0) {
      this.info.result = false;
      this.info.msg = "仓库不能为空!";
      return this.info;
    }
    if (originalPositionName == targetPositionName) {
      this.info.result = false;
      this.info.msg = "原货位和目标货位不能相同!";
      return this.info;
    }
    sql = `
        SELECT   PP.product_Id,PP.productCode,PP.productModel,PP.productName,ISNULL(PP.ProductSpec,'') AS productSpec, 
          PP.ValidQuantity as productStorage, PP.positionName,PP.storageName,ISNULL(PP.batchNumber,'') AS batchNumber,
          PP.produceDate, PP.productAttribute, PP.storageStatus,
          ISNULL(PP.PlateCode,'') AS plateCode, PP.consignor_Id, PP.consignorCode, PP.consignorName,
          (SELECT  TOP  1 ServerPath FROM dbo.Base_ProductInfo_Image WHERE product_Id=PP.product_Id) AS serverPath,
                    PI.relationCode, PI.relationCode2, PI.relationCode3, PI.relationCode4, PI.relationCode5,
                    PI.middleBarcode, PI.middleUnitConvert, PI.bigBarcode, PI.unitConvert,ISNULL(PP.Weight,0) as weight,PP.ValidQuantity*ISNULL(PP.Weight,0) as  sumTotalWeight
                FROM      dbo.vBase_ProductInfo_SKU AS PI INNER JOIN
                        dbo.vBase_ProductPositionGroup AS PP ON PI.product_Id = PP.product_Id
        WHERE PP.PositionName COLLATE Chinese_PRC_CS_AS=@0 AND storage_Id=@1  
          AND ValidQuantity>0 /*AND PositionType IN(1,2,4,5,6,8,12,13)*/`;
 
    let orderList = await this.dbRead.query(sql, [body.originalPositionName, body.storage_Id]);
    if (!orderList) {
      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 storagePositionTransferSave(storage_Id, productModelList, user_Id) {
    let { ctx } = this;
 
    try {
      // #region  验证用户信息
      if (!productModelList.length) {
        this.info.msg = "转移数据错误";
        this.info.result = false;
        return this.info;
      }
      // #endregion
 
      // #region  创建一个DataTable
      let 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.NVarChar(50));
      excelDT.columns.add("productSpec", sql.NVarChar(50));
      excelDT.columns.add("plateCode", sql.NVarChar(50));
      excelDT.columns.add("consignor_Id", sql.Int);
      excelDT.columns.add("ScanWeight", sql.Decimal(12, 4));
      excelDT.columns.add("Weight", sql.Decimal(12, 4));
      excelDT.columns.add("storageStatus", sql.NVarChar(50));
      excelDT.columns.add("productAttribute", sql.NVarChar(50));
 
      for (var dataItem of productModelList) {
        if (!dataItem.targetPositionName) {
          this.info.msg = "目标货位不能为空";
          this.info.result = false;
          return this.info;
        }
        excelDT.rows.add(
          dataItem.product_Id,
          dataItem.finishedQuantity,
          dataItem.originalPositionName,
          dataItem.targetPositionName,
          dataItem.batchNumber,
          dataItem.produceDate,
          dataItem.productSpec,
          dataItem.plateCode,
          dataItem.consignor_Id,
          dataItem.scanWeight,
          dataItem.weight,
          dataItem.storageStatus,
          dataItem.productAttribute
        );
      }
      // #endregion
      let transferCode = await ctx.service.common.getCodeRegular(450);
      const connection: any = await this.dbWrite.connection;
      let request = new sql.Request(connection.driver.master);
      request.input("transferCode", transferCode);
      request.input("storage_Id", storage_Id);
      request.input("user_Id", user_Id);
      request.input("excelDT", excelDT);
      request.output("outMsg", sql.NVarChar(2000));
      let result = await request.execute("sp_Storage_PositionTransfer_Create");
      let outMsg = result.output.outMsg;
      if (outMsg) {
        this.info.msg = outMsg;
        this.info.result = false;
        return this.info;
      } else {
        let where = { transferCode: transferCode };
        let transfer = await this.dbRead.findOne(StoragePositionTransfer, where);
        if (transfer != null) {
          const connection: any = await this.dbWrite.connection;
          let request = new sql.Request(connection.driver.master);
          request.input("transfer_Id", transfer.transfer_Id);
          request.input("user_Id", user_Id);
          request.input("Menu_Id", 450);
          request.output("outMsg", sql.NVarChar(2000));
          let result = await request.execute("sp_Storage_PositionTransfer_Check");
          let outMsgs = result.output.outMsg;
          if (outMsgs) {
            this.info.msg = outMsgs;
            this.info.result = false;
            return this.info;
          } else {
            this.info.msg = "确认转移成功";
            this.info.result = true;
            return this.info;
          }
        } else {
          this.info.msg = "转移失败,单据创建成功";
          this.info.result = false;
          return this.info;
        }
      }
    } catch (error) {
      this.info.msg = "转移错误:" + error.message;
      this.info.result = false;
      return this.info;
    }
  }
  //#endregion
 
  // #region Stop
  /// <summary>
  /// 货位转移终止
  /// </summary>
  /// <param name="loadInfo"></param>
  /// <returns></returns>
  public async stop(): Promise<ResultInfo> {
    let { ctx } = this;
    let body = ctx.request.body;
 
    try {
      let transferInfo = await this.dbRead.find(StoragePositionTransfer, {
        transfer_Id: In(this.body.idValues)
      });
      for (let item of transferInfo) {
        await this.dbWrite.update(
          body.tableName,
          {
            [body.idField]: item.transfer_Id
          },
          {
            statusID: 3,
            statusText: "终止",
            sortingStatus: 1,
            sortingDate: null
          }
        );
      }
      this.info.result = true;
      this.info.msg = "终止成功!";
    } catch (error) {
      this.info.result = true;
      this.info.msg = "终止失败!" + error.message;
    }
    return this.info;
  }
  //#endregion
 
  //  #region Open
  /// <summary>
  /// 货位转移开启
  /// </summary>
  /// <returns></returns>
  public async open(): Promise<ResultInfo> {
    let { ctx } = this;
    let body = ctx.request.body;
    try {
      let transferInfo = await this.dbRead.find(StoragePositionTransfer, {
        transfer_Id: In(this.body.idValues)
      });
      for (let item of transferInfo) {
        if (item.statusText != "终止") {
          this.info.result = false;
          this.info.msg = "只有状态为终止, 才能开启!";
          this.ctx.body = this.info;
          return this.info;
        }
        await this.dbWrite.update(
          body.tableName,
          {
            [body.idField]: item.transfer_Id
          },
          {
            statusID: 1,
            statusText: "新建",
            sortingStatus: 1,
            auditing: null,
            sortingDate: null
          }
        );
      }
      this.info.result = true;
      this.info.msg = "开启成功!";
    } catch (error) {
      this.info.result = false;
      this.info.msg = "开启失败!" + error.message;
    }
    return this.info;
  }
  //#endregion
}