zhao
2021-07-19 8347f2fbddbd25369359dcb2da1233ac48a19fdc
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
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
 
using HH.WMS.Common;
using HH.WMS.Common.Algorithm;
using HH.WMS.Common.Algorithm.Out;
using HH.WMS.DAL.Algorithm;
using HH.WMS.DAL.Basic;
using HH.WMS.Entitys.Algorithm;
using HH.WMS.Entitys.Basic;
using HH.WMS.Entitys.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HH.WMS.DAL.InStock;
using HH.WMS.BLL.Common;
using Newtonsoft.Json;
using HH.WMS.Entitys.Dto;
using HH.WMS.Entitys;
 
namespace HH.WMS.BLL.Algorithm
{
    /// <summary>
    /// 出库算法BLL层
    /// </summary>
    public class Out_AlgorBLL : BaseBLL
    {
        /**************计算  返回物料所在货位的算法开始/**************/
 
        #region 非作业区  出库算法
        /// <summary>
        /// 返回物料 先进先出、质保期将近的先出 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        //public List<OutResultEntity> Out(OutAlgorEnitty model)
        //{
        //    List<OutResultEntity> lstResultEntity = new List<OutResultEntity>();
        //    Log.AlgorInfo("Out_AlgorBLL—Out", "出库算法开始,传入参数:" + JsonConvert.SerializeObject(model));
        //    //获取项目策略
        //    List<STRATEGYALGOREntity> lstStrategy = new List<STRATEGYALGOREntity>();
        //    HH.WMS.Entitys.Basic.AutoBomStockAreaEntity stockEntity = CreateDAL<TN_AB_B_STOCK_AREADAL>().GetStockAreaEntity(model.lstQueryItem[0].areaCode);
        //    lstStrategy = CreateDAL<TN_WM_B_STRATEGYDAL>().GetStrateListByAreaOrStock(stockEntity.CN_S_STOCK_CODE, "", "");
        //    List<string> lstStrate = lstStrategy.Select(o => o.CN_S_NAME).ToList();
        //    //if (lstStrate.Contains("JXXAlgor"))
        //    //{
        //    lstResultEntity = OutJxxFlat(model);
        //    //}
        //    //if (lstStrate.Contains("SszyAlgor"))
        //    //{
        //    //    lstResultEntity = OutSszyFlat(model);
        //    //}
        //    return lstResultEntity;
        //}
        #endregion
 
        #region 非作业区  平库立库出库算法,根据物料的数量出库
        ///// <summary>
        ///// 非作业区  平库立库出库算法,根据物料的数量出库
        ///// </summary>
        ///// <param name="model"></param>
        ///// <returns></returns>
        //public List<OutResultEntity> OutJxxFlat(OutAlgorEnitty model)
        //{
        //    Log.AlgorInfo("Out_AlgorBLL—Out", "OutJxxFlat");
        //    List<OutResultEntity> lstResultEntity = new List<OutResultEntity>();
        //    //将传递过来的参数按货主、状态、等查询条件分组
        //    List<Out_AreaItemClass> lstAreaItemGroup = GroupAreaCode(model.lstQueryItem);
        //    foreach (Out_AreaItemClass areaItemEntity in lstAreaItemGroup)
        //    {
        //        Log.AlgorInfo("Out_AlgorBLL—Out", "循环处理lstAreaItemGroup数据:" + JsonConvert.SerializeObject(areaItemEntity));
        //        string areaCode = areaItemEntity.areaCode;
        //        string itemCodes = string.Join(",", areaItemEntity.lstItem.Select(o => o.itemCode)).Replace(",", "','");
        //        //组合查询条件
        //        string strWhere = GetPropWhereStr(itemCodes, areaItemEntity.itemState, areaItemEntity.batchCode, areaItemEntity.prodBatchCode, areaItemEntity.ownerName);
 
        //        //查询该库区配置的出库策略及优先级(生产日期早先出、先进先出、出库整托优先)
        //        List<STRATEGYALGOREntity> lstStrategy = new List<STRATEGYALGOREntity>();
        //        lstStrategy = CreateDAL<TN_WM_B_STRATEGYDAL>().GetStrateListByAreaOrStock("", areaCode, "出库");
        //        List<string> lstStrate = lstStrategy.Select(o => o.CN_S_NAME).ToList();
 
        //        //获得库区中所有匹配的物料
        //        List<itemQty> v = CreateDAL<Out_AlgorDAL>().GetItemQtyJxx(strWhere, areaCode);
        //        Log.AlgorInfo("Out_AlgorBLL—Out", "List<itemQty>数据:" + JsonConvert.SerializeObject(v));
        //        //循环处理批分分配量
        //        List<itemQty> lstAllocQty = v.Select(g => new itemQty()
        //        {
        //            trayCode = g.trayCode,
        //            CN_F_ALLOC_QTY = g.CN_F_ALLOC_QTY,
        //            CN_S_OWNER = g.CN_S_OWNER,
        //            CN_S_ITEM_STATE = g.CN_S_ITEM_STATE,
        //            itemCode = g.itemCode,
        //        }).Distinct().ToList();
 
        //        int tmpAllocQty = 0;
        //        foreach (itemQty item in v)
        //        {
        //            //循环托盘物料详细数据,按生产批次早、入库批次早批分分配量
        //            tmpAllocQty = lstAllocQty.Where(o => (o.trayCode == item.trayCode && o.CN_S_OWNER == item.CN_S_OWNER && o.CN_S_ITEM_STATE == item.CN_S_ITEM_STATE && o.itemCode == item.itemCode)).FirstOrDefault().CN_F_ALLOC_QTY;
        //            if (tmpAllocQty > 0)
        //            {
        //                item.Qty = item.Qty - tmpAllocQty;
        //                lstAllocQty.Where(o => (o.trayCode == item.trayCode && o.CN_S_OWNER == item.CN_S_OWNER && o.CN_S_ITEM_STATE == item.CN_S_ITEM_STATE && o.itemCode == item.itemCode)).FirstOrDefault().CN_F_ALLOC_QTY = item.Qty - tmpAllocQty;
        //            }
        //        }
        //        v.RemoveAll(o => o.Qty <= 0);
 
        //        Log.AlgorInfo("Out_AlgorBLL—Out", "获得库区中所有匹配的物料:lstItemQty:" + JsonConvert.SerializeObject(v));
 
        //        List<string> lstCodes = v.GroupBy(a => a.locationCode).Select(o => o.Key).ToList();
        //        //到mongodb中查询每个货位的基本信息排除货位被报废的数据
        //        List<AutoBomLocationAbbreEntity> lstFalseLocation = CreateDAL<TN_WMS_LOCATIONDAL>().GetScrapLocationCode(lstCodes);
        //        lstCodes = lstFalseLocation.Select(a => a.CN_S_LOCATION_CODE).ToList();
        //        v.RemoveAll(o => lstCodes.Contains(o.locationCode));
 
        //        //循环前端物料列表
        //        foreach (itemInClass item in areaItemEntity.lstItem)
        //        {
        //            //定义返回值
        //            OutResultEntity resultEntity = new OutResultEntity();
        //            //验证过的物料货位集合
        //            List<itemQty> lstTrueItem = new List<itemQty>();
        //            //判断是否合法
        //            MsgEntity msgEntity = IsPassItem(item, v, out lstTrueItem);
        //            if (!msgEntity.Success)//不合法
        //            {
        //                resultEntity.Success = false;
        //                resultEntity.Msg = msgEntity.Msg;
        //                resultEntity.itemCode = item.itemCode;
        //            }
        //            else //合法
        //            {
        //                resultEntity.Success = true;
        //                resultEntity.itemCode = item.itemCode;
        //                /*返回时加入条件*/
        //                resultEntity.areaCode = areaItemEntity.areaCode;
        //                resultEntity.batchCode = areaItemEntity.batchCode;
        //                resultEntity.prodBatchCode = areaItemEntity.prodBatchCode;
        //                resultEntity.itemState = areaItemEntity.itemState;
        //                resultEntity.ownerName = areaItemEntity.ownerName;
        //                /*条件结束*/
        //                List<itemTrayLocation> lstResult = GetJXXTrayLocation(lstStrate, item, model.lstDevice, lstTrueItem, model.lockLocation);
        //                resultEntity.itemLocations = lstResult.GroupBy(a => new
        //                {
        //                    a.agvLocationCode,
        //                    a.locationCode,
        //                    a.trayCode,
        //                    a.trayGrid
        //                }).Select(g => new itemTrayLocation()
        //               {
        //                   itemQty = g.Sum(s => s.itemQty),
        //                   agvLocationCode = g.Key.agvLocationCode,
        //                   locationCode = g.Key.locationCode,
        //                   trayCode = g.Key.trayCode,
        //                   trayGrid = g.Key.trayGrid
        //               }).ToList();
        //            }
        //            lstResultEntity.Add(resultEntity);
        //        }
        //    }
        //    return lstResultEntity;
        //}
 
        #endregion
 
 
 
        public OutResultEntityNew OutNew(OutAlgorEnitty model)
        {
            OutResultEntityNew resultE = new OutResultEntityNew();
            Log.AlgorInfo("Out_AlgorBLL—Out", "出库算法开始,传入参数:" + JsonConvert.SerializeObject(model));
 
            resultE = OutCommonFlat(model);
 
            return resultE;
        }
        #region 非作业区  平库立库出库算法,根据物料的数量出库
 
 
        /// <summary>
        /// 非作业区  平库立库出库算法,根据物料的数量出库
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public OutResultEntityNew OutCommonFlat(OutAlgorEnitty model)
        {
            OutResultEntityNew resultEntity = new OutResultEntityNew();
            resultEntity.Success = true;
            resultEntity.Msg = "";
            try
            {
                #region 定义变量
                Log.AlgorInfo("Out_AlgorBLL—Out", "OutCommonFlat");
                StringBuilder sbWhere = new StringBuilder();
                List<itemQty> lstAllIQ = new List<itemQty>();
                List<trayOutItem> lstTray = new List<trayOutItem>();
                resultEntity.lstItemNotEnough = new List<itemQueryClass>();
                // itemQty tmpItemQty = null;
                #endregion
 
                #region 查询该库区配置的出库策略及优先级
                List<STRATEGYALGOREntity> lstStrategy = new List<STRATEGYALGOREntity>();
                lstStrategy = CreateDAL<TN_WM_B_STRATEGYDAL>().GetStrateListByAreaOrStock("", model.lstQueryItem[0].areaCode, "出库");
                List<string> lstStrate = lstStrategy.OrderByDescending(a => a.CN_N_PRIORITY).Select(o => o.CN_S_CODE).ToList();
                #endregion
 
                #region 缓存记录托盘、货主、物料、状态对应的待分配量,循环itemQueryClass时根据结果实时更新
                //List<itemQty> lstWaitAlloc = new List<itemQty>();
                //List<itemQty> lstForAlloc = new List<itemQty>();
                //foreach (itemQueryClass itemQuery in model.lstQueryItem)
                //{
                //    lstForAlloc = CreateDAL<Out_AlgorDAL>().GetOutItemAllocQty(itemQuery.itemCode, itemQuery.itemState, itemQuery.ownerName, itemQuery.areaCode);
                //    if (lstForAlloc != null && lstForAlloc.Count > 0)
                //    {
                //        lstWaitAlloc.AddRange(lstForAlloc);
                //    }
                //}
                //lstWaitAlloc = lstWaitAlloc.Distinct().ToList();
                //if (lstWaitAlloc.Count == 0)
                //{
                //    //如果从库区中查找不到符合出库条件的托盘,则将传入物料记录到物料不足列表中反馈
                //    resultEntity.lstItemNotEnough.AddRange(model.lstQueryItem);
                //    return resultEntity;
                //}
                #endregion
 
                foreach (itemQueryClass itemQuery in model.lstQueryItem)
                {
                    Log.AlgorInfo("Out_AlgorBLL—Out", "循环处理lstQueryItem数据:" + JsonConvert.SerializeObject(itemQuery));
 
                    #region 循环处理批分分配量
                    //获得库区中所有匹配的待出库的物料库存数据
                    List<itemQty> lstIQ = CreateDAL<Out_AlgorDAL>().GetOutItemQty(itemQuery.itemCode, itemQuery.itemState, itemQuery.prodBatchCode, itemQuery.batchCode, itemQuery.ownerName, itemQuery.areaCode);
                    if (lstIQ.Count == 0)
                    {
                        resultEntity.lstItemNotEnough.Add(itemQuery);
                        continue;
                    }
                    //Log.AlgorInfo("Out_AlgorBLL—Out", "批分前库区中所有匹配的物料数据:" + JsonConvert.SerializeObject(lstIQ));
                    ////过滤lstWaitAlloc中不在lstIQ中的托盘,因为这些托盘的待分配量不需要处理
                    //List<string> lstTrayCode = lstIQ.Select(o => o.trayCode).Distinct().ToList();
                    //List<itemQty> lstAllocQty = lstWaitAlloc.Where(o => o.CN_F_ALLOC_QTY > 0 && lstTrayCode.Contains(o.trayCode) && o.itemCode == itemQuery.itemCode && o.CN_S_OWNER == itemQuery.ownerName && o.CN_S_ITEM_STATE == itemQuery.itemState).Select(g => new itemQty()
                    //{
                    //    trayCode = g.trayCode,
                    //    CN_F_ALLOC_QTY = g.CN_F_ALLOC_QTY,
                    //    CN_S_OWNER = g.CN_S_OWNER,
                    //    CN_S_ITEM_STATE = g.CN_S_ITEM_STATE,
                    //    itemCode = g.itemCode
                    //}).Distinct().ToList();
 
                    //decimal alloc_qty = 0;
                    //foreach (itemQty iQty in lstAllocQty)
                    //{
                    //    alloc_qty = iQty.CN_F_ALLOC_QTY;
                    //    //递归批分处理货位托盘中的待分配量,去除lstIQ中已经被批分的数据(已被其他分拣单占用的数据)
                    //    DivideAllocQty(ref lstIQ, iQty, ref alloc_qty, lstStrate);
                    //}
                    //Log.AlgorInfo("Out_AlgorBLL—Out", "批分后库区中所有匹配的物料数据:" + JsonConvert.SerializeObject(lstIQ));
                    #endregion
 
                    #region 到mongodb中查询每个货位的基本信息,排除货位被报废的数据
                    List<string> lstCodes = lstIQ.GroupBy(a => a.locationCode).Select(o => o.Key).ToList();
                    List<AutoBomLocationAbbreEntity> lstFalseLocation = CreateDAL<TN_WMS_LOCATIONDAL>().GetScrapLocationCode(lstCodes);
                    lstCodes = lstFalseLocation.Select(a => a.CN_S_LOCATION_CODE).ToList();
                    //此时的lstIQ是去除批分量、货位无故障的可出库物料数据
                    lstIQ.RemoveAll(o => lstCodes.Contains(o.locationCode));
                    #endregion
 
                    #region 处理不满足出库数量的物料并计算可出库货位
 
                    //按照库区等查询条件分组
                    var lstItemGroup = lstIQ.GroupBy(a => new
                    {
                        a.itemCode
                    }).Select(g => (new
                    {
                        itemCode = g.Key.itemCode,
                        itemQty = g.Sum(p => p.Qty)
                    }));
                    var equalItem = lstItemGroup.Where(o => o.itemCode == itemQuery.itemCode).FirstOrDefault();
 
                    if (equalItem == null || equalItem.itemQty < itemQuery.itemQty)
                    {
                        //记录不满足出库条件的物料
                        itemQuery.itemQty = itemQuery.itemQty - equalItem.itemQty;
                        resultEntity.lstItemNotEnough.Add(itemQuery);
                    }
                    else
                    {
                        Log.AlgorInfo("Out_AlgorBLL—Out", "计算具体出库货位开始:");
                        decimal needOutQty = itemQuery.itemQty;
                        lstTray.AddRange(CalculateOutLocation(itemQuery, ref needOutQty, ref lstIQ, lstStrate));
                        //增加缓存批分量
                        //foreach (trayOutItem toItem in lstTray)
                        //{
                        //    tmpItemQty = lstWaitAlloc.Where(o => o.trayCode == toItem.trayCode && o.itemCode == toItem.itemCode && o.CN_S_OWNER == toItem.ownerName && o.CN_S_ITEM_STATE == toItem.itemState).FirstOrDefault();
                        //    if (tmpItemQty != null)
                        //    {
                        //        tmpItemQty.CN_F_ALLOC_QTY = tmpItemQty.CN_F_ALLOC_QTY + toItem.itemQty;
                        //    }
                        //}
                    }
 
                    #endregion
                }
                resultEntity.itemLocations = lstTray;
            }
            catch (Exception ex)
            {
                resultEntity.Success = false;
                resultEntity.Msg = ex.Message;
            }
            Log.AlgorInfo("Out_AlgorBLL—Out", "返回计算结果:" + JsonConvert.SerializeObject(resultEntity));
            return resultEntity;
 
        }
 
        public List<trayOutItem> CalculateOutLocation(itemQueryClass itemQC, ref decimal needOutQty, ref  List<itemQty> lstAllIQ, List<string> lstStrate)
        {
            bool isNoDivide = false;
            bool forceDivide = false;
            List<itemQty> lstFilterItem = CalculateItemByStegy(needOutQty, lstAllIQ, lstStrate, out isNoDivide, out forceDivide);
            List<trayOutItem> lstTray = new List<trayOutItem>();
            decimal tmpQty = 0;
            List<string> lstNeedRemove = new List<string>();
            if (!isNoDivide)
            {
                #region 可拆包,优先整托
                foreach (itemQty item in lstFilterItem)
                {
                    tmpQty = needOutQty - item.Qty;
                    trayOutItem trayItem = new trayOutItem();
                    trayItem.itemCode = item.itemCode;
                    trayItem.itemState = item.CN_S_ITEM_STATE;
                    trayItem.ownerName = item.CN_S_OWNER;
                    trayItem.batchCode = item.lotNo;
                    trayItem.prodBatchCode = item.prodBatchCode;
                    trayItem.trayCode = item.trayCode;
                    trayItem.trayGrid = item.trayGrid;
                    trayItem.locationCode = item.locationCode;
                    trayItem.areaCode = itemQC.areaCode;
                    trayItem.CN_S_TIMESTAMP = item.CN_S_TIMESTAMP;
                    trayItem.packUnit = item.packUnit;
                    trayItem.packQty = item.PAKQty;
                    if (tmpQty < 0)
                    {
                        //降低该记录的可出库量,并跳出循环
                        lstAllIQ.Find(O => O.ID == item.ID).Qty = item.Qty - needOutQty;
                        trayItem.itemQty = needOutQty;
                        trayItem.needSort = "Y";
                        needOutQty = tmpQty;
                        lstTray.Add(trayItem);
                        break;
                    }
                    else
                    {
                        //删除该记录
                        needOutQty = tmpQty;
                        lstNeedRemove.Add(item.ID);
                        trayItem.itemQty = item.Qty;
                        trayItem.needSort = "N";
                        lstTray.Add(trayItem);
                    }
                    if (needOutQty <= 0)
                    {
                        //待出库量为0则结束循环
                        break;
                    }
                }
                #endregion
            }
            else
            {
                #region 不可拆包,优先拣包装数量较大的
                foreach (itemQty item in lstFilterItem)
                {
                    Log.AlgorInfo("Out_AlgorBLL—Out", "循环lstFilterItem物料数据:" + JsonConvert.SerializeObject(item));
                    trayOutItem trayItem = new trayOutItem();
                    trayItem.itemCode = item.itemCode;
                    trayItem.itemState = item.CN_S_ITEM_STATE;
                    trayItem.ownerName = item.CN_S_OWNER;
                    trayItem.batchCode = item.lotNo;
                    trayItem.prodBatchCode = item.prodBatchCode;
                    trayItem.trayCode = item.trayCode;
                    trayItem.trayGrid = item.trayGrid;
                    trayItem.locationCode = item.locationCode;
                    trayItem.areaCode = itemQC.areaCode;
                    trayItem.CN_S_TIMESTAMP = item.CN_S_TIMESTAMP;
                    trayItem.packUnit = item.packUnit;
                    trayItem.packQty = item.PAKQty;
                    Log.AlgorInfo("Out_AlgorBLL—Out", "needOutQty < item.Qty:" + needOutQty.ToString() + "," + item.Qty.ToString());
                    if (needOutQty < item.Qty)
                    {
                        Log.AlgorInfo("Out_AlgorBLL—Out", "needOutQty < item.PAKQty:" + needOutQty.ToString() + "," + item.PAKQty.ToString());
                        //待出库量比该托盘中物料数量小
                        if (needOutQty >= item.PAKQty)
                        {
                            trayItem.itemQty = needOutQty - needOutQty % item.PAKQty;
                            if (item.Qty - trayItem.itemQty >= 0)
                            {
                                //降低该记录的可出库量,
                                lstAllIQ.Find(O => O.ID == item.ID).Qty = item.Qty - trayItem.itemQty;
                            }
                            else
                            {
                                lstNeedRemove.Add(item.ID);
                            }
                            needOutQty = needOutQty % item.PAKQty;
                            lstTray.Add(trayItem);
                            Log.AlgorInfo("Out_AlgorBLL—Out", "needOutQty >= item.PAKQty,trayItem:" + JsonConvert.SerializeObject(trayItem));
                        }
                        else
                        {
                            Log.AlgorInfo("Out_AlgorBLL—Out", "forceDivide:" + forceDivide.ToString());
                            if (forceDivide)
                            {
                                //库区中无不拆包可出库数据,强制拆包
                                trayItem.itemQty = needOutQty;
                                lstAllIQ.Find(O => O.ID == item.ID).Qty = item.Qty - needOutQty;
                                needOutQty = 0;
                                lstTray.Add(trayItem);
                                Log.AlgorInfo("Out_AlgorBLL—Out", "forceDivide,trayItem:" + JsonConvert.SerializeObject(trayItem));
                            }
                            else
                            {
                                //待出库量比该托盘中该单位的包装数量小,则跳出循环,寻找比该包装规格小的物料数据  
                                break;
                            }
                        }
                    }
                    else
                    {
                        //待出库量比该托盘中物料数量大,将该托盘中物料全出
                        trayItem.itemQty = item.Qty;
                        lstNeedRemove.Add(item.ID);
                        needOutQty = needOutQty - item.Qty;
                        lstTray.Add(trayItem);
                        Log.AlgorInfo("Out_AlgorBLL—Out", "待出库量比该托盘中物料数量大,trayItem:" + JsonConvert.SerializeObject(trayItem));
                    }
 
                    if (needOutQty < item.PAKQty)
                    {
                        //待出库量小于规格数量则结束循环
                        break;
                    }
                }
                #endregion
            }
            lstAllIQ.RemoveAll(o => lstNeedRemove.Contains(o.ID));
            if (needOutQty > 0 && lstAllIQ.Count > 0)
            {
                //如果未分配完则递归循环
                lstTray.AddRange(CalculateOutLocation(itemQC, ref needOutQty, ref lstAllIQ, lstStrate));
            }
            return lstTray;
 
        }
 
        /// <summary>
        /// 将货位中的物料数据按照策略排序
        /// </summary>
        /// <param name="lstIQ"></param>
        /// <param name="lstStrate"></param>
        /// <returns></returns>
        public List<itemQty> CalculateItemByStegy(decimal needOutQty, List<itemQty> lstIQ, List<string> lstStrate, out bool isNoDivide, out bool forceDivide)
        {
            List<itemQty> lstFilterItem = lstIQ;
            List<itemQty> lstEmptyPBC;
            List<itemQty> lstTpm;
            List<itemQty> lstTpm1;
            bool IsIntegerOut = false;
            isNoDivide = false;
            forceDivide = false;
            foreach (string stegy in lstStrate)
            {
                //逐个策略进行计算
                switch (stegy)
                {
                    case "ProductTimeOutFirst":
                        lstFilterItem = lstFilterItem.OrderBy(o => o.productDate).ToList();
                        lstFilterItem = lstFilterItem.Where(o => o.productDate == lstFilterItem.FirstOrDefault().productDate).ToList();
                        break;
                    case "FirstWarrantFirstOut":
                        lstEmptyPBC = lstFilterItem.Where(o => string.IsNullOrEmpty(o.prodBatchCode)).ToList();
                        lstFilterItem = lstFilterItem.Where(o => !string.IsNullOrEmpty(o.prodBatchCode)).OrderBy(o => o.prodBatchCode).ToList();
                        lstFilterItem.AddRange(lstEmptyPBC);
                        lstFilterItem = lstFilterItem.Where(o => o.prodBatchCode == lstFilterItem.FirstOrDefault().prodBatchCode).ToList();
                        break;
                    case "IntegerTrayFirstOut":
                        IsIntegerOut = true;
                        lstTpm = lstFilterItem.GroupBy(a => new { a.trayCode }).Select(g => new itemQty()
                        {
                            Qty = g.Sum(s => s.Qty),
                            trayCode = g.Key.trayCode
                        }).ToList();
                        lstTpm = lstTpm.OrderByDescending(o => o.Qty).ToList();
                        lstTpm1 = lstTpm.Where(o => o.Qty >= needOutQty).ToList();
                        if (lstTpm1 == null || lstTpm1.Count == 0)
                        {
                            //该托盘中总数量不满足出库需求,则找最大数量的托盘
                            lstFilterItem = lstFilterItem.Where(o => o.trayCode == lstTpm.FirstOrDefault().trayCode).ToList();
                        }
                        else
                        {
                            //该托盘中总数量满足出库需求
                            lstTpm1 = lstTpm1.OrderBy(o => o.Qty).ToList();
                            lstFilterItem = lstFilterItem.Where(o => o.trayCode == lstTpm1.FirstOrDefault().trayCode).ToList();
                        }
                        break;
                    case "NoDivideFirstOut":
                        IsIntegerOut = true;
                        isNoDivide = true;
                        lstFilterItem = lstFilterItem.OrderByDescending(o => o.PAKQty).ToList();
                        Log.AlgorInfo("Out_AlgorBLL—Out", "NoDivideFirstOut前lstFilterItem物料数据:" + JsonConvert.SerializeObject(lstFilterItem));
                        lstTpm1 = lstFilterItem.Where(o => o.PAKQty <= needOutQty).ToList();
                        if (lstTpm1.Count == 0)
                        {
                            //货位中物料匹配数据中无包装单位比待出库数量小的数据,只有拆包
                            forceDivide = true;
                            lstTpm1 = lstFilterItem.OrderBy(o => o.PAKQty).ToList();
                            //寻找最接近出库数量的包装拆包
                            lstFilterItem = lstFilterItem.Where(o => o.Qty == lstTpm1.FirstOrDefault().Qty).ToList();
                        }
                        else
                        {
                            //货位中物料匹配数据中存在包装单位比带出库数量小的数据,先计算大包装的
                            lstFilterItem = lstFilterItem.Where(o => o.PAKQty == lstTpm1.FirstOrDefault().PAKQty).ToList();
                        }
                        Log.AlgorInfo("Out_AlgorBLL—Out", "NoDivideFirstOut后lstFilterItem物料数据:" + JsonConvert.SerializeObject(lstFilterItem));
                        break;
                }
                if (IsIntegerOut)
                {
                    break;
                }
            }
            return lstFilterItem;
        }
 
 
 
        /// <summary>
        /// 降低货位库存分配量
        /// </summary>
        /// <param name="lstIQ"></param>
        /// <param name="allocItem"></param>
        /// <param name="needDivideQty"></param>
        /// <param name="lstStrate"></param>
        public void DivideAllocQty(ref List<itemQty> lstIQ, itemQty allocItem, ref decimal needDivideQty, List<string> lstStrate)
        {
            List<itemQty> lstFilterItem = FilterItemByStegy(lstIQ.Where(o => o.trayCode == allocItem.trayCode && o.itemCode == allocItem.itemCode && o.CN_S_OWNER == allocItem.CN_S_OWNER && o.CN_S_ITEM_STATE == allocItem.CN_S_ITEM_STATE).ToList(), lstStrate);
            decimal tmpQty = 0;
            List<string> lstNeedRemove = new List<string>();
            foreach (itemQty item in lstFilterItem)
            {
                tmpQty = needDivideQty - item.Qty;
                if (tmpQty < 0)
                {
 
                    //降低该记录的可出库量,并跳出循环
                    lstIQ.Find(O => O.ID == item.ID).Qty = item.Qty - needDivideQty;
                    needDivideQty = tmpQty;
                    break;
                }
                else
                {
                    needDivideQty = tmpQty;
                    //删除该记录
                    lstNeedRemove.Add(item.ID);
 
                }
            }
            lstIQ.RemoveAll(o => lstNeedRemove.Contains(o.ID));
            if (needDivideQty > 0 && lstIQ.Count > 0)
            {
                //如果未分配完则递归循环
                DivideAllocQty(ref lstIQ, allocItem, ref needDivideQty, lstStrate);
            }
        }
        /// <summary>
        /// 将货位中的物料数据按照策略排序
        /// </summary>
        /// <param name="lstIQ"></param>
        /// <param name="lstStrate"></param>
        /// <returns></returns>
        public List<itemQty> FilterItemByStegy(List<itemQty> lstIQ, List<string> lstStrate)
        {
            List<itemQty> lstFilterItem = lstIQ;
            List<itemQty> lstEmptyPBC;
            foreach (string stegy in lstStrate)
            {
                //逐个策略进行计算
                switch (stegy)
                {
                    case "FirstInFirstOut":
                        lstFilterItem = lstFilterItem.OrderBy(o => o.lotNo).ToList();
                        lstFilterItem = lstFilterItem.Where(o => o.lotNo == lstFilterItem.FirstOrDefault().lotNo).ToList();
                        break;
                    case "FirstWarrantFirstOut":
                        lstEmptyPBC = lstFilterItem.Where(o => string.IsNullOrEmpty(o.prodBatchCode)).ToList();
                        lstFilterItem = lstFilterItem.Where(o => !string.IsNullOrEmpty(o.prodBatchCode)).OrderBy(o => o.prodBatchCode).ToList();
                        lstFilterItem.AddRange(lstEmptyPBC);
                        lstFilterItem = lstFilterItem.Where(o => o.prodBatchCode == lstFilterItem.FirstOrDefault().prodBatchCode).ToList();
                        break;
                }
            }
            return lstFilterItem;
        }
        #endregion
 
 
 
 
        #region 生成查询条件
        /// <summary>
        /// 生成查询条件
        /// </summary>
        /// <param name="itemCodes"></param>
        /// <param name="itemState"></param>
        /// <param name="batchCode"></param>
        /// <param name="ownerName"></param>
        /// <returns></returns>
        public string GetPropWhereStr1(string itemCodes, string itemState, string batchCode, string prodBatchCode, string ownerName)
        {
            StringBuilder strWhere = new StringBuilder();
            // strWhere.Append(" 1=1 ");
 
            //物料编码
            if (!string.IsNullOrEmpty(itemCodes))
            {
                strWhere.AppendLine(" AND CN_S_ITEM_CODE IN ('" + itemCodes + "') ");
            }
            //物料状态
            if (!string.IsNullOrEmpty(itemState))
            {
                strWhere.AppendLine(" AND CN_S_ITEM_STATE = '" + itemState + "' ");
            }
            //批次号
            if (!string.IsNullOrEmpty(batchCode))
            {
                strWhere.AppendLine(" AND CN_S_LOT_NO = '" + batchCode + "' ");
            }
            //生产批次号
            if (!string.IsNullOrEmpty(prodBatchCode))
            {
                strWhere.AppendLine(" AND CN_S_PRODUCTION_BATCH = '" + prodBatchCode + "' ");
            }
            //货主
            if (!string.IsNullOrEmpty(ownerName))
            {
                strWhere.AppendLine(" AND CN_S_OWNER = '" + ownerName + "' ");
            }
            return strWhere.ToString();
        }
        #endregion
        #region 出库调用的方法 将传递过来的请求出库的物料进行分组
        /// <summary>
        /// 将传递过来的请求出库的物料进行分组 保证算法的执行速度
        /// </summary>
        /// <param name="lstAreaItem"></param>
        /// <returns></returns>
        private List<Out_AreaItemClass> GroupAreaCode(List<itemQueryClass> lstQueryItem)
        {
            //定义分组后的物料数据
            List<Out_AreaItemClass> lstAreaItemGroup = new List<Out_AreaItemClass>();
            if (lstQueryItem == null || lstQueryItem.Count == 0)
            {
                return lstAreaItemGroup;
            }
 
            //按照库区等查询条件分组
            var v_AreaGroup = lstQueryItem.GroupBy(a => new
            {
                //追加条件后 需要在此处加入分组条件定义  &&&&
                a.stockCode,
                a.areaCode,
                a.ownerName,
                a.batchCode,
                a.prodBatchCode,
                a.itemState,
                a.itemCode
            }).Select(g => (new
            {
                //追加条件后 需要在此处加入分组条件定义  &&&&
                stockCode = g.Key.stockCode,
                areaCode = g.Key.areaCode,
                ownerName = g.Key.ownerName,
                batchCode = g.Key.batchCode,
                prodBatchCode = g.Key.prodBatchCode,
                itemState = g.Key.itemState,
                itemCode = g.Key.itemCode,
                itemQty = g.Sum(p => p.itemQty)
            }));
            var v_AreaGroupNoItemCode = lstQueryItem.GroupBy(a => new
            {
                //追加条件后 需要在此处加入分组条件定义  &&&&
                a.stockCode,
                a.areaCode,
                a.ownerName,
                a.batchCode,
                a.prodBatchCode,
                a.itemState,
            }).Select(g => (new
            {
                //追加条件后 需要在此处加入分组条件定义  &&&&
                stockCode = g.Key.stockCode,
                areaCode = g.Key.areaCode,
                ownerName = g.Key.ownerName,
                batchCode = g.Key.batchCode,
                prodBatchCode = g.Key.prodBatchCode,
                itemState = g.Key.itemState
            }));
            foreach (var i_group in v_AreaGroupNoItemCode)
            {
                Out_AreaItemClass areaItemEntity = new Out_AreaItemClass();
                List<itemInClass> lstItem = new List<itemInClass>();
 
                //循环传递的实体参数数量
                foreach (var areaItem in v_AreaGroup)
                {
                    //如果库区等查询条件相同
                    if ((areaItem.stockCode == i_group.stockCode) && (areaItem.areaCode == i_group.areaCode) && (areaItem.ownerName == i_group.ownerName) && (areaItem.batchCode == i_group.batchCode) && (areaItem.prodBatchCode == i_group.prodBatchCode) && (areaItem.itemState == i_group.itemState))
                    {
                        //则将物料列表信息传递给分组后的列表
                        itemInClass itemIn = new itemInClass();
                        itemIn.itemCode = areaItem.itemCode;
                        itemIn.itemQty = decimal.Parse(areaItem.itemQty.ToString());
                        lstItem.Add(itemIn);
                    }
                }
                areaItemEntity.stockCode = i_group.stockCode;
                areaItemEntity.areaCode = i_group.areaCode;
                //追加条件后 需要在此处加入分组条件定义  &&&&
                areaItemEntity.ownerName = i_group.ownerName;
                areaItemEntity.batchCode = i_group.batchCode;
                areaItemEntity.prodBatchCode = i_group.prodBatchCode;
                areaItemEntity.itemState = i_group.itemState;
                areaItemEntity.lstItem = lstItem.OrderByDescending(o => o.itemQty).ToList();
                lstAreaItemGroup.Add(areaItemEntity);
            }
            return lstAreaItemGroup;
        }
        #endregion
 
        /**************计算  返回物料所在货位的算法结束/**************/
 
 
        /*****************计算  返回库区的算法开始/*****************/
 
        #region 非作业区平库立库  返回库区对应出库物料的算法,该算法只算到库区可出库数量
        /// <summary>
        /// 出库  返回库区对应出库物料的算法,该算法只算到库区,不到货位
        /// </summary>
        /// <param name="model">返回库区算法传递的类</param>
        /// <returns></returns>
        public OutAreaResultAllEntity OutArea(List<itemQueryClass> lstQueryOut, List<areaPriorClass> lstAreaPrior)
        {
            Log.AlgorInfo("Out_AlgorBLL—OutArea", "算法开始,传入参数lstQueryOut:" + JsonConvert.SerializeObject(lstQueryOut) + ",lstAreaPrior:" + JsonConvert.SerializeObject(lstAreaPrior));
            //返回数据的实体集合   
            OutAreaResultAllEntity outAreaResult = new OutAreaResultAllEntity();
            //存放计算物料库存结果
            List<OutAreaResultEntity> lstOutResultEntity = new List<OutAreaResultEntity>();
            bool checkEnough = true;
            //将传递过来的参数按货主、批次号、物料状态分组后再按物料分组
            List<Out_AreaItemClass> lstAreaItemGroup = GroupAreaCode(lstQueryOut);
            //循环处理
            foreach (var i_Group in lstAreaItemGroup)
            {
                Log.AlgorInfo("Out_AlgorBLL—OutArea", "循环处理lstAreaItemGroup:" + i_Group.areaCode + "," + i_Group.batchCode + "," + i_Group.itemState + "," + i_Group.ownerName);
                string stockCode = i_Group.stockCode;
                #region 判断库存是否足够
                //获得所有物料
                string itemCodes = string.Join(",", i_Group.lstItem.Select(o => o.itemCode).ToList()).Replace(",", "','");
 
                //获得查询条件
                string strWhere = GetPropWhereStr1(itemCodes, i_Group.itemState, i_Group.batchCode, i_Group.prodBatchCode, i_Group.ownerName);
                //获得物料在仓库中的数量
                List<OutAreaItemQty> lstAreaItemQty = null;
                lstAreaItemQty = CreateDAL<TN_WM_B_AREA_QTYDAL>().GetAreaQty(strWhere, stockCode);
                lstAreaItemQty.ForEach(x => x.areaCode = x.areaCode.Trim());
                lstAreaItemQty.RemoveAll(o => o.Qty <= 0);
                lstAreaItemQty = lstAreaItemQty.Join(lstAreaPrior, u => u.areaCode, d => d.areaCode, (u, d) => new { u, d })
                    .Select(o => new OutAreaItemQty
                    {
                        areaCode = o.u.areaCode,
                        itemCode = o.u.itemCode,
                        Qty = o.u.Qty,
                        locationCode = o.u.locationCode,
                        Prior = o.d.Prior
                    }
                    ).OrderBy(p => p.Prior).ToList();
 
                var lstStockItemQty = lstAreaItemQty.GroupBy(a => a.itemCode).Select(g => new
                {
                    itemCode = g.Key,
                    stockNum = g.Sum(p => p.Qty)
                });
 
                List<itemOutClass> lstFalseItem = new List<itemOutClass>();
                foreach (itemInClass item in i_Group.lstItem)
                {
                    #region 循环处理查询不满足出库数量的物料
                    itemOutClass outClassEntity = new itemOutClass();
                    var itemEntity = lstStockItemQty.Where(o => o.itemCode == item.itemCode).SingleOrDefault();
                    if (itemEntity != null)
                    {
                        //获得仓库内该物料的数量
                        decimal qty = (decimal)item.itemQty;
                        if (qty < (decimal)item.itemQty)
                        {
                            outClassEntity.Success = false;
                            outClassEntity.Msg = "当前仓库中该物料的数量不满足需要出库的数量";
                            outClassEntity.itemCode = item.itemCode;
                            outClassEntity.itemQty = qty;
                            lstFalseItem.Add(outClassEntity);
                        }
                    }
                    else
                    {
                        outClassEntity.Success = false;
                        outClassEntity.Msg = "在仓库中未找到该物料对应的库存数量信息";
                        outClassEntity.itemCode = item.itemCode;
                        outClassEntity.itemQty = 0;
                        lstFalseItem.Add(outClassEntity);
                    }
                    #endregion
                }
                if (lstFalseItem != null && lstFalseItem.Count > 0)
                {
                    checkEnough = false;
                }
 
                lstOutResultEntity.AddRange(GetOutAreaList(stockCode, lstAreaItemQty, i_Group, i_Group.lstItem, lstAreaPrior));
 
                #endregion
 
            }
            if (!checkEnough)
            {
                outAreaResult.Success = false;
                outAreaResult.Msg = "物料库存不足";
            }
            else
            {
                outAreaResult.Success = true;
                outAreaResult.Msg = "";
            }
            outAreaResult.itemOutAreaResult = lstOutResultEntity;
            Log.AlgorInfo("Out_AlgorBLL—OutArea", "计算结果outAreaResult:" + JsonConvert.SerializeObject(outAreaResult));
            return outAreaResult;
        }
        #endregion
        #region 用于计算返回库区的算法
        /// <summary>
        /// 用于计算返回库区的算法
        /// </summary>
        /// <param name="stockCode"></param>
        /// <param name="lstAreaOutItemQty">每个库区对应的物料数量</param>
        /// <param name="lstItem"></param>
        /// <returns></returns>
        private List<OutAreaResultEntity> GetOutAreaList(string stockCode, List<OutAreaItemQty> lstAreaOutItemQty, Out_AreaItemClass outAreaItem, List<itemInClass> lstItem, List<areaPriorClass> lstAreaPrior)
        {
            List<OutAreaResultEntity> lstOutResultEntity = new List<OutAreaResultEntity>();
            List<string> lstArea = lstAreaPrior.OrderBy(o => o.Prior).Select(p => p.areaCode).ToList();
            //循环所有库区
            for (int i = 0; i < lstArea.Count; i++)
            {
                //找到该库区下的物料
                List<OutAreaItemQty> lstItem_Area = lstAreaOutItemQty.Where(o => o.areaCode == lstArea[i]).ToList();
                if (lstItem_Area != null && lstItem_Area.Count > 0)
                {
                    if (outAreaItem.lstItem != null && outAreaItem.lstItem.Count > 0)
                    {
                        OutAreaResultEntity resultEntity = new OutAreaResultEntity();
                        resultEntity.stockCode = stockCode;
                        resultEntity.areaCode = lstArea[i];
                        resultEntity.ownerName = outAreaItem.ownerName;
                        resultEntity.batchCode = outAreaItem.batchCode;
                        resultEntity.prodBatchCode = outAreaItem.prodBatchCode;
                        resultEntity.itemState = outAreaItem.itemState;
                        //获取当前库区下可分配的物料列表
                        List<itemOutClass> LstItemOut = GetItemArea(lstItem_Area, ref lstItem);
                        //得到该库区下分配的所有物料
                        resultEntity.lstItem = LstItemOut;
                        lstOutResultEntity.Add(resultEntity);
                    }
                }
            }
            return lstOutResultEntity;
        }
        #endregion
        #region 计算库区与物料的对应关系
        /// <summary>
        /// 计算库区与物料的对应关系
        /// </summary>
        /// <param name="lstItem_Area">当前库区下物料集合</param>
        /// <param name="lstItem">需要计算的物料</param>
        /// <returns></returns>
        public List<itemOutClass> GetItemArea(List<OutAreaItemQty> lstItem_Area, ref List<itemInClass> lstItem)
        {
            List<itemOutClass> LstItemOut = new List<itemOutClass>();
            List<string> lstNeedRemove = new List<string>();
 
            foreach (itemInClass itemInEntity in lstItem)
            {
                itemOutClass outEntity = new itemOutClass();
 
                OutAreaItemQty area_item = lstItem_Area.Where(o => o.itemCode == itemInEntity.itemCode).SingleOrDefault();
                if (area_item != null)
                {
                    outEntity.itemCode = itemInEntity.itemCode;
                    //如果该库区中库存满足条件
                    if (itemInEntity.itemQty != 0)
                    {
                        if (itemInEntity.itemQty <= area_item.Qty)
                        {
                            //物料数量 = 需要的物料数量
                            outEntity.itemQty = itemInEntity.itemQty;
                            outEntity.Success = true;
                            //表示需求物料数量在该库区中满足要求
                            //删除该物料
                            lstNeedRemove.Add(area_item.itemCode);
 
                        }
                        else//如果数量不满足
                        {
                            //需求数量下降
                            itemInEntity.itemQty -= area_item.Qty;
                            //等于该库区中的数量
                            outEntity.itemQty = area_item.Qty;
                        }
                        LstItemOut.Add(outEntity);
                    }
 
 
                }
            }
            foreach (string itemCode in lstNeedRemove)
            {
                lstItem.RemoveAll(o => o.itemCode == itemCode);
            }
 
            return LstItemOut;
        }
        #endregion
 
        //*****************计算  返回库区的算法结束/*****************/
 
        #region 作业区出库算法入口
 
        public OutAssignResultEntity OutAssign(OutAssignEnitty model)
        {
            Log.AlgorInfo("OutAssign", "出库算法参数:" + JsonConvert.SerializeObject(model));
            OutAssignResultEntity outResult = new OutAssignResultEntity();
            outResult.errCode = "";
            try
            {
                //标准出库算法
                string areaType = CreateDAL<TN_WMS_AREADAL>().GetAreaTypeByCode(model.lstAreaPrior[0].areaCode.ToString());
                if (areaType == Constants.Area_Struc_PingStock || areaType == Constants.Area_Struc_LiStock)
                {
                    outResult = FlatAreaOutLocation(model);
                }
                else if (areaType == Constants.Area_Struc_LiuLiStock)
                {
 
                }
                outResult.areaType = areaType;
                Log.AlgorInfo("OutAssign", "出库返回结果:" + JsonConvert.SerializeObject(outResult));
                return outResult;
            }
            catch (Exception ex)
            {
                Log.AlgorInfo("OutAssign", "异常:" + ex.Message + ex.StackTrace);
                outResult.Msg = "算法异常," + ex.Message + ex.StackTrace;
                outResult.errCode = "5";
                return outResult;
            }
        }
 
        #endregion
 
        #region 平库出库算法
        public OutAssignResultEntity FlatAreaOutLocation(OutAssignEnitty model)
        {
            OutAssignResultEntity outResult = new OutAssignResultEntity();
            #region 传入参数判断
            if (model == null)
            {
                outResult.Success = false;
                outResult.errCode = "6";
                outResult.Msg = "参数实体不能为 null !";
                return outResult;
            }
            //判断指定的入作业库对象类型不能为空
            if (string.IsNullOrEmpty(model.stockCode))
            {
                outResult.Success = false;
                outResult.errCode = "6";
                outResult.Msg = "指定的出库仓库编号不能为空!";
                return outResult;
            }
            //判断传入库区列表不能为空
            if (model.lstAreaPrior == null || model.lstAreaPrior.Count == 0)
            {
                outResult.Success = false;
                outResult.errCode = "6";
                outResult.Msg = "指定的出作业库库区列表不能为空!";
                return outResult;
            }
            #endregion
            List<STRATEGYALGOREntity> lstStrategy = CreateDAL<TN_WM_B_STRATEGYDAL>().GetStrateListByAreaOrStock(model.stockCode, "", "出库");
            List<string> lstStrate = lstStrategy.Select(o => o.CN_S_NAME).ToList();
            if (lstStrate.Count > 0)
            {
                //如果仓库配置策略(如生产日期早先出等),则优先响应仓库策略
                outResult = OutStockPrior(model, lstStrate);
            }
            else
            {
                //如果仓库没有配置策略则优先响应库区优先策略
                outResult = OutAreaPrior(model);
 
            }
            return outResult;
        }
 
 
        /// <summary>
        /// 作业区出库算法  优先仓库策略
        /// </summary>
        /// <param name="model"></param>
        /// <param name="lstStrategy"></param>
        /// <returns></returns>
        private OutAssignResultEntity OutStockPrior(OutAssignEnitty model, List<string> lstStrategy)
        {
            //返回实体集合
            OutAssignResultEntity outResult = new OutAssignResultEntity();
            outResult.Success = false;
            if (string.IsNullOrEmpty(model.itemCode))
            {
                outResult.Msg = "库区中无该规格的空托";
            }
            else
            {
                outResult.Msg = "库存不足";
            }
            SqlExecuteResult result = null;
            List<Device> lstDevice = model.lstDevice;
            string deviceCode = string.Empty;
            AutoBomLocationAbbreEntity at_l_Entity = null;
            List<AutoBomLocationAbbreEntity> lstTmp_Location = new List<AutoBomLocationAbbreEntity>();
            List<AutoBomLocationAbbreEntity> lstTrueLocation = new List<AutoBomLocationAbbreEntity>();
            string isControlQty = CreateDAL<TN_WMS_AREADAL>().GetIsControlQtyByCode(model.lstAreaPrior[0].areaCode.ToString());
            model.lockLocation = isControlQty.Equals("Y") ? true : false;//不管控数量时,不锁定起点货位
            outResult.isControlQty = isControlQty;
            string areaCodes = string.Join("','", model.lstAreaPrior.Select(o => o.areaCode));
            //获得作业区中所有匹配条件(物料等)的货位
            List<outAssignLocation> lstCanOutL = CreateDAL<Out_AlgorDAL>().GetAssignFlatItemQty(model.itemCode, areaCodes);
            if (lstCanOutL.Count == 0)
            {
                return outResult;
            }
            if (lstStrategy.Contains("ProductTimeOutFirst"))
            {
                lstCanOutL = lstCanOutL.OrderBy(o => o.productDate).ToList();
                at_l_Entity = CreateDAL<TN_WMS_LOCATIONDAL>().GetLocationByLocationCodeAbbre(lstCanOutL[0].ToString());
            }
            else if (lstStrategy.Contains("InStockTimeOutFirst"))
            {
                lstCanOutL = lstCanOutL.OrderBy(o => o.opTime).ToList();
                at_l_Entity = CreateDAL<TN_WMS_LOCATIONDAL>().GetLocationByLocationCodeAbbre(lstCanOutL[0].ToString());
            }
 
            if (at_l_Entity == null)
            {
                return outResult;
            }
 
            else
            {
                //更新货位状态
                if (model.lockLocation)
                {
                    result = CreateDAL<TN_WM_LOCATION_EXTDAL>().UpdateLocationState(at_l_Entity.CN_S_LOCATION_CODE, Constants.Location_State_OutLock, "出库", null);
                    if (result != null)
                    {
                        if (result.Success && result.Row > 0)
                        {
                            outResult.Success = true;
                            outResult.Msg = "";
                            outResult.locationCode = at_l_Entity.CN_S_LOCATION_CODE;
                            outResult.trayCode = lstCanOutL.Where(o => o.locationCode == at_l_Entity.CN_S_LOCATION_CODE).FirstOrDefault().trayCode;
                        }
                        else
                        {
                            outResult.Success = false;
                            outResult.Msg = "锁定出库货位失败";
                        }
                    }
                }
                else
                {
                    outResult.Success = true;
                    outResult.Msg = "";
                    outResult.locationCode = at_l_Entity.CN_S_LOCATION_CODE;
                    outResult.trayCode = lstCanOutL.Where(o => o.locationCode == at_l_Entity.CN_S_LOCATION_CODE).FirstOrDefault().trayCode;
                }
            }
 
            return outResult;
        }
 
        /// <summary>
        /// 作业区出库算法  优先库区
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private OutAssignResultEntity OutAreaPrior(OutAssignEnitty model)
        {
            //返回实体集合
            OutAssignResultEntity outResult = new OutAssignResultEntity();
            outResult.Success = false;
            outResult.errCode = "7";
            if (string.IsNullOrEmpty(model.itemCode))
            {
                outResult.Msg = "库区中无空托";
            }
            else
            {
                outResult.Msg = "库存不足";
            }
 
            List<STRATEGYALGOREntity> lstStrategy = null;
            List<outAssignLocation> lstCanOutL = null;
            List<Device> lstDevice = model.lstDevice;
            string deviceCode = string.Empty;
            List<string> lstStrate = null;
            SqlExecuteResult result = null;
            outAssignLocation at_l_Entity = null;
            List<outAssignLocation> lstTmp_Location = new List<outAssignLocation>();
            List<AutoBomLocationAbbreEntity> lstTrueLocation = new List<AutoBomLocationAbbreEntity>();
            List<AutoBomLocationAbbreEntity> lstAllLocation = new List<AutoBomLocationAbbreEntity>();
            //被锁定的货位
            List<AutoBomLocationAbbreEntity> lstLockLItem = new List<AutoBomLocationAbbreEntity>();
            List<AutoBomLocationAbbreEntity> lstLocation = new List<AutoBomLocationAbbreEntity>();
            string isControlQty = string.Empty;
            string isControlInv = string.Empty;
            decimal needOutWeight = 0;
            AutoBomItemEntity autoBomE = null;
            if (model.projectCode == "ys001")
            {
                if (!string.IsNullOrEmpty(model.itemCode))
                {
                    autoBomE = CreateDAL<TN_WMS_ITEMDAL>().GetItemEntity(model.itemCode);
                }
            }
            //将传递过来的库区列表按库区出库优先级排序
            List<areaPriorClass> lstAreaPrior = model.lstAreaPrior.OrderBy(o => o.Prior).ToList();
            foreach (areaPriorClass item in lstAreaPrior)
            {
                AutoBomStockAreaEntity areaModel = CreateDAL<TN_WMS_AREADAL>().GetModel(item.areaCode.ToString());
                isControlQty = areaModel.CN_C_IS_CONTROL_QTY;
                isControlInv = areaModel.CN_C_IS_INVENTORY;
                if (model.needCalLock)
                {
                    model.lockLocation = isControlQty.Equals("Y") ? true : false;//不管控数量时,不锁定目的货位
                }
                lstStrategy = CreateDAL<TN_WM_B_STRATEGYDAL>().GetStrateListByAreaOrStock("", item.areaCode, "出库");
                lstStrate = lstStrategy.Select(o => o.CN_S_CODE).ToList();
                if (string.IsNullOrEmpty(model.itemCode))
                {
 
                    #region 空托盘出库
                    //空托盘出库,add by ly 20190102  增加根据托盘类型出库
                    lstCanOutL = CreateDAL<Out_AlgorDAL>().GetAssignFlatEmptySpecTray(model.traySpec, item.areaCode);
                    if (lstCanOutL.Count == 0)
                    {
                        continue;
                    }
                    else
                    {
                        lstTrueLocation = CreateDAL<TN_WMS_LOCATIONDAL>().GetLocationByLocationCode(lstCanOutL.Select(o => o.locationCode).ToList());
                        lstCanOutL = lstCanOutL.Join(lstTrueLocation, u => u.locationCode, d => d.CN_S_LOCATION_CODE, (u, d) => new { u, d }).Select(o => new outAssignLocation
                        {
                            trayCode = o.u.trayCode,
                            locationCode = o.u.locationCode,
                            stockAreaCode = o.u.stockAreaCode,
                            useState = o.u.useState,
                            opTime = o.u.opTime,
                            productDate = o.u.productDate,
                            lotNo = o.u.lotNo,
                            CN_S_STOCK_CODE = o.d.CN_S_STOCK_CODE,
                            CN_S_ROADWAY = o.d.CN_S_ROADWAY,
                            CN_S_ROW = o.d.CN_S_ROW,
                            CN_S_COL = o.d.CN_S_COL,
                            CN_S_FLOOR = o.d.CN_S_FLOOR,
                            CN_S_LOCATION_STATE = o.d.CN_S_LOCATION_STATE
 
                        }).ToList();
                        lstTmp_Location = CalculateLocByStegy(ref lstDevice, lstCanOutL, lstStrate, out deviceCode);
                        if (lstTmp_Location.Count > 0)
                        {
                            at_l_Entity = lstTmp_Location[0];
                            #region 锁定待出库货位
                            if (at_l_Entity != null)
                            {
                                if (model.lockLocation)
                                {
                                    result = CreateDAL<TN_WM_LOCATION_EXTDAL>().UpdateLocationState(at_l_Entity.locationCode, Constants.Location_State_OutLock, "出库", null);
                                    if (result != null)
                                    {
                                        if (result.Success && result.Row > 0)
                                        {
                                            outResult.Success = true;
                                            outResult.Msg = "";
                                            outResult.errCode = "";
                                            outResult.locationCode = at_l_Entity.locationCode;
                                            outResult.trayCode = lstCanOutL.Where(o => o.locationCode == at_l_Entity.locationCode).FirstOrDefault().trayCode;
                                            outResult.areaCode = item.areaCode;
                                            outResult.isControlQty = isControlQty;
                                            outResult.isControlInv = isControlInv;
                                            break;
                                        }
 
                                    }
                                }
                                else
                                {
                                    outResult.Success = true;
                                    outResult.Msg = "";
                                    outResult.errCode = "";
                                    outResult.locationCode = at_l_Entity.locationCode;
                                    outResult.trayCode = lstCanOutL.Where(o => o.locationCode == at_l_Entity.locationCode).FirstOrDefault().trayCode;
                                    outResult.areaCode = item.areaCode;
                                    outResult.isControlQty = isControlQty;
                                    outResult.isControlInv = isControlInv;
                                    break;
                                }
                            }
                            #endregion
                            break;
                        }
                    }
                    #endregion
 
                }
                else
                {
                    #region 满托盘出库
 
                    lstCanOutL = CreateDAL<Out_AlgorDAL>().GetAssignFlatItemQty(model.itemCode, item.areaCode);
                    lstTrueLocation = CreateDAL<TN_WMS_LOCATIONDAL>().GetLocationByLocationCode(lstCanOutL.Select(o => o.locationCode).ToList());
                    lstCanOutL = lstCanOutL.Join(lstTrueLocation, u => u.locationCode, d => d.CN_S_LOCATION_CODE, (u, d) => new { u, d }).Select(o => new outAssignLocation
                    {
                        trayCode = o.u.trayCode,
                        locationCode = o.u.locationCode,
                        stockAreaCode = o.u.stockAreaCode,
                        useState = o.u.useState,
                        opTime = o.u.opTime,
                        productDate = o.u.productDate,
                        lotNo = o.u.lotNo,
                        CN_S_STOCK_CODE = o.d.CN_S_STOCK_CODE,
                        CN_S_IN_AREA_CODE = o.u.CN_S_IN_AREA_CODE,
                        CN_S_ROADWAY = o.d.CN_S_ROADWAY,
                        CN_S_ROW = o.d.CN_S_ROW,
                        CN_S_COL = o.d.CN_S_COL,
                        CN_S_FLOOR = o.d.CN_S_FLOOR,
                        CN_S_LOCATION_STATE = o.d.CN_S_LOCATION_STATE
                    }).ToList();
                    Log.AlgorInfo("OutAssign-GetAssignFlatItemQty结果", JsonConvert.SerializeObject(lstCanOutL));
                    if (lstCanOutL.Count > 0)
                    {
                        //获取该库区中可用货位的信息
                        Log.AlgorInfo("OutAssign-lstStrate", JsonConvert.SerializeObject(lstStrate));
                        lstTmp_Location = CalculateLocByStegy(ref lstDevice, lstCanOutL, lstStrate, out deviceCode);
                        if (lstTmp_Location.Count > 0)
                        {
                            at_l_Entity = lstTmp_Location[0];
                            #region 锁定待出库货位
                            if (at_l_Entity != null)
                            {
                                if (model.lockLocation)
                                {
                                    result = CreateDAL<TN_WM_LOCATION_EXTDAL>().UpdateLocationState(at_l_Entity.locationCode, Constants.Location_State_OutLock, "出库", null);
                                    if (result != null)
                                    {
                                        if (result.Success && result.Row > 0)
                                        {
                                            outResult.Success = true;
                                            outResult.errCode = "";
                                            outResult.Msg = "";
                                            outResult.locationCode = at_l_Entity.locationCode;
                                            outResult.trayCode = lstCanOutL.Where(o => o.locationCode == at_l_Entity.locationCode).FirstOrDefault().trayCode;
                                            outResult.areaCode = item.areaCode;
                                            outResult.isControlQty = isControlQty;
                                            outResult.isControlInv = isControlInv;
                                            break;
                                        }
 
                                    }
                                }
                                else
                                {
                                    outResult.Success = true;
                                    outResult.Msg = "";
                                    outResult.errCode = "";
                                    outResult.locationCode = at_l_Entity.locationCode;
                                    outResult.trayCode = lstCanOutL.Where(o => o.locationCode == at_l_Entity.locationCode).FirstOrDefault().trayCode;
                                    outResult.areaCode = item.areaCode;
                                    outResult.isControlQty = isControlQty;
                                    outResult.isControlInv = isControlInv;
                                    break;
                                }
                            }
                            #endregion
                            break;
                        }
                    }
                    #endregion
 
                }
 
 
            }
            return outResult;
 
        }
 
        /// <summary>
        /// 根据入库策略筛选符合条件的货位
        /// </summary>
        /// <param name="lstDevice"></param>
        /// <param name="lstLocation"></param>
        /// <param name="lstStrate"></param>
        /// <param name="deviceCode"></param>
        /// <returns></returns>
        public List<outAssignLocation> CalculateLocByStegy(ref List<Device> lstDevice, List<outAssignLocation> lstLocation, List<string> lstStrate, out string deviceCode)
        {
            List<outAssignLocation> lstFilterLoc = lstLocation;
            deviceCode = string.Empty;
            AlgorTacticsCommon Trctics = new AlgorTacticsCommon();
            foreach (string stegy in lstStrate)
            {
                //逐个策略进行计算
                switch (stegy)
                {
                    case "EquipmentBalance":   //设备均衡
                        lstFilterLoc = Trctics.EquipmentBalanceOut(lstDevice, lstFilterLoc, out deviceCode);
                        break;
                    case "RoadWayBalance":    //巷道均衡
                        lstFilterLoc = Trctics.RoadWayBalanceOut(lstFilterLoc); ;
                        break;
                    case "NearbyBalance":     //就近原则
                        lstFilterLoc = Trctics.NearbyBalanceOut(lstFilterLoc); ;
                        break;
                    case "ProductTimeOutFirst":  //生产日期早先出
                        lstFilterLoc = lstFilterLoc.OrderBy(o => o.productDate).ToList();
                        lstFilterLoc = lstFilterLoc.Where(o => o.productDate == lstFilterLoc.FirstOrDefault().productDate).ToList();
                        break;
                    case "InStockTimeOutFirst":    //入库时间早先出
                        lstFilterLoc = lstFilterLoc.OrderBy(o => o.opTime).ToList();
                        lstFilterLoc = lstFilterLoc.Where(o => o.opTime == lstFilterLoc.FirstOrDefault().opTime).ToList();
                        break;
                    case "SatisfyWeightFirst":    //满足重量托盘先出
                        lstFilterLoc = lstFilterLoc.OrderByDescending(o => o.totalWeight).ToList();
                        if (lstFilterLoc[0].totalWeight >= lstFilterLoc[0].needWeight)
                        {
                            lstFilterLoc = lstFilterLoc.Where(o => o.totalWeight >= lstFilterLoc[0].needWeight).ToList();
                        }
                        else
                        {
                            lstFilterLoc = lstFilterLoc.Where(o => o.totalWeight == lstFilterLoc[0].totalWeight).ToList();
                        }
                        break;
                    case "MinMoveTray":    //移动托盘小列先出
                        lstFilterLoc = lstFilterLoc.OrderBy(o => o.moveStep).ToList();
                        lstFilterLoc = lstFilterLoc.Where(o => o.moveStep == lstFilterLoc[0].moveStep).ToList();
                        break;
                    case "BackItemFirst":    //退料先出
                        lstFilterLoc = lstFilterLoc.OrderBy(o => o.CN_S_IN_AREA_CODE).ToList();
                        lstFilterLoc = lstFilterLoc.Where(o => o.CN_S_IN_AREA_CODE == lstFilterLoc[0].CN_S_IN_AREA_CODE).ToList();
                        break;
                    case "FirstInLastOut":    //先进后出
                        Log.AlgorInfo("OutAssign-CalculateLocByStegy", "FirstInLastOut");
                        if (lstStrate.Contains("OutBigToSmall"))
                        {
                            Log.AlgorInfo("OutAssign-OutBigToSmall", "OutBigToSmall");
                            lstFilterLoc = lstFilterLoc.OrderBy(o => int.Parse(o.CN_S_ROW)).ThenByDescending(o => int.Parse(o.CN_S_COL)).ThenByDescending(b => int.Parse(b.CN_S_FLOOR)).ToList();
                        }
                        else if (lstStrate.Contains("OutSmallToBig"))
                        {
                            Log.AlgorInfo("OutAssign-OutSmallToBig", "OutSmallToBig");
                            lstFilterLoc = lstFilterLoc.OrderBy(o => int.Parse(o.CN_S_ROW)).ThenBy(o => int.Parse(o.CN_S_COL)).ThenByDescending(b => int.Parse(b.CN_S_FLOOR)).ToList();
                        }
                        else
                        {
                            lstFilterLoc = lstFilterLoc.OrderByDescending(o => int.Parse(o.CN_S_COL)).ThenBy(b => int.Parse(b.CN_S_FLOOR)).ToList();
                        }
                        Log.AlgorInfo("OutAssign-lstFilterLoc", JsonConvert.SerializeObject(lstFilterLoc));
                        lstFilterLoc = lstFilterLoc.Where(o => o.CN_S_COL == lstFilterLoc.FirstOrDefault().CN_S_COL && o.CN_S_FLOOR == lstFilterLoc.FirstOrDefault().CN_S_FLOOR).ToList();
                        break;
                    case "FirstInLastOutLj":    //先进后出
                        Log.AlgorInfo("OutAssign-CalculateLocByStegy", "FirstInLastOut");
                        if (lstStrate.Contains("OutBigToSmall"))
                        {
                            Log.AlgorInfo("OutAssign-OutBigToSmall", "OutBigToSmall");
                            lstFilterLoc = lstFilterLoc.OrderBy(o => int.Parse(o.CN_S_ROW)).ThenByDescending(o => int.Parse(o.CN_S_COL)).ThenByDescending(b => int.Parse(b.CN_S_FLOOR)).ToList();
                        }
                        else if (lstStrate.Contains("OutSmallToBig"))
                        {
                            Log.AlgorInfo("OutAssign-OutSmallToBig", "OutSmallToBig");
                            lstFilterLoc = lstFilterLoc.OrderBy(o => int.Parse(o.CN_S_ROW)).ThenBy(o => int.Parse(o.CN_S_COL)).ThenByDescending(b => int.Parse(b.CN_S_FLOOR)).ToList();
                        }
                        else
                        {
                            lstFilterLoc = lstFilterLoc.OrderByDescending(o => o.CN_S_ROW).ThenBy(b => int.Parse(b.CN_S_FLOOR)).ToList();
                        }
                        Log.AlgorInfo("OutAssign-lstFilterLoc", JsonConvert.SerializeObject(lstFilterLoc));
                        lstFilterLoc = lstFilterLoc.Where(o => o.CN_S_ROW == lstFilterLoc.FirstOrDefault().CN_S_ROW && o.CN_S_FLOOR == lstFilterLoc.FirstOrDefault().CN_S_FLOOR).ToList();
                        break;
 
                }
 
            }
            return lstFilterLoc;
        }
        /// <summary>
        /// 根据入库策略筛选符合条件的货位
        /// </summary>
        /// <param name="lstDevice"></param>
        /// <param name="lstLocation"></param>
        /// <param name="lstStrate"></param>
        /// <param name="deviceCode"></param>
        /// <returns></returns>
        public List<outAssignLocation> CalculateCanOutByStegy(List<outAssignLocation> lstCanOutL, List<string> lstStrate, decimal needOutWeight)
        {
            List<outAssignLocation> lstFilterLoc = lstCanOutL;
            foreach (string stegy in lstStrate)
            {
                //逐个策略进行计算
                switch (stegy)
                {
                    case "ProductTimeOutFirst":  //生产日期早先出
                        lstFilterLoc = lstFilterLoc.OrderBy(o => o.productDate).ToList();
                        lstFilterLoc = lstFilterLoc.Where(o => o.productDate == lstFilterLoc.FirstOrDefault().productDate).ToList();
                        break;
                    case "InStockTimeOutFirst":    //入库时间早先出
                        lstFilterLoc = lstFilterLoc.OrderBy(o => o.opTime).ToList();
                        lstFilterLoc = lstFilterLoc.Where(o => o.opTime == lstFilterLoc.FirstOrDefault().opTime).ToList();
                        break;
 
                }
 
            }
            return lstFilterLoc;
        }
 
        #endregion
 
    }
}