IHeightWeghtLocationGetApp.cs 3.37 KB
using System;
using System.Collections.Generic;
using System.Linq;
using Infrastructure;
using WebRepository;

namespace WebApp
{
    /// <summary>
    /// WCS根据高度和重量请求对应仓位接口App
    /// </summary>

    public partial class IHeightWeghtLocationGetApp : ApiApp
    {

        public IHeightWeghtLocationGetApp(IUnitWork unitWork, IAuth auth, BaseDBContext context) : base(unitWork, auth, context)
        {

        }

        public Response<Location> GetHeightLocation(string taskno, decimal height, decimal weight)
        {
            using (var tran = _context.Database.BeginTransaction())
            {
                Response<Location> Response = new Response<Location>();
                try
                {
                    decimal? vweight = weight;
                    Location loc = new Location();
                    if (vweight != null && vweight.HasValue)
                    {
                        loc = _unitWork.Find<Location>(u => u.Status == LocationStatus.空仓位 && u.IsStop == false && u.MaxHeight == height && u.MaxWeight == vweight).OrderBy(a => a.Code).FirstOrDefault();
                    }
                    else
                    {
                        loc = _unitWork.Find<Location>(u => u.Status == LocationStatus.空仓位 && u.IsStop == false && u.MaxHeight == height).OrderBy(a => a.Code).FirstOrDefault();
                    }

                    if (loc == null)
                    {
                        Response.Code = 500;
                        Response.Status = false;
                        Response.Message = "仓库中没有与之高度和重量相匹配的空库位!";
                    }
                    else
                    {
                        //更新托盘回库的目的仓位
                        TaskDetail taskDetail = _unitWork.Find<TaskDetail>(u => u.TaskNo == taskno).FirstOrDefault();
                        taskDetail.DestinationLocation = loc.Code;
                        taskDetail.UpdateBy = "wms";
                        taskDetail.UpdateTime = DateTime.Now;
                        _unitWork.Update(taskDetail);


                        Location oldlocation = _unitWork.Find<Location>(u => u.Code == taskDetail.DestinationLocation).FirstOrDefault();
                        //释放原仓位
                        oldlocation.Status = LocationStatus.空仓位;
                        oldlocation.UpdateBy = "wms";
                        oldlocation.UpdateTime = DateTime.Now;
                        _unitWork.Update(oldlocation);

                        //锁定此新仓位
                        loc.Status = LocationStatus.任务锁定中;
                        loc.UpdateTime = DateTime.Now;
                        if (loc.UpdateBy == null)
                        {
                            loc.UpdateBy = "wms";
                            loc.UpdateTime = DateTime.Now;
                        }
                        _unitWork.Update(loc);
                        tran.Commit();

                        Response.Result = loc;
                    }
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    Response.Code = 500;
                    Response.Status = false;
                    Response.Message = ex.Message;
                }

                return Response;
            }
        }
    }
}