DESKTOP-9BNTV8O
2024-12-16 66b1b95f207cbf623e0ef216c963d2e96a6923f6
Wms/WMS.BLL/BllTransServer/RcsServer.cs
@@ -1,5 +1,7 @@
using Model.InterFaceModel;
using Model.ModelDto;
using Model.ModelDto.DataDto;
using Model.ModelDto.LogDto;
using Newtonsoft.Json;
using SqlSugar;
using System;
@@ -7,7 +9,9 @@
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using Utility.Tools;
using WMS.BLL.LogServer;
using WMS.DAL;
using WMS.Entity.BllAsnEntity;
using WMS.Entity.BllSoEntity;
@@ -17,6 +21,7 @@
using WMS.Entity.SysEntity;
using WMS.IBLL.IBllTransServer;
using static Model.InterFaceModel.RCSModel;
using static System.Collections.Specialized.BitVector32;
namespace WMS.BLL.BllTransServer
{
@@ -56,12 +61,19 @@
                switch (model.Type)
                {
                    case "0"://叫净桶
                        pallet = stockDetail.Where(w => w.WareHouseNo == houseNo && w.PalletStatus == "0"
                        && (w.AreaNo.Contains("01") || w.AreaNo.Contains("11") || w.AreaNo.Contains("21") || w.AreaNo.Contains("31"))).OrderByDescending(o => o.CreateTime).First();
                        //查找到该车间净桶区
                        List<string> areaNoList = Db.Queryable<SysStorageArea>().Where(w => w.IsDel == "0" && w.Status == "0" && w.Type == "0" && w.WareHouseNo == houseNo).Select(s=>s.AreaNo).ToList();
                        //查找净桶
                        pallet = stockDetail.Where(w => w.WareHouseNo == houseNo && w.PalletStatus == "0" && w.Status == "0"
                        && areaNoList.Contains(w.AreaNo)).OrderByDescending(o => o.CreateTime).First();
                        if (pallet == null)
                        {
                            throw new Exception("暂无净桶可分配");
                        }
                        }
                        pallet.Status = "2";//分配状态 0:待分配,1:部分分配 , 2:已分配
                        //更新桶库存明细
                        Db.Updateable(pallet).ExecuteCommand();
                        taskMsg = "设备叫净桶任务";
                        break;
                    case "1"://叫料桶(混料)
@@ -70,15 +82,17 @@
                        {
                            throw new Exception("批次号为空!");
                        }
                        //查找到该车间预混区
                        List<string> areaNoList2 = Db.Queryable<SysStorageArea>().Where(w => w.IsDel == "0" && w.Status == "0" && w.Type == "1" && w.WareHouseNo == houseNo).Select(s => s.AreaNo).ToList();
                        //查找库存中是否有可用的此批次的混料桶
                        pallet = stockDetail.Where(w => w.WareHouseNo == houseNo && w.PalletStatus == "1" && w.LotNo == model.LotNo
                        && (w.AreaNo.Contains("02") || w.AreaNo.Contains("12") || w.AreaNo.Contains("22") || w.AreaNo.Contains("32"))).OrderByDescending(o => o.CompleteTime).First();
                        pallet = stockDetail.Where(w => w.WareHouseNo == houseNo && w.PalletStatus == "1" && w.Status == "0" && w.LotNo == model.LotNo
                        && areaNoList2.Contains(w.AreaNo)).OrderByDescending(o => o.CompleteTime).First();
                        if (pallet == null)
                        {
                            throw new Exception("暂无混料桶可分配");
                        }
                        pallet.LotNo = model.LotNo;
                        //更新桶库存明细维护批次号
                        pallet.Status = "2";//分配状态 0:待分配,1:部分分配 , 2:已分配
                        //更新桶库存明细
                        Db.Updateable(pallet).ExecuteCommand();
                        taskMsg = "设备叫料桶混料任务";
@@ -107,19 +121,26 @@
                        //更新出库单
                        Db.Updateable(soNotice).ExecuteCommand();
                        //查找到该车间半成品区
                        List<string> areaNoList3 = Db.Queryable<SysStorageArea>().Where(w => w.IsDel == "0" && w.Status == "0" && w.Type == "2" && w.WareHouseNo == houseNo).Select(s => s.AreaNo).ToList();
                        //查找库存中是否有此批次的合格下料桶
                        pallet = stockDetail.Where(w => w.WareHouseNo == houseNo && w.PalletStatus == "2" && w.LotNo == model.LotNo && w.InspectStatus=="1"
                        && (w.AreaNo.Contains("03") || w.AreaNo.Contains("13") || w.AreaNo.Contains("23") || w.AreaNo.Contains("33"))).OrderByDescending(o => o.CompleteTime).First();
                        pallet = stockDetail.Where(w => w.WareHouseNo == houseNo && w.PalletStatus == "2" && w.Status == "0" && w.LotNo == model.LotNo && w.InspectStatus=="1"
                        && areaNoList3.Contains(w.AreaNo)).OrderByDescending(o => o.CompleteTime).First();
                        if (pallet == null)
                        {
                            throw new Exception("暂无下料桶可分配");
                        }
                        pallet.Status = "2";//分配状态 0:待分配,1:部分分配 , 2:已分配
                        //更新桶库存明细
                        Db.Updateable(pallet).ExecuteCommand();
                        taskMsg = "设备叫料桶下料任务";
                        break;
                    case "3"://叫脏桶
                        List<DataStockDetail> palletList = stockDetail.Where(w => w.WareHouseNo == houseNo && w.PalletStatus == "3"
                        && (w.AreaNo.Contains("03") || w.AreaNo.Contains("13") || w.AreaNo.Contains("23") || w.AreaNo.Contains("33"))).OrderByDescending(o => o.CompleteTime).ToList();
                        //查找到该车间脏桶区
                        List<string> areaNoList4 = Db.Queryable<SysStorageArea>().Where(w => w.IsDel == "0" && w.Status == "0" && w.Type == "3" && w.WareHouseNo == houseNo).Select(s => s.AreaNo).ToList();
                        List<DataStockDetail> palletList = stockDetail.Where(w => w.WareHouseNo == houseNo && w.PalletStatus == "3" && w.Status == "0"
                        && areaNoList4.Contains(w.AreaNo)).OrderByDescending(o => o.CompleteTime).ToList();
                        if (palletList.Count <= 0)
                        {
                            throw new Exception("暂无脏桶可分配");
@@ -160,6 +181,9 @@
                            //}
                            #endregion
                            item.Status = "2";//分配状态 0:待分配,1:部分分配 , 2:已分配
                            //更新桶库存明细
                            Db.Updateable(item).ExecuteCommand();
                            //起始储位地址信息
                            var startLoction2 = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == item.LocatNo);
                            if (startLoction2 == null)
@@ -183,7 +207,7 @@
                                IsCancel = 1,//是否可取消
                                IsFinish = 1,//是否可完成
                                Type = "1",//任务类型 0 入库任务 1 出库任务  2 移库任务
                                OrderType = "3",//单据类型 0 入库 1 出库 3其他
                                OrderType = "3",//单据类型 0 入库 1 出库 3 移库
                                Status = "0",//任务状态0:等待执行1正在执行2执行完成
                                NoticeDetailNo = int.Parse(noticeNo),
                                Msg = taskMsg, //关键信息
@@ -195,18 +219,17 @@
                                Startport = item.LocatNo,
                                Endport = model.Location,
                                Pallno = item.PalletNo,
                                Type = model.Type,
                                Crtype = "1",
                                Noticedetailno = int.Parse(noticeNo),
                                Crtype = "1",//叫桶
                            };
                            CreateLotTask(task2, url);
                            //给下车下发任务
                            logTaskEntry2.SendDate = DateTime.Now;//发送时间
                            var agvResult2 = CreateLotTask(task2, url);
                            var agvResult2 = CreateTaskForAgv(task2, url);
                            if (agvResult2)//成功
                            {
                                //请求成功修改任务表相应字段状态
                                logTaskEntry2.IsSuccess = 1;
                                logTaskEntry2.IsSend = 0;
                                //logTaskEntry2.IsCancel = 0;
                                logTaskEntry2.BackDate = DateTime.Now;
                                logTaskEntry2.Status = "1";
                                Db.Insertable(logTaskEntry2).ExecuteCommand();
@@ -257,7 +280,7 @@
                    IsCancel = 1,//是否可取消
                    IsFinish = 1,//是否可完成
                    Type = "1",//任务类型 0 入库任务 1 出库任务  2 移库任务
                    OrderType = "3",//单据类型 0 入库 1 出库 3其他
                    OrderType = "3",//单据类型 0 入库 1 出库 3移库
                    Status = "0",//任务状态0:等待执行1正在执行2执行完成
                    NoticeDetailNo = int.Parse(noticeNo),
                    Msg = taskMsg, //关键信息
@@ -271,18 +294,18 @@
                    Endport = model.Location,//目标位置
                    Pallno = pallet.PalletNo,//桶号
                    Crtype = "1",//叫桶
                    Noticedetailno = int.Parse(noticeNo),
                    LotNo = model.LotNo,
                };
                //给下车下发任务
                logTaskEntry.SendDate = DateTime.Now;//发送时间
                var agvResult = CreateLotTask(task, url);
                var agvResult = CreateTaskForAgv(task, url);
                if (agvResult)//成功
                {
                    //请求成功修改任务表相应字段状态
                    logTaskEntry.IsSuccess = 1;
                    logTaskEntry.IsSend = 0;
                    //logTaskEntry.IsCancel = 0;
                    logTaskEntry.BackDate = DateTime.Now;
                    logTaskEntry.Status = "1";
                    logTaskEntry.Status = "1";//正在执行
                    Db.Insertable(logTaskEntry).ExecuteCommand();
                    startLoction.Status = "3";//出库中
@@ -320,6 +343,12 @@
            try
            {
                string taskMsg = "";
                //查找该桶有没有正在执行的任务
                var tasking = Db.Queryable<LogTask>().First(w => w.IsDel == "0" && w.Status == "1" && w.PalletNo == model.PalletNo);
                if (tasking != null)
                {
                    throw new Exception($"该桶有正在执行的任务,PalletNo:{model.PalletNo}");
                }
                //设备所在储位信息(起始储位)
                var deviceLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == model.Location);
                if (deviceLocatInfo == null)
@@ -328,6 +357,12 @@
                }
                var houseNo = deviceLocatInfo.WareHouseNo;//申请储位设备所在车间编号
                var noticeno = "0";//出入库单据明细ID
                //桶库存信息
                var palletInfo = Db.Queryable<DataStockDetail>().First(w => w.IsDel == "0" && w.PalletNo == model.PalletNo);
                if (palletInfo == null)
                {
                    throw new Exception($"未查询到该桶的库存信息,PalletNo:{model.PalletNo}");
                }
                var storageLocat = Db.Queryable<SysStorageLocat>().Where(w => w.IsDel == "0" && w.Status == "0");
                //开启事务
@@ -337,7 +372,9 @@
                switch (model.Type)
                {
                    case "3"://清洗设备申请储位(净桶申请储位)
                        loction = storageLocat.Where(w => w.WareHouseNo == houseNo && (w.AreaNo.Contains("01") || w.AreaNo.Contains("11") || w.AreaNo.Contains("21") || w.AreaNo.Contains("31"))).OrderByDescending(o => o.CreateTime).First();
                        //查找到该车间净桶区
                        List<string> areaNoList = Db.Queryable<SysStorageArea>().Where(w => w.IsDel == "0" && w.Status == "0" && w.Type == "0" && w.WareHouseNo == houseNo).Select(s => s.AreaNo).ToList();
                        loction = storageLocat.Where(w => w.WareHouseNo == houseNo && areaNoList.Contains(w.AreaNo)).OrderByDescending(o => o.CreateTime).First();
                        if (loction == null)
                        {
                            throw new Exception("该车间净桶区暂无空余储位");
@@ -351,11 +388,16 @@
                        {
                            throw new Exception("批次号为空!");
                        }
                        loction = storageLocat.Where(w => w.WareHouseNo == houseNo && (w.AreaNo.Contains("02") || w.AreaNo.Contains("12") || w.AreaNo.Contains("22") || w.AreaNo.Contains("32"))).OrderByDescending(o => o.CreateTime).First();
                        //查找到该车间预混区
                        List<string> areaNoList2 = Db.Queryable<SysStorageArea>().Where(w => w.IsDel == "0" && w.Status == "0" && w.Type == "1" && w.WareHouseNo == houseNo).Select(s => s.AreaNo).ToList();
                        loction = storageLocat.Where(w => w.WareHouseNo == houseNo && areaNoList2.Contains(w.AreaNo)).OrderByDescending(o => o.CreateTime).First();
                        if (loction == null)
                        {
                            throw new Exception("该车间预混区暂无空余储位");
                        }
                        palletInfo.LotNo = model.LotNo;//批次号
                        //更新桶库存信息
                        Db.Updateable(palletInfo).ExecuteCommand();
                        taskMsg = "接料设备(预混料桶)申请储位任务";
                        break;
@@ -382,7 +424,9 @@
                        //更新入库单状态
                        Db.Updateable(notice).ExecuteCommand();
                        loction = storageLocat.Where(w => w.WareHouseNo == houseNo && (w.AreaNo.Contains("03") || w.AreaNo.Contains("13") || w.AreaNo.Contains("23") || w.AreaNo.Contains("33"))).OrderByDescending(o => o.CreateTime).First();
                        //查找到该车间半成品区
                        List<string> areaNoList3 = Db.Queryable<SysStorageArea>().Where(w => w.IsDel == "0" && w.Status == "0" && w.Type == "2" && w.WareHouseNo == houseNo).Select(s => s.AreaNo).ToList();
                        loction = storageLocat.Where(w => w.WareHouseNo == houseNo && areaNoList3.Contains(w.AreaNo)).OrderByDescending(o => o.CreateTime).First();
                        if (loction == null)
                        {
                            throw new Exception("该车间半成品区暂无空余储位");
@@ -391,7 +435,9 @@
                        taskMsg = "接料设备(混料桶)申请储位任务";
                        break;
                    case "2"://下料设备申请储位(脏桶申请储位)
                        loction = storageLocat.Where(w => w.WareHouseNo == houseNo && (w.AreaNo.Contains("04") || w.AreaNo.Contains("14") || w.AreaNo.Contains("24") || w.AreaNo.Contains("34"))).OrderByDescending(o => o.CreateTime).First();
                        //查找到该车间脏桶区
                        List<string> areaNoList4 = Db.Queryable<SysStorageArea>().Where(w => w.IsDel == "0" && w.Status == "0" && w.Type == "3" && w.WareHouseNo == houseNo).Select(s => s.AreaNo).ToList();
                        loction = storageLocat.Where(w => w.WareHouseNo == houseNo && areaNoList4.Contains(w.AreaNo)).OrderByDescending(o => o.CreateTime).First();
                        if (loction == null)
                        {
                            throw new Exception("该车间脏桶区暂无空余储位");
@@ -412,36 +458,36 @@
                    SendDate = DateTime.Now,  //发送时间
                    //BackDate = DateTime.Now,  //返回时间
                    StartLocat = model.Location,//起始位置
                    EndLocat = model.Location,//目标位置
                    EndLocat = loction.LocatNo,//目标位置
                    PalletNo = model.PalletNo,//桶号
                    IsSend = 1,//是否可再次下发
                    IsCancel = 1,//是否可取消
                    IsFinish = 1,//是否可完成
                    Type = "0",//任务类型 0 入库任务 1 出库任务  2 移库任务
                    OrderType = "3",//单据类型 0 入库 1 出库 3其他
                    OrderType = "3",//单据类型 0 入库 1 出库 3 移库
                    Status = "0",//任务状态0:等待执行1正在执行2执行完成
                    NoticeDetailNo = int.Parse(noticeno),
                    Msg = taskMsg, //关键信息
                };
                var task = new TaskDetial
                {
                    Taskno = taskNo,//任务号
                    Startport = model.Location,
                    Endport = loction.LocatNo,
                    Pallno = model.PalletNo,
                    Type = model.Type,
                    Crtype = "0",//入库
                    Noticedetailno = int.Parse(noticeno),
                    LotNo = model.LotNo,
                };
                //给下车下发任务
                logTaskEntry.SendDate = DateTime.Now;//发送时间
                var agvResult = CreateLotTask(task, url);
                var agvResult = CreateTaskForAgv(task, url);
                if (agvResult)//成功
                {
                    //请求成功修改任务表相应字段状态
                    logTaskEntry.IsSuccess = 1;
                    logTaskEntry.IsSend = 0;
                    //logTaskEntry.IsCancel = 0;
                    logTaskEntry.BackDate = DateTime.Now;
                    logTaskEntry.Status = "1";
                    logTaskEntry.Status = "1";//正在执行
                    Db.Insertable(logTaskEntry).ExecuteCommand();
                    deviceLocatInfo.Status = "3";//出库中
@@ -507,12 +553,10 @@
                        Startport = detail.LocatNo,
                        Endport = endLocat.LocatNo,
                        Pallno = PalletNo,
                        Type = "3",
                        Crtype = "2",
                        Noticedetailno = 0,
                    };
                    string taskNo = "";
                    CreateLotTask(ztask, url);
                    CreateTaskForAgv(ztask, url);
                    statrtLocat.Status = "3";//出库中
                    //修改起始储位状态
@@ -551,13 +595,10 @@
                    Startport = detail.LocatNo,
                    Endport = pallnetmsg.Location,
                    Pallno = detail.PalletNo,
                    Type = "3",
                    Crtype = "1",
                    Noticedetailno = 0,
                    LotNo = "",
                };
                string taskNo = "";
                CreateLotTask(task,url);
                CreateTaskForAgv(task,url);
            }
            catch (Exception ex)
            {
@@ -566,127 +607,37 @@
        }
        /// <summary>
        /// 生成任务单到LogTask
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public bool CreateLotTask(TaskDetial taskDetial,string url)
        {
            bool result = false;
            #region 呼叫小车代码
            List<AgvSchedulingTask> agvTaskList = new List<AgvSchedulingTask>();
            List<PositionCodePath> pahtList = new List<PositionCodePath>();
            //起始位置
            PositionCodePath path1 = new PositionCodePath();
            path1.positionCode = taskDetial.Startport;
            if (taskDetial.Crtype == "1")//叫桶(桶出库)
            {
                path1.type = "05";
            }
            else//申请储位(桶入库)
            {
                path1.type = "05";
            }
            pahtList.Add(path1);
            //目标位置
            PositionCodePath path2 = new PositionCodePath();
            path2.positionCode = taskDetial.Endport;
            if (taskDetial.Crtype == "1")//叫桶(桶出库)
            {
                path2.type = "05";
            }
            else//申请储位(桶入库)
            {
                path2.type = "05";
            }
            pahtList.Add(path2);
            //下车任务单
            AgvSchedulingTask agvTask = new AgvSchedulingTask();
            agvTask.reqCode = taskDetial.Taskno;//任务号
            agvTask.ctnrCode = taskDetial.Pallno;//桶号
            agvTask.ctnrTyp = "1";
            agvTask.reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//请求时间
            agvTask.wbCode = "";
            agvTask.positionCodePath = pahtList;//小车路径
            agvTask.podCode = "";
            agvTask.userCallCode = "";//taskDetial.Endport;//目标位置
            if (taskDetial.Crtype == "1")//叫桶(桶出库)
            {
                agvTask.taskTyp = "Z3";//任务类型 线边到托盘收集器 Z1, 托盘垛申请入库 Z2
            }
            else//申请储位(桶入库)
            {
                agvTask.taskTyp = "Z4";//任务类型 线边到托盘收集器 Z1, 托盘垛申请入库 Z2
            }
            agvTaskList.Add(agvTask);
            // 正式运行程序放开
            var list2 = agvTaskList.Select(m => m.reqCode).ToList();
            var jsonData = JsonConvert.SerializeObject(agvTaskList);
            jsonData = jsonData.Substring(1);
            jsonData = jsonData.Substring(0, jsonData.Length - 1);
            string response = HttpHelper.DoPost(url, jsonData, "下发给AGV转运命令", "AGV");
            //解析返回数据
            var agvModel = JsonConvert.DeserializeObject<OutCommanAgvDto>(response);
            if (agvModel.Code == "0")
            {
                result = true;//给下车下发任务成功
            }
            else
            {
                string logMsg = "";
                if (taskDetial.Crtype == "1")//叫桶(桶出库)
                {
                    logMsg = "申请叫桶";
                }
                else if(taskDetial.Crtype == "0")//申请储位(桶入库)
                {
                    logMsg = "申请储位";
                }
                var logStr = $@".\log\AGV\AGV{logMsg}" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                LogFile.SaveLogToFile($"AGV{logMsg}异常:( {agvModel.Message} ),", logStr);
            }
            return result;
            #endregion
        }
        /// <summary>
        /// 任务完成
        /// </summary>
        /// <param name="TaskNo"></param>
        /// <param name="Status"></param>
        /// <exception cref="Exception"></exception>
        public void RCSFinishTask(string TaskNo, string Status)
        public void RCSFinishTask(string taskNo, string status, string comeFrom, int userId = 0)
        {
            try
            {
                #region 条件判断
                var resultModel = new ErpModel() { Success = -1, Message = "" };
                if (string.IsNullOrEmpty(TaskNo))
                if (string.IsNullOrEmpty(taskNo))
                {
                    throw new Exception("任务号不可为空");
                }
                //任务信息
                var taskInfo = Db.Queryable<LogTask>().First(w => w.TaskNo == TaskNo);
                var taskInfo = Db.Queryable<LogTask>().First(w => w.TaskNo == taskNo);
                if (taskInfo == null)
                {
                    throw new Exception($"任务号为:{TaskNo}的任务不存在!");
                    throw new Exception($"任务号为:{taskNo}的任务不存在!");
                }
                if (taskInfo.Status != "1")
                if (taskInfo.Status != "1" && comeFrom != "WMS")
                {
                    throw new Exception($"任务号为:{TaskNo}的任务状态异常");
                    throw new Exception($"任务号为:{taskNo}的任务状态异常");
                }
                #endregion
                //开启事务
                Db.BeginTran();
                var comTime = DateTime.Now;
                if (Status == "0")
                if (status == "0")
                {
                    taskInfo.Status = "3";//异常结束
                    //修改任务状态
@@ -726,36 +677,45 @@
                //修改目标储位状态
                Db.Updateable(endLocatInfo).ExecuteCommand();
                //目标储位所属区域
                var endAreaInfo = Db.Queryable<SysStorageArea>().First(w => w.IsDel == "0" && w.WareHouseNo == endLocatInfo.WareHouseNo && w.AreaNo == endLocatInfo.AreaNo);
                if (endAreaInfo == null)
                {
                    throw new Exception($"目标储位所属区域信息不存在");
                }
                stockDetail.WareHouseNo = endLocatInfo.WareHouseNo;//所属仓库
                stockDetail.RoadwayNo = endLocatInfo.RoadwayNo;//所属巷道
                stockDetail.AreaNo = endLocatInfo.AreaNo;//所属区域
                stockDetail.LocatNo = endLocatInfo.LocatNo;//储位地址
                stockDetail.Status = "0";
                if (endLocatInfo.AreaNo.Contains("01") || endLocatInfo.AreaNo.Contains("11") || endLocatInfo.AreaNo.Contains("21") || endLocatInfo.AreaNo.Contains("31"))//洁净区
                stockDetail.LocatNo = endLocatInfo.LocatNo;//储位地址
                if (endAreaInfo.Type == "0")//洁净区
                {
                    stockDetail.PalletStatus = "0";
                    stockDetail.Status = "0";
                    stockDetail.Status = "0";//待分配
                }
                else if (endLocatInfo.AreaNo.Contains("02") || endLocatInfo.AreaNo.Contains("12") || endLocatInfo.AreaNo.Contains("22") || endLocatInfo.AreaNo.Contains("32"))//预混区
                else if (endAreaInfo.Type == "1")//预混区
                {
                    stockDetail.PalletStatus = "1";
                    stockDetail.Status = "0";//待分配
                }
                else if (endLocatInfo.AreaNo.Contains("03") || endLocatInfo.AreaNo.Contains("13") || endLocatInfo.AreaNo.Contains("23") || endLocatInfo.AreaNo.Contains("33"))//半成品区
                else if (endAreaInfo.Type == "2")//半成品区
                {
                    stockDetail.PalletStatus = "2";
                    stockDetail.Status = "0";//待分配
                }
                else if (endLocatInfo.AreaNo.Contains("04") || endLocatInfo.AreaNo.Contains("14") || endLocatInfo.AreaNo.Contains("24")|| endLocatInfo.AreaNo.Contains("34"))//脏桶区
                else if (endAreaInfo.Type == "3")//脏桶区
                {
                    stockDetail.PalletStatus = "3";
                    stockDetail.LotNo = "";//批次
                    stockDetail.SkuNo = "";
                    stockDetail.SkuName = "";
                    stockDetail.Status = "0";//待分配
                    stockDetail.InspectStatus = "0";//待检验
                }
                //修改库存明细
                Db.Updateable(stockDetail).ExecuteCommand();
                //入库任务,处理入库单
                if (taskInfo.Type == "0" && taskInfo.NoticeDetailNo!=0)
                if (taskInfo.Type == "0" && taskInfo.NoticeDetailNo != 0)
                {
                    var notice = Db.Queryable<BllArrivalNotice>().First(w => w.IsDel == "0" && w.Status == "1");
                    if (notice != null)
@@ -787,9 +747,18 @@
                #endregion
                taskInfo.Status = "2";//执行完成
                taskInfo.IsSend = 0;
                taskInfo.IsCancel = 0;
                taskInfo.IsFinish = 0;
                taskInfo.FinishDate = DateTime.Now;//完成时间
                //修改任务状态
                Db.Updateable(taskInfo).ExecuteCommand();
                if (comeFrom == "WMS")
                {
                    //添加操作日志记录
                    var k = new OperationCrServer().AddLogOperationCr("库内作业", "库内日志", taskInfo.TaskNo, "完成", $"点击完成按钮、任务号为:{taskInfo.TaskNo}的任务", userId);
                }
                //提交事务
                Db.CommitTran();
            }
@@ -800,5 +769,259 @@
                throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// 手动取消任务
        /// </summary>
        /// <param name="taskNo"></param>
        /// <param name="userId"></param>
        /// <exception cref="Exception"></exception>
        public void CancelTask(string taskNo, int userId)
        {
            try
            {
                //开启事务
                Db.BeginTran();
                //任务信息
                var task = Db.Queryable<LogTask>().First(m => m.TaskNo == taskNo && m.IsDel == "0");
                if (task == null)
                {
                    throw new Exception("未查询到任务信息");
                }
                //if (task.Status != "0")
                //{
                //    throw new Exception("只有等待执行的任务才可取消");
                //}
                //修改任务
                task.IsSuccess = 0;
                task.IsSend = 0;
                task.IsCancel = 0;
                task.IsFinish = 0;
                task.Status = "4";//已取消
                task.CancelDate = DateTime.Now;
                Db.Updateable(task).ExecuteCommand();
                //修改起始储位信息
                var locat = Db.Queryable<SysStorageLocat>().First(a => a.IsDel == "0" && a.LocatNo == task.StartLocat);
                if (locat == null)
                {
                    throw new Exception("未查询到起始位置储位信息,请核实!");
                }
                locat.Status = "1"; //有物品
                Db.Updateable(locat).ExecuteCommand();
                //修改目标储位信息
                var locatEnd = Db.Queryable<SysStorageLocat>().First(a => a.IsDel == "0" && a.LocatNo == task.EndLocat);
                if (locatEnd == null)
                {
                    throw new Exception("未查询到目标位置储位信息,请核实!");
                }
                locatEnd.Status = "0"; //空储位
                Db.Updateable(locatEnd).ExecuteCommand();
                if (task.Type == "1")//出库任务
                {
                    //修改库存明细
                    var stockDetail = Db.Queryable<DataStockDetail>().First(w => w.IsDel == "0" && w.PalletNo == task.PalletNo && w.Status == "2");
                    if (stockDetail == null)
                    {
                        throw new Exception("未查询到该桶库存信息,请检查!");
                    }
                    stockDetail.Status = "0";//待分配
                    Db.Updateable(stockDetail).ExecuteCommand();
                }
                //添加操作日志记录
                var k = new OperationCrServer().AddLogOperationCr("库内作业", "库内日志", taskNo, "取消", $"点击取消按钮、取消了任务号为:{taskNo}的任务", userId);
                Db.CommitTran();
            }
            catch (Exception e)
            {
                Db.RollbackTran();
                throw new Exception(e.Message);
            }
        }
        /// <summary>
        /// 手动下发任务
        /// </summary>
        /// <param name="taskNo"></param>
        /// <param name="userId"></param>
        /// <exception cref="Exception"></exception>
        public void DownTask(string taskNo, int userId,string url)
        {
            try
            {
                //开启事务
                Db.BeginTran();
                //任务信息
                var logTaskEntry = Db.Queryable<LogTask>().First(m => m.TaskNo == taskNo && m.IsDel == "0");
                if (logTaskEntry == null)
                {
                    throw new Exception("未查询到任务信息");
                }
                if (logTaskEntry.Status != "0")
                {
                    throw new Exception("只有等待执行的任务才可手动下发");
                }
                //查找该桶有没有正在执行的任务
                var tasking = Db.Queryable<LogTask>().First(w => w.IsDel == "0" && w.Status == "1" && w.PalletNo == logTaskEntry.PalletNo);
                if (tasking != null)
                {
                    throw new Exception($"该桶有正在执行的任务");
                }
                //起始储位信息
                var startLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == logTaskEntry.StartLocat);
                if (startLocatInfo == null)
                {
                    throw new Exception($"未查询到起始储位信息");
                }
                if (startLocatInfo.Status != "1")
                {
                    throw new Exception($"起始储位状态异常");
                }
                //目标储位信息
                var endLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == logTaskEntry.EndLocat);
                if (endLocatInfo == null)
                {
                    throw new Exception($"未查询到目标储位信息");
                }
                if (endLocatInfo.Status != "0")
                {
                    throw new Exception($"目标储位状态异常");
                }
                //组织给小车下发任务信息
                var task = new TaskDetial
                {
                    Taskno = taskNo,//任务号
                    Startport = logTaskEntry.StartLocat,
                    Endport = logTaskEntry.EndLocat,
                    Pallno = logTaskEntry.PalletNo,
                    Crtype = logTaskEntry.Type,
                };
                //给下车下发任务
                logTaskEntry.SendDate = DateTime.Now;//发送时间
                var agvResult = CreateTaskForAgv(task, url);
                if (agvResult)//成功
                {
                    //请求成功修改任务表相应字段状态
                    logTaskEntry.IsSuccess = 1;
                    logTaskEntry.IsSend = 0;
                    logTaskEntry.IsCancel = 0;
                    logTaskEntry.BackDate = DateTime.Now;
                    logTaskEntry.Status = "1";//正在执行
                    Db.Insertable(logTaskEntry).ExecuteCommand();
                    startLocatInfo.Status = "3";//出库中
                    Db.Updateable(startLocatInfo).ExecuteCommand();
                    endLocatInfo.Status = "2";//入库中
                    Db.Updateable(endLocatInfo).ExecuteCommand();
                }
                else//失败
                {
                    logTaskEntry.IsSuccess = 0;
                    Db.Insertable(logTaskEntry).ExecuteCommand();
                }
                //添加操作日志记录
                var k = new OperationCrServer().AddLogOperationCr("库内作业", "库内日志", taskNo, "下发", $"点击下发按钮、任务号为:{taskNo}的任务", userId);
                Db.CommitTran();
            }
            catch (Exception e)
            {
                Db.RollbackTran();
                throw new Exception(e.Message);
            }
        }
        /// <summary>
        /// 生成任务单到LogTask
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public bool CreateTaskForAgv(TaskDetial taskDetial, string url)
        {
            bool result = false;
            #region 呼叫小车代码
            List<AgvSchedulingTask> agvTaskList = new List<AgvSchedulingTask>();
            List<PositionCodePath> pahtList = new List<PositionCodePath>();
            //起始位置
            PositionCodePath path1 = new PositionCodePath();
            path1.positionCode = taskDetial.Startport;
            if (taskDetial.Crtype == "1")//叫桶(桶出库)
            {
                path1.type = "05";
            }
            else//申请储位(桶入库)
            {
                path1.type = "05";
            }
            pahtList.Add(path1);
            //目标位置
            PositionCodePath path2 = new PositionCodePath();
            path2.positionCode = taskDetial.Endport;
            if (taskDetial.Crtype == "1")//叫桶(桶出库)
            {
                path2.type = "05";
            }
            else//申请储位(桶入库)
            {
                path2.type = "05";
            }
            pahtList.Add(path2);
            //下车任务单
            AgvSchedulingTask agvTask = new AgvSchedulingTask();
            agvTask.reqCode = taskDetial.Taskno;//请求编号
            agvTask.taskCode = taskDetial.Taskno;//任务号
            agvTask.ctnrCode = taskDetial.Pallno;//桶号
            agvTask.ctnrTyp = "1";
            agvTask.reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//请求时间
            agvTask.wbCode = "";
            agvTask.positionCodePath = pahtList;//小车路径
            agvTask.podCode = "";
            agvTask.userCallCode = "";//taskDetial.Endport;//目标位置
            if (taskDetial.Crtype == "1")//叫桶(桶出库)
            {
                agvTask.taskTyp = "Z3";//任务类型 线边到托盘收集器 Z1, 托盘垛申请入库 Z2
            }
            else//申请储位(桶入库)
            {
                agvTask.taskTyp = "Z4";//任务类型 线边到托盘收集器 Z1, 托盘垛申请入库 Z2
            }
            agvTaskList.Add(agvTask);
            // 正式运行程序放开
            var list2 = agvTaskList.Select(m => m.reqCode).ToList();
            var jsonData = JsonConvert.SerializeObject(agvTaskList);
            jsonData = jsonData.Substring(1);
            jsonData = jsonData.Substring(0, jsonData.Length - 1);
            string response = HttpHelper.DoPost(url, jsonData, "下发给AGV转运命令", "AGV");
            //解析返回数据
            var agvModel = JsonConvert.DeserializeObject<OutCommanAgvDto>(response);
            if (agvModel.Code == "0")
            {
                result = true;//给下车下发任务成功
            }
            else
            {
                string logMsg = "";
                if (taskDetial.Crtype == "1")//叫桶(桶出库)
                {
                    logMsg = "申请叫桶";
                }
                else if (taskDetial.Crtype == "0")//申请储位(桶入库)
                {
                    logMsg = "申请储位";
                }
                var logStr = $@".\log\AGV\AGV{logMsg}" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                LogFile.SaveLogToFile($"AGV{logMsg}异常:( {agvModel.Message} ),", logStr);
            }
            return result;
            #endregion
        }
    }
}