EmptyOutAction.cs 8.89 KB
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Infrastructure;
using Quartz;
using WebApp;
using WebRepository;

namespace WebMvc
{
    /// <summary>
    /// 空板补给
    /// </summary>
    [PersistJobDataAfterExecution]
    [DisallowConcurrentExecution]
    public class EmptyOutAction
    {
        private string ConnString { set; get; }
        IJobExecutionContext Context { set; get; }

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

        public void Execute(JobContainer jobContainer)
        {
            UnitWork _unitWork = new UnitWork(AppSettingsJson.JobContext(ConnString));
            try
            {
                List<Station> stations = _unitWork.Find<Station>(n => n.Type == StationType.空板暂存区站台).ToList();
                string type = "p_1";
                foreach (Station station in stations)
                {
                    //正在执行的空板补给任务
                    int EmptyCount = _unitWork.Find<TaskDetail>(n => n.DestinationLocation == station.Code).Count();
                    //正在执行空板任务小于最大数量
                    if (EmptyCount < station.MaxNum)
                    {
                        StationRoadway stationRoadway = _unitWork.Find<StationRoadway>(n => n.StationCode == station.Code).FirstOrDefault();
                        if (stationRoadway != null)
                        {
                            //可用的空容器
                            Container container = null;
                            int num = _unitWork.Find<StoreStation>(n => n.StationCode == station.Code).Count();
                            while (num < station.MaxNum)
                            {
                                if (stationRoadway.StationPlace == StationPlace.巷道北面)
                                {
                                    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
                                                      orderby  loc.RowIndex == 2 ? 0 : loc.RowIndex, loc.Row
                                                 select con).FirstOrDefault();
                                }
                                else if (stationRoadway.StationPlace == StationPlace.巷道南面)
                                {
                                    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
                                                      orderby  loc.RowIndex == 2 ? 0 : loc.RowIndex, loc.Row descending
                                                      select con).FirstOrDefault();
                                }
                                else
                                {
                                    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
                                                      orderby  loc.RowIndex == 2 ? 0 : loc.RowIndex
                                                      select con).FirstOrDefault();
                                }

                                if (container != null)
                                {
                                    //建立容器出库任务
                                    //任务表中不存在当前托盘的任务
                                    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 == station.Code).Count();
                                        ////如果小于最大值
                                        //if (ptdcount < station.MaxNum)
                                        //{
                                            Task ptask = new Task();
                                            TaskDetail ptaskDetail = new TaskDetail();
                                            var taskNo = "ZD" + _unitWork.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 = station.Code;
                                            ptaskDetail.OderQty = 0;
                                            ptaskDetail.ContainerQty = 0;
                                            ptaskDetail.HadQty = 0;
                                            ptaskDetail.Roadway = stationRoadway.RoadWay;
                                            ptaskDetail.Station = station.Code;
                                            ptaskDetail.Status = TaskStatus.待下发任务;
                                            ptaskDetail.Priority = 99;
                                            ptaskDetail.CreateTime = DateTime.Now;
                                            _unitWork.Add(ptaskDetail);

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

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

                                            StoreStation storeStation = new StoreStation
                                            {
                                                StationCode = station.Code,
                                                ContainerCode = container.Code,
                                                Status = StoreStationStatus.任务中,
                                                CreateTime = DateTime.Now
                                            };
                                            _unitWork.Add(storeStation);
                                            break;
                                        //}
                                        //else
                                        //{
                                        //    break;
                                        //}
                                    }
                                    
                                }
                                //num = _unitWork.Find<StoreStation>(n => n.StationCode == station.Code).Count();
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}