杨前锦
2025-07-01 a93b0e99036c24b9bd58c79bf5e7364b1ba28bae
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
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
using System;
using System.Collections.Generic;
using System.Linq;
using HH.WCS.Mobox3.HD.util;
using SqlSugar;
using HH.WCS.Mobox3.HD.models;
using HH.WCS.Mobox3.HD.wms;
using static HH.WCS.Mobox3.HD.api.DigitHelper;
using static HH.WCS.Mobox3.HD.api.DigitHelper.GJSpecStatisticData;
using System.Reflection;
using HH.WCS.Mobox3.HD.device;
using static HH.WCS.Mobox3.HD.core.Monitor;
using Newtonsoft.Json;
using static HH.WCS.Mobox3.HD.api.WmsController;
 
 
namespace HH.WCS.Mobox3.HD.api
{
    public class DigitHelper
    {
 
        /// <summary>
        /// 1.查询当前备货区所有成品信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static List<BaseItemInfo> queryAllFinishProduct(AreaModel model)
        {
            BaseItemInfo itemInfo = new BaseItemInfo();
            var db = new SqlHelper<object>().GetInstance();
            List<BaseItemInfo> itemInfoList = db.Queryable<Location>()
            .LeftJoin<LocCntrRel>((l, lc) => l.S_CODE == lc.S_LOC_CODE)
            .LeftJoin<CntrItemRel>((l, lc, ci) => lc.S_CNTR_CODE == ci.S_CNTR_CODE)
            .Where((l, lc, ci) => l.S_AREA_CODE == model.areaCode && l.N_CURRENT_NUM > 0)
                .Select(
                   (l, lc, ci) => new BaseItemInfo
                   {
                       areaCode = l.S_AREA_CODE,
                       trayCode = lc.S_CNTR_CODE,
                       locCode = l.S_CODE,
                       workNo = ci.S_WORK_NO,
                       workName = ci.S_WORK_NAME,
                       dePartNo = ci.S_DEPART_NO,
                       partDrawNo = ci.S_PARTDRAW_NO,
                       partName = ci.S_PART_NAME,
                       itemCode = ci.S_ITEM_CODE,
                       itemName = ci.S_ITEM_NAME,
                       weight = ci.F_WEIGHT,
                       quantity = ci.F_QTY,
                   }
                ).ToList();
            return itemInfoList;
        }
 
        /// <summary>
        /// 2.查询三个立体库的汇总数据
        /// </summary>
        /// <returns></returns>
        public static LTKSummaryData queryLTKSummaryData()
        {
            LTKSummaryData summaryData = new LTKSummaryData();
            var db = new SqlHelper<object>().GetInstance();
            List<string> areaCodes = Settings.getStoreAreaCodes(0, 1);
            var query = db.Queryable<Location>().Where(a => areaCodes.Contains(a.S_AREA_CODE));
 
            int sum = query.Count(); // 三个立体库总货位数
            int usageNum = query.Where(a => a.N_CURRENT_NUM > 0).Count(); // 三个立体库总共已使用库位数
            double usageRate = Math.Round((double)usageNum / sum * 100, 2); //三个立体库库位使用率(=已使用库位数/总库位数)
 
            int semiProdNum = 0;
            double semiProdProp = 0;
            {
                //三个立体库合计半成品所占库位数
                semiProdNum = db.Queryable<Location>()
                 .LeftJoin<LocCntrRel>((l, lc) => l.S_CODE == lc.S_LOC_CODE)
                 .LeftJoin<CntrItemRel>((l, lc, ci) => lc.S_CNTR_CODE == ci.S_CNTR_CODE)
                 .Where((l, lc, ci) => areaCodes.Contains(l.S_AREA_CODE) && l.N_CURRENT_NUM > 0 && ci.S_ITEM_TYPE == "半成品").Count();
                //个立体库合计半成品所占库位数在总体中占比
                semiProdProp = Math.Round((double)semiProdNum / sum * 100, 2);
            }
 
            int finishProdNum = 0;
            double finishProdProp;
            {
                //三个立体库合计成品所占库位数
                finishProdNum = db.Queryable<Location>()
                 .LeftJoin<LocCntrRel>((l, lc) => l.S_CODE == lc.S_LOC_CODE)
                 .LeftJoin<CntrItemRel>((l, lc, ci) => lc.S_CNTR_CODE == ci.S_CNTR_CODE)
                 .Where((l, lc, ci) => areaCodes.Contains(l.S_AREA_CODE) && l.N_CURRENT_NUM > 0 && ci.S_ITEM_TYPE == "成品").Count(); ;
                //三个立体库合计成品所占库位数在总体中占比
                finishProdProp = Math.Round((double)finishProdNum / sum * 100, 2);
            }
 
            summaryData.sum = sum;
            summaryData.usageNum = usageNum;
            summaryData.usageRate = usageRate + "%";
            summaryData.semiProdNum = semiProdNum;
            summaryData.semiProdProp = semiProdProp + "%";
            summaryData.finishProdNum = finishProdNum;
            summaryData.finishProdProp = finishProdProp + "%";
            return summaryData;
        }
 
        /// <summary>
        /// 3.按图号汇总成品、半成品所占库位数量列表
        /// </summary>
        /// <returns></returns>
        public static List<WorkNoSummaryData> queryLTKWorkNoSummaryData1(ItemTypeModel model)
        {
            var db = new SqlHelper<object>().GetInstance();
            // 三个立体库总货位数
            List<string> areaCodes = Settings.getStoreAreaCodes(0, 1);
            List<WorkNoSummaryData> workSummaryDatas = db.Queryable<Location>()
                        .LeftJoin<LocCntrRel>((l, lc) => l.S_CODE == lc.S_LOC_CODE)
                        .LeftJoin<CntrItemRel>((l, lc, ci) => lc.S_CNTR_CODE == ci.S_CNTR_CODE && ci.S_ITEM_TYPE == model.itemType)
                        .Where((l, lc, ci) => areaCodes.Contains(l.S_AREA_CODE) && l.N_CURRENT_NUM > 0 && ci.S_ITEM_TYPE == model.itemType)
                        .GroupBy((l, lc, ci) => new { ci.S_WORK_NO, ci.S_WORK_NAME })
                        .Select(
                            (l, lc, ci) => new WorkNoSummaryData()
                            {
                                workNo = ci.S_WORK_NO,
                                workName = ci.S_WORK_NAME,
                                amount = SqlFunc.AggregateCount(ci.S_WORK_NO)
                            }
                        )
                        .MergeTable()
                        .OrderByDescending(a => a.amount)
                        .Take(5)
                        .ToList();
            if (workSummaryDatas != null && workSummaryDatas.Count > 0)
            {
                float sum = workSummaryDatas.Sum(a => a.amount);
                foreach (var item in workSummaryDatas)
                {
                    item.proportion = Math.Round((double)item.amount / sum * 100, 2) + "%";
                }
            }
            return workSummaryDatas;
        }
 
        /// <summary>
        /// 4.按工作号汇总成品、半成品所占库位数量、占比
        /// </summary>
        /// <returns></returns>
        public static List<WorkNoSummaryData> queryLTKWorkNoSummaryData()
        {
            var db = new SqlHelper<object>().GetInstance();
            // 三个立体库总货位数
            List<string> areaCodes = Settings.getStoreAreaCodes(0, 1);
            List<WorkNoSummaryData> summaryDatas = db.Queryable<Location>()
             .LeftJoin<LocCntrRel>((l, lc) => l.S_CODE == lc.S_LOC_CODE)
             .LeftJoin<CntrItemRel>((l, lc, ci) => lc.S_CNTR_CODE == ci.S_CNTR_CODE)
             .Where((l, lc, ci) => areaCodes.Contains(l.S_AREA_CODE) && l.N_CURRENT_NUM > 0 )
             .GroupBy((l, lc, ci) => new { ci.S_WORK_NO, ci.S_WORK_NAME })
             .Select(
                 (l, lc, ci) => new WorkNoSummaryData()
                 {
                     workNo = ci.S_WORK_NO,
                     workName = ci.S_WORK_NAME,
                     amount = SqlFunc.AggregateCount(ci.S_PARTDRAW_NO)
                 }
             )
             .MergeTable()
             .OrderByDescending(a => a.amount)
             .Take(5)
             .ToList();
 
            if (summaryDatas != null && summaryDatas.Count > 0)
            {
                float sum = summaryDatas.Sum(a => a.amount);
                foreach (var item in summaryDatas)
                {
                    item.proportion = Math.Round((double)item.amount / sum * 100, 2) + "%";
                }
            }
            return summaryDatas;
        }
 
        /// <summary>
        /// 4.1.按工作号汇总成品、半成品所占库位数量、占比
        /// </summary>
        /// <returns></returns>
        public static List<WorkNoSummaryData> queryWorkNoAndItemTypeLTKSummaryData(AreaModel model)
        {
            var db = new SqlHelper<object>().GetInstance();
            // 三个立体库总货位数
            List<string> areaCodes = Settings.getStoreAreaCodes(0, 1);
            var queryable = db.Queryable<Location>()
             .LeftJoin<LocCntrRel>((l, lc) => l.S_CODE == lc.S_LOC_CODE)
             .LeftJoin<CntrItemRel>((l, lc, ci) => lc.S_CNTR_CODE == ci.S_CNTR_CODE)
             .Where((l, lc, ci) => l.N_CURRENT_NUM > 0);
 
            if (model.areaCode == null)
            {
                queryable = queryable.Where((l, lc, ci) => areaCodes.Contains(l.S_AREA_CODE));
            }
            else
            {
                queryable = queryable.Where((l, lc, ci) => l.S_AREA_CODE == model.areaCode);
            }
 
            List<WorkNoSummaryData> summaryDatas = queryable
            .GroupBy((l, lc, ci) => new { l.S_AREA_CODE, ci.S_WORK_NO, ci.S_ITEM_TYPE })
            .Select(
                (l, lc, ci) => new WorkNoSummaryData()
                {
                    areaCode = l.S_AREA_CODE,
                    workNo = ci.S_WORK_NO,
                    type = ci.S_ITEM_TYPE,
                    amount = SqlFunc.AggregateCount(ci.S_PARTDRAW_NO)
                }
            )
            .MergeTable()
            .OrderByDescending(a => a.amount)
            .ToList();
 
            if (summaryDatas != null && summaryDatas.Count > 0)
            {
                float sum = summaryDatas.Sum(a => a.amount);
                foreach (var item in summaryDatas)
                {
                    item.areaName = LocationHelper.GetArea(item.areaCode).S_NAME;
                    item.proportion = Math.Round((double)item.amount / sum * 100, 2) + "%";
                }
            }
            return summaryDatas;
        }
 
        /// <summary>
        /// 5.根据在库时长汇总成品、半成品的所占库位数量
        /// </summary>
        /// <returns></returns>
        public static StorageTimeSummaryData storageTimeSummary()
        {
            StorageTimeSummaryData summaryData = new StorageTimeSummaryData();
            var db = new SqlHelper<object>().GetInstance();
            // 三个立体库总货位数
            List<string> areaCodes = Settings.getStoreAreaCodes(0, 1);
            var query = db.Queryable<Location>()
            .LeftJoin<LocCntrRel>((l, lc) => l.S_CODE == lc.S_LOC_CODE)
            .LeftJoin<CntrItemRel>((l, lc, ci) => lc.S_CNTR_CODE == ci.S_CNTR_CODE)
            .Where((l, lc, ci) => areaCodes.Contains(l.S_AREA_CODE));
 
            int sum = query.Count(); // 总库位数
            int usageNum = query.Where((l, lc, ci) => l.N_CURRENT_NUM > 0 && (ci.S_ITEM_TYPE == "成品" || ci.S_ITEM_TYPE == "半成品")).Count(); // 使用数
            string usageRate = Math.Round((double)usageNum / sum * 100, 2) + "%";  // 使用率
            int amountA = 0;
            string proportionA = null;
            {
                var queryWrapper = db.Queryable<Location>()
                .LeftJoin<LocCntrRel>((l, lc) => l.S_CODE == lc.S_LOC_CODE)
                .LeftJoin<CntrItemRel>((l, lc, ci) => lc.S_CNTR_CODE == ci.S_CNTR_CODE)
                .Where((l, lc, ci) => areaCodes.Contains(l.S_AREA_CODE) && l.N_CURRENT_NUM > 0 && (ci.S_ITEM_TYPE == "成品" || ci.S_ITEM_TYPE == "半成品"));
                amountA = queryWrapper.Where("DATEDIFF(day, l.T_FULL_TIME, GETDATE()) <= 7").Count();
                proportionA = Math.Round((double)amountA / sum * 100, 2) + "%";
            }
            int amountB = 0;
            string proportionB = null;
            {
                var queryWrapper = db.Queryable<Location>()
                .LeftJoin<LocCntrRel>((l, lc) => l.S_CODE == lc.S_LOC_CODE)
                .LeftJoin<CntrItemRel>((l, lc, ci) => lc.S_CNTR_CODE == ci.S_CNTR_CODE)
                .Where((l, lc, ci) => areaCodes.Contains(l.S_AREA_CODE) && l.N_CURRENT_NUM > 0 && (ci.S_ITEM_TYPE == "成品" || ci.S_ITEM_TYPE == "半成品"));
                amountB = queryWrapper.Where("DATEDIFF(day, l.T_FULL_TIME, GETDATE()) > 7 and DATEDIFF(day, T_FULL_TIME, GETDATE()) <= 15").Count();
                proportionB = Math.Round((double)amountB / sum * 100, 2) + "%";
            }
            int amountC = 0;
            string proportionC = null;
            {
                var queryWrapper = db.Queryable<Location>()
                .LeftJoin<LocCntrRel>((l, lc) => l.S_CODE == lc.S_LOC_CODE)
                .LeftJoin<CntrItemRel>((l, lc, ci) => lc.S_CNTR_CODE == ci.S_CNTR_CODE)
                .Where((l, lc, ci) => areaCodes.Contains(l.S_AREA_CODE) && l.N_CURRENT_NUM > 0 && (ci.S_ITEM_TYPE == "成品" || ci.S_ITEM_TYPE == "半成品"));
                amountC = queryWrapper.Where("DATEDIFF(day, l.T_FULL_TIME, GETDATE()) > 15 and DATEDIFF(day, T_FULL_TIME, GETDATE()) <= 30").Count();
                proportionC = Math.Round((double)amountC / sum * 100, 2) + "%";
            }
            int amountD = 0;
            string proportionD = null;
            {
                var queryWrapper = db.Queryable<Location>()
                .LeftJoin<LocCntrRel>((l, lc) => l.S_CODE == lc.S_LOC_CODE)
                .LeftJoin<CntrItemRel>((l, lc, ci) => lc.S_CNTR_CODE == ci.S_CNTR_CODE)
                .Where((l, lc, ci) => areaCodes.Contains(l.S_AREA_CODE) && l.N_CURRENT_NUM > 0 && (ci.S_ITEM_TYPE == "成品" || ci.S_ITEM_TYPE == "半成品"));
                amountD = queryWrapper.Where("DATEDIFF(day, l.T_FULL_TIME, GETDATE()) > 30 and DATEDIFF(day, T_FULL_TIME, GETDATE()) <= 90").Count();
                proportionD = Math.Round((double)amountD / sum * 100, 2) + "%";
            }
            int amountE = 0;
            string proportionE = null;
            {
                var queryWrapper = db.Queryable<Location>()
                .LeftJoin<LocCntrRel>((l, lc) => l.S_CODE == lc.S_LOC_CODE)
                .LeftJoin<CntrItemRel>((l, lc, ci) => lc.S_CNTR_CODE == ci.S_CNTR_CODE)
                .Where((l, lc, ci) => areaCodes.Contains(l.S_AREA_CODE) && l.N_CURRENT_NUM > 0 && (ci.S_ITEM_TYPE == "成品" || ci.S_ITEM_TYPE == "半成品"));
                amountE = queryWrapper.Where("DATEDIFF(day, l.T_FULL_TIME, GETDATE()) > 90").Count();
                proportionE = Math.Round((double)amountE / sum * 100, 2) + "%";
            }
            summaryData.sum = sum;
            summaryData.usageNum = usageNum;
            summaryData.usageRate = usageRate;
            summaryData.amountA = amountA;
            summaryData.proportionA = proportionA;
            summaryData.amountB = amountB;
            summaryData.proportionB = proportionB;
            summaryData.amountC = amountC;
            summaryData.proportionC = proportionC;
            summaryData.amountD = amountD;
            summaryData.proportionD = proportionD;
            summaryData.amountE = amountE;
            summaryData.proportionE = proportionE;
            return summaryData;
        }
 
        /// <summary>
        /// 6.按图号汇总三个立体库的出库(入库)数量和占比
        /// </summary>
        /// <returns></returns>
        public static List<DrawSummaryData> outInStockDrawNoSummary(ActModel actModel)
        {
            var db = new SqlHelper<object>().GetInstance();
            // 三个立体库总货位数
            List<string> areaCodes = Settings.getStoreAreaCodes(0, 1);
            var query = db.Queryable<StockRecord>().Where(a => areaCodes.Contains(a.S_AREA_CODE));
            DateTime date = DateTime.Parse(actModel.dataDate).Date;
            string sql = null;
            if (actModel.actType.Equals("入库"))
            {
                sql = "CONVERT(date, T_IN_TIME) = @date";
            }
            else
            {
                sql = "CONVERT(date, T_OUT_TIME) = @date";
            }
            List<DrawSummaryData> drawSummaryDatas = query.Where(sql, new { date }).GroupBy(a => new { a.S_PARTDRAW_NO, a.S_PART_NAME ,a.S_WORK_NO })
                .Select(
                    a => new DrawSummaryData()
                    {
                        partDrawNo = a.S_PARTDRAW_NO,
                        partName = a.S_PART_NAME,
                        workNo = a.S_WORK_NO,
                        amount = SqlFunc.AggregateCount(a.S_PARTDRAW_NO)
                    }
                ).OrderByDescending(a => a.amount).Take(5).ToList();
            if (drawSummaryDatas != null && drawSummaryDatas.Count > 0)
            {
                float sum = drawSummaryDatas.Sum(a => a.amount);
                foreach (var item in drawSummaryDatas)
                {
                    if (item.partDrawNo == null) { 
                        drawSummaryDatas.Remove(item);
                        continue;
                    }
                    item.proportion = Math.Round((double)item.amount / sum * 100, 2) + "%";
                }
            }
            return drawSummaryDatas;
        }
 
        /// <summary>
        /// 7.查询今日出厂的成品信息
        /// </summary>
        /// <returns></returns>
        public static List<WorkNoOutStockData> queryOutFactoryFinishProdList(DateModel model)
        {
            var db = new SqlHelper<object>().GetInstance();
            DateTime date = DateTime.Parse(model.dataDate).Date;
            List<string> areaCodes = Settings.getDispatchAreaCodes();
            var query = db.Queryable<StockRecord>().Where(a => areaCodes.Contains(a.S_AREA_CODE));
            List<WorkNoOutStockData> outStockDatas = new List<WorkNoOutStockData>();
            {
                outStockDatas = query.Where("CONVERT(date, T_OUT_TIME) = @date", new { date }).GroupBy(a => new { a.S_WORK_NO, a.S_WORK_NAME, a.S_DEPART_NO })
                .Select(
                    a => new WorkNoOutStockData()
                    {
                        workNo = a.S_WORK_NO,
                        workName = a.S_WORK_NAME,
                        dePartNo = a.S_DEPART_NO,
                        quantity = SqlFunc.AggregateSumNoNull(a.F_QTY),
                        weight = SqlFunc.AggregateSumNoNull(a.F_WEIGHT)
                    }
                ).ToList();
            }
            foreach (var item in outStockDatas)
            {
                item.accuQuantity = db.Queryable<StockRecord>().Where(a => areaCodes.Contains(a.S_AREA_CODE) && a.S_ACT_TYPE == "出库" && a.S_WORK_NO == item.workNo).Sum(a => a.F_QTY);
            }
            return outStockDatas;
        }
 
        /// <summary>
        /// 8.查询当前立体库汇总数据
        /// </summary>
        /// <returns></returns>
        public static AreaSummaryData queryLTKAreaSummaryData(AreaModel model)
        {
            AreaSummaryData areaSummaryData = new AreaSummaryData();
            var db = new SqlHelper<object>().GetInstance();
            var query = db.Queryable<Location>().Where(a => a.S_AREA_CODE == model.areaCode);
            {
                int sum = query.Count();
                int usageNum = query.Where(a => a.N_CURRENT_NUM > 0).Count();
                double usageRate = Math.Round((double)usageNum / sum * 100, 2);
                areaSummaryData.sum = sum;
                areaSummaryData.usageNum = usageNum;
                areaSummaryData.usageRate = usageRate.ToString();
            }
            List<WorkNoSummaryData> summaryDatas = db.Queryable<Location>()
            .LeftJoin<LocCntrRel>((l, lc) => l.S_CODE == lc.S_LOC_CODE)
            .LeftJoin<CntrItemRel>((l, lc, ci) => lc.S_CNTR_CODE == ci.S_CNTR_CODE)
            .Where((l, lc, ci) => l.N_CURRENT_NUM > 0 && ci.S_ITEM_TYPE == "成品" && l.S_AREA_CODE == model.areaCode)
            .GroupBy((l, lc, ci) => new { ci.S_WORK_NO, ci.S_WORK_NAME })
            .Select(
                    (l, lc, ci) => new WorkNoSummaryData()
                    {
                        workNo = ci.S_WORK_NO,
                        workName = ci.S_WORK_NAME,
                        amount = SqlFunc.AggregateCount(ci.S_PARTDRAW_NO)
                    }
            )
            .MergeTable()
            .OrderByDescending(a => a.amount)
            .Take(5)
            .ToList();
 
            areaSummaryData.workNoSummaryData = summaryDatas;
            return areaSummaryData;
        }
 
        /// <summary>
        /// 9.查询出入库信息汇总
        /// </summary>
        /// <returns></returns>
        public static List<DrawSummaryData> outInStackSummary(OutInStockModel model)
        {
            var db = new SqlHelper<object>().GetInstance();
            DateTime date = DateTime.Parse(model.dataDate).Date;
            var query = db.Queryable<StockRecord>().Where(a => a.S_AREA_CODE == model.areaCode && a.S_ACT_TYPE == model.actType);
            List<DrawSummaryData> outStockDatas = new List<DrawSummaryData>();
            {
                outStockDatas = query.Where("(CONVERT(date, T_OUT_TIME) = @date or CONVERT(date, T_IN_TIME) = @date)", new { date }).GroupBy(a => new { a.S_WORK_NO })
                .Where(a => a.S_WORK_NO != null)
                .Select(
                    a => new DrawSummaryData()
                    {
                        workNo = a.S_WORK_NO,
                        amount = SqlFunc.AggregateCount(a.S_CODE),
                    }
                ).ToList();
            }
            return outStockDatas;
        }
 
        /// <summary>
        /// 10.查询当前执行中和待执行任务列表汇总
        /// </summary>
        /// <returns></returns>
        public static List<ProceedTaskData> queryProceedTaskList(AreaModel model)
        {
            var db = new SqlHelper<object>().GetInstance();
            List<WCSTask> wcsTasks = db.Queryable<WCSTask>().Where(a => (a.S_START_AREA == model.areaCode || a.S_END_AREA == model.areaCode) && a.N_B_STATE < 3).OrderByDescending(a => a.T_CREATE).Take(10).ToList();
 
            List<ProceedTaskData> proceedTasks = new List<ProceedTaskData>();
            foreach (var item in wcsTasks)
            {
                ProceedTaskData proceedTask = new ProceedTaskData()
                {
                    taskType = item.S_TYPE,
                    taskStatus = item.S_B_STATE,
                };
                var cntrItemRels = ItemHelper.GetCntrItemByCntrCode(item.S_CNTR_CODE);
                if (cntrItemRels != null && cntrItemRels.Count > 0)
                {
                    proceedTask.prodName = cntrItemRels[0].S_WORK_NAME;
                    proceedTask.quantity = cntrItemRels[0].F_QTY;
                }
                proceedTasks.Add(proceedTask);
            }
            return proceedTasks;
        }
 
        /// <summary>
        /// 11.查询库位数据
        /// </summary>
        /// <returns></returns>
        public static AreaLocData queryAreaLocData(AreaModel model)
        {
            AreaLocData areaLocData = new AreaLocData();
            var db = new SqlHelper<object>().GetInstance();
            var rows = db.Queryable<Location>()
                    .Where(a => a.S_AREA_CODE == model.areaCode)
                    .GroupBy(l => new { l.N_ROW, l.N_LAYER })
                    .OrderBy(a => new { a.N_ROW, a.N_LAYER })
                            .Select(g => new
                            {
                                row = g.N_ROW,
                                layer = g.N_LAYER,
                            }).ToList();
 
            Dictionary<string, string> areaData = new Dictionary<string, string>();
            if (rows.Count > 0) 
            {
                rows.ForEach(it =>
                {
                    string locData = "";
                    List<Location> locList = db.Queryable<Location>()
                            .Where(a => a.S_AREA_CODE == model.areaCode && a.N_ROW == it.row && a.N_LAYER == it.layer)
                            .OrderBy(a => new { a.N_COL })
                            .ToList();
                    if (locList.Count > 0) 
                    {
                        foreach (var loc in locList)
                        {
                            if (loc.N_CURRENT_NUM > 0)
                            {
                                var locCntrs = LocationHelper.GetLocCntr(loc.S_CODE);
                                if (locCntrs.Count > 0)
                                {
                                    var cntrItemRels = ContainerHelper.GetCntrItemRel(locCntrs[0].S_CNTR_CODE);
                                    if (cntrItemRels.Count > 0)
                                    {
                                        string workNo = cntrItemRels[0].S_WORK_NO;
                                        if (workNo != null)
                                        {
                                            if (workNo.Contains("001"))
                                            {
                                                locData = locData + "4";
                                            }
                                            else if (workNo.Contains("002"))
                                            {
                                                locData = locData + "2";
                                            }
                                            else if (workNo.Contains("003"))
                                            {
                                                locData = locData + "3";
                                            }
                                            else 
                                            {
                                                locData = locData + "1";
                                            }
                                        }
                                        else 
                                        {
                                            locData = locData + "5";
                                        }
                                    }
                                    else
                                    {
                                        locData = locData + "5";
                                    }
                                }
                                else
                                {
                                    locData = locData + "1";
                                }
 
                            }
                            else
                            {
                                locData = locData + "0";
                            }
                        }
                        areaData.Add(it.row + "-" + it.layer, locData);
                    }
                });
                var area = db.Queryable<Area>().Where(a => a.S_CODE == model.areaCode).First();
                if (area != null) 
                {
                    areaLocData.areaCode = model.areaCode;
                    areaLocData.areaName = area.S_NAME;
                    areaLocData.areaData = areaData;
                }
            }
            return areaLocData;
        }
 
        /// <summary>
        /// 12.统计各库区库位总数、使用数量和使用率
        /// </summary>
        /// <returns></returns>
        public static List<AreaSummaryData> statisticsAllArea()
        {
            var db = new SqlHelper<object>().GetInstance();
            List<AreaSummaryData> areaSummaryDatas = db.Queryable<Location>().GroupBy(a => new { a.S_AREA_CODE })
                .Select(
                    a => new AreaSummaryData()
                    {
                        areaCode = a.S_AREA_CODE,
                        sum = SqlFunc.AggregateCount(a.S_CODE),
                    }
                ).ToList();
            if (areaSummaryDatas != null && areaSummaryDatas.Count > 0)
            {
                foreach (var item in areaSummaryDatas)
                {
                    var area = db.Queryable<Area>().Where(a => a.S_CODE == item.areaCode).First();
                    if (area == null) {
                        continue;
                    }
                    item.areaName = area.S_NAME;
                    item.usageNum = db.Queryable<Location>().Where(a => a.S_AREA_CODE == item.areaCode && a.N_CURRENT_NUM > 0).Count();
                    item.usageRate = Math.Round((double)item.usageNum / item.sum * 100, 2) + "%";
                }
            }
            return areaSummaryDatas;
        }
 
        /// <summary>
        /// 13.查询立体库库位数据
        /// </summary>
        /// <returns></returns>
        public static LTKLocationData queryLTKLocData(LocModel model)
        {
            var db = new SqlHelper<object>().GetInstance();
            string areaName = db.Queryable<Area>().Where(a => a.S_CODE == model.areaCode).First().S_NAME;
            LTKLocationData locItemData = db.Queryable<Location>()
            .LeftJoin<LocCntrRel>((l, lc) => l.S_CODE == lc.S_LOC_CODE)
            .LeftJoin<CntrItemRel>((l, lc, ci) => lc.S_CNTR_CODE == ci.S_CNTR_CODE)
            .Where((l, lc, ci) => l.S_AREA_CODE == model.areaCode && l.S_CODE == model.locCode)
            .Select(
                (l, lc, ci) =>
                new LTKLocationData()
                {
                    areaName = areaName,
                    areaCode = l.S_AREA_CODE,
                    areaType = "立体库",
                    trayCode = lc.S_CNTR_CODE,
                    locCode = l.S_CODE,
                    inStockTime = l.T_FULL_TIME,
                    itemCode = ci.S_ITEM_CODE,
                    itemName = ci.S_ITEM_NAME,
                    itemType = ci.S_ITEM_TYPE,
                    workNo = ci.S_WORK_NO,
                    workName = ci.S_WORK_NAME,
                    dePartNo = ci.S_DEPART_NO,
                    partDrawNo = ci.S_PARTDRAW_NO,
                    partName = ci.S_PART_NAME,
                    weight = ci.F_WEIGHT,
                    quantity = ci.F_QTY
                }
                )
            .First();
 
            if (locItemData != null)
            {
                TimeSpan? storagDuration = DateTime.Now - locItemData.inStockTime;
                if (storagDuration != null)
                {
                    locItemData.storagDuration = ((TimeSpan)storagDuration).Hours + "h";
                }
            }
            return locItemData;
        }
 
        /// <summary>
        /// 14.查询钢卷库库位数据
        /// </summary>
        /// <returns></returns>
        public static GJKLocationData queryGJKLocData(LocModel model)
        {
            var db = new SqlHelper<object>().GetInstance();
            string areaName = db.Queryable<Area>().Where(a => a.S_CODE == model.areaCode).First().S_NAME;
            GJKLocationData locationData = db.Queryable<Location>()
            .LeftJoin<LocCntrRel>((l, lc) => l.S_CODE == lc.S_LOC_CODE)
            .LeftJoin<CntrItemRel>((l, lc, ci) => lc.S_CNTR_CODE == ci.S_CNTR_CODE)
            .Where((l, lc, ci) => l.S_AREA_CODE == model.areaCode && l.S_CODE == model.locCode)
            .Select(
                (l, lc, ci) => new GJKLocationData
                {
                    areaName = areaName,
                    areaCode = l.S_AREA_CODE,
                    areaType = "钢卷库",
                    locCode = l.S_CODE,
                    inStockTime = l.T_FULL_TIME,
                    itemCode = ci.S_ITEM_CODE,
                    itemName = ci.S_ITEM_NAME,
                    itemType = ci.S_ITEM_TYPE,
                    workNo = ci.S_WORK_NO,
                    workName = ci.S_WORK_NAME,
                    coilNo = ci.S_COIL_NO,
                    spec = ci.S_ITEM_SPEC,
                    width = ci.S_WIDTH,
                    heatNo = ci.S_HEAT_NO,
                    shootNo = ci.S_SHOOT_NO,
                    weight = ci.F_WEIGHT,
                    quantity = ci.F_QTY,
                    initWeight = ci.F_INIT_WEIGHT
                }
                ).First();
            return locationData;
        }
 
        /// <summary>
        /// 15.查询模具库库位数据
        /// </summary>
        /// <returns></returns>
        public static MJKLocationData queryMJKLocData(LocModel model)
        {
            MJKLocationData locationData = null;
            var db = new SqlHelper<object>().GetInstance();
            string areaName = db.Queryable<Area>().Where(a => a.S_CODE == model.areaCode).First().S_NAME;
            Location location = db.Queryable<Location>().Where(a => a.S_AREA_CODE == model.areaCode && a.S_CODE == model.locCode).Includes(a => a.LocCntrRel, a => a.CntrItemRel).First();
 
            if (location != null)
            {
                locationData = new MJKLocationData()
                {
                    areaName = areaName,
                    areaCode = model.areaCode,
                    areaType = "模具库",
                    locCode = location.S_CODE,
                };
                if (location.LocCntrRel != null)
                {
                    locationData.trayCode = location.LocCntrRel.S_CNTR_CODE;
                    var mouldCntr = MouldHelper.GetMouldCntr(new MouldCntr() { S_CNTR_CODE = location.LocCntrRel.S_CNTR_CODE });
                    if (mouldCntr != null)
                    {
                        locationData.mouldNo = mouldCntr.S_MOULD_NO;
                    }
                }
            }
            return locationData;
        }
 
        /// <summary>
        /// 16.查询钢卷库区汇总数据
        /// </summary>
        /// <returns></returns>
        public static List<GJKAreaSummaryData> queryGJKAreaSummaryData(AreaListModel model)
        {
            List<GJKAreaSummaryData> gjkAreaSummaryDatas = new List<GJKAreaSummaryData>();
            var db = new SqlHelper<object>().GetInstance();
            List<string> gjAreaCodes = model.areaCodes;
            if (gjAreaCodes == null)
            {
                gjAreaCodes = Settings.getStoreAreaCodes(1, 2);
            }
            foreach (var areaCode in gjAreaCodes)
            {
                int stockNum = db.Queryable<Location>()
                    .LeftJoin<LocCntrRel>((l, lc) => l.S_CODE == lc.S_LOC_CODE)
                    .LeftJoin<CntrItemRel>((l, lc, ci) => lc.S_CNTR_CODE == ci.S_CNTR_CODE)
                    .Where((l, lc, ci) => l.S_AREA_CODE == areaCode && l.N_CURRENT_NUM > 0 ).Count();
                int intactNum = db.Queryable<Location>()
                    .LeftJoin<LocCntrRel>((l, lc) => l.S_CODE == lc.S_LOC_CODE)
                    .LeftJoin<CntrItemRel>((l, lc, ci) => lc.S_CNTR_CODE == ci.S_CNTR_CODE)
                    .Where((l, lc, ci) => l.S_AREA_CODE == areaCode && l.N_CURRENT_NUM > 0 && ci.F_WEIGHT == ci.F_INIT_WEIGHT).Count();
                
                int noIntactNum = db.Queryable<Location>()
                    .LeftJoin<LocCntrRel>((l, lc) => l.S_CODE == lc.S_LOC_CODE)
                    .LeftJoin<CntrItemRel>((l, lc, ci) => lc.S_CNTR_CODE == ci.S_CNTR_CODE)
                    .Where((l, lc, ci) => l.S_AREA_CODE == areaCode && l.N_CURRENT_NUM > 0 && ci.F_WEIGHT != ci.F_INIT_WEIGHT).Count();
               
                // 按钢卷号分组查询今日出库的最大钢卷重量(去除剪切位出库)
                var outCoilNoList = db.Queryable<StockRecord>().Where(a => a.S_AREA_CODE == areaCode && a.S_LOC_CODE != "剪切货位" && a.S_ACT_TYPE == "出库").Where("(CONVERT(date, T_OUT_TIME) = @date)", new { DateTime.Parse(model.dataDate).Date })
                                .GroupBy(a => a.S_COIL_NO)
                                .Select(group => new
                                {
                                    S_COIL_NO = group.S_COIL_NO,
                                    F_WEIGHT = SqlFunc.AggregateMax(group.F_WEIGHT)
                                }).ToList();
               
 
                // 计算今日钢卷消耗量
                float consumeWeight = 0;
                foreach (var item in outCoilNoList)
                {
                    consumeWeight = consumeWeight + item.F_WEIGHT;
                }
                GJKAreaSummaryData locationData = new GJKAreaSummaryData()
                {
                    areaCode = areaCode,
                    stockNum = stockNum,
                    intactNum = intactNum,
                    noIntactNum = noIntactNum,
                    consumeWeight = consumeWeight,
                    dataDate = model.dataDate,
                };
                gjkAreaSummaryDatas.Add(locationData);
            }
            return gjkAreaSummaryDatas;
        }
 
        /// <summary>
        /// 17.查询模具库区汇总数据
        /// </summary>
        /// <returns></returns>
        public static MJKAreaSummaryData queryMJKAreaSummaryData(AreaModel model)
        {
 
            var db = new SqlHelper<object>().GetInstance();
            int stockMouldNum = db.Queryable<Location>().Where(a => a.S_AREA_CODE == model.areaCode && a.N_CURRENT_NUM > 0).Count();
            int lendMouldNum = db.Queryable<MouldCntr>().Where(a => a.S_ABLE_FLAG == "否").Count();
            MJKAreaSummaryData areaSummaryData = new MJKAreaSummaryData()
            {
                stockMouldNum = stockMouldNum,
                lendMouldNum = lendMouldNum
            };
            return areaSummaryData;
        }
 
        /* /// <summary>
         /// 18.查询发货库区汇总数据
         /// </summary>
         /// <returns></returns>
         public static FHQAreaSummaryData queryFHQAreaSummaryData(AreaModel model)
         {
 
             var db = new SqlHelper<object>().GetInstance();
             int finishProdNum = db.Queryable<Location>().Where(a => a.S_AREA_CODE == model.areaCode && a.N_CURRENT_NUM > 0).Count();
             int lendMouldNum = db.Queryable<MouldCntr>().Where(a => a.S_ABLE_FLAG == "否").Count();
             FHQAreaSummaryData areaSummaryData = new FHQAreaSummaryData()
             {
                 finishProdNum = finishProdNum,
                 occupyLocNum = lendMouldNum,
                 leisureLocNum = lendMouldNum
             };
             return areaSummaryData;
         }*/
 
        /// <summary>
        /// 18.钢卷按规格分类统计
        /// </summary>
        /// <returns></returns>
        public static GJSpecStatisticData steelCoilSpecStatistic(AreaModel model)
        {
            GJSpecStatisticData gJSpecStatisticData = new GJSpecStatisticData();
            var db = new SqlHelper<object>().GetInstance();
            List<string> gjAreaCodes = new List<string>();
            if (model.areaCode == null)
            {
                gjAreaCodes = Settings.getStoreAreaCodes(1, 2);
            }
            else
            {
                gjAreaCodes.Add(model.areaCode);
            }
 
            int stockNum = db.Queryable<Location>().Where(a => gjAreaCodes.Contains(a.S_AREA_CODE) && a.N_CURRENT_NUM > 0).Count();
            List<GJSpecTypeStatistic> gJSpecStatisticList = db.Queryable<Location>()
            .LeftJoin<LocCntrRel>((l, lc) => l.S_CODE == lc.S_LOC_CODE)
            .LeftJoin<CntrItemRel>((l, lc, ci) => lc.S_CNTR_CODE == ci.S_CNTR_CODE)
            .Where((l, lc, ci) => gjAreaCodes.Contains(l.S_AREA_CODE) && l.N_CURRENT_NUM > 0)
            .GroupBy((l, lc, ci) => ci.S_ITEM_SPEC)
            .Select(
                (l, lc, ci) => new GJSpecTypeStatistic
                {
                    coilSpec = ci.S_ITEM_SPEC,
                    weight = SqlFunc.AggregateSum(ci.F_WEIGHT),
                    proportion = SqlFunc.AggregateCount(l.S_CODE).ToString(),
                }
            ).ToList();
            foreach (var item in gJSpecStatisticList)
            {
                item.proportion = Math.Round(double.Parse(item.proportion) / stockNum * 100, 2) + "%";
            }
            gJSpecStatisticData.dataDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            gJSpecStatisticData.coilSpecTypeList = gJSpecStatisticList;
            return gJSpecStatisticData;
        }
 
        /// <summary>
        /// 查询堆垛机状态信息
        /// </summary>
        /// <returns></returns>
        public static List<DDJStatusInfo> getDDJStatusInfos() {
            var ddjPlcInfos = Settings.devicePlcInfos.Where(a => a.deviceType == 1).ToList();
            List<DDJStatusInfo> dDJStatusInfos = new List<DDJStatusInfo>();
            foreach (var item in ddjPlcInfos)
            {
                LogHelper.Info("读取堆垛机 " + item.deviceNo, "数字孪生");
                short[] resSignal1 = S7Helper.ReadInt(item.deviceNo, 551, 0, 8);
                int resSignal2 = S7Helper.ReadDint(item.deviceNo, 551, 16);
                short[] resSignal3 = S7Helper.ReadInt(item.deviceNo, 551, 20, 3);
                LogHelper.Info("读取堆垛机 " + item.deviceNo + " 信号:  resSignal1:" + JsonConvert.SerializeObject(resSignal1) + "  resSignal2(任务号):" + JsonConvert.SerializeObject(resSignal2) + "  resSignal3:" + JsonConvert.SerializeObject(resSignal3), "数字孪生");
                if (resSignal1 != null && resSignal2 != 0 && resSignal3 != null)
                {
                    ResSignalInfo resSignalInfo = new ResSignalInfo()
                    {
                        HandShake = resSignal1[0],
                        SRM_Num = resSignal1[1],
                        Auto = resSignal1[2],
                        Alarm_Code = resSignal1[3],
                        State = resSignal1[4],
                        TaskStatus1 = resSignal1[5],
                        Loaded1 = resSignal1[6],
                        Busines1 = resSignal1[7],
                        TaskId1 = resSignal2,
                        Row = resSignal3[0],
                        Layer = resSignal3[1],
                        b_Fork1Pos = resSignal3[2],
                    };
                    LogHelper.Info("查询堆垛机信息:"+ JsonConvert.SerializeObject(resSignalInfo), "数字孪生");
 
                    // FromWcs(DB550)下发任务数据
                    short[] resSignal = S7Helper.ReadInt(item.deviceNo, 550, 8, 8);
                   
                    DDJStatusInfo info = new DDJStatusInfo()
                    {
                        deviceNo = resSignalInfo.SRM_Num.ToString(),
                        auto = resSignalInfo.Auto.ToString(),
                        status = resSignalInfo.State.ToString(),
                        alertorInfo = resSignalInfo.Alarm_Code.ToString(),
                        Loaded1 = resSignalInfo.Loaded1.ToString(),
                        Busines1 = resSignalInfo.Busines1.ToString(),
                        Row = resSignalInfo.Row.ToString(),
                        Layer = resSignalInfo.Layer.ToString(),
                        b_Fork1Pos = resSignalInfo.b_Fork1Pos.ToString(),
                        TaskId1 = resSignalInfo.TaskId1.ToString(),
                        dataDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    };
 
                    if (resSignal != null)
                    {
                        info.Source_Z_S1 = resSignal[0].ToString();
                        info.Source_X_S1 = resSignal[1].ToString();
                        info.Source_Y_S1 = resSignal[2].ToString();
                        info.Dest_Z_S1 = resSignal[4].ToString();
                        info.Dest_X_S1 = resSignal[5].ToString();
                        info.Dest_Y_S1 = resSignal[6].ToString();
                    }
 
                    dDJStatusInfos.Add(info);
                }
            }
            return dDJStatusInfos;
        }
 
        /// <summary>
        /// 查询接驳位物料信息
        /// </summary>
        /// <param name="locCode"></param>
        public static JBItemInfo queryJBItemInfo(string locCode) {
            var db = new SqlHelper<object>().GetInstance();
            var location = LocationHelper.GetLoc(locCode);
            JBItemInfo jBItemInfo = new JBItemInfo()
            {
                locCode = locCode,
                isHave = location.N_CURRENT_NUM > 0 ? "是" : "否",
            };
            var itemInfo  = db.Queryable<LocCntrRel>()
                .LeftJoin<CntrItemRel>((a, b) => a.S_CNTR_CODE == b.S_CNTR_CODE )
                .Where((a,b)=> a.S_LOC_CODE == locCode)
                .GroupBy((a, b) => new { b.S_WORK_NO })
                .Select((a,b) => new
                {
                    workNo = b.S_WORK_NO,
                    weight = SqlFunc.AggregateSum(b.F_WEIGHT),
                })
                .First();
            if (itemInfo != null) {
                jBItemInfo.workNo = itemInfo.workNo;
                jBItemInfo.weight = itemInfo.weight;
            }
            return jBItemInfo;
        }
 
        /// <summary>
        /// 查询钢卷出入库记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static List<GjOutInStockRecord> queryGjOutInStockRecord(AreaModel model) {
            var db = new SqlHelper<object>().GetInstance();
            var query = db.Queryable<StockRecord>().Where(a => a.S_AREA_CODE == model.areaCode && a.S_ACT_TYPE == model.actType);
 
            if (model.actType == "出库")
            {
                query = query.Where("(CONVERT(date, T_OUT_TIME) = @date)", new { DateTime.Parse(model.dataDate).Date });
            }
            else if (model.actType == "入库")
            {
                query = query.Where("(CONVERT(date, T_IN_TIME) = @date)", new { DateTime.Parse(model.dataDate).Date });
            }
            return query.Select(a => new GjOutInStockRecord {
                S_CODE = a.S_CODE,
                S_COIL_NO = a.S_COIL_NO,
                S_AREA_CODE = a.S_AREA_CODE,
                S_LOC_CODE = a.S_LOC_CODE,
                S_ACT_TYPE = a.S_ACT_TYPE,
                S_ITEM_CODE = a.S_ITEM_CODE,
                S_ITEM_NAME = a.S_ITEM_NAME,
                S_WORK_NO = a.S_WORK_NO,
                S_WORK_NAME = a.S_WORK_NAME,
                F_WEIGHT = a.F_WEIGHT,
                F_QTY = a.F_QTY,
                T_IN_TIME = a.T_IN_TIME,
                T_OUT_TIME = a.T_OUT_TIME,
            }).ToList();
        }
 
        /// <summary>
        /// 查询汇总的钢卷重量数据
        /// </summary>
        /// <returns></returns>
        public static ResponseResult querySummaryGJKWeightData() { 
            ResponseResult result = new ResponseResult();
            var db = new SqlHelper<object>().GetInstance();
            var gjAreaCodes = Settings.getStoreAreaCodes(1, 2);
            // 按钢卷号分组查询今日出库的最大钢卷重量(去除剪切位出库)
            var coilWeightSummary = db.Queryable<Location>()
                    .LeftJoin<LocCntrRel>((a, b) => a.S_CODE == b.S_LOC_CODE)
                    .LeftJoin<CntrItemRel>((a, b, c) => b.S_CNTR_CODE == c.S_CNTR_CODE)
                    .Where((a, b, c) => gjAreaCodes.Contains(a.S_AREA_CODE) && a.N_CURRENT_NUM > 0 && c.S_CNTR_CODE != null)
                            .GroupBy((a, b, c) => c.S_WORK_NO)
                            .Select((a, b, c) => new
                            {
                                workNo = c.S_WORK_NO.Trim(),
                                weight = SqlFunc.AggregateMax(c.F_WEIGHT)
                            }).ToList();
            result.result= coilWeightSummary;
            return result;
        }
 
        public class GjOutInStockRecord
        {
            public string S_CODE { get; set; } //记录编码
            public string S_COIL_NO { get; set; } // 钢卷号
            public string S_AREA_CODE { get; set; } //库区编码
            public string S_LOC_CODE { get; set; } //库位编码
            public string S_ACT_TYPE { get; set; } //行为类型 入库、出库
            public string S_ITEM_CODE { get; set; } //物料编码
            public string S_ITEM_NAME { get; set; } //物料名称
            public string S_WORK_NO { get; set; } //工作号
            public string S_WORK_NAME { get; set; } //工作名称
            public float F_WEIGHT { get; set; } //重量
            public float F_QTY { get; set; } //数量
            public DateTime? T_IN_TIME { get; set; } //入库时间
            public DateTime? T_OUT_TIME { get; set; } // 出库时间
        }
 
 
        public class JBItemInfo {
            public string locCode { get; set; }  // 接驳位编码
            public string isHave { get; set; }  // 是否有货 是/否
            public string workNo { get; set; }  // 工作号
            public float weight { get; set; }  // 货物重量
 
        }
 
        public class DDJStatusInfo
        {
            public string deviceNo { get; set; }  // 设备号
            public string auto { get; set; }  // 堆垛机当前模式指示  1 联机自动;2手动;3半自动;4维修;
            public string status { get; set; }  //  1:状态正常,允许任务下发
            public string alertorInfo { get; set; }  // 堆垛机报警代码 0:默认;1:正常 ;其他数字故障信息
            public string Loaded1 { get; set; }  // 载货台有无货 1:有货;0:无货
            public string Busines1 { get; set; }   // 工位1特殊业务状态反馈 [1:满入,2.空取,3:放货阻塞,4:取货阻塞]
            public string Row { get; set; }   // 堆垛机当前列(行走方向)
            public string Layer { get; set; }   // 堆垛机当前层(升降方向)
            public string b_Fork1Pos { get; set; }   // 堆垛机货叉1位置 [1:左远,2.左近,3:居中,4:右近,5:右远]
            public string TaskId1 { get; set; }      // 任务号
            public string Source_Z_S1 { get; set; }  // 取货排 1/2/3/4
            public string Source_X_S1 { get; set; }  // 取货列
            public string Source_Y_S1 { get; set; }  // 取货层
            public string Dest_Z_S1 { get; set; }  // 放货排
            public string Dest_X_S1 { get; set; }  // 放货列
            public string Dest_Y_S1 { get; set; } // 放货层
            public string dataDate { get; set; }  // 数据日期
        }
 
 
        public class GJSpecStatisticData
        {
            public string dataDate { get; set; }
            public List<GJSpecTypeStatistic> coilSpecTypeList { get; set; }
            public class GJSpecTypeStatistic
            {
                public string coilSpec { get; set; }
                public float weight { get; set; }
                public string proportion { get; set; }
            }
        }
        public class FHQAreaSummaryData
        {
            public int finishProdNum { get; set; }
            public int occupyLocNum { get; set; }
            public int leisureLocNum { get; set; }
        }
 
        public class MJKAreaSummaryData
        {
            public int stockMouldNum { get; set; }
            public int lendMouldNum { get; set; }
        }
 
        public class GJKAreaSummaryData
        {
            public string areaCode { get; set; } // 库区编码
            public int stockNum { get; set; } // 在库数量
            public int intactNum { get; set; } // 完整数量
            public int noIntactNum { get; set; } // 非完整数量
            public float consumeWeight { get; set; } // 今日钢卷消耗量
            public string dataDate { get; set; }
 
        }
 
        /// <summary>
        /// 模具库货位数据
        /// </summary>
        public class MJKLocationData
        {
            public string areaName { get; set; }
            public string areaCode { get; set; }
            public string areaType { get; set; }
            public string locCode { get; set; }
            public string trayCode { get; set; }
            public string mouldNo { get; set; }
 
        }
 
        /// <summary>
        /// 钢卷库货位数据
        /// </summary>
        public class GJKLocationData
        {
            public string areaName { get; set; }
            public string areaCode { get; set; }
            public string areaType { get; set; }
            public string locCode { get; set; }
            public DateTime? inStockTime { get; set; }
            public string itemCode { get; set; }
            public string itemName { get; set; }
            public string itemType { get; set; }
            public string workNo { get; set; }
            public string workName { get; set; }
            public string coilNo { get; set; }
            public string spec { get; set; }
            public string width { get; set; }
            public string heatNo { get; set; }
            public string shootNo { get; set; }
            public float weight { get; set; }
            public float quantity { get; set; }
            public float initWeight { get; set; }
        }
 
        /// <summary>
        /// 立体库货位数据
        /// </summary>
        public class LTKLocationData
        {
            public string areaName { get; set; }
            public string areaCode { get; set; }
            public string areaType { get; set; }
            public string trayCode { get; set; }
            public string locCode { get; set; }
            public DateTime? inStockTime { get; set; }
            public string itemCode { get; set; }
            public string itemName { get; set; }
            public string itemType { get; set; }
            public string workNo { get; set; }
            public string workName { get; set; }
            public string dePartNo { get; set; }
            public string partDrawNo { get; set; }
            public string partName { get; set; }
            public float weight { get; set; }
            public float quantity { get; set; }
            public string storagDuration { get; set; }
        }
 
        public class LocModel
        {
            public string areaCode { get; set; }
            public string locCode { get; set; }
        }
 
 
        public class AreaLocData
        {
            public string areaCode { get; set; }
            public string areaName { get; set; }
            public object areaData { get; set; }
        }
 
        public class ProceedTaskData
        {
            public string taskType { get; set; }
            public string prodName { get; set; }
            public float quantity { get; set; }
            public string taskStatus { get; set; }
        }
 
        public class OutInStockModel
        {
            public string areaCode { get; set; }
            public string dataDate { get; set; }
            public string actType { get; set; }
        }
 
        public class AreaSummaryData
        {
            public string areaCode { get; set; }
            public string areaName { get; set; }
            public int sum { get; set; }
            public int usageNum { get; set; }
            public string usageRate { get; set; }
            public List<WorkNoSummaryData> workNoSummaryData { get; set; }
 
        }
 
 
        public class WorkNoOutStockData
        {
            public string workNo { get; set; }
            public string workName { get; set; }
            public string dePartNo { get; set; }
            public float quantity { get; set; }
            public float weight { get; set; }
            public float accuQuantity { get; set; } // 累计数量
        }
 
        public class DateModel
        {
            public string dataDate { get; set; } // 数据日期(yyyy-MM-dd)
        }
 
        public class ActModel
        {
            public string actType { get; set; } // 类型 入库 出库
            public string dataDate { get; set; } // 数据日期(yyyy-MM-dd)
        }
 
        public class StorageTimeSummaryData
        {
            public int amountA { get; set; } // 在库时长7日内数量
            public string proportionA { get; set; } // 在库时长7日内占比
            public int amountB { get; set; } // 在库时长8-15日数量
            public string proportionB { get; set; } // 在库时长8-15日占比
            public int amountC { get; set; } // 在库时长16-30日数量
            public string proportionC { get; set; } // 在库时长16-30日占比
            public int amountD { get; set; } // 在库时长30-90日数量
            public string proportionD { get; set; } // 在库时长30-90日占比
            public int amountE { get; set; } // 在库时长91日以上数量
            public string proportionE { get; set; } // 在库时长91日以上占比
            public int sum { get; set; } // 总库位数
            public int usageNum { get; set; } // 使用数
            public string usageRate { get; set; } // 使用率
        }
 
        public class WorkNoSummaryData
        {
            public string areaCode { get; set; }
            public string areaName { get; set; }
            public string workNo { get; set; }
            public string workName { get; set; }
            public string type { get; set; }
            public float amount { get; set; }
            public string proportion { get; set; }
        }
 
        public class DrawSummaryData
        {
            public string partDrawNo { get; set; }
            public string partName { get; set; }
            public string workNo { get; set; }
            public float amount { get; set; }
            public string proportion { get; set; }
        }
 
        public class LTKSummaryData
        {
            public int sum { get; set; }
            public int usageNum { get; set; }
            public string usageRate { get; set; }
            public int finishProdNum { get; set; }
            public string finishProdProp { get; set; }
            public int semiProdNum { get; set; }
            public string semiProdProp { get; set; }
        }
 
 
        public class AreaModel
        {
            public string areaCode { get; set; }
            public string dataDate { get; set; }// 数据日期(yyyy-MM-dd)
            public string actType { get; set; }
        }
 
        public class AreaListModel
        {
            public List<string> areaCodes { get; set; }
            public string dataDate { get; set; } // 数据日期(yyyy-MM-dd)
        }
 
        public class ItemTypeModel
        {
            public string itemType { get; set; }
        }
 
        /// <summary>
        /// 成品物料信息
        /// </summary>
        public class BaseItemInfo
        {
            public string areaCode { get; set; }
            public string trayCode { get; set; }
            public string locCode { get; set; }
            public string workNo { get; set; }
            public string workName { get; set; }
            public string dePartNo { get; set; }
            public string partDrawNo { get; set; }
            public string partName { get; set; }
            public string itemCode { get; set; }
            public string itemName { get; set; }
            public float weight { get; set; }
            public float quantity { get; set; }
        }
    }
}