From f147cfa91feec613eb304c78fecafdf43286034a Mon Sep 17 00:00:00 2001
From: Administrator <Administrator@DESKTOP-5BIMHQ3>
Date: 星期六, 30 三月 2024 11:12:24 +0800
Subject: [PATCH] Merge branch 'wxw'

---
 Wms/WMS.BLL/BllPdaServer/PdaCrServer.cs |  563 +++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 434 insertions(+), 129 deletions(-)

diff --git a/Wms/WMS.BLL/BllPdaServer/PdaCrServer.cs b/Wms/WMS.BLL/BllPdaServer/PdaCrServer.cs
index e01b252..063f080 100644
--- a/Wms/WMS.BLL/BllPdaServer/PdaCrServer.cs
+++ b/Wms/WMS.BLL/BllPdaServer/PdaCrServer.cs
@@ -6,11 +6,13 @@
 using Model.ModelDto.PdaDto;
 using SqlSugar;
 using WMS.BLL.LogServer;
+using WMS.DAL;
 using WMS.Entity.BllAsnEntity;
 using WMS.Entity.BllCheckEntity;
 using WMS.Entity.BllSoEntity;
 using WMS.Entity.Context;
 using WMS.Entity.DataEntity;
+using WMS.Entity.LogEntity;
 using WMS.Entity.SysEntity;
 using WMS.IBLL.IPdaServer;
 
@@ -83,7 +85,7 @@
         }
 
         //鑾峰彇瑕佺洏鐐圭殑绠辩爜淇℃伅锛堢洏鐐硅褰曚腑鏁版嵁锛�
-        public List<StockCheckLogDto> GetStockCheckLogList(string crNo, string crDetail, string palletNo, string boxNo)
+        public List<StockCheckLogDto> GetStockCheckLogList(string crNo, string crDetail, string palletNo, string boxNo, string isContinue)
         {
             try
             {
@@ -102,6 +104,18 @@
 
                 var detail = crDetail.Split("-");
                 var sku = detail[0];
+                //鍒ゆ柇鏄惁涓烘棤鐮佺墿鏂�
+                var skuinfo = Db.Queryable<SysMaterials>().First(m => m.IsDel == "0" && m.SkuNo == sku);
+                //鍒ゆ柇鏄惁瀛樺湪鐗╂枡娑堟伅
+                if (skuinfo == null)
+                {
+                    throw new Exception("鎵樼洏鐮佷笉鑳戒负绌�");
+                }
+                int isQtySku = 0;
+                if (skuinfo.Type == "4" && isContinue == "1")
+                {
+                    isQtySku = 1;
+                }
                 var lotNo = detail[1];
                 var list = Db.Queryable<BllStockCheckLog>().Where(m => m.IsDel == "0" && m.CRNo == crNo);
                 //var deList= Db.Queryable<BllStockCheckDetail>().Where(m => m.IsDel == "0" && m.SkuNo == sku && m.LotNo == lotNo && m.PalletNo == palletNo);
@@ -109,25 +123,47 @@
                 //{
                 //    throw new Exception("鏈煡璇㈠埌鎵樼洏涓婄殑鐩樼偣鐗╂枡鎵规淇℃伅");
                 //}
-
                 list = list.Where(m => m.SkuNo == sku && m.LotNo == lotNo && m.PalletNo == palletNo);
                 if (!string.IsNullOrWhiteSpace(boxNo))
                 {
                     list = list.Where(m => m.BoxNo == boxNo);
                 }
-
-                var data = list.Select(m => new StockCheckLogDto()
+                List<StockCheckLogDto> data = new List<StockCheckLogDto>();
+                //鍒ゆ柇鏄惁涓烘棤鐮佺墿鏂�
+                if (isQtySku == 1)
                 {
-                    BoxNo = m.BoxNo,
-                    BoxNo3 = m.BoxNo3,
-                    SkuNo = m.SkuNo,
-                    SkuName = m.SkuName,
-                    LotNo = m.LotNo,
-                    Qty = m.Qty,
-                    CheckResult = m.CheckResult,
+                    //鑾峰彇搴撳瓨淇℃伅
+                    var stockDetail = Db.Queryable<DataStockDetail>().First(a => a.IsDel == "0" && a.Status == "3" && a.SkuNo == sku && a.PalletNo == palletNo);
 
-                }).ToList();
+                    var model = new StockCheckLogDto()
+                    {
+                        PalletNo = palletNo,
+                        SkuNo = sku,
+                        Qty = stockDetail.Qty,
+                        CheckResult = 4,
+                    };
+
+                    data.Add(model);
+
+                }
+                else
+                {
+                    data = list.Select(m => new StockCheckLogDto()
+                    {
+                        BoxNo = m.BoxNo,
+                        BoxNo3 = m.BoxNo3,
+                        SkuNo = m.SkuNo,
+                        SkuName = m.SkuName,
+                        LotNo = m.LotNo,
+                        Qty = m.Qty,
+                        CheckResult = m.CheckResult,
+
+                    }).ToList();
+
+                }
+
                 return data;
+
             }
             catch (Exception e)
             {
@@ -135,7 +171,7 @@
             }
         }
 
-        public void CrSetCheck(string crNo, string crDetail, string palletNo, string boxNo, string boxNo3, string result, int? qty, int userId)
+        public void CrSetCheck(string crNo, string crDetail, string palletNo, string boxNo, string boxNo3, string result, int? qty, string isContinue, int userId)
         {
             try
             {
@@ -165,8 +201,8 @@
                 //鐩樼偣璁板綍
                 var checkLog = Db.Queryable<BllStockCheckLog>().Where(m =>
                     m.IsDel == "0" && m.CRNo == crNo && m.PalletNo == palletNo && m.SkuNo == sku && m.LotNo == lotNo && m.CheckResult == null);
-
                 var time = DateTime.Now;
+                //姝e父
                 if (result == "0")
                 {
                     if (!string.IsNullOrWhiteSpace(boxNo))
@@ -178,7 +214,7 @@
                         checkLog = checkLog.Where(m => m.BoxNo3 == boxNo3);
                     }
 
-                    if (checkLog.Count() == 0)
+                    if (checkLog.Count() == 0 && isContinue == "0")
                     {
                         throw new Exception("鏈煡璇㈠埌鏈洏鐐圭殑绠辨敮淇℃伅");
                     }
@@ -189,7 +225,7 @@
                     {
                         if (l.CheckResult != null)
                         {
-                           continue;
+                            continue;
                         }
                         l.RealQty = l.Qty;
                         l.CheckResult = 0;
@@ -204,6 +240,7 @@
                         checkDetail.RealQty = 0;
                     }
                     checkDetail.RealQty += num;
+                    checkDetail.CheckResult = 0;
                     //if (checkDetail.CheckResult == null || checkDetail.CheckResult == 0)
                     //{
                     //    checkDetail.CheckResult = 0;
@@ -211,6 +248,7 @@
                     Db.Updateable(checkDetail).ExecuteCommand();
                     Db.Updateable(list).ExecuteCommand();
                 }
+                //鐩樹簭
                 else if (result == "1")
                 {
                     if (!string.IsNullOrWhiteSpace(boxNo))
@@ -222,84 +260,141 @@
                         checkLog = checkLog.Where(m => m.BoxNo3 == boxNo3);
                     }
 
-                    if (checkLog.Count() == 0)
+                    if (checkLog.Count() == 0 && isContinue == "0")
                     {
                         throw new Exception("鏈煡璇㈠埌绠辨敮淇℃伅");
                     }
 
+                    //
+                    if (isContinue == "1")
+                    {
+                        if (qty == null || qty <= 0)
+                        {
+                            throw new Exception("鏁伴噺鐩樼偣鏃躲�佺洏浜忔暟閲忎笉鑳戒负绌轰笖闇�澶т簬0");
+                        }
+                    }
+
+
                     var list = checkLog.ToList();
-                    var num = 0;
+                    decimal num = 0;
                     foreach (var l in list)
                     {
                         l.RealQty = 0;
                         l.CheckResult = 1;
                         l.CheckDate = time;
                         l.CheckUserId = userId;
-                        num -= int.Parse(l.RealQty.ToString());
+                        num -= (decimal)l.RealQty;
                     }
                     if (checkDetail.RealQty == null)
                     {
                         checkDetail.RealQty = 0;
                     }
                     checkDetail.RealQty += num;
-                    //checkDetail.CheckResult = 1;
+                    if (isContinue == "1")
+                    {
+                        checkDetail.RealQty = qty;
+                    }
+                    checkDetail.CheckResult = 1;
+                    var checkInfo = Db.Queryable<BllStockCheckLog>().First(m =>
+                    m.IsDel == "0" && m.CRNo == crNo && m.PalletNo == palletNo && m.SkuNo == sku && m.LotNo == lotNo);
+                    checkInfo.RealQty = qty;
+                    checkInfo.CheckResult = 1;
+
+                    checkInfo.CheckUserId = userId;
+                    checkInfo.CheckDate = time;
+
+                    checkInfo.UpdateUser = userId;
+                    checkInfo.UpdateTime = time;
+                    Db.Updateable(checkInfo).ExecuteCommand();
                     Db.Updateable(checkDetail).ExecuteCommand();
                     Db.Updateable(list).ExecuteCommand();
                 }
+                //鐩樼泩
                 else if (result == "2")
                 {
-                    if (string.IsNullOrWhiteSpace(boxNo) || string.IsNullOrWhiteSpace(boxNo3))
+                    if (isContinue == "0")
                     {
-                        throw new Exception("鐩樼泩鏃剁鐮佷笌鏀爜涓嶈兘涓虹┖");
+                        if (string.IsNullOrWhiteSpace(boxNo) || string.IsNullOrWhiteSpace(boxNo3))
+                        {
+                            throw new Exception("鐩樼泩鏃剁鐮佷笌鏀爜涓嶈兘涓虹┖");
+                        }
                     }
-
                     if (qty == null || qty <= 0)
                     {
                         throw new Exception("鐩樼泩鏃舵暟閲忎笉鑳戒负绌轰笖闇�澶т簬0");
                     }
-
-                    var count = Db.Queryable<DataBoxInfo>()
-                        .Count(m => m.IsDel == "0" && m.BoxNo == boxNo && m.BoxNo3 == boxNo3);
-                    var count2 = Db.Queryable<BllStockCheckLog>()
-                        .Count(m => m.IsDel == "0" && m.BoxNo == boxNo && m.BoxNo3 == boxNo3);
-                    if (count > 0 || count2 > 0)
+                    if (isContinue == "0")
                     {
-                        throw new Exception("褰撳墠搴撳瓨涓凡瀛樺湪璇ョ鏀俊鎭�");
+                        var count = Db.Queryable<DataBoxInfo>()
+                        .Count(m => m.IsDel == "0" && m.BoxNo == boxNo && m.BoxNo3 == boxNo3);
+                        var count2 = Db.Queryable<BllStockCheckLog>()
+                            .Count(m => m.IsDel == "0" && m.BoxNo == boxNo && m.BoxNo3 == boxNo3);
+                        if (count > 0 || count2 > 0)
+                        {
+                            throw new Exception("褰撳墠搴撳瓨涓凡瀛樺湪璇ョ鏀俊鎭�");
+                        }
                     }
-                    var crLog = new BllStockCheckLog()
+                    BllStockCheckLog crLog = new BllStockCheckLog();
+                    if (isContinue == "0")
                     {
-                        CRNo = crNo,
-                        PalletNo = palletNo,
-                        BoxNo = boxNo,
-                        BoxNo2 = null,
-                        BoxNo3 = boxNo3,
-                        Qty = qty,
-                        SkuNo = checkDetail.SkuNo,
-                        SkuName = checkDetail.SkuName,
-                        Standard = checkDetail.Standard,
-                        LotNo = checkDetail.LotNo,
-                        LotText = checkDetail.LotText,
-                        SupplierLot = checkDetail.SupplierLot,
+                        crLog = new BllStockCheckLog()
+                        {
+                            CRNo = crNo,
+                            PalletNo = palletNo,
+                            BoxNo = boxNo,
+                            BoxNo2 = null,
+                            BoxNo3 = boxNo3,
+                            Qty = qty,
+                            SkuNo = checkDetail.SkuNo,
+                            SkuName = checkDetail.SkuName,
+                            Standard = checkDetail.Standard,
+                            LotNo = checkDetail.LotNo,
+                            LotText = checkDetail.LotText,
+                            SupplierLot = checkDetail.SupplierLot,
 
-                        RealQty = qty,
-                        CheckResult = 2,
-                        CheckDate = time,
-                        CheckUserId = userId,
+                            RealQty = qty,
+                            CheckResult = 2,
+                            CheckDate = time,
+                            CheckUserId = userId,
 
-                        CreateUser = userId,
-                        CreateTime = time
-                    };
-                    if (checkDetail.RealQty == null)
+                            CreateUser = userId,
+                            CreateTime = time
+                        };
+                    }
+                    else
+                    {
+                        var checkInfo = Db.Queryable<BllStockCheckLog>().First(m =>
+                    m.IsDel == "0" && m.CRNo == crNo && m.PalletNo == palletNo && m.SkuNo == sku && m.LotNo == lotNo);
+                        checkInfo.RealQty = qty;
+                        checkInfo.CheckResult = 2;
+
+                        checkInfo.CheckUserId = userId;
+                        checkInfo.CheckDate = time;
+
+                        checkInfo.UpdateUser = userId;
+                        checkInfo.UpdateTime = time;
+                        Db.Updateable(checkInfo).ExecuteCommand();
+                    }
+
+                    if (checkDetail.RealQty == null && isContinue == "0")
                     {
                         checkDetail.RealQty = 0;
                     }
                     checkDetail.RealQty += qty;
-                    //checkDetail.CheckResult = 1;
+                    if (isContinue == "1")
+                    {
+                        checkDetail.RealQty = qty;
+                    }
+                    checkDetail.CheckResult = 2;
                     Db.Updateable(checkDetail).ExecuteCommand();
-                    Db.Insertable(crLog).ExecuteCommand();
+                    if (isContinue == "0")
+                    {
+                        Db.Insertable(crLog).ExecuteCommand();
+                    }
                 }
                 else
                 {
+                    Db.RollbackTran();
                     throw new Exception("鐩樼偣缁撴灉涓嶇锛岃鏍稿疄锛�");
                 }
                 var checkLogNum = Db.Queryable<BllStockCheckLog>().Where(m => m.IsDel == "0" && m.CRNo == crNo && m.PalletNo == palletNo && m.SkuNo == sku && m.LotNo == lotNo);
@@ -352,7 +447,7 @@
             {
                 str += " and PalletNo like @palletno";
             }
-            
+
             //鎺掑簭
             str += " order by LotNo,LocatNo,PalletNo";
             List<DataStockDetail> stockList = Db.Ado.SqlQuery<DataStockDetail>(str, new
@@ -377,7 +472,7 @@
                 {
                     throw new Exception("鎵樼洏鍙蜂笉鑳戒负绌�");
                 }
-                var detail = Db.Queryable<DataStockDetail>().Where(m => m.IsDel == "0" && m.PalletNo == palletNo).Select(m=>m.Id).ToList();
+                var detail = Db.Queryable<DataStockDetail>().Where(m => m.IsDel == "0" && m.PalletNo == palletNo).Select(m => m.Id).ToList();
                 var info = Db.Queryable<DataBoxInfo>().Where(m => m.IsDel == "0" && detail.Contains(m.StockDetailId))
                     .GroupBy(m => m.BoxNo).Select(a => new PdaPalletNoCheckDto
                     {
@@ -386,7 +481,7 @@
                     }).ToList();
                 return info;
             }
-            catch(Exception e)
+            catch (Exception e)
             {
                 throw new Exception(e.Message);
             }
@@ -401,9 +496,9 @@
                 {
                     throw new Exception("绠辩爜涓嶈兘涓虹┖");
                 }
-                 
+
                 var info = Db.Queryable<DataBoxInfo>().Where(m => m.IsDel == "0" && m.BoxNo == boxNo)
-                    .GroupBy(m => new{m.BoxNo,m.SkuNo,m.SkuName,m.LotNo}).Select(a => new PdaPalletNoCheckDto
+                    .GroupBy(m => new { m.BoxNo, m.SkuNo, m.SkuName, m.LotNo }).Select(a => new PdaPalletNoCheckDto
                     {
                         BoxNo = a.BoxNo,
                         SkuNo = a.SkuNo,
@@ -411,7 +506,7 @@
                         LotNo = a.LotNo,
                         Qty = SqlFunc.AggregateSum(a.Qty)
                     }).ToList();
-                if (info.Count>1)
+                if (info.Count > 1)
                 {
                     throw new Exception("褰撳墠绠辩爜鏌ヨ鍑哄鏉$墿鏂欐垨鎵规淇℃伅锛岃鏍稿疄");
                 }
@@ -425,7 +520,7 @@
         }
 
         //瑙g粦鍘熸墭鐩樼粦瀹氭柊鎵樼洏
-        public void SaveUnbind(string palletNo,string boxNo,string palletNoNew,int userId)
+        public void SaveUnbind(string palletNo, string boxNo, string palletNoNew, int userId)
         {
             try
             {
@@ -450,7 +545,7 @@
                     throw new Exception("鍘熸墭鐩樹笂鏈煡鍒扮鏀槑缁嗭紝璇锋牳瀹�");
                 }
                 //搴撳瓨鏄庣粏id
-                var infoIds = infos.GroupBy(m => m.StockDetailId).Select(a =>a.StockDetailId).ToList();
+                var infoIds = infos.GroupBy(m => m.StockDetailId).Select(a => a.StockDetailId).ToList();
                 if (infoIds.Count == 0)
                 {
                     throw new Exception("鏈煡璇㈠埌褰撳墠绠辩爜淇℃伅锛岃鏍稿疄");
@@ -485,7 +580,7 @@
                                 }
                             }
                         }
-                    }                                    
+                    }
                     #endregion
                 }
                 if (!newPalletHaveGoods)//鏂版墭鐩樹笂娌℃湁鐗╁搧
@@ -520,7 +615,7 @@
                     }
                     var stockDetail2 = stockDetailList.FirstOrDefault(w => w.SkuNo == stockDetail1.SkuNo && w.LotNo == stockDetail1.LotNo);
 
-                    var stId = 0;                    
+                    var stId = 0;
                     if (stockDetail2 == null) //娣诲姞-鏂版墭鐩樹笂娌℃湁鍚岀墿鏂欏悓鎵规鐗╁搧
                     {
                         //鏂版墭鐩樻坊鍔犲簱瀛樻槑缁�
@@ -678,33 +773,41 @@
         {
             try
             {
-                var models = Db.Queryable<DataStockDetail>().First(w => w.IsDel == "0" && w.PalletNo == palletNo);
-                if (models == null)
-                {
-                    throw new Exception("鎵樼洏搴撳瓨淇℃伅涓嶅瓨鍦�,璇锋鏌�!");
-                }
-                if (string.IsNullOrEmpty(models.LocatNo))
-                {
-                    throw new Exception("鎵樼洏鍌ㄤ綅淇℃伅涓嶅瓨鍦�,璇锋鏌�!");
-                }
-                if (models.WareHouseNo != "W02")
-                {
-                    throw new Exception("璇ユ墭鐩樻湭鍦ㄥ钩搴撳唴,璇锋鏌�!");
-                }
-                var storageLocat = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == models.LocatNo);
-                if (storageLocat == null)
-                {
-                    throw new Exception("鍌ㄤ綅淇℃伅涓嶅瓨鍦�,璇锋鏌�!");
-                }
                 var storageArea = new List<SysStorageArea>();
-                if (storageLocat.AreaNo.Contains("B0"))
+                if (string.IsNullOrEmpty(palletNo))
                 {
-                    storageArea = Db.Queryable<SysStorageArea>().Where(w => w.AreaNo.Contains("B0")).ToList();
+                    storageArea = Db.Queryable<SysStorageArea>().Where(w => w.IsDel == "0" && w.Status == "0" && w.WareHouseNo == "W02").OrderBy(o=>o.AreaNo).ToList();
                 }
                 else
                 {
-                    storageArea = Db.Queryable<SysStorageArea>().Where(w => w.AreaNo.Contains("B1")).ToList();
-                }
+                    var models = Db.Queryable<DataStockDetail>().First(w => w.IsDel == "0" && w.PalletNo == palletNo);
+                    if (models == null)
+                    {
+                        throw new Exception("鎵樼洏搴撳瓨淇℃伅涓嶅瓨鍦�,璇锋鏌�!");
+                    }
+                    if (string.IsNullOrEmpty(models.LocatNo))
+                    {
+                        throw new Exception("鎵樼洏鍌ㄤ綅淇℃伅涓嶅瓨鍦�,璇锋鏌�!");
+                    }
+                    if (models.WareHouseNo != "W02")
+                    {
+                        throw new Exception("璇ユ墭鐩樻湭鍦ㄥ钩搴撳唴,璇锋鏌�!");
+                    }
+                    var storageLocat = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == models.LocatNo);
+                    if (storageLocat == null)
+                    {
+                        throw new Exception("鍌ㄤ綅淇℃伅涓嶅瓨鍦�,璇锋鏌�!");
+                    }
+                   
+                    if (storageLocat.AreaNo.Contains("B0"))
+                    {
+                        storageArea = Db.Queryable<SysStorageArea>().Where(w => w.IsDel == "0" && w.Status == "0" && w.AreaNo.Contains("B0") && w.WareHouseNo == "W02").OrderBy(o => o.AreaNo).ToList();
+                    }
+                    else
+                    {
+                        storageArea = Db.Queryable<SysStorageArea>().Where(w => w.IsDel == "0" && w.Status == "0" && w.AreaNo.Contains("B1") && w.WareHouseNo == "W02").OrderBy(o => o.AreaNo).ToList();
+                    }
+                }                
                 return storageArea;
             }
             catch (Exception ex)
@@ -772,7 +875,7 @@
         }
 
         /// <summary>
-        /// agv杞繍鍛煎彨灏忚溅-寮�鍙戞湭瀹屾垚寰呯‘璁�
+        /// agv杞繍鍛煎彨灏忚溅
         /// </summary>
         /// <param name="palletNo"></param>
         /// <param name="areaNo"></param>
@@ -787,12 +890,41 @@
             {
                 throw new Exception("璇烽�夋嫨鐩爣鍖哄煙鎴栧叆搴撳彛锛�");
             }
+            if (!string.IsNullOrEmpty(areaNo) && !string.IsNullOrEmpty(ruku))
+            {
+                throw new Exception("鐩爣鍖哄煙鍜屽叆搴撳彛涓嶈兘鍚屾椂閫夋嫨锛�");
+            }            
             try
             {
+                string EndLocat = string.Empty;//鐩爣浣嶇疆                
+
+                var log = Db.Queryable<LogTask>().First(w => w.IsDel == "0" && w.PalletNo == palletNo && (w.Status == "0" || w.Status == "1"));
+                if (log != null)
+                {
+                    throw new Exception("璇ユ墭鐩樺凡鏈夊皬杞︾瓑寰呮墽琛屾垨姝e湪鎵ц鐨勪换鍔�!");
+                }                
                 var stockDetail = Db.Queryable<DataStockDetail>().First(w => w.IsDel == "0" && w.PalletNo == palletNo);
                 if (stockDetail == null)
                 {
-                    throw new Exception("鎵樼洏搴撳瓨淇℃伅涓嶅瓨鍦�,璇锋鏌�!");
+                    throw new Exception("鎵樼洏涓婄墿鏂欏簱瀛樻槑缁嗕俊鎭笉瀛樺湪,璇锋鏌�!");
+                }
+                if (!string.IsNullOrEmpty(areaNo))
+                {
+                    var storageArea = Db.Queryable<SysStorageArea>().First(w => w.IsDel == "0" && w.AreaNo == areaNo);
+                    if (storageArea == null)
+                    {
+                        throw new Exception("鎵�閫夊尯鍩熶俊鎭笉瀛樺湪,璇锋鏌�!");
+                    }
+                    EndLocat = GetLocat(areaNo, stockDetail.SkuNo, stockDetail.LotNo,palletNo);
+                }
+                else
+                {
+                    EndLocat = ruku;
+                }
+                var stock = Db.Queryable<DataStock>().First(w => w.IsDel == "0" && w.SkuNo == stockDetail.SkuNo && w.LotNo == stockDetail.LotNo);
+                if (stock == null)
+                {
+                    throw new Exception("鎵樼洏涓婄墿鏂欏簱瀛樹俊鎭笉瀛樺湪,璇锋鏌�!");
                 }
                 if (string.IsNullOrEmpty(stockDetail.LocatNo))
                 {
@@ -802,30 +934,76 @@
                 {
                     throw new Exception("璇ユ墭鐩樻湭鍦ㄥ钩搴撳唴,璇锋鏌�!");
                 }
+                //璧峰鍌ㄤ綅淇℃伅
                 var storageLocat = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == stockDetail.LocatNo);
                 if (storageLocat == null)
                 {
                     throw new Exception("鍌ㄤ綅淇℃伅涓嶅瓨鍦�,璇锋鏌�!");
                 }
+                //鐩爣鍌ㄤ綅淇℃伅
+                var storageLocatEnd = new SysStorageLocat();
+                if (!string.IsNullOrEmpty(areaNo))
+                {
+                    storageLocatEnd = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == EndLocat && w.Flag == "0" && w.Status == "0");
+                    if (storageLocatEnd == null)
+                    {
+                        throw new Exception("鐩爣鍌ㄤ綅淇℃伅涓嶅瓨鍦�,璇锋鏌�!");
+                    }
+                }
+                
                 //寮�鍚簨鍔�
                 Db.BeginTran();
+
+                //娣诲姞鍑哄簱浠诲姟
+                var taskNo = new Common().GetMaxNo("TK");
+                var exTask = new LogTask
+                {
+                    TaskNo = taskNo,
+                    Sender = "WMS",
+                    Receiver = "AGV",
+                    IsSuccess = 0, //鏄惁涓嬪彂鎴愬姛 0澶辫触 1鎴愬姛
+
+                    StartLocat = stockDetail.LocatNo,//璧峰浣嶇疆
+                    EndLocat = EndLocat,//鐩爣浣嶇疆
+                    PalletNo = palletNo,//鎵樼洏鐮�
+                    Msg = string.Format("杞繍浠诲姟锛歿0}=>>{1}", stockDetail.LocatNo, EndLocat),
+                    IsSend = 1,//鏄惁鍙啀娆′笅鍙�
+                    IsCancel = 1,//鏄惁鍙彇娑�
+                    IsFinish = 1,//鏄惁鍙畬鎴�
+                    Type = "2",//浠诲姟绫诲瀷 0 鍏ュ簱浠诲姟 1 鍑哄簱浠诲姟  2 绉诲簱浠诲姟
+                    Status = "0",//浠诲姟鐘舵��0锛氱瓑寰呮墽琛�1姝e湪鎵ц2鎵ц瀹屾垚
+                    OrderType = "3",//0 鍏ュ簱鍗� 1 鍑哄簱鍗�  2 鐩樼偣鍗�  3 绉诲簱鍗�
+
+                    CreateTime = DateTime.Now
+                };
+                Db.Insertable(exTask).ExecuteCommand();
+
                 //淇敼搴撳瓨鏄庣粏淇℃伅
-                stockDetail.WareHouseNo = "";
-                stockDetail.RoadwayNo = "";
-                stockDetail.AreaNo = "";
-                stockDetail.LocatNo = "";
-
+                stockDetail.Status = "4";//绉诲簱閿佸畾
+                stockDetail.LockQty = stockDetail.Qty;//閿佸畾搴撳瓨鏁伴噺
                 Db.Updateable(stockDetail).ExecuteCommand();
-                //淇敼鍌ㄤ綅鍦板潃鐘舵��
-                storageLocat.Status = "0";//绌哄偍浣�
+                //淇敼搴撳瓨淇℃伅
+                stock.LockQty += (decimal)stockDetail.Qty;
+                Db.Updateable(stock).ExecuteCommand();
 
+                //淇敼璧峰鍌ㄤ綅鍦板潃鐘舵��
+                storageLocat.Status = "5";//0锛氱┖鍌ㄤ綅 1锛氭湁鐗╁搧 2锛氬叆搴撲腑 3锛氬嚭搴撲腑 4锛氱Щ鍏ヤ腑 5锛氱Щ鍑轰腑 
                 Db.Updateable(storageLocat).ExecuteCommand();
+
+                //淇敼鐩爣鍌ㄤ綅鍦板潃鐘舵��
+                if (storageLocatEnd != null)
+                {
+                    storageLocatEnd.Status = "4";//0锛氱┖鍌ㄤ綅 1锛氭湁鐗╁搧 2锛氬叆搴撲腑 3锛氬嚭搴撲腑 4锛氱Щ鍏ヤ腑 5锛氱Щ鍑轰腑 
+                    Db.Updateable(storageLocatEnd).ExecuteCommand();
+                }               
+                //娣诲姞鎿嶄綔鏃ュ織璁板綍
+                var k = new OperationCrServer().AddLogOperationCr("PDA妯″潡", "AGV杞繍", palletNo, "绉诲簱", $"PDA鍛煎彨灏忚溅瀵规墭鐩樺彿锛歿palletNo}鍙戣捣杞繍", userId);
+                //鎻愪氦浜嬪姟
+                Db.CommitTran();
 
                 #region 鍛煎彨灏忚溅浠g爜
 
                 #endregion
-                //鎻愪氦浜嬪姟
-                Db.CommitTran();
             }
             catch (Exception ex)
             {
@@ -834,7 +1012,7 @@
             }
         }
         /// <summary>
-        /// agv杞繍鍛煎彨灏忚溅鍙栬揣-寮�鍙戞湭瀹屾垚寰呯‘璁�
+        /// agv杞繍鍛煎彨灏忚溅鍙栬揣
         /// </summary>
         /// <param name="soNo"></param>
         /// <param name="palletNo"></param>
@@ -846,63 +1024,145 @@
             {
                 throw new Exception("璇烽�夋嫨鍑哄簱鍗曟嵁锛�");
             }
-            if (string.IsNullOrEmpty(palletNo))
-            {
-                throw new Exception("璇锋壂鎻忔墭鐩樻潯鐮侊紒");
-            }
             if (string.IsNullOrEmpty(areaNo))
             {
                 throw new Exception("璇烽�夋嫨鐩爣鍖哄煙锛�");
             }
             try
             {
+                //寮�鍚簨鍔�
+                Db.BeginTran();
+                //鍑哄簱鍗曚俊鎭�
                 var notice = Db.Queryable<BllExportNotice>().First(w => w.IsDel == "0" && w.SONo == soNo);
                 if (notice == null)
                 {
                     throw new Exception("鍑哄簱鍗曟嵁淇℃伅涓嶅瓨鍦�,璇锋鏌�!");
                 }
-                var allot = Db.Queryable<BllExportAllot>().First(w => w.IsDel == "0" && w.SONo == soNo && w.PalletNo == palletNo);
-                if (allot == null)
+                //鐩爣鍖哄煙淇℃伅
+                var storageArea = Db.Queryable<SysStorageArea>().First(w => w.IsDel == "0" && w.AreaNo == areaNo);
+                if (storageArea == null)
                 {
-                    throw new Exception("璇ユ墭鐩樺嚭搴撳垎閰嶄俊鎭笉瀛樺湪,璇锋鏌�!");
+                    throw new Exception("鎵�閫夊尯鍩熶俊鎭笉瀛樺湪,璇锋鏌�!");
                 }
-                var stockDetail = Db.Queryable<DataStockDetail>().First(w => w.IsDel == "0" && w.PalletNo == palletNo);
-                if (stockDetail == null)
+                List<DataStockDetail> stockDetailList = new List<DataStockDetail>();//闇�瑕佺殑鎵樼洏鏄庣粏
+                if (!string.IsNullOrEmpty(palletNo))//鍗曟墭鐩樺彨璐�
                 {
-                    throw new Exception("鎵樼洏搴撳瓨淇℃伅涓嶅瓨鍦�,璇锋鏌�!");
+                    stockDetailList = Db.Queryable<DataStockDetail>().Where(w => w.IsDel == "0" && w.PalletNo == palletNo).ToList();
                 }
-                if (string.IsNullOrEmpty(stockDetail.LocatNo))
+                else//鏁翠釜鍑哄簱鍗曞彨璐�
                 {
-                    throw new Exception("鎵樼洏鍌ㄤ綅淇℃伅涓嶅瓨鍦�,璇锋鏌�!");
+                    string sqlStr = $"select * from DataStockDetail where PalletNo in (select PalletNo from BllExportAllot where IsDel='0' and SONo='{soNo}')";
+                    stockDetailList = Db.Ado.SqlQuery<DataStockDetail>(sqlStr).ToList();
                 }
-                if (stockDetail.WareHouseNo != "W02")
+                if (stockDetailList.Count<=0)
                 {
-                    throw new Exception("璇ユ墭鐩樻湭鍦ㄥ钩搴撳唴,璇锋鏌�!");
+                    throw new Exception("鎵樼洏鏄庣粏涓嶅瓨鍦�,璇锋鏌�!");
                 }
-                var storageLocat = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == stockDetail.LocatNo);
-                if (storageLocat == null)
-                {
-                    throw new Exception("鍌ㄤ綅淇℃伅涓嶅瓨鍦�,璇锋鏌�!");
-                }
-                //寮�鍚簨鍔�
-                Db.BeginTran();
-                //淇敼搴撳瓨鏄庣粏淇℃伅
-                stockDetail.WareHouseNo = "";
-                stockDetail.RoadwayNo = "";
-                stockDetail.AreaNo = "";
-                stockDetail.LocatNo = "";
 
-                Db.Updateable(stockDetail).ExecuteCommand();
-                //淇敼鍌ㄤ綅鍦板潃鐘舵��
-                storageLocat.Status = "0";//绌哄偍浣�
+                var log = Db.Queryable<LogTask>().Where(w => w.IsDel == "0" && (w.Status == "0" || w.Status == "1"));//浠诲姟琛�
+                var storageLocat = Db.Queryable<SysStorageLocat>().Where(w => w.IsDel == "0");//鍌ㄤ綅琛�
+                var stock = Db.Queryable<DataStock>().Where(w => w.IsDel == "0");//搴撳瓨鎬昏〃
+                string EndLocat = string.Empty;//鐩爣浣嶇疆
+                string hasLocatNoList = string.Empty;//宸插垎閰嶇殑鍌ㄤ綅
+                foreach (var item in stockDetailList)
+                {
+                    //浠诲姟淇℃伅
+                    var logInfo = log.First(w =>w.PalletNo == item.PalletNo);
+                    if (logInfo != null)
+                    {
+                        throw new Exception($"鎵樼洏鍙凤細{item.PalletNo}宸叉湁灏忚溅绛夊緟鎵ц鎴栨鍦ㄦ墽琛岀殑浠诲姟!");
+                    }
+                    if (string.IsNullOrEmpty(item.LocatNo))
+                    {
+                        throw new Exception($"鎵樼洏鍙凤細{item.PalletNo}鐨勫偍浣嶄俊鎭笉瀛樺湪,璇锋鏌�!");
+                    }
+                    if (item.WareHouseNo != "W02")
+                    {
+                        throw new Exception($"鎵樼洏鍙凤細{item.PalletNo}鏈湪骞冲簱鍐�,璇锋鏌�!");
+                    }
+                    //璧峰鍌ㄤ綅淇℃伅
+                    var storageLocatBegin = storageLocat.First(w => w.LocatNo == item.LocatNo);
+                    if (storageLocat == null)
+                    {
+                        throw new Exception($"鎵樼洏鍙凤細{item.PalletNo}鎵�鍦ㄧ殑鍌ㄤ綅淇℃伅涓嶅瓨鍦�,璇锋鏌�!");
+                    }
+                    //鑾峰彇鐩爣鍌ㄤ綅
+                    EndLocat = GetLocat(areaNo, item.SkuNo, item.LotNo, palletNo);
+                    if (string.IsNullOrEmpty(EndLocat))
+                    {
+                        throw new Exception("鍒嗛厤鐩爣鍌ㄤ綅澶辫触,璇锋鏌�!");
+                    }
+                    if (string.IsNullOrEmpty(hasLocatNoList))
+                    {
+                        hasLocatNoList = EndLocat;
+                    }
+                    else
+                    {
+                        hasLocatNoList = hasLocatNoList + "," + EndLocat;
+                    }
+                    //鐩爣鍌ㄤ綅淇℃伅
+                    var storageLocatEnd = storageLocat.First(w => w.LocatNo == EndLocat && w.Flag == "0" && w.Status == "0");
+                    if (storageLocatEnd == null)
+                    {
+                        throw new Exception("鐩爣鍌ㄤ綅淇℃伅涓嶅瓨鍦�,璇锋鏌�!");
+                    }
 
-                Db.Updateable(storageLocat).ExecuteCommand();
+                    //娣诲姞鍑哄簱浠诲姟
+                    var taskNo = new Common().GetMaxNo("TK");
+                    var exTask = new LogTask
+                    {
+                        TaskNo = taskNo,
+                        Sender = "WMS",
+                        Receiver = "AGV",
+                        IsSuccess = 0, //鏄惁涓嬪彂鎴愬姛 0澶辫触 1鎴愬姛
+                        Msg = string.Format("杞繍浠诲姟锛歿0}=>>{1}", item.LocatNo, EndLocat),
+                        StartLocat = item.LocatNo,//璧峰浣嶇疆
+                        EndLocat = EndLocat,//鐩爣浣嶇疆
+                        PalletNo = palletNo,//鎵樼洏鐮�
+                        IsSend = 1,//鏄惁鍙啀娆′笅鍙�
+                        IsCancel = 1,//鏄惁鍙彇娑�
+                        IsFinish = 1,//鏄惁鍙畬鎴�
+                        Type = "2",//浠诲姟绫诲瀷 0 鍏ュ簱浠诲姟 1 鍑哄簱浠诲姟  2 绉诲簱浠诲姟
+                        Status = "0",//浠诲姟鐘舵��0锛氱瓑寰呮墽琛�1姝e湪鎵ц2鎵ц瀹屾垚
+                        OrderType = "3",//0 鍏ュ簱鍗� 1 鍑哄簱鍗�  2 鐩樼偣鍗�  3 绉诲簱鍗�
+
+                        CreateTime = DateTime.Now
+                    };
+                    Db.Insertable(exTask).ExecuteCommand();
+
+                    //淇敼搴撳瓨鏄庣粏淇℃伅
+                    item.Status = "4";//绉诲簱閿佸畾
+                    item.LockQty = item.Qty;//閿佸畾搴撳瓨鏁伴噺
+                    Db.Updateable(item).ExecuteCommand();
+
+                    //淇敼璧峰鍌ㄤ綅鍦板潃鐘舵��
+                    storageLocatBegin.Status = "5";//0锛氱┖鍌ㄤ綅 1锛氭湁鐗╁搧 2锛氬叆搴撲腑 3锛氬嚭搴撲腑 4锛氱Щ鍏ヤ腑 5锛氱Щ鍑轰腑 
+                    Db.Updateable(storageLocatBegin).ExecuteCommand();
+                    //淇敼鐩爣鍌ㄤ綅鍦板潃鐘舵��
+                    storageLocatEnd.Status = "4";//0锛氱┖鍌ㄤ綅 1锛氭湁鐗╁搧 2锛氬叆搴撲腑 3锛氬嚭搴撲腑 4锛氱Щ鍏ヤ腑 5锛氱Щ鍑轰腑 
+                    Db.Updateable(storageLocatEnd).ExecuteCommand();
+
+                    var stockInfo = stock.First(w => w.SkuNo == item.SkuNo && w.LotNo == item.LotNo);
+                    //淇敼搴撳瓨淇℃伅
+                    stockInfo.LockQty += (decimal)item.Qty;//閿佸畾鏁伴噺
+                    Db.Updateable(stockInfo).ExecuteCommand();
+                }
+                if (string.IsNullOrEmpty(palletNo))
+                {
+                    //娣诲姞鎿嶄綔鏃ュ織璁板綍
+                    var k = new OperationCrServer().AddLogOperationCr("PDA妯″潡", "AGV杞繍", palletNo, "绉诲簱", $"PDA鍛煎彨灏忚溅瀵瑰嚭搴撳崟锛歿soNo}涓嬫墍鏈夋墭鐩樺彂璧疯浆杩�", userId);
+                }
+                else
+                {
+                    //娣诲姞鎿嶄綔鏃ュ織璁板綍
+                    var k = new OperationCrServer().AddLogOperationCr("PDA妯″潡", "AGV杞繍", palletNo, "绉诲簱", $"PDA鍛煎彨灏忚溅瀵规墭鐩樺彿锛歿palletNo}鍙戣捣杞繍", userId);
+                }
+                //鎻愪氦浜嬪姟
+                Db.CommitTran();
 
                 #region 鍛煎彨灏忚溅浠g爜
 
                 #endregion
-                //鎻愪氦浜嬪姟
-                Db.CommitTran();
             }
             catch (Exception ex)
             {
@@ -910,6 +1170,51 @@
                 throw new Exception(ex.Message);
             }
         }
+        /// <summary>
+        /// 鏍规嵁鍖哄煙鍒嗛厤鍌ㄤ綅
+        /// </summary>
+        /// <param name="areaNo"></param>
+        /// <param name="skuNo"></param>
+        /// <param name="lotNo"></param>
+        /// <returns></returns>
+        private string GetLocat(string areaNo,string skuNo,string lotNo,string palletNo,string hasLocatNoList="")
+        {
+            try
+            {
+                string endLocat = string.Empty;//鐩爣鍌ㄤ綅
+                //褰撶劧鍖哄煙鎵�鏈夊偍浣嶄俊鎭�
+                var storageLocatList = Db.Queryable<SysStorageLocat>().Where(w => w.WareHouseNo == "W02" && w.AreaNo == areaNo).ToList();
+                //鍚屽尯鍩熷悓鎵规鐗╂枡鐨勫偍浣嶄俊鎭�
+                List<string> locatList = Db.Queryable<DataStockDetail>().Where(w => w.IsDel == "0" && w.SkuNo == skuNo && w.LotNo == lotNo && w.WareHouseNo == "W02" && w.AreaNo == areaNo && w.PalletNo != palletNo)
+                    .OrderByDescending(o=>o.LocatNo).Select(s=>s.LocatNo).Distinct().ToList();
+                foreach (var item in locatList)
+                {
+                    var locatInfo = storageLocatList.Where(w => w.LocatNo == item).First();
+
+                    var locatInfo2 = storageLocatList.Where(w => w.IsDel == "0" && w.Status == "0" && w.Flag == "0" && w.LocatNo != locatInfo.LocatNo && w.Row == locatInfo.Row && !hasLocatNoList.Contains(w.LocatNo))
+                        .OrderByDescending(o => o.Column).First();
+                    if (locatInfo2 != null)
+                    {
+                        endLocat = locatInfo2.LocatNo;
+                    }
+                }
+                if (string.IsNullOrEmpty(endLocat))
+                {
+                    var locatInfo3 = storageLocatList.Where(w => w.IsDel == "0" && w.Flag == "0" && w.Status == "0" && !hasLocatNoList.Contains(w.LocatNo))
+                                .OrderByDescending(m => m.Layer).OrderByDescending(m=>m.Column).OrderByDescending(m=> m.Row).First();
+
+                    if (locatInfo3 != null)
+                    {
+                        endLocat = locatInfo3.LocatNo;
+                    }
+                }
+                return endLocat;
+            }
+            catch (Exception ex)
+            {
+                throw new Exception(ex.Message);
+            }
+        }
         #endregion
 
     }

--
Gitblit v1.8.0