SingeForkStockerExcute.cs 50 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
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>
    /// 单叉堆垛机标准实现
    /// </summary>
    public class SingeForkStockerExcute : StockerExcute
    {
        /// <summary>
        /// 记录堆垛机当前所在的列
        /// </summary>
        public int CurrentColumn { get; private set; }

        public override BllResult Excute(List<Equipment> stockers, OPCHelp plc)
        {
            try
            {
                foreach (var stocker in stockers)
                {
                    CurrentColumn = int.Parse(stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "CurrentColumn").Value);
                    //var plc = plcs.Find(t => t.IP == stocker.IP);

                    ExcuteSingle(stocker, plc);
                    //心跳
                    Heartbeat(stocker, plc);

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

        /// <summary>
        /// 以堆垛机状态驱动,重写堆垛机控制逻辑
        /// </summary>
        /// <param name="stocker"></param>
        /// <param name="plc"></param>
        /// <returns></returns>
        public BllResult ExcuteSingle(Equipment stocker, OPCHelp 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 lastStatus>={TaskEntityStatus.下发任务.GetIndexInt()} and lastStatus < {TaskEntityStatus.任务完成.GetIndexInt()} and zoneCode = 'LK'");
                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;
                var taskId = stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskNo").Value;
                var taskActionType = stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskType").Value;
                #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.RowIndex.ToString(),locationForResend.Line.ToString(),locationForResend.Layer.ToString(), 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.RowIndex.ToString(),locationForResend.Line.ToString(),locationForResend.Layer.ToString(), 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)
                        {
                            BllResult<Equipment> stationResult = AppSession.Bll.GetOutStationByIndex(taskForResend.Station,int.Parse(taskForResend.Port));
                            if (!stationResult.Success)
                            {
                                Logger.Log($"重新下发任务时,根据任务号{taskForResend.Id}未能获取到站台号:{stationResult.Msg}", LogLevel.Error);
                                return BllResultFactory.Error();
                            }
                            return ResendTask(stocker, plc, stationResult.Data.RowIndex.ToString(), "0","0", 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)
                        {
                            BllResult<Equipment> stationResult = AppSession.Bll.GetOutStationByIndex(taskForResend.Station,int.Parse(taskForResend.Port));
                            if (!stationResult.Success)
                            {
                                Logger.Log($"重新下发任务时,根据任务号{taskForResend.Id}未能获取到站台号:{stationResult.Msg}", LogLevel.Error);
                                return BllResultFactory.Error();
                            }
                            return ResendTask(stocker, plc, stationResult.Data.RowIndex.ToString(), "0","0", taskForResend, ForkTaskFlag.库外出库);
                        }

                        #endregion
                        //判断堆垛机货叉上是不是有货,有货就只能接受放货任务,无货就可以接受取货任务
                        if (stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1ForkHasPallet").Value == "True")
                        {
                            //有货,则查找同巷道的取货任务完成状态的任务,同一个堆垛机,单叉情况下最多一条
                            #region 判断是否超出一条
                            if(tasks.Count(t=>(t.LastStatus== TaskEntityStatus.响应堆垛机库内取货任务完成.GetIndexInt()) || t.LastStatus == TaskEntityStatus.响应堆垛机库外取货任务完成.GetIndexInt()) > 1)
                            {
                                Logger.Log($"堆垛机{stocker.Name}显示货叉上有货,但是对应的任务超过1条,请检查状态为{TaskEntityStatus.响应堆垛机库内取货任务完成}和{TaskEntityStatus.响应堆垛机库外取货任务完成}的任务", LogLevel.Error);
                                return BllResultFactory.Error();
                            }
                            #endregion
                            //先找库内取货完成的任务
                            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.库内放货}成功,库位编码:{location.Code}", 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())
                                    {
                                        BllResult<Equipment> stationResult = AppSession.Bll.GetOutStationByIndex(task.Station,int.Parse(task.Port));
                                        if (!stationResult.Success)
                                        {
                                            Logger.Log($"下发堆垛机库外出库任务时,根据任务号{task.Id}未能获取到站台号:{stationResult.Msg}", LogLevel.Error);
                                            return BllResultFactory.Error();
                                        }
                                        //库内取,那么库外放
                                        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.库外出库, stationResult.Data.RowIndex.ToString(), "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.库内放货}成功,库位:{location.Code}", 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();
                            }
                            //查看是否有开关控制
                            //todo://huhai-2019-06-04 去掉开关相关的
                            //if (stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSCanOut").Value != "True")
                            //{
                            //    //过滤去1号站台的出库任务
                            //    tempTasks = tempTasks.Where(t => (t.Type==TaskType.整盘入库.GetIndexInt()||t.Type==TaskType.空容器入库.GetIndexInt())||t.Station != 1).ToList();
                            //}
                            if (tempTasks.Count == 0)
                            {
                                return BllResultFactory.Error();
                            }
                            else
                            {
                                //判断堆垛机的位置
                                TaskEntity task = null;
                                if (CurrentColumn == 0&&stocker.EquipmentProps.Find(t=>t.EquipmentTypePropTemplateCode== "CurrentStation").Value!="0")
                                {
                                    //先入库
                                    //同巷道内的入库
                                    if (ExcuteIn(tempTasks, locations, stocker, plc,stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "CurrentStation").Value).Success)
                                    {
                                        return BllResultFactory.Sucess();
                                    }
                                }

                                //出库性质的取货
                                ///获取同巷道的离堆垛机优先级最高且最近的一条任务
                                task = tempTasks.Where(t => locations.Count(a => a.Code == t.SourceLocation) > 0 
                                && t.LastStatus == TaskEntityStatus.下发任务.GetIndexInt() && t.FirstStatus == TaskEntityStatus.下发任务.GetIndexInt() )
                                .OrderByDescending(t => t.Priority).ThenBy(t => Math.Abs(locationsResult.Data.First(a => a.Code == t.SourceLocation).Line - CurrentColumn))
                                .FirstOrDefault();
                                if (task != null)
                                {
                                    ////todo:huhai-2019-06-04  增加判断前面是否有出库任务(整盘出库任务除外)未完成
                                    //if (task.Type == TaskType.出库查看.GetIndexInt() || task.Type == TaskType.分拣出库.GetIndexInt() || task.Type == TaskType.补充入库.GetIndexInt()
                                    //    || task.Type == TaskType.盘点.GetIndexInt())
                                    //{
                                    //    var st = tempTasks.Where(t => t.Station == task.Station && t.Port == task.Port && t.FirstStatus > 10).ToList();
                                    //    if (st.Count >= 1)
                                    //    {
                                    //        Logger.Log($"站台{task.Station} 存在需要回库的任务,请稍后进行出库操作", LogLevel.Warning);
                                    //        if (ExcuteIn(tempTasks, locations, stocker, plc).Success)
                                    //        {
                                    //            return BllResultFactory.Sucess();
                                    //        }
                                    //        return BllResultFactory.Error($"站台{task.Station} 存在需要回库的任务,请稍后进行出库操作");
                                    //    }
                                    //}// 如果是整盘的任务则,需要判断这个任务对应的口是否 出库查看、分拣出库、补充入库、盘点任务
                                    //else if(task.Type==TaskType.整盘出库.GetIndexInt()|| task.Type==TaskType.空容器出库.GetIndexInt())
                                    //{
                                    //    var st = tempTasks.Where(t => t.Port == task.Port && t.FirstStatus > 10
                                    //      && (t.Type == TaskType.出库查看.GetIndexInt() || t.Type == TaskType.分拣出库.GetIndexInt() || t.Type == TaskType.盘点.GetIndexInt() 
                                    //      || t.Type == TaskType.补充入库.GetIndexInt()|| t.Type==TaskType.整盘入库.GetIndexInt())
                                    //    ).ToList();
                                    //    if (st != null && st.Count > 0)
                                    //    {
                                    //        Logger.Log($"站台{task.Station} 存在需要回库的任务,请稍后进行出库操作", LogLevel.Warning);
                                    //        if (ExcuteIn(tempTasks, locations, stocker, plc).Success)
                                    //        {
                                    //            return BllResultFactory.Sucess();
                                    //        }
                                    //        return BllResultFactory.Error($"站台{task.Station} 存在需要回库的任务,请稍后进行出库操作");
                                    //    }
                                    //}
                                   /* //todo:huhai-2019-06-04 移库判断
                                    var result = AppSession.Bll.ExecuteTaskRequest<int>(task.Id);
                                    if (!result.Success)
                                    {
                                        Logger.Log($"任务{task.Id}安排移库失败", LogLevel.Warning);
                                        return BllResultFactory.Error(result.Msg);
                                    }
                                    if (result.Data != task.Id)
                                    {
                                        var t1 = AppSession.Bll.GetCommonModelByCondition<TaskEntity>($" where id={result.Data} ");
                                        if (!t1.Success && t1.Data.Count == 0)
                                        {
                                            return BllResultFactory.Error($"任务ID:{task.Id}请求WMS是否移库,WMS返回的任务ID:{result.Data}在数据库为查询到相应任务");
                                        }
                                        //再库外取货
                                        if (ExcuteIn(tempTasks, locations, stocker, plc).Success)
                                        {
                                            return BllResultFactory.Sucess();
                                        }
                                        Logger.Log($"任务:{task.Id}有需要移动库位的移库任务:{t1.Data[0].Id}需要先执行...", LogLevel.Info);
                                        return BllResultFactory.Error($"任务:{task.Id}有需要移动库位的移库任务:{t1.Data[0].Id}需要先执行...") ;
                                    } */
                                    //先更改任务状态
                                    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(), "0", task.Id.ToString());
                                        if (sendResult.Success)
                                        {
                                            Logger.Log($"下发堆垛机{stocker.Name},任务:{task.Id},任务类型:{task.Type},货叉类型:{ForkTaskFlag.库内取货}成功,库位:{location.Code},出库口:{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 && t.Id==int.Parse(taskId));
                    if (task != null && task.Id.ToString()==taskId&& taskActionType==ForkTaskFlag.库内取货.GetIndexString())
                    {
                        //更新任务状态
                        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);
                            var sendResult = plc.WriteAddress(new List<EquipmentProp> { prop });
                            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.DestinationLocation) > 0);
                    if (task != null && task.Id.ToString() == taskId&& taskActionType==ForkTaskFlag.库内放货.GetIndexString())
                    {
                        //调用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);
                            var sendResult = plc.WriteAddress(new List<EquipmentProp> { prop });
                            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.DestinationLocation) > 0);
                    if (task != null && task.Id.ToString() == taskId&& taskActionType==ForkTaskFlag.库外入库.GetIndexString())
                    {
                        //更新任务状态
                        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);
                            var sendResult = plc.WriteAddress(new List<EquipmentProp> { prop });
                            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.Id.ToString() == taskId&& taskActionType==ForkTaskFlag.库外出库.GetIndexString())
                    {
                        //更新任务状态
                        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);
                            var sendResult = plc.WriteAddress(new List<EquipmentProp> { prop });
                            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}");
                        }
                    }
                    //Logger.Log($"堆垛机完成任务时,没有找到对应的任务{taskId}-动作{taskActionType}不匹配",LogLevel.Info);
                    //return BllResultFactory.Error($"堆垛机完成任务时,没有找到对应的任务{taskId}-动作{taskActionType}不匹配");
                }
                else
                {
                    //未知情况
                    Logger.Log($"堆垛机{stocker.Name}出现了未知的执行状态:{stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskExcuteStatus").Value}", LogLevel.Warning);
                    return BllResultFactory.Sucess();
                }
            }
            return BllResultFactory.Sucess();
        }

        /// <summary>
        /// 重写库外取货逻辑
        /// </summary>
        public BllResult ExcuteIn(List<TaskEntity> tempTasks, List<Location> locations, Equipment stocker, OPCHelp 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)
            {
                return ExcuteInInner(stocker, plc, task);
            }
            return BllResultFactory.Error();
        }

        private BllResult ExcuteInInner(Equipment stocker, OPCHelp plc, TaskEntity task)
        {
            //下发堆垛机入库任务
            //todo:huhai-2019-06-04 移库判断
            /* var result = AppSession.Bll.ExecuteTaskRequest<int>(task.Id);
            if (!result.Success)
            {
                Logger.Log("移库的时候出现错误", LogLevel.Warning, null);
                return BllResultFactory.Error(result.Msg);
            } */
            
            BllResult<Equipment> stationResult = AppSession.Bll.GetInStationByIndex(task.Station,int.Parse(task.Port));
            if (!stationResult.Success)
            {
                Logger.Log($"堆垛机入库时,根据任务号{task.Id}未能获取到站台号:{stationResult.Msg}", LogLevel.Error);
                return BllResultFactory.Error();
            }
            /*if (result.Data != task.Id)
            {
                var t1 = AppSession.Bll.GetCommonModelByCondition<TaskEntity>($" where id={result.Data} ");
                if (!t1.Success && t1.Data.Count == 0)
                {
                    Logger.Log($"任务ID:{task.Id}请求WMS是否移库,WMS返回的任务ID:{result.Data}在数据库为查询到相应任务", LogLevel.Warning, null);
                    return BllResultFactory.Error($"任务ID:{task.Id}请求WMS是否移库,WMS返回的任务ID:{result.Data}在数据库为查询到相应任务");
                }
                return BllResultFactory.Error($"任务:{task.Id}有需要移动库位的移库任务:{t1.Data[0].Id}需要先执行...") ;
            } */
            task.FirstStatus = TaskEntityStatus.下发堆垛机库外取货任务.GetIndexInt();
            task.LastStatus = TaskEntityStatus.下发堆垛机库外取货任务.GetIndexInt();
            var temp = AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
            if (temp.Success)
            {
                BllResult sendResult = SendTaskToStocker(stocker, plc, ForkAction.货叉1, ForkTaskFlag.库外入库, stationResult.Data.RowIndex.ToString(), "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}");
            }
        }

        /// <summary>
        /// 重写库外取货逻辑
        /// </summary>
        public BllResult ExcuteIn(List<TaskEntity> tempTasks, List<Location> locations, Equipment stocker, OPCHelp plc,string station)
        {
            var task = tempTasks.FirstOrDefault(t => t.FirstStatus == TaskEntityStatus.响应接入口位置到达.GetIndexInt() && t.LastStatus == TaskEntityStatus.响应接入口位置到达.GetIndexInt() && locations.Count(a => a.Code == t.DestinationLocation) > 0&&t.Station?.ToString()==station);
            if (task != null)
            {
                return ExcuteInInner(stocker, plc, task);
            }
            return BllResultFactory.Error();
        }

        private BllResult ResendTask(Equipment stocker, OPCHelp plc, string row,string column,string layer, TaskEntity taskForResend, ForkTaskFlag forkTaskFlag)
        {
            BllResult sendResult = SendTaskToStocker(stocker, plc, ForkAction.货叉1, forkTaskFlag, row,column,layer, 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();
            }
        }
    }
}