From ce47c445a686ce10e8e1de4f386aaf09b34ae8ce Mon Sep 17 00:00:00 2001
From: Administrator <Administrator@DESKTOP-5BIMHQ3>
Date: 星期四, 01 二月 2024 14:22:11 +0800
Subject: [PATCH] 修改pda组托功能

---
 Wms/WMS.BLL/BllPdaServer/PdaAsnServer.cs |  899 ++++++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 637 insertions(+), 262 deletions(-)

diff --git a/Wms/WMS.BLL/BllPdaServer/PdaAsnServer.cs b/Wms/WMS.BLL/BllPdaServer/PdaAsnServer.cs
index 38278f0..5d15df5 100644
--- a/Wms/WMS.BLL/BllPdaServer/PdaAsnServer.cs
+++ b/Wms/WMS.BLL/BllPdaServer/PdaAsnServer.cs
@@ -29,7 +29,7 @@
             try
             {
                 // 鏈叧鍗曠殑鍗曟嵁
-                sqlString = $"select * from BllArrivalNotice where Type in ({model.Type}) and Status != '3' and IsDel='0' order by CreateTime;";
+                sqlString = $"select * from BllArrivalNotice where Type in ({model.Type}) and Status != '3' and Status != '4'  and IsDel='0' order by CreateTime;";
                 var modelList = Db.Ado.SqlQuery<ArrivalNoticeDto>(sqlString);
 
                 return modelList;
@@ -162,253 +162,6 @@
             }
             catch (Exception ex)
             {
-                throw ex;
-            }
-        }
-
-        // 缁戝畾鐗╂枡鎵樼洏
-        public void BindPallet(PdaPalletBindVm model, int userId,string origin)
-        {
-            try
-            {
-                #region 鍒ゆ柇
-
-                if (string.IsNullOrEmpty(model.AsnNo))
-                {
-                    throw new Exception("-1:鍗曟嵁鍙蜂笉鍙负绌�!");
-                }
-                if (model.AsnDetailId == null || model.AsnDetailId == 0)
-                {
-                    throw new Exception("-1:鐗╂枡涓嶅彲涓虹┖!");
-                }
-                if (string.IsNullOrEmpty(model.PalletNo))
-                {
-                    throw new Exception("-1:鎵樼洏鍙蜂笉鍙负绌�!");
-                }
-                if (string.IsNullOrEmpty(model.BoxNo))
-                {
-                    throw new Exception("-1:绠辩爜淇℃伅涓嶅彲涓虹┖!");
-                }
-
-                if (model.IsContinue == "1")
-                {
-                    if (string.IsNullOrWhiteSpace(model.TailBoxNo))
-                    {
-                        throw new Exception("-1:寮�鍚繛缁粍鎵樻椂锛屽熬绠辩爜淇℃伅涓嶅彲涓虹┖!");
-                    }
-
-                }
-                #endregion
-
-                Db.BeginTran();
-
-                //鎵樼洏鏄惁瀛樺湪
-                var pallet = Db.Queryable<SysPallets>().First(m => m.IsDel == "0" && m.PalletNo == model.PalletNo);
-                if (pallet == null)
-                {
-                    throw new Exception("鏈煡璇㈠埌鎵樼洏淇℃伅锛岃鏍稿疄锛�");
-                }
-                // 楠岃瘉鍏ュ簱鍗曟槑缁嗘槸鍚﹀瓨鍦�
-                var detail = Db.Queryable<BllArrivalNoticeDetail>().First(m => m.IsDel == "0" && m.Id == model.AsnDetailId && m.ASNNo == model.AsnNo);
-                if (detail == null)
-                {
-                    throw new Exception("-1:褰撳墠鐗╂枡鍙婃壒娆′笌鍗曟嵁鏃犲叧鑱旓紝璇锋牳瀹�!");
-                }
-                #region 鍖呰
-                var package = Db.Queryable<SysPackag>().Where(m => m.IsDel == "0");
-                var sku = Db.Queryable<SysMaterials>().First(m => m.IsDel == "0" && m.SkuNo == detail.SkuNo);
-                var pack = package.First(m => m.IsDel == "0" && m.PackagNo == sku.PackagNo);
-                if (pack == null) 
-                {
-                    throw new Exception("-1:鑾峰彇鐗╂枡鍖呰澶辫触锛岃鏍稿疄!");
-                }
-                var pNum = 0;//鎵樼洏鐗╁搧鏁伴噺 
-                var bNum = 0;//绠辩爜鐗╁搧鏁伴噺 
-                if (pack.L5Num.HasValue)
-                {
-                    pNum = (int)pack.L5Num;
-                    bNum = (int)pack.L4Num;
-                }
-                else if (pack.L4Num.HasValue)
-                {
-                    pNum = (int)pack.L4Num;
-                    bNum = (int)pack.L3Num;
-                }
-                else if (pack.L3Num.HasValue)
-                {
-                    pNum = (int)pack.L3Num;
-                    bNum = (int)pack.L2Num;
-                }
-                else if (pack.L2Num.HasValue)
-                {
-                    pNum = (int)pack.L2Num;
-                    bNum = (int)pack.L1Num;
-                }
-                else if (pack.L1Num.HasValue)
-                {
-                    pNum = (int)pack.L1Num;
-                    bNum = (int)pack.L1Num;
-                }
-                if (pNum == 0 || bNum == 0)
-                {
-                    throw new Exception($"缁戝畾澶辫触锛寋detail.SkuNo}鐗╁搧鍖呰鏈壘鍒帮紒");
-                }
-                #region 楠岃瘉鏄惁鍏佽绔嬪簱鍚屾墭鐩樹笉鍚岀墿鏂欏叆搴撴垨鍚屾墭鐩樺悓鐗╂枡涓嶅悓鎵规鍏ュ簱
-                var palletBind = Db.Queryable<BllPalletBind>().First(m => m.IsDel == "0" && m.ASNNo == model.AsnNo && m.PalletNo == model.PalletNo && m.ASNDetailNo != model.AsnDetailId);
-                if (palletBind != null)
-                {
-                    var box = Db.Queryable<BllBoxInfo>().First(m => m.IsDel == "0" && m.BindNo == palletBind.Id && m.BitBoxMark == "0");
-                    if (box != null && (box.SkuNo != detail.SkuNo || box.LotNo != detail.LotNo))
-                    {
-                        var funSetting = Db.Queryable<SysFunSetting>().First(a => a.IsDel == "0" && a.FunSetNo == "Fun045");
-                        if (funSetting == null || funSetting.IsEnable == "OFF")
-                        {
-                            throw new Exception($"涓嶅厑璁哥珛搴撳悓鎵樼洏涓嶅悓鐗╂枡鍏ュ簱鎴栧悓鎵樼洏涓嶅悓鎵规鍏ュ簱锛�");
-                        }
-                    }
-                }
-                #endregion
-                var bind = Db.Queryable<BllPalletBind>().First(m => m.IsDel == "0" && m.ASNDetailNo == model.AsnDetailId && m.PalletNo == model.PalletNo);
-                var bindId = 0;
-                if (bind == null)
-                {
-                    bind = new BllPalletBind
-                    {
-                        ASNNo = model.AsnNo,
-                        ASNDetailNo = (int)model.AsnDetailId,
-                        PalletNo = model.PalletNo,
-                        PalletNo2 = "",
-                        PalletNo3 = "",
-                        Qty = 0,
-                        FullQty = pNum,
-                        Status = "0",
-                        Type = "0",
-                        LotNo = detail.LotNo,
-                        LotText = detail.LotText,
-                        SupplierLot = "",
-                        InspectMark = "0",
-                        BitPalletMark = "1",
-                        IsBale = "0",
-                        IsBelt = "0",
-                        CreateUser = userId
-                    };
-                    // 鎻掑叆鎵樼洏缁戝畾琛�
-                    bindId = Db.Insertable(bind).ExecuteReturnIdentity();
-                }
-                else
-                {
-                    if (bind.Status != "0")
-                    {
-                        throw new Exception("-1:褰撳墠鎵樼洏姝e湪鎵ц涓紝缁戝畾澶辫触锛岃鏍稿疄!");
-                    }
-                    bindId = bind.Id;
-                }
-
-                #region 绠辩爜淇℃伅
-
-                var msgStr = $"绠卞彿涓簕model.BoxNo}";
-                var boxInfoList = new List<BllBoxInfo>();
-                //棣栫
-                var boxInfo = Db.Queryable<BllBoxInfo>().Where(m => m.IsDel == "0" && m.BoxNo == model.BoxNo && m.Status == "0").ToList();
-                if (boxInfo.Count == 0)
-                {
-                    throw new Exception("-1:绠辩爜淇℃伅涓嶅瓨鍦�!");
-                }
-                boxInfoList.AddRange(boxInfo);
-                //鏄惁杩炵画缁勬墭
-                if (model.IsContinue == "1")
-                {
-                    //灏剧
-                    var boxInfo2 = Db.Queryable<BllBoxInfo>().Where(m => m.IsDel == "0" && m.BoxNo == model.TailBoxNo && m.Status == "0").ToList();
-                    if (boxInfo2.Count == 0)
-                    {
-                        throw new Exception("-1:灏剧鐮佷俊鎭笉瀛樺湪!");
-                    }
-                    boxInfoList.AddRange(boxInfo2);
-                    var sql = $"select * from BllBoxInfo where IsDel = '0' and Status = '0' and boxNo>'{model.BoxNo}' and boxNo<'{model.TailBoxNo}'; ";
-                    var list = Db.Ado.SqlQuery<BllBoxInfo>(sql);
-                    boxInfoList.AddRange(list);
-                    msgStr += $"灏剧鍙蜂负{model.TailBoxNo}";
-                }
-                #endregion
-                #endregion
-                // 鏇存敼绠辨敮鍏崇郴琛�
-                var factQty = 0;//鎵樼洏鎬绘暟閲�
-                var boxGroup = boxInfoList.GroupBy(m => m.BoxNo).ToList();
-                foreach (var g in boxGroup)
-                {
-                    var boxFullQty = 0;//绠卞唴鎬绘暟閲�
-                    foreach (var box in g)
-                    {
-                        if (box.BindNo != null && box.BindNo != 0)
-                        {
-                            continue;
-                        }
-                        //绠卞唴鐗╂枡鎵规涓庡崟鎹槑缁嗕笉绗﹀悎
-                        if (box.SkuNo != detail.SkuNo || box.LotNo != detail.LotNo)
-                        {
-                            throw new Exception($"-1:{box.BoxNo}绠卞唴鐗╂枡鍙婃壒娆′笌鍗曟嵁涓嶄竴鑷达紝璇锋牳瀹�!");
-                        }
-
-                        box.ASNNo = model.AsnNo;
-                        box.ASNDetailNo = model.AsnDetailId;
-                        box.BindNo = bindId;
-                        box.PalletNo = model.PalletNo;
-                        box.Status = "1";
-                        box.CompleteTime = DateTime.Now;
-                        box.UpdateTime = DateTime.Now;
-                        box.UpdateUser = userId;
-
-                        factQty += box.Qty;
-                        boxFullQty += box.Qty;
-                    }
-                    if (boxFullQty > bNum)
-                    {
-                        throw new Exception($"缁戝畾澶辫触锛寋g.Key}绠辩爜缁戝畾鏁伴噺澶т簬璇ョ墿鍝佸寘瑁呮暟閲忥紒");
-                    }
-                }
-                Db.Updateable(boxInfoList).ExecuteCommand();
-
-                // 鏇存柊鎵樼洏缁戝畾琛�
-                bind.Qty += factQty;
-                if (bind.FullQty < bind.Qty)
-                {
-                    throw new Exception("鎵樼洏缁戝畾鏁伴噺宸茶秴鍑鸿鐗╂枡鍖呰鏁伴噺");
-                }
-
-                if (bind.FullQty == bind.Qty)
-                {
-                    bind.BitPalletMark = "0";
-                }
-                Db.Updateable(bind).Where(m => m.Id == bindId).ExecuteCommand();
-
-                // 鏇存敼鍏ュ簱鍗曟槑缁嗗凡缁勬暟閲�
-                var sqlString = string.Empty;
-                sqlString += $"update BllArrivalNoticeDetail set FactQty = FactQty + '{factQty}' where id = '{model.AsnDetailId}';";
-                Db.Ado.ExecuteCommand(sqlString);
-
-                // 鏇存敼鍏ュ簱鍗曞強鍏ュ簱鏄庣粏鐘舵��
-                if (detail.Status == "0")
-                {
-                    var sqlString2 = string.Empty;
-                    sqlString2 += $"update BllArrivalNotice set Status = '1',CompleteTime= getDate() where ASNNo = '{model.AsnNo}' and Status ='0';";
-                    sqlString2 += $"update BllArrivalNoticeDetail set Status = '1',CompleteTime= getDate() where id = '{model.AsnDetailId}' and Status ='0';";
-                    Db.Ado.ExecuteCommand(sqlString2);
-                }
-
-                // 鏇存敼鎵樼洏浣跨敤鐘舵��
-                var sqlStr = $"update SysPallets set Status = '1' where PalletNo = '{model.PalletNo}';";
-                //娣诲姞鎵樼洏璁板綍琛ㄦ暟鎹�
-                sqlStr += $"insert into LogPalletTrack values('{model.PalletNo}','{model.AsnNo}','缁勭洏','0',getDate(),{userId},NULL,NULL);";
-                Db.Ado.ExecuteCommand(sqlStr);
-                new OperationASNServer().AddLogOperationAsn("PDA妯″潡", "浜у搧缁勬墭", model.AsnNo, "娣诲姞", $"娣诲姞浜嗘墭鐩樼爜涓猴細{model.PalletNo}銆亄msgStr}鐨勭粍鐩樹俊鎭�", userId);
-
-                Db.CommitTran();
-
-            }
-            catch (Exception ex)
-            {
-                Db.Ado.RollbackTran();
                 throw ex;
             }
         }
@@ -735,9 +488,6 @@
         // 骞冲簱瀹屾垚鍏ュ簱鎿嶄綔
         public string CompleteInStock(PalletBindVm model)
         {
-            //string sttr = "select * from BllArrivalNoticeDetail where id in (select ASNDetailNo from BllBoxInfo where ASNNo = 'ASN2023042400002' and PalletNo = 'T2300004' and Status = '1');";
-            //var noticeDetaila = Db.Ado.SqlQuery<BllArrivalNoticeDetail>(sttr);
-
             string strMsg = "";
             string sqlString = string.Empty;
             try
@@ -780,6 +530,15 @@
                         }
                     }
                     iscount = 1; //鍥炴祦鍏ュ簱
+                }
+                #endregion
+
+                #region 鍦扮爜淇℃伅(鍌ㄤ綅淇℃伅)
+                var storageLocat = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == model.LocatNo && w.Status == "0");
+                if (storageLocat == null)
+                {
+                    strMsg = "-1:鍌ㄤ綅淇℃伅涓嶅瓨鍦ㄦ垨闈炵┖闂茬姸鎬侊紝璇锋牳鏌�!";
+                    return strMsg;
                 }
                 #endregion
 
@@ -847,17 +606,6 @@
                         return strMsg;
                     }
                 }
-                #endregion
-
-                #region 浠诲姟
-                //var task = Db.Queryable<LogTask>().First(t => t.IsDel == "0" && t.TaskNo == palletbind.TaskNo && t.Status == "0" || t.Status == "1");
-                ////楠岃瘉浠诲姟淇℃伅鏄惁瀛樺湪
-                //if (task == null)
-                //{
-                //    strMsg = "-1:浠诲姟淇℃伅涓嶅瓨鍦紝璇锋牳鏌�!";
-                //    return strMsg;
-                //}
-
                 #endregion
 
                 #endregion
@@ -1731,5 +1479,632 @@
                 throw ex;
             }
         }
+
+        #region 浜у搧缁勬墭
+        /// <summary>
+        /// 鏍规嵁绠辩爜鑾峰彇鏍囩绠辩爜淇℃伅
+        /// </summary>
+        /// <param name="model"></param>
+        /// <returns></returns>
+        public List<BoxInfoDto> GetBindBoxInfos(BoxInfoVm model)
+        {
+            try
+            {
+                if (string.IsNullOrEmpty(model.ASNNo))
+                {
+                    throw new Exception("璇烽�夋嫨鍏ュ簱鍗曟嵁!");
+                }
+                if (string.IsNullOrEmpty(model.BoxNo))
+                {
+                    throw new Exception("绠辩爜涓嶈兘涓虹┖!");
+                }
+                var order = Db.Queryable<BllArrivalNotice>().First(a => a.IsDel == "0" && a.ASNNo == model.ASNNo);
+                if (order == null)
+                {
+                    throw new Exception("鍏ュ簱鍗曟嵁涓嶅瓨鍦�!");
+                }
+                string sqlString = string.Empty;
+                //鍏ュ簱鍗曠被鍨嬶紝0锛氭垚鍝佸叆搴� 1锛氶噰璐叆搴� 2锛氫腑闂村搧鍏ュ簱 3锛氶��璐у叆搴� 4锛氳溅闂翠綑鏂欓��鍥炲叆搴� 5锛氬叾瀹冨叆搴� 6锛氫唬鍌ㄥ叆搴�
+                if (order.Type == "0" || order.Type == "3" || order.Type == "5" || order.Type == "6")
+                {
+                    sqlString = "select BoxNo, SkuNo,SkuName, LotNo, SUM(Qty) as Qty from BllBoxInfo where IsDel = '0' ";
+                    sqlString += $"and BoxNo = '{model.BoxNo}' and Status='0' ";
+                    sqlString += $"group by BoxNo,SkuNo,SkuName,LotNo; ";
+                }
+                else
+                {                    
+                    sqlString = "select BoxNo, SkuNo,SkuName, LotNo, SUM(Qty) as Qty from BLLLabelBoxNo where IsDel = '0' ";
+                    sqlString += $"and BoxNo = '{model.BoxNo}' and IsUse='0' ";
+                    sqlString += $"group by BoxNo,SkuNo,SkuName,LotNo; ";                    
+                }
+                var models = Db.Ado.SqlQuery<BoxInfoDto>(sqlString);
+                if (models == null)
+                {
+                    throw new Exception("绠辩爜淇℃伅涓嶅瓨鍦�!");
+                }
+                return models;
+            }
+            catch (Exception ex)
+            {
+                throw ex;
+            }
+        }
+
+        public void BindPallet(PdaPalletBindVm model, int userId, string origin)
+        {
+            try
+            {
+                #region 鍒ゆ柇
+
+                if (string.IsNullOrEmpty(model.AsnNo))
+                {
+                    throw new Exception("-1:鍗曟嵁鍙蜂笉鍙负绌�!");
+                }
+                if (model.AsnDetailId == null || model.AsnDetailId == 0)
+                {
+                    throw new Exception("-1:鐗╂枡涓嶅彲涓虹┖!");
+                }
+                if (string.IsNullOrEmpty(model.PalletNo))
+                {
+                    throw new Exception("-1:鎵樼洏鍙蜂笉鍙负绌�!");
+                }
+                if (string.IsNullOrEmpty(model.BoxNo))
+                {
+                    throw new Exception("-1:绠辩爜淇℃伅涓嶅彲涓虹┖!");
+                }
+
+                if (model.IsContinue == "1")
+                {
+                    if (string.IsNullOrWhiteSpace(model.TailBoxNo))
+                    {
+                        throw new Exception("-1:寮�鍚繛缁粍鎵樻椂锛屽熬绠辩爜淇℃伅涓嶅彲涓虹┖!");
+                    }
+
+                }
+                #endregion
+                // 楠岃瘉鍏ュ簱鍗曟�昏〃
+                var notice = Db.Queryable<BllArrivalNotice>().First(m => m.IsDel == "0" && m.ASNNo == model.AsnNo);
+                if (notice == null)
+                {
+                    throw new Exception("-1:褰撳墠鐗╂枡鍙婃壒娆′笌鍗曟嵁鏃犲叧鑱旓紝璇锋牳瀹�!");
+                }
+                //鍏ュ簱鍗曠被鍨嬶紝0锛氭垚鍝佸叆搴� 1锛氶噰璐叆搴� 2锛氫腑闂村搧鍏ュ簱 3锛氶��璐у叆搴� 4锛氳溅闂翠綑鏂欓��鍥炲叆搴� 5锛氬叾瀹冨叆搴� 6锛氫唬鍌ㄥ叆搴�
+                if (notice.Type == "0" || notice.Type == "3" || notice.Type == "5" || notice.Type == "6")
+                {
+                    BindPalletForBox(model,userId,origin);
+                }
+                else
+                {
+                    BindPalletForLabelBox(model,userId);
+                }
+            }
+            catch (Exception ex)
+            {
+                throw ex;
+            }
+        }
+        // 缁戝畾鐗╂枡鎵樼洏
+        private void BindPalletForBox(PdaPalletBindVm model, int userId, string origin)
+        {
+            try
+            {
+                Db.BeginTran();
+
+                //鎵樼洏鏄惁瀛樺湪
+                var pallet = Db.Queryable<SysPallets>().First(m => m.IsDel == "0" && m.PalletNo == model.PalletNo);
+                if (pallet == null)
+                {
+                    throw new Exception("鏈煡璇㈠埌鎵樼洏淇℃伅锛岃鏍稿疄锛�");
+                }
+                // 楠岃瘉鍏ュ簱鍗曟槑缁嗘槸鍚﹀瓨鍦�
+                var detail = Db.Queryable<BllArrivalNoticeDetail>().First(m => m.IsDel == "0" && m.Id == model.AsnDetailId && m.ASNNo == model.AsnNo);
+                if (detail == null)
+                {
+                    throw new Exception("-1:褰撳墠鐗╂枡鍙婃壒娆′笌鍗曟嵁鏃犲叧鑱旓紝璇锋牳瀹�!");
+                }
+                #region 鍖呰
+                var package = Db.Queryable<SysPackag>().Where(m => m.IsDel == "0");
+                var sku = Db.Queryable<SysMaterials>().First(m => m.IsDel == "0" && m.SkuNo == detail.SkuNo);
+                var pack = package.First(m => m.IsDel == "0" && m.PackagNo == sku.PackagNo);
+                if (pack == null)
+                {
+                    throw new Exception("-1:鑾峰彇鐗╂枡鍖呰澶辫触锛岃鏍稿疄!");
+                }
+                var pNum = 0;//鎵樼洏鐗╁搧鏁伴噺 
+                var bNum = 0;//绠辩爜鐗╁搧鏁伴噺 
+                if (pack.L5Num.HasValue)
+                {
+                    pNum = (int)pack.L5Num;
+                    bNum = (int)pack.L4Num;
+                }
+                else if (pack.L4Num.HasValue)
+                {
+                    pNum = (int)pack.L4Num;
+                    bNum = (int)pack.L3Num;
+                }
+                else if (pack.L3Num.HasValue)
+                {
+                    pNum = (int)pack.L3Num;
+                    bNum = (int)pack.L2Num;
+                }
+                else if (pack.L2Num.HasValue)
+                {
+                    pNum = (int)pack.L2Num;
+                    bNum = (int)pack.L1Num;
+                }
+                else if (pack.L1Num.HasValue)
+                {
+                    pNum = (int)pack.L1Num;
+                    bNum = (int)pack.L1Num;
+                }
+                if (pNum == 0 || bNum == 0)
+                {
+                    throw new Exception($"缁戝畾澶辫触锛寋detail.SkuNo}鐗╁搧鍖呰鏈壘鍒帮紒");
+                }
+                #endregion
+
+                #region 楠岃瘉鏄惁鍏佽绔嬪簱鍚屾墭鐩樹笉鍚岀墿鏂欏叆搴撴垨鍚屾墭鐩樺悓鐗╂枡涓嶅悓鎵规鍏ュ簱
+                var palletBind = Db.Queryable<BllPalletBind>().First(m => m.IsDel == "0" && m.ASNNo == model.AsnNo && m.PalletNo == model.PalletNo && m.ASNDetailNo != model.AsnDetailId);
+                if (palletBind != null)
+                {
+                    var box = Db.Queryable<BllBoxInfo>().First(m => m.IsDel == "0" && m.BindNo == palletBind.Id && m.BitBoxMark == "0");
+                    if (box != null && (box.SkuNo != detail.SkuNo || box.LotNo != detail.LotNo))
+                    {
+                        var funSetting = Db.Queryable<SysFunSetting>().First(a => a.IsDel == "0" && a.FunSetNo == "Fun045");
+                        if (funSetting == null || funSetting.IsEnable == "OFF")
+                        {
+                            throw new Exception($"涓嶅厑璁哥珛搴撳悓鎵樼洏涓嶅悓鐗╂枡鍏ュ簱鎴栧悓鎵樼洏涓嶅悓鎵规鍏ュ簱锛�");
+                        }
+                    }
+                }
+                #endregion
+                var bind = Db.Queryable<BllPalletBind>().First(m => m.IsDel == "0" && m.ASNDetailNo == model.AsnDetailId && m.PalletNo == model.PalletNo);
+                var bindId = 0;
+                if (bind == null)
+                {
+                    bind = new BllPalletBind
+                    {
+                        ASNNo = model.AsnNo,
+                        ASNDetailNo = (int)model.AsnDetailId,
+                        PalletNo = model.PalletNo,
+                        PalletNo2 = "",
+                        PalletNo3 = "",
+                        Qty = 0,
+                        FullQty = pNum,
+                        Status = "0",
+                        Type = "0",
+                        LotNo = detail.LotNo,
+                        LotText = detail.LotText,
+                        SupplierLot = "",
+                        InspectMark = "0",
+                        BitPalletMark = "1",
+                        IsBale = "0",
+                        IsBelt = "0",
+                        CreateUser = userId
+                    };
+                    // 鎻掑叆鎵樼洏缁戝畾琛�
+                    bindId = Db.Insertable(bind).ExecuteReturnIdentity();
+                }
+                else
+                {
+                    if (bind.Status != "0")
+                    {
+                        throw new Exception("-1:褰撳墠鎵樼洏姝e湪鎵ц涓紝缁戝畾澶辫触锛岃鏍稿疄!");
+                    }
+                    bindId = bind.Id;
+                }
+
+                #region 绠辩爜淇℃伅
+
+                var msgStr = $"绠卞彿涓簕model.BoxNo}";
+                var boxInfoList = new List<BllBoxInfo>();
+                //棣栫
+                var boxInfo = Db.Queryable<BllBoxInfo>().Where(m => m.IsDel == "0" && m.BoxNo == model.BoxNo && m.Status == "0").ToList();
+                if (boxInfo.Count == 0)
+                {
+                    throw new Exception("-1:绠辩爜淇℃伅涓嶅瓨鍦�!");
+                }
+                boxInfoList.AddRange(boxInfo);
+                //鏄惁杩炵画缁勬墭
+                if (model.IsContinue == "1")
+                {
+                    //灏剧
+                    var boxInfo2 = Db.Queryable<BllBoxInfo>().Where(m => m.IsDel == "0" && m.BoxNo == model.TailBoxNo && m.Status == "0").ToList();
+                    if (boxInfo2.Count == 0)
+                    {
+                        throw new Exception("-1:灏剧鐮佷俊鎭笉瀛樺湪!");
+                    }
+                    boxInfoList.AddRange(boxInfo2);
+                    var sql = $"select * from BllBoxInfo where IsDel = '0' and Status = '0' and boxNo>'{model.BoxNo}' and boxNo<'{model.TailBoxNo}'; ";
+                    var list = Db.Ado.SqlQuery<BllBoxInfo>(sql);
+                    boxInfoList.AddRange(list);
+                    msgStr += $"灏剧鍙蜂负{model.TailBoxNo}";
+                }
+                #endregion
+
+                // 鏇存敼绠辨敮鍏崇郴琛�
+                var factQty = 0;//鎵樼洏鎬绘暟閲�
+                var boxGroup = boxInfoList.GroupBy(m => m.BoxNo).ToList();
+                foreach (var g in boxGroup)
+                {
+                    var boxFullQty = 0;//绠卞唴鎬绘暟閲�
+                    foreach (var box in g)
+                    {
+                        if (box.BindNo != null && box.BindNo != 0)
+                        {
+                            continue;
+                        }
+                        //绠卞唴鐗╂枡鎵规涓庡崟鎹槑缁嗕笉绗﹀悎
+                        if (box.SkuNo != detail.SkuNo || box.LotNo != detail.LotNo)
+                        {
+                            throw new Exception($"-1:{box.BoxNo}绠卞唴鐗╂枡鍙婃壒娆′笌鍗曟嵁涓嶄竴鑷达紝璇锋牳瀹�!");
+                        }
+
+                        box.ASNNo = model.AsnNo;
+                        box.ASNDetailNo = model.AsnDetailId;
+                        box.BindNo = bindId;
+                        box.PalletNo = model.PalletNo;
+                        box.Status = "1";
+                        box.CompleteTime = DateTime.Now;
+                        box.UpdateTime = DateTime.Now;
+                        box.UpdateUser = userId;
+
+                        factQty += box.Qty;
+                        boxFullQty += box.Qty;
+                    }
+                    if (boxFullQty > bNum)
+                    {
+                        throw new Exception($"缁戝畾澶辫触锛寋g.Key}绠辩爜缁戝畾鏁伴噺澶т簬璇ョ墿鍝佸寘瑁呮暟閲忥紒");
+                    }
+                }
+                Db.Updateable(boxInfoList).ExecuteCommand();
+
+                // 鏇存柊鎵樼洏缁戝畾琛�
+                bind.Qty += factQty;
+                if (bind.FullQty < bind.Qty)
+                {
+                    throw new Exception("鎵樼洏缁戝畾鏁伴噺宸茶秴鍑鸿鐗╂枡鍖呰鏁伴噺");
+                }
+
+                if (bind.FullQty == bind.Qty)
+                {
+                    bind.BitPalletMark = "0";
+                }
+                Db.Updateable(bind).Where(m => m.Id == bindId).ExecuteCommand();
+
+                // 鏇存敼鍏ュ簱鍗曟槑缁嗗凡缁勬暟閲�
+                var sqlString = string.Empty;
+                sqlString += $"update BllArrivalNoticeDetail set FactQty = FactQty + '{factQty}' where id = '{model.AsnDetailId}';";
+                Db.Ado.ExecuteCommand(sqlString);
+
+                // 鏇存敼鍏ュ簱鍗曞強鍏ュ簱鏄庣粏鐘舵��
+                if (detail.Status == "0")
+                {
+                    var sqlString2 = string.Empty;
+                    sqlString2 += $"update BllArrivalNotice set Status = '1',CompleteTime= getDate() where ASNNo = '{model.AsnNo}' and Status ='0';";
+                    sqlString2 += $"update BllArrivalNoticeDetail set Status = '1',CompleteTime= getDate() where id = '{model.AsnDetailId}' and Status ='0';";
+                    Db.Ado.ExecuteCommand(sqlString2);
+                }
+
+                // 鏇存敼鎵樼洏浣跨敤鐘舵��
+                var sqlStr = $"update SysPallets set Status = '1' where PalletNo = '{model.PalletNo}';";
+                //娣诲姞鎵樼洏璁板綍琛ㄦ暟鎹�
+                sqlStr += $"insert into LogPalletTrack values('{model.PalletNo}','{model.AsnNo}','缁勭洏','0',getDate(),{userId},NULL,NULL);";
+                Db.Ado.ExecuteCommand(sqlStr);
+                new OperationASNServer().AddLogOperationAsn("PDA妯″潡", "浜у搧缁勬墭", model.AsnNo, "娣诲姞", $"娣诲姞浜嗘墭鐩樼爜涓猴細{model.PalletNo}銆亄msgStr}鐨勭粍鐩樹俊鎭�", userId);
+
+                Db.CommitTran();
+
+            }
+            catch (Exception ex)
+            {
+                Db.Ado.RollbackTran();
+                throw ex;
+            }
+        }
+        // 缁戝畾鐗╂枡鎵樼洏
+        public void BindPalletForLabelBox(PdaPalletBindVm model, int userId)
+        {
+            try
+            {
+                #region 鍒ゆ柇
+
+                if (string.IsNullOrEmpty(model.AsnNo))
+                {
+                    throw new Exception("-1:鍗曟嵁鍙蜂笉鍙负绌�!");
+                }
+                if (model.AsnDetailId == null || model.AsnDetailId == 0)
+                {
+                    throw new Exception("-1:鐗╂枡涓嶅彲涓虹┖!");
+                }
+                if (string.IsNullOrEmpty(model.PalletNo))
+                {
+                    throw new Exception("-1:鎵樼洏鍙蜂笉鍙负绌�!");
+                }
+                if (string.IsNullOrEmpty(model.BoxNo))
+                {
+                    throw new Exception("-1:绠辩爜淇℃伅涓嶅彲涓虹┖!");
+                }
+
+                if (model.IsContinue == "1")
+                {
+                    if (string.IsNullOrWhiteSpace(model.TailBoxNo))
+                    {
+                        throw new Exception("-1:寮�鍚繛缁粍鎵樻椂锛屽熬绠辩爜淇℃伅涓嶅彲涓虹┖!");
+                    }
+
+                }
+                if (model.IsContinue == "0" && model.IsContinue2 == "1")
+                {
+                    if (string.IsNullOrWhiteSpace(model.TailBoxNo01))
+                    {
+                        throw new Exception("-1:寮�鍚繛缁粍鎵樻椂锛屼簩绾х爜灏剧爜淇℃伅涓嶅彲涓虹┖!");
+                    }
+
+                }
+                #endregion
+
+                Db.BeginTran();
+
+                //鎵樼洏鏄惁瀛樺湪
+                var pallet = Db.Queryable<SysPallets>().First(m => m.IsDel == "0" && m.PalletNo == model.PalletNo);
+                if (pallet == null)
+                {
+                    throw new Exception("鏈煡璇㈠埌鎵樼洏淇℃伅锛岃鏍稿疄锛�");
+                }
+                // 楠岃瘉鍏ュ簱鍗曟槑缁嗘槸鍚﹀瓨鍦�
+                var detail = Db.Queryable<BllArrivalNoticeDetail>().First(m => m.IsDel == "0" && m.Id == model.AsnDetailId && m.ASNNo == model.AsnNo);
+                if (detail == null)
+                {
+                    throw new Exception("-1:褰撳墠鐗╂枡鍙婃壒娆′笌鍗曟嵁鏃犲叧鑱旓紝璇锋牳瀹�!");
+                }
+                #region 鍖呰
+                var package = Db.Queryable<SysPackag>().Where(m => m.IsDel == "0");
+                var sku = Db.Queryable<SysMaterials>().First(m => m.IsDel == "0" && m.SkuNo == detail.SkuNo);
+                var pack = package.First(m => m.IsDel == "0" && m.PackagNo == sku.PackagNo);
+                if (pack == null)
+                {
+                    throw new Exception("-1:鑾峰彇鐗╂枡鍖呰澶辫触锛岃鏍稿疄!");
+                }
+                var pNum = 0;//鎵樼洏鐗╁搧鏁伴噺 
+                var bNum = 0;//绠辩爜鐗╁搧鏁伴噺 
+                if (pack.L5Num.HasValue)
+                {
+                    pNum = (int)pack.L5Num;
+                    bNum = (int)pack.L4Num;
+                }
+                else if (pack.L4Num.HasValue)
+                {
+                    pNum = (int)pack.L4Num;
+                    bNum = (int)pack.L3Num;
+                }
+                else if (pack.L3Num.HasValue)
+                {
+                    pNum = (int)pack.L3Num;
+                    bNum = (int)pack.L2Num;
+                }
+                else if (pack.L2Num.HasValue)
+                {
+                    pNum = (int)pack.L2Num;
+                    bNum = (int)pack.L1Num;
+                }
+                else if (pack.L1Num.HasValue)
+                {
+                    pNum = (int)pack.L1Num;
+                    bNum = (int)pack.L1Num;
+                }
+                if (pNum == 0 || bNum == 0)
+                {
+                    throw new Exception($"缁戝畾澶辫触锛寋detail.SkuNo}鐗╁搧鍖呰鏈壘鍒帮紒");
+                }
+                #region 楠岃瘉鏄惁鍏佽绔嬪簱鍚屾墭鐩樹笉鍚岀墿鏂欏叆搴撴垨鍚屾墭鐩樺悓鐗╂枡涓嶅悓鎵规鍏ュ簱
+                var palletBind = Db.Queryable<BllPalletBind>().First(m => m.IsDel == "0" && m.ASNNo == model.AsnNo && m.PalletNo == model.PalletNo && m.ASNDetailNo != model.AsnDetailId);
+                if (palletBind != null)
+                {
+                    var box = Db.Queryable<BllBoxInfo>().First(m => m.IsDel == "0" && m.BindNo == palletBind.Id && m.BitBoxMark == "0");
+                    if (box != null && (box.SkuNo != detail.SkuNo || box.LotNo != detail.LotNo))
+                    {
+                        var funSetting = Db.Queryable<SysFunSetting>().First(a => a.IsDel == "0" && a.FunSetNo == "Fun045");
+                        if (funSetting == null || funSetting.IsEnable == "OFF")
+                        {
+                            throw new Exception($"涓嶅厑璁哥珛搴撳悓鎵樼洏涓嶅悓鐗╂枡鍏ュ簱鎴栧悓鎵樼洏涓嶅悓鎵规鍏ュ簱锛�");
+                        }
+                    }
+                }
+                #endregion
+                var bind = Db.Queryable<BllPalletBind>().First(m => m.IsDel == "0" && m.ASNDetailNo == model.AsnDetailId && m.PalletNo == model.PalletNo);
+                var bindId = 0;
+                if (bind == null)
+                {
+                    bind = new BllPalletBind
+                    {
+                        ASNNo = model.AsnNo,
+                        ASNDetailNo = (int)model.AsnDetailId,
+                        PalletNo = model.PalletNo,
+                        PalletNo2 = "",
+                        PalletNo3 = "",
+                        Qty = 0,
+                        FullQty = pNum,
+                        Status = "0",
+                        Type = "0",
+                        LotNo = detail.LotNo,
+                        LotText = detail.LotText,
+                        SupplierLot = "",
+                        InspectMark = "0",
+                        BitPalletMark = "1",
+                        IsBale = "0",
+                        IsBelt = "0",
+                        CreateUser = userId
+                    };
+                    // 鎻掑叆鎵樼洏缁戝畾琛�
+                    bindId = Db.Insertable(bind).ExecuteReturnIdentity();
+                }
+                else
+                {
+                    if (bind.Status != "0")
+                    {
+                        throw new Exception("-1:褰撳墠鎵樼洏姝e湪鎵ц涓紝缁戝畾澶辫触锛岃鏍稿疄!");
+                    }
+                    bindId = bind.Id;
+                }
+
+                #region 绠辩爜淇℃伅
+
+                var msgStr = $"绠卞彿涓簕model.BoxNo}";
+                var boxInfoList = new List<BllLabelBoxNo>();
+                //棣栫
+                var boxInfo = Db.Queryable<BllLabelBoxNo>().Where(m => m.IsDel == "0" && m.BoxNo == model.BoxNo && m.IsUse == "0" && m.Level == "1").ToList();
+                if (boxInfo.Count == 0)
+                {
+                    throw new Exception("-1:绠辩爜淇℃伅涓嶅瓨鍦�!");
+                }                
+                //鏄惁杩炵画缁勬墭
+                if (model.IsContinue == "1")
+                {
+                    boxInfoList.AddRange(boxInfo);
+                    //灏剧
+                    var boxInfo2 = Db.Queryable<BllLabelBoxNo>().Where(m => m.IsDel == "0" && m.BoxNo == model.TailBoxNo && m.IsUse == "0" && m.Level == "1").ToList();
+                    if (boxInfo2.Count == 0)
+                    {
+                        throw new Exception("-1:灏剧鐮佷俊鎭笉瀛樺湪!");
+                    }
+                    boxInfoList.AddRange(boxInfo2);
+                    var sql = $"select * from BllLabelBoxNo where IsDel = '0' and IsUse = '0' and boxNo>'{model.BoxNo}' and boxNo<'{model.TailBoxNo}' and Level='1'; ";
+                    var list = Db.Ado.SqlQuery<BllLabelBoxNo>(sql);
+                    boxInfoList.AddRange(list);
+
+                    //鎵惧埌鐩稿叧鐨勪簩绾х爜
+                    foreach (var item in boxInfoList)
+                    {
+                        sql = $"select * from BllLabelBoxNo  where IsDel = '0' and IsUse = '0' and ParentBoxNo='{item.BoxNo}' and Level = '2' ";
+                        list = Db.Ado.SqlQuery<BllLabelBoxNo>(sql);
+                        boxInfoList.AddRange(list);
+                    }
+                    msgStr += $"灏剧鍙蜂负{model.TailBoxNo}";
+                }
+                else
+                {
+                    var boxInfoList2 = new List<BllLabelBoxNo>();//鎵�鏈変簩绾х爜
+                    decimal boxQty = 0.00m;//绠卞唴浜岀骇鐮佹暟閲�
+                    if (!string.IsNullOrEmpty(model.BoxNo01))
+                    {
+                        //浜岀骇鐮侀鐮�
+                        var box01Info = Db.Queryable<BllLabelBoxNo>().Where(m => m.IsDel == "0" && m.BoxNo == model.BoxNo01 && m.IsUse == "0" && m.Level == "2" && (string.IsNullOrEmpty(m.ParentBoxNo) || m.ParentBoxNo == model.BoxNo)).ToList();
+                        if (box01Info.Count == 0)
+                        {
+                            throw new Exception("-1:浜岀骇鐮侀鐮佷俊鎭笉瀛樺湪!");
+                        }
+                        boxInfoList2.AddRange(box01Info);
+                        if (model.IsContinue2 == "1")
+                        {
+                            //浜岀骇鐮佸熬鐮�
+                            var box01Info2 = Db.Queryable<BllLabelBoxNo>().Where(m => m.IsDel == "0" && m.BoxNo == model.TailBoxNo01 && m.IsUse == "0" && m.Level == "2" && (string.IsNullOrEmpty(m.ParentBoxNo) || m.ParentBoxNo == model.BoxNo)).ToList();
+                            if (box01Info2.Count == 0)
+                            {
+                                throw new Exception("-1:浜岀骇鐮佸熬鐮佷俊鎭笉瀛樺湪!");
+                            }
+                            boxInfoList2.AddRange(box01Info2);
+                            //涓棿鐮�
+                            var sql = $"select * from BllLabelBoxNo where IsDel = '0' and IsUse = '0' and boxNo>'{model.BoxNo}' and boxNo<'{model.TailBoxNo}' and Level='2' and (ParentBoxNo='' || ParentBoxNo='{model.BoxNo}'); ";
+                            var list = Db.Ado.SqlQuery<BllLabelBoxNo>(sql);                            
+                            boxInfoList2.AddRange(list);                            
+                        }
+                        foreach (var item in boxInfoList2)
+                        {
+                            item.ParentBoxNo = model.BoxNo;
+                            boxQty += item.Qty;
+                        }
+                        boxInfoList.AddRange(boxInfoList2);
+                        foreach (var item in boxInfo)
+                        {
+                            item.Qty = boxQty;
+                        }
+                    }
+                    boxInfoList.AddRange(boxInfo);
+                }
+                #endregion
+                #endregion
+                // 鏇存敼绠辨敮鍏崇郴琛�
+                decimal factQty = 0.00m;//鎵樼洏鎬绘暟閲�
+                var boxGroup = boxInfoList.GroupBy(m => m.BoxNo).ToList();
+                foreach (var g in boxGroup)
+                {
+                    decimal boxFullQty = 0.00m;//绠卞唴鎬绘暟閲�
+                    foreach (var box in g)
+                    {
+                        if (box.IsUse != "0")
+                        {
+                            continue;
+                        }
+                        //绠卞唴鐗╂枡鎵规涓庡崟鎹槑缁嗕笉绗﹀悎
+                        if (box.SkuNo != detail.SkuNo || box.LotNo != detail.LotNo)
+                        {
+                            throw new Exception($"-1:{box.BoxNo}绠卞唴鐗╂枡鍙婃壒娆′笌鍗曟嵁涓嶄竴鑷达紝璇锋牳瀹�!");
+                        }
+
+                        box.ASNNo = model.AsnNo;
+                        box.ASNDetailNo = model.AsnDetailId;
+                        box.IsUse = "1";
+                        box.CompleteTime = DateTime.Now;
+                        box.UpdateTime = DateTime.Now;
+                        box.UpdateUser = userId;
+
+                        if (box.ParentBoxNo == "" && box.Level == "1")
+                        {
+                            factQty += box.Qty;
+                            boxFullQty += box.Qty;
+                        }                        
+                    }
+                    if (boxFullQty > bNum)
+                    {
+                        throw new Exception($"缁戝畾澶辫触锛寋g.Key}绠辩爜缁戝畾鏁伴噺澶т簬璇ョ墿鍝佸寘瑁呮暟閲忥紒");
+                    }
+                }
+                Db.Updateable(boxInfoList).ExecuteCommand();
+
+                // 鏇存柊鎵樼洏缁戝畾琛�
+                bind.Qty += factQty;
+                if (bind.FullQty < bind.Qty)
+                {
+                    throw new Exception("鎵樼洏缁戝畾鏁伴噺宸茶秴鍑鸿鐗╂枡鍖呰鏁伴噺");
+                }
+
+                if (bind.FullQty == bind.Qty)
+                {
+                    bind.BitPalletMark = "0";
+                }
+                Db.Updateable(bind).Where(m => m.Id == bindId).ExecuteCommand();
+
+                // 鏇存敼鍏ュ簱鍗曟槑缁嗗凡缁勬暟閲�
+                var sqlString = string.Empty;
+                sqlString += $"update BllArrivalNoticeDetail set FactQty = FactQty + '{factQty}' where id = '{model.AsnDetailId}';";
+                Db.Ado.ExecuteCommand(sqlString);
+
+                // 鏇存敼鍏ュ簱鍗曞強鍏ュ簱鏄庣粏鐘舵��
+                if (detail.Status == "0")
+                {
+                    var sqlString2 = string.Empty;
+                    sqlString2 += $"update BllArrivalNotice set Status = '1',CompleteTime= getDate() where ASNNo = '{model.AsnNo}' and Status ='0';";
+                    sqlString2 += $"update BllArrivalNoticeDetail set Status = '1',CompleteTime= getDate() where id = '{model.AsnDetailId}' and Status ='0';";
+                    Db.Ado.ExecuteCommand(sqlString2);
+                }
+
+                // 鏇存敼鎵樼洏浣跨敤鐘舵��
+                var sqlStr = $"update SysPallets set Status = '1' where PalletNo = '{model.PalletNo}';";
+                //娣诲姞鎵樼洏璁板綍琛ㄦ暟鎹�
+                sqlStr += $"insert into LogPalletTrack values('{model.PalletNo}','{model.AsnNo}','缁勭洏','0',getDate(),{userId},NULL,NULL);";
+                Db.Ado.ExecuteCommand(sqlStr);
+                new OperationASNServer().AddLogOperationAsn("PDA妯″潡", "浜у搧缁勬墭", model.AsnNo, "娣诲姞", $"娣诲姞浜嗘墭鐩樼爜涓猴細{model.PalletNo}銆亄msgStr}鐨勭粍鐩樹俊鎭�", userId);
+
+                Db.CommitTran();
+
+            }
+            catch (Exception ex)
+            {
+                Db.Ado.RollbackTran();
+                throw ex;
+            }
+        }
+        #endregion
     }
 }

--
Gitblit v1.8.0