schangxiang@126.com
2025-09-18 49a51c068d62084bc4c3e77c4be94a20de556c4a
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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using wcftest.BussinessExtension.Common;
using wcftest.EnumDefine;
using wcftest.EnumDefine.Position;
using wcftest.Model;
using wcftest.orm;
using wcftest.sendToSap;
 
namespace wcftest.BussinessExtension
{
    /// <summary>
    /// 业务扩展类
    /// </summary>
    public class BussinessExtension
    {
 
        //public static void xx()
        //{
        //    var isQrCode = false;
        //    if (!string.IsNullOrEmpty(item.ExtendField06) && item.ExtendField06.Length > 15)
        //    {
        //        isQrCode = true;
        //        addPP.qrCode_guid = item.ExtendField06;
        //    }
        //}
 
        /// <summary>
        /// 是否存在 未结束的任务 【空托出库】
        /// </summary>
        /// <param name="edm"></param>
        /// <param name="toPositionName"></param>
        /// <returns></returns>
        public static bool IsExistNoFinishedTaskFor空托出库(dbModel edm, string toPositionName)
        {
            var queryTaskType = ((int)TaskTypeEnum.空托出库).ToString();
            var num = edm.Task_Queue.Where(x =>
                    x.TaskType == queryTaskType &&
                       (x.ToPositionName == toPositionName) &&
                       (x.TaskStatus == "未下发" || x.TaskStatus == "已下发" || x.TaskStatus == "下发中")
                      ).Count();
            return num > 0 ? true : false;
        }
 
        /// <summary>
        /// 是否存在 未结束的任务 【满空托回库】
        /// </summary>
        /// <param name="edm"></param>
        /// <param name="toPositionName"></param>
        /// <returns></returns>
        public static bool IsExistNoFinishedTaskFor满空托回库(dbModel edm, string fromPositionName)
        {
            var queryTaskType = ((int)TaskTypeEnum.满空托回库).ToString();
            var num = edm.Task_Queue.Where(x =>
                    x.TaskType == queryTaskType &&
                       (x.FromPositionName == fromPositionName) &&
                       (x.TaskStatus == "未下发" || x.TaskStatus == "已下发" || x.TaskStatus == "下发中")
                      ).Count();
            return num > 0 ? true : false;
        }
 
 
 
        /// <summary>
        /// 验证 指定库位号出库的条件
        /// </summary>
        /// <param name="mod"></param>
        /// <param name="stockALL_No收货暂存区"></param>
        /// <param name="sale"></param>
        /// <param name="saleList"></param>
        /// <param name="errMsg"></param>
        /// <returns></returns>
        public static bool ValidateSale_OrderForPointPositionName(dbModel mod, List<vvBase_ProductPosition> stockALL_No收货暂存区, Sale_Order sale, List<Sale_OrderList> saleList, ref string errMsg)
        {
            if (string.IsNullOrEmpty(sale.AuditRemark))
            {//说明 不是指定库位号出库的
                return true;
            }
            //按照指定库位号出库,要校验下 他要出库的明细跟库存匹配不?
            //获取该库位的库存
            var stockList = stockALL_No收货暂存区.Where(x => x.PositionName == sale.AuditRemark).ToList();
            if (stockList == null || stockList.Count == 0)
            {
                errMsg = "目标库位无库存";
                return false;
            }
            var products = new List<vvBase_ProductPosition>();
            foreach (var item in saleList)
            {
                //按跟踪号查找   ContainerNo = "1";//库位锁定
                if (item.TrackingNumber != null)
                {
                    products = stockALL_No收货暂存区.Where(x => x.ExtendField04 == item.TrackingNumber && x.ContainerNo != "1"
                    && x.ProductCode == item.ProductCode && x.IsLocked == (int)IsLockedEnum.未锁定 && x.PositionName != "收货暂存区"
                        //排序改为 限用日期 升序排序  【Editby shaocx,2024-06-22】
                        //).OrderBy(x => x.InStorageDate).ToList();
                    ).OrderBy(x => x.LimitDate).ToList();
                }
                //2021-6-19 修改为 picklist 有跟踪号的只能找跟踪号的,没跟踪号只能找没跟踪号
                else
                {
                    int prodectId = Convert.ToInt32(item.Product_Id);
 
                    products = stockALL_No收货暂存区.Where(x => x.Product_Id == prodectId && x.PositionName != "收货暂存区"
                        && (x.ExtendField04 == "" || x.ExtendField04 == null) && x.ContainerNo != "1" && x.IsLocked == (int)IsLockedEnum.未锁定
                        //排序改为 限用日期 升序排序  【Editby shaocx,2024-06-22】
                        //).OrderBy(x => x.InStorageDate).ToList();
                       ).OrderBy(x => x.LimitDate).ToList();
 
                }
                #region 增加指定库位号的过滤 【EditBy shaocx,2022-06-15】
                //增加指定库位号的过滤 【EditBy shaocx,2022-06-15】
                products = products.Where(x => x.PositionName == sale.AuditRemark).ToList();
                #endregion
                if (products.Count < 1)
                {
                    //errMsg = "目标库位" + sale.AuditRemark + "无 " + item.ProductName + " 库存 ";
                    errMsg = "目标库位无库存";
                }
                decimal i = (decimal)products.Where(x => x.ProductCode == item.ProductCode).Sum(x => x.ProductStorage);
                if (i < item.QuantityOrder)
                {
                    //errMsg = "目标库位" + sale.AuditRemark + " " + item.ProductName + " 库存数量" + i + ",小于需求数量:" + item.QuantityOrder;
                    errMsg = "目标库位库存不足 ";
                    return false;
                }
            }
 
            return true;
        }
 
        /// <summary>
        /// 获取出库口
        /// </summary>
        /// <param name="edm"></param>
        /// <returns></returns>
        public static string GetOut(dbModel edm, string last_toplace)
        {
            if (!string.IsNullOrEmpty(last_toplace))
            {//增加此次循环的校验 [EditBy shaocx,2022-10-15]
                return DoGetOut(last_toplace);
            }
            //判断上一个出库任务是哪个出库口
            var queryTaskType = ((int)TaskTypeEnum.常规出库).ToString();
            var task = edm.Task_Queue.Where(x => x.TaskType == queryTaskType).OrderByDescending(x => x.CreateDate).FirstOrDefault();
            if (task != null)
            {
                var toPlace = task.ToPositionName;
                return DoGetOut(toPlace);
            }
            return "1003";//默认二号口
        }
 
        private static string DoGetOut(string toPlace)
        {
            if (toPlace == "1003")
            {
                return "1008";//三号口
            }
            else if (toPlace == "1008")
            {
                return "1003";//二号口
            }
            else if (toPlace == "1001")
            {
                return "1003";//二号口
            }
            return "1003";//默认二号口
        }
 
 
        /// <summary>
        /// 用 在库天数过滤数据
        /// </summary>
        /// <param name="takeStockList"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public static List<vvBase_ProductPosition> FiltervvBase_ProductPositionByInStoreDays(List<vvBase_ProductPosition> takeStockList, stockSearchEntity page
            , ref string errMsg)
        {
            if (!string.IsNullOrEmpty(page.inStoreDaysFilterTypeValue) && string.IsNullOrEmpty(page.inStoreDaysValue))
            {
                errMsg = "在库天数值请输入数字!";
                return takeStockList;
            }
            if (string.IsNullOrEmpty(page.inStoreDaysFilterTypeValue) && !string.IsNullOrEmpty(page.inStoreDaysValue))
            {
                errMsg = "请选择在库天数选择条件!";
                return takeStockList;
            }
            //判断在库天数,增加在库天数查询条件
            if (!string.IsNullOrEmpty(page.inStoreDaysFilterTypeValue) && !string.IsNullOrEmpty(page.inStoreDaysValue))
            {
                int i_inStoreDaysValue = 0;
                var isInt = int.TryParse(page.inStoreDaysValue, out i_inStoreDaysValue);
                if (isInt)
                {
                    int i_inStoreDaysFilterTypeValue = 0;
                    isInt = int.TryParse(page.inStoreDaysFilterTypeValue, out i_inStoreDaysFilterTypeValue);
                    if (isInt)
                    {
                        NumberFilterType _NumberFilterType = (NumberFilterType)Enum.Parse(typeof(NumberFilterType), i_inStoreDaysFilterTypeValue.ToString());
                        switch (_NumberFilterType)
                        {
                            case NumberFilterType.大于:
                                takeStockList = takeStockList.Where(x => x.InStoreDays > i_inStoreDaysValue).ToList();
                                break;
                            case NumberFilterType.大于等于:
                                takeStockList = takeStockList.Where(x => x.InStoreDays >= i_inStoreDaysValue).ToList();
                                break;
                            case NumberFilterType.等于:
                                takeStockList = takeStockList.Where(x => x.InStoreDays == i_inStoreDaysValue).ToList();
                                break;
                            case NumberFilterType.小于:
                                takeStockList = takeStockList.Where(x => x.InStoreDays < i_inStoreDaysValue).ToList();
                                break;
                            case NumberFilterType.小于等于:
                                takeStockList = takeStockList.Where(x => x.InStoreDays <= i_inStoreDaysValue).ToList();
                                break;
                        }
                    }
                    else
                    {
                        errMsg = "请选择在库天数选择条件!";
                    }
                }
                else
                {
                    errMsg = "在库天数值请输入数字!";
                }
            }
            return takeStockList;
        }
 
 
        /// <summary>
        /// 用 在库天数过滤数据
        /// </summary>
        /// <param name="takeStockList"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public static List<vvBase_ProductPosition_all> FiltervvBase_ProductPositionByInStoreDays(List<vvBase_ProductPosition_all> takeStockList, stockSearchEntity page
            , ref string errMsg)
        {
            if (!string.IsNullOrEmpty(page.inStoreDaysFilterTypeValue) && string.IsNullOrEmpty(page.inStoreDaysValue))
            {
                errMsg = "在库天数值请输入数字!";
                return takeStockList;
            }
            if (string.IsNullOrEmpty(page.inStoreDaysFilterTypeValue) && !string.IsNullOrEmpty(page.inStoreDaysValue))
            {
                errMsg = "请选择在库天数选择条件!";
                return takeStockList;
            }
            //判断在库天数,增加在库天数查询条件
            if (!string.IsNullOrEmpty(page.inStoreDaysFilterTypeValue) && !string.IsNullOrEmpty(page.inStoreDaysValue))
            {
                int i_inStoreDaysValue = 0;
                var isInt = int.TryParse(page.inStoreDaysValue, out i_inStoreDaysValue);
                if (isInt)
                {
                    int i_inStoreDaysFilterTypeValue = 0;
                    isInt = int.TryParse(page.inStoreDaysFilterTypeValue, out i_inStoreDaysFilterTypeValue);
                    if (isInt)
                    {
                        NumberFilterType _NumberFilterType = (NumberFilterType)Enum.Parse(typeof(NumberFilterType), i_inStoreDaysFilterTypeValue.ToString());
                        switch (_NumberFilterType)
                        {
                            case NumberFilterType.大于:
                                takeStockList = takeStockList.Where(x => x.InStoreDays > i_inStoreDaysValue).ToList();
                                break;
                            case NumberFilterType.大于等于:
                                takeStockList = takeStockList.Where(x => x.InStoreDays >= i_inStoreDaysValue).ToList();
                                break;
                            case NumberFilterType.等于:
                                takeStockList = takeStockList.Where(x => x.InStoreDays == i_inStoreDaysValue).ToList();
                                break;
                            case NumberFilterType.小于:
                                takeStockList = takeStockList.Where(x => x.InStoreDays < i_inStoreDaysValue).ToList();
                                break;
                            case NumberFilterType.小于等于:
                                takeStockList = takeStockList.Where(x => x.InStoreDays <= i_inStoreDaysValue).ToList();
                                break;
                        }
                    }
                    else
                    {
                        errMsg = "请选择在库天数选择条件!";
                    }
                }
                else
                {
                    errMsg = "在库天数值请输入数字!";
                }
            }
            return takeStockList;
        }
 
 
        /// <summary>
        /// 库存明细计算在库天数
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static List<vvBase_ProductPosition> CalcInStoreDays(List<vvBase_ProductPosition> list)
        {
            if (list != null && list.Count > 0)
            {
                var nowDate = DateTime.Now;
                foreach (var item in list)
                {
                    TimeSpan ts = nowDate - item.InStorageDate;
                    item.InStoreDays = ts.Days;
                }
                return list;
            }
            else
            {
                return new List<vvBase_ProductPosition>();
            }
        }
 
        /// <summary>
        /// 库存明细计算在库天数
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static List<vvBase_ProductPosition_all> CalcInStoreDays(List<vvBase_ProductPosition_all> list)
        {
            if (list != null && list.Count > 0)
            {
                var nowDate = DateTime.Now;
                foreach (var item in list)
                {
                    TimeSpan ts = nowDate - item.InStorageDate;
                    item.InStoreDays = ts.Days;
                }
                return list;
            }
            else
            {
                return new List<vvBase_ProductPosition_all>();
            }
        }
 
 
 
        /// <summary>
        /// 通过物料编码获取物料信息
        /// </summary>
        /// <param name="mod"></param>
        /// <param name="productCode"></param>
        public static Base_ProductInfo GetProductInfoByCode(dbModel mod, string productCode)
        {
            if (string.IsNullOrEmpty(productCode))
                return null;
            Base_ProductInfo modify = mod.Base_ProductInfo.Where(x => x.ProductCode == productCode).FirstOrDefault();
            return modify;
        }
 
        /// <summary>
        /// 配置对象
        /// </summary>
        /// <param name="sapGi"></param>
        /// <param name="resultSap"></param>
        public static void SetSapSendInfoByGIMaterialEntity(dbModel mod, SapSendInfo sapGi, GIMaterialEntity item)
        {
            sapGi.Batch = item.Batch;
            //sapGi.FromStorageLocation = item.FromStorageLocation;
            sapGi.MaterialCode = item.MaterialCode;
            sapGi.Plant = item.Batch;
            sapGi.Quantity = item.Quantity;
            sapGi.SType = item.Batch;
            sapGi.SalesOrder = item.Batch;
            sapGi.SalesOrderItem = item.Batch;
            sapGi.SerialNo = item.Batch;
            //sapGi.ToStorageLocation = item.ToStorageLocation;
            sapGi.Unit = item.Unit;
            sapGi.Vendor = item.Batch;
            sapGi.WorkOrder = item.Batch;
            sapGi.StorageLocation = item.Batch;
            var product = GetProductInfoByCode(mod, sapGi.MaterialCode);
            if (product != null) sapGi.MaterialName = product.ProductName;
        }
        /// <summary>
        /// 配置对象
        /// </summary>
        /// <param name="sapGi"></param>
        /// <param name="resultSap"></param>
        public static void SetSapSendInfoByGRMaterialEntity(dbModel mod, SapSendInfo sapGi, List<GRMaterialEntity> resultSap)
        {
            var item = resultSap[0];
            //sapGi.Batch = item.Batch;
            //sapGi.FromStorageLocation = item.FromStorageLocation;
            sapGi.MaterialCode = item.MaterialCode;
            //sapGi.Plant = item.Batch;
            sapGi.Quantity = item.Quantity;
            //sapGi.SType = item.Batch;
            // sapGi.SalesOrder = item.Batch;
            //sapGi.SalesOrderItem = item.Batch;
            //sapGi.SerialNo = item.Batch;
            //sapGi.ToStorageLocation = item.ToStorageLocation;
            //sapGi.Unit = item.Unit;
            //sapGi.Vendor = item.Batch;
            //sapGi.WorkOrder = item.Batch;
            //sapGi.StorageLocation = item.Batch;
            var product = GetProductInfoByCode(mod, sapGi.MaterialCode);
            if (product != null) sapGi.MaterialName = product.ProductName;
        }
        /// <summary>
        /// 配置对象
        /// </summary>
        /// <param name="sapGi"></param>
        /// <param name="resultSap"></param>
        public static void SetSapSendInfoByMovMaterialEntity(dbModel mod, SapSendInfo sapGi, List<MovMaterialEntity> moveMaterials)
        {
            var item = moveMaterials[0];
            sapGi.Batch = item.Batch;
            sapGi.FromStorageLocation = item.FromStorageLocation;
            sapGi.MaterialCode = item.MaterialCode;
            sapGi.Plant = item.Batch;
            sapGi.Quantity = item.Quantity;
            sapGi.SType = item.Batch;
            sapGi.SalesOrder = item.Batch;
            sapGi.SalesOrderItem = item.Batch;
            sapGi.SerialNo = item.Batch;
            sapGi.ToStorageLocation = item.ToStorageLocation;
            sapGi.Unit = item.Unit;
            sapGi.Vendor = item.Batch;
            sapGi.WorkOrder = item.Batch;
            sapGi.StorageLocation = item.Batch;
            var product = GetProductInfoByCode(mod, sapGi.MaterialCode);
            if (product != null) sapGi.MaterialName = product.ProductName;
        }
 
 
 
 
        /// <summary>
        /// 寻找空库位时,增加判断是否有库存数据的筛选 【EditBy shaocx,2022-01-27】
        /// </summary>
        /// <param name="finallyResultSql"></param>
        /// <param name="edm"></param>
        /// <returns></returns>
        public static List<Base_Position> FitlterFindEmptyPlace(List<Base_Position> finallyResultSql, int areaCode, dbModel edm)
        {
            if (finallyResultSql == null || finallyResultSql.Count == 0)
            {
                return finallyResultSql;
            }
 
            //排除出有库存的,优化 【EditBy shaocx,2022-09-21】
            finallyResultSql = FilterProductPositionForFindEmptyPosition(finallyResultSql, edm);
 
 
            //排除掉有任务占用的情况,优化  【EditBy shaocx,2022-09-21】
            finallyResultSql = FilterPositionTakeDoingTask(finallyResultSql, edm, areaCode);
 
            //原先的代码,效率较慢,使用上面的优化代码  【EditBy shaocx,2022-09-21】
            /*
            foreach (var place in finallyResultSql)
            {
                num = edm.Base_ProductPosition.Where(x => x.PositionName == place.PositionName).Count();
                if (num > 0)
                {//说明有库存,不允许再入东西了
                    continue;
                }
                else
                {//空闲库位
                    //继续判断该库位是否被未结束的任务占用 【EditBy shaocx,2022-03-11】
                    //下面这种方式满足条件也查询不到数据,应该是拼接字符串+的问题 【EditBy shaocx,2022-03-17】
                    //num = edm.Task_Queue.Where(x =>
                    //     ((x.DoCount + '-' + x.ToPositionName) == place.PositionName || (x.DoCount + '-' + x.FromPositionName) == place.PositionName)
                    //     && (x.TaskStatus != "手工处理异常" && x.TaskStatus != "完成" && x.TaskStatus != "任务异常" && x.TaskStatus != "确认异常")
                    //     ).Count();
                    var queryPositionName = place.PositionName.Substring(2);
                    num = GetDoingTaskCountByPosition(edm, areaCode, queryPositionName);
                    //num = edm.Task_Queue.Where(x =>
                    //      x.DoCount == areaCode && (x.ToPositionName == queryPositionName || x.FromPositionName == queryPositionName) &&
                    //     (x.TaskStatus != "手工处理异常" && x.TaskStatus != "完成" && x.TaskStatus != "任务异常" && x.TaskStatus != "确认异常")
                    //    ).Count();
 
                    if (num > 0)
                    {//说明有未结束的任务占用,不允许再入东西了
                        continue;
                    }
 
                    new_finallyResultSql.Add(place);
                }
            }
            //*/
            return finallyResultSql;
        }
 
        /// <summary>
        /// 过滤掉 已经有库存的库位
        /// </summary>
        /// <param name="finallyResultSql"></param>
        /// <param name="edm"></param>
        /// <returns></returns>
        public static List<Base_Position> FilterProductPositionForFindEmptyPosition(List<Base_Position> finallyResultSql, dbModel edm)
        {
            var allStoreList = edm.Base_ProductPosition.Where(x => x.PositionName != SysGlole.PositionName_SHZCQ).ToList();//目前所有的库存
            if (allStoreList != null && allStoreList.Count > 0)
            {
                List<string> positionNameList = allStoreList.Select(x => x.PositionName).ToList();
                finallyResultSql = finallyResultSql.Where(x => !positionNameList.Contains(x.PositionName)).ToList();
            }
            return finallyResultSql;
        }
 
 
        /// <summary>
        /// 过滤 库位目前正在被占用的任务,把任务占用的排除掉
        /// </summary>
        /// <param name="edm"></param>
        /// <param name="areaCode"></param>
        /// <param name="queryPositionName"></param>
        /// <returns></returns>
        public static List<vvBase_ProductPosition> FilterPositionTakeDoingTask(List<vvBase_ProductPosition> finallyResultSql, dbModel edm, int areaCode)
        {
            var list = GetDoingTaskList(edm, areaCode);
            var fromPositionNameList = list.Select(x => x.DoCount + "-" + x.FromPositionName).ToList();//任务占用的起点集合
            var toPositionNameList = list.Select(x => x.DoCount + "-" + x.ToPositionName).ToList();//任务占用的目标集合
 
 
 
            finallyResultSql = finallyResultSql.Where(x => !fromPositionNameList.Contains(x.PositionName)).ToList();
            finallyResultSql = finallyResultSql.Where(x => !toPositionNameList.Contains(x.PositionName)).ToList();
            return finallyResultSql;
        }
        /// <summary>
        /// 过滤 库位目前正在被占用的任务,把任务占用的排除掉
        /// </summary>
        /// <param name="edm"></param>
        /// <param name="areaCode"></param>
        /// <param name="queryPositionName"></param>
        /// <returns></returns>
        public static List<Base_Position> FilterPositionTakeDoingTask(List<Base_Position> finallyResultSql, dbModel edm, int areaCode)
        {
            var list = GetDoingTaskList(edm, areaCode);
            var fromPositionNameList = list.Select(x => x.DoCount + "-" + x.FromPositionName).ToList();//任务占用的起点集合
            var toPositionNameList = list.Select(x => x.DoCount + "-" + x.ToPositionName).ToList();//任务占用的目标集合
 
 
 
            finallyResultSql = finallyResultSql.Where(x => !fromPositionNameList.Contains(x.PositionName)).ToList();
            finallyResultSql = finallyResultSql.Where(x => !toPositionNameList.Contains(x.PositionName)).ToList();
            return finallyResultSql;
        }
 
        /// <summary>
        /// 过滤库位-把任务占用的排除掉 [EditBy shaocx,2022-05-31]
        /// </summary>
        /// <param name="edm"></param>
        /// <param name="positionList"></param>
        /// <returns></returns>
        public static List<Base_Position> FilterPositionTakeDoingTask(dbModel edm, List<Base_Position> positionList)
        {
            List<Base_Position> positionList_01 = positionList.Where(x => x.ShelveCode == "01").ToList();
            List<Base_Position> positionList_02 = positionList.Where(x => x.ShelveCode == "02").ToList();
 
            positionList_01 = FilterPositionTakeDoingTask(positionList_01, edm, 1);
            positionList_02 = FilterPositionTakeDoingTask(positionList_02, edm, 2);
 
            List<Base_Position> newList = new List<Base_Position>();
            newList.AddRange(positionList_01);
            newList.AddRange(positionList_02);
            return newList;
            //优化下面的逻辑
            //List<Base_Position> new_positionList = new List<Base_Position>();
            //if (positionList != null && positionList.Count > 0)
            //{
            //    foreach (var item in positionList)
            //    {
            //        var queryPositionName = item.PositionName.Substring(2);//PositionName值:1-01-15-05
            //        var num = GetDoingTaskCountByPosition(edm, Convert.ToInt32(item.AreaCode), queryPositionName);
            //        if (num > 0)
            //        {//说明有未结束的任务占用,不允许再入东西了
 
            //        }
            //        else
            //        {
            //            new_positionList.Add(item);
            //        }
            //    }
            //}
            //return new_positionList;
        }
 
 
 
 
        /// <summary>
        /// 获取目前正在被占用的任务列表
        /// </summary>
        /// <param name="edm"></param>
        /// <param name="areaCode">库区</param>
        /// <returns></returns>
        public static List<Task_Queue> GetDoingTaskList(dbModel edm, int areaCode)
        {
            var list = edm.Task_Queue.Where(x =>
                        x.DoCount == areaCode &&
                       (x.TaskStatus != "手工处理异常" && x.TaskStatus != "完成" && x.TaskStatus != "任务异常" && x.TaskStatus != "确认异常")
                      ).ToList();
            if (list == null) return new List<Task_Queue>();
            else return list;
        }
 
        /// <summary>
        /// 获取目前正在被占用的任务列表
        /// </summary>
        /// <param name="edm"></param>
        /// <param name="areaCode">库区</param>
        /// <param name="positionName">库位</param>
        /// <returns></returns>
        public static List<Task_Queue> GetDoingTaskList(dbModel edm, int areaCode, string positionName)
        {
            var list = edm.Task_Queue.Where(x =>
                         x.DoCount == areaCode &&
                        (x.ToPositionName == positionName || x.FromPositionName == positionName) &&
                       (x.TaskStatus != "手工处理异常" && x.TaskStatus != "完成" && x.TaskStatus != "任务异常" && x.TaskStatus != "确认异常")
                      ).ToList();
            if (list == null) return new List<Task_Queue>();
            else return list;
        }
        /// <summary>
        /// 获取目前某个托盘正在被占用的任务列表
        /// </summary>
        /// <param name="edm"></param>
        /// <param name="areaCode">库区</param>
        /// <param name="positionName">库位</param>
        /// <returns></returns>
        public static List<Task_Queue> GetDoingTaskListForPlateCode(dbModel edm, string plateCode)
        {
            var list = edm.Task_Queue.Where(x =>
                         x.PlateCode == plateCode &&
                       (x.TaskStatus != "手工处理异常" && x.TaskStatus != "完成" && x.TaskStatus != "任务异常" && x.TaskStatus != "确认异常")
                      ).ToList();
            if (list == null) return new List<Task_Queue>();
            else return list;
        }
 
        /// <summary>
        /// 获取目前某个托盘正在被占用的任务列表
        /// </summary>
        /// <param name="edm"></param>
        /// <param name="areaCode">库区</param>
        /// <param name="positionName">库位</param>
        /// <returns></returns>
        public static List<Purchase_Shelve> GetDoingShelveTaskListForPlateCode(dbModel edm, string plateCode)
        {
            var list = edm.Purchase_Shelve.Where(x =>
                         x.PlateCode == plateCode &&
                       (x.OnShelveStatus == "待上架"
                       || x.OnShelveStatus == "上架中"
                       )
                      ).ToList();
            if (list == null) return new List<Purchase_Shelve>();
            else return list;
        }
 
        /// <summary>
        /// 获取目前正在被占用的任务列表
        /// </summary>
        /// <param name="edm"></param>
        /// <param name="areaCode">库区</param>
        /// <param name="_taskType">任务类型</param>
        /// <returns></returns>
        public static List<Task_Queue> GetDoingTaskList(dbModel edm, int areaCode, TaskTypeEnum _taskType)
        {
            var queryTaskType = ((int)_taskType).ToString();
            var list = edm.Task_Queue.Where(x =>
                        x.DoCount == areaCode &&
                        x.TaskType == queryTaskType
                        &&
                       (x.TaskStatus != "手工处理异常" && x.TaskStatus != "完成" && x.TaskStatus != "任务异常" && x.TaskStatus != "确认异常")
                      ).ToList();
            if (list == null) return new List<Task_Queue>();
            else return list;
        }
 
 
 
        /// <summary>
        /// 获取指定物料编码的当前库存数
        /// </summary>
        /// <param name="edm"></param>
        /// <param name="productCode">物料编码</param>
        /// <param name="positionName">库位号</param>
        /// <param name="cghw_ProductStorage">常规货位的库存数</param>
        /// <param name="zcg_ProductStorage">暂存区的库存数</param>
        /// <param name="dqkw_ProductStorage">当前库位的库存数</param>
        /// <param name="trackNumber">当前库位的跟踪号</param>
        /// <param name="dqkw_trackNumber_ProductStorage">当前库位的跟踪号库存数</param>
        public static void GetCurrentProductStorage(dbModel edm, string productCode, string positionName, out decimal cghw_ProductStorage, out  decimal zcg_ProductStorage, out  decimal dqkw_ProductStorage
            , string trackNumber, out  decimal dqkw_trackNumber_ProductStorage)
        {
            cghw_ProductStorage = 0;
            zcg_ProductStorage = 0;
            dqkw_ProductStorage = 0;
            dqkw_trackNumber_ProductStorage = 0;
            var storeList = edm.vvBase_ProductPosition.Where(x => x.ProductCode == productCode && x.ProductStorage > 0).ToList();
            if (storeList != null && storeList.Count > 0)
            {
                //常规货位的库存
                var queryPositionType = (int)PositionType.常规货位;
                cghw_ProductStorage = (decimal)storeList.Where(x => x.PositionType == queryPositionType).Sum(x => x.ProductStorage);
 
                dqkw_ProductStorage = (decimal)storeList.Where(x => x.PositionType == queryPositionType && x.PositionName == positionName).Sum(x => x.ProductStorage);
                if (!string.IsNullOrEmpty(trackNumber))
                {
                    dqkw_trackNumber_ProductStorage = (decimal)storeList.Where(x => x.PositionType == queryPositionType && x.PositionName == positionName && x.ExtendField04 == trackNumber).Sum(x => x.ProductStorage);
                }
 
                queryPositionType = (int)PositionType.暂存区;
                zcg_ProductStorage = (decimal)storeList.Where(x => x.PositionType == queryPositionType).Sum(x => x.ProductStorage);
            }
        }
 
 
        #region 空库位统计
 
        /// <summary>
        /// 巷道1空库位统计
        /// </summary>
        /// <param name="mod"></param>
        /// <param name="emtyps"></param>
        public static void EmptyPositionTongJiForArea1(dbModel mod, ref List<emtypPosition> emtyps)
        {
            //巷道1空库位
            List<Base_Position> emptyPositionArea1 = mod.Base_Position.Where(x => x.AreaCode == "1" && x.Enable == 1 && x.IsFreeze == 0 && x.IsLocked == 0).ToList();
            List<Base_Position> allPositionArea1 = mod.Base_Position.Where(x => x.AreaCode == "1" && x.Enable == 1).ToList();//巷道1 总库位数
            //过滤库位-把任务占用的排除掉 [EditBy shaocx,2022-05-31]
            emptyPositionArea1 = BussinessExtension.FilterPositionTakeDoingTask(mod, emptyPositionArea1);
 
 
            List<Base_Position> emptyPositionArea1_height = emptyPositionArea1.Where(x => x.RowCode == "01" || x.RowCode == "11").ToList();
            List<Base_Position> allPositionArea1_height = allPositionArea1.Where(x => x.RowCode == "01" || x.RowCode == "11").ToList();
 
            emtypPosition emptyHigh = new emtypPosition();
            emptyHigh.name = "巷道1小托盘 [1到28列],1,11层 (975-1850)";
            emptyHigh.quantity = emptyPositionArea1_height.Count;
            emptyHigh.all_quantity = allPositionArea1_height.Count;
            emtypPosition emptyLow = new emtypPosition();
            emptyLow.name = "巷道1小托盘 [1到28列],2到10层 (0-975)";
            emptyLow.quantity = emptyPositionArea1.Count - emptyPositionArea1_height.Count;
            emptyLow.all_quantity = allPositionArea1.Count - allPositionArea1_height.Count;
            emtyps.Add(emptyHigh);
            emtyps.Add(emptyLow);
        }
 
        ///// <summary>
        ///// 巷道2小库位 空库位统计
        ///// </summary>
        ///// <param name="mod"></param>
        ///// <param name="emtyps"></param>
        //public static void EmptyPositionTongJiForArea2_Small(dbModel mod, ref List<emtypPosition> emtyps)
        //{
        //    string row2Sql = "select * from [dbo].[" + "Base_Position" + "] where AreaCode='2'and ColumnCode>1 and ColumnCode<10 and Enable=" + (int)usable.可用;//isFreeze    是否有货   先拿这个字段用 
 
        //    List<Base_Position> allPositionArea2_small = mod.Database.SqlQuery<Base_Position>(row2Sql).ToList();//所有空库位
 
        //    List<Base_Position> emptyPositionArea2_small = allPositionArea2_small.Where(x => x.IsLocked == (int)IsLockedEnum.未锁定 && x.IsFreeze == (int)IsFreezeEnum.无货).ToList();
        //    //过滤库位-把任务占用的排除掉 [EditBy shaocx,2022-05-31]
        //    emptyPositionArea2_small = BussinessExtension.FilterPositionTakeDoingTask(mod, emptyPositionArea2_small);
 
        //    List<Base_Position> emptyPositionArea2_small_height2 = emptyPositionArea2_small.Where(x => x.RowCode == "03" || x.RowCode == "05" || x.RowCode == "07" || x.RowCode == "09" || x.RowCode == "10").ToList();
        //    List<Base_Position> allPositionArea2_small_height2 = allPositionArea2_small.Where(x => x.RowCode == "03" || x.RowCode == "05" || x.RowCode == "07" || x.RowCode == "09" || x.RowCode == "10").ToList();
        //    List<Base_Position> emptyPositionArea2_small_height3 = emptyPositionArea2_small.Where(x => x.RowCode == "01" || x.RowCode == "11").ToList();
        //    List<Base_Position> allPositionArea2_small_height3 = allPositionArea2_small.Where(x => x.RowCode == "01" || x.RowCode == "11").ToList();
        //    emtypPosition empty1 = new emtypPosition();
        //    empty1.name = "巷道2小托盘2,4,6,8层(0-750)";
        //    empty1.quantity = emptyPositionArea2_small.Count - emptyPositionArea2_small_height2.Count - emptyPositionArea2_small_height3.Count;
        //    empty1.all_quantity = allPositionArea2_small.Count - allPositionArea2_small_height2.Count - allPositionArea2_small_height3.Count;
        //    emtypPosition empty2 = new emtypPosition();
        //    empty2.name = "巷道2小托盘3,5,7,9,10层(750-1150)";
        //    empty2.quantity = emptyPositionArea2_small_height2.Count;
        //    empty2.all_quantity = allPositionArea2_small_height2.Count;
        //    emtypPosition empty3 = new emtypPosition();
        //    empty3.name = "巷道2小托盘1,11层(1150-1350)";
        //    empty3.quantity = emptyPositionArea2_small_height3.Count;
        //    empty3.all_quantity = allPositionArea2_small_height3.Count;
        //    emtyps.Add(empty3);
        //    emtyps.Add(empty2);
        //    emtyps.Add(empty1);
        //}
 
        /// <summary>
        /// 巷道2小库位 空库位统计
        /// </summary>
        /// <param name="mod"></param>
        /// <param name="emtyps"></param>
        public static void EmptyPositionTongJiForArea2_Small(dbModel mod, ref List<emtypPosition> emtyps)
        {
            string row2Sql = "select * from [dbo].[" + "Base_Position" + "] where AreaCode='2'and ColumnCode>1  and ColumnCode>1 and ColumnCode<10 and (RowCode>=1 AND RowCode<=11 )  and Enable=" + (int)usable.可用;//isFreeze    是否有货   先拿这个字段用 
 
            List<Base_Position> allPositionArea2_small = mod.Database.SqlQuery<Base_Position>(row2Sql).ToList();//所有空库位
 
            List<Base_Position> emptyPositionArea2_small = allPositionArea2_small.Where(x => x.IsLocked == (int)IsLockedEnum.未锁定 && x.IsFreeze == (int)IsFreezeEnum.无货).ToList();
            //过滤库位-把任务占用的排除掉 [EditBy shaocx,2022-05-31]
            emptyPositionArea2_small = BussinessExtension.FilterPositionTakeDoingTask(mod, emptyPositionArea2_small);
 
 
            emtypPosition empty1 = new emtypPosition();
            empty1.name = "巷道2小托盘 [2到9列],1到11层 (0-1150)";
            empty1.quantity = emptyPositionArea2_small.Count;
            empty1.all_quantity = allPositionArea2_small.Count;
 
            emtyps.Add(empty1);
        }
 
        /// <summary>
        /// 巷道2大库位 空库位统计
        /// </summary>
        /// <param name="mod"></param>
        /// <param name="emtyps"></param>
        public static void EmptyPositionTongJiForArea2_Big(dbModel mod, ref List<emtypPosition> emtyps)
        {
            //巷道2大库位-最高库位
            string row2SqlBig4 = "select * from [dbo].[" + "Base_Position" + "] where AreaCode='2' and Enable=" + (int)usable.可用
                     + " and  (ColumnCode<2 or ColumnCode>9) and (RowCode=1  or RowCode=13 or RowCode=14 ) ";//isFreeze    是否有货   先拿这个字段用 
 
            List<Base_Position> allPositionArea2_big4 = mod.Database.SqlQuery<Base_Position>(row2SqlBig4).ToList();
            List<Base_Position> emptyPositionArea2_big4 = allPositionArea2_big4.Where(x => x.IsLocked == (int)IsLockedEnum.未锁定 && x.IsFreeze == (int)IsFreezeEnum.无货).ToList();
            //过滤库位-把任务占用的排除掉 [EditBy shaocx,2022-05-31]
            emptyPositionArea2_big4 = BussinessExtension.FilterPositionTakeDoingTask(mod, emptyPositionArea2_big4);
 
            //第3高
            string row2SqlBig3 = "select * from [dbo].[" + "Base_Position" + "] where AreaCode='2' and Enable=" + (int)usable.可用
                     + " and  (ColumnCode<2 or ColumnCode>9) and ( RowCode=2  ) ";//isFreeze    是否有货   先拿这个字段用 
 
            List<Base_Position> allPositionArea2_big3 = mod.Database.SqlQuery<Base_Position>(row2SqlBig3).ToList();
            List<Base_Position> emptyPositionArea2_big3 = allPositionArea2_big3.Where(x => x.IsLocked == (int)IsLockedEnum.未锁定 && x.IsFreeze == (int)IsFreezeEnum.无货).ToList();
            //过滤库位-把任务占用的排除掉 [EditBy shaocx,2022-05-31]
            emptyPositionArea2_big3 = BussinessExtension.FilterPositionTakeDoingTask(mod, emptyPositionArea2_big3);
 
 
            //第2高
            string row2SqlBig2 = "select * from [dbo].[" + "Base_Position" + "] where AreaCode='2'  and Enable=" + (int)usable.可用
                       + " and  (ColumnCode>=10 and ColumnCode<=21) and (RowCode=12 ) ";//isFreeze    是否有货   先拿这个字段用 
 
            List<Base_Position> allPositionArea2_big2 = mod.Database.SqlQuery<Base_Position>(row2SqlBig2).ToList();
            List<Base_Position> emptyPositionArea2_big2 = allPositionArea2_big2.Where(x => x.IsLocked == (int)IsLockedEnum.未锁定 && x.IsFreeze == (int)IsFreezeEnum.无货).ToList();
            //过滤库位-把任务占用的排除掉 [EditBy shaocx,2022-05-31]
            emptyPositionArea2_big2 = BussinessExtension.FilterPositionTakeDoingTask(mod, emptyPositionArea2_big2);
 
 
            //最低库位
            string row2SqlBig1 = "select * from [dbo].[" + "Base_Position" + "] where AreaCode='2' and Enable=" + (int)usable.可用
                       + " and  ( ( (ColumnCode=1 and (RowCode>=3 and RowCode<=12) ) OR ( ColumnCode>=10 and ColumnCode<=21 and (RowCode>=3 and RowCode<=11)) ) )  ";//isFreeze    是否有货   先拿这个字段用 
 
            List<Base_Position> allPositionArea2_big1 = mod.Database.SqlQuery<Base_Position>(row2SqlBig1).ToList();
            List<Base_Position> emptyPositionArea2_big1 = allPositionArea2_big1.Where(x => x.IsLocked == (int)IsLockedEnum.未锁定 && x.IsFreeze == (int)IsFreezeEnum.无货).ToList();
            //过滤库位-把任务占用的排除掉 [EditBy shaocx,2022-05-31]
            emptyPositionArea2_big1 = BussinessExtension.FilterPositionTakeDoingTask(mod, emptyPositionArea2_big1);
 
            emtypPosition emptyBig1 = new emtypPosition();
            emptyBig1.name = "巷道2大托盘 [10到21列,3到11层],[第1列12层] (0-600)";
            emptyBig1.quantity = emptyPositionArea2_big1.Count;
            emptyBig1.all_quantity = allPositionArea2_big1.Count;
            emtypPosition emptyBig2 = new emtypPosition();
            emptyBig2.name = "巷道2大托盘 [10到21列],12层 (600-750)";
            emptyBig2.quantity = emptyPositionArea2_big2.Count;
            emptyBig2.all_quantity = allPositionArea2_big2.Count;
            emtypPosition emptyBig3 = new emtypPosition();
            emptyBig3.name = "巷道2大托盘 [10到21列],2层 (750-1150)";
            emptyBig3.quantity = emptyPositionArea2_big3.Count;
            emptyBig3.all_quantity = allPositionArea2_big3.Count;
            emtypPosition emptyBig4 = new emtypPosition();
            emptyBig4.name = "巷道2大托盘 [第1列],[10到21列],1,13,14层 (1150-1350)";
            emptyBig4.quantity = emptyPositionArea2_big4.Count;
            emptyBig4.all_quantity = allPositionArea2_big4.Count;
 
 
            emtyps.Add(emptyBig1);
            emtyps.Add(emptyBig2);
            emtyps.Add(emptyBig3);
            emtyps.Add(emptyBig4);
        }
 
        #endregion
    }
}