DESKTOP-9BNTV8O
2024-12-24 2fd828d1dc0bb0383d8047b855cfc3a6c48ca6cf
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
{
@@ -37,198 +42,264 @@
                string taskMsg = "";
                if (string.IsNullOrEmpty(model.Location))
                {
                    throw new Exception("设备所在位置不可为空");
                    throw new Exception("设备编码不可为空");
                }
                //叫桶设备所在储位信息(目标储位)
                var endLoction = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == model.Location);
                if (endLoction == null)
                //通过设备号查找到所属设备(区域)
                var deviceInfo = Db.Queryable<SysStorageArea>().First(w => w.IsDel == "0" && w.DeviceCode.Contains(model.Location));
                if (deviceInfo == null)
                {
                    throw new Exception("设备储位信息不存在");
                    throw new Exception("设备信息不存在");
                }
                var houseNo = endLoction.WareHouseNo;//叫桶位置,后续根据位置关联或定义,来判断叫桶位置所属车间
                var houseNo = deviceInfo.WareHouseNo;//叫桶设备所属车间编号
                //叫桶设备所在储位信息(目标储位)
                var endLoction = new SysStorageLocat();
                var urlMes = "";//回传MES的接口地址
                var noticeNo = "0";//出入库单据明细ID
                var stockDetail = Db.Queryable<DataStockDetail>().Where(w => w.Status == "0");//所有待分配的桶
                //所有待分配的桶
                var stockDetail = Db.Queryable<DataStockDetail>().Where(w => w.Status == "0");
                //分配桶信息
                var pallet = new DataStockDetail();
                //开始事务
                Db.BeginTran();
                DataStockDetail pallet = new DataStockDetail();
                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();
                        if (pallet == null)
                    case "0"://接料设备叫净桶
                        {
                            throw new Exception("暂无净桶可分配");
                        }
                        taskMsg = "设备叫净桶任务";
                        break;
                    case "1"://叫料桶(混料)
                        //判断是否有批次号
                        if (string.IsNullOrWhiteSpace(model.LotNo))
                        {
                            throw new Exception("批次号为空!");
                        }
                        //查找库存中是否有可用的此批次的混料桶
                        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();
                        if (pallet == null)
                        {
                            throw new Exception("暂无混料桶可分配");
                        }
                        pallet.LotNo = model.LotNo;
                        //更新桶库存明细维护批次号
                        Db.Updateable(pallet).ExecuteCommand();
                        taskMsg = "设备叫料桶混料任务";
                        break;
                    case "2"://叫料桶(下料)
                        //判断是否有批次号
                        if (string.IsNullOrWhiteSpace(model.LotNo))
                        {
                            throw new Exception("批次号为空!");
                        }
                        //查找是否有此批次出库单明细
                        var soNoticeDetail = Db.Queryable<BllExportNoticeDetail>().Where(w => w.IsDel == "0" && w.LotNo == model.LotNo).OrderBy(o => o.CreateTime).First();
                        if (soNoticeDetail == null)
                        {
                            throw new Exception("该批次没有对应的出库单明细");
                        }
                        noticeNo = soNoticeDetail.Id.ToString();
                        //出库总单
                        var soNotice = Db.Queryable<BllExportNotice>().Where(w => w.IsDel == "0" && w.SONo == soNoticeDetail.SONo).First();
                        if (soNotice == null)
                        {
                            throw new Exception("该批次没有对应的出库单");
                        }
                        soNotice.Status = "3";//正在执行
                        soNotice.UpdateTime = DateTime.Now;
                        //更新出库单
                        Db.Updateable(soNotice).ExecuteCommand();
                        //查找库存中是否有此批次的合格下料桶
                        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();
                        if (pallet == null)
                        {
                            throw new Exception("暂无下料桶可分配");
                        }
                        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();
                        if (palletList.Count <= 0)
                        {
                            throw new Exception("暂无脏桶可分配");
                        }
                        //遍历库存脏桶,返回MES验证
                        foreach (var item in palletList)
                        {
                            #region 去掉MES
                            //string jsonReq = JsonConvert.SerializeObject(item.PalletNo);
                            //var response = HttpHelper.DoPost(urlMes, jsonReq, "回传MES脏桶编号", "RCS").ToString();
                            //var obj = JsonConvert.DeserializeObject<ReMes>(response);//解析返回数据
                            //if (obj.Success == "0")
                            //{
                            //    if (obj.Result != "1")//不可清洗,将脏桶冻结
                            //    {
                            //        item.Status = "5";//异常冻结
                            //        //修改该桶库存状态
                            //        Db.Updateable(item).ExecuteCommand();
                            //        continue;
                            //    }
                            //    //下发小车任务
                            //    var task2 = new TaskDetial
                            //    {
                            //        Startport = item.LocatNo,
                            //        Endport = model.Location,
                            //        Pallno = item.PalletNo,
                            //        Type = model.Type,
                            //        Crtype = "1",
                            //        Noticedetailno = int.Parse(noticeno),
                            //    };
                            //    CreateLotTask(task2);
                            //    return;
                            //}
                            //else
                            //{
                            //    throw new Exception("回传MES净桶编号失败!");
                            //}
                            #endregion
                            //起始储位地址信息
                            var startLoction2 = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == item.LocatNo);
                            if (startLoction2 == null)
                            //叫桶目标储位地址信息
                            endLoction = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo);
                            if (endLoction == null)
                            {
                                throw new Exception($"起始目标储位信息不存在,桶号:{item.LocatNo}");
                                throw new Exception("叫桶设备目标储位信息不存在");
                            }
                            taskNo = new Common().GetMaxNo("TK");//任务号
                            var logTaskEntry2 = new LogTask
                            //查找到该车间净桶区
                            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)
                            {
                                TaskNo = taskNo,
                                Sender = "WMS",
                                Receiver = "RCS",
                                //IsSuccess = 1, //是否下发成功 0失败 1成功
                                SendDate = DateTime.Now,  //发送时间
                                                          //BackDate = DateTime.Now,  //返回时间
                                StartLocat = item.LocatNo,//起始位置
                                EndLocat = model.Location,//目标位置
                                PalletNo = item.PalletNo,//托盘码
                                IsSend = 1,//是否可再次下发
                                IsCancel = 1,//是否可取消
                                IsFinish = 1,//是否可完成
                                Type = "1",//任务类型 0 入库任务 1 出库任务  2 移库任务
                                OrderType = "3",//单据类型 0 入库 1 出库 3其他
                                Status = "0",//任务状态0:等待执行1正在执行2执行完成
                                NoticeDetailNo = int.Parse(noticeNo),
                                Msg = taskMsg, //关键信息
                            };
                            //组织下发小车任务信息
                            var task2 = new TaskDetial
                            {
                                Taskno = taskNo,//任务号
                                Startport = item.LocatNo,
                                Endport = model.Location,
                                Pallno = item.PalletNo,
                                Type = model.Type,
                                Crtype = "1",
                                Noticedetailno = int.Parse(noticeNo),
                            };
                            CreateLotTask(task2, url);
                            //给下车下发任务
                            logTaskEntry2.SendDate = DateTime.Now;//发送时间
                            var agvResult2 = CreateLotTask(task2, url);
                            if (agvResult2)//成功
                            {
                                //请求成功修改任务表相应字段状态
                                logTaskEntry2.IsSuccess = 1;
                                logTaskEntry2.BackDate = DateTime.Now;
                                logTaskEntry2.Status = "1";
                                Db.Insertable(logTaskEntry2).ExecuteCommand();
                                startLoction2.Status = "3";//出库中
                                Db.Updateable(startLoction2).ExecuteCommand();
                                endLoction.Status = "2";//入库中
                                Db.Updateable(endLoction).ExecuteCommand();
                                throw new Exception("暂无净桶可分配");
                            }
                            else//失败
                            {
                                logTaskEntry2.IsSuccess = 0;
                                Db.Insertable(logTaskEntry2).ExecuteCommand();
                            pallet.Status = "2";//分配状态 0:待分配,1:部分分配 , 2:已分配
                                                //更新桶库存明细
                            Db.Updateable(pallet).ExecuteCommand();
                            }
                            //提交事务
                            Db.CommitTran();
                            return;
                            taskMsg = "设备叫净桶任务";
                        }
                        break;
                    case "1"://混料设备叫料桶(混料)
                        {
                            //叫桶目标储位地址信息
                            endLoction = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo);
                            if (endLoction == null)
                            {
                                throw new Exception("叫桶设备目标储位信息不存在");
                            }
                            //判断是否有批次号
                            if (string.IsNullOrWhiteSpace(model.LotNo))
                            {
                                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.Status == "0" && w.LotNo == model.LotNo
                            && areaNoList2.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 = "设备叫脏桶任务";
                            taskMsg = "设备叫料桶混料任务";
                        }
                        break;
                    case "2"://下料设备叫料桶(下料)
                        {
                            if (houseNo == "M04")//除了喷干车间下料口都是2个储位(投料储位、投完料储位)
                            {
                                //叫桶目标储位地址信息
                                endLoction = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo);
                            }
                            else
                            {
                                //叫桶目标储位地址信息
                                endLoction = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo && w.Column == 1);// Column=1 投料储位,Column=2 投完料储位
                            }
                            if (endLoction == null)
                            {
                                throw new Exception("叫桶设备目标储位信息不存在");
                            }
                            //判断是否有批次号
                            if (string.IsNullOrWhiteSpace(model.LotNo))
                            {
                                throw new Exception("批次号为空!");
                            }
                            //查找是否有此批次出库单明细
                            var soNoticeDetail = Db.Queryable<BllExportNoticeDetail>().Where(w => w.IsDel == "0" && w.LotNo == model.LotNo).OrderBy(o => o.CreateTime).First();
                            if (soNoticeDetail == null)
                            {
                                throw new Exception("该批次没有对应的出库单明细");
                            }
                            noticeNo = soNoticeDetail.Id.ToString();
                            //出库总单
                            var soNotice = Db.Queryable<BllExportNotice>().Where(w => w.IsDel == "0" && w.SONo == soNoticeDetail.SONo).First();
                            if (soNotice == null)
                            {
                                throw new Exception("该批次没有对应的出库单");
                            }
                            soNotice.Status = "3";//正在执行
                            soNotice.UpdateTime = DateTime.Now;
                            //更新出库单
                            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.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"://清洗设备叫脏桶
                        {
                            //叫桶目标储位地址信息
                            endLoction = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo && w.Column == 1);// Column=1 清洗储位,Column=2 清洗完料储位
                            if (endLoction == null)
                            {
                                throw new Exception("叫桶设备目标储位信息不存在");
                            }
                            //查找到该车间脏桶区
                            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("暂无脏桶可分配");
                            }
                            taskMsg = "设备叫脏桶任务";
                            //遍历库存脏桶,返回MES验证
                            foreach (var item in palletList)
                            {
                                #region 去掉MES
                                //string jsonReq = JsonConvert.SerializeObject(item.PalletNo);
                                //var response = HttpHelper.DoPost(urlMes, jsonReq, "回传MES脏桶编号", "RCS").ToString();
                                //var obj = JsonConvert.DeserializeObject<ReMes>(response);//解析返回数据
                                //if (obj.Success == "0")
                                //{
                                //    if (obj.Result != "1")//不可清洗,将脏桶冻结
                                //    {
                                //        item.Status = "5";//异常冻结
                                //        //修改该桶库存状态
                                //        Db.Updateable(item).ExecuteCommand();
                                //        continue;
                                //    }
                                //    //下发小车任务
                                //    var task2 = new TaskDetial
                                //    {
                                //        Startport = item.LocatNo,
                                //        Endport = model.Location,
                                //        Pallno = item.PalletNo,
                                //        Type = model.Type,
                                //        Crtype = "1",
                                //        Noticedetailno = int.Parse(noticeno),
                                //    };
                                //    CreateLotTask(task2);
                                //    return;
                                //}
                                //else
                                //{
                                //    throw new Exception("回传MES净桶编号失败!");
                                //}
                                #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)
                                {
                                    throw new Exception($"起始目标储位信息不存在,桶号:{item.LocatNo}");
                                }
                                taskNo = new Common().GetMaxNo("TK");//任务号
                                var logTaskEntry2 = new LogTask
                                {
                                    TaskNo = taskNo,
                                    Sender = "WMS",
                                    Receiver = "RCS",
                                    //IsSuccess = 1, //是否下发成功 0失败 1成功
                                    SendDate = DateTime.Now,  //发送时间
                                                              //BackDate = DateTime.Now,  //返回时间
                                    StartLocat = item.LocatNo,//起始位置
                                    EndLocat = model.Location,//目标位置
                                    PalletNo = item.PalletNo,//托盘码
                                    IsSend = 1,//是否可再次下发
                                    IsCancel = 1,//是否可取消
                                    IsFinish = 1,//是否可完成
                                    Type = "1",//任务类型 0 入库任务 1 出库任务  2 移库任务
                                    OrderType = "3",//单据类型 0 入库 1 出库 3 移库
                                    Status = "0",//任务状态0:等待执行1正在执行2执行完成
                                    NoticeDetailNo = int.Parse(noticeNo),
                                    Msg = taskMsg, //关键信息
                                };
                                //组织下发小车任务信息
                                var task2 = new TaskDetial
                                {
                                    Taskno = taskNo,//任务号
                                    Startport = item.LocatNo,
                                    Endport = model.Location,
                                    Pallno = item.PalletNo,
                                    Crtype = "1",//叫桶
                                    WareHouseNo = houseNo
                                };
                                //给下车下发任务
                                logTaskEntry2.SendDate = DateTime.Now;//发送时间
                                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();
                                    startLoction2.Status = "3";//出库中
                                    Db.Updateable(startLoction2).ExecuteCommand();
                                    endLoction.Status = "2";//入库中
                                    Db.Updateable(endLoction).ExecuteCommand();
                                }
                                else//失败
                                {
                                    logTaskEntry2.IsSuccess = 0;
                                    Db.Insertable(logTaskEntry2).ExecuteCommand();
                                }
                                //提交事务
                                Db.CommitTran();
                                return;
                            }
                        }
                        break;
                    default:
                        throw new Exception("任务类型无效");
@@ -257,7 +328,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, //关键信息
@@ -266,23 +337,24 @@
                //组织下发小车任务信息
                var task = new TaskDetial
                {
                    Taskno= taskNo,//任务号
                    Taskno = taskNo,//任务号
                    Startport = pallet.LocatNo,//起始位置
                    Endport = model.Location,//目标位置
                    Pallno = pallet.PalletNo,//桶号
                    Crtype = "1",//叫桶
                    Noticedetailno = int.Parse(noticeNo),
                    LotNo = model.LotNo,
                    WareHouseNo = houseNo//车间编码
                };
                //给下车下发任务
                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,14 +392,33 @@
            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 deviceInfo = Db.Queryable<SysStorageArea>().First(w => w.IsDel == "0" && w.DeviceCode == model.Location);
                if (deviceInfo == null)
                {
                    throw new Exception("设备信息不存在");
                }
                var houseNo = deviceInfo.WareHouseNo;//申请储位设备所在车间编号
                //设备所在储位信息(起始储位)
                var deviceLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == model.Location);
                var deviceLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo);
                if (deviceLocatInfo == null)
                {
                    throw new Exception($"未查询到该设备所在储位信息,Location:{model.Location}");
                }
                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,67 +428,114 @@
                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();
                        if (loction == null)
                        {
                            throw new Exception("该车间净桶区暂无空余储位");
                        }
                            //设备所在储位信息(起始储位)
                            deviceLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo && w.Column == 2);// Column=1 清洗储位,Column=2 清洗完料储位
                            if (deviceLocatInfo == null)
                            {
                                throw new Exception($"未查询到该设备所在储位信息,Location:{model.Location}");
                            }
                            //查找到该车间净桶区;
                            loction = GetLocatModel(houseNo, "0");
                            if (loction == null)
                            {
                                throw new Exception("该车间净桶区暂无空余储位");
                            }
                        taskMsg = "清洗设备(净桶)申请储位任务";
                            taskMsg = "清洗设备(净桶)申请储位任务";
                        }
                        break;
                    case "0"://接料设备申请储位(预混料桶申请储位)
                        //判断是否有批次号
                        if (string.IsNullOrWhiteSpace(model.LotNo))
                        {
                            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();
                        if (loction == null)
                        {
                            throw new Exception("该车间预混区暂无空余储位");
                        }
                            //设备所在储位信息(起始储位)
                            deviceLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo);
                            if (deviceLocatInfo == null)
                            {
                                throw new Exception($"未查询到该设备所在储位信息,Location:{model.Location}");
                            }
                            //判断是否有批次号
                            if (string.IsNullOrWhiteSpace(model.LotNo))
                            {
                                throw new Exception("批次号为空!");
                            }
                            //查找到该车间预混区
                            loction = GetLocatModel(houseNo, "1");
                            if (loction == null)
                            {
                                throw new Exception("该车间预混区暂无空余储位");
                            }
                            palletInfo.LotNo = model.LotNo;//批次号
                                                           //更新桶库存信息
                            Db.Updateable(palletInfo).ExecuteCommand();
                        taskMsg = "接料设备(预混料桶)申请储位任务";
                            taskMsg = "接料设备(预混料桶)申请储位任务";
                        }
                        break;
                    case "1"://混料设备申请储位(半成品桶申请储位)
                        //判断是否有批次号
                        if (string.IsNullOrWhiteSpace(model.LotNo))
                        {
                            throw new Exception("批次号为空!");
                        }
                        //校验此批次是否有入库单
                        var noticeDetail = Db.Queryable<BllArrivalNoticeDetail>().Where(w => w.IsDel == "0" && w.Status == "0" && w.LotNo == model.LotNo).OrderByDescending(o => o.CreateTime).First();
                        if (noticeDetail == null)
                        {
                            throw new Exception("该批次没有对应的入库单明细");
                        }
                        noticeno = noticeDetail.Id.ToString();
                        //入库总单
                        var notice = Db.Queryable<BllArrivalNotice>().Where(w => w.IsDel == "0" && w.ASNNo == noticeDetail.ASNNo).First();
                        if (notice == null)
                        {
                            throw new Exception("该批次没有对应的入库单");
                        }
                        notice.Status = "1";//正在执行
                        //更新入库单状态
                        Db.Updateable(notice).ExecuteCommand();
                            //设备所在储位信息(起始储位)
                            deviceLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo);
                            if (deviceLocatInfo == null)
                            {
                                throw new Exception($"未查询到该设备所在储位信息,Location:{model.Location}");
                            }
                            //判断是否有批次号
                            if (string.IsNullOrWhiteSpace(model.LotNo))
                            {
                                throw new Exception("批次号为空!");
                            }
                            //校验此批次是否有入库单
                            var noticeDetail = Db.Queryable<BllArrivalNoticeDetail>().Where(w => w.IsDel == "0" && w.Status == "0" && w.LotNo == model.LotNo).OrderByDescending(o => o.CreateTime).First();
                            if (noticeDetail == null)
                            {
                                throw new Exception("该批次没有对应的入库单明细");
                            }
                            noticeno = noticeDetail.Id.ToString();
                            //入库总单
                            var notice = Db.Queryable<BllArrivalNotice>().Where(w => w.IsDel == "0" && w.ASNNo == noticeDetail.ASNNo).First();
                            if (notice == null)
                            {
                                throw new Exception("该批次没有对应的入库单");
                            }
                            notice.Status = "1";//正在执行
                                                //更新入库单状态
                            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();
                        if (loction == null)
                        {
                            throw new Exception("该车间半成品区暂无空余储位");
                        }
                            //查找到该车间半成品区
                            loction = GetLocatModel(houseNo, "2");
                            if (loction == null)
                            {
                                throw new Exception("该车间半成品区暂无空余储位");
                            }
                        taskMsg = "接料设备(混料桶)申请储位任务";
                            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();
                        if (loction == null)
                        {
                            throw new Exception("该车间脏桶区暂无空余储位");
                        }
                            if (houseNo == "M04")//除了喷干车间下料口都是2个储位(投料储位、投完料储位)
                            {
                                //设备所在储位信息(起始储位)
                                deviceLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo);
                            }
                            else
                            {
                                //设备所在储位信息(起始储位)
                                deviceLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo && w.Column == 2);//Column=1 投料储位,Column=2 投完料储位
                            }
                            if (deviceLocatInfo == null)
                            {
                                throw new Exception($"未查询到该设备所在储位信息,Location:{model.Location}");
                            }
                            //查找到该车间脏桶区
                            loction = GetLocatModel(houseNo, "3");
                            if (loction == null)
                            {
                                throw new Exception("该车间脏桶区暂无空余储位");
                            }
                        taskMsg = "下料设备(脏桶)申请储位任务";
                            taskMsg = "下料设备(脏桶)申请储位任务";
                        }
                        break;
                }
@@ -412,36 +550,37 @@
                    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,
                    WareHouseNo = houseNo
                };
                //给下车下发任务
                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";//出库中
@@ -462,6 +601,33 @@
            {
                //回滚事务
                Db.RollbackTran();
                throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// 获取空储位
        /// </summary>
        /// <param name="houseNo">车间编号</param>
        /// <param name="Type">类别 0:净桶区 1:预混区 2:半成品区 3:脏桶区</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        private SysStorageLocat GetLocatModel(string houseNo,string type)
        {
            try
            {
                //通过类别查找车间区域
                List<string> areaNoList = Db.Queryable<SysStorageArea>().Where(w => w.IsDel == "0" && w.Status == "0" && w.Type == type && w.WareHouseNo == houseNo).Select(s => s.AreaNo).ToList();
                //查找储位
                var loctionModel = Db.Queryable<SysStorageLocat>().Where(w => w.IsDel == "0" && w.Status == "0" && w.Flag == "0" && w.WareHouseNo == houseNo && areaNoList.Contains(w.AreaNo))
                    .OrderBy(o => o.Row).OrderByDescending(o => o.Column).First();
                if (loctionModel == null)
                {
                    throw new Exception("该车间净桶区暂无空余储位");
                }
                return loctionModel;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
@@ -507,12 +673,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 +715,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 +727,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,59 +797,99 @@
                //修改目标储位状态
                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)
                    {
                        notice.Status = "2";//执行完成
                        notice.UpdateTime = DateTime.Now;
                        notice.CompleteTime = DateTime.Now;
                        //修改入库单状态
                        Db.Updateable(notice).ExecuteCommand();
                    //入库单明细
                    var noticeDetail = Db.Queryable<BllArrivalNoticeDetail>().First(w => w.IsDel == "0" && w.Id == taskInfo.NoticeDetailNo);
                    if (noticeDetail != null)
                    {
                        noticeDetail.CompleteQty += 1;//完成数量
                        if (noticeDetail.CompleteQty >= noticeDetail.Qty)
                        {
                            noticeDetail.CompleteTime = DateTime.Now;//完成时间
                            //入库单
                            var notice = Db.Queryable<BllArrivalNotice>().First(w => w.IsDel == "0" && w.ASNNo == noticeDetail.ASNNo);
                            var noticeDetailList = Db.Queryable<BllArrivalNoticeDetail>().Where(w => w.IsDel == "0" && w.CompleteQty < w.Qty && w.Id != noticeDetail.Id).ToList();
                            if (noticeDetailList.Count <= 0)
                            {
                                notice.Status = "2";//执行完成
                                notice.UpdateTime = DateTime.Now;
                                notice.CompleteTime = DateTime.Now;
                                //修改入库单状态
                                Db.Updateable(notice).ExecuteCommand();
                            }
                        }
                        //修改入库单明细
                        Db.Updateable(noticeDetail).ExecuteCommand();
                    }
                }
                //出库任务,处理出库单
                if (taskInfo.Type == "1" && taskInfo.NoticeDetailNo != 0)//入库
                {
                    var notice = Db.Queryable<BllExportNotice>().First(w => w.IsDel == "0" && w.Status == "3");
                    if (notice != null)
                    var noticeDetail = Db.Queryable<BllExportNoticeDetail>().First(w => w.IsDel == "0" && w.Id==taskInfo.NoticeDetailNo);
                    if (noticeDetail != null)
                    {
                        notice.Status = "4";//执行完成
                        notice.UpdateTime = DateTime.Now;
                        notice.CompleteTime = DateTime.Now;
                        //修改入库单状态
                        Db.Updateable(notice).ExecuteCommand();
                        noticeDetail.CompleteQty += 1;//完成数量
                        if (noticeDetail.CompleteQty >= noticeDetail.Qty)
                        {
                            var notice = Db.Queryable<BllExportNotice>().First(w => w.IsDel == "0" && w.SONo == noticeDetail.SONo);
                            var noticeDetailList = Db.Queryable<BllExportNoticeDetail>().Where(w => w.IsDel == "0" && w.CompleteQty < w.Qty && w.Id != noticeDetail.Id).ToList();
                            if (noticeDetailList.Count <= 0)
                            {
                                notice.Status = "4";//执行完成
                                notice.UpdateTime = DateTime.Now;
                                notice.CompleteTime = DateTime.Now;
                                //修改入库单状态
                                Db.Updateable(notice).ExecuteCommand();
                            }
                        }
                        //修改入库单明细
                        Db.Updateable(noticeDetail).ExecuteCommand();
                    }
                }
                #region 去掉MES
@@ -787,9 +898,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 +920,273 @@
                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<PositionCodePath> pahtList = new List<PositionCodePath>();
            //起始位置
            PositionCodePath path1 = new PositionCodePath();
            path1.positionCode = taskDetial.Startport;
            if (taskDetial.Crtype == "1")//叫桶(桶出库)
            {
                path1.type = "05";
            }
            else if (taskDetial.Crtype == "0")//申请储位(桶入库)
            {
                path1.type = "00";
            }
            else//同车间平层搬运
            {
                path1.type = "05";
            }
            pahtList.Add(path1);
            //目标位置
            PositionCodePath path2 = new PositionCodePath();
            path2.positionCode = taskDetial.Endport;
            if (taskDetial.Crtype == "1")//叫桶(桶出库)
            {
                path2.type = "00";
            }
            else 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.reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//请求时间
            agvTask.wbCode = "";
            agvTask.positionCodePath = pahtList;//小车路径
            agvTask.podCode = "";
            agvTask.userCallCode = "";//taskDetial.Endport;//目标位置
            //判断容器类型
            if (taskDetial.WareHouseNo == "M04")//喷干车间
            {
                agvTask.ctnrTyp = "2";// 1:桶 2:桶(小) 3:托盘
            }
            else
            {
                agvTask.ctnrTyp = "1";// 1:桶 2:桶(小) 3:托盘
            }
            //判断任务类型
            if (taskDetial.Crtype == "1")//叫桶(桶出库)
            {
                agvTask.taskTyp = "Z3";//任务类型 线边到托盘收集器 Z1, 托盘垛申请入库 Z2
            }
            else if (taskDetial.Crtype == "0")//申请储位(桶入库)
            {
                agvTask.taskTyp = "Z4";//任务类型 线边到托盘收集器 Z1, 托盘垛申请入库 Z2
            }
            else//同车间平层搬运
            {
                agvTask.taskTyp = "Z5";
            }
            // 正式运行程序放开
            var jsonData = JsonConvert.SerializeObject(agvTask);
            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
        }
    }
}