ShipmentContainerHeaderApp.cs 16.6 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Infrastructure;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json.Linq;
using WebRepository;

namespace WebApp
{
    /// <summary>
    /// 出库组盘明细表
    /// </summary>

    public partial class ShipmentContainerHeaderApp
    {
        private IUnitWork _unitWork;
        public IRepository<ShipmentContainerHeader> _app;
        public IRepository<ShipmentDetail> _appsd;
        public IRepository<ShipmentHeader> _appsh;
        public IRepository<Task> _appt;
        public IRepository<TaskDetail> _apptd;
        private BaseDBContext _context;
        private static IHostingEnvironment _hostingEnvironment;
        ExcelHelper imp = new ExcelHelper(_hostingEnvironment);

        public ShipmentContainerHeaderApp(IUnitWork unitWork, IRepository<ShipmentContainerHeader> repository, IRepository<ShipmentHeader> repositorysh, IRepository<Task> repositoryt, IRepository<TaskDetail> repositorytd, IRepository<ShipmentDetail> repositorysd, IHostingEnvironment hostingEnvironment, BaseDBContext context)
        {
            _unitWork = unitWork;
            _app = repository;
            _appsh = repositorysh;
            _appsd = repositorysd;
            _appt = repositoryt;
            _apptd = repositorytd;
            _context = context;
            _hostingEnvironment = hostingEnvironment;
        }

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

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

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

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

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

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

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

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

            for (int i = 0; i < exp.Count; i++)
            {
                try
                {
                    ShipmentContainerHeader 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(ShipmentContainerHeader entity)
        {
            return _app.ExportData(entity);
        }

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

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

            return result;
        }

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

        //任务添加
        public bool AddTaskInfo(ShipmentDetail shipmentDetail, Inventory inventory, int dividedqty)
        {
            using (var tran = _context.Database.BeginTransaction())
            {
                try
                {
                    ShipmentHeader shipmentHeader = _unitWork.Find<ShipmentHeader>(n => n.Code == shipmentDetail.ShipmentCode).FirstOrDefault();
                    Task task = new Task();
                    TaskDetail taskDetail = new TaskDetail();


                    var tno = "";
                    int tcount = _unitWork.Find<Task>(n => n.OrderCode == shipmentDetail.ShipmentCode).Count();
                    if (tcount > 0)
                    {
                        var tasknum = _unitWork.Find<Task>(n => n.OrderCode == shipmentDetail.ShipmentCode).Select(a => a.TaskNo).First();
                        tno = tasknum;
                    }
                    else
                    {
                        tno = _app.GetTaskNo(TaskNo.出库手动分配);
                        task.TaskNo = tno;
                        task.SourceCode = shipmentDetail.SourceCode;
                        task.OrderCode = shipmentDetail.ShipmentCode;
                        task.BusinessType = shipmentHeader.Type;
                        task.TotalQty = shipmentHeader.TotalQty;
                        task.TotalLines = shipmentHeader.TotalLines;
                        task.FirstStatus = shipmentHeader.FirstStatus;
                        task.LastStatus = shipmentHeader.LastStatus;
                        task.Project = shipmentDetail.Project;
                        task.CreateTime = DateTime.Now;
                        _appt.Add(task);
                    }

                    //var outtype = _unitWork.Find<SysDictData>(u => u.DictType == "shipmentType").Select(a => a.DictValue).ToList();
                    if (_unitWork.IsExist<TaskDetail>(n => n.MaterialCode == shipmentDetail.MaterialCode && (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.ContainerCode == inventory.ContainerCode && !n.TaskNo.StartsWith("I")))
                    {
                        decimal? taskhadqty = _unitWork.Find<TaskDetail>(n => n.MaterialCode == shipmentDetail.MaterialCode && (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.ContainerCode == inventory.ContainerCode && !n.TaskNo.StartsWith("I")).Select(a => a.ContainerQty).Sum();
                        inventory.Qty = inventory.Qty - taskhadqty;
                    }
                    else
                    {
                        inventory.Qty = inventory.Qty - 0;
                    }

                    taskDetail.TaskNo = tno;
                    taskDetail.SourceCode = shipmentDetail.SourceCode;
                    taskDetail.OrderCode = shipmentDetail.ShipmentCode;
                    taskDetail.ContainerCode = inventory.ContainerCode;
                    if (inventory.Qty > dividedqty)
                    {
                        taskDetail.TaskType = TaskType.分拣出库;
                    }
                    else
                    {
                        taskDetail.TaskType = TaskType.整盘出库;
                    }
                    taskDetail.MaterialCode = shipmentDetail.MaterialCode;
                    taskDetail.MaterialName = _unitWork.Find<Material>(n => n.Code == shipmentDetail.MaterialCode).Select(a => a.Name).First();
                    taskDetail.Batch = shipmentDetail.Batch;
                    taskDetail.Lot = shipmentDetail.Lot;
                    taskDetail.OderQty = shipmentDetail.Qty;
                    taskDetail.ContainerQty = dividedqty;
                    taskDetail.SourceLocation = inventory.LocationCode;
                    taskDetail.DestinationLocation = shipmentHeader.Station;
                    taskDetail.HadQty = 0;
                    int lcount = _unitWork.Find<Location>(n => n.Code == inventory.LocationCode).Count();
                    if (lcount > 0)
                    {
                        taskDetail.Roadway = _unitWork.Find<Location>(n => n.Code == inventory.LocationCode).Select(a => a.Roadway).First();
                    }
                    else
                    {
                        taskDetail.Roadway = 0;
                    }
                    taskDetail.Station = shipmentHeader.Station;
                    taskDetail.Priority = shipmentHeader.Priority;
                    taskDetail.Status = TaskStatus.待下发任务;
                    taskDetail.CreateTime = DateTime.Now;
                    _apptd.Add(taskDetail);

                    //建立容器出库任务
                    if (_unitWork.IsExist<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.MaterialCode == shipmentDetail.MaterialCode && n.OrderCode == shipmentDetail.ShipmentCode))
                    {
                        int ptdcount = _unitWork.Find<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.ContainerCode == taskDetail.ContainerCode && 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.待下发任务;
                            ptask.CreateTime = DateTime.Now;
                            _appt.Add(ptask);

                            ptaskDetail.TaskNo = taskNo;
                            ptaskDetail.OrderCode = taskNo;
                            ptaskDetail.TaskType = TaskType.容器出库;
                            ptaskDetail.ContainerCode = taskDetail.ContainerCode;
                            ptaskDetail.SourceLocation = taskDetail.SourceLocation;
                            ptaskDetail.DestinationLocation = taskDetail.DestinationLocation;
                            ptaskDetail.OderQty = 0;
                            ptaskDetail.ContainerQty = 0;
                            ptaskDetail.HadQty = 0;
                            ptaskDetail.Roadway = taskDetail.Roadway;
                            ptaskDetail.Station = taskDetail.Station;
                            ptaskDetail.Status = TaskStatus.待下发任务;
                            if (taskDetail.Priority != null)
                            {
                                ptaskDetail.Priority = taskDetail.Priority;
                            }
                            else
                            {
                                ptaskDetail.Priority = 0;
                            }
                            ptaskDetail.CreateTime = DateTime.Now;
                            _apptd.Add(ptaskDetail);
                        }
                    }

                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    return false;
                }
                return true;
            }
        }

        public TableData DivideToTask(ShipmentDetail shipmentDtl, Inventory inv, int dqty)
        {
            TableData tab = new TableData();
            tab.code = 200;
            try
            {
                if (inv.TaskStatus != "idle")
                {
                    tab.code = 300;
                    tab.msg = "出库手动分配失败!原因:正在盘点!";
                    return tab;
                }
                if (AddTaskInfo(shipmentDtl, inv, dqty))
                {
                    if (string.IsNullOrEmpty(shipmentDtl.QtyDivided.ToString()))
                    {
                        shipmentDtl.QtyDivided = 0;
                    }
                    shipmentDtl.QtyDivided = shipmentDtl.QtyDivided + dqty;
                    if (shipmentDtl.Qty == shipmentDtl.QtyDivided)
                    {
                        shipmentDtl.Status = ShipmentHeaderStatus.分配完成;
                    }
                    _appsd.Update(shipmentDtl);
                    tab.code = 200;
                    tab.msg = "分配成功!";

                    ShipmentHeader shipmentHeader = _unitWork.Find<ShipmentHeader>(n => n.Code == shipmentDtl.ShipmentCode && n.LastStatus <= ShipmentHeaderStatus.分配完成).FirstOrDefault();
                    int statuscount = _unitWork.Find<ShipmentDetail>(n => n.ShipmentCode == shipmentDtl.ShipmentCode && n.Status < ShipmentHeaderStatus.分配完成 && n.Qty > n.QtyDivided).Count();
                    if (statuscount > 0)
                    {
                        shipmentHeader.FirstStatus = ShipmentHeaderStatus.分配完成;
                        _appsh.Update(shipmentHeader);
                    }
                    else
                    {
                        shipmentHeader.FirstStatus = ShipmentHeaderStatus.分配完成;
                        shipmentHeader.LastStatus = ShipmentHeaderStatus.分配完成;
                        _appsh.Update(shipmentHeader);
                    }
                }
                else
                {
                    tab.code = 300;
                    tab.msg = "出库手动分配失败!";
                }
            }
            catch (Exception ex)
            {
                tab.code = 300;
                tab.msg += ex.Message;
            }
            return tab;
        }

        public TableData GetContainerQtyApp(Inventory inventory)
        {
            var result = new TableData();
            try
            {
                //var outtype = _unitWork.Find<SysDictData>(u => u.DictType == "shipmentType").Select(a => a.DictValue).ToList();
                if (_unitWork.IsExist<TaskDetail>(n => n.MaterialCode == inventory.MaterialCode && n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成 && n.ContainerCode == inventory.ContainerCode && !n.TaskNo.StartsWith("I")))
                {
                    decimal? taskhadqty = _unitWork.Find<TaskDetail>(n => n.MaterialCode == inventory.MaterialCode && n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成 && n.ContainerCode == inventory.ContainerCode && !n.TaskNo.StartsWith("I")).Select(n => n.ContainerQty).Sum();
                    inventory.Qty = inventory.Qty - taskhadqty;
                }
                else
                {
                    inventory.Qty = inventory.Qty - 0;
                }

                JObject jObject = new JObject();
                jObject.Add("yqty", inventory.Qty);
                result.code = 200;
                result.data = jObject;
                result.count = 1;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }

            return result;
        }

        public TableData GetShipmentStatus(string Code)
        {
            var result = new TableData();
            try
            {
                int TotalLines = 0;
                decimal TotalQty = 0;
                decimal TotalQtyCompleted = 0;
                var shipmentDetail = _unitWork.Find<ShipmentDetail>(u => u.ShipmentCode.Equals(Code));
                foreach (var item in shipmentDetail)
                {
                    TotalLines += 1;
                    TotalQty += item.Qty.Value;
                    TotalQtyCompleted += item.QtyDivided.Value;
                }
                JObject jObject = new JObject();
                jObject.Add("TotalLines", TotalLines);
                jObject.Add("TotalQty", TotalQty);
                jObject.Add("TotalQtyCompleted", TotalQtyCompleted);

                result.data = jObject;
                result.count = 1;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }

            return result;
        }
    }
}