IEmptyPalletOutApp.cs 10.3 KB
using System;
using System.Linq;
using Infrastructure;
using WebRepository;

namespace WebApp
{
    /// <summary>
    /// 空栈板补给接口App
    /// </summary>
    public partial class IEmptyPalletOutApp : ApiApp
    {
        public IRepository<ReceiptHeader> _app;
        public IEmptyPalletOutApp(IUnitWork unitWork, IAuth auth, BaseDBContext context, IRepository<ReceiptHeader> app) : base(unitWork, auth, context)
        {
            _app = app;
        }

        public Response EmptyPalletOutApp(string station, string type)
        {
            Response response = new Response();
            Station sta = _unitWork.Find<Station>(N => N.Code == station).FirstOrDefault(); 
            try
            {
                StationRoadway stationRoadway = null;
                if (sta == null)
                {
                    response.Code = 300;
                    response.Message = station + "未找到该站台!";
                    return response;
                }

                if (sta.Type == StationType.PP片上胶站台)
                {
                    //用PP片入库位置找出PP片空栈板补给仓位  
                    StationToStation stationToStation = _unitWork.Find<StationToStation>(n => n.EndStation == station).FirstOrDefault();
                    stationRoadway = (from sr in _unitWork.Find<StationRoadway>(n => n.StationCode == stationToStation.StartStation)
                                                     join st in _unitWork.Find<Station>(n => n.Code == stationToStation.StartStation && n.IsEmpty == 1 && n.Containercode == "")
                                                     on sr.StationCode equals st.Code
                                                     select sr).FirstOrDefault();
                }
                else
                {
                    stationRoadway = (from sr in _unitWork.Find<StationRoadway>(n => n.StationCode == station)
                                                     join st in _unitWork.Find<Station>(n => n.Code == station && n.IsEmpty == 1 && n.Containercode == "")
                                                     on sr.StationCode equals st.Code
                                                     select sr).FirstOrDefault();
                }

                if (stationRoadway == null)
                {
                    response.Code = 300;
                    response.Message = station + "状态不正确!";
                    return response;
                }

                int count = _unitWork.Find<TaskDetail>(u => u.DestinationLocation == stationRoadway.StationCode && u.Status >= TaskStatus.新建任务 && u.Status < TaskStatus.已经完成 && (u.TaskType == TaskType.容器出库 || u.TaskType == TaskType.空容器出库 || u.TaskType == TaskType.站台到站台)).Count();
                if (count > 0)
                {
                    response.Message = "已有该站台出库任务!";
                    return response;
                }

                if (stationRoadway != null)
                {
                    if (stationRoadway.RoadWay == 3)
                    {
                        Station s = _unitWork.Find<Station>(n => n.Code == "InStationC03").FirstOrDefault();
                        if ((s.Containercode != "" || _unitWork.IsExist<TaskDetail>(n => n.DestinationLocation == s.Code && n.TaskType == TaskType.空容器出库)) && sta.Type == StationType.PP片上胶站台 && !_unitWork.IsExist<TaskDetail>(n => n.ContainerCode == s.Containercode && n.ContainerCode != "" && n.TaskType == TaskType.站台到站台))
                        {
                            Task ptask = new Task();
                            TaskDetail ptaskDetail = new TaskDetail();
                            var taskNo = _app.GetTaskNo(TaskNo.空板补充);
                            ptask.TaskNo = taskNo;
                            ptask.OrderCode = taskNo;
                            ptask.BusinessType = BusinessType.出库_其他出库单;
                            ptask.FirstStatus = TaskStatus.待下发任务;
                            ptask.LastStatus = TaskStatus.待下发任务;
                            ptask.CreateTime = DateTime.Now;
                            _unitWork.Add(ptask);

                            ptaskDetail.TaskNo = taskNo;
                            ptaskDetail.OrderCode = taskNo;
                            ptaskDetail.TaskType = TaskType.站台到站台;
                            if (s.Containercode == "")
                            {
                                ptaskDetail.ContainerCode = _unitWork.Find<TaskDetail>(n => n.DestinationLocation == s.Code && n.TaskType == TaskType.空容器出库).Select(a => a.ContainerCode).FirstOrDefault();
                            }
                            else
                            {
                                ptaskDetail.ContainerCode = s.Containercode;
                            }
                            ptaskDetail.SourceLocation = s.Code;
                            ptaskDetail.DestinationLocation = stationRoadway.StationCode;
                            ptaskDetail.OderQty = 0;
                            ptaskDetail.ContainerQty = 0;
                            ptaskDetail.HadQty = 0;
                            ptaskDetail.Roadway = stationRoadway.RoadWay;
                            ptaskDetail.Station = stationRoadway.StationCode;
                            if (_unitWork.IsExist<TaskDetail>(n => n.TaskType == TaskType.空容器出库 && n.DestinationLocation == s.Code))
                            {
                                ptaskDetail.Status = TaskStatus.新建任务;
                            }
                            else
                            {
                                ptaskDetail.Status = TaskStatus.待下发任务;
                            }
                            ptaskDetail.Priority = 20;
                            ptaskDetail.CreateTime = DateTime.Now;
                            _unitWork.Add(ptaskDetail);
                            return response;
                        }
                    }
                    var container = (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 == stationRoadway.RoadWay && n.Status == LocationStatus.空容器)
                                     on con.LocationCode equals loc.Code
                                     select con).FirstOrDefault();
                    if (container == null)
                    {
                        response.Code = 300;
                        response.Message = "库内无空栈板!";
                        return response;
                    }
                    //建立容器出库任务
                    if (!_unitWork.IsExist<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.ContainerCode == container.Code))
                    {
                        int ptdcount = _unitWork.Find<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.DestinationLocation == stationRoadway.StationCode).Count();
                        if (ptdcount < 1)
                        {
                            Task ptask = new Task();
                            TaskDetail ptaskDetail = new TaskDetail();
                            var taskNo = _app.GetTaskNo(TaskNo.空板补充);
                            ptask.TaskNo = taskNo;
                            ptask.OrderCode = taskNo;
                            ptask.BusinessType = BusinessType.出库_其他出库单;
                            ptask.FirstStatus = TaskStatus.待下发任务;
                            ptask.LastStatus = TaskStatus.待下发任务;
                            ptask.CreateTime = DateTime.Now;
                            _unitWork.Add(ptask);

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

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

                            Location loc = _unitWork.Find<Location>(n => n.Code == container.LocationCode).FirstOrDefault();
                            _unitWork.Update<Location>(n => n.Id == loc.Id && n.Version == loc.Version, n => new Location
                            {
                                Status = LocationStatus.任务锁定中
                            });
                        }
                        else
                        {
                            response.Code = 300;
                            response.Message = "异常站台:" + stationRoadway.StationCode + "有任务!";
                            return response;
                        }
                    }
                    else
                    {
                        response.Code = 300;
                        response.Message = "异常" + container.Code + "栈板有任务!";
                        return response;
                    }

                }
                else
                {
                    response.Code = 300;
                    response.Message = "此站台暂时不能补给空栈板!";
                    return response;
                }
            }
            catch (Exception ex)
            {
                response.Code = 300;
                response.Message = ex.Message;
            }
            return response;
        }
    }
}