222
schangxiang@126.com
2025-06-13 6a8393408d8cefcea02b7a598967de8dc1e565c2
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
// import * as sql from "mssql";
import { Post } from "egg-shell-decorators";
import BaseController from "../../baseController";
import { StsNodeWarn } from "../../../entity/stat/warn/stsNodeWarn";
// import { Like, Between } from "typeorm";
import { Between } from "typeorm";
import { vShelfLifeEarlyWarning } from "../../../entity/storage/stat/vShelfLifeEarlyWarning";
import { vOldLibraryEarlyWarning } from "../../../entity/storage/stat/vOldLibraryEarlyWarning";
import { vBaseProductPositionLower } from "../../../entity/storage/stat/vBaseProductPositionLower";
import { BaseProductPositionHistory } from "../../../entity/storage/log/baseProductPositionHistory";
 
/**
 * 库存统计
 */
export default class VpurchaseOrderController extends BaseController {
  // #region 节点延误报警
  @Post()
  public async getStsNodeWarn() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userProduct_Id = body.userProduct_Id;
 
    let where: any = {};
    where.userProduct_Id = userProduct_Id;
    try {
      let warnList = await this.dbRead.find(StsNodeWarn, where);
      this.info.result = true;
      this.info.msg = "获取节点延误报警成功";
      this.info.data = warnList;
      ctx.body = this.info;
      return;
    } catch (error) {
      this.info.result = false;
      this.info.msg = error.message;
      ctx.body = this.info;
      return;
    }
  }
 
  // #endregion
 
  // #region 保质期报警
  /**
        / 保质期报警
        */
  @Post()
  public async getvShelfLifeEarly() {
    let { ctx } = this;
    let body = ctx.request.body;
    // let userInfo = await ctx.helper.userInfo();
    let storage_Id = body.storage_Id;
    let consignor_Id = body.consignor_Id;
    let beginDate = body.beginDate;
    let endDate = body.endDate;
    let userProduct_Id = body.userProduct_Id;
    let provider_Id = body.provider_Id;
    // let likeCode = body.likeCode;
    let where: any = {};
    where.userProduct_Id = userProduct_Id;
    if (storage_Id) {
      where.storage_Id = storage_Id;
    }
    if (provider_Id) {
      where.provider_Id = provider_Id;
    }
    if (consignor_Id) {
      where.consignor_Id = consignor_Id;
    }
    // if (likeCode) {
    //   [
    //     (where.productCode = Like("%" + likeCode + "%")),
    //     (where.productName = Like("%" + likeCode + "%")),
    //     (where.areaCode = Like("%" + likeCode + "%")),
    //     (where.positionName = Like("%" + likeCode + "%")),
    //     (where.productModel = Like("%" + likeCode + "%"))
    //   ];
    // }
    if (beginDate && endDate) {
      endDate = endDate + " 23:59:59";
      where.inStorageDate = Between(beginDate, endDate);
    }
    try {
      let qantity;
      let enterQuantity;
      let inQuantity;
      let shelvedQuantity;
      //已超过库存有效期
      qantity = await this.dbRead
        .createQueryBuilder(vShelfLifeEarlyWarning, "t")
        .select("count(distinct product_Id)", "sum")
        .where(where)
        .andWhere("validityDay<0")
        .getRawOne();
 
      // 库存有效期剩余1/3
      enterQuantity = await this.dbRead
        .createQueryBuilder(vShelfLifeEarlyWarning, "t")
        .select("count(distinct product_Id)", "sum")
        .where(where)
        .andWhere("ValidityDay<ShelfLifeDay*0.3333333")
        .getRawOne();
 
      //库存有效期剩余2/3
      inQuantity = await this.dbRead
        .createQueryBuilder(vShelfLifeEarlyWarning, "t")
        .select("count(distinct product_Id)", "sum")
        .where(where)
        .andWhere("ValidityDay<ShelfLifeDay*0.6666666 AND ValidityDay>ShelfLifeDay*0.3333333")
        .getRawOne();
 
      // 库存有效期剩余2/3以上
      shelvedQuantity = await this.dbRead
        .createQueryBuilder(vShelfLifeEarlyWarning, "t")
        .select("count(distinct product_Id)", "sum")
        .where(where)
        .andWhere("(ValidityDay>ShelfLifeDay*0.6666666 OR ShelfLifeDay IS NULL)")
        .getRawOne();
 
      this.info.result = true;
      this.info.msg = "获取成功";
      qantity = qantity.sum; // 已超过库存有效期
      enterQuantity = enterQuantity.sum;
      inQuantity = inQuantity.sum;
      shelvedQuantity = shelvedQuantity.sum;
      this.info.data = {
        quantityCount: qantity,
        enterQuantityCount: enterQuantity,
        inQuantityCount: inQuantity,
        shelvedQuantityCount: shelvedQuantity
      };
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "获取数据时出错:" + ex.message;
    }
    ctx.body = this.info;
  }
  // #endregion
 
  // #region 库龄预警
  @Post()
  public async getvOldLibraryEarlyWarning() {
    let { ctx } = this;
    let body = ctx.request.body;
    let storage_Id = body.storage_Id;
    let consignor_Id = body.consignor_Id;
    let userProduct_Id = body.userProduct_Id;
    let where: any = {};
    where.userProduct_Id = userProduct_Id;
    if (storage_Id) {
      where.storage_Id = storage_Id;
    }
    if (consignor_Id) {
      where.consignor_Id = consignor_Id;
    }
    try {
      let productList;
 
      productList = await this.dbRead.createQueryBuilder(vOldLibraryEarlyWarning, "t").select(["productName", "inDay"]).where(where).getRawMany();
      let datalist = productList.slice(0, 10);
      let datainfo: any = {};
      let category = [];
      let seriesList: any = [];
      for (let item of datalist) {
        category.push(item.productName);
        seriesList.push(item.inDay);
      }
      let series = [
        {
          name: "入库量",
          type: "bar",
          data: seriesList
        }
      ];
      datainfo = {
        category: category,
        series: series
      };
 
      this.info.result = true;
      this.info.msg = "获取图标成功";
      this.info.data = datainfo;
      ctx.body = this.info;
      return;
    } catch (error) {
      this.info.result = false;
      this.info.msg = error.message;
      ctx.body = this.info;
      return;
    }
  }
 
  // #endregion
 
  // #region 库存预警
  /**
        / 库存预警
        */
  @Post()
  public async getProductPositionLower() {
    let { ctx } = this;
    let body = ctx.request.body;
    // let userInfo = await ctx.helper.userInfo();
    let storage_Id = body.storage_Id;
    let consignor_Id = body.consignor_Id;
    let beginDate = body.beginDate;
    let endDate = body.endDate;
    let userProduct_Id = body.userProduct_Id;
    let provider_Id = body.provider_Id;
    // let likeCode = body.likeCode;
    let where: any = {};
    where.userProduct_Id = userProduct_Id;
    if (storage_Id) {
      where.storage_Id = storage_Id;
    }
    if (provider_Id) {
      where.provider_Id = provider_Id;
    }
    if (consignor_Id) {
      where.consignor_Id = consignor_Id;
    }
    // if (likeCode) {
    //   [
    //     (where.productCode = Like("%" + likeCode + "%")),
    //     (where.productName = Like("%" + likeCode + "%")),
    //     (where.areaCode = Like("%" + likeCode + "%")),
    //     (where.positionName = Like("%" + likeCode + "%")),
    //     (where.productModel = Like("%" + likeCode + "%"))
    //   ];
    // }
    if (beginDate && endDate) {
      endDate = endDate + " 23:59:59";
      where.inStorageDate = Between(beginDate, endDate);
    }
    try {
      let qantity;
      let enterQuantity;
      //预警货位数量
      qantity = await this.dbRead
        .createQueryBuilder(vBaseProductPositionLower, "t")
        .select("COUNT(consignor_Id)", "sum")
        .where(where)
        .andWhere("productStorage<minCapacity")
        .getRawOne();
 
      // 低于数量
      enterQuantity = await this.dbRead
        .createQueryBuilder(vBaseProductPositionLower, "t")
        .select("sum(diffStorage)", "sum")
        .where(where)
        // .andWhere("productStorage<minCapacity")
        .getRawOne();
 
      this.info.result = true;
      this.info.msg = "获取成功";
      qantity = qantity.sum;
      enterQuantity = enterQuantity.sum;
      this.info.data = {
        quantityCount: qantity,
        enterQuantityCount: enterQuantity
      };
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "获取数据时出错:" + ex.message;
    }
    ctx.body = this.info;
  }
  // #endregion
 
  // #region 出入轨迹
  /**
        / 出入轨迹
        */
  @Post()
  public async getProductPositionHistory() {
    let { ctx } = this;
    let body = ctx.request.body;
    // let userInfo = await ctx.helper.userInfo();
    let storage_Id = body.storage_Id;
    let consignor_Id = body.consignor_Id;
    let beginDate = body.beginDate;
    let endDate = body.endDate;
    let userProduct_Id = body.userProduct_Id;
    let provider_Id = body.provider_Id;
    // let likeCode = body.likeCode;
    let where: any = {};
    where.userProduct_Id = userProduct_Id;
    if (storage_Id) {
      where.storage_Id = storage_Id;
    }
    if (provider_Id) {
      where.provider_Id = provider_Id;
    }
    if (consignor_Id) {
      where.consignor_Id = consignor_Id;
    }
    // if (likeCode) {
    //   [
    //     (where.productCode = Like("%" + likeCode + "%")),
    //     (where.productName = Like("%" + likeCode + "%")),
    //     (where.areaCode = Like("%" + likeCode + "%")),
    //     (where.positionName = Like("%" + likeCode + "%")),
    //     (where.productModel = Like("%" + likeCode + "%"))
    //   ];
    // }
    if (beginDate && endDate) {
      endDate = endDate + " 23:59:59";
      where.createDate = Between(beginDate, endDate);
    }
    try {
      let qantity;
      let enterQuantity;
      let inQuantity;
      let shelvedQuantity;
      //当前入库数量合计
      qantity = await this.dbRead
        .createQueryBuilder(BaseProductPositionHistory, "t")
        .select("sum(inQuantity)", "sum")
        .where(where)
        // .andWhere("validityDay<0")
        .getRawOne();
 
      // 当前出库数量合计
      enterQuantity = await this.dbRead
        .createQueryBuilder(BaseProductPositionHistory, "t")
        .select("sum(outQuantity)", "sum")
        .where(where)
        // .andWhere("ValidityDay<ShelfLifeDay*0.3333333")
        .getRawOne();
 
      //当前入库重量合计
      inQuantity = await this.dbRead
        .createQueryBuilder(BaseProductPositionHistory, "t")
        .select("sum(inWeight)", "sum")
        .where(where)
        // .andWhere("ValidityDay<ShelfLifeDay*0.6666666 AND ValidityDay>ShelfLifeDay*0.3333333")
        .getRawOne();
 
      // 当前出库重量合计
      shelvedQuantity = await this.dbRead
        .createQueryBuilder(BaseProductPositionHistory, "t")
        .select("sum(outWeight)", "sum")
        .where(where)
        // .andWhere("(ValidityDay>ShelfLifeDay*0.6666666 OR ShelfLifeDay IS NULL)")
        .getRawOne();
 
      this.info.result = true;
      this.info.msg = "获取成功";
      qantity = qantity.sum;
      enterQuantity = enterQuantity.sum;
      inQuantity = inQuantity.sum;
      shelvedQuantity = shelvedQuantity.sum;
      this.info.data = {
        quantityCount: qantity,
        enterQuantityCount: enterQuantity,
        inQuantityCount: inQuantity,
        shelvedQuantityCount: shelvedQuantity
      };
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "获取数据时出错:" + ex.message;
    }
    ctx.body = this.info;
  }
  // #endregion
}