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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import * as sql from "mssql";
import BaseService from "../baseService";
import { PurchaseShelve } from "../../entity/inbound/purchase/purchaseShelve";
import { BaseShelveRegular } from "../../entity/basicInfo/rule/baseShelveRegular";
import { ResultInfo } from "../../public/commonInterface";
 
/**
 * 上架扫描Service
 */
export default class ShelveScanService extends BaseService {
  //#region  待上架单扫描上架 - 获取数据
  /// <summary>
  /// 待上架单扫描上架 - 获取数据
  /// </summary>
  /// <returns></returns>
  public async getData() {
    let userInfo = await this.userInfo;
    if (!this.body.shelveCode) {
      this.info.result = false;
      this.info.msg = "待上架单号不能为空!";
      return this.info;
    }
 
    let shelveInfo = await this.dbRead.findOne(PurchaseShelve, {
      shelveCode: this.body.shelveCode,
      userProduct_Id: userInfo.userProduct_Id
    });
    if (!shelveInfo) {
      this.info.result = false;
      this.info.msg = this.body.shelveCode + "待上架单不存在!";
      return this.info;
    }
 
    if ("待上架,部分上架,上架中".indexOf(shelveInfo.onShelveStatus) < 0) {
      this.info.result = false;
      this.info.msg = "只有待上架、部分上架或上架中的才允许操作!";
      return this.info;
    }
 
    let sql = `
                SELECT shelveList_Id,L.productCode,L.productModel,L.productName,L.productSpec,
                        ISNULL(quantity,0) - ISNULL(L.shelvedQuantity,0) AS quantity, 
                    ISNULL(L.onShelveQuantity,0) AS onShelveQuantity,
                    ISNULL(L.shelvedQuantity,0) AS shelvedQuantity,
                    dbo.getRecommendPositionName(S.storage_Id, L.product_Id, L.productSpec, 0) AS positionName,
                    L.plateCode, L.produceDate,  I.purchasePrice,
                    (SELECT shelveCode FROM Purchase_Shelve WHERE shelve_Id=L.shelve_Id) AS shelveCode, 
                    I.relationCode, I.relationCode2, I.relationCode3, I.relationCode4, I.relationCode5,
                    middleBarcode, middleunitConvert, bigBarcode, I.unitConvert,L.weight,L.totalWeight, I.smallUnit
                FROM dbo.Purchase_ShelveList L INNER JOIN dbo.Base_ProductInfo I ON L.product_Id=I.product_Id
                    INNER JOIN dbo.Purchase_Shelve S ON S.shelve_Id=L.shelve_Id
                WHERE L.shelve_Id=@0 And quantity>isnull(L.shelvedQuantity, 0)
                Order By productModel;
                `;
 
    let dataList = await this.dbRead.query(sql, [shelveInfo.shelve_Id]);
    if (dataList.length == 0) {
      this.info.result = false;
      this.info.msg = "上架单号没有可上架的数量!";
      return this.info;
    } else {
      //更新这个上架单为当前用户所有
      await this.dbWrite.update(PurchaseShelve, shelveInfo.shelve_Id, {
        user_Id: userInfo.user_Id,
        userTrueName: userInfo.userTrueName,
        onShelveStatus: () => {
          return "(case when onShelveStatus='待上架' then '上架中' else onShelveStatus end)";
        }
      });
 
      this.info.result = true;
      this.info.msg = "";
      this.info.data = dataList;
      return this.info;
    }
  }
  //#endregion
 
  //#region 根据上架单获得获取最近上架货位
  public async getShelvePositionName(scanDataList: any, shelveCode: string) {
    let userInfo = await this.userInfo;
    let shelveInfo = await this.dbRead.findOne(PurchaseShelve, {
      shelveCode: shelveCode,
      userProduct_Id: userInfo.userProduct_Id
    });
    let info: ResultInfo = {
      result: false
    };
    if (shelveInfo == null) {
      info.result = false;
      info.msg = "上架单号不存在";
      return info;
    }
    let regularInfo = await this.dbRead.find(BaseShelveRegular, {
      where: {
        userProduct_Id: userInfo.userProduct_Id,
        enable: 1
      },
      order: {
        orderNo: "DESC"
      }
    });
    if (regularInfo.length == 0) {
      info.result = false;
      info.msg = "请创建上架策略数据";
      return info;
    }
    for (let shelveDetail of scanDataList) {
      for (let List of regularInfo) {
        let sql = "";
        let params = [shelveDetail.enterList_Id, shelveInfo.storage_Id];
        if (List.shelveRegularName == "已有库存货位") {
          sql = `SELECT  distinct TOP(5) p.positionName AS name,L.enterList_Id AS id
                  FROM dbo.Base_ProductPosition AS p INNER JOIN Purchase_EnterList AS L
                       ON  p.product_Id=L.product_Id
                  WHERE L.enterList_Id=@0 AND p.storage_Id=@1 
                  AND EXISTS(SELECT *  FROM dbo.Base_Position WHERE positionName=p.positionName AND positionType in(1,12,13) 
                  AND storage_Id=@1  AND Enable=1);`;
          params = [shelveDetail.enterList_Id, shelveInfo.storage_Id];
          if (shelveInfo.orderType == "残品预到货") {
            sql = sql.replace("in(1,12,13)", "= 2");
          }
        } else if (List.shelveRegularName == "默认货位") {
          sql = `SELECT distinct TOP(5) P.positionName AS name,L.enterList_Id AS id
                FROM dbo.vBase_ProductInfo_SKU AS P INNER JOIN  Purchase_EnterList AS L
                      ON  p.product_Id=L.product_Id
                WHERE  L.enterList_Id=@0  AND ISNULL(P.positionName,'')<>''
                AND EXISTS(SELECT * FROM dbo.Base_Position WHERE positionName=P.positionName AND positionType in(1,12,13) 
                                AND storage_Id=@1 AND Enable=1)`;
          params = [shelveDetail.enterList_Id, shelveInfo.storage_Id];
          if (shelveInfo.orderType == "残品预到货") {
            sql = sql.replace("in(1,12,13)", "= 2");
          }
        } else if (List.shelveRegularName == "未使用的货位") {
          sql = `SELECT  distinct TOP(30) positionName AS name,Position_Id AS id
                  FROM dbo.Base_Position p 
                  WHERE positionType in(1,12,13) AND
                  NOT EXISTS(SELECT positionName FROM dbo.Base_ProductPosition WHERE positionName=p.positionName and  storage_Id=@0)
                  AND NOT EXISTS(SELECT Position_Id FROM Base_Position t WHERE t.ParentId=p.Position_Id and  storage_Id=@0) 
                  AND  p.storage_Id=@0 AND p.Enable=1;`;
          params = [shelveInfo.storage_Id];
 
          if (shelveInfo.orderType == "残品预到货") {
            sql = sql.replace("in(1,12,13)", "= 2");
          }
        } else if (List.shelveRegularName == "同品牌货位") {
          sql = `SELECT  distinct TOP(30) positionName AS name ,ROW_NUMBER() OVER(ORDER BY positionName) AS id
          FROM dbo.Base_ProductPosition  
          WHERE product_Id IN(SELECT product_Id FROM dbo.Base_ProductInfo 
          WHERE Brand_Id=(SELECT Brand_Id FROM dbo.Purchase_EnterList WHERE enterList_Id=@0) 
          AND ISNULL(BrandName,'')<>'') AND storage_Id=@1
          AND EXISTS(SELECT * FROM dbo.Base_Position WHERE positionName=Base_ProductPosition.positionName AND positionType in(1,12,13) 
          AND storage_Id=@1  AND Enable=1)
          GROUP BY positionName;`;
          params = [shelveDetail.enterList_Id, shelveInfo.storage_Id];
        } else if (List.shelveRegularName == "同供应商货位") {
          sql = `SELECT  distinct TOP(30) p.positionName AS name,L.enterList_Id AS id
          FROM dbo.Base_ProductPosition AS p INNER JOIN dbo.vPurchase_EnterList AS L
              ON L.Provider_Id = p.Provider_Id
          WHERE L.enterList_Id=@0 AND p.storage_Id=@1 
          AND EXISTS(SELECT * FROM dbo.Base_Position WHERE positionName=p.positionName AND positionType in(1,12,13) 
          AND storage_Id=@1  AND Enable=1);`;
          params = [shelveDetail.enterList_Id, shelveInfo.storage_Id];
          if (shelveInfo.orderType == "残品预到货") {
            sql = sql.replace("in(1,12,13)", "= 2");
          }
        } else if (List.shelveRegularName == "同批次货位") {
          sql = `SELECT  distinct TOP(30)  p.positionName AS name,L.enterList_Id AS id
          FROM dbo.Base_ProductPosition AS p INNER JOIN Purchase_EnterList AS L
              ON  p.batchNumber=L.batchNumber
          WHERE L.enterList_Id=@0 AND p.storage_Id=@1 
          AND EXISTS(SELECT * FROM dbo.Base_Position WHERE positionName=p.positionName AND positionType in(1,12,13) AND storage_Id=@1  AND Enable=1);`;
          params = [shelveDetail.enterList_Id, shelveInfo.storage_Id];
          if (shelveInfo.orderType == "残品预到货") {
            sql = sql.replace("in(1,12,13)", "= 2");
          }
        }
        if (!sql) {
          continue;
        }
 
        let dataList = await this.dbRead.query(sql, params);
        if (dataList.length) shelveDetail.shelvePositionNameList = dataList;
      }
      if (!shelveDetail.shelvePositionNameList) {
        shelveDetail.shelvePositionNameList = [];
      }
    }
  }
  //#endregion
 
  //#region 常规扫描上架 - shelveSave
  /// <summary>
  /// 常规扫描上架保存
  /// </summary>
  /// <returns></returns>
  public async shelveSave(shelveCode, shelveDataList: Array<any>) {
    let userInfo = await this.userInfo;
    let menu_Id = 223;
 
    if (shelveDataList.length == 0) {
      this.info.result = false;
      this.info.msg = "已扫描数量不能全部为零";
      return this.info;
    }
    if (!shelveCode) {
      this.info.result = false;
      this.info.msg = "采购入库单号【" + shelveCode + "】不能为空";
      return this.info;
    }
    let excelDT = new sql.Table();
    excelDT.columns.add("shelveList_Id", sql.Int);
    excelDT.columns.add("finishedQuantity", sql.Int);
    excelDT.columns.add("shelvePositionName", sql.NVarChar(50));
    excelDT.columns.add("weight", sql.Decimal(14, 4));
    excelDT.columns.add("totalWeight", sql.Decimal(14, 4));
    for (let item of shelveDataList) {
      excelDT.rows.add(item.shelveList_Id, item.finishedQuantity, item.shelvePositionName, item.weight, item.totalWeight);
    }
    const connection: any = await this.dbWrite.connection;
    let request = new sql.Request(connection.driver.master);
    request.input("shelveCode", shelveCode);
    request.input("user_Id", userInfo.user_Id);
    request.input("menu_Id", menu_Id);
    request.input("excelDT", excelDT);
    request.output("outMsg", sql.NVarChar(2000));
    let result = await request.execute("sp_Purchase_ShelveWait_Check");
    let outMsg = result.output.outMsg;
 
    if (outMsg && "部分上架,完全上架".indexOf(outMsg) < 0) {
      this.info.msg = outMsg;
      this.info.result = false;
      return this.info;
    } else {
      //获得上架单状态
      let oStatusText = await this.dbRead.findOne(PurchaseShelve, {
        select: ["onShelveStatus"],
        where: {
          shelveCode: shelveCode
        }
      });
      let statusText = oStatusText != null ? oStatusText : null;
      // 是否实时将库存推送到ERP
      let in_sendInventoryToErp = this.ctx.service.common.getConfigBool("in_sendInventoryToErp");
      if (in_sendInventoryToErp) {
        await this.ctx.curl("http://127.0.0.1:7001/api/task/inventoryPush", {
          // 必须指定 method
          method: "GET",
          // 明确告诉 HttpClient 以 JSON 格式处理返回的响应 body
          dataType: "json"
        });
      }
 
      this.info.msg = "确认上架成功";
      this.info.result = true;
      this.info.dynamic = statusText;
      return this.info;
    }
  }
  //#endregion
 
  /* *****************************************************
   * 无单扫描上架
   * *****************************************************/
  //#region 无单扫描上架 - 获取无单数据
  /**
   * 获取待上架单数据
   */
  public async getShelveNoBillData(storage_Id: number, positionName: string, productModel: string) {
    try {
      let sKU_ProductToMultiBarcode = await this.ctx.service.common.getConfigBool("sKU_ProductToMultiBarcode"); //支持一品多码
      var where = "";
      if (sKU_ProductToMultiBarcode) {
        where = `(I.productModel COLLATE Chinese_PRC_CS_AS=@2 
                    OR I.relationCode COLLATE Chinese_PRC_CS_AS=@2 
                    OR I.relationCode2 COLLATE Chinese_PRC_CS_AS=@2 
                    OR I.relationCode3 COLLATE Chinese_PRC_CS_AS=@2
                    OR I.relationCode4 COLLATE Chinese_PRC_CS_AS=@2
                    OR I.relationCode5 COLLATE Chinese_PRC_CS_AS=@2
                    OR I.middleBarcode COLLATE Chinese_PRC_CS_AS=@2
                    OR I.bigBarcode COLLATE Chinese_PRC_CS_AS=@2)
                `;
      } else {
        where = "I.productModel COLLATE Chinese_PRC_CS_AS=@2 ";
      }
 
      let sql = "";
      if (!positionName) {
        this.info.result = false;
        this.info.msg = "收货位不能为空!";
        return this.info;
      }
      if (!productModel) {
        this.info.result = false;
        this.info.msg = "条码不能为空!";
        return this.info;
      }
 
      sql = `
                SELECT top 1 L.product_Id,L.productCode,L.productModel,0 AS finishedQuantity,L.productSpec, L.productName,
                    L.positionName, NULL AS shelvePosition, storage_Id, storageName, L.batchNumber,L.purchasePrice,
                    L.plateCode, L.produceDate, I.relationCode, I.relationCode2, I.relationCode3, I.relationCode4, I.relationCode5,
                    I.middleBarcode, middleUnitConvert, I.bigBarcode, I.unitConvert,L.consignor_Id, L.consignorCode, L.consignorName,L.weight
                FROM dbo.vBase_ProductPosition L INNER JOIN dbo.vBase_ProductInfo_SKU I ON L.product_Id=I.product_Id
                WHERE L.positionName=@1 
                    AND L.storage_Id=@0 
                    AND (${where})
                    AND L.positionType=4
                    AND L.validQty>0
                ;`;
 
      let orderList = await this.dbRead.query(sql, [storage_Id, positionName, productModel]);
      if (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;
      }
    } catch (e) {
      this.info.result = false;
      this.info.msg = e.message;
      return this.info;
    }
  }
  //#endregion
 
  //#region 无单扫描上架 - 无单扫描保存
  /// <summary>
  /// 确认入库并生成上架单
  /// </summary>
  /// <returns></returns>
  public async shelveNoBillSave(orderDataList: Array<any>) {
    let userInfo = await this.userInfo;
    let menu_Id = 103;
    if (orderDataList.length == 0) {
      this.info.result = false;
      this.info.msg = "已扫描数量不能全部为零";
      this.ctx.body = this.info;
      return;
    }
    try {
      let excelDT = new sql.Table();
      excelDT.columns.add("storage_Id", sql.Int);
      excelDT.columns.add("storageName", sql.NVarChar(50));
      excelDT.columns.add("product_Id", sql.Int);
      excelDT.columns.add("finishedQuantity", sql.Int);
      excelDT.columns.add("productCode", sql.NVarChar(50));
      excelDT.columns.add("productModel", sql.NVarChar(50));
      excelDT.columns.add("positionName", sql.NVarChar(50));
      excelDT.columns.add("shelvePosition", sql.NVarChar(50));
      excelDT.columns.add("produceDate", sql.NVarChar(50));
 
      excelDT.columns.add("purchasePrice", sql.Decimal(14, 4));
      excelDT.columns.add("batchNumber", 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("consignorCode", sql.NVarChar(50));
      excelDT.columns.add("consignorName", sql.NVarChar(50));
 
      excelDT.columns.add("weight", sql.Decimal(14, 4));
      excelDT.columns.add("totalWeight", sql.Decimal(14, 4));
      for (let dataItem of orderDataList) {
        excelDT.rows.add(
          dataItem.storage_Id,
          dataItem.storageName,
          dataItem.product_Id,
          dataItem.finishedQuantity,
 
          dataItem.productCode,
          dataItem.productModel,
          dataItem.positionName,
          dataItem.shelvePosition,
          dataItem.produceDate,
 
          dataItem.purchasePrice,
          dataItem.batchNumber,
          dataItem.productSpec,
          dataItem.plateCode,
          dataItem.consignor_Id,
          dataItem.consignorCode,
          dataItem.consignorName,
 
          dataItem.weight,
          dataItem.totalWeight
        );
      }
      let shelveCode = await this.ctx.service.common.getCodeRegular(269);
 
      const connection: any = await this.dbWrite.connection;
      let request = new sql.Request(connection.driver.master);
      request.input("shelveCode", shelveCode);
      request.input("user_Id", userInfo.user_Id);
      request.input("menu_Id", menu_Id);
      request.input("excelDT", excelDT);
      request.output("outMsg", sql.NVarChar(2000));
      let result = await request.execute("sp_Purchase_Shelve_NoBill");
      let outMsg = result.output.outMsg;
 
      if (outMsg) {
        this.info.msg = outMsg;
        this.info.result = false;
        return this.info;
      } else {
        this.info.msg = "无单扫描上架成功";
        this.info.result = true;
        return this.info;
      }
    } catch (error) {
      this.info.msg = error.message;
      this.info.result = false;
      return this.info;
    }
  }
  //#endregion
}