From 8cd6bb29fe8e46930ad199c4edfbec216475a988 Mon Sep 17 00:00:00 2001
From: wxw <Administrator@DESKTOP-5BIMHQ3>
Date: 星期二, 11 三月 2025 19:47:15 +0800
Subject: [PATCH] 修改问题

---
 Wms/WMS.BLL/BllTransServer/RcsServer.cs | 1709 +++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 1,305 insertions(+), 404 deletions(-)

diff --git a/Wms/WMS.BLL/BllTransServer/RcsServer.cs b/Wms/WMS.BLL/BllTransServer/RcsServer.cs
index 25b3176..219fec2 100644
--- a/Wms/WMS.BLL/BllTransServer/RcsServer.cs
+++ b/Wms/WMS.BLL/BllTransServer/RcsServer.cs
@@ -1,4 +1,7 @@
 锘縰sing Model.InterFaceModel;
+using Model.ModelDto;
+using Model.ModelDto.DataDto;
+using Model.ModelDto.LogDto;
 using Newtonsoft.Json;
 using SqlSugar;
 using System;
@@ -6,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;
@@ -16,177 +21,734 @@
 using WMS.Entity.SysEntity;
 using WMS.IBLL.IBllTransServer;
 using static Model.InterFaceModel.RCSModel;
+using static System.Collections.Specialized.BitVector32;
 
 namespace WMS.BLL.BllTransServer
 {
     public class RcsServer:IRcsServer
     {
+        private readonly object RcsLock = new object();
+
         private static readonly SqlSugarScope Db = DataContext.Db;
         /// <summary>
-        /// RCS鍙《(鍑�妗跺拰鑴忔《)
+        ///  RCS鍙《
         /// </summary>
-        /// <param name="warehouseno">搴撳尯</param>
-        /// <param name="type">鍙枡绫诲瀷</param>
-        /// <returns></returns>
-        public void GetPalletNo(Pallnetmsg model,string url)
+        /// <param name="model"></param>
+        /// <param name="url"></param>
+        /// <param name="taskNo"></param>
+        /// <exception cref="Exception"></exception>
+        public void GetPalletNo(Pallnetmsg model, string url, out string taskNo)
         {
-            DataStockDetail pallet = new DataStockDetail();
+            lock (RcsLock)
+            {
+                try
+                {
+                    string taskMsg = "";
+                    if (string.IsNullOrEmpty(model.Location))
+                    {
+                        throw new Exception("璁惧缂栫爜涓嶅彲涓虹┖");
+                    }
+                    //閫氳繃璁惧鍙锋煡鎵惧埌鎵�灞炶澶囷紙鍖哄煙锛�
+                    var deviceInfo = Db.Queryable<SysStorageArea>().First(w => w.IsDel == "0" && w.DeviceCode.Contains(model.Location));
+                    if (deviceInfo == null)
+                    {
+                        throw new Exception("璁惧淇℃伅涓嶅瓨鍦�");
+                    }
+                    var houseNo = deviceInfo.WareHouseNo;//鍙《璁惧鎵�灞炶溅闂寸紪鍙�
+
+                    //鍙《璁惧鎵�鍦ㄥ偍浣嶄俊鎭紙鐩爣鍌ㄤ綅锛�
+                    var endLoction = new SysStorageLocat();
+
+                    var urlMes = "";//鍥炰紶MES鐨勬帴鍙e湴鍧�
+                    var noticeNo = "0";//鍑哄叆搴撳崟鎹槑缁咺D
+
+                    //鎵�鏈夊緟鍒嗛厤鐨勬《
+                    var stockDetail = Db.Queryable<DataStockDetail>().Where(w => w.Status == "0");
+                    //鍒嗛厤妗朵俊鎭�
+                    var pallet = new DataStockDetail();
+
+                    //寮�濮嬩簨鍔�
+                    Db.BeginTran();
+                    //鍒ゆ柇鍙枡璁惧鏄惁鏄疉鍖虹殑娣锋枡璁惧03锛岃璁惧鏄帴鏂欐贩鏂欎竴浣撴満锛孉GV鍙《搴旇鍙噣妗舵帴鏂�
+                    if (houseNo == "M03" && model.Location == "125" && model.Type == "1")
+                    {
+                        model.Type = "0";
+                    }
+                    string agvMsg = string.Empty;
+                    switch (model.Type)
+                    {
+                        case "0"://鎺ユ枡璁惧鍙噣妗�
+                            {
+                                //鍙《鐩爣鍌ㄤ綅鍦板潃淇℃伅
+                                endLoction = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo && w.Column == 1);
+                                if (endLoction == null)
+                                {
+                                    throw new Exception("鍙《璁惧鐩爣鍌ㄤ綅淇℃伅涓嶅瓨鍦�");
+                                }
+                                //鏌ユ壘鍒拌杞﹂棿鍑�妗跺尯
+                                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();
+                                //鏌ユ壘鍑�妗�
+                                if (houseNo == "M03")//澶у崟浣撹溅闂村噣妗舵槸缁戝畾鎺ユ枡璁惧鐨�
+                                {
+                                    pallet = stockDetail.Where(w => w.WareHouseNo == houseNo && w.PalletStatus == "0" && w.Status == "0"
+                                && areaNoList.Contains(w.AreaNo) && w.UDF1 == deviceInfo.DeviceCode).OrderBy(o => o.UpdateTime).First();
+                                }
+                                else
+                                {
+                                    pallet = stockDetail.Where(w => w.WareHouseNo == houseNo && w.PalletStatus == "0" && w.Status == "0"
+                                && areaNoList.Contains(w.AreaNo)).OrderBy(o => o.UpdateTime).First();
+                                }
+
+                                if (pallet == null)
+                                {
+                                    throw new Exception("鏆傛棤鍑�妗跺彲鍒嗛厤");
+                                }
+                                //鍒嗛厤妗跺悗鍒ゆ柇璇ユ《澶栦晶鍌ㄤ綅鏄惁闇�瑕佺Щ搴�
+                                bool yikuResult = YikuTask(pallet.PalletNo, url);
+                                if (!yikuResult)
+                                {
+                                    throw new Exception("绉诲簱澶辫触");
+                                }
+
+                                pallet.Status = "2";//鍒嗛厤鐘舵�� 0:寰呭垎閰嶏紝1锛氶儴鍒嗗垎閰� 锛� 2:宸插垎閰�
+                                                    //鏇存柊妗跺簱瀛樻槑缁�
+                                Db.Updateable(pallet).ExecuteCommand();
+
+                                taskMsg = "璁惧鍙噣妗朵换鍔�";
+                            }
+                            break;
+                        case "1"://娣锋枡璁惧鍙枡妗讹紙娣锋枡锛�
+                            {
+                                //鍙《鐩爣鍌ㄤ綅鍦板潃淇℃伅
+                                endLoction = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo && w.Column == 1);
+                                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)).OrderBy(o => o.UpdateTime).First();
+                                if (pallet == null)
+                                {
+                                    throw new Exception("鏆傛棤娣锋枡妗跺彲鍒嗛厤");
+                                }
+                                //鍒嗛厤妗跺悗鍒ゆ柇璇ユ《澶栦晶鍌ㄤ綅鏄惁闇�瑕佺Щ搴�
+                                bool yikuResult = YikuTask(pallet.PalletNo, url);
+                                if (!yikuResult)
+                                {
+                                    throw new Exception("绉诲簱澶辫触");
+                                }
+
+                                pallet.Status = "2";//鍒嗛厤鐘舵�� 0:寰呭垎閰嶏紝1锛氶儴鍒嗗垎閰� 锛� 2:宸插垎閰�
+                                                    //鏇存柊妗跺簱瀛樻槑缁�
+                                Db.Updateable(pallet).ExecuteCommand();
+
+                                taskMsg = "璁惧鍙枡妗舵贩鏂欎换鍔�";
+                            }
+                            break;
+                        case "2"://涓嬫枡璁惧鍙枡妗讹紙涓嬫枡锛�
+                            {
+                                if (houseNo == "M04")//闄や簡鍠峰共杞﹂棿涓嬫枡鍙i兘鏄�2涓偍浣嶏紙鎶曟枡鍌ㄤ綅銆佹姇瀹屾枡鍌ㄤ綅锛�
+                                {
+                                    //鍙《鐩爣鍌ㄤ綅鍦板潃淇℃伅
+                                    endLoction = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo && w.Column == 1);
+                                }
+                                else
+                                {
+                                    //鍙《鐩爣鍌ㄤ綅鍦板潃淇℃伅
+                                    endLoction = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo && w.Column == 1);// Column=1 鎶曟枡鍌ㄤ綅锛孋olumn=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";//姝e湪鎵ц
+                                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)).OrderBy(o => o.UpdateTime).First();
+                                if (pallet == null)
+                                {
+                                    throw new Exception("鏆傛棤涓嬫枡妗跺彲鍒嗛厤");
+                                }
+                                //鍒嗛厤妗跺悗鍒ゆ柇璇ユ《澶栦晶鍌ㄤ綅鏄惁闇�瑕佺Щ搴�
+                                bool yikuResult = YikuTask(pallet.PalletNo, url);
+                                if (!yikuResult)
+                                {
+                                    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 娓呮礂鍌ㄤ綅锛孋olumn=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 = new List<DataStockDetail>();
+                                if (houseNo == "M01")
+                                {
+                                    palletList = Db.Queryable<DataStockDetail>()
+                                         .LeftJoin<SysStorageLocat>((d, l) => d.LocatNo == l.LocatNo)
+                                        .Where(d => d.WareHouseNo == houseNo && d.PalletStatus == "3" && d.Status == "0" && areaNoList4.Contains(d.AreaNo))
+                                        .OrderBy((d, l) => l.Row)
+                                        .OrderBy((d, l) => l.Column)
+                                        .ToList();
+                                }
+                                else
+                                {
+                                    palletList = stockDetail.Where(w => w.WareHouseNo == houseNo && w.PalletStatus == "3" && w.Status == "0"
+                                    && areaNoList4.Contains(w.AreaNo)).OrderBy(o => o.UpdateTime).ToList();
+                                }
+
+                                if (palletList.Count <= 0)
+                                {
+                                    throw new Exception("鏆傛棤鑴忔《鍙垎閰�");
+                                }
+                                taskMsg = "璁惧鍙剰妗朵换鍔�";
+                                //閬嶅巻搴撳瓨鑴忔《锛岃繑鍥濵ES楠岃瘉
+                                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);//瑙f瀽杩斿洖鏁版嵁
+                                    //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("TN");//浠诲姟鍙�
+                                    var logTaskEntry2 = new LogTask
+                                    {
+                                        TaskNo = taskNo,
+                                        Sender = "WMS",
+                                        Receiver = "RCS",
+                                        //IsSuccess = 1, //鏄惁涓嬪彂鎴愬姛 0澶辫触 1鎴愬姛
+                                        SendDate = DateTime.Now,  //鍙戦�佹椂闂�
+                                                                  //BackDate = DateTime.Now,  //杩斿洖鏃堕棿
+                                        StartLocat = item.LocatNo,//璧峰浣嶇疆
+                                        EndLocat = endLoction.LocatNo,//鐩爣浣嶇疆
+                                        PalletNo = item.PalletNo,//鎵樼洏鐮�
+                                        IsSend = 1,//鏄惁鍙啀娆′笅鍙�
+                                        IsCancel = 1,//鏄惁鍙彇娑�
+                                        IsFinish = 1,//鏄惁鍙畬鎴�
+                                        Type = "1",//浠诲姟绫诲瀷 0 鍏ュ簱浠诲姟 1 鍑哄簱浠诲姟  2 绉诲簱浠诲姟
+                                        OrderType = "3",//鍗曟嵁绫诲瀷 0 鍏ュ簱 1 鍑哄簱 3 绉诲簱
+                                        Status = "0",//浠诲姟鐘舵��0锛氱瓑寰呮墽琛�1姝e湪鎵ц2鎵ц瀹屾垚
+                                        NoticeDetailNo = int.Parse(noticeNo),
+                                        Msg = taskMsg, //鍏抽敭淇℃伅
+                                        LotNo = item.LotNo//鎵规鍙�
+                                    };
+                                    //缁勭粐涓嬪彂灏忚溅浠诲姟淇℃伅
+                                    var task2 = new TaskDetial
+                                    {
+                                        Taskno = taskNo,//浠诲姟鍙�
+                                        Startport = item.LocatNo,
+                                        Endport = model.Location,//endLoction.LocatNo,
+                                        Pallno = item.PalletNo,
+                                        Crtype = "1",//鍙《
+                                        WareHouseNo = houseNo
+                                    };
+                                    //缁欎笅杞︿笅鍙戜换鍔�
+                                    logTaskEntry2.SendDate = DateTime.Now;//鍙戦�佹椂闂�
+                                    var agvResult2 = CreateTaskForAgv(task2, url, out agvMsg);
+                                    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;
+                                        logTaskEntry2.Information = agvMsg;
+                                        Db.Insertable(logTaskEntry2).ExecuteCommand();
+
+                                    }
+                                    //鎻愪氦浜嬪姟
+                                    Db.CommitTran();
+                                    return;
+                                }
+                            }
+                            break;
+                        default:
+                            throw new Exception("浠诲姟绫诲瀷鏃犳晥");
+                    }
+                    //璧峰鍌ㄤ綅鍦板潃淇℃伅
+                    var startLoction = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == pallet.LocatNo);
+                    if (startLoction == null)
+                    {
+                        throw new Exception($"璧峰鐩爣鍌ㄤ綅淇℃伅涓嶅瓨鍦紝妗跺彿锛歿pallet.LocatNo}");
+                    }
+
+                    taskNo = new Common().GetMaxNo("TN");//浠诲姟鍙�
+                                                         //浠诲姟淇℃伅
+                    var logTaskEntry = new LogTask
+                    {
+                        TaskNo = taskNo,
+                        Sender = "WMS",
+                        Receiver = "RCS",
+                        //IsSuccess = 1, //鏄惁涓嬪彂鎴愬姛 0澶辫触 1鎴愬姛
+                        SendDate = DateTime.Now,  //鍙戦�佹椂闂�
+                                                  //BackDate = DateTime.Now,  //杩斿洖鏃堕棿
+                        StartLocat = pallet.LocatNo,//璧峰浣嶇疆
+                        EndLocat = endLoction.LocatNo,//鐩爣浣嶇疆
+                        PalletNo = pallet.PalletNo,//鎵樼洏鐮�
+                        IsSend = 1,//鏄惁鍙啀娆′笅鍙�
+                        IsCancel = 1,//鏄惁鍙彇娑�
+                        IsFinish = 1,//鏄惁鍙畬鎴�
+                        Type = "1",//浠诲姟绫诲瀷 0 鍏ュ簱浠诲姟 1 鍑哄簱浠诲姟  2 绉诲簱浠诲姟
+                        OrderType = "3",//鍗曟嵁绫诲瀷 0 鍏ュ簱 1 鍑哄簱 3绉诲簱
+                        Status = "0",//浠诲姟鐘舵��0锛氱瓑寰呮墽琛�1姝e湪鎵ц2鎵ц瀹屾垚
+                        NoticeDetailNo = int.Parse(noticeNo),
+                        Msg = taskMsg, //鍏抽敭淇℃伅
+                        LotNo = pallet.LotNo//鎵规鍙�
+                    };
+
+                    //缁勭粐涓嬪彂灏忚溅浠诲姟淇℃伅
+                    var task = new TaskDetial
+                    {
+                        Taskno = taskNo,//浠诲姟鍙�
+                        Startport = pallet.LocatNo,//璧峰浣嶇疆
+                        Endport = model.Location,//endLoction.LocatNo,//鐩爣浣嶇疆
+                        Pallno = pallet.PalletNo,//妗跺彿
+                        Crtype = "1",//鍙《
+                        WareHouseNo = houseNo//杞﹂棿缂栫爜
+                    };
+                    //缁欎笅杞︿笅鍙戜换鍔�
+                    logTaskEntry.SendDate = DateTime.Now;//鍙戦�佹椂闂�
+                    var agvResult = CreateTaskForAgv(task, url, out agvMsg);
+                    if (agvResult)//鎴愬姛
+                    {
+                        //璇锋眰鎴愬姛淇敼浠诲姟琛ㄧ浉搴斿瓧娈电姸鎬�
+                        logTaskEntry.IsSuccess = 1;
+                        logTaskEntry.IsSend = 0;
+                        //logTaskEntry.IsCancel = 0;
+                        logTaskEntry.BackDate = DateTime.Now;
+                        logTaskEntry.Status = "1";//姝e湪鎵ц
+                        Db.Insertable(logTaskEntry).ExecuteCommand();
+
+                        startLoction.Status = "3";//鍑哄簱涓�
+                        Db.Updateable(startLoction).ExecuteCommand();
+
+                        endLoction.Status = "2";//鍏ュ簱涓�
+                        Db.Updateable(endLoction).ExecuteCommand();
+                    }
+                    else//澶辫触
+                    {
+                        logTaskEntry.IsSuccess = 0;
+                        logTaskEntry.Information = agvMsg;
+                        Db.Insertable(logTaskEntry).ExecuteCommand();
+
+                    }
+                    //鎻愪氦浜嬪姟
+                    Db.CommitTran();
+                }
+                catch (Exception ex)
+                {
+                    //鍥炴粴浜嬪姟
+                    Db.RollbackTran();
+                    throw new Exception(ex.Message);
+                }
+            }
+        }
+
+        /// <summary>
+        /// 鐢宠鍌ㄤ綅
+        /// </summary>
+        /// <param name="model"></param>
+        /// <param name="url"></param>
+        /// <param name="taskNo"></param>
+        /// <exception cref="Exception"></exception>
+        public void ApplyLocatNo(Pallnetmsg model, string url, out string taskNo)
+        {
+            lock (RcsLock)
+            {
+                try
+                {
+                    string taskMsg = "";
+
+                    //閫氳繃璁惧鍙锋煡鎵惧埌鎵�灞炶澶囷紙鍖哄煙锛�
+                    var deviceInfo = Db.Queryable<SysStorageArea>().First(w => w.IsDel == "0" && w.DeviceCode.Contains(model.Location));
+                    if (deviceInfo == null)
+                    {
+                        throw new Exception("璁惧淇℃伅涓嶅瓨鍦�");
+                    }
+                    //鐢宠鍌ㄤ綅璁惧鎵�鍦ㄨ溅闂寸紪鍙�
+                    var houseNo = deviceInfo.WareHouseNo;
+                    //璁惧鎵�鍦ㄥ偍浣嶄俊鎭紙璧峰鍌ㄤ綅锛�
+                    var deviceLocatInfo = new SysStorageLocat();
+                    //鍑哄叆搴撳崟鎹槑缁咺D
+                    var noticeno = "0";
+                    //鐩爣鍌ㄤ綅淇℃伅
+                    var loction = new SysStorageLocat();
+                    //妗跺簱瀛樹俊鎭�
+                    var palletInfo = new DataStockDetail();
+
+                    //寮�鍚簨鍔�
+                    Db.BeginTran();
+                    switch (model.Type)
+                    {
+                        case "3"://娓呮礂璁惧鐢宠鍌ㄤ綅锛堝噣妗剁敵璇峰偍浣嶏級
+                            {
+                                //璁惧鏀炬《鍌ㄤ綅淇℃伅锛堝師濮嬪彨妗剁洰鏍囧偍浣嶏級
+                                var deviceLocatInfo2 = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo && w.Column == 1);// Column=1 娓呮礂鍌ㄤ綅锛孋olumn=2 娓呮礂瀹屾枡鍌ㄤ綅 
+                                if (deviceLocatInfo2 == null)
+                                {
+                                    throw new Exception($"鏈煡璇㈠埌璇ヨ澶囨墍鍦ㄥ偍浣嶄俊鎭紝Location:{model.Location}");
+                                }
+                                //鏌ユ壘妗跺簱瀛樹俊鎭�
+                                palletInfo = Db.Queryable<DataStockDetail>().Where(w => w.IsDel == "0" && w.WareHouseNo == deviceLocatInfo2.WareHouseNo && w.AreaNo == deviceLocatInfo2.AreaNo && w.LocatNo == deviceLocatInfo2.LocatNo).OrderBy(o => o.UpdateTime).First();
+                                if (palletInfo == null)
+                                {
+                                    throw new Exception($"鏈煡璇㈠埌璇ヨ澶囦笂妗剁殑搴撳瓨淇℃伅");
+                                }
+                                //鏌ユ壘璇ユ《鏈夋病鏈夋鍦ㄦ墽琛岀殑浠诲姟
+                                var tasking = Db.Queryable<LogTask>().First(w => w.IsDel == "0" && w.Status == "1" && w.PalletNo == palletInfo.PalletNo);
+                                if (tasking != null)
+                                {
+                                    throw new Exception($"璇ユ《鏈夋鍦ㄦ墽琛岀殑浠诲姟锛孭alletNo:{palletInfo.PalletNo}");
+                                }
+
+                                //璁惧鎵�鍦ㄥ偍浣嶄俊鎭紙璧峰鍌ㄤ綅锛�
+                                deviceLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo && w.Column == 2);// Column=1 娓呮礂鍌ㄤ綅锛孋olumn=2 娓呮礂瀹屾枡鍌ㄤ綅 
+                                if (deviceLocatInfo == null)
+                                {
+                                    throw new Exception($"鏈煡璇㈠埌璇ヨ澶囨墍鍦ㄥ偍浣嶄俊鎭紝Location:{model.Location}");
+                                }
+                                //鏌ユ壘鍒拌杞﹂棿鍑�妗跺尯;
+                                loction = GetLocatModel(houseNo, "0");
+                                if (loction == null)
+                                {
+                                    throw new Exception("璇ヨ溅闂村噣妗跺尯鏆傛棤绌轰綑鍌ㄤ綅");
+                                }
+
+                                taskMsg = "娓呮礂璁惧锛堝噣妗讹級鐢宠鍌ㄤ綅浠诲姟";
+                            }
+                            break;
+                        case "0"://鎺ユ枡璁惧鐢宠鍌ㄤ綅锛堥娣锋枡妗剁敵璇峰偍浣嶏級
+                            {
+                                //璁惧鎵�鍦ㄥ偍浣嶄俊鎭紙璧峰鍌ㄤ綅锛�
+                                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}");
+                                }
+                                //鏌ユ壘妗跺簱瀛樹俊鎭�
+                                palletInfo = Db.Queryable<DataStockDetail>().Where(w => w.IsDel == "0" && w.WareHouseNo == deviceLocatInfo.WareHouseNo && w.AreaNo == deviceLocatInfo.AreaNo && w.LocatNo == deviceLocatInfo.LocatNo).OrderBy(o => o.UpdateTime).First();
+                                if (palletInfo == null)
+                                {
+                                    throw new Exception($"鏈煡璇㈠埌璇ヨ澶囦笂妗剁殑搴撳瓨淇℃伅");
+                                }
+                                palletInfo.LotNo = model.LotNo;//鎵规鍙�
+                                                               //鏇存柊妗跺簱瀛樹俊鎭�
+                                Db.Updateable(palletInfo).ExecuteCommand();
+
+                                //鏌ユ壘璇ユ《鏈夋病鏈夋鍦ㄦ墽琛岀殑浠诲姟
+                                var tasking = Db.Queryable<LogTask>().First(w => w.IsDel == "0" && w.Status == "1" && w.PalletNo == palletInfo.PalletNo);
+                                if (tasking != null)
+                                {
+                                    throw new Exception($"璇ユ《鏈夋鍦ㄦ墽琛岀殑浠诲姟锛孭alletNo:{palletInfo.PalletNo}");
+                                }
+
+                                //鍒ゆ柇鏄惁鏈夋壒娆″彿
+                                if (string.IsNullOrWhiteSpace(model.LotNo))
+                                {
+                                    throw new Exception("鎵规鍙蜂负绌猴紒");
+                                }
+                                //鏌ユ壘鍒拌杞﹂棿棰勬贩鍖�
+                                loction = GetLocatModel(houseNo, "1");
+                                if (loction == null)
+                                {
+                                    throw new Exception("璇ヨ溅闂撮娣峰尯鏆傛棤绌轰綑鍌ㄤ綅");
+                                }
+
+
+                                taskMsg = "鎺ユ枡璁惧锛堥娣锋枡妗讹級鐢宠鍌ㄤ綅浠诲姟";
+                            }
+                            break;
+                        case "1"://娣锋枡璁惧鐢宠鍌ㄤ綅锛堝崐鎴愬搧妗剁敵璇峰偍浣嶏級
+                            {
+                                //璁惧鎵�鍦ㄥ偍浣嶄俊鎭紙璧峰鍌ㄤ綅锛�
+                                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}");
+                                }
+                                //鏌ユ壘妗跺簱瀛樹俊鎭�
+                                palletInfo = Db.Queryable<DataStockDetail>().Where(w => w.IsDel == "0" && w.WareHouseNo == deviceLocatInfo.WareHouseNo && w.AreaNo == deviceLocatInfo.AreaNo && w.LocatNo == deviceLocatInfo.LocatNo).OrderBy(o => o.UpdateTime).First();
+                                if (palletInfo == null)
+                                {
+                                    throw new Exception($"鏈煡璇㈠埌璇ヨ澶囦笂妗剁殑搴撳瓨淇℃伅");
+                                }
+                                //鏌ユ壘璇ユ《鏈夋病鏈夋鍦ㄦ墽琛岀殑浠诲姟
+                                var tasking = Db.Queryable<LogTask>().First(w => w.IsDel == "0" && w.Status == "1" && w.PalletNo == palletInfo.PalletNo);
+                                if (tasking != null)
+                                {
+                                    throw new Exception($"璇ユ《鏈夋鍦ㄦ墽琛岀殑浠诲姟锛孭alletNo:{palletInfo.PalletNo}");
+                                }
+
+                                //鍒ゆ柇鏄惁鏈夋壒娆″彿
+                                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";//姝e湪鎵ц
+                                //鏇存柊鍏ュ簱鍗曠姸鎬�
+                                Db.Updateable(notice).ExecuteCommand();*/
+
+                                //鏌ユ壘鍒拌杞﹂棿鍗婃垚鍝佸尯
+                                loction = GetLocatModel(houseNo, "2");
+                                if (loction == null)
+                                {
+                                    throw new Exception("璇ヨ溅闂村崐鎴愬搧鍖烘殏鏃犵┖浣欏偍浣�");
+                                }
+
+                                taskMsg = "鎺ユ枡璁惧锛堟贩鏂欐《锛夌敵璇峰偍浣嶄换鍔�";
+                            }
+                            break;
+                        case "2"://涓嬫枡璁惧鐢宠鍌ㄤ綅锛堣剰妗剁敵璇峰偍浣嶏級
+                            {
+                                //璁惧鏀炬《鍌ㄤ綅淇℃伅锛堝師濮嬪彨妗剁洰鏍囧偍浣嶏級
+                                var deviceLocatInfo2 = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo && w.Column == 1);// Column=1 鎶曟枡鍌ㄤ綅锛孋olumn=2 鎶曞畬鏂欏偍浣�
+                                if (deviceLocatInfo2 == null)
+                                {
+                                    throw new Exception($"鏈煡璇㈠埌璇ヨ澶囨墍鍦ㄥ偍浣嶄俊鎭紝Location:{model.Location}");
+                                }
+                                //鏌ユ壘妗跺簱瀛樹俊鎭�
+                                palletInfo = Db.Queryable<DataStockDetail>().Where(w => w.IsDel == "0" && w.WareHouseNo == deviceLocatInfo2.WareHouseNo && w.AreaNo == deviceLocatInfo2.AreaNo && w.LocatNo == deviceLocatInfo2.LocatNo).OrderBy(o => o.UpdateTime).First();
+                                if (palletInfo == null)
+                                {
+                                    throw new Exception($"鏈煡璇㈠埌璇ヨ澶囦笂妗剁殑搴撳瓨淇℃伅");
+                                }
+                                //鏌ユ壘璇ユ《鏈夋病鏈夋鍦ㄦ墽琛岀殑浠诲姟
+                                var tasking = Db.Queryable<LogTask>().First(w => w.IsDel == "0" && w.Status == "1" && w.PalletNo == palletInfo.PalletNo);
+                                if (tasking != null)
+                                {
+                                    throw new Exception($"璇ユ《鏈夋鍦ㄦ墽琛岀殑浠诲姟锛孭alletNo:{palletInfo.PalletNo}");
+                                }
+                                if (houseNo == "M04")//闄や簡鍠峰共杞﹂棿涓嬫枡鍙i兘鏄�2涓偍浣嶏紙鎶曟枡鍌ㄤ綅銆佹姇瀹屾枡鍌ㄤ綅锛�
+                                {
+                                    //璁惧鎵�鍦ㄥ偍浣嶄俊鎭紙璧峰鍌ㄤ綅锛�
+                                    deviceLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo && w.Column == 1);
+                                }
+                                else
+                                {
+                                    //璁惧鎵�鍦ㄥ偍浣嶄俊鎭紙璧峰鍌ㄤ綅锛�
+                                    deviceLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo && w.Column == 2);//Column=1 鎶曟枡鍌ㄤ綅锛孋olumn=2 鎶曞畬鏂欏偍浣�           
+                                }
+                                if (deviceLocatInfo == null)
+                                {
+                                    throw new Exception($"鏈煡璇㈠埌璇ヨ澶囨墍鍦ㄥ偍浣嶄俊鎭紝Location:{model.Location}");
+                                }
+                                //鏌ユ壘鍒拌杞﹂棿鑴忔《鍖�
+                                loction = GetLocatModel(houseNo, "3");
+                                if (loction == null)
+                                {
+                                    throw new Exception("璇ヨ溅闂磋剰妗跺尯鏆傛棤绌轰綑鍌ㄤ綅");
+                                }
+
+                                taskMsg = "涓嬫枡璁惧锛堣剰妗讹級鐢宠鍌ㄤ綅浠诲姟";
+                            }
+                            break;
+                    }
+
+                    taskNo = new Common().GetMaxNo("TN");//浠诲姟鍙�
+                                                         //浠诲姟淇℃伅
+                    var logTaskEntry = new LogTask
+                    {
+                        TaskNo = taskNo,
+                        Sender = "WMS",
+                        Receiver = "RCS",
+                        //IsSuccess = 1, //鏄惁涓嬪彂鎴愬姛 0澶辫触 1鎴愬姛
+                        SendDate = DateTime.Now,  //鍙戦�佹椂闂�
+                                                  //BackDate = DateTime.Now,  //杩斿洖鏃堕棿
+                        StartLocat = deviceLocatInfo.LocatNo,//璧峰浣嶇疆
+                        EndLocat = loction.LocatNo,//鐩爣浣嶇疆
+                        PalletNo = palletInfo.PalletNo,//妗跺彿
+                        IsSend = 1,//鏄惁鍙啀娆′笅鍙�
+                        IsCancel = 1,//鏄惁鍙彇娑�
+                        IsFinish = 1,//鏄惁鍙畬鎴�
+                        Type = "0",//浠诲姟绫诲瀷 0 鍏ュ簱浠诲姟 1 鍑哄簱浠诲姟  2 绉诲簱浠诲姟
+                        OrderType = "3",//鍗曟嵁绫诲瀷 0 鍏ュ簱 1 鍑哄簱 3 绉诲簱
+                        Status = "0",//浠诲姟鐘舵��0锛氱瓑寰呮墽琛�1姝e湪鎵ц2鎵ц瀹屾垚
+                        NoticeDetailNo = int.Parse(noticeno),
+                        Msg = taskMsg, //鍏抽敭淇℃伅
+                        LotNo = palletInfo.LotNo//鎵规鍙�
+                    };
+                    var task = new TaskDetial
+                    {
+                        Taskno = taskNo,//浠诲姟鍙�
+                        Startport = model.Location,//deviceLocatInfo.LocatNo,//璧峰浣嶇疆
+                        Endport = loction.LocatNo,//鐩爣浣嶇疆
+                        Pallno = palletInfo.PalletNo,
+                        Crtype = "0",//鍏ュ簱
+                        WareHouseNo = houseNo
+                    };
+                    string agvMsg = string.Empty;
+                    //缁欎笅杞︿笅鍙戜换鍔�
+                    logTaskEntry.SendDate = DateTime.Now;//鍙戦�佹椂闂�
+                    var agvResult = CreateTaskForAgv(task, url, out agvMsg);
+                    if (agvResult)//鎴愬姛
+                    {
+                        //璇锋眰鎴愬姛淇敼浠诲姟琛ㄧ浉搴斿瓧娈电姸鎬�
+                        logTaskEntry.IsSuccess = 1;
+                        logTaskEntry.IsSend = 0;
+                        //logTaskEntry.IsCancel = 0;
+                        logTaskEntry.BackDate = DateTime.Now;
+                        logTaskEntry.Status = "1";//姝e湪鎵ц
+                        Db.Insertable(logTaskEntry).ExecuteCommand();
+
+                        deviceLocatInfo.Status = "3";//鍑哄簱涓�
+                        Db.Updateable(deviceLocatInfo).ExecuteCommand();
+
+                        loction.Status = "2";//鍏ュ簱涓�
+                        Db.Updateable(loction).ExecuteCommand();
+                    }
+                    else//澶辫触
+                    {
+                        logTaskEntry.IsSuccess = 0;
+                        logTaskEntry.Information = agvMsg;
+                        Db.Insertable(logTaskEntry).ExecuteCommand();
+                    }
+                    //鎻愪氦浜嬪姟
+                    Db.CommitTran();
+                }
+                catch (Exception ex)
+                {
+                    //鍥炴粴浜嬪姟
+                    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
             {
-                if (string.IsNullOrEmpty(model.Location))
-                {
-                    throw new Exception("璁惧鎵�鍦ㄤ綅缃笉鍙负绌�");
-                }
-                var locatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == model.Location);
-                if (locatInfo == null)
-                {
-                    throw new Exception("璁惧鍌ㄤ綅淇℃伅涓嶅瓨鍦�");
-                }
-                var house = locatInfo.WareHouseNo;//鍙《浣嶇疆锛屽悗缁牴鎹綅缃叧鑱旀垨瀹氫箟锛屾潵鍒ゆ柇鍙《浣嶇疆鎵�灞炶溅闂�
-                var urlMes = "";//鍥炰紶MES鐨勬帴鍙e湴鍧�
-                var noticeno = "0";//鍑哄叆搴撳崟鎹槑缁咺D
-                var sql = "select PalletNo,LocatNo from DataStockDetail where Status = '0'";
-                switch (model.Type)
-                {
-                    case "0"://鍙噣妗�
-                        sql += $"and WareHouseNo = '{house}' and (AreaNo like '%01' or AreaNo like '%11' or AreaNo like '%21' or AreaNo like '%31') and PalletStatus = '0' order by CreateTime desc";
-                        pallet = Db.Ado.SqlQuery<DataStockDetail>(sql).FirstOrDefault();
-                        if (pallet == null)
-                        {
-                            throw new Exception("鏆傛棤鍑�妗跺彲鍒嗛厤");
-                        }
-
-                        break;
-                    case "1"://鍙枡妗讹紙娣锋枡锛�
-                        //鍒ゆ柇鏄惁鏈夋壒娆″彿
-                        if (string.IsNullOrWhiteSpace(model.LotNo))
-                        {
-                            throw new Exception("鎵规鍙蜂负绌猴紒");
-                        }
-                        /*BllArrivalNoticeDetail ArriveMes = new BllArrivalNoticeDetail();
-                        //鍒ゆ柇璇ユ壒娆℃槸鍚︽湁瀵瑰簲鍏ュ簱鍗�
-                        sql = $"select * from BllArrivalNoticeDetail where LotNo =  '{pallnetmsg.LotNo}' order by CreateTime desc";
-                        ArriveMes = Db.Ado.SqlQuery<BllArrivalNoticeDetail>(sql).FirstOrDefault();
-                        if (ArriveMes == null)
-                        {
-                            throw new Exception("璇ユ壒娆℃病鏈夊搴旂殑鍏ュ簱鍗�");
-                        }
-                        noticeno = ArriveMes.Id.ToString();*/
-                        //鏌ユ壘搴撳瓨涓槸鍚︽湁鍙敤鐨勬鎵规鐨勬贩鏂欐《
-                        sql= $@"select PalletNo,LocatNo from DataStockDetail where LotNo = '{model.LotNo}'and  (AreaNo like '%02' or AreaNo like '%12' or AreaNo like '%22' or AreaNo like '%32')
-                                   and WareHouseNo = '{house}' and status = '0' and PalletStatus = '1' order by CompleteTime desc";
-                        pallet = Db.Ado.SqlQuery<DataStockDetail>(sql).FirstOrDefault();
-                        if (pallet == null)
-                        {
-                            throw new Exception("鏆傛棤娣锋枡妗跺彲鍒嗛厤");
-                        }
-
-                        break;
-                    case "2"://鍙枡妗讹紙涓嬫枡锛�
-                        BllExportNoticeDetail bllSo = new BllExportNoticeDetail();
-                        //鍒ゆ柇鏄惁鏈夋壒娆″彿
-                        if (string.IsNullOrWhiteSpace(model.LotNo))
-                        {
-                            throw new Exception("鎵规鍙蜂负绌猴紒");
-                        }
-                        //鏌ユ壘鏄惁鏈夋鎵规鍑哄簱鍗�
-                        sql = $"select * from BllExportNoticeDetail where LotNo =  '{model.LotNo}' order by CreateTime desc";
-                        bllSo = Db.Ado.SqlQuery<BllExportNoticeDetail>(sql).FirstOrDefault();
-                        if (bllSo == null)
-                        {
-                            throw new Exception("璇ユ壒娆℃病鏈夊搴旂殑鍑哄簱鍗�");
-                        }
-                        noticeno = bllSo.Id.ToString();
-                        //鏌ユ壘搴撳瓨涓槸鍚︽湁姝ゆ壒娆$殑鍚堟牸涓嬫枡妗�
-                        sql = $@"select PalletNo,LocatNo from DataStockDetail where LotNo = '{model.LotNo}'and (AreaNo like '%03' or AreaNo like '%13' or AreaNo like '%23' or AreaNo like '%33')
-                                    and WareHouseNo = '{house}' and status = '0' and PalletStatus = '2' and InspectStatus = '1' order by CompleteTime desc";
-                        pallet = Db.Ado.SqlQuery<DataStockDetail>(sql).FirstOrDefault();
-                        if (pallet == null)
-                        {
-                            throw new Exception("鏆傛棤涓嬫枡妗跺彲鍒嗛厤");
-                        }
-
-                        break;
-                    case "3"://鍙剰妗�
-                        sql += $"and WareHouseNo = '{house}' and  (AreaNo like '%04' or AreaNo like '%14' or AreaNo like '%24' or AreaNo like '%34') and PalletStatus='3' order by CreateTime desc";
-                        List<DataStockDetail> palletList = Db.Ado.SqlQuery<DataStockDetail>(sql).ToList();
-                        if (palletList.Count <= 0)
-                        {
-                            throw new Exception("鏆傛棤鑴忔《鍙垎閰�");
-                        }
-                        //閬嶅巻搴撳瓨鑴忔《锛岃繑鍥濵ES楠岃瘉
-                        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);//瑙f瀽杩斿洖鏁版嵁
-                            //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 task2 = new TaskDetial
-                            {
-                                Startport = item.LocatNo,
-                                Endport = model.Location,
-                                Pallno = item.PalletNo,
-                                Type = model.Type,
-                                Crtype = "1",
-                                Noticedetailno = int.Parse(noticeno),
-                            };
-                            CreateLotTask(task2, url);
-                            return;
-                        }
-                        break;
-                    default:
-                        throw new Exception("浠诲姟绫诲瀷鏃犳晥");
-                }
-                //涓嬪彂灏忚溅浠诲姟
-                var task = new TaskDetial
-                {
-                    Startport = pallet.LocatNo,//璧峰浣嶇疆
-                    Endport = model.Location,//鐩爣浣嶇疆
-                    Pallno = pallet.PalletNo,//妗跺彿
-                    Type = model.Type,//妗剁被鍨�
-                    Crtype = "1",
-                    Noticedetailno = int.Parse(noticeno),
-                    LotNo = model.LotNo,
-                };
-                CreateLotTask(task, url);
-                return;
+                //閫氳繃绫诲埆鏌ユ壘杞﹂棿鍖哄煙
+                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)
             {
-                Db.Ado.RollbackTran();
                 throw new Exception(ex.Message);
             }
         }
+
 
         /// <summary>
         /// MES涓嬪彂灏嗗噣妗舵敼涓鸿剰妗跺苟鎷夊埌鑴忔《鍖�
@@ -228,11 +790,11 @@
                         Startport = detail.LocatNo,
                         Endport = endLocat.LocatNo,
                         Pallno = PalletNo,
-                        Type = "3",
                         Crtype = "2",
-                        Noticedetailno = 0,
                     };
-                    CreateLotTask(ztask,url);
+                    string taskNo = "";
+                    string agvMsg = string.Empty;
+                    CreateTaskForAgv(ztask, url, out agvMsg);
 
                     statrtLocat.Status = "3";//鍑哄簱涓�
                     //淇敼璧峰鍌ㄤ綅鐘舵��
@@ -271,12 +833,11 @@
                     Startport = detail.LocatNo,
                     Endport = pallnetmsg.Location,
                     Pallno = detail.PalletNo,
-                    Type = "3",
                     Crtype = "1",
-                    Noticedetailno = 0,
-                    LotNo = "",
                 };
-                CreateLotTask(task,url);
+                string taskNo = "";
+                string agvMsg = string.Empty;
+                CreateTaskForAgv(task, url, out agvMsg);
             }
             catch (Exception ex)
             {
@@ -285,103 +846,363 @@
         }
 
         /// <summary>
-        /// 鐢宠鍌ㄤ綅
+        /// 浠诲姟瀹屾垚
         /// </summary>
-        /// <param name="PalletNo"></param>
-        /// <returns></returns>
-        public void ApplyLocatNo(Pallnetmsg model,string url)
+        /// <param name="TaskNo"></param>
+        /// <param name="Status"></param>
+        /// <exception cref="Exception"></exception>
+        public void RCSFinishTask(string taskNo, string status, string comeFrom, int userId = 0)
         {
-            var sql = $"select LocatNo from SysStorageLocat where status = '0'";
-            var sql2 = "";
-            var houseNo = "";//鐢宠鍌ㄤ綅璁惧鎵�鍦ㄨ溅闂寸紪鍙�
-            var noticeno = "0";//鍑哄叆搴撳崟鎹槑缁咺D
-            SysStorageLocat loction = new SysStorageLocat();//鐩爣鍌ㄤ綅淇℃伅
-            BllArrivalNoticeDetail noticeDetail = new BllArrivalNoticeDetail();//鍏ュ簱鍗曟槑缁�
             try
             {
-                //璁惧鎵�鍦ㄥ偍浣嶄俊鎭�
-                var deviceLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == model.LotNo);
-                if (deviceLocatInfo == null)
+                #region 鏉′欢鍒ゆ柇
+                var resultModel = new ErpModel() { Success = -1, Message = "" };
+                if (string.IsNullOrEmpty(taskNo))
                 {
-                    throw new Exception($"鏈煡璇㈠埌璇ヨ澶囨墍鍦ㄥ偍浣嶄俊鎭紝Location:{model.Location}");
+                    throw new Exception("浠诲姟鍙蜂笉鍙负绌�");
                 }
-                houseNo = deviceLocatInfo.WareHouseNo;//璁惧鎵�灞炶溅闂�
-                switch (model.Type)
+                //浠诲姟淇℃伅
+                var taskInfo = Db.Queryable<LogTask>().First(w => w.TaskNo == taskNo);
+                if (taskInfo == null)
                 {
-                    case "3"://娓呮礂璁惧鐢宠鍌ㄤ綅锛堝噣妗剁敵璇峰偍浣嶏級
-                        sql += $" and (AreaNo like '%01' or AreaNo like '%11' or AreaNo like '%21' or AreaNo like '%31') and WareHouseNo = '{houseNo}'";
-                        loction = Db.Ado.SqlQuery<SysStorageLocat>(sql).OrderByDescending(a => a.CreateTime).FirstOrDefault();
-                        if (loction == null)
-                        {
-                            throw new Exception("璇ヨ溅闂村噣妗跺尯鏆傛棤绌轰綑鍌ㄤ綅");
-                        }
-                        break;
-                    case "0"://鎺ユ枡璁惧鐢宠鍌ㄤ綅锛堟贩鏂欐《鐢宠鍌ㄤ綅锛�
-                        //鍒ゆ柇鏄惁鏈夋壒娆″彿
-                        if (string.IsNullOrWhiteSpace(model.LotNo))
-                        {
-                            throw new Exception("鎵规鍙蜂负绌猴紒");
-                        }
-                        //鏍¢獙姝ゆ壒娆℃槸鍚︽湁鍏ュ簱鍗�
-                        /*sql2 = $"select * from BllArrivalNoticeDetail where LotNo =  '{pallnetmsg.LotNo}' order by CreateTime desc";
-                        noticeDetail = Db.Ado.SqlQuery<BllArrivalNoticeDetail>(sql).FirstOrDefault();
-                        if (noticeDetail == null)
-                        {
-                            throw new Exception("璇ユ壒娆℃病鏈夊搴旂殑鍏ュ簱鍗�");
-                        }
-                        noticeno = noticeDetail.Id.ToString();*/
-                        sql += $" and (AreaNo like '%02' or AreaNo like '%12' or AreaNo like '%22' or AreaNo like '%32') and WareHouseNo = '{houseNo}'";
-                        loction = Db.Ado.SqlQuery<SysStorageLocat>(sql).OrderByDescending(a => a.CreateTime).FirstOrDefault();
-                        if (loction == null)
-                        {
-                            throw new Exception("璇ヨ溅闂撮娣峰尯鏆傛棤绌轰綑鍌ㄤ綅");
-                        }
-                        break;
-                    case "1"://娣锋枡璁惧鐢宠鍌ㄤ綅锛堝崐鎴愬搧妗剁敵璇峰偍浣嶏級
-                        //鍒ゆ柇鏄惁鏈夋壒娆″彿
-                        if (string.IsNullOrWhiteSpace(model.LotNo))
-                        {
-                            throw new Exception("鎵规鍙蜂负绌猴紒");
-                        }
-                        //鏍¢獙姝ゆ壒娆℃槸鍚︽湁鍏ュ簱鍗�
-                        sql2 = $"select * from BllArrivalNoticeDetail where LotNo =  '{model.LotNo}' order by CreateTime desc";
-                        noticeDetail = Db.Ado.SqlQuery<BllArrivalNoticeDetail>(sql2).FirstOrDefault();
-                        if (noticeDetail == null)
-                        {
-                            throw new Exception("璇ユ壒娆℃病鏈夊搴旂殑鍏ュ簱鍗�");
-                        }
-                        noticeno = noticeDetail.Id.ToString();
-                        sql += $" and (AreaNo like '%03' or AreaNo like '%13' or AreaNo like '%23' or AreaNo like '%33') and WareHouseNo = '{houseNo}'";
-                        loction = Db.Ado.SqlQuery<SysStorageLocat>(sql).OrderByDescending(a => a.CreateTime).FirstOrDefault();
-                        if (loction == null)
-                        {
-                            throw new Exception("璇ヨ溅闂村崐鎴愬搧鍖烘殏鏃犵┖浣欏偍浣�");
-                        }
-                        break;
-                    case "2"://涓嬫枡璁惧鐢宠鍌ㄤ綅锛堣剰妗剁敵璇峰偍浣嶏級
-                        sql += $" and (AreaNo like '%04' or AreaNo like '%14' or AreaNo like '%24' or AreaNo like '%34') and WareHouseNo = '{houseNo}'";
-                        loction = Db.Ado.SqlQuery<SysStorageLocat>(sql).OrderByDescending(a => a.CreateTime).FirstOrDefault();
-                        if (loction == null)
-                        {
-                            throw new Exception("璇ヨ溅闂磋剰妗跺尯鏆傛棤绌轰綑鍌ㄤ綅");
-                        }
-                        break;
+                    throw new Exception($"浠诲姟鍙蜂负:{taskNo}鐨勪换鍔′笉瀛樺湪!");
                 }
-                var task = new TaskDetial
+                if (taskInfo.Status != "1" && comeFrom != "WMS")
                 {
-                    Startport = model.Location,
-                    Endport = loction.LocatNo,
-                    Pallno = model.PalletNo,
-                    Type = model.Type,
-                    Crtype = "0",
-                    Noticedetailno = int.Parse(noticeno),
-                    LotNo = model.LotNo,
-                };
-                CreateLotTask(task,url);
+                    throw new Exception($"浠诲姟鍙蜂负:{taskNo}鐨勪换鍔$姸鎬佸紓甯�");
+                }
+                #endregion
+                //寮�鍚簨鍔�
+                Db.BeginTran();
+                var comTime = DateTime.Now;
+
+                if (status == "0")
+                {
+                    taskInfo.Status = "3";//寮傚父缁撴潫
+                    //淇敼浠诲姟鐘舵��
+                    Db.Updateable(taskInfo).ExecuteCommand();
+
+                    resultModel.Success = 0;
+                    resultModel.Message = "鎴愬姛";
+
+                    //鎻愪氦浜嬪姟
+                    Db.CommitTran();
+
+                    return;
+                }
+                //搴撳瓨鏄庣粏
+                var stockDetail = Db.Queryable<DataStockDetail>().First(w => w.PalletNo == taskInfo.PalletNo);
+                if (stockDetail == null)
+                {
+                    throw new Exception($"妗跺簱瀛樹俊鎭笉瀛樺湪");
+                }
+                //璧峰鍌ㄤ綅淇℃伅
+                var startLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.LocatNo == taskInfo.StartLocat && w.IsDel == "0");
+                if (startLocatInfo == null)
+                {
+                    throw new Exception($"璧峰鍌ㄤ綅淇℃伅涓嶅瓨鍦�");
+                }
+                startLocatInfo.Status = "0";//绌哄偍浣�
+                //淇敼璧峰鍌ㄤ綅鐘舵��
+                Db.Updateable(startLocatInfo).ExecuteCommand();
+
+                //鐩爣鍌ㄤ綅淇℃伅
+                var endLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.LocatNo == taskInfo.EndLocat && w.IsDel == "0");
+                if (endLocatInfo == null)
+                {
+                    throw new Exception($"鐩爣鍌ㄤ綅淇℃伅涓嶅瓨鍦�");
+                }
+                endLocatInfo.Status = "1";//鏈夌墿鍝�
+                //淇敼鐩爣鍌ㄤ綅鐘舵��
+                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.UpdateTime = DateTime.Now;//鏇存柊鏃堕棿
+                if (endAreaInfo.Type == "0")//娲佸噣鍖�
+                {
+                    stockDetail.PalletStatus = "0";
+                    stockDetail.Status = "0";//寰呭垎閰�
+                }
+                else if (endAreaInfo.Type == "1")//棰勬贩鍖�
+                {
+                    stockDetail.PalletStatus = "1";
+                    stockDetail.Status = "0";//寰呭垎閰�
+                }
+                else if (endAreaInfo.Type == "2")//鍗婃垚鍝佸尯
+                {
+                    stockDetail.PalletStatus = "2";
+                    stockDetail.Status = "0";//寰呭垎閰�
+                }
+                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)
+                {
+                    //鍏ュ簱鍗曟槑缁�
+                    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 noticeDetail = Db.Queryable<BllExportNoticeDetail>().First(w => w.IsDel == "0" && w.Id==taskInfo.NoticeDetailNo);
+                    if (noticeDetail != null)
+                    {
+                        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
+                //鍥炰紶妗剁姸鎬佺粰MES
+                //BackPalletStatus(taskInfo.PalletNo, sd3.PalletStatus);
+                #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();
             }
             catch (Exception ex)
             {
+                //鍥炴粴浜嬪姟
+                Db.RollbackTran();
                 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,
+                };
+                string agvMsg = string.Empty;
+                //缁欎笅杞︿笅鍙戜换鍔�
+                logTaskEntry.SendDate = DateTime.Now;//鍙戦�佹椂闂�
+                var agvResult = CreateTaskForAgv(task, url, out agvMsg);
+                if (agvResult)//鎴愬姛
+                {
+                    //璇锋眰鎴愬姛淇敼浠诲姟琛ㄧ浉搴斿瓧娈电姸鎬�
+                    logTaskEntry.IsSuccess = 1;
+                    logTaskEntry.IsSend = 0;
+                    logTaskEntry.IsCancel = 0;
+                    logTaskEntry.BackDate = DateTime.Now;
+                    logTaskEntry.Status = "1";//姝e湪鎵ц
+                    Db.Insertable(logTaskEntry).ExecuteCommand();
+
+                    startLocatInfo.Status = "3";//鍑哄簱涓�
+                    Db.Updateable(startLocatInfo).ExecuteCommand();
+
+                    endLocatInfo.Status = "2";//鍏ュ簱涓�
+                    Db.Updateable(endLocatInfo).ExecuteCommand();
+                }
+                else//澶辫触
+                {
+                    logTaskEntry.IsSuccess = 0;
+                    logTaskEntry.Information = agvMsg;
+                    Db.Insertable(logTaskEntry).ExecuteCommand();
+                }
+
+                //娣诲姞鎿嶄綔鏃ュ織璁板綍
+                var k = new OperationCrServer().AddLogOperationCr("搴撳唴浣滀笟", "搴撳唴鏃ュ織", taskNo, "涓嬪彂", $"鐐瑰嚮涓嬪彂鎸夐挳銆佷换鍔″彿涓猴細{taskNo}鐨勪换鍔�", userId);
+                Db.CommitTran();
+            }
+            catch (Exception e)
+            {
+                Db.RollbackTran();
+                throw new Exception(e.Message);
             }
         }
 
@@ -390,187 +1211,267 @@
         /// </summary>
         /// <param name="req"></param>
         /// <returns></returns>
-        public void CreateLotTask(TaskDetial taskDetial,string url)
+        public bool CreateTaskForAgv(TaskDetial taskDetial, string url, out string agvMsg, string priority=null)
+        {
+            bool result = false;
+
+            #region 鍛煎彨灏忚溅浠g爜
+            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;//鐩爣浣嶇疆
+            agvTask.priority = priority;//浼樺厛绾�
+            //鍒ゆ柇瀹瑰櫒绫诲瀷
+            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";
+            }
+
+            // 姝e紡杩愯绋嬪簭鏀惧紑
+            var jsonData = JsonConvert.SerializeObject(agvTask);
+            string response = HttpHelper.DoPost(url, jsonData, "涓嬪彂缁橝GV杞繍鍛戒护", "AGV");
+            //瑙f瀽杩斿洖鏁版嵁 
+            var agvModel = JsonConvert.DeserializeObject<OutCommanAgvDto>(response);
+            if (agvModel.Code == "0")
+            {
+                result = true;//缁欎笅杞︿笅鍙戜换鍔℃垚鍔�
+
+                agvMsg = "";
+            }
+            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);
+
+                agvMsg = agvModel.Message;
+            }
+            return result;
+            #endregion
+        }
+
+        /// <summary>
+        /// 鍒嗛厤妗跺悗鍒ゆ柇璇ユ《澶栦晶鍌ㄤ綅鏄惁闇�瑕佺Щ搴�
+        /// </summary>
+        /// <param name="palletNo"></param>
+        /// <param name="url"></param>
+        /// <returns></returns>
+        /// <exception cref="Exception"></exception>
+        private bool YikuTask(string palletNo,string url)
         {
             try
             {
-                //寮�鍚簨鍔�
-                Db.BeginTran();
-                var typeDesc = "";
-                switch (taskDetial.Crtype)
+                //鍒嗛厤妗剁殑搴撳瓨淇℃伅
+                var palletInfo = Db.Queryable<DataStockDetail>().First(w => w.IsDel == "0" && w.PalletNo == palletNo);
+                if (palletInfo == null)
                 {
-                    case "0":
-                        typeDesc = "鍏ュ簱";
-                        break;
-                    case "1":
-                        typeDesc = "鍑哄簱";
-                        break;
-                    case "2":
-                        typeDesc = "绉诲簱";
-                        break;
+                    throw new Exception("鏈煡璇㈠埌鍒嗛厤妗剁殑搴撳瓨淇℃伅");
                 }
-                var msg = "";
-                switch (taskDetial.Type)
+                //鍒ゆ柇鍒嗛厤鐨勬《鏈夋病鏈夋鍦ㄦ墽琛岀殑浠诲姟锛堢Щ搴撲腑锛�
+                var taskIng= Db.Queryable<LogTask>().First(w => w.IsDel == "0" && (w.Status == "0" || w.Status == "1") && w.PalletNo == palletNo);
+                if (taskIng != null)
                 {
-                    case "0":
-                        msg = "鍑�妗�"+ typeDesc + "浠诲姟";
-                        break;
-                    case "1":
-                        msg = "棰勬贩鏂欐《" + typeDesc + "浠诲姟";
-                        break;
-                    case "2":
-                        msg = "鍗婃垚鍝佹枡妗�" + typeDesc + "浠诲姟";
-                        break;
-                    case "3":
-                        msg = "鑴忔《" + typeDesc + "浠诲姟";
-                        break;
+                    throw new Exception("鍒嗛厤鐨勬《鏈夋鍦ㄦ墽琛岀殑浠诲姟锛岃绋嶅悗鍐嶈瘯");
                 }
-                var sql = "";
-                var sql2 = "";
-                var ordertype = "3";
-                //娑夊強鍑哄叆搴撳崟鎹殑鐘舵�佷俊鎭敼鍙�
-                if (taskDetial.Type == "2" && taskDetial.Crtype =="1")//鍗婃垚鍝佸嚭搴�
+                //鍒嗛厤妗剁殑鍌ㄤ綅淇℃伅
+                var locatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == palletInfo.LocatNo);
+                if (locatInfo == null)
                 {
-                    ordertype = "1";
-                    sql = $"UPDATE BllExportNotice SET Status ='1' " +
-                        $"where SoNO = (select SoNO from BllExportNoticeDetail where id = {taskDetial.Noticedetailno})";
-                    Db.Ado.ExecuteCommand(sql);
-                }
-                else if(taskDetial.Type =="2" && taskDetial.Crtype == "0")//鍗婃垚鍝佸叆搴�
-                {
-                    ordertype = "0";
-                    var ArrivalMsg = Db.Queryable<BllArrivalNoticeDetail>().First(it => it.Id == taskDetial.Noticedetailno);
-                    //淇敼鍏ュ簱鍗曟�昏〃涓崟鎹姸鎬佷负姝e湪鎵ц
-                    sql = $"UPDATE BllArrivalNotice SET Status ='1' where asnno = '{ArrivalMsg.ASNNo}'";
-                    Db.Ado.ExecuteCommand(sql);
-                    //搴撳瓨涓坊鍔犳鎵樼洏鐗╂枡缂栫爜鍜岀紪鐮佸悕绉�
-                    sql2 = $"UPDATE DataStockDetail SET SkuNo = '{ArrivalMsg.SkuNo}',SkuName = '{ArrivalMsg.SkuName}' " +
-                        $"where PalletNo = '{taskDetial.Pallno}'and LotNo = '{taskDetial.LotNo}'";
-                    Db.Ado.ExecuteCommand(sql2);
-                }
-                else if (taskDetial.Type == "1" && taskDetial.Crtype == "0")//棰勬贩妗跺叆搴�
-                {
-                    //棰勬贩妗跺叆搴撴椂娣诲姞鍒板簱瀛樹腑姝ゆ墭鐩樼殑鎵规淇℃伅
-                    sql2 = $"UPDATE DataStockDetail SET LotNo =  '{taskDetial.LotNo}' where PalletNo = '{taskDetial.Pallno}'";
-                    Db.Ado.ExecuteCommand(sql2);
+                    throw new Exception("鏈煡璇㈠埌鍒嗛厤妗舵墍鍦ㄧ殑鍌ㄤ綅淇℃伅");
                 }
                 
-                //鍒ゆ柇浠诲姟鏄惁涓烘柊浠诲姟
-                if (string.IsNullOrWhiteSpace(taskDetial.Taskno))
+                //鍒ゆ柇璇ュ偍浣嶆槸鍚︽槸鍐呬晶鍌ㄤ綅
+                if (!string.IsNullOrEmpty(locatInfo.AisleOne))
                 {
-                    taskDetial.Taskno = new Common().GetMaxNo("TK");
-                }
-                var logTaskEntry = new LogTask    
-                {
-                    TaskNo = taskDetial.Taskno,
-                    Sender = "WMS",
-                    Receiver = "RCS",
-                    //IsSuccess = 1, //鏄惁涓嬪彂鎴愬姛 0澶辫触 1鎴愬姛
-                    SendDate = DateTime.Now,  //鍙戦�佹椂闂�
-                    //BackDate = DateTime.Now,  //杩斿洖鏃堕棿
-                    StartLocat = taskDetial.Startport,//璧峰浣嶇疆
-                    EndLocat = taskDetial.Endport,//鐩爣浣嶇疆
-                    PalletNo = taskDetial.Pallno,//鎵樼洏鐮�
-                    IsSend = 1,//鏄惁鍙啀娆′笅鍙�
-                    IsCancel = 1,//鏄惁鍙彇娑�
-                    IsFinish = 1,//鏄惁鍙畬鎴�
-                    Type = taskDetial.Crtype,//浠诲姟绫诲瀷 0 鍏ュ簱浠诲姟 1 鍑哄簱浠诲姟  2 绉诲簱浠诲姟
-                    OrderType = ordertype,//鍗曟嵁绫诲瀷 0 鍏ュ簱 1 鍑哄簱 3鍏朵粬
-                    Status = "0",//浠诲姟鐘舵��0锛氱瓑寰呮墽琛�1姝e湪鎵ц2鎵ц瀹屾垚
-                    NoticeDetailNo = int.Parse(taskDetial.Noticedetailno.ToString()),
-                    Msg = msg, //鍏抽敭淇℃伅
-                };
-
-                #region 鍛煎彨灏忚溅浠g爜
-                List<AgvSchedulingTask> agvTaskList = new List<AgvSchedulingTask>();
-
-                List<PositionCodePath> pahtList = new List<PositionCodePath>();
-                //璧峰浣嶇疆
-                PositionCodePath path1 = new PositionCodePath();
-                path1.positionCode = taskDetial.Startport;
-                path1.type = "05";
-
-                pahtList.Add(path1);
-
-                //鐩爣浣嶇疆
-                PositionCodePath path2 = new PositionCodePath();
-                path2.positionCode = taskDetial.Endport;
-                path2.type = "00";
-
-                pahtList.Add(path2);
-                //涓嬭溅浠诲姟鍗�
-                AgvSchedulingTask agvTask = new AgvSchedulingTask();
-                agvTask.reqCode = logTaskEntry.TaskNo;//浠诲姟鍙�
-                agvTask.ctnrTyp = "3";
-                agvTask.reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//璇锋眰鏃堕棿
-                agvTask.taskTyp = "Z2";//浠诲姟绫诲瀷 绾胯竟鍒版墭鐩樻敹闆嗗櫒 Z1锛� 鎵樼洏鍨涚敵璇峰叆搴� Z2
-                agvTask.wbCode = "";
-                agvTask.positionCodePath = pahtList;//灏忚溅璺緞
-                agvTask.podCode = "";
-                agvTask.userCallCode = taskDetial.Endport;//鐩爣浣嶇疆
-
-                agvTaskList.Add(agvTask);
-
-                // 姝e紡杩愯绋嬪簭鏀惧紑
-                string str = "";
-                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 = "";
-
-                try
-                {
-                    logTaskEntry.SendDate = DateTime.Now;//鍙戦�佹椂闂�
-
-                    response = HttpHelper.DoPost(url, jsonData, "涓嬪彂缁橝GV杞繍鍛戒护", "AGV");
-                    //瑙f瀽杩斿洖鏁版嵁 
-                    var agvModel = JsonConvert.DeserializeObject<OutCommanAgvDto>(response);
-                    if (agvModel.Code == "0")
+                    //鍒ゆ柇澶栦晶鍌ㄤ綅鏄惁鏈夋《
+                    var palletInfoYi = Db.Queryable<DataStockDetail>().First(w => w.IsDel == "0" && w.LocatNo == locatInfo.AisleOne);
+                    if (palletInfoYi != null)
                     {
-                        str += "涓嬪彂鎴愬姛";
-
-                        //璇锋眰鎴愬姛淇敼浠诲姟琛ㄧ浉搴斿瓧娈电姸鎬�
-                        logTaskEntry.IsSuccess = 1;
-                        logTaskEntry.BackDate = DateTime.Now;
-                        logTaskEntry.Status = "1";
-                        if (taskDetial.Crtype == "0" || taskDetial.Crtype == "2")
+                        //鍒ゆ柇瑕佺Щ搴撶殑妗舵槸鍚﹁鍒嗛厤
+                        if (palletInfoYi.Status != "0")
                         {
-                            sql = $"UPDATE SysStorageLocat SET Status = '2' where LocatNo = '{taskDetial.Endport}'";
-                            if (taskDetial.Type == "2")
+                            throw new Exception("瑕佺Щ搴撶殑妗跺凡琚垎閰嶏紝璇风◢鍚庡啀璇�");
+                        }
+                        //鍒ゆ柇瑕佺Щ搴撶殑妗舵槸鍚︽湁姝e湪鎵ц鐨勪换鍔�
+                        var taskInfo = Db.Queryable<LogTask>().First(w => w.IsDel == "0" && (w.Status == "0" || w.Status == "1") && w.PalletNo == palletInfoYi.PalletNo);
+                        if (taskInfo != null)
+                        {
+                            throw new Exception("瑕佺Щ搴撶殑妗舵湁姝e湪鎵ц鐨勪换鍔★紝璇风◢鍚庡啀璇�");
+                        }
+                        #region#缁欒绉诲簱鐨勬《鍒嗛厤鏂板偍浣�
+                        var locatInfoNew = new SysStorageLocat();
+                        //浼樺厛鍒嗛厤鍐呬晶鍌ㄤ綅
+                        var locatListNei = Db.Queryable<SysStorageLocat>().Where(w => w.IsDel == "0" && w.Status == "0" && !string.IsNullOrEmpty(w.AisleOne) && w.WareHouseNo == palletInfoYi.WareHouseNo && w.AreaNo == palletInfoYi.AreaNo).ToList();
+                        foreach (var item in locatListNei)
+                        {
+                            //鍒ゆ柇璇ュ唴渚у偍浣嶇殑澶栦晶鍌ㄤ綅鏄惁鏈夋《
+                            var detailWai = Db.Queryable<DataStockDetail>().First(w => w.IsDel == "0" && w.LocatNo == item.AisleOne);
+                            if (detailWai != null)
                             {
-                                sql2 = $"UPDATE DataStockDetail SET PalletStatus = '{taskDetial.Type}'" +
-                                $"where PalletNo = '{taskDetial.Pallno}'";
-                                Db.Ado.ExecuteCommand(sql2);
+                                continue;
+                            }
+                            locatInfoNew = item;
+                            break;
+                        }
+                        if (locatInfoNew == null)
+                        {
+                            //娌℃湁鍐呬晶鍌ㄤ綅锛屽啀鏌ユ壘澶栦晶鍌ㄤ綅
+                            var locatListWai = Db.Queryable<SysStorageLocat>().Where(w => w.IsDel == "0" && w.Status == "0" && string.IsNullOrEmpty(w.AisleOne) && w.WareHouseNo == palletInfoYi.WareHouseNo && w.AreaNo == palletInfoYi.AreaNo).OrderBy(o => o.Row).ToList();
+                            foreach (var item in locatListWai)
+                            {
+                                //鍒ゆ柇璇ュ偍浣嶇殑鍐呬晶鍌ㄤ綅鏄惁鏈夋鍦ㄦ墽琛岀殑浠诲姟
+                                var locatItemNei = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.AisleOne == item.LocatNo);
+                                if (locatItemNei != null && locatItemNei.Status != "0")
+                                {
+                                    continue;
+                                }
+                                locatInfoNew = item;
+                                break;
                             }
                         }
-                        else
+                        if (locatInfoNew == null)
                         {
-                            sql = $"UPDATE SysStorageLocat SET Status = '3' where LocatNo = '{taskDetial.Startport}' ";
+                            throw new Exception("娌℃湁绌轰綑鐨勭┖鍌ㄤ綅杩涜绉诲簱");
                         }
-                        Db.Insertable(logTaskEntry).ExecuteCommand();
-                        Db.Ado.ExecuteCommand(sql);
-                    }
-                    if (agvModel.Code == "1")
-                    {
-                        logTaskEntry.IsSuccess = 0;
-                        Db.Insertable(logTaskEntry).ExecuteCommand();
-                        Db.CommitTran();
-                        throw new Exception("RCS浠诲姟涓嬪彂閿欒锛孯CS杩斿洖娑堟伅锛�" + agvModel.Message);
+                        #endregion
+
+                        //缁欎笅杞︿笅鍙戠Щ搴撲换鍔�
+                        var taskNo = new Common().GetMaxNo("TN");//浠诲姟鍙�
+                        //浠诲姟淇℃伅
+                        var logTaskEntry = new LogTask
+                        {
+                            TaskNo = taskNo,
+                            Sender = "WMS",
+                            Receiver = "RCS",
+                            //IsSuccess = 1, //鏄惁涓嬪彂鎴愬姛 0澶辫触 1鎴愬姛
+                            SendDate = DateTime.Now,  //鍙戦�佹椂闂�
+                            //BackDate = DateTime.Now,  //杩斿洖鏃堕棿
+                            StartLocat = palletInfoYi.LocatNo,//璧峰浣嶇疆
+                            EndLocat = locatInfoNew.LocatNo,//鐩爣浣嶇疆
+                            PalletNo = palletInfoYi.PalletNo,//鎵樼洏鐮�
+                            IsSend = 1,//鏄惁鍙啀娆′笅鍙�
+                            IsCancel = 1,//鏄惁鍙彇娑�
+                            IsFinish = 1,//鏄惁鍙畬鎴�
+                            Type = "1",//浠诲姟绫诲瀷 0 鍏ュ簱浠诲姟 1 鍑哄簱浠诲姟  2 绉诲簱浠诲姟
+                            OrderType = "3",//鍗曟嵁绫诲瀷 0 鍏ュ簱 1 鍑哄簱 3绉诲簱
+                            Status = "0",//浠诲姟鐘舵��0锛氱瓑寰呮墽琛�1姝e湪鎵ц2鎵ц瀹屾垚
+                            NoticeDetailNo = 0,
+                            Msg = $"灏嗘《{palletInfoYi.PalletNo}浠巤palletInfoYi.LocatNo}绉诲埌{locatInfoNew.LocatNo}", //鍏抽敭淇℃伅
+                            LotNo = ""//鎵规鍙�
+                        };
+
+                        //缁勭粐涓嬪彂灏忚溅浠诲姟淇℃伅
+                        var task = new TaskDetial
+                        {
+                            Taskno = taskNo,//浠诲姟鍙�
+                            Startport = palletInfoYi.LocatNo,//璧峰浣嶇疆
+                            Endport = locatInfoNew.LocatNo,//endLoction.LocatNo,//鐩爣浣嶇疆
+                            Pallno = palletInfoYi.PalletNo,//妗跺彿
+                            Crtype = "1",//鍙《
+                            WareHouseNo = palletInfoYi.WareHouseNo//杞﹂棿缂栫爜
+                        };
+                        string agvMsg = string.Empty;
+                        //缁欎笅杞︿笅鍙戜换鍔�
+                        logTaskEntry.SendDate = DateTime.Now;//鍙戦�佹椂闂�
+                        var agvResult = CreateTaskForAgv(task, url, out agvMsg, "70");
+                        if (agvResult)//鎴愬姛
+                        {
+                            //璇锋眰鎴愬姛淇敼浠诲姟琛ㄧ浉搴斿瓧娈电姸鎬�
+                            logTaskEntry.IsSuccess = 1;
+                            logTaskEntry.IsSend = 0;
+                            //logTaskEntry.IsCancel = 0;
+                            logTaskEntry.BackDate = DateTime.Now;
+                            logTaskEntry.Status = "1";//姝e湪鎵ц
+                            Db.Insertable(logTaskEntry).ExecuteCommand();
+
+
+                            locatInfo.Status = "5";//绉诲嚭涓�
+                            Db.Updateable(locatInfo).ExecuteCommand();
+
+                            locatInfoNew.Status = "4";//绉诲叆涓�
+                            Db.Updateable(locatInfoNew).ExecuteCommand();
+                        }
+                        else//澶辫触
+                        {
+                            logTaskEntry.IsSuccess = 0;
+                            logTaskEntry.Information = agvMsg;
+                            Db.Insertable(logTaskEntry).ExecuteCommand();
+
+                            throw new Exception($"缁欏皬杞︿笅鍙戠Щ搴撲换鍔″け璐ワ紝妗跺彿锛歿palletInfoYi.PalletNo}");
+                        }
                     }
                 }
-                catch (Exception ex)
-                {
-                    throw new Exception(ex.Message);
-                }
-                #endregion
-                //鎻愪氦浜嬪姟
-                Db.CommitTran();
+                return true;
             }
-            catch (Exception ex)
+            catch (Exception e)
             {
-                Db.RollbackTran();
-                throw ex;
+                throw new Exception(e.Message);
             }
         }
     }

--
Gitblit v1.8.0