chengsc
2024-10-18 1e26a7575d9969e728b0aa01466f490409cb173c
Wms/WMS.BLL/BllPdaServer/PdaCrServer.cs
@@ -2,9 +2,13 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.InterFaceModel;
using Model.ModelDto.BllCheckDto;
using Model.ModelDto.PdaDto;
using Model.ModelVm;
using Newtonsoft.Json;
using SqlSugar;
using Utility.Tools;
using WMS.BLL.LogServer;
using WMS.DAL;
using WMS.Entity.BllAsnEntity;
@@ -139,7 +143,7 @@
                    {
                        PalletNo = palletNo,
                        SkuNo = sku,
                        Qty = (int?)stockDetail.Qty,
                        Qty = stockDetail.Qty,
                        CheckResult = 4,
                    };
@@ -171,7 +175,7 @@
            }
        }
        public void CrSetCheck(string crNo, string crDetail, string palletNo, string boxNo, string boxNo3, string result, int? qty, string isContinue, int userId)
        public void CrSetCheck(string crNo, string crDetail, string palletNo, string boxNo, string boxNo3, string result, decimal? qty, string isContinue, int userId)
        {
            try
            {
@@ -776,7 +780,7 @@
                var storageArea = new List<SysStorageArea>();
                if (string.IsNullOrEmpty(palletNo))
                {
                    storageArea = Db.Queryable<SysStorageArea>().Where(w => w.IsDel == "0" && w.Status == "0" && w.WareHouseNo == "W02").OrderBy(o=>o.AreaNo).ToList();
                    storageArea = Db.Queryable<SysStorageArea>().Where(w => w.IsDel == "0" && w.Status == "0" && w.WareHouseNo == "W02").OrderBy(o => o.AreaNo).ToList();
                }
                else
                {
@@ -798,7 +802,7 @@
                    {
                        throw new Exception("储位信息不存在,请检查!");
                    }
                    if (storageLocat.AreaNo.Contains("B0"))
                    {
                        storageArea = Db.Queryable<SysStorageArea>().Where(w => w.IsDel == "0" && w.Status == "0" && w.AreaNo.Contains("B0") && w.WareHouseNo == "W02").OrderBy(o => o.AreaNo).ToList();
@@ -807,7 +811,7 @@
                    {
                        storageArea = Db.Queryable<SysStorageArea>().Where(w => w.IsDel == "0" && w.Status == "0" && w.AreaNo.Contains("B1") && w.WareHouseNo == "W02").OrderBy(o => o.AreaNo).ToList();
                    }
                }
                }
                return storageArea;
            }
            catch (Exception ex)
@@ -823,7 +827,7 @@
        {
            try
            {
                var allotList = Db.Queryable<BllExportNotice>().Where(m => m.IsDel == "0" && m.Status == "2").Select(m => m.SONo).Distinct().ToList();
                var allotList = Db.Queryable<BllExportNotice>().Where(m => m.IsDel == "0" && (m.Status == "3" || m.Status == "4")).Select(m => m.SONo).Distinct().ToList();
                return allotList;
            }
            catch (Exception ex)
@@ -880,7 +884,7 @@
        /// <param name="palletNo"></param>
        /// <param name="areaNo"></param>
        /// <param name="ruku"></param>
        public void AgvTransport(string palletNo, string areaNo, string ruku, int userId)
        public void AgvTransport(string palletNo, string areaNo, string ruku, int userId, string url)
        {
            if (string.IsNullOrEmpty(palletNo))
            {
@@ -893,7 +897,7 @@
            if (!string.IsNullOrEmpty(areaNo) && !string.IsNullOrEmpty(ruku))
            {
                throw new Exception("目标区域和入库口不能同时选择!");
            }
            }
            try
            {
                string EndLocat = string.Empty;//目标位置                
@@ -902,7 +906,7 @@
                if (log != null)
                {
                    throw new Exception("该托盘已有小车等待执行或正在执行的任务!");
                }
                }
                var stockDetail = Db.Queryable<DataStockDetail>().First(w => w.IsDel == "0" && w.PalletNo == palletNo);
                if (stockDetail == null)
                {
@@ -915,7 +919,7 @@
                    {
                        throw new Exception("所选区域信息不存在,请检查!");
                    }
                    EndLocat = GetLocat(areaNo, stockDetail.SkuNo, stockDetail.LotNo,palletNo);
                    EndLocat = GetLocat(areaNo, stockDetail.SkuNo, stockDetail.LotNo, palletNo);
                }
                else
                {
@@ -950,7 +954,7 @@
                        throw new Exception("目标储位信息不存在,请检查!");
                    }
                }
                //开启事务
                Db.BeginTran();
@@ -980,11 +984,11 @@
                //修改库存明细信息
                stockDetail.Status = "4";//移库锁定
                stockDetail.LockQty = stockDetail.Qty;//锁定库存数量
                //stockDetail.LockQty = stockDetail.Qty;//锁定库存数量
                Db.Updateable(stockDetail).ExecuteCommand();
                //修改库存信息
                stock.LockQty += (decimal)stockDetail.Qty;
                Db.Updateable(stock).ExecuteCommand();
                //stock.LockQty += (decimal)stockDetail.Qty;
                //Db.Updateable(stock).ExecuteCommand();
                //修改起始储位地址状态
                storageLocat.Status = "5";//0:空储位 1:有物品 2:入库中 3:出库中 4:移入中 5:移出中 
@@ -995,15 +999,63 @@
                {
                    storageLocatEnd.Status = "4";//0:空储位 1:有物品 2:入库中 3:出库中 4:移入中 5:移出中 
                    Db.Updateable(storageLocatEnd).ExecuteCommand();
                }
                }
                //添加操作日志记录
                var k = new OperationCrServer().AddLogOperationCr("PDA模块", "AGV转运", palletNo, "移库", $"PDA呼叫小车对托盘号:{palletNo}发起转运", userId);
                //提交事务
                Db.CommitTran();
                #region 呼叫小车代码
                List<AgvSchedulingTask> agvTaskList = new List<AgvSchedulingTask>();
                AgvSchedulingTask agvTask = new AgvSchedulingTask();
                agvTask.ReqCode = taskNo;
                agvTask.TaskTyp = "F01";
                agvTask.WbCode = "";
                agvTask.PositionCodePath = null;
                agvTask.PodCode = "-1";
                agvTaskList.Add(agvTask);
                var IsTrue = false;
                while (IsTrue == true)
                {
                    // 正式运行程序放开
                    string str = "";
                    var list2 = agvTaskList.Select(m => m.ReqCode).ToList();
                    var jsonData = JsonConvert.SerializeObject(agvTaskList);
                    string response = "";
                    try
                    {
                        var time1 = DateTime.Now;//发送时间 .ToString("yyyy-MM-dd HH:mm:ss")
                        response = HttpHelper.DoPost(url, jsonData, "下发给AGV转运命令", "AGV");
                        var time2 = DateTime.Now;//返回时间 .ToString("yyyy-MM-dd HH:mm:ss")
                        //////解析返回数据
                        var agvModel = JsonConvert.DeserializeObject<OutCommanAgvDto>(response);
                        if (agvModel.Code == "0")
                        {
                            //更改任务的发送返回时间//
                            new TaskServer().EditTaskIssueOk(list2, time1, time2);
                            str += "下发成功";
                            IsTrue = true;
                        }
                        if (agvModel.Code == "1")
                        {
                            new TaskServer().EditTaskIssueNo(list2, time1, time2, agvModel.Message);
                            throw new Exception(agvModel.Message);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }
                #endregion
                //提交事务
                Db.CommitTran();
            }
            catch (Exception ex)
            {
@@ -1018,7 +1070,7 @@
        /// <param name="palletNo"></param>
        /// <param name="areaNo"></param>
        /// <param name="userId"></param>
        public void AgvTransport2(string soNo,string palletNo, string areaNo, int userId)
        public List<string> AgvTransport2(string soNo, string palletNo, string areaNo, int userId, string url)
        {
            if (string.IsNullOrEmpty(soNo))
            {
@@ -1032,6 +1084,8 @@
            {
                //开启事务
                Db.BeginTran();
                List<AgvSchedulingTask> agvTaskList = new List<AgvSchedulingTask>();
                var strList = new List<string>();//错误信息集合  //0 :部分托盘已有小车等待执行或正在执行的任务
                //出库单信息
                var notice = Db.Queryable<BllExportNotice>().First(w => w.IsDel == "0" && w.SONo == soNo);
                if (notice == null)
@@ -1051,10 +1105,11 @@
                }
                else//整个出库单叫货
                {
                    string sqlStr = $"select * from DataStockDetail where PalletNo in (select PalletNo from BllExportAllot where IsDel='0' and SONo='{soNo}')";
                    stockDetailList = Db.Ado.SqlQuery<DataStockDetail>(sqlStr).ToList();
                //    string sqlStr = $"select * from DataStockDetail where PalletNo in (select PalletNo from BllExportAllot where IsDel='0' and SONo='{soNo}')";
                //    stockDetailList = Db.Ado.SqlQuery<DataStockDetail>(sqlStr).ToList();
                    stockDetailList = Db.Queryable<DataStockDetail>().Where(w => w.IsDel == "0" && w.SONo == soNo).ToList();
                }
                if (stockDetailList.Count<=0)
                if (stockDetailList.Count <= 0)
                {
                    throw new Exception("托盘明细不存在,请检查!");
                }
@@ -1064,14 +1119,10 @@
                var stock = Db.Queryable<DataStock>().Where(w => w.IsDel == "0");//库存总表
                string EndLocat = string.Empty;//目标位置
                string hasLocatNoList = string.Empty;//已分配的储位
                foreach (var item in stockDetailList)
                {
                    //任务信息
                    var logInfo = log.First(w =>w.PalletNo == item.PalletNo);
                    if (logInfo != null)
                    {
                        throw new Exception($"托盘号:{item.PalletNo}已有小车等待执行或正在执行的任务!");
                    }
                    if (string.IsNullOrEmpty(item.LocatNo))
                    {
                        throw new Exception($"托盘号:{item.PalletNo}的储位信息不存在,请检查!");
@@ -1079,6 +1130,17 @@
                    if (item.WareHouseNo != "W02")
                    {
                        throw new Exception($"托盘号:{item.PalletNo}未在平库内,请检查!");
                    }
                    //任务信息
                    var logInfo = log.First(w => w.PalletNo == item.PalletNo);
                    if (logInfo != null)
                    {
                        if (!strList.Contains("1"))
                        {
                            strList.Add("1");
                        }
                        continue;
                        //throw new Exception($"托盘号:{item.PalletNo}已有小车等待执行或正在执行的任务!");
                    }
                    //起始储位信息
                    var storageLocatBegin = storageLocat.First(w => w.LocatNo == item.LocatNo);
@@ -1118,7 +1180,7 @@
                        Msg = string.Format("转运任务:{0}=>>{1}", item.LocatNo, EndLocat),
                        StartLocat = item.LocatNo,//起始位置
                        EndLocat = EndLocat,//目标位置
                        PalletNo = palletNo,//托盘码
                        PalletNo = item.PalletNo,//托盘码
                        IsSend = 1,//是否可再次下发
                        IsCancel = 1,//是否可取消
                        IsFinish = 1,//是否可完成
@@ -1130,9 +1192,18 @@
                    };
                    Db.Insertable(exTask).ExecuteCommand();
                    AgvSchedulingTask agvTask = new AgvSchedulingTask();
                    agvTask.ReqCode = taskNo;
                    agvTask.TaskTyp = "F01";
                    agvTask.WbCode = "";
                    agvTask.PositionCodePath = null;
                    agvTask.PodCode = "-1";
                    agvTaskList.Add(agvTask);
                    //修改库存明细信息
                    item.Status = "4";//移库锁定
                    item.LockQty = item.Qty;//锁定库存数量
                    //item.LockQty = item.Qty;//锁定库存数量
                    Db.Updateable(item).ExecuteCommand();
                    //修改起始储位地址状态
@@ -1141,11 +1212,16 @@
                    //修改目标储位地址状态
                    storageLocatEnd.Status = "4";//0:空储位 1:有物品 2:入库中 3:出库中 4:移入中 5:移出中 
                    Db.Updateable(storageLocatEnd).ExecuteCommand();
                    if (!strList.Contains("0"))
                    {
                        strList.Add("0");
                    }
                    var stockInfo = stock.First(w => w.SkuNo == item.SkuNo && w.LotNo == item.LotNo);
                    //修改库存信息
                    stockInfo.LockQty += (decimal)item.Qty;//锁定数量
                    Db.Updateable(stockInfo).ExecuteCommand();
                    //var stockInfo = stock.First(w => w.SkuNo == item.SkuNo && w.LotNo == item.LotNo);
                    ////修改库存信息
                    //stockInfo.LockQty += (decimal)item.Qty;//锁定数量
                    //Db.Updateable(stockInfo).ExecuteCommand();
                }
                if (string.IsNullOrEmpty(palletNo))
                {
@@ -1157,12 +1233,52 @@
                    //添加操作日志记录
                    var k = new OperationCrServer().AddLogOperationCr("PDA模块", "AGV转运", palletNo, "移库", $"PDA呼叫小车对托盘号:{palletNo}发起转运", userId);
                }
                //提交事务
                Db.CommitTran();
                #region 呼叫小车代码
                var IsTrue = false;
                while (IsTrue == true)
                {
                    // 正式运行程序放开
                    string str = "";
                    var list2 = agvTaskList.Select(m => m.ReqCode).ToList();
                    var jsonData = JsonConvert.SerializeObject(agvTaskList);
                    string response = "";
                    try
                    {
                        var time1 = DateTime.Now;//发送时间 .ToString("yyyy-MM-dd HH:mm:ss")
                        response = HttpHelper.DoPost(url, jsonData, "下发给AGV转运取货命令", "AGV");
                        var time2 = DateTime.Now;//返回时间 .ToString("yyyy-MM-dd HH:mm:ss")
                        //////解析返回数据
                        var agvModel = JsonConvert.DeserializeObject<OutCommanAgvDto>(response);
                        if (agvModel.Code == "0")
                        {
                            //更改任务的发送返回时间//
                            new TaskServer().EditTaskIssueOk(list2, time1, time2);
                            str += "下发成功";
                            IsTrue = true;
                        }
                        if (agvModel.Code == "1")
                        {
                            new TaskServer().EditTaskIssueNo(list2, time1, time2, agvModel.Message);
                            throw new Exception(agvModel.Message);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }
                #endregion
                //提交事务
                Db.CommitTran();
                return strList;
            }
            catch (Exception ex)
            {
@@ -1177,16 +1293,16 @@
        /// <param name="skuNo"></param>
        /// <param name="lotNo"></param>
        /// <returns></returns>
        private string GetLocat(string areaNo,string skuNo,string lotNo,string palletNo,string hasLocatNoList="")
        private string GetLocat(string areaNo, string skuNo, string lotNo, string palletNo, string hasLocatNoList = "")
        {
            try
            {
                string endLocat = string.Empty;//目标储位
                //当然区域所有储位信息
                                               //当然区域所有储位信息
                var storageLocatList = Db.Queryable<SysStorageLocat>().Where(w => w.WareHouseNo == "W02" && w.AreaNo == areaNo).ToList();
                //同区域同批次物料的储位信息
                List<string> locatList = Db.Queryable<DataStockDetail>().Where(w => w.IsDel == "0" && w.SkuNo == skuNo && w.LotNo == lotNo && w.WareHouseNo == "W02" && w.AreaNo == areaNo && w.PalletNo != palletNo)
                    .OrderByDescending(o=>o.LocatNo).Select(s=>s.LocatNo).Distinct().ToList();
                    .OrderByDescending(o => o.LocatNo).Select(s => s.LocatNo).Distinct().ToList();
                foreach (var item in locatList)
                {
                    var locatInfo = storageLocatList.Where(w => w.LocatNo == item).First();
@@ -1201,7 +1317,7 @@
                if (string.IsNullOrEmpty(endLocat))
                {
                    var locatInfo3 = storageLocatList.Where(w => w.IsDel == "0" && w.Flag == "0" && w.Status == "0" && !hasLocatNoList.Contains(w.LocatNo))
                                .OrderByDescending(m => m.Layer).OrderByDescending(m=>m.Column).OrderByDescending(m=> m.Row).First();
                                .OrderByDescending(m => m.Layer).OrderByDescending(m => m.Column).OrderByDescending(m => m.Row).First();
                    if (locatInfo3 != null)
                    {
@@ -1217,5 +1333,110 @@
        }
        #endregion
        #region 箱码查询
        /// <summary>
        /// 箱码查询
        /// </summary>
        /// <param name="boxNo">箱码</param>
        /// <returns></returns>
        public DataBoxDto GetBoxQueryList(string boxNo)
        {
            // 实例化返回类
            DataBoxDto boxModel = new DataBoxDto();
            List<DataBoxInfoDto> list = new List<DataBoxInfoDto>();
            // 根据箱码获取箱信息
            var boxData = Db.Queryable<DataBoxInfo>().Where(m => m.IsDel == "0" && m.BoxNo == boxNo);
            if (boxData.Count() == 0)
            {
                // 根据盒码获取箱信息
                boxData = Db.Queryable<DataBoxInfo>().Where(m => m.IsDel == "0" && m.BoxNo2 == boxNo);
                if (boxData.Count() == 0)
                {
                    boxModel = Db.Queryable<DataBoxInfo>().Where(m => m.IsDel == "0" && m.BoxNo3 == boxNo).Select(it => new DataBoxDto() { Qty = it.Qty }, true).First();
                    return boxModel;
                }
                list = boxData.Select(it => new DataBoxInfoDto()
                {
                    BoxNo2 = it.BoxNo3,
                    Qty2 = it.Qty
                }).ToList();
                boxData = Db.Queryable<DataBoxInfo>().Where(m => m.IsDel == "0" && m.BoxNo2 == boxNo);
                boxModel = boxData.GroupBy(m => new
                {
                    m.BoxNo,
                    m.SkuName,
                    m.SkuNo,
                    m.LotNo,
                    m.SupplierLot,
                    m.InspectStatus
                }).Select(it => new DataBoxDto()
                {
                    BoxNo = it.BoxNo,
                    SkuName = it.SkuName,
                    SkuNo = it.SkuNo,
                    LotNo = it.LotNo,
                    SupplierLot = it.SupplierLot,
                    InspectStatus = it.InspectStatus == "0" ? "待检" : it.InspectStatus == "1" ? "合格":"不合格",
                    Qty = SqlFunc.AggregateSum(it.Qty)
                }).First();
                boxModel.InfoList = list;
                return boxModel;
            }
            else  //箱码内数据
            {
                // 处理多级码
                var num = boxData.Count(m => !string.IsNullOrWhiteSpace(m.BoxNo2));
                if (num > 0)
                {
                    list = boxData.GroupBy(m => new
                    {
                        m.BoxNo2
                    }).Select(it => new DataBoxInfoDto()
                    {
                        BoxNo2 = it.BoxNo2,
                        Qty2 = SqlFunc.AggregateSum(it.Qty)
                    }).ToList();
                }
                else
                {
                    list = boxData.Select(it => new DataBoxInfoDto()
                    {
                        BoxNo2 = SqlFunc.IsNull(it.BoxNo3,""),
                        Qty2 = it.Qty
                    }).ToList();
                }
                // 箱码信息赋值
                boxData = Db.Queryable<DataBoxInfo>().Where(m => m.IsDel == "0" && m.BoxNo == boxNo);
                boxModel = boxData.GroupBy(m => new
                {
                    m.BoxNo,
                    m.SkuName,
                    m.SkuNo,
                    m.LotNo,
                    m.SupplierLot,
                    m.InspectStatus
                }).Select(it => new DataBoxDto()
                {
                    BoxNo = it.BoxNo,
                    SkuName = it.SkuName,
                    SkuNo = it.SkuNo,
                    LotNo = it.LotNo,
                    SupplierLot = it.SupplierLot,
                    InspectStatus = it.InspectStatus == "0" ? "待检" : it.InspectStatus == "1" ? "合格" : "不合格",
                    Qty = SqlFunc.AggregateSum(it.Qty)
                }).First();
                boxModel.InfoList = list;
                return boxModel;
            }
        }
        #endregion
    }
}