test
10 小时以前 8193a4f5f70dbae83f5292aa6175476603653cd7
Wms/WMS.BLL/BllPdaServer/PdaCrServer.cs
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection.Metadata;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
@@ -9,6 +11,7 @@
using Model.ModelDto;
using Model.ModelDto.BllCheckDto;
using Model.ModelDto.PdaDto;
using Model.ModelDto.SysDto;
using Newtonsoft.Json;
using SqlSugar;
using Utility;
@@ -417,15 +420,115 @@
        /// <param name="locatNo">储位编号</param>
        /// <param name="palletNo">托盘号</param> 
        /// <returns></returns>
        public async Task<List<DataStockDetail>> GetStockQueryList(string locatNo, string palletNo)
        //public async Task<List<DataStockDetail>> GetStockQueryList(string locatNo, string palletNo)
        //{
        //    return await Db.Queryable<DataStockDetail>()
        //             .Where(s => s.IsDel == "0" && s.LocatNo.Contains(locatNo) || s.PalletNo.Contains(palletNo))
        //             .OrderBy(s => new { s.LotNo, s.LocatNo, s.PalletNo })
        //             .ToListAsync();
        //}
        public async Task<List<StockDetailWithQtyDto>> GetStockQueryList(string locatNo, string palletNo, string boxNo)
        {
            return await Db.Queryable<DataStockDetail>()
                     .Where(s => s.IsDel == "0" && s.LocatNo.Contains(locatNo) && s.PalletNo.Contains(palletNo))
                     .OrderBy(s => new { s.LotNo, s.LocatNo, s.PalletNo })
                     .ToListAsync();
            return await Db.Queryable<DataStockDetail>("a")
                .InnerJoin<DataBoxInfo>((a, b) => a.LotNo == b.LotNo)
                .Where((a, b) => a.IsDel == "0")
                .WhereIF(!string.IsNullOrWhiteSpace(locatNo), (a, b) => a.LocatNo.Contains(locatNo))
                .WhereIF(!string.IsNullOrWhiteSpace(palletNo), (a, b) => a.PalletNo.Contains(palletNo))
                .WhereIF(!string.IsNullOrWhiteSpace(boxNo), (a, b) => b.BoxNo.Contains(boxNo))
                .GroupBy((a, b) => new
                {
                    a.LotNo,
                    a.LotText,
                    a.SupplierLot,
                    a.OwnerNo,
                    a.OwnerName,
                    a.SupplierNo,
                    a.SupplierName,
                    a.SkuNo,
                    a.SkuName,
                    a.Standard,
                    a.LockQty,
                    a.FrozenQty,
                    a.InspectQty,
                    a.ASNNo,
                    a.ASNDetailNo,
                    a.SONo,
                    a.WareHouseNo,
                    a.RoadwayNo,
                    a.AreaNo,
                    a.LocatNo,
                    a.PalletNo,
                    a.PalletNo2,
                    a.PalletNo3,
                    a.PalletTags,
                    a.CompleteTime,
                    a.ProductionTime,
                    a.ExpirationTime,
                    a.Status,
                    a.InspectMark,
                    a.BitPalletMark,
                    a.InspectStatus,
                    a.InspectTime,
                    a.PackagNo,
                    a.IsBale,
                    a.IsBelt,
                    a.UDF5,
                    a.IsDel,
                    a.CreateTime,
                    a.CreateUser,
                    a.UpdateTime,
                    a.UpdateUser,
                    a.Demo
                })
                .OrderBy((a, b) => new { a.LotNo, a.LocatNo, a.PalletNo })
                .Select((a, b) => new StockDetailWithQtyDto
                {
                    LotNo = a.LotNo,
                    LotText = a.LotText,
                    SupplierLot = a.SupplierLot,
                    OwnerNo = a.OwnerNo,
                    OwnerName = a.OwnerName,
                    SupplierNo = a.SupplierNo,
                    SupplierName = a.SupplierName,
                    SkuNo = a.SkuNo,
                    SkuName = a.SkuName,
                    Standard = a.Standard,
                    Qty = SqlFunc.AggregateSum(b.Qty),  // DataBoxInfo的Qty字段
                    LockQty = a.LockQty,
                    FrozenQty = a.FrozenQty,
                    InspectQty = a.InspectQty,
                    ASNNo = a.ASNNo,
                    ASNDetailNo = a.ASNDetailNo,
                    SONo = a.SONo,
                    WareHouseNo = a.WareHouseNo,
                    RoadwayNo = a.RoadwayNo,
                    AreaNo = a.AreaNo,
                    LocatNo = a.LocatNo,
                    PalletNo = a.PalletNo,
                    PalletNo2 = a.PalletNo2,
                    PalletNo3 = a.PalletNo3,
                    PalletTags = a.PalletTags,
                    CompleteTime = a.CompleteTime,
                    ProductionTime = a.ProductionTime,
                    ExpirationTime = a.ExpirationTime,
                    Status = a.Status,
                    InspectMark = a.InspectMark,
                    BitPalletMark = a.BitPalletMark,
                    InspectStatus = a.InspectStatus,
                    InspectTime = a.InspectTime,
                    PackagNo = a.PackagNo,
                    IsBale = a.IsBale,
                    IsBelt = a.IsBelt,
                    UDF5 = a.UDF5,
                    IsDel = a.IsDel,
                    CreateTime = a.CreateTime,
                    CreateUser = a.CreateUser,
                    UpdateTime = a.UpdateTime,
                    UpdateUser = a.UpdateUser,
                    Demo = a.Demo
                })
                .ToListAsync();
        }
        #endregion
        #region 托盘变更(托盘解绑绑定)
@@ -691,9 +794,9 @@
            {
                throw Oops.Bah("托盘储位信息不存在,请检查!");
            }
            if (models.WareHouseNo != "W04")
            if (models.WareHouseNo != "W04" && models.WareHouseNo != "W02")
            {
                throw Oops.Bah("该托盘未在平库内,请检查!");
                throw Oops.Bah("该托盘未在平库内或货架库内,请检查!");
            }
            return models.LocatNo;
        }
@@ -721,7 +824,7 @@
                {
                    throw Oops.Bah("托盘储位信息不存在,请检查!");
                }
                if (models.WareHouseNo != "W04")
                if (models.WareHouseNo != "W04" && models.WareHouseNo != "W02")
                {
                    throw Oops.Bah("该托盘未在平库内,请检查!");
                }
@@ -731,8 +834,14 @@
                    throw Oops.Bah("储位信息不存在,请检查!");
                }
                storageArea = await Db.Queryable<SysStorageArea>().Where(w => w.IsDel == "0" && w.Status == "0" && w.AreaNo != storageLocat.AreaNo && w.WareHouseNo == "W04").OrderBy(o => o.AreaNo).ToListAsync();
                if (models.WareHouseNo == "W02")
                {
                    storageArea = await Db.Queryable<SysStorageArea>().Where(w => w.IsDel == "0" && w.Status == "0" && w.AreaNo != storageLocat.AreaNo && w.WareHouseNo == "W04" && (w.AreaNo == "B01" || w.AreaNo == "B02")).OrderBy(o => o.AreaNo).ToListAsync();
                }
                else
                {
                    storageArea = await Db.Queryable<SysStorageArea>().Where(w => w.IsDel == "0" && w.Status == "0" && w.AreaNo != storageLocat.AreaNo && w.WareHouseNo == "W04").OrderBy(o => o.AreaNo).ToListAsync();
                }
                
            }
            return storageArea;
@@ -779,7 +888,7 @@
        /// <param name="palletNo"></param>
        /// <param name="areaNo"></param>
        /// <param name="ruku"></param>
        public async Task AgvTransport(string palletNo, string areaNo, string ruku, string url, int userId)
        /*public async Task AgvTransport(string palletNo, string areaNo, string ruku, string url, int userId)
        {
            try
            {
@@ -829,7 +938,7 @@
                {
                    throw Oops.Bah("托盘储位信息不存在,请检查!");
                }
                if (stockDetail.WareHouseNo != "W04")
                if (stockDetail.WareHouseNo != "W04" && stockDetail.WareHouseNo != "W02")
                {
                    throw Oops.Bah("该托盘未在平库内,请检查!");
                }
@@ -927,6 +1036,339 @@
                    if (wcsModel.code == 200)
                    {
                       // 更改任务的发送返回时间//
                        new TaskServer().EditTaskIssueOk(list2, time1, time2);
                    }
                    else
                    {
                        new TaskServer().EditTaskIssueNo(list2, time1, time2, wcsModel.message);
                        throw new Exception(wcsModel.message);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }*/
        public async Task AgvTransport(string palletNo, string areaNo, string ruku, string url, int userId)
        {
            try
            {
                if (string.IsNullOrEmpty(palletNo))
                {
                    throw Oops.Bah("请扫描托盘条码!");
                }
                if (string.IsNullOrEmpty(areaNo) && string.IsNullOrEmpty(ruku))
                {
                    throw Oops.Bah("请选择目标区域或入库口!");
                }
                if (!string.IsNullOrEmpty(areaNo) && !string.IsNullOrEmpty(ruku))
                {
                    throw Oops.Bah("目标区域和入库口不能同时选择!");
                }
                string EndLocat = string.Empty;//目标位置
                var log = await Db.Queryable<LogTask>().FirstAsync(w => w.IsDel == "0" && w.PalletNo == palletNo && (w.Status == "0" || w.Status == "1"));
                if (log != null)
                {
                    throw Oops.Bah("该托盘已有等待执行或正在执行的任务!");
                }
                var stockDetail = await Db.Queryable<DataStockDetail>().FirstAsync(w => w.IsDel == "0" && w.PalletNo == palletNo);
                if (stockDetail == null)
                {
                    throw Oops.Bah("托盘上物料库存明细信息不存在,请检查!");
                }
                if (!string.IsNullOrEmpty(areaNo))
                {
                    var storageArea = await Db.Queryable<SysStorageArea>().FirstAsync(w => w.IsDel == "0" && w.AreaNo == areaNo);
                    if (storageArea == null)
                    {
                        throw Oops.Bah("所选区域信息不存在,请检查!");
                    }
                    EndLocat = await GetLocat(areaNo);
                }
                else
                {
                    EndLocat = ruku;
                }
                var stock = await Db.Queryable<DataStock>().FirstAsync(w => w.IsDel == "0" && w.SkuNo == stockDetail.SkuNo && w.LotNo == stockDetail.LotNo);
                if (stock == null)
                {
                    throw Oops.Bah("托盘上物料库存信息不存在,请检查!");
                }
                if (string.IsNullOrEmpty(stockDetail.LocatNo))
                {
                    throw Oops.Bah("托盘储位信息不存在,请检查!");
                }
                if (stockDetail.WareHouseNo != "W04")
                {
                    throw Oops.Bah("该托盘未在平库内,请检查!");
                }
                //起始储位信息
                var storageLocat = await Db.Queryable<SysStorageLocat>().FirstAsync(w => w.IsDel == "0" && w.LocatNo == stockDetail.LocatNo);
                if (storageLocat == null)
                {
                    throw Oops.Bah("储位信息不存在,请检查!");
                }
                //目标储位信息
                //var storageLocatEnd = new SysStorageLocat();
                var storageLocatEnd = await Db.Queryable<SysStorageLocat>().FirstAsync(w => w.IsDel == "0" && w.LocatNo == EndLocat && w.Flag == "0");
                if (storageLocatEnd == null)
                {
                    throw Oops.Bah("目标储位信息不存在,请检查!");
                }
                //添加出库任务
                var taskNo = new Common().GetMaxNo("TK");
                var moveTask = new LogTask
                {
                    TaskNo = taskNo,
                    Sender = "WMS",
                    Receiver = "WCS",
                    IsSuccess = 0, //是否下发成功 0失败 1成功
                    StartLocat = stockDetail.LocatNo,//起始位置
                    EndLocat = EndLocat,//目标位置
                    PalletNo = palletNo,//托盘码
                    Msg = string.Format("转运任务:{0}=>>{1}", stockDetail.LocatNo, EndLocat),
                    IsSend = 1,//是否可再次下发
                    IsCancel = 1,//是否可取消
                    IsFinish = 1,//是否可完成
                    Type = "2",//任务类型 0 入库任务 1 出库任务  2 移库任务
                    Status = "0",//任务状态0:等待执行1正在执行2执行完成
                    OrderType = "3",//0 入库单 1 出库单  2 盘点单  3 移库单
                    CreateTime = DateTime.Now
                };
                await Db.Insertable(moveTask).ExecuteCommandAsync();
                //修改库存明细信息
                //stockDetail.Status = "4";//移库锁定
                //stockDetail.LockQty = stockDetail.Qty;//锁定库存数量
                //await Db.Updateable(stockDetail).ExecuteCommandAsync();
                ////修改库存信息
                //stock.LockQty += (decimal)stockDetail.Qty;
                //await Db.Updateable(stock).ExecuteCommandAsync();
                //修改起始储位地址状态
                storageLocat.Status = "5";//0:空储位 1:有物品 2:入库中 3:出库中 4:移入中 5:移出中
                await Db.Updateable(storageLocat).ExecuteCommandAsync();
                //修改目标储位地址状态
                if (storageLocatEnd != null)
                {
                    storageLocatEnd.Status = "4";//0:空储位 1:有物品 2:入库中 3:出库中 4:移入中 5:移出中
                    await Db.Updateable(storageLocatEnd).ExecuteCommandAsync();
                }
                //添加操作日志记录
                var k = new OperationCrServer().AddLogOperationCr("PDA模块", "AGV转运", palletNo, "移库", $"PDA呼叫小车对托盘号:{palletNo}发起转运", userId);
                var outDto = new List<OutCommandDto>
                {
                    new OutCommandDto()
                    {
                        TaskNo = moveTask.TaskNo, // 任务号
                        TaskType = "2",// 任务类型 0入 1出 2移
                        PalletNo = moveTask.PalletNo,//托盘号
                        StartLocate = moveTask.StartLocat, // 起始位置
                        EndLocate = EndLocat, // 目标位置
                        Order = 999,
                        Type = PLCTypeEnum.AGV
                    }
                };
                // 正式运行程序放开
                var list2 = outDto.Select(m => m.TaskNo).ToList();
                var jsonData = JsonConvert.SerializeObject(outDto);
                string response = "";
                try
                {
                    var time1 = DateTime.Now;//发送时间 .ToString("yyyy-MM-dd HH:mm:ss")
                    response = HttpHelper.DoPost(url, jsonData, "下发给WCS出库命令", "WCS");
                    var time2 = DateTime.Now;//返回时间 .ToString("yyyy-MM-dd HH:mm:ss")
                    //解析返回数据
                    var wcsModel = JsonConvert.DeserializeObject<WcsModel2>(response);
                    if (wcsModel.code == 200)
                    {
                        // 更改任务的发送返回时间//
                        new TaskServer().EditTaskIssueOk(list2, time1, time2);
                    }
                    else
                    {
                        new TaskServer().EditTaskIssueNo(list2, time1, time2, wcsModel.message);
                        throw new Exception(wcsModel.message);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        /// <summary>
        /// 呼叫agv转运空托盘
        /// </summary>
        /// <param name="palletNo"></param>
        /// <param name="areaNo"></param>
        /// <param name="locatNo"></param>
        /// <param name="url"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task AgvNullPallet(string palletNo, string areaNo, string locatNo, string url, int userId)
        {
            try
            {
                if (string.IsNullOrEmpty(palletNo))
                {
                    throw Oops.Bah("请扫描托盘条码!");
                }
                if (string.IsNullOrEmpty(locatNo))
                {
                    throw Oops.Bah("请扫描起始地码!");
                }
                if (string.IsNullOrEmpty(areaNo))
                {
                    throw Oops.Bah("请选择目标区域!");
                }
                string EndLocat = string.Empty;//目标位置
                var log = await Db.Queryable<LogTask>().FirstAsync(w => w.IsDel == "0" && w.PalletNo == palletNo && (w.Status == "0" || w.Status == "1"));
                if (log != null)
                {
                    throw Oops.Bah("该托盘已有等待执行或正在执行的任务!");
                }
                var stockDetail = await Db.Queryable<DataStockDetail>().FirstAsync(w => w.IsDel == "0" && w.PalletNo == palletNo);
                if (stockDetail != null)
                {
                    throw Oops.Bah("托盘上物料存在库存明细信息,请检查!");
                }
                var storageArea = await Db.Queryable<SysStorageArea>().FirstAsync(w => w.IsDel == "0" && w.AreaNo == areaNo);
                if (storageArea == null)
                {
                    throw Oops.Bah("所选区域信息不存在,请检查!");
                }
                var palletInfo = await Db.Queryable<SysPallets>().FirstAsync(w => w.IsDel == "0" && w.PalletNo == palletNo);
                if (palletInfo == null)
                {
                    throw Oops.Bah("该托盘信息不存在!");
                }
                if (palletInfo.Status!="0")
                {
                    throw Oops.Bah("该托盘不是空托盘!");
                }
                //根据目标区域获取目标储位地址
                EndLocat = await GetLocat(areaNo);
                if (string.IsNullOrEmpty(EndLocat))
                {
                    throw Oops.Bah("获取目标储位失败!");
                }
                //起始储位信息
                var storageLocat = await Db.Queryable<SysStorageLocat>().FirstAsync(w => w.IsDel == "0" && w.LocatNo == locatNo);
                if (storageLocat == null)
                {
                    throw Oops.Bah("储位信息不存在,请检查!");
                }
                //目标储位信息
                //var storageLocatEnd = new SysStorageLocat();
                var storageLocatEnd = await Db.Queryable<SysStorageLocat>().FirstAsync(w => w.IsDel == "0" && w.LocatNo == EndLocat && w.Flag == "0");
                if (storageLocatEnd == null)
                {
                    throw Oops.Bah("目标储位信息不存在,请检查!");
                }
                //添加出库任务
                var taskNo = new Common().GetMaxNo("TK");
                var moveTask = new LogTask
                {
                    TaskNo = taskNo,
                    Sender = "WMS",
                    Receiver = "WCS",
                    IsSuccess = 0, //是否下发成功 0失败 1成功
                    StartLocat = storageLocat.LocatNo,//起始位置
                    EndLocat = EndLocat,//目标位置
                    PalletNo = palletNo,//托盘码
                    Msg = string.Format("转运任务:{0}=>>{1}", storageLocat.LocatNo, EndLocat),
                    IsSend = 1,//是否可再次下发
                    IsCancel = 1,//是否可取消
                    IsFinish = 1,//是否可完成
                    Type = "2",//任务类型 0 入库任务 1 出库任务  2 移库任务
                    Status = "0",//任务状态0:等待执行1正在执行2执行完成
                    OrderType = "3",//0 入库单 1 出库单  2 盘点单  3 移库单
                    CreateTime = DateTime.Now
                };
                await Db.Insertable(moveTask).ExecuteCommandAsync();
                //修改起始储位地址状态
                storageLocat.Status = "5";//0:空储位 1:有物品 2:入库中 3:出库中 4:移入中 5:移出中
                await Db.Updateable(storageLocat).ExecuteCommandAsync();
                //修改目标储位地址状态
                //if (storageLocatEnd != null)
                //{
                //    storageLocatEnd.Status = "4";//0:空储位 1:有物品 2:入库中 3:出库中 4:移入中 5:移出中
                //    await Db.Updateable(storageLocatEnd).ExecuteCommandAsync();
                //}
                //添加操作日志记录
                var k = new OperationCrServer().AddLogOperationCr("PDA模块", "AGV空托转运", palletNo, "移库", $"PDA呼叫小车对托盘号:{palletNo}发起转运", userId);
                var outDto = new List<OutCommandDto>
                {
                    new OutCommandDto()
                    {
                        TaskNo = moveTask.TaskNo, // 任务号
                        TaskType = "2",// 任务类型 0入 1出 2移
                        PalletNo = moveTask.PalletNo,//托盘号
                        StartLocate = moveTask.StartLocat, // 起始位置
                        EndLocate = EndLocat, // 目标位置
                        Order = 999,
                        Type = PLCTypeEnum.AGV
                    }
                };
                // 正式运行程序放开
                var list2 = outDto.Select(m => m.TaskNo).ToList();
                var jsonData = JsonConvert.SerializeObject(outDto);
                string response = "";
                try
                {
                    var time1 = DateTime.Now;//发送时间 .ToString("yyyy-MM-dd HH:mm:ss")
                    response = HttpHelper.DoPost(url, jsonData, "下发给WCS出库命令", "WCS");
                    var time2 = DateTime.Now;//返回时间 .ToString("yyyy-MM-dd HH:mm:ss")
                    //解析返回数据
                    var wcsModel = JsonConvert.DeserializeObject<WcsModel2>(response);
                    if (wcsModel.code == 200)
                    {
                        // 更改任务的发送返回时间//
                        new TaskServer().EditTaskIssueOk(list2, time1, time2);
                    }
                    else
@@ -1105,9 +1547,18 @@
            string endLocat = string.Empty;//目标储位
            //当前区域所有储位信息
            var storageLocatList = await Db.Queryable<SysStorageLocat>().Where(w => w.WareHouseNo == "W04" && w.AreaNo == areaNo).ToListAsync();
            var locatInfo2 = storageLocatList.Where(w => w.IsDel == "0" && w.Flag == "0" && w.Status == "0")
            var locatInfo2 = new SysStorageLocat();
            if (areaNo == "B10")
            {
                locatInfo2 = storageLocatList.Where(w => w.IsDel == "0" && w.Flag == "0")
                           .OrderByDescending(m => m.Layer).OrderByDescending(m => m.Column).OrderByDescending(m => m.Row).First();
            }
            else
            {
                locatInfo2 = storageLocatList.Where(w => w.IsDel == "0" && w.Flag == "0" && w.Status == "0")
                               .OrderByDescending(m => m.Layer).OrderByDescending(m => m.Column).OrderByDescending(m => m.Row).First();
            }
            if (locatInfo2 != null)
            {
@@ -1227,9 +1678,9 @@
                }
                //开启事务
                Db.BeginTran();
                //库存箱支明细信息
                var boxList = Db.Queryable<DataBoxInfo>().Where(w => w.IsDel == "0" && w.BoxNo == boxNo).ToList();
                var boxList = Db.Queryable<DataBoxInfo>().Where(w => w.IsDel == "0" && w.BoxNo == boxNo && w.Status == "2").ToList();
                if (boxList.Count != 1)
                {
                    throw new Exception("该箱码信息错误,存在多个此箱码信息");