IEmptyPalletOutApp.cs 5.68 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 string EmptyPalletOutApp(string station, string type)
        {
            Response response = new Response();
            try
            {
                StationRoadway 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)
                {
                        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 JsonHelper.Instance.Serialize(response);
                        }
                        //建立容器出库任务
                        if (!_unitWork.IsExist<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.MaterialCode == container.Code))
                        {
                            int ptdcount = _unitWork.Find<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.ContainerCode == container.Code && n.TaskType == TaskType.容器出库).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.待下发任务;
                                _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;
                                _unitWork.Add(ptaskDetail);

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

                                Location loc = _unitWork.Find<Location>(n => n.Code == container.LocationCode).FirstOrDefault();
                                loc.Status = LocationStatus.任务锁定中;
                                _unitWork.Update(loc);
                            }
                            else
                            {
                                response.Code = 300;
                                response.Message = "异常" + container.Code + "栈板有任务!";
                                return JsonHelper.Instance.Serialize(response);
                            }
                        }
                        else
                        {
                            var t = _unitWork.Find<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.MaterialCode == container.Code).ToList();
                            response.Code = 300;
                            response.Message = "异常" + container.Code + "栈板有任务!";
                            return JsonHelper.Instance.Serialize(response);
                        }

                }
                else
                {
                    response.Code = 300;
                    response.Message = "无此站台!";
                    return JsonHelper.Instance.Serialize(response);
                }
            }
            catch (Exception ex)
            {
                response.Code = 300;
                response.Message = ex.Message;
            }
            return JsonHelper.Instance.Serialize(response);
        }
    }
}