SendWcsTaskAction.cs 20.1 KB
using System;
using System.Collections.Generic;
using System.Data;
using Infrastructure;
using Quartz;
using WebApp;
using WebRepository;
using System.Linq;

namespace WebMvc
{
    /// <summary>
    /// 单据结果回传
    /// </summary>

    /// <summary>
    /// 入库单结果回传
    /// </summary>
    [PersistJobDataAfterExecution]
    [DisallowConcurrentExecution]
    public class SendWcsTaskAction
    {
        private string ConnString { set; get; }
        IJobExecutionContext Context { set; get; }

        public SendWcsTaskAction(string _ConnString, IJobExecutionContext _Context)
        {
            ConnString = _ConnString;
            Context = _Context;
        }

        public void Execute(JobContainer jobContainer)
        {
            UnitWork _unitWork = new UnitWork(AppSettingsJson.JobContext(ConnString));
            #region 任务下发逻辑
            try
            {
                List<TaskDetail> taskDetails = (from td in _unitWork.Find<TaskDetail>(n => n.Status == TaskStatus.待下发任务)
                                                join con in _unitWork.Find<Container>(n => n.Code != null)
                                                on td.ContainerCode equals con.Code
                                                where (new string[] { TaskType.容器出库, TaskType.直接出库 }.Contains(td.TaskType) && con.IsLock == 0) || (new string[] { TaskType.空容器入库, TaskType.容器回库, TaskType.站台到站台, TaskType.空容器出库, TaskType.盘点 }.Contains(td.TaskType))
                                                select td).ToList();

                foreach (TaskDetail taskDetail in taskDetails)
                {
                    //成品库入库空板补给成功触发下一个入库
                    if (taskDetail.TaskType == TaskType.容器回库 && taskDetail.Roadway == 5 && _unitWork.IsExist<Station>(n => n.Code == taskDetail.ContainerCode))
                    {
                        int num = _unitWork.Find<StoreStation>(n => n.StationCode == "BufferStationE05").Count();
                        if (num < 3)
                        {
                            if (!_unitWork.IsExist<TaskDetail>(n => n.TaskType == TaskType.空容器入库 && n.DestinationLocation == "BufferStationE05"))
                            {
                                string type = "";
                                if (taskDetail.MaterialCode == "1" || taskDetail.MaterialCode == "2" || taskDetail.MaterialCode == "3")
                                {
                                    type = ContainerType.木栈板母板_1;
                                }
                                else
                                {
                                    type = ContainerType.木栈板母板_2;
                                }
                                var emptyContainer = (from con in _unitWork.Find<Container>(n => n.Type == type && n.Status == ContainerStatus. && n.IsLock == 0)
                                                      join loc in _unitWork.Find<Location>(n => n.Roadway == taskDetail.Roadway && n.Status == LocationStatus.空容器)
                                                      on con.LocationCode equals loc.Code
                                                      orderby loc.MaxHeight
                                                      select con).FirstOrDefault();
                                if (emptyContainer == null)
                                {
                                    taskDetail.Error = "暂无空母板无法入库:" + DateTime.Now.ToString("HH:mm:ss");
                                    _unitWork.Update(taskDetail);
                                    continue;
                                }
                                //建立容器出库任务
                                if (!_unitWork.IsExist<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.ContainerCode == emptyContainer.Code))
                                {
                                    int ptdcount = _unitWork.Find<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.DestinationLocation == taskDetail.DestinationLocation).Count();
                                    if (ptdcount < 1)
                                    {
                                        Task ptask = new Task();
                                        TaskDetail ptaskDetail = new TaskDetail();
                                        var taskNo = _unitWork.GetTaskNo(TaskNo.空板补充);
                                        ptask.TaskNo = taskNo;
                                        ptask.OrderCode = taskDetail.TaskNo;
                                        ptask.BusinessType = BusinessType.出库_其他出库单;
                                        ptask.FirstStatus = TaskStatus.待下发任务;
                                        ptask.LastStatus = TaskStatus.待下发任务;
                                        ptask.CreateTime = DateTime.Now;
                                        _unitWork.Add(ptask);

                                        ptaskDetail.TaskNo = taskNo;
                                        ptaskDetail.OrderCode = taskDetail.TaskNo;
                                        ptaskDetail.TaskType = TaskType.空容器出库;
                                        ptaskDetail.ContainerCode = emptyContainer.Code;
                                        ptaskDetail.SourceLocation = emptyContainer.LocationCode;
                                        ptaskDetail.DestinationLocation = taskDetail.DestinationLocation;
                                        ptaskDetail.OderQty = 0;
                                        ptaskDetail.ContainerQty = 0;
                                        ptaskDetail.HadQty = 0;
                                        ptaskDetail.Roadway = taskDetail.Roadway;
                                        ptaskDetail.Station = taskDetail.Station;
                                        ptaskDetail.Status = TaskStatus.待下发任务;
                                        ptaskDetail.Priority = 0;
                                        ptaskDetail.CreateTime = DateTime.Now;
                                        _unitWork.Add(ptaskDetail);

                                        emptyContainer.IsLock = 1;
                                        _unitWork.Update(emptyContainer);

                                        Location loc = _unitWork.Find<Location>(n => n.Code == emptyContainer.LocationCode).FirstOrDefault();
                                        _unitWork.Update<Location>(n => n.Id == loc.Id && n.Version == loc.Version, n => new Location
                                        {
                                            Status = LocationStatus.任务锁定中
                                        });

                                        List<Inventory> list = _unitWork.Find<Inventory>(n => n.ContainerCode == taskDetail.ContainerCode).ToList();
                                        foreach (Inventory inv in list)
                                        {
                                            inv.SourceCode = ptask.TaskNo;
                                            _unitWork.Update(inv);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }
                    if (_unitWork.IsExist<Station>(n => n.Containercode == taskDetail.ContainerCode && n.Code != taskDetail.Station) && taskDetail.TaskType == TaskType.容器出库)
                    {
                        taskDetail.Error = "托盘:" + taskDetail.ContainerCode + "已占用别的站台" + ":" + DateTime.Now.ToString("HH:mm:ss");
                        _unitWork.Update(taskDetail);
                        continue;
                    }
                    //暂存站台判断
                    if (_unitWork.IsExist<Station>(n => n.Type == StationType.暂存区站台 && n.Code == taskDetail.Station))
                    {
                        Station bufferStation = null;
                        if (taskDetail.Roadway == 4)
                        {
                            bufferStation = (from st in _unitWork.Find<Station>(n => n.Type == StationType.暂存区站台 && n.Containercode == "")
                                             join sr in _unitWork.Find<StationRoadway>(n => n.RoadWay == taskDetail.Roadway)
                                             on st.Code equals sr.StationCode
                                             orderby st.Code descending
                                             select st).FirstOrDefault();
                        }
                        else
                        {
                            bufferStation = (from st in _unitWork.Find<Station>(n => n.Type == StationType.暂存区站台 && n.Containercode == "")
                                             join sr in _unitWork.Find<StationRoadway>(n => n.RoadWay == taskDetail.Roadway)
                                             on st.Code equals sr.StationCode
                                             orderby st.Code
                                             select st).FirstOrDefault();
                        }

                        if (bufferStation == null)
                        {
                            taskDetail.Error = "暂无暂存台,托盘:" + taskDetail.ContainerCode + ":" + DateTime.Now.ToString("HH:mm:ss");
                            _unitWork.Update(taskDetail);
                            continue;
                        }
                        else
                        {
                            taskDetail.Station = bufferStation.Code;
                            taskDetail.DestinationLocation = bufferStation.Code;
                        }
                    }

                    Station station = null;
                    Location location = null;
                    Container container = _unitWork.Find<Container>(n => n.Code == taskDetail.ContainerCode).FirstOrDefault(); ;
                    if (taskDetail.TaskType == TaskType.容器出库 || taskDetail.TaskType == TaskType.直接出库 || taskDetail.TaskType == TaskType.站台到站台 || taskDetail.TaskType == TaskType.盘点)
                    {
                        station = _unitWork.Find<Station>(n => n.Code == taskDetail.Station && (n.Containercode == "" || n.Containercode == taskDetail.ContainerCode) && n.IsOut == 1 && n.IsStop == 0).FirstOrDefault();
                    }
                    else if (taskDetail.TaskType == TaskType.空容器出库)
                    {
                        station = _unitWork.Find<Station>(n => n.Code == taskDetail.Station && (n.Containercode == "" || n.Containercode == taskDetail.ContainerCode) && n.IsOut == 1 && n.IsStop == 0 && n.IsEmpty == 1).FirstOrDefault();
                    }
                    else
                    {
                        station = _unitWork.Find<Station>(n => n.IsStop == 0).FirstOrDefault();
                    }

                    if (station != null)
                    {
                        if (taskDetail.TaskType == TaskType.容器出库 || taskDetail.TaskType == TaskType.直接出库 || taskDetail.TaskType == TaskType.盘点)
                        {
                            if (!_unitWork.IsExist<Location>(n => n.ContainerCode == taskDetail.ContainerCode && n.Code == taskDetail.SourceLocation))
                            {
                                taskDetail.Error = "WMS:容器出库起始仓位与实际仓位不符 托盘:" + taskDetail.ContainerCode + ":" + DateTime.Now.ToString("HH:mm:ss");
                                _unitWork.Update(taskDetail);
                                continue;
                            }
                        }
                    }
                    else
                    {
                        taskDetail.Error = "站台:" + taskDetail.Station + "异常" + ":" + DateTime.Now.ToString("HH:mm:ss");
                        _unitWork.Update(taskDetail);
                        continue;
                    }

                    ApiRequest apiRequest = new ApiRequest("WCS");
                    //WCSResponse<WcsTask> _WCSResponse = apiRequest.Post<WCSResponse<WcsTask>>(JsonHelper.Instance.Serialize(taskDetail), "WcsWebApi/TaskAssign", "任务下发");
                    WCSResponse<WcsTask> _WCSResponse = apiRequest.Post<WCSResponse<WcsTask>>(JsonHelper.Instance.Serialize(taskDetail), "IWcsTaskTest/CreateWcsTask", "任务下发");
                    if (_WCSResponse.Code == 200)
                    {
                        if (taskDetail.TaskType == TaskType.容器出库 || taskDetail.TaskType == TaskType.空容器出库 || taskDetail.TaskType == TaskType.直接出库 || taskDetail.TaskType == TaskType.盘点)
                        {
                            taskDetail.Status = TaskStatus.下达任务;
                            taskDetail.DestinationLocation = station.Code;

                            location = _unitWork.Find<Location>(n => n.Code == taskDetail.SourceLocation).FirstOrDefault();
                            location.Status = LocationStatus.任务锁定中;
                            station.Containercode = taskDetail.ContainerCode;
                            if (taskDetail.TaskType == TaskType.盘点)
                            {
                                container.IsLock = ContainerLock.盘点锁;
                            }
                            else
                            {
                                container.IsLock = ContainerLock.任务锁;
                            }
                        }
                        else if(taskDetail.TaskType == TaskType.空容器入库 && taskDetail.SourceLocation != "PP_SpecialPoint")
                        {
                                taskDetail.Status = TaskStatus.下达任务;
                                location = _unitWork.Find<Location>(n => n.Code == taskDetail.DestinationLocation).FirstOrDefault();
                                location.Status = LocationStatus.任务锁定中;
                                station.Containercode = taskDetail.ContainerCode;
                                container.IsLock = ContainerLock.任务锁;
                        }
                        else if (taskDetail.TaskType == TaskType.站台到站台)
                        {
                            taskDetail.Status = TaskStatus.下达任务;
                            taskDetail.DestinationLocation = station.Code;
                            if (taskDetail.ContainerCode != "woodPallet")
                            {
                                station.Containercode = taskDetail.ContainerCode;
                                if (taskDetail.TaskType == TaskType.盘点)
                                {
                                    container.IsLock = ContainerLock.盘点锁;
                                }
                                else
                                {
                                    container.IsLock = ContainerLock.任务锁;
                                }
                            }
                        }
                        else
                        {
                            taskDetail.Status = TaskStatus.下达任务;
                        }

                        if (station != null)
                        {
                            _unitWork.Update(station);
                        }

                        if (location != null)
                        {
                            _unitWork.Update<Location>(n => n.Id == location.Id && n.Version == location.Version, n => new Location
                            {
                                Status = location.Status,
                                UpdateBy = "wms_wcs",
                                UpdateTime = DateTime.Now
                            });
                        }

                        if (container != null)
                        {
                            _unitWork.Update(container);
                        }

                        taskDetail.Error = "";
                        _unitWork.Update(taskDetail);

                        Task task = null;
                        if (!_unitWork.IsExist<TaskDetail>(n => n.TaskNo == taskDetail.TaskNo && n.Status > taskDetail.Status))
                        {
                            task = _unitWork.Find<Task>(n => n.TaskNo == taskDetail.TaskNo).FirstOrDefault();
                            if (task != null)
                            {
                                task.FirstStatus = taskDetail.Status;
                            }
                            _unitWork.Update(task);
                        }

                        if (!_unitWork.IsExist<TaskDetail>(n => n.TaskNo == taskDetail.TaskNo && n.Status < taskDetail.Status))
                        {
                            task = _unitWork.Find<Task>(n => n.TaskNo == taskDetail.TaskNo).FirstOrDefault();
                            if (task != null)
                            {
                                task.LastStatus = taskDetail.Status;
                            }
                            _unitWork.Update(task);
                        }

                        List<TaskDetail> list = new List<TaskDetail>();
                        if (taskDetail.TaskType == TaskType.容器出库 || taskDetail.TaskType == TaskType.直接出库)
                        {
                            list = _unitWork.Find<TaskDetail>(n => n.ContainerCode == taskDetail.ContainerCode && (taskDetail.TaskType == TaskType.整盘出库 || taskDetail.TaskType == TaskType.分拣出库)).ToList();
                        }
                        else if (taskDetail.TaskType == TaskType.容器回库)
                        {
                            list = _unitWork.Find<TaskDetail>(n => n.ContainerCode == taskDetail.ContainerCode && (taskDetail.TaskType == TaskType.整盘入库)).ToList();
                        }

                        if (list.Count > 0)
                        {
                            foreach (TaskDetail td in list)
                            {
                                td.Status = TaskStatus.下达任务;
                                if (!_unitWork.IsExist<TaskDetail>(n => n.TaskNo == td.TaskNo && n.Status > taskDetail.Status))
                                {
                                    task = _unitWork.Find<Task>(n => n.TaskNo == td.TaskNo).FirstOrDefault();
                                    if (task != null)
                                    {
                                        task.FirstStatus = td.Status;
                                    }
                                    _unitWork.Update(task);
                                }

                                if (!_unitWork.IsExist<TaskDetail>(n => n.TaskNo == td.TaskNo && n.Status < taskDetail.Status))
                                {
                                    task = _unitWork.Find<Task>(n => n.TaskNo == td.TaskNo).FirstOrDefault();
                                    if (task != null)
                                    {
                                        task.LastStatus = td.Status;
                                    }
                                    _unitWork.Update(task);
                                }
                                _unitWork.Update(td);
                            }
                        }

                    }
                    else
                    {
                        taskDetail.Error = "WCS报错:" + _WCSResponse.Message + ":" + DateTime.Now.ToString("HH:mm:ss");
                        _unitWork.Update(taskDetail);
                        continue;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            #endregion
        }
    }
}