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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
import { default as BaseController } from "../baseController";
import { Post } from "egg-shell-decorators";
import { StorageCheck } from "../../entity/storage/check/storageCheck";
import { SysParamValue } from "../../entity/sys/core/sysParamValue";
import { StorageCheckList } from "../../entity/storage/check/storageCheckList";
import * as sql from "mssql";
import { BaseStorage } from "../../entity/basicInfo/base/baseStorage";
import { BaseConsignor } from "../../entity/basicInfo/consignor/baseConsignor";
import * as XLSX from "xlsx";
import * as path from "path";
import { BaseProductInfo } from "../../entity/basicInfo/base/baseProductInfo";
import { xBaseProductPositionGroup } from "../../entity/storage/product/xBaseProductPositionGroup";
import { StorageAssembleEnterList } from "../../entity/storage/operation/storageAssembleEnterList";
import { In } from "typeorm";
import moment = require("moment");
import { isNumber } from "util";
import { vBaseProductPosition } from "../../entity/storage/product/vBaseProductPosition";
import { BasePositionPrint } from "../../entity/storage/product/basePositionPrint";
import { siemensApi } from "../../public/webServiceHelper";
export default class CheckController extends BaseController {
  //    #region storageCheck
  @Post()
  public async storageCheck() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    let sql = "";
    let checkType = body.checkType;
    let storage_Id = body.storage_Id;
    let consignor_Id = body.consignor_Id;
    let ids = body.ids; // 选中ID
    let diffDate = body.diffDate;
    let allWhere = await ctx.service.common.getWhere();
    let otherWhere = "";
    let paras: any = {};
    let isBlind = body.isBlind;
 
    //#region 判断是否有库存
    if (checkType == "动态盘") {
      otherWhere = `Storage_Id=:storage_Id And ProductStorage>0 AND Consignor_Id=:consignor_Id  AND     EXISTS
                    (
                        SELECT ProductCode FROM dbo.Base_ProductPositionHistory 
                        WHERE createDate>=DATEADD(DAY, 0 - :diffDate, Convert(Date, GETDATE())) AND ProductCode=t.ProductCode
          )`;
      paras.storage_Id = storage_Id;
      paras.consignor_Id = consignor_Id;
      paras.diffDate = Number(diffDate);
    }
    let builder = this.dbRead.createQueryBuilder(xBaseProductPositionGroup, "t").where(allWhere);
    if (otherWhere) {
      builder.andWhere(otherWhere, paras);
    }
    if (ids) {
      builder.andWhere(`productPosition_Id in(${ids.join(",")})`);
    }
 
    let storageCheckList = await builder.getMany();
 
    if (!storageCheckList.length) {
      this.info.result = false;
      this.info.msg = "未发现可用于动态盘的数据!";
      ctx.body = this.info;
      return;
    }
 
    //    #endregion
    let storageModel = await this.dbRead.findOne(BaseStorage, {
      storage_Id: storage_Id
    });
    let consignor = await this.dbRead.findOne(BaseConsignor, {
      consignor_Id: consignor_Id
    });
    try {
      //    #region  库存盘点主表信息
      let checkInfo = new StorageCheck();
      let checkCode = await ctx.service.common.getCodeRegular(147);
      checkInfo.checkCode = checkCode;
      checkInfo.user_Id = userInfo.user_Id;
      checkInfo.userTrueName = userInfo.userTrueName;
      checkInfo.checkType = checkType;
      if (checkType == "动态盘") {
        checkInfo.diffDate = diffDate;
      }
      checkInfo.storage_Id = storageModel.storage_Id;
      checkInfo.storageName = storageModel.storageName;
      checkInfo.consignor_Id = consignor.consignor_Id;
      checkInfo.consignorCode = consignor.consignorCode;
      checkInfo.consignorName = consignor.consignorName;
      checkInfo.statusID = 1;
      checkInfo.statusText = "新建";
      checkInfo.dept_Id = userInfo.dept_Id;
      checkInfo.deptName = userInfo.deptName;
      checkInfo.applyDate = new Date();
      checkInfo.auditing = 0;
      checkInfo.createID = userInfo.user_Id;
      checkInfo.creator = userInfo.userTrueName;
      checkInfo.createDate = new Date();
      checkInfo.isBlind = isBlind;
 
      await this.setAccountInfo(checkInfo);
 
      await this.dbWrite.insert(StorageCheck, checkInfo);
      //#endregion
 
      // 保存盘点单明细
      for (let item of storageCheckList) {
        let checkDetail = new StorageCheckList();
        checkDetail.check_Id = checkInfo.check_Id;
        checkDetail.product_Id = item.product_Id;
        checkDetail.productCode = item.productCode;
        checkDetail.productName = item.productName;
        checkDetail.productModel = item.productModel;
        checkDetail.productSpec = item.productSpec;
 
        checkDetail.smallUnit = item.smallUnit;
        checkDetail.bigUnit = item.bigUnit;
        checkDetail.unitConvert = item.unitConvert;
        checkDetail.unitConvertText = item.unitConvertText;
        checkDetail.positionName = item.positionName;
        checkDetail.productStorage = item.productStorage;
        checkDetail.purchasePrice = item.purchasePrice;
        checkDetail.purchaseMoney = item.purchaseMoney;
        checkDetail.createID = userInfo.user_Id;
        checkDetail.creator = userInfo.userTrueName;
        checkDetail.createDate = new Date();
 
        checkDetail.batchNumber = item.batchNumber;
        checkDetail.plateCode = item.plateCode;
        checkDetail.produceDate = item.produceDate;
        checkDetail.weight = item.weight;
        checkDetail.totalWeight = item.totalWeight;
        await this.dbWrite.save(checkDetail);
      }
 
      //更新主表
 
      sql = `UPDATE dbo.Storage_Check SET 
                        TotalProductStorage=(SELECT SUM(ProductStorage) FROM dbo.Storage_CheckList WHERE Check_Id=dbo.Storage_Check.Check_Id),
                        TotalPurchaseMoney=(SELECT SUM(ISNULL(PurchaseMoney,0))*SUM(ISNULL(ProductStorage,0)) FROM dbo.Storage_CheckList WHERE Check_Id=dbo.Storage_Check.Check_Id),
                        TotalCheckQuantity=0,
                        TotalProfitQuantity=0,
                        TotalLossQuantity=0,
                        TotalProfitMoney=0,
                        TotalLossMoney=0,
            TotalWeight=(SELECT SUM(TotalWeight) FROM dbo.Storage_CheckList WHERE Check_Id=dbo.Storage_Check.Check_Id)
                    WHERE dbo.Storage_Check.Check_Id=@0`;
      //if (isBlind == 1)
      //{
      //  sql += "UPDATE dbo.Storage_CheckList SET ProductStorage=0,PurchasePrice=0,PurchaseMoney=0 WHERE Check_Id=" + checkInfo.Check_Id + ";";
      //  sql += "UPDATE dbo.Storage_Check SET TotalProductStorage=0 WHERE Check_Id=" + checkInfo.Check_Id + ";";
      //}
      await this.dbWrite.query(sql, [checkInfo.check_Id]);
 
      this.info.msg = "生成盘点单号为" + checkCode;
      this.info.result = true;
      ctx.body = this.info;
    } catch (error) {
      this.info.result = false;
      this.info.msg = "生成盘点单号失败" + error.message;
      ctx.body = this.info;
    }
  }
  //    #endregion
 
  //#region Dropdown
  //盘点类型下拉框
  @Post()
  public async dropdown() {
    let { ctx } = this;
    try {
      let data = await this.dbRead.find(SysParamValue, {
        select: ["value01", "value02"],
        where: { type_Id: 727 }
      });
      let list = [];
 
      for (let row of data) {
        let rows = {};
        rows["body.selectIDs"] = row.value01;
        rows["name"] = row.value02;
        list.push(rows);
      }
 
      if (data) {
        this.info.result = true;
        this.info.data = list;
      }
    } catch (error) {
      this.info.result = true;
      this.info.msg = error.message;
    }
 
    ctx.body = this.info;
  }
  //#endregion
 
  //    #region 提交审核
  /// <summary>
  /// 说明:提交上级审核
  /// 时间:2015-2-16
  /// </summary>
  /// <param name="idLists"></param>
  /// <param name="Provider_Ids"></param>
  /// <returns></returns>
  @Post()
  public async submit() {
    let { ctx } = this;
    let body = ctx.request.body;
    try {
      let checkModel = await this.dbRead.findOne(StorageCheck, {
        check_Id: body.check_Id
      });
 
      // let oproductCode = await this.dbRead.find(StorageCheckList, {
      //   select: ["productCode", "checkQuantity"],
      //   where: {
      //     // checkQuantity: null,
      //     check_Id: body.check_Id
      //   }
      // });
 
      let oproductCode = await this.dbRead.find(StorageCheckList, {
        check_Id: body.check_Id
      });
      for (let list of oproductCode) {
        if (list.checkQuantity === null) {
          this.info.result = false;
          this.info.msg = "物料条码【" + list.productModel + "】盘点数量不能为空!";
          ctx.body = this.info;
          return;
        }
      }
      await this.dbWrite.update(StorageCheck, body.check_Id, {
        statusText: "已提交"
      });
      //复盘提交后修改盈亏单中的复盘数量和复盘差异
      if (checkModel.checkType == "复盘") {
        // 将复盘的盘亏盘盈数量更新盈亏单中
        let sql = `UPDATE dbo.Storage_ProfitLossList SET ReCheckQuantity=cl.CheckQuantity, 
          ReCheckDiff=cl.CheckQuantity-cl.ProductStorage,
          LossQuantity = cl.LossQuantity,
          profitQuantity = cl.profitQuantity
          FROM 
                        Storage_ProfitLossList l INNER JOIN dbo.Storage_CheckList cl 
                        ON l.ProfitLossList_Id=cl.ProfitLossList_Id AND l.ProfitLoss_Id=cl.ProfitLoss_Id
                        WHERE cl.Check_Id=@0;
                        UPDATE dbo.Storage_ProfitLoss SET StatusText='已复盘' WHERE ProfitLoss_Id=(
                            SELECT TOP 1 ProfitLoss_Id FROM dbo.Storage_Check WHERE Check_Id=@0
                        );`;
        await this.dbRead.query(sql, [body.check_Id]);
      }
      this.info.msg = "提交成功";
      this.info.result = true;
    } catch (error) {
      this.info.result = false;
      this.info.msg = "提交失败" + error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 生成盈亏单
  /// <summary>
  /// 插入数据到盈亏单的时候,已经是这个[盘点数量和账面数量不一样]的物料的所有明细了
  /// </summary>
  /// <param name="ids"></param>
  /// <returns></returns>
  @Post()
  public async createYKD() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userinfo = await ctx.helper.userInfo();
    try {
      if (!body.check_Id) {
        this.info.msg = "订单ID不能为空!";
        this.info.result = false;
        ctx.body = this.info;
      } else {
        //生成完盈亏单之后,把盘点单状态更新为 已生成盈亏单
        let code = await ctx.service.common.getCodeRegular(1565);
        //只有已提交状态的盘点单才能生成盈亏单
        const connection: any = await this.dbWrite.connection;
        let request = new sql.Request(connection.driver.master);
        request.input("Ids", body.check_Id);
        request.input("User_Id", userinfo.user_Id);
        request.output("outMsg", sql.NVarChar(2000));
        let result = await request.execute("sp_Storage_ProfitLoss_CreateYKD");
        let outMsg = result.output.outMsg;
        if (outMsg) {
          this.info.result = false;
          this.info.msg = outMsg;
        } else {
          this.info.result = true;
          this.info.msg = "生成成功,单号【" + code + "】";
        }
      }
    } catch (error) {
      this.info.result = false;
      this.info.msg = "错误信息:" + error.message;
    }
    ctx.body = this.info;
  }
  //    #endregion
 
  //#region 开始盘点
  @Post()
  public async startSorting() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    if (!body.selectIDs) {
      this.info.result = false;
      this.info.msg = "数据不存在";
      ctx.body = this.info;
      return;
    }
 
    try {
      // 执行分拣
      const connection: any = await this.dbWrite.connection;
      let request = new sql.Request(connection.driver.master);
      request.input("ProfitLoss_Id", body.selectIDs);
      request.input("user_Id", userInfo.user_Id);
      request.output("outMsg", sql.NVarChar(2000));
      let result = await request.execute("sp_Storage_ProfitLoss_Sorting");
      let outMsg = result.output.outMsg;
      if (outMsg != null && outMsg) {
        this.info.msg = outMsg;
        this.info.result = false;
        ctx.body = this.info;
        return;
      }
 
      let proquest = new sql.Request(connection.driver.master);
      proquest.input("ProfitLoss_Id", body.selectIDs);
      proquest.input("user_Id", userInfo.user_Id);
      proquest.output("outMsg", sql.NVarChar(2000));
      result = await proquest.execute("sp_Storage_ProfitLoss_Check");
      outMsg += result.output.outMsg;
      if (outMsg != null && outMsg) {
        this.info.msg = outMsg;
        this.info.result = false;
        ctx.body = this.info;
        return;
      }
 
      this.info.msg = "盘点盈亏调整成功";
      this.info.result = true;
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 生成复盘单
  /// <summary>
  /// 生成复盘单
  /// 因为这里已经是所有盘点有差别的物料 明细,所以直接把这里的数据创建到复盘单就行
  /// </summary>
  /// <param name="body.selectIDs">盈亏单Id</param>
  /// <param name="ids">视图vStorage_ProfitLossListRecheck的id</param>
  /// <returns></returns>
  @Post()
  public async reCheck() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    let code = await ctx.service.common.getCodeRegular(147); //得到自动编号
    try {
      const connection: any = await this.dbWrite.connection;
      let request = new sql.Request(connection.driver.master);
      request.input("ProfitLoss_Id", body.profitLoss_Id);
      request.input("Product_Ids", body.product_Ids);
      request.input("user_Id", userInfo.user_Id);
      request.input("IsBlind", body.isBlind);
      request.output("outMsg", sql.NVarChar(2000));
      let result = await request.execute("sp_Creact_reCheck");
      let outMsg = result.output.outMsg;
      if (outMsg != null && outMsg) {
        this.info.msg = outMsg;
        this.info.result = false;
        ctx.body = this.info;
        return;
      }
      this.info.result = true;
      this.info.msg = "生成成功,单号【" + code + "】";
    } catch (error) {
      this.info.result = false;
      this.info.msg = "错误信息:" + error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region getStorageAdjustConfig
  @Post()
  public async getStorageAdjustConfig() {
    let { ctx } = this;
    try {
      let state = await ctx.service.common.getConfigBool("IsAdjust");
      this.info.result = true;
      this.info.msg = state.toString();
    } catch (ex) {
      this.info.result = false;
      this.info.msg = ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 盘点单明细导出
  /// <summary>
  /// 货位转移明细导出
  /// </summary>
  /// <param name="body"></param>
  /// <returns></returns>
  @Post()
  public async exportList() {
    let { ctx } = this;
    let body = ctx.request.body;
 
    try {
      let sql = "";
      sql = `SELECT 
 CheckList_Id AS '明细ID',positionName AS '货位名称',ProductCode AS '物料编号',ProductName AS '物料名称',ProductModel AS '条形码',ProductSpec AS '物料规格',CheckQuantity AS '盘点数量',
 ProfitQuantity AS '盘盈数量',ProfitMoney AS '盘盈金额',LossQuantity AS '盘亏数量',LossMoney AS '盘亏金额',ProductStorage AS '账面库存量',PurchaseMoney AS '账面成本额',
 totalWeight AS '账面毛重合计',weight AS '单位毛重',batchNumber AS '批次号',produceDate AS '生产日期',
 limitDate as '到货日期',relationCode AS '关联码'
FROM  Storage_CheckList
WHERE CheckList_Id IN(select col from dbo.split(@0, ','))`;
 
      let transferList: Array<any> = await this.dbRead.query(sql, [body.ids]);
 
      let root = path.resolve();
      let url = "/download/盘点单明细数据.xlsx";
      let fileName = root + url.replace(/\//g, path.sep);
      let pathToCreate = fileName.substring(0, fileName.lastIndexOf(path.sep));
      ctx.helper.mkdir(pathToCreate);
 
      // wayBillList = wayBillList.splice(0, 0, {
      //   "分运单号":"xxxxxx"
      // }, {"申报类型": "xxxxsssss"}, {});
      let jsonWorkSheet = XLSX.utils.json_to_sheet(transferList);
      // 构造workBook
      let workBook = {
        SheetNames: ["数据"],
        Sheets: {
          数据: jsonWorkSheet
        }
      };
      XLSX.writeFile(workBook, fileName);
 
      this.info.result = true;
      this.info.data = {
        url: url
      };
      this.info.msg = "导出成功!";
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 导入Excel
  /// <summary>
  /// 导入Excel
  /// </summary>
  /// <returns></returns>
  @Post()
  public async importExel() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    let redis = ctx.app.redis.clients.get("common"); // 将消息放入redis缓存
    let fileUrl = body.url;
    redis.expire(body.key, 5 * 60);
    if (!fileUrl) {
      redis.rpush(body.key, "上传文件不存在");
      return;
    }
    try {
      let rootPath = path.resolve(); // 获得根目录
      let filePath = rootPath + path.sep + fileUrl.replace(/\//gi, path.sep); // 上传文件路径
      var workbook = XLSX.readFile(filePath); //整个 excel 文档
      var sheetNames = workbook.SheetNames; //获取所有工作薄名
      var sheet1 = workbook.Sheets[sheetNames[0]]; //根据工作薄名获取工作薄
      let dataList = XLSX.utils.sheet_to_json(sheet1); // 获得当前sheet表单数据转为json格式
 
      //#region 验证数据正确性
      this.info.result = true;
      if (!dataList.length) {
        redis.rpush(body.key, "没有可导入的数据");
        return;
      }
      let msg = "";
      let i = 0;
      for (let row of dataList) {
        i++;
        let checkList_Id = row["明细ID"];
        let positionName = row["货位名称"];
        let productCode = row["物料编号"];
        let productName = row["物料名称"];
        let productModel = row["条形码"];
        let checkQuantity = row["盘点数量"];
        if (!positionName) {
          msg += `${i}、货位名称不能为空`;
          redis.rpush(body.key, msg);
          this.info.result = false;
        }
        if (!productCode) {
          msg += `${i}、物料编号不能为空`;
          redis.rpush(body.key, msg);
          this.info.result = false;
        }
        if (!productName) {
          msg += `${i}、物料名称不能为空`;
          redis.rpush(body.key, msg);
          this.info.result = false;
        }
        if (!productModel) {
          msg += `${i}、条形码不能为空`;
          redis.rpush(body.key, msg);
          this.info.result = false;
        }
        let produceDate = row["生产日期"];
        if (produceDate) {
          if (!isNumber(produceDate)) {
            msg = `<span style='color:red;font-weight:bold;'>${i}"、产品编号[${productCode}]生产时间的格式不正确!</span>`;
            redis.rpush(body.key, msg);
            this.info.result = false;
          } else {
            row["生产日期"] = moment(new Date(1900, 0, produceDate - 1)).format("YYYY-MM-DD");
          }
        }
        if (!checkQuantity) {
          msg += `${i}、盘点数量不能为空`;
          redis.rpush(body.key, msg);
          this.info.result = false;
        }
        // #endregion
 
        if (msg) {
          this.info.result = false;
          this.info.msg = msg;
        } else {
          // 物料信息
          let prodInfo = await this.dbRead.findOne(BaseProductInfo, {
            userProduct_Id: userInfo.userProduct_Id,
            productModel: productModel
          });
          // 盘点明细
          let checkDetail = await this.dbRead.findOne(StorageCheckList, {
            check_Id: body.check_Id,
            checkList_Id: checkList_Id
          });
          let checkQuantity = Number(row["盘点数量"]);
          let productStorage = Number(row["账面库存量"]);
          let profitQuantity = 0;
          let profitMoney = 0;
          let lossQuantity = 0;
          let lossMoney = 0;
          if (checkDetail) {
            if (productStorage <= checkQuantity) {
              profitQuantity = checkQuantity - productStorage;
              profitMoney = profitQuantity * checkDetail.purchasePrice;
            } else {
              lossQuantity = productStorage - checkQuantity;
              lossMoney = lossQuantity * checkDetail.purchasePrice;
            }
            // 明细盘点数量
            await this.dbWrite.update(StorageCheckList, checkDetail.checkList_Id, {
              checkQuantity: checkQuantity,
              profitQuantity: profitQuantity,
              profitMoney: profitMoney,
              lossQuantity: lossQuantity,
              lossMoney: profitMoney
            });
          } else {
            if (productStorage <= checkQuantity) {
              profitQuantity = checkQuantity - productStorage;
              profitMoney = profitQuantity * (prodInfo ? prodInfo.purchasePrice : 0);
            } else {
              lossQuantity = productStorage - checkQuantity;
              lossMoney = lossQuantity * (prodInfo ? prodInfo.purchasePrice : 0);
            }
 
            await this.dbWrite.save(StorageCheckList, {
              check_Id: body.check_Id,
              createID: userInfo.user_Id,
              creator: userInfo.userTrueName,
              positionName: row["货位名称"],
              product_Id: prodInfo.product_Id,
              productCode: row["物料编号"],
              productName: row["物料名称"],
              productModel: row["条形码"],
              productSpec: row["物料规格"],
              checkQuantity: checkQuantity,
              profitQuantity: profitQuantity,
              profitMoney: profitMoney,
              lossQuantity: lossQuantity,
              lossMoney: lossMoney,
              productStorage: productStorage,
              purchaseMoney: row["账面成本额"],
              totalWeight: row["账面毛重合计"],
              weight: row["单位毛重"],
              batchNumber: row["批次号"],
              produceDate: row["生产日期"],
              limitDate: row["到货日期"],
              relationCode: row["关联码"]
            });
          }
          this.info.result = true;
          this.info.msg = "导入成功";
        }
      }
      // 导入成功以后计算主表数据
      let sql = `
      UPDATE Storage_Check SET
                    totalCheckQuantity=(SELECT SUM(checkQuantity) FROM Storage_CheckList WHERE check_Id = Storage_Check.Check_Id),
                    totalWeight=(SELECT SUM(totalWeight) FROM Storage_CheckList WHERE check_Id = Storage_Check.Check_Id),
                    totalProfitQuantity=(SELECT SUM(profitQuantity) FROM Storage_CheckList WHERE check_Id = Storage_Check.Check_Id),
                    totalProfitMoney=(SELECT SUM(profitMoney) FROM Storage_CheckList WHERE check_Id = Storage_Check.Check_Id),
                    totalLossQuantity=(SELECT SUM(lossQuantity) FROM Storage_CheckList WHERE check_Id = Storage_Check.Check_Id),
                    totalLossMoney=(SELECT SUM(lossMoney) FROM Storage_CheckList WHERE check_Id = Storage_Check.Check_Id)
                    WHERE Storage_Check.Check_Id=${body.check_Id}`;
      await this.dbRead.query(sql);
      // 计算明细
      let sqlDetail = `UPDATE Storage_CheckList SET
                    profitWeight=profitQuantity*weight
                    WHERE Storage_CheckList.Check_Id=${body.check_Id}`;
      await this.dbRead.query(sqlDetail);
    } catch (ex) {
      this.info.msg = "出现异常:" + ex.message;
      this.info.result = false;
    }
 
    ctx.body = this.info;
  }
 
  // #endregion
 
  //#region 盘点盈亏单明细导出
  /// <summary>
  /// 盘点盈亏单货位导出
  /// </summary>
  /// <param name="body"></param>
  /// <returns></returns>
  @Post()
  public async exportlossList() {
    let { ctx } = this;
    let body = ctx.request.body;
 
    try {
      let sql = "";
      sql = `SELECT   ProductCode AS '物料编号', MAX(ProductName) AS '物料名称', MAX(ProductModel) AS '条形码', 
                MAX(RelationCode) AS '关联码', SUM(ProductStorage) AS '账面库存量', SUM(CheckQuantity) 
                AS '初盘数量', SUM(ReCheckQuantity) AS '复盘数量', SUM(CheckDiff) AS '初盘差异', SUM(ReCheckDiff) 
                AS '复盘差异','' AS '备注'
                FROM      Storage_ProfitLossList
                WHERE   (ProfitLoss_Id =@0)
                GROUP BY ProductCode`;
 
      let transferList: Array<any> = await this.dbRead.query(sql, [body.ids]);
 
      let root = path.resolve();
      let url = "/download/盈亏单明细数据.xlsx";
      let fileName = root + url.replace(/\//g, path.sep);
      let pathToCreate = fileName.substring(0, fileName.lastIndexOf(path.sep));
      ctx.helper.mkdir(pathToCreate);
 
      // wayBillList = wayBillList.splice(0, 0, {
      //   "分运单号":"xxxxxx"
      // }, {"申报类型": "xxxxsssss"}, {});
      let jsonWorkSheet = XLSX.utils.json_to_sheet(transferList);
      // 构造workBook
      let workBook = {
        SheetNames: ["数据"],
        Sheets: {
          数据: jsonWorkSheet
        }
      };
      XLSX.writeFile(workBook, fileName);
 
      this.info.result = true;
      this.info.data = {
        url: url
      };
      this.info.msg = "导出成功!";
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 盘点盈亏单货位导出
  /// <summary>
  /// 盘点盈亏单明细导出
  /// </summary>
  /// <param name="body"></param>
  /// <returns></returns>
  @Post()
  public async exportPositionName() {
    let { ctx } = this;
    let body = ctx.request.body;
 
    try {
      let sql = "";
      sql = `SELECT   ProductCode AS '物料编号', MAX(ProductName) AS '物料名称', MAX(ProductModel) AS '条形码', 
                MAX(RelationCode) AS '关联码', SUM(ProductStorage) AS '账面库存量', SUM(CheckQuantity) 
                AS '初盘数量', SUM(ReCheckQuantity) AS '复盘数量', SUM(CheckDiff) AS '初盘差异', SUM(ReCheckDiff) 
                AS '复盘差异','' AS '备注'
                FROM      Storage_ProfitLossList
                WHERE   (ProfitLoss_Id =@0)
                GROUP BY ProductCode,PositionName`;
 
      let transferList: Array<any> = await this.dbRead.query(sql, [body.ids]);
 
      let root = path.resolve();
      let url = "/download/盈亏单货位数据.xlsx";
      let fileName = root + url.replace(/\//g, path.sep);
      let pathToCreate = fileName.substring(0, fileName.lastIndexOf(path.sep));
      ctx.helper.mkdir(pathToCreate);
 
      // wayBillList = wayBillList.splice(0, 0, {
      //   "分运单号":"xxxxxx"
      // }, {"申报类型": "xxxxsssss"}, {});
      let jsonWorkSheet = XLSX.utils.json_to_sheet(transferList);
      // 构造workBook
      let workBook = {
        SheetNames: ["数据"],
        Sheets: {
          数据: jsonWorkSheet
        }
      };
      XLSX.writeFile(workBook, fileName);
 
      this.info.result = true;
      this.info.data = {
        url: url
      };
      this.info.msg = "导出成功!";
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "错误信息:" + ex.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region app-根据盘点单号获取盘点单数据接口
  @Post()
  public async getCheckInfoList() {
    let { ctx } = this;
    let body = ctx.request.body;
    let checkCode = body.checkCode; // 盘点单号
    try {
      this.info = await ctx.service.storage.checkService.getCheckInfoList(checkCode);
    } catch (error) {
      this, (this.info.result = false);
      this.info.msg = "错误:" + error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 入库明细批量修改货位名称
  @Post()
  public async updatepositionName() {
    let { ctx } = this;
    let body = ctx.request.body;
    let positionName = body.positionName; // 货位名称
    let assemble_Id = body.assemble_Id; // 货位名称
    let assembleEnterList_Id = body.assembleEnterList_Id;
    try {
      await this.dbWrite.update(
        StorageAssembleEnterList,
        {
          assemble_Id: assemble_Id,
          assembleEnterList_Id: In(assembleEnterList_Id)
        },
        {
          positionName: positionName
        }
      );
      this.info.result = true;
      this.info.msg = "保存成功";
    } catch (error) {
      this.info.result = false;
      this.info.msg = "错误:" + error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region app-提交盘点
  @Post()
  public async saveCheckByCheckList() {
    let { ctx } = this;
    let body = ctx.request.body;
    let checkCode = body.checkCode; // 盘点单号
    let dataArray = body.dataArray; // 盘点扫描提交
    try {
      this.info = await ctx.service.storage.checkService.saveCheckByCheckList(checkCode, dataArray);
    } catch (error) {
      this, (this.info.result = false);
      this.info.msg = "错误:" + error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 更改打印标签数量
  /**
   *更改打印标签数量
   */
  @Post()
  public async updateData() {
    let { ctx } = this;
    let body = ctx.request.body;
    try {
      let positionOne = await this.dbRead.findOne(vBaseProductPosition, {
        productPosition_Id: body.productPosition_Id
      });
      // const productStorage = positionOne.productStorage - body.quantity;
      // await this.dbWrite.update(vBaseProductPosition, body.productPosition_Id, {
      //   productStorage: productStorage
      // });
      // 是否打开服务   打开的话就wcs扣除库存数量
      siemensApi.domainUrl = this.app.config.domainUrl;
      const subtractQtyResult = await siemensApi.subtractQty({
        detailList: body.detailList
      });
      let ssss = subtractQtyResult;
      if (ssss !== true) {
        this.info.msg = "请打开后台服务";
        ctx.body = this.info;
        return;
      }
      // 打印记录添加记录
      let shelve = new BasePositionPrint();
      shelve.poCode = positionOne.poCode;
      shelve.inStorageDate = positionOne.inStorageDate;
      shelve.productName = positionOne.productName;
      shelve.saleCode = positionOne.saleCode;
      shelve.productCode = positionOne.productCode;
      shelve.quantity = body.quantity;
      shelve.providerShortName = positionOne.providerShortName;
      shelve.extendField04 = positionOne.extendField04;
      shelve.extendField07 = positionOne.extendField07;
      shelve.extendField08 = positionOne.extendField08;
      shelve.smallUnit = positionOne.smallUnit;
      // 生成打印条码值
      shelve.extendField06 = await ctx.service.common.getCodeRegular(1840);
      shelve.itemNumber = positionOne.itemNumber;
      shelve.limitDate = positionOne.limitDate;
      // 重量根据输入的重量存储
      shelve.totalWeight = body.totalWeight;
      shelve.productPosition_Id = body.productPosition_Id;
      // 打印记录的条码值更新到相对应库存
      await this.dbWrite.update(vBaseProductPosition, body.productPosition_Id, {
        extendField06: shelve.extendField06
      });
      await this.setAccountInfo(shelve);
      await this.dbWrite.save(shelve);
      this.info.result = true;
      // this.info.msg = "已更新库存";
      ctx.body = this.info;
    } catch (error) {
      this.info.result = false;
      this.info.msg = "已更新库存" + error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 更改打印标签数量
  /**
   *更改打印标签数量
   */
  @Post()
  public async updatePrintCount() {
    let { ctx } = this;
    let body = ctx.request.body;
    try {
      let positionOne = await this.dbRead.findOne(BasePositionPrint, {
        position_Id: body.position_Id
      });
      await this.dbWrite.update(BasePositionPrint, body.position_Id, {
        printCount: (positionOne.printCount || 0) + 1
      });
      this.info.result = true;
      // this.info.msg = "已更新库存";
      ctx.body = this.info;
    } catch (error) {
      this.info.result = false;
      this.info.msg = "已更新库存" + error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
}