InventoryApp.cs 8 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 自定义方法
        public void GoodsOut(List<Inventory> entitys)
        {
            var taskNo = _app.GetTaskNo(TaskNo.出库自动分配);
            var task = new Task();
            task.TaskNo = taskNo;
            task.OrderCode = taskNo;
            task.BusinessType = BusinessType.出库_其他出库单;
            task.FirstStatus = task.LastStatus = TaskStatus.待下发任务;

            _unitWork.Add(task);

            foreach (var item in entitys)
            {
                var taskdetail = new TaskDetail();
                taskdetail.TaskNo = taskNo;
                taskdetail.OrderCode = taskNo;
                taskdetail.TaskType = TaskType.出库查看;
                taskdetail.ContainerCode = item.ContainerCode;
                taskdetail.MaterialCode = item.MaterialCode;
                taskdetail.MaterialName = _unitWork.Find<Material>(n=>n.Code==item.MaterialCode).Select(a=>a.Name).FirstOrDefault();
                taskdetail.OderQty = item.Qty;
                taskdetail.HadQty = 0;
                taskdetail.ContainerQty = 0;
                taskdetail.InventoryId = item.Id;
                taskdetail.SourceLocation = item.LocationCode;
                taskdetail.Roadway = _unitWork.FindSingle<Location>(n => n.Code == item.LocationCode).Roadway;
                taskdetail.Status = TaskStatus.待下发任务;
                taskdetail.Priority = 0;
                taskdetail.IsEmptyOut = 0;
                _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.待下发任务;
                    _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;
                    _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;
        }
        #endregion
    }
}