InventoryApp.cs 13.1 KB
using Infrastructure;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using WebRepository;

namespace WebApp
{
    /// <summary>
    /// 库存表
    /// </summary>

    public partial class InventoryApp
    {
        private IUnitWork _unitWork;
        public IRepository<Inventory> _app;
        public IRepository<Task> _appt;
        public IRepository<TaskDetail> _apptd;
        private static IHostingEnvironment _hostingEnvironment;
        ExcelHelper imp = new ExcelHelper(_hostingEnvironment);

        public InventoryApp(IUnitWork unitWork, IRepository<Inventory> repository, IRepository<Task> repositoryt, IRepository<TaskDetail> repositorytd, IHostingEnvironment hostingEnvironment)
        {
            _unitWork = unitWork;
            _app = repository;
            _appt = repositoryt;
            _apptd = repositorytd;
            _hostingEnvironment = hostingEnvironment;
        }

        public InventoryApp SetLoginInfo(LoginInfo loginInfo)
        {
            _app._loginInfo = loginInfo;
            _appt._loginInfo = loginInfo;
            _apptd._loginInfo = loginInfo;
            return this;
        }

        public TableData Load(PageReq pageRequest, Inventory entity)
        {
            return _app.Load(pageRequest, entity);
        }

        public void Ins(Inventory entity)
        {
            _app.Add(entity);
        }

        public void Upd(Inventory entity)
        {
            _app.Update(entity);
        }

        public void DelByIds(int[] ids)
        {
            _app.Delete(u => ids.Contains(u.Id.Value));
        }

        public Inventory FindSingle(Expression<Func<Inventory, bool>> exp)
        {
            return _app.FindSingle(exp);
        }

        public IQueryable<Inventory> Find(Expression<Func<Inventory, bool>> exp)
        {
            return _app.Find(exp);
        }

        public Response ImportIn(IFormFile excelfile)
        {
            Response result = new Infrastructure.Response();
            List<Inventory> exp = imp.ConvertToModel<Inventory>(excelfile);
            string sErrorMsg = "";

            for (int i = 0; i < exp.Count; i++)
            {
                try
                {
                    Inventory e = exp[i];
                    e.Id = null;
                    _app.Add(e);
                }
                catch (Exception ex)
                {
                    sErrorMsg += "第" + (i + 2) + "行:" + ex.Message + "<br>";
                    result.Message = sErrorMsg;
                    break;
                }
            }
            if (sErrorMsg.Equals(string.Empty))
            {
                if (exp.Count == 0)
                {
                    sErrorMsg += "没有发现有效数据, 请确定模板是否正确, 或是否有填充数据!";
                    result.Message = sErrorMsg;
                }
                else
                {
                    result.Message = "导入完成";
                }
            }
            else
            {
                result.Status = false;
                result.Message = result.Message;
            }
            return result;
        }

        public TableData ExportData(Inventory entity)
        {
            return _app.ExportData(entity);
        }

        public TableData Query(Inventory entity)
        {
            var result = new TableData();
            var data = _app.Find(EntityToExpression<Inventory>.GetExpressions(entity));

            GetData(data, result);
            result.count = data.Count();

            return result;
        }

        public void GetData(IQueryable<Inventory> data, TableData result, PageReq pageRequest = null)
        {
            _app.GetData(data, result, pageRequest);
        }

        #region 自定义方法
        /// <summary>
        /// 出库查看
        /// </summary>
        /// <param name="entitys"></param>
        public void GoodsOut(List<Inventory> entitys)
        {
            var taskNo = _app.GetTaskNo(TaskNo.出库自动分配);
            var task = new Task
            {
                TaskNo = taskNo,
                OrderCode = taskNo,
                BusinessType = BusinessType.出库_其他出库单
            };
            task.FirstStatus = task.LastStatus = TaskStatus.待下发任务;

            _unitWork.Add(task);

            foreach (var item in entitys)
            {
                var taskdetail = new TaskDetail
                {
                    TaskNo = taskNo,
                    OrderCode = taskNo,
                    TaskType = TaskType.出库查看,
                    ContainerCode = item.ContainerCode,
                    MaterialCode = item.MaterialCode,
                    MaterialName = _unitWork.Find<Material>(n => n.Code == item.MaterialCode).Select(a => a.Name).FirstOrDefault(),
                    OderQty = item.Qty,
                    HadQty = 0,
                    ContainerQty = 0,
                    InventoryId = item.Id,
                    SourceLocation = item.LocationCode,
                    Roadway = _unitWork.FindSingle<Location>(n => n.Code == item.LocationCode).Roadway,
                    Status = TaskStatus.待下发任务,
                    Priority = 0,
                    CreateTime = DateTime.Now
            };
                _unitWork.Add(taskdetail);


                //建立盘点容器出库任务
                int ptdcount = _unitWork.Find<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.ContainerCode == item.ContainerCode && n.TaskType == TaskType.容器出库).Count();
                if (ptdcount < 1)
                {

                    Task ptask = new Task();
                    TaskDetail ptaskDetail = new TaskDetail();
                    var ctaskNo = _app.GetTaskNo(TaskNo.查看容器出库);
                    ptask.TaskNo = ctaskNo;
                    ptask.OrderCode = ctaskNo;
                    ptask.BusinessType = BusinessType.出库_其他出库单;
                    ptask.FirstStatus = TaskStatus.待下发任务;
                    ptask.LastStatus = TaskStatus.待下发任务;
                    ptask.CreateTime = DateTime.Now;
                    _appt.Add(ptask);

                    ptaskDetail.TaskNo = ctaskNo;
                    ptaskDetail.OrderCode = ctaskNo;
                    ptaskDetail.TaskType = TaskType.出库查看;
                    ptaskDetail.ContainerCode = item.ContainerCode;
                    ptaskDetail.SourceLocation = item.LocationCode;
                    ptaskDetail.OderQty = 0;
                    ptaskDetail.ContainerQty = 0;
                    ptaskDetail.HadQty = 0;
                    ptaskDetail.Roadway = _unitWork.Find<Location>(n => n.Code == item.LocationCode).Select(a => a.Roadway).FirstOrDefault();
                    ptaskDetail.Status = TaskStatus.待下发任务;
                    ptaskDetail.Priority = 0;
                    ptaskDetail.CreateTime = DateTime.Now;
                    _apptd.Add(ptaskDetail);

                }

            }

        }

        public TableData CheckLoad(PageReq pageRequest, Inventory entity)
        {
            var result = new TableData();
            var inventory = _app.Find(EntityToExpression<Inventory>.GetExpressions(entity)).Where(u => u.TaskStatus != "1");

            GetData(inventory, result, pageRequest);
            result.count = inventory.Count();

            return result;
        }

        public TableData FindInventoryApp(string materialCode)
        {
            TableData tab = new TableData();
            try
            {
                List<Inventory> invs = (from inventory in _unitWork.Find<Inventory>(n => n.MaterialCode == materialCode && n.ContainerStatus == ContainerStatus.)
                                 join container in _unitWork.Find<Container>(x => x.IsLock == 0)
                                 on inventory.ContainerCode equals container.Code
                                 select inventory).ToList();
                tab.data = invs;
            }
            catch (Exception)
            {
                tab.data = null;
            }
            return tab;
        }
        /// <summary>
        /// 直接出库
        /// </summary>
        /// <param name="entitys"></param>
        /// <param name="station"></param>
        /// <returns></returns>
        public Response TaskOutNoBack(List<Inventory> entitys,string station)
        {
            Response response = new Response();
            try
            {
                foreach (var item in entitys)
                {
                    //建立盘点容器出库任务
                    int ptdcount = _unitWork.Find<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.ContainerCode == item.ContainerCode).Count();
                    if (ptdcount < 1)
                    {
                        Task ptask = new Task();
                        var ctaskNo = _app.GetTaskNo(TaskNo.容器出库);
                        ptask.TaskNo = ctaskNo;
                        ptask.OrderCode = ctaskNo;
                        ptask.BusinessType = BusinessType.出库_其他出库单;
                        ptask.FirstStatus = TaskStatus.待下发任务;
                        ptask.LastStatus = TaskStatus.待下发任务;
                        ptask.CreateTime = DateTime.Now;


                        TaskDetail ptaskDetail = new TaskDetail();
                        Station stat = _unitWork.Find<Station>(n => n.Code == item.LocationCode && n.Type == StationType.暂存区站台).FirstOrDefault();
                        if (stat != null)
                        {
                            ptaskDetail.TaskType = TaskType.站台到站台;
                            ptaskDetail.Roadway = _unitWork.Find<StationRoadway>(n => n.StationCode == item.LocationCode).Select(a => a.RoadWay).FirstOrDefault();
                        }
                        else
                        {
                            ptaskDetail.TaskType = TaskType.直接出库;
                            ptaskDetail.Roadway = _unitWork.Find<Location>(n => n.Code == item.LocationCode).Select(a => a.Roadway).FirstOrDefault();
                        }
                        ptaskDetail.TaskNo = ctaskNo;
                        ptaskDetail.OrderCode = ctaskNo;
                        ptaskDetail.ContainerCode = item.ContainerCode;
                        ptaskDetail.SourceLocation = item.LocationCode;
                        if (_unitWork.IsExist<Station>(n => n.Code == station && n.Type == StationType.暂存区站台)) {
                           Station s = (from sta in _unitWork.Find<Station>(n => n.Type == StationType.暂存区站台 && n.Containercode == "")
                                                              join sr in _unitWork.Find<StationRoadway>(a => a.RoadWay == ptaskDetail.Roadway)
                                                              on sta.Code equals sr.StationCode
                                                              select sta).FirstOrDefault();
                            if (s != null)
                            {
                                s.Containercode = item.ContainerCode;
                                _unitWork.Update(s);
                            }
                            else
                            {
                                response.Code = 300;
                                response.Message += item.ContainerCode + "未找到缓存站台!";
                                continue;
                            }
                            ptaskDetail.DestinationLocation = s.Code;
                            ptaskDetail.Station = s.Code;
                        }
                        else
                        {
                            ptaskDetail.DestinationLocation = station;
                            ptaskDetail.Station = station;
                        }
                        _appt.Add(ptask);

                        ptaskDetail.OderQty = 0;
                        ptaskDetail.ContainerQty = 0;
                        ptaskDetail.HadQty = 0;
                        ptaskDetail.Status = TaskStatus.待下发任务;
                        ptaskDetail.Priority = 0;
                        ptaskDetail.CreateTime = DateTime.Now;

                        _apptd.Add(ptaskDetail);
                    }
                    else if (_unitWork.IsExist<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.ContainerCode == item.ContainerCode && (n.TaskType != TaskType.容器出库 || n.TaskType != TaskType.直接出库)))
                    {
                        response.Code = 300;
                        response.Message += item.ContainerCode + "有别的任务正在执行!";
                        continue;
                    }
                }
            }
            catch (Exception ex)
            {
                response.Code = 300;
                response.Message = ex.Message;
            }
            return response;
        }
        #endregion
    }
}