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
import BaseService from "../baseService";
import * as sql from "mssql";
import { PurchaseEnter } from "../../entity/inbound/purchase/purchaseEnter";
import { BaseStorage } from "../../entity/basicInfo/base/baseStorage";
import { BaseShelveRegular } from "../../entity/basicInfo/rule/baseShelveRegular";
import { PurchaseShelve } from "../../entity/inbound/purchase/purchaseShelve";
/**
 * LPN - 入库扫描Service
 */
export default class EnterlpnScanService extends BaseService {
  //#region  Lpn入库扫描 - 保存数据
  public async purchaseEnterSaveLpn(lpnCode: string, orderCode: string, orderDataList: Array<any>, user_Id: number) {
    let { ctx } = this;
    let menu_Id = 1724;
    if (orderDataList.length == 0) {
      this.info.result = false;
      this.info.msg = "已扫描数量不能全部为零";
      return this.info;
    }
    try {
      let excelDT = new sql.Table();
      excelDT.columns.add("orderList_Id", sql.Int);
      excelDT.columns.add("finishedQuantity", sql.Int);
      excelDT.columns.add("plateCode", sql.NVarChar(50));
      excelDT.columns.add("lpnCode", sql.NVarChar(50));
      excelDT.columns.add("produceDate", sql.NVarChar(50));
      excelDT.columns.add("limitDate", sql.NVarChar(50));
      for (var dataItem of orderDataList) {
        excelDT.rows.add(
          dataItem.orderList_Id,
          dataItem.finishedQuantity,
          dataItem.plateCode,
          dataItem.lpnCode,
          dataItem.produceDate,
          dataItem.limitDate
        );
      }
      let enterCode = await ctx.service.common.getCodeRegular(103);
      const connection: any = await this.dbWrite.connection;
      let request = new sql.Request(connection.driver.master);
      request.input("enterCode", enterCode);
      request.input("orderCode", orderCode);
      request.input("lpnCode", lpnCode);
      request.input("user_Id", 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_Enter_ConfirmBarcode_Lpn");
      let outMsg = result.output.outMsg;
      if (outMsg != null && outMsg) {
        this.info.msg = outMsg;
        this.info.result = false;
        return this.info;
      } else {
        this.info.msg = "确认入库成功!";
        this.info.result = true;
        return this.info;
      }
    } catch (e) {
      this.info.result = false;
      this.info.msg = e.message;
      return this.info;
    }
  }
  //#endregion
 
  //#region  Lpn获取采购入库单数据
  public async getPurchaseEnterLpnData(lpnCode: string) {
    var where = "lpnCode COLLATE Chinese_PRC_CS_AS=:lpnCode AND statusText in ('确认入库', '部分上架')";
    let orderInfo = await this.dbRead
      .createQueryBuilder(PurchaseEnter, "t")
      .where(where, {
        lpnCode: lpnCode
      })
      .getOne();
    if (orderInfo == null) {
      this.info.result = false;
      this.info.msg = "采购入库单数据不存在!";
      return this.info;
    }
    if (orderInfo.statusText == "完全上架") {
      this.info.result = false;
      this.info.msg = "该订单已经完成上架!";
      return this.info;
    }
    if (orderInfo.statusText != "确认入库" && orderInfo.statusText != "部分上架") {
      this.info.result = false;
      this.info.msg = "采购入库单的订单状态不是确认入库或者部分上架,不能进行上架操作!";
      return this.info;
    }
    let sql = `SELECT enterList_Id,L.productCode,L.productModel,L.productName,L.productSpec,L.purchasePrice,
                  ISNULL(L.quantity,0) AS quantity,ISNULL(L.shelveQuantity,0) AS shelveQuantity,L.positionName,plateCode,
                   (SELECT enterCode FROM Purchase_Enter WHERE enter_Id=@0) AS enterCode,
                   (SELECT lpnCode FROM Purchase_Enter WHERE enter_Id=@0) AS lpnCode,
                   produceDate,limitDate,
         I.relationCode, I.relationCode2, I.relationCode3, I.relationCode4, I.relationCode5,
         middleBarcode, middleUnitConvert, bigBarcode, I.unitConvert
                FROM dbo.Purchase_EnterList L INNER JOIN dbo.vBase_ProductInfo_SKU I ON L.product_Id=I.product_Id
                WHERE enter_Id=@0 and  (quantity-ISNULL(shelveQuantity,0))>0 And ISNULL(statusText, '')!='完全上架'`;
    let orderList = await this.dbRead.query(sql, [orderInfo.enter_Id]);
    if (orderList.length == 0) {
      this.info.result = false;
      this.info.msg = "未找到对应的入库单明细数据!";
      return this.info;
    } else {
      let storageInfo = await this.dbRead.findOne(BaseStorage, {
        storage_Id: orderInfo.storage_Id
      });
      let info = {
        orderList: orderList
      };
      this.info.dynamic = storageInfo;
      this.info.data = info;
      this.info.result = true;
      return this.info;
    }
  }
  //#endregion
 
  //#region  Lpn上架扫描 - 根据LpnCode获取最近上架货位
  public async comboboxLpn(lpnCode: string, enterList_Id: number) {
    var where = "lpnCode =:lpnCode";
    let orderInfo = await this.dbRead
      .createQueryBuilder(PurchaseEnter, "t")
      .where(where, {
        lpnCode: lpnCode
      })
      .getOne();
    if (orderInfo == null) {
      this.info.result = false;
      this.info.msg = "没有查询采购入库单号!";
      return this.info;
    }
    // 查询上架策略数据
    let reg = await this.dbRead.find(BaseShelveRegular, {
      where: { enable: 1 },
      order: { orderNo: "DESC" }
    });
    if (reg.length == 0) {
      this.info.result = false;
      this.info.msg = "请创建上架数据!";
      return this.info;
    }
    let n = 0;
    let sql = "";
    for (var list of reg) {
      if (n == 1) {
        break;
      }
      if (list.shelveRegularName == "已有库存货位") {
        sql = `SELECT TOP 5 * FROM(
         SELECT 
          p.positionName AS name ,
          L.enterList_Id AS ID,
          MAX(productPosition_Id) AS productPosition_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)
        GROUP BY p.positionName, L.enterList_Id
       ) t
       ORDER BY t.productPosition_Id DESC`;
        if (orderInfo.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)`;
        if (orderInfo.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=@1)
                  AND NOT EXISTS(SELECT position_Id FROM Base_Position t WHERE t.parentId=p.position_Id and  storage_Id=@1) 
                  AND  p.storage_Id=@1 AND p.enable=1`;
        if (orderInfo.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
                  GROUP BY positionName`;
      } 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)`;
        if (orderInfo.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)`;
        if (orderInfo.orderType == "残品预到货") {
          sql = sql.replace("in(1,12,13)", "= 2");
        }
      }
      let orderList = await this.dbRead.query(sql, [enterList_Id, orderInfo.storage_Id]);
      if (orderList.length > 0) {
        n = 1;
        this.info.result = true;
        this.info.data = orderList;
        return this.info;
      }
    }
    return this.info;
  }
  //#endregion
 
  //#region  Lpn上架扫描 - 保存数据
  public async purchaseEnterLpnSave(lpnCode: string, enterDataList: Array<any>, userInfo) {
    let { ctx } = this;
    let menu_Id = 1724;
    try {
      let excelDT = new sql.Table();
      excelDT.columns.add("enterList_Id", sql.Int);
      excelDT.columns.add("finishedQuantity", sql.Int);
      excelDT.columns.add("shelvePositionName", sql.NVarChar(50));
      for (var dataItem of enterDataList) {
        excelDT.rows.add(dataItem.enterList_Id, dataItem.finishedQuantity, dataItem.shelvePositionName);
      }
      let shelveCode = await 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("lpnCode", lpnCode);
      request.input("user_Id", userInfo.user_Id);
      request.input("menu_Id", menu_Id);
      request.input("execelDT", excelDT);
      request.output("outMsg", sql.NVarChar(2000));
      let result = await request.execute("sp_Purchase_Shelve_ConfirmBarcode_Lpn");
      let outMsg = result.output.outMsg;
      if (outMsg != null && outMsg) {
        this.info.msg = outMsg;
        this.info.result = false;
        return this.info;
      } else {
        var shelve = await this.dbRead.findOne(PurchaseShelve, {
          shelveCode: shelveCode
        });
        if (shelve != null) {
          const connection: any = await this.dbWrite.connection;
          let requestOne = new sql.Request(connection.driver.master);
          requestOne.input("shelve_Id", shelve.shelve_Id);
          requestOne.input("user_Id", userInfo.user_Id);
          requestOne.input("menu_Id", menu_Id);
          requestOne.output("outMsg", sql.NVarChar(2000));
          let result = await requestOne.execute("sp_Purchase_Shelve_Check_Lpn");
          let outMsgOne = result.output.outMsg;
          if (outMsgOne != null && outMsgOne) {
            let sql = `DELETE dbo.Purchase_Shelve WHERE Shelve_Id=@0`;
            await this.dbRead.query(sql, [shelve.shelve_Id]);
            let sqlOne = `DELETE dbo.Purchase_ShelveList WHERE Shelve_Id=@0`;
            await this.dbRead.query(sqlOne, [shelve.shelve_Id]);
            this.info.msg = outMsgOne;
            this.info.result = false;
            return this.info;
          } else {
            // 获得上架单状态
            var shelve = await this.dbRead.findOne(PurchaseShelve, {
              shelve_Id: shelve.shelve_Id
            });
            this.info.dynamic = shelve.onShelveStatus;
            this.info.result = true;
            this.info.msg = "确认上架成功!";
            return this.info;
          }
        } else {
          this.info.result = false;
          this.info.msg = "上架单创建成功,分拣失败!";
          return this.info;
        }
      }
    } catch (e) {
      this.info.result = false;
      this.info.msg = e.message;
      return this.info;
    }
  }
  //#endregion
}