XiangdianStockerExcute.cs 64.9 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HHECS.Bll;
using HHECS.Model;
using HHECS.OPC;
using S7.Net;

namespace HHECS.Common
{
    /// <summary>
    /// todo:湘电单叉双伸位堆垛机处理函数
    /// </summary>
    public class XiangdianStockerExcute : IStockerExcute
    {

        #region 接口实现 

        /// <summary>
        /// 标记这个堆垛机处理类所处理的设备类型
        /// </summary>
        public EquipmentType EquipmentType { get; set; }

        /// <summary>
        /// 标记堆垛机是否可以执行任务
        /// 由于我们是抓一次数据然后处理一次数据
        /// </summary>
        public bool CanExcute { get; set; }

        /// <summary>
        /// 记录堆垛机当前所在的列
        /// </summary>
        public int CurrentColumn { get; private set; }

        public BllResult Excute(List<Equipment> stockers, List<Plc> plcs)
        {
            try
            {
                foreach (var stocker in stockers)
                {
                    //对于每一个堆垛机而言,每一次处理开始都是可执行的
                    //CanExcute = true;
                    //一般根据堆垛机的位置来决定是否执行入库任务 hack:这里默认堆垛机在站台旁时列为0
                    CurrentColumn = int.Parse(stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "CurrentColumn").Value);
                    //if (CurrentColumn == 0)
                    //{
                    //    //先处理入库
                    //    ExcuteTaskIn(stocker, plcs.Find(t => t.IP == stocker.IP));
                    //}
                    ////处理出库
                    //ExcuteTaskOut(stocker, plcs.Find(t => t.IP == stocker.IP));
                    ////先处理入库
                    //ExcuteTaskIn(stocker, plcs.Find(t => t.IP == stocker.IP));
                    ////处理巷道内移库(巷道间的移库交给出库处理,这个处理同巷道内的移库)
                    //ExcuteTaskTransferInRoadway(stocker, plcs.Find(t => t.IP == stocker.IP));
                    ////处理堆垛机任务完成
                    //ExcuteTaskComplete(stocker, plcs.Find(t => t.IP == stocker.IP));
                    ////任务下发错误处理
                    //ExcuteTaskError(stocker, plcs.Find(t => t.IP == stocker.IP));

                    ExcuteSingle(stocker, plcs.Find(t => t.IP == stocker.IP));
                    //心跳
                    Heartbeat(stocker, plcs.Find(t => t.IP == stocker.IP));

                }
                return BllResultFactory.Sucess();
            }
            catch (Exception ex)
            {
                Logger.Log($"堆垛机处理过程中出现异常:{ex.Message}", LogLevel.Exception);
                return BllResultFactory.Error($"堆垛机处理过程中出现异常:{ex.Message}");
            }
        }

        private void Heartbeat(Equipment stocker, Plc plc)
        {
            var prop = stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSHeartBeat");
            prop.Value = "1";
            var result = S7Helper.PlcSplitWrite(plc, new List<EquipmentProp>() { prop }, 20);
            if (!result.Success)
            {
                Logger.Log($"发送堆垛机{stocker.Name}心跳数据失败:{result.Msg}", LogLevel.Error);
            }
        }

        /// <summary>
        /// 以堆垛机状态驱动,重写堆垛机控制逻辑
        /// </summary>
        /// <param name="stocker"></param>
        /// <param name="plc"></param>
        /// <returns></returns>
        public BllResult ExcuteSingle(Equipment stocker, Plc plc)
        {
            //联机、无故障
            if (Validate(stocker).Success)
            {
                if (stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskExcuteStatus").Value == TaskExcuteStatus.任务执行中.GetIndexString())
                {
                    //任务执行中就return
                    return BllResultFactory.Sucess();
                }
                if (stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskExcuteStatus").Value == TaskExcuteStatus.任务中断_出错.GetIndexString())
                {
                    //由人工处理,一般为空出和重入
                    return BllResultFactory.Sucess();
                }
                if (stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskExcuteStatus").Value == TaskExcuteStatus.下发任务错误.GetIndexString())
                {
                    //由人工处理,需要重新下发任务
                    return BllResultFactory.Sucess();
                }

                #region 任务和货位
                //找出所有未完成的任务
                var tasksResult = AppSession.Bll.GetCommonModelByCondition<TaskEntity>($"where status < {TaskEntityStatus.任务完成}");
                if (!tasksResult.Success)
                {
                    //如果没有找到任务就直接返回
                    return BllResultFactory.Error(tasksResult.Msg);
                }
                //找出同巷道的库位
                var locationsResult = AppSession.Bll.GetAllLocations(null, null, null, null, stocker.RoadWay, null, null);
                if (!locationsResult.Success)
                {
                    return BllResultFactory.Error(locationsResult.Msg);
                }

                var tasks = tasksResult.Data;
                var locations = locationsResult.Data;
                #endregion

                //堆垛机待机情况下
                if (stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskExcuteStatus").Value == TaskExcuteStatus.待机.GetIndexString())
                {
                    //货叉任务待机时,可执行放和取任务,同时当执行完成时,交互后堆垛机会从任务完成更新为待机
                    if (stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1TaskFlag").Value == ForkTaskFlag.任务完成.GetIndexString())
                    {
                        return ClearWCSData(stocker, plc);
                    }
                    else if (stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1TaskFlag").Value == ForkTaskFlag.无任务.GetIndexString())
                    {
                        #region 优先处理重新下发的任务

                        var taskForResend = tasks.FirstOrDefault(t => t.FirstStatus == TaskEntityStatus.下发堆垛机库内取货任务.GetIndexInt() && t.LastStatus == TaskEntityStatus.下发堆垛机库内取货任务.GetIndexInt() && locations.Count(a => a.Code == t.SourceLocation) > 0 && t.SendAgain == 1);
                        if (taskForResend != null)
                        {
                            var locationForResend = locations.Find(t => t.Code == taskForResend.SourceLocation);
                            return ResendTask(stocker, plc, locationForResend, taskForResend,ForkTaskFlag.库内取货);
                        }

                        taskForResend = tasks.FirstOrDefault(t => t.FirstStatus == TaskEntityStatus.下发堆垛机库内放货任务.GetIndexInt() && t.LastStatus == TaskEntityStatus.下发堆垛机库内放货任务.GetIndexInt() && locations.Count(a => a.Code == t.DestinationLocation) > 0 && t.SendAgain == 1);
                        if (taskForResend != null)
                        {
                            var locationForResend = locations.Find(t => t.Code == taskForResend.DestinationLocation);
                            return ResendTask(stocker, plc, locationForResend, taskForResend,ForkTaskFlag.库内放货);
                        }

                        taskForResend = tasks.FirstOrDefault(t => t.FirstStatus == TaskEntityStatus.下发堆垛机库外取货任务.GetIndexInt() && t.LastStatus == TaskEntityStatus.下发堆垛机库外取货任务.GetIndexInt() && locations.Count(a => a.Code == t.DestinationLocation) > 0 && t.SendAgain == 1);
                        if (taskForResend != null)
                        {
                            return ResendTask(stocker, plc, null, taskForResend, ForkTaskFlag.库外入库);
                        }

                        taskForResend = tasks.FirstOrDefault(t => t.FirstStatus == TaskEntityStatus.下发堆垛机库外放货任务.GetIndexInt() && t.LastStatus == TaskEntityStatus.下发堆垛机库外放货任务.GetIndexInt() && locations.Count(a => a.Code == t.SourceLocation) > 0 && t.SendAgain == 1);
                        if (taskForResend != null)
                        {
                            return ResendTask(stocker, plc, null, taskForResend, ForkTaskFlag.库外出库);
                        }

                        #endregion
                        //判断堆垛机货叉上是不是有货,有货就只能接受放货任务,无货就可以接受取货任务
                        if (stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1ForkHasPallet").Value == "True")
                        {
                            //有货,则查找同巷道的取货任务完成状态的任务,同一个堆垛机,单叉情况下最多一条
                            //先找库内取货完成的任务
                            var task = tasks.FirstOrDefault(t => (t.FirstStatus == TaskEntityStatus.响应堆垛机库内取货任务完成.GetIndexInt() && t.LastStatus == TaskEntityStatus.响应堆垛机库内取货任务完成.GetIndexInt()) && locations.Count(a => a.Code == t.SourceLocation) > 0);
                            if (task == null)
                            {
                                //如果没有就找库外取货完成的任务
                                task = tasks.FirstOrDefault(t => (t.FirstStatus == TaskEntityStatus.响应堆垛机库外取货任务完成.GetIndexInt() && t.LastStatus == TaskEntityStatus.响应堆垛机库外取货任务完成.GetIndexInt()) && locations.Count(a => a.Code == t.DestinationLocation) > 0);
                            }
                            if (task == null)
                            {
                                //如果还是没有找到任务,那就说明的确没有这条任务,或是认为原因导致的,但此时又显示货叉有货,所以这里是有问题的,做个日志
                                Logger.Log($"堆垛机{stocker.Name}显示货叉上有货,但是没有对应的任务", LogLevel.Error);
                                return BllResultFactory.Error();
                            }
                            else
                            {
                                //判断任务类型
                                //巷道内移库
                                if (task.Type == TaskType.移库.GetIndexInt() && locations.Count(t => t.Code == task.DestinationLocation) > 0)
                                {
                                    //下发
                                    task.FirstStatus = TaskEntityStatus.下发堆垛机库内放货任务.GetIndexInt();
                                    task.LastStatus = TaskEntityStatus.下发堆垛机库内放货任务.GetIndexInt();
                                    var tempResult = AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                                    if (tempResult.Success)
                                    {
                                        var location = locations.Find(t => t.Code == task.DestinationLocation);
                                        BllResult sendResult = SendTaskToStocker(stocker, plc, ForkAction.货叉1, ForkTaskFlag.库内放货, location.RowIndex.ToString(), location.Line.ToString(), location.Layer.ToString(), "0", task.Id.ToString());
                                        if (sendResult.Success)
                                        {
                                            Logger.Log($"下发堆垛机{stocker.Name},任务:{task.Id},任务类型:{task.Type},货叉类型:{ForkTaskFlag.库内放货}成功", LogLevel.Success);
                                            return BllResultFactory.Sucess();
                                        }
                                        else
                                        {
                                            Logger.Log($"下发堆垛机{stocker.Name},任务:{task.Id},任务类型:{task.Type},货叉类型:{ForkTaskFlag.库内放货}失败:{sendResult.Msg};回滚任务{task.Id}状态。", LogLevel.Error);
                                            //回滚任务状态,记录日志
                                            task.FirstStatus = TaskEntityStatus.响应堆垛机库内取货任务完成.GetIndexInt();
                                            task.LastStatus = TaskEntityStatus.响应堆垛机库内取货任务完成.GetIndexInt();
                                            AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                                            return BllResultFactory.Error();
                                        }
                                    }
                                    else
                                    {
                                        Logger.Log($"下发堆垛机{stocker.Name}巷道内移库任务时,更新任务{task.Id}状态失败:{tempResult.Msg}", LogLevel.Error);
                                        return BllResultFactory.Error($"下发堆垛机{stocker.Name}巷道内移库任务时,更新任务{task.Id}状态失败:{tempResult.Msg}");
                                    }
                                }
                                else
                                {
                                    //除了巷道内移库之外,就是取货到站台,或从站台取货到库位了
                                    if (task.FirstStatus == TaskEntityStatus.响应堆垛机库内取货任务完成.GetIndexInt())
                                    {
                                        //库内取,那么库外放
                                        task.FirstStatus = TaskEntityStatus.下发堆垛机库外放货任务.GetIndexInt();
                                        task.LastStatus = TaskEntityStatus.下发堆垛机库外放货任务.GetIndexInt();
                                        var tempResult = AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                                        if (tempResult.Success)
                                        {
                                            BllResult sendResult = SendTaskToStocker(stocker, plc, ForkAction.货叉1, ForkTaskFlag.库外出库, "0", "0", "0", task.Station.ToString(), task.Id.ToString());
                                            if (sendResult.Success)
                                            {
                                                Logger.Log($"下发堆垛机{stocker.Name},任务:{task.Id},任务类型:{task.Type},货叉类型:{ForkTaskFlag.库外出库}成功", LogLevel.Success);
                                                return BllResultFactory.Sucess();
                                            }
                                            else
                                            {
                                                Logger.Log($"下发堆垛机{stocker.Name},任务:{task.Id},任务类型:{task.Type},货叉类型:{ForkTaskFlag.库外出库}失败:{sendResult.Msg};回滚任务{task.Id}状态。", LogLevel.Error);
                                                //回滚任务状态,记录日志
                                                task.FirstStatus = TaskEntityStatus.响应堆垛机库内取货任务完成.GetIndexInt();
                                                task.LastStatus = TaskEntityStatus.响应堆垛机库内取货任务完成.GetIndexInt();
                                                AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                                                return BllResultFactory.Error();
                                            }
                                        }
                                        else
                                        {
                                            Logger.Log($"下发堆垛机{stocker.Name}库外放货任务时,更新任务{task.Id}状态失败:{tempResult.Msg}", LogLevel.Error);
                                            return BllResultFactory.Error($"下发堆垛机{stocker.Name}库外放货任务时,更新任务{task.Id}状态失败:{tempResult.Msg}");
                                        }
                                    }
                                    else
                                    {
                                        //库外取,那么库内放
                                        //下发
                                        task.FirstStatus = TaskEntityStatus.下发堆垛机库内放货任务.GetIndexInt();
                                        task.LastStatus = TaskEntityStatus.下发堆垛机库内放货任务.GetIndexInt();
                                        var tempResult = AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                                        if (tempResult.Success)
                                        {
                                            var location = locations.Find(t => t.Code == task.DestinationLocation);
                                            BllResult sendResult = SendTaskToStocker(stocker, plc, ForkAction.货叉1, ForkTaskFlag.库内放货, location.RowIndex.ToString(), location.Line.ToString(), location.Layer.ToString(), "0", task.Id.ToString());
                                            if (sendResult.Success)
                                            {
                                                Logger.Log($"下发堆垛机{stocker.Name},任务:{task.Id},任务类型:{task.Type},货叉类型:{ForkTaskFlag.库内放货}成功", LogLevel.Success);
                                                return BllResultFactory.Sucess();
                                            }
                                            else
                                            {
                                                Logger.Log($"下发堆垛机{stocker.Name},任务:{task.Id},任务类型:{task.Type},货叉类型:{ForkTaskFlag.库内放货}失败:{sendResult.Msg};回滚任务{task.Id}状态。", LogLevel.Error);
                                                //回滚任务状态,记录日志
                                                task.FirstStatus = TaskEntityStatus.响应堆垛机库外取货任务完成.GetIndexInt();
                                                task.LastStatus = TaskEntityStatus.响应堆垛机库外取货任务完成.GetIndexInt();
                                                AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                                                return BllResultFactory.Error();
                                            }
                                        }
                                        else
                                        {
                                            Logger.Log($"下发堆垛机{stocker.Name}库内放货时,更新任务{task.Id}状态失败:{tempResult.Msg}", LogLevel.Error);
                                            return BllResultFactory.Error($"下发堆垛机{stocker.Name}库内放货时,更新任务{task.Id}状态失败:{tempResult.Msg}");
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            //无货,说明完全处在空闲状态
                            //判断任务限制情况
                            var tempTasks = tasks;
                            //hack:关于拣选的限制暂时没有做
                            if (stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "TaskLimit").Value == TaskLimit.限制出库.GetIndexString())
                            {
                                //限制出库,则不能执行下发状态的出库性质任务
                                var temp = tasks.Where(t => t.FirstStatus == TaskEntityStatus.下发任务.GetIndexInt() && t.LastStatus == TaskEntityStatus.下发任务.GetIndexInt()).Where(t => t.Type != TaskType.整盘入库.GetIndexInt() && t.Type != TaskType.空容器入库.GetIndexInt()).ToList();
                                tempTasks = tasks.Where(t => temp.Count(a => a.Id == t.Id) == 0).ToList();
                            }
                            else if (stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "TaskLimit").Value == TaskLimit.限制入库.GetIndexString())
                            {
                                //限制入库,则不能响应待入状态的入库性质任务
                                tempTasks = tasks.Where(t => t.FirstStatus != TaskEntityStatus.响应接入口位置到达.GetIndexInt() && t.LastStatus != TaskEntityStatus.响应接入口位置到达.GetIndexInt()).ToList();
                            }
                            if (tempTasks.Count == 0)
                            {
                                return BllResultFactory.Error();
                            }
                            else
                            {
                                //判断堆垛机的位置
                                TaskEntity task = null;
                                if (CurrentColumn == 0)
                                {
                                    //先入库
                                    //同巷道内的入库
                                    if (ExcuteIn(tempTasks, locations, stocker, plc).Success)
                                    {
                                        return BllResultFactory.Sucess();
                                    }
                                }

                                //出库性质的取货
                                ///获取同巷道的离堆垛机优先级最高且最近的一条任务
                                task = tasks.Where(t => locations.Count(a => a.Code == t.SourceLocation) > 0 && t.LastStatus == TaskEntityStatus.下发任务.GetIndexInt() && t.FirstStatus == TaskEntityStatus.下发任务.GetIndexInt() && (t.Type != TaskType.整盘入库.GetIndexInt() && t.Type != TaskType.空容器入库.GetIndexInt())).OrderByDescending(t => t.Priority).ThenBy(t => Math.Abs(locationsResult.Data.First(a => a.Code == t.SourceLocation).Line - CurrentColumn)).FirstOrDefault();
                                if (task != null)
                                {
                                    //先更改任务状态
                                    task.FirstStatus = TaskEntityStatus.下发堆垛机库内取货任务.GetIndexInt();
                                    task.LastStatus = TaskEntityStatus.下发堆垛机库内取货任务.GetIndexInt();
                                    var tempResult = AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                                    if (tempResult.Success)
                                    {
                                        var location = locationsResult.Data.Find(t => t.Code == task.SourceLocation);
                                        BllResult sendResult = SendTaskToStocker(stocker, plc, ForkAction.货叉1, ForkTaskFlag.库内取货, location.RowIndex.ToString(), location.Line.ToString(), location.Layer.ToString(), task.Station.ToString(), task.Id.ToString());
                                        if (sendResult.Success)
                                        {
                                            Logger.Log($"下发堆垛机{stocker.Name},任务:{task.Id},任务类型:{task.Type},货叉类型:{ForkTaskFlag.库内取货}成功,出库口:{task.Station}", LogLevel.Info);
                                            return BllResultFactory.Sucess();
                                        }
                                        else
                                        {
                                            Logger.Log($"下发堆垛机{stocker.Name},任务:{task.Id},任务类型:{task.Type},货叉类型:{ForkTaskFlag.库内取货}失败:{sendResult.Msg};回滚任务{task.Id}状态。", LogLevel.Error);
                                            task.FirstStatus = TaskEntityStatus.下发任务.GetIndexInt();
                                            task.LastStatus = TaskEntityStatus.下发任务.GetIndexInt();
                                            AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                                            return BllResultFactory.Error();
                                        }
                                    }
                                    else
                                    {
                                        Logger.Log($"下发堆垛机{stocker.Name}库内取货时,更新任务{task.Id}状态失败:{tempResult.Msg}", LogLevel.Error);
                                        return BllResultFactory.Error($"下发堆垛机{stocker.Name}库内取货时,更新任务{task.Id}状态失败:{tempResult.Msg}");
                                    }
                                }

                                //再库外取货
                                if (ExcuteIn(tempTasks, locations, stocker, plc).Success)
                                {
                                    return BllResultFactory.Sucess();
                                }

                            }
                        }
                    }
                    else if (stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1TaskFlag").Value == ForkTaskFlag.删除任务.GetIndexString())
                    {
                        return ClearWCSData(stocker, plc);
                    }
                    else
                    {
                        //hack:其他情况1-库内取货,2-库内放货,3-库外入库,4库外出库, 5重新分配入库地址,暂时不做处理,这里也应不需要处理这些情况

                    }
                }
                else if (stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskExcuteStatus").Value == TaskExcuteStatus.任务完成.GetIndexString())
                {
                    //todo:响应任务完成
                    //一共4种完成情况
                    //库内取货完成
                    var task = tasks.FirstOrDefault(t => t.LastStatus == TaskEntityStatus.下发堆垛机库内取货任务.GetIndexInt() && t.FirstStatus == TaskEntityStatus.下发堆垛机库内取货任务.GetIndexInt() && locations.Count(a => a.Code == t.SourceLocation) > 0);
                    if (task != null)
                    {
                        //更新任务状态
                        task.FirstStatus = TaskEntityStatus.响应堆垛机库内取货任务完成.GetIndexInt();
                        task.LastStatus = TaskEntityStatus.响应堆垛机库内取货任务完成.GetIndexInt();
                        var tempResult = AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                        if (tempResult.Success)
                        {
                            //标记交换区地址,任务完成10
                            var prop = stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1TaskFlag");
                            prop.Value = "10";
                            var sendResult = S7Helper.PlcSplitWrite(plc, new List<EquipmentProp> { prop }, 20);
                            if (sendResult.Success)
                            {
                                Logger.Log($"堆垛机{stocker.Name}完成库内取货成功,任务:{task.Id}", LogLevel.Success);
                                return BllResultFactory.Sucess();
                            }
                            else
                            {
                                Logger.Log($"堆垛机{stocker.Name}完成库内取货失败,任务:{task.Id},原因:{sendResult.Msg}", LogLevel.Error);
                                task.FirstStatus = TaskEntityStatus.下发堆垛机库内取货任务.GetIndexInt();
                                task.LastStatus = TaskEntityStatus.下发堆垛机库内取货任务.GetIndexInt();
                                AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                                return BllResultFactory.Error();
                            }
                        }
                        else
                        {
                            Logger.Log($"完成堆垛机{stocker.Name}库内取货时,更新任务{task.Id}状态失败:{tempResult.Msg}", LogLevel.Error);
                            return BllResultFactory.Error($"完成堆垛机{stocker.Name}库内取货时,更新任务{task.Id}状态失败:{tempResult.Msg}");
                        }
                    }
                    //库内放货完成
                    task = tasks.FirstOrDefault(t => t.LastStatus == TaskEntityStatus.下发堆垛机库内放货任务.GetIndexInt() && t.FirstStatus == TaskEntityStatus.下发堆垛机库内放货任务.GetIndexInt() && locations.Count(a => a.Code == t.SourceLocation) > 0);
                    if (task != null)
                    {
                        //调用WMS任务完成接口
                        var tempResult = AppSession.Bll.CompleteTask(task.Id.ToString());
                        if (tempResult.Success)
                        {
                            //标记交换区地址,任务完成10
                            var prop = stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1TaskFlag");
                            prop.Value = "10";
                            var sendResult = S7Helper.PlcSplitWrite(plc, new List<EquipmentProp> { prop }, 20);
                            if (sendResult.Success)
                            {
                                Logger.Log($"堆垛机{stocker.Name}完成库内放货成功,任务:{task.Id}", LogLevel.Success);
                                return BllResultFactory.Sucess();
                            }
                            else
                            {
                                //hack:这里如果更新PLC地址失败,WMS端并不会回滚,此时应考虑如何人工处理;
                                Logger.Log($"堆垛机{stocker.Name}完成库内放货失败,任务:{task.Id},原因:{sendResult.Msg}", LogLevel.Error);
                                return BllResultFactory.Error();
                            }
                        }
                        else
                        {
                            Logger.Log($"完成堆垛机{stocker.Name}库内放货失败,任务{task.Id}请求WMS接口失败:{tempResult.Msg}", LogLevel.Error);
                            return BllResultFactory.Error($"完成堆垛机{stocker.Name}库内放货失败,任务{task.Id}请求WMS接口失败:{tempResult.Msg}");
                        }
                    }
                    //库外取货完成时
                    task = tasks.FirstOrDefault(t => t.LastStatus == TaskEntityStatus.下发堆垛机库外取货任务.GetIndexInt() && t.FirstStatus == TaskEntityStatus.下发堆垛机库外取货任务.GetIndexInt() && locations.Count(a => a.Code == t.SourceLocation) > 0);
                    if (task != null)
                    {
                        //更新任务状态
                        task.FirstStatus = TaskEntityStatus.响应堆垛机库外取货任务完成.GetIndexInt();
                        task.LastStatus = TaskEntityStatus.响应堆垛机库外取货任务完成.GetIndexInt();
                        var tempResult = AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                        if (tempResult.Success)
                        {
                            //标记交换区地址,任务完成10
                            var prop = stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1TaskFlag");
                            prop.Value = "10";
                            var sendResult = S7Helper.PlcSplitWrite(plc, new List<EquipmentProp> { prop }, 20);
                            if (sendResult.Success)
                            {
                                Logger.Log($"堆垛机{stocker.Name}完成库外取货成功,任务:{task.Id}", LogLevel.Success);
                                return BllResultFactory.Sucess();
                            }
                            else
                            {
                                Logger.Log($"堆垛机{stocker.Name}完成库外取货失败,任务:{task.Id},原因:{sendResult.Msg}", LogLevel.Error);
                                task.FirstStatus = TaskEntityStatus.下发堆垛机库外取货任务.GetIndexInt();
                                task.LastStatus = TaskEntityStatus.下发堆垛机库外取货任务.GetIndexInt();
                                AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                                return BllResultFactory.Error();
                            }
                        }
                        else
                        {
                            Logger.Log($"完成堆垛机{stocker.Name}库外取货时,更新任务{task.Id}状态失败:{tempResult.Msg}", LogLevel.Error);
                            return BllResultFactory.Error($"完成堆垛机{stocker.Name}库外取货时,更新任务{task.Id}状态失败:{tempResult.Msg}");
                        }
                    }
                    //库外放货完成时
                    task = tasks.FirstOrDefault(t => t.LastStatus == TaskEntityStatus.下发堆垛机库外放货任务.GetIndexInt() && t.FirstStatus == TaskEntityStatus.下发堆垛机库外放货任务.GetIndexInt() && locations.Count(a => a.Code == t.SourceLocation) > 0);
                    if (task != null)
                    {
                        //更新任务状态
                        task.FirstStatus = TaskEntityStatus.响应堆垛机库外放货任务完成.GetIndexInt();
                        task.LastStatus = TaskEntityStatus.响应堆垛机库外放货任务完成.GetIndexInt();
                        var tempResult = AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                        if (tempResult.Success)
                        {
                            //标记交换区地址,任务完成10
                            var prop = stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1TaskFlag");
                            prop.Value = "10";
                            var sendResult = S7Helper.PlcSplitWrite(plc, new List<EquipmentProp> { prop }, 20);
                            if (sendResult.Success)
                            {
                                Logger.Log($"堆垛机{stocker.Name}完成库外放货成功,任务:{task.Id}", LogLevel.Success);
                                return BllResultFactory.Sucess();
                            }
                            else
                            {
                                Logger.Log($"堆垛机{stocker.Name}完成库外放货失败,任务:{task.Id},原因:{sendResult.Msg}", LogLevel.Error);
                                task.FirstStatus = TaskEntityStatus.下发堆垛机库外取货任务.GetIndexInt();
                                task.LastStatus = TaskEntityStatus.下发堆垛机库外取货任务.GetIndexInt();
                                AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                                return BllResultFactory.Error();
                            }
                        }
                        else
                        {
                            Logger.Log($"完成堆垛机{stocker.Name}库外放货时,更新任务{task.Id}状态失败:{tempResult.Msg}", LogLevel.Error);
                            return BllResultFactory.Error($"完成堆垛机{stocker.Name}库外放货时,更新任务{task.Id}状态失败:{tempResult.Msg}");
                        }
                    }
                }
                else
                {
                    //未知情况
                    Logger.Log($"堆垛机{stocker.Name}出现了未知的执行状态:{stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskExcuteStatus").Value}", LogLevel.Warning);
                    return BllResultFactory.Sucess();
                }
            }
            return BllResultFactory.Sucess();
        }

        private BllResult ResendTask(Equipment stocker, Plc plc, Location locationForResend, TaskEntity taskForResend,ForkTaskFlag forkTaskFlag)
        {
            BllResult sendResult = SendTaskToStocker(stocker, plc, ForkAction.货叉1, forkTaskFlag, locationForResend == null?"0":locationForResend.RowIndex.ToString(), locationForResend==null?"0":locationForResend.Line.ToString(), locationForResend==null?"0":locationForResend.Layer.ToString(), taskForResend.Station.ToString(), taskForResend.Id.ToString());
            if (sendResult.Success)
            {
                taskForResend.SendAgain = 2;
                AppSession.Bll.UpdateCommonModel<TaskEntity>(taskForResend);
                Logger.Log($"重新下发堆垛机{stocker.Name},任务:{taskForResend.Id},任务类型:{taskForResend.Type},货叉类型:{forkTaskFlag}成功,出库口:{taskForResend.Station}", LogLevel.Info);
                return BllResultFactory.Sucess();
            }
            else
            {
                Logger.Log($"重新下发堆垛机{stocker.Name},任务:{taskForResend.Id},任务类型:{taskForResend.Type},货叉类型:{forkTaskFlag}失败:{sendResult.Msg};", LogLevel.Error);
                return BllResultFactory.Error();
            }
        }

        private BllResult ClearWCSData(Equipment stocker, Plc plc)
        {
            //表示堆垛机已经收到WCS发送给他的任务完成信号,此时WCS可以清除自己的交换区地址
            BllResult sendResult = SendTaskToStocker(stocker, plc, ForkAction., ForkTaskFlag.无任务, "0", "0", "0", "0", "0");
            if (sendResult.Success)
            {
                Logger.Log($"任务完成后,清除堆垛机{stocker.Name}交换区地址成功", LogLevel.Info);
                return BllResultFactory.Sucess();
            }
            else
            {
                Logger.Log($"任务完成后,清除堆垛机{stocker.Name}交换区地址失败", LogLevel.Error);
                return BllResultFactory.Error();
            }
        }

        /// <summary>
        /// 通用条件验证
        /// </summary>
        /// <param name="stocker"></param>
        /// <returns></returns>
        private BllResult Validate(Equipment stocker)
        {
            //联机,无故障
            if (stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "OperationModel").Value == OperationModel.联机.GetIndexString() && stocker.EquipmentProps.Where(t => t.EquipmentTypePropTemplate.IsMonitor == true).Count(t => t.Value != t.EquipmentTypePropTemplate.MonitorCompareValue) == 0)
            {
                return BllResultFactory.Sucess();
            }
            else
            {
                return BllResultFactory.Error();
            }
        }

        /// <summary>
        /// 重写库外取货逻辑
        /// </summary>
        public BllResult ExcuteIn(List<TaskEntity> tempTasks, List<Location> locations, Equipment stocker, Plc plc)
        {
            var task = tempTasks.FirstOrDefault(t => t.FirstStatus == TaskEntityStatus.响应接入口位置到达.GetIndexInt() && t.LastStatus == TaskEntityStatus.响应接入口位置到达.GetIndexInt() && locations.Count(a => a.Code == t.DestinationLocation) > 0);
            if (task != null)
            {
                //下发堆垛机入库任务
                task.FirstStatus = TaskEntityStatus.下发堆垛机库外取货任务.GetIndexInt();
                task.LastStatus = TaskEntityStatus.下发堆垛机库外取货任务.GetIndexInt();
                var temp = AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                if (temp.Success)
                {
                    var location = locations.Find(t => t.Code == task.DestinationLocation);
                    BllResult sendResult = SendTaskToStocker(stocker, plc, ForkAction.货叉1, ForkTaskFlag.库外入库, "0", "0", "0", task.Station.ToString(), task.Id.ToString());
                    if (sendResult.Success)
                    {
                        Logger.Log($"下发堆垛机{stocker.Name},任务:{task.Id},任务类型:{task.Type},货叉类型:{ForkTaskFlag.库外入库}成功", LogLevel.Success);
                        return BllResultFactory.Sucess();
                    }
                    else
                    {
                        Logger.Log($"下发堆垛机{stocker.Name},任务:{task.Id},任务类型:{task.Type},货叉类型:{ForkTaskFlag.库外入库}失败:{sendResult.Msg};回滚任务{task.Id}状态。", LogLevel.Error);
                        //回滚任务状态,记录日志
                        task.FirstStatus = TaskEntityStatus.响应接入口位置到达.GetIndexInt();
                        task.LastStatus = TaskEntityStatus.响应接入口位置到达.GetIndexInt();
                        AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                        return BllResultFactory.Error();
                    }
                }
                else
                {
                    Logger.Log($"下发堆垛机{stocker.Name}库外取货时,更新任务{task.Id}状态失败:{temp.Msg}", LogLevel.Error);
                    return BllResultFactory.Error($"下发堆垛机{stocker.Name}库外取货时,更新任务{task.Id}状态失败:{temp.Msg}");
                }
            }
            return BllResultFactory.Error();
        }

        public BllResult ExcuteTaskError(Equipment stocker, Plc plc)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 堆垛机任务完成,这里任务号由软件来记
        /// </summary>
        /// <param name="stocker"></param>
        /// <param name="plc"></param>
        /// <returns></returns>
        public BllResult ExcuteTaskComplete(Equipment stocker, Plc plc)
        {
            if (ValidateStockerForCompleteTask(stocker).Success && CanExcute)
            {
                //获取堆垛机正在执行的这条任务
                var tasksResult = AppSession.Bll.GetCommonModelByCondition<TaskEntity>($"where status > {(int)TaskEntityStatus.下发任务} and status<{(int)TaskEntityStatus.任务完成}");
                if (tasksResult.Success)
                {
                    var tasks = tasksResult.Data;
                    var locationsResult = AppSession.Bll.GetAllLocations(null, null, null, null, stocker.RoadWay, null, null);
                    if (locationsResult.Success)
                    {
                        var locations = locationsResult.Data;
                        //一共4种完成情况
                        //库内取货完成
                        var task = tasks.FirstOrDefault(t => t.LastStatus == TaskEntityStatus.下发堆垛机库内取货任务.GetIndexInt() && t.FirstStatus == TaskEntityStatus.下发堆垛机库内取货任务.GetIndexInt() && locations.Count(a => a.Code == t.SourceLocation) > 0);
                        if (task != null)
                        {
                            //判断是否巷道内移库
                            if (task.Type == TaskType.移库.GetIndexInt() && locations.Count(t => t.Code == task.DestinationLocation) > 0)
                            {
                                //下发移库任务
                                var location = locations.First(t => t.Code == task.DestinationLocation);

                            }
                            else
                            {
                                //如果不是巷道内的移库,那么就全是出库性质任务

                            }
                        }

                    }
                }

            }
            return BllResultFactory.Sucess();
        }

        public BllResult ExcuteTaskIn(Equipment stocker, Plc plc)
        {
            try
            {
                //校验入库条件
                if (ValidateStockerIn(stocker).Success && CanExcute)
                {
                    //找出入库状态的任务
                    var tasksResult = AppSession.Bll.GetCommonModelByCondition<TaskEntity>($"where status = {(int)TaskEntityStatus.响应接入口位置到达}");
                    if (tasksResult.Success)
                    {
                        //过滤一次任务类型
                        var tasks = tasksResult.Data.Where(t => t.Type != TaskType.整盘出库.GetIndexInt() && t.Type != TaskType.空容器出库.GetIndexInt()).ToList();
                        if (tasks.Count > 0)
                        {
                            //找出同巷道的任务
                            var locationsResult = AppSession.Bll.GetAllLocations(null, null, null, null, stocker.RoadWay, null, null);
                            if (locationsResult.Success)
                            {
                                var task = tasks.FirstOrDefault(t => locationsResult.Data.Count(a => a.Code == t.DestinationLocation) > 0);
                                if (task != null)
                                {
                                    //更改任务状态
                                    task.FirstStatus = TaskEntityStatus.下发堆垛机库外取货任务.GetIndexInt();
                                    task.LastStatus = TaskEntityStatus.下发堆垛机库外取货任务.GetIndexInt();
                                    var temp = AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                                    if (temp.Success)
                                    {
                                        //说明找到了则下发
                                        var location = locationsResult.Data.Find(t => t.Code == task.DestinationLocation);
                                        BllResult sendResult = SendTaskToStocker(stocker, plc, ForkAction.货叉1, ForkTaskFlag.库外入库, "0", "0", "0", task.Station.ToString(), task.Id.ToString());
                                        if (sendResult.Success)
                                        {
                                            Logger.Log($"下发堆垛机{stocker.Name},任务:{task.Id},任务类型:{task.Type},货叉类型:{ForkTaskFlag.库外入库}成功,入库口:{task.Station}", LogLevel.Info);
                                            //手动标记堆垛机为不可执行,以免被再次响应
                                            CanExcute = false;
                                        }
                                        else
                                        {
                                            //回滚任务状态,记录日志
                                            task.FirstStatus = TaskEntityStatus.下发堆垛机库外取货任务.GetIndexInt();
                                            task.LastStatus = TaskEntityStatus.下发堆垛机库外取货任务.GetIndexInt();
                                            AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                                            Logger.Log($"ExcuteTaskIn回滚任务{task.Id}状态", LogLevel.Info);
                                        }
                                    }
                                    else
                                    {
                                        Logger.Log($"ExcuteTaskIn更新任务{task.Id}状态失败:{temp.Msg}", LogLevel.Error);
                                        return BllResultFactory.Error($"ExcuteTaskIn更新任务{task.Id}状态失败:{temp.Msg}");
                                    }
                                }
                            }
                        }
                    }
                }
                return BllResultFactory.Sucess();
            }
            catch (Exception ex)
            {
                Logger.Log($"堆垛机{stocker.Name}处理[入库任务]过程中出现异常:{ex.Message}", LogLevel.Exception);
                return BllResultFactory.Error($"堆垛机{stocker.Name}处理[入库任务]过程中出现异常:{ex.Message}");
            }
        }

        public BllResult SendTaskToStocker(Equipment stocker, Plc plc, ForkAction forkAction, ForkTaskFlag forkTaskFlag, string forkRow, string forkColumn, string forkLayer, string forkStation, string taskNo)
        {
            try
            {
                List<EquipmentProp> propsToWriter = new List<EquipmentProp>();
                var props = stocker.EquipmentProps;
                var action = props.Find(t => t.EquipmentTypePropTemplateCode == "WCSForkAction");
                action.Value = forkAction.GetIndexString();
                var taskFlag = props.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1TaskFlag");
                taskFlag.Value = forkTaskFlag.GetIndexString();
                var row = props.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1Row");
                row.Value = forkRow;
                var column = props.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1Column");
                column.Value = forkColumn;
                var layer = props.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1Layer");
                layer.Value = forkLayer;
                var station = props.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1Station");
                station.Value = forkStation;
                var task = props.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1Task");
                task.Value = taskNo;
                propsToWriter.AddRange(new List<EquipmentProp>() { action, taskFlag, row, column, layer, station, task });
                return S7Helper.PlcSplitWrite(plc, propsToWriter, 20);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error($"下发任务出现异常:{ex.Message}");
            }
        }

        public BllResult ExcuteTaskOut(Equipment stocker, Plc plc)
        {
            if (ValidateStockerOut(stocker).Success && CanExcute)
            {
                //找寻下发状态的任务
                var tasksResult = AppSession.Bll.GetCommonModelByCondition<TaskEntity>($"where status = {(int)TaskEntityStatus.下发任务}");
                if (tasksResult.Success)
                {
                    //找寻出库任务
                    var tasks = tasksResult.Data.Where(t => t.Type != TaskType.整盘入库.GetIndexInt() && t.Type != TaskType.空容器入库.GetIndexInt()).ToList();
                    if (tasks.Count > 0)
                    {
                        //找出同巷道的任务
                        var locationsResult = AppSession.Bll.GetAllLocations(null, null, null, null, stocker.RoadWay, null, null);
                        if (locationsResult.Success)
                        {
                            ///获取同巷道的离堆垛机优先级最高且最近的一条任务
                            var task = tasks.Where(t => locationsResult.Data.Count(a => a.Code == t.SourceLocation) > 0).OrderByDescending(t => t.Priority).ThenBy(t => Math.Abs(locationsResult.Data.First(a => a.Code == t.SourceLocation).Line - CurrentColumn)).First();
                            if (task != null)
                            {
                                //先更改任务状态
                                task.FirstStatus = TaskEntityStatus.下发堆垛机库内取货任务.GetIndexInt();
                                task.LastStatus = TaskEntityStatus.下发堆垛机库内取货任务.GetIndexInt();
                                var temp = AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                                if (temp.Success)
                                {
                                    var location = locationsResult.Data.Find(t => t.Code == task.SourceLocation);
                                    BllResult sendResult = SendTaskToStocker(stocker, plc, ForkAction.货叉1, ForkTaskFlag.库内取货, location.RowIndex.ToString(), location.Line.ToString(), location.Layer.ToString(), task.Station.ToString(), task.Id.ToString());
                                    if (sendResult.Success)
                                    {
                                        Logger.Log($"下发堆垛机{stocker.Name},任务:{task.Id},任务类型:{task.Type},货叉类型:{ForkTaskFlag.库内取货}成功,出库口:{task.Station}", LogLevel.Info);
                                        //手动标记堆垛机为不可执行,以免被再次响应
                                        CanExcute = false;
                                    }
                                    else
                                    {
                                        task.FirstStatus = TaskEntityStatus.下发任务.GetIndexInt();
                                        task.LastStatus = TaskEntityStatus.下发任务.GetIndexInt();
                                        AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                                        Logger.Log($"ExcuteTaskOut回滚任务{task.Id}状态", LogLevel.Info);
                                    }
                                }
                                else
                                {
                                    Logger.Log($"ExcuteTaskOut更新任务{task.Id}状态失败:{temp.Msg}", LogLevel.Error);
                                    return BllResultFactory.Error($"ExcuteTaskOut更新任务{task.Id}状态失败:{temp.Msg}");
                                }
                            }
                        }
                    }
                }
            }
            return BllResultFactory.Sucess();
        }

        /// <summary>
        /// 这里写个示例,判断一下同巷道内任务
        /// </summary>
        /// <param name="stocker"></param>
        /// <param name="plc"></param>
        /// <returns></returns>
        public BllResult ExcuteTaskTransferInRoadway(Equipment stocker, Plc plc)
        {
            if (ValidateStockerTransferInRoadway(stocker).Success && CanExcute)
            {
                //找寻下发状态的任务
                var tasksResult = AppSession.Bll.GetCommonModelByCondition<TaskEntity>($"where status = {(int)TaskEntityStatus.下发任务} and type = {TaskType.移库.GetIndexInt()}");
                if (tasksResult.Success)
                {
                    //找寻同巷道移库任务
                    var locationsResult = AppSession.Bll.GetAllLocations(null, null, null, null, stocker.RoadWay, null, null);
                    if (locationsResult.Success)
                    {
                        var task = tasksResult.Data.Where(t => locationsResult.Data.Count(a => a.Code == t.SourceLocation) > 0 && locationsResult.Data.Count(a => a.Code == t.DestinationLocation) > 0).OrderByDescending(t => t.Priority).ThenBy(t => Math.Abs(locationsResult.Data.First(a => a.Code == t.SourceLocation).Line - CurrentColumn)).First();
                        if (task != null)
                        {
                            //有则下发
                            //先更改任务状态
                            task.FirstStatus = TaskEntityStatus.下发堆垛机库内取货任务.GetIndexInt();
                            task.LastStatus = TaskEntityStatus.下发堆垛机库内取货任务.GetIndexInt();
                            var temp = AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                            if (temp.Success)
                            {
                                var location = locationsResult.Data.Find(t => t.Code == task.SourceLocation);
                                BllResult sendResult = SendTaskToStocker(stocker, plc, ForkAction.货叉1, ForkTaskFlag.库内取货, location.RowIndex.ToString(), location.Line.ToString(), location.Layer.ToString(), task.Station.ToString(), task.Id.ToString());
                                if (sendResult.Success)
                                {
                                    Logger.Log($"下发堆垛机{stocker.Name},任务:{task.Id},任务类型:{task.Type},货叉类型:{ForkTaskFlag.库内取货}成功", LogLevel.Info);
                                    //手动标记堆垛机为不可执行,以免被再次响应
                                    CanExcute = false;
                                }
                                else
                                {
                                    task.FirstStatus = TaskEntityStatus.下发任务.GetIndexInt();
                                    task.LastStatus = TaskEntityStatus.下发任务.GetIndexInt();
                                    AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                                    Logger.Log($"ExcuteTaskOut回滚任务{task.Id}状态", LogLevel.Info);
                                }
                            }
                            else
                            {
                                Logger.Log($"ExcuteTaskTransferInRoadway更新任务{task.Id}状态失败:{temp.Msg}", LogLevel.Error);
                                return BllResultFactory.Error($"ExcuteTaskTransferInRoadway更新任务{task.Id}状态失败:{temp.Msg}");
                            }
                        }
                    }
                }
            }
            return BllResultFactory.Sucess();
        }

        /// <summary>
        /// 堆垛机任务完成条件
        /// </summary>
        /// <param name="stocker"></param>
        /// <returns></returns>
        public BllResult ValidateStockerForCompleteTask(Equipment stocker)
        {
            try
            {
                //1.联机
                //2.货叉有货
                //3.任务执行状态为任务完成
                if (stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "OperationModel").Value == OperationModel.联机.GetIndexString() && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1ForkHasPallet").Value == "True" && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskExcuteStatus").Value == TaskExcuteStatus.任务完成.GetIndexString())
                {
                    return BllResultFactory.Sucess();
                }
                else
                {
                    return BllResultFactory.Error();
                }
            }
            catch (Exception ex)
            {
                Logger.Log($"校验堆垛机{stocker.Name}任务完成条件异常:{ex.Message}", LogLevel.Exception, ex);
                return BllResultFactory.Error($"校验堆垛机{stocker.Name}任务完成条件异常:{ex.Message}");
            }
        }

        public BllResult ValidateStockerIn(Equipment stocker)
        {
            try
            {
                //判断条件,这里没有判断货叉故障和堆垛机故障,这特么怎么判断,这么多字段
                //1.操作模式 = 5  联机;
                //2.双货叉原位,任务货叉上无货;
                //3.任务货叉无故障无任务;
                //4.堆垛机无故障
                //5.对应货叉任务类型:0 - 无任务
                //6.对应货叉任务标志:1待机
                //WCS交换区无任务
                if (stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "OperationModel").Value == OperationModel.联机.GetIndexString() && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1ForkHasPallet").Value == "False" && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskNo").Value == "0" && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskType").Value == ForkTaskFlag.无任务.GetIndexString() && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskExcuteStatus").Value == TaskExcuteStatus.待机.GetIndexString() && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1TaskFlag").Value == ForkTaskFlag.无任务.GetIndexString())
                {
                    return BllResultFactory.Sucess();
                }
                else
                {
                    return BllResultFactory.Error();
                }
            }
            catch (Exception ex)
            {
                Logger.Log($"校验堆垛机{stocker.Name}入库条件异常:{ex.Message}", LogLevel.Exception, ex);
                return BllResultFactory.Error($"校验堆垛机{stocker.Name}入库条件异常:{ex.Message}");
            }
        }

        public BllResult ValidateStockerOut(Equipment stocker)
        {
            try
            {
                //判断条件,这里没有判断货叉故障和堆垛机故障,这特么怎么判断,这么多字段
                //1.操作模式:5  联机;
                //2.任务限制:不等于3; 
                //3.双货叉原位,任务货叉上有货;
                //4.任务货叉无故障无任务;
                //5.堆垛机无故障
                //5.对应货叉任务类型:0 - 无任务
                //6.对应货叉任务标志:1待机
                if (stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "OperationModel").Value == OperationModel.联机.GetIndexString() && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1ForkHasPallet").Value == "False" && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskNo").Value == "0" && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskType").Value == ForkTaskFlag.无任务.GetIndexString() && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskExcuteStatus").Value == TaskExcuteStatus.待机.GetIndexString() && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "TaskLimit").Value != TaskLimit.限制出库.GetIndexString() && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1TaskFlag").Value == ForkTaskFlag.无任务.GetIndexString())
                {
                    return BllResultFactory.Sucess();
                }
                else
                {
                    return BllResultFactory.Error();
                }

            }
            catch (Exception ex)
            {
                Logger.Log($"校验堆垛机{stocker.Name}出库条件异常:{ex.Message}", LogLevel.Exception, ex);
                return BllResultFactory.Error($"校验堆垛机{stocker.Name}出库条件异常:{ex.Message}");
            }
        }

        /// <summary>
        /// 同巷道内移库条件判断
        /// </summary>
        /// <param name="stocker"></param>
        /// <returns></returns>
        public BllResult ValidateStockerTransferInRoadway(Equipment stocker)
        {
            try
            {
                //这个同巷道内的移库任务,判断条件与出库一致
                //判断条件,这里没有判断货叉故障和堆垛机故障,这特么怎么判断,这么多字段
                //1.操作模式:5  联机;
                //2.任务限制:不等于3; (同巷道内移库不判断这个)
                //3.双货叉原位,任务货叉上有货;
                //4.任务货叉无故障无任务;
                //5.堆垛机无故障
                //5.对应货叉任务类型:0 - 无任务
                //6.对应货叉任务标志:1待机
                if (stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "OperationModel").Value == OperationModel.联机.GetIndexString() && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1ForkHasPallet").Value == "False" && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskNo").Value == "0" && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskType").Value == ForkTaskFlag.无任务.GetIndexString() && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskExcuteStatus").Value == TaskExcuteStatus.待机.GetIndexString() && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1TaskFlag").Value == ForkTaskFlag.无任务.GetIndexString())
                {
                    return BllResultFactory.Sucess();
                }
                else
                {
                    return BllResultFactory.Error();
                }

            }
            catch (Exception ex)
            {
                Logger.Log($"校验堆垛机{stocker.Name}移库条件异常:{ex.Message}", LogLevel.Exception, ex);
                return BllResultFactory.Error($"校验堆垛机{stocker.Name}移库条件异常:{ex.Message}");
            }
        }

        #endregion
    }
}