From ad6ccae53244330399fe871d78209faa988219c3 Mon Sep 17 00:00:00 2001
From: duheng <2784771470@qq.com>
Date: 星期三, 23 十月 2024 16:00:00 +0800
Subject: [PATCH] 增加MatchingViewModel字段

---
 WinFrmUI/HStation.WinFrmUI.Basic/01-SysCatalogManage/SysCatalogManageMainPanel.cs |  115 +++++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 94 insertions(+), 21 deletions(-)

diff --git a/WinFrmUI/HStation.WinFrmUI.Basic/01-SysCatalogManage/SysCatalogManageMainPanel.cs b/WinFrmUI/HStation.WinFrmUI.Basic/01-SysCatalogManage/SysCatalogManageMainPanel.cs
index 08dba85..0e25282 100644
--- a/WinFrmUI/HStation.WinFrmUI.Basic/01-SysCatalogManage/SysCatalogManageMainPanel.cs
+++ b/WinFrmUI/HStation.WinFrmUI.Basic/01-SysCatalogManage/SysCatalogManageMainPanel.cs
@@ -1,5 +1,11 @@
 锘縰sing DevExpress.Utils.DragDrop;
-using DevExpress.XtraLayout.Customization.Behaviours;
+using DevExpress.XtraEditors;
+using DevExpress.XtraTreeList;
+using DevExpress.XtraTreeList.Handler;
+using DevExpress.XtraTreeList.Nodes;
+using SqlSugar;
+using System.ComponentModel;
+using System.Reflection;
 
 namespace HStation.WinFrmUI.Basic
 {
@@ -19,6 +25,8 @@
 
         private Yw.BLL.SysCatalog _bll;
 
+        private List<SysCatalogViewModel> _IndexList;
+
         public override void InitialDataSource()
         {
             this.typeTreeListCtrl2.SetBindingData();
@@ -35,8 +43,8 @@
             {
                 _allBindingList.Add(new SysCatalogViewModel(item));
             }
-            _allBindingList = _allBindingList.OrderBy(x => x.SortCode).ToList();
             this.catalogViewModelBindingSource.DataSource = _allBindingList;
+            _IndexList = _allBindingList.Select(x => new SysCatalogViewModel(x)).ToList();
             this.catalogViewModelBindingSource.ResetBindings(false);
             this.treeList1.ExpandAll();
         }
@@ -149,7 +157,7 @@
                 return;
             }
             var dlg = new UpdateCatalogParentDlg();
-            dlg.SetBindingData(currentVm.TypeID, currentVm.ID);
+            dlg.SetBindingData(currentVm.TypeID, currentVm.ID, currentVm.ParentID);
             dlg.ReloadEvent += async (parentId) =>
             {
                 var bol = await _bll.UpdateTreeSortCode(currentVm.ID, parentId, 1);
@@ -163,32 +171,97 @@
             dlg.ShowDialog();
         }
 
+        private TreeListNode currentDragNode = null;
+
+        private void treeList1_DragDrop(object sender, DragEventArgs e)
+        {
+            currentDragNode = (TreeListNode)e.Data.GetData(typeof(TreeListNode));
+            var currentvm = GetViewModel(currentDragNode);
+            var dragInsertPosition = AjustDirection(sender, e);
+            var targetNode = GetTargetNode(e);
+            var targetVm = GetViewModel(targetNode);
+            var currentIndex = _allBindingList.IndexOf(currentvm);  //鎿嶄綔鐩爣绱㈠紩
+            var targetIndex = _allBindingList.IndexOf(targetVm);  //鐩爣浣嶇疆绱㈠紩
+
+            if (dragInsertPosition == DragInsertPosition.AsChild || targetVm == null || currentvm == null)
+            {
+                e.Effect = DragDropEffects.None;
+                return;
+            }
+            if (dragInsertPosition == DragInsertPosition.After)
+            {
+                _IndexList.RemoveAt(currentIndex);
+                _IndexList.Insert(targetIndex + 1, currentvm);
+            }
+            else if (dragInsertPosition == DragInsertPosition.Before)
+            {
+                _IndexList.RemoveAt(currentIndex);
+                _IndexList.Insert(targetIndex - 1, currentvm);
+            }
+            if (currentvm.ParentID == targetVm.ParentID)
+            {
+                /*  var sortIndex = currentvm.SortCode;
+                  currentvm.SortCode = targetVm.SortCode;
+                  targetVm.SortCode = sortIndex;*/
+                SetTreeListSorter(currentvm.ParentID);
+                return;
+            }
+            e.Effect = DragDropEffects.None;
+        }
+
+        /// <summary>
+        /// 鑾峰彇鎷栧姩杩囩▼涓殑鏂瑰悜
+        /// </summary>
+        private DragInsertPosition AjustDirection(object sender, DragEventArgs e)
+        {
+            PropertyInfo pi = typeof(TreeList).GetProperty("Handler", BindingFlags.Instance | BindingFlags.NonPublic);
+            TreeListHandler handler = (TreeListHandler)pi.GetValue(this.treeList1, null);
+            return handler.StateData.DragInfo.DragInsertPosition;
+        }
+
+        private async void SetTreeListSorter(long parentId)
+        {
+            var sortList = new List<Yw.Vmo.Sorter>();
+            var targetList = _IndexList.Where(x => x.ParentID == parentId).ToList();
+            int i = 1;
+            foreach (var item in targetList)
+            {
+                sortList.Add(new Yw.Vmo.Sorter { ID = item.ID, SortCode = i });
+                i++;
+            }
+            if (!await _bll.UpdateSorter(sortList))
+            {
+                TipFormHelper.ShowError("淇敼鎺掑簭澶辫触!");
+            }
+            //  this.catalogViewModelBindingSource.ResetBindings(false);
+        }
+
+        private TreeListNode GetTargetNode(DragEventArgs e)
+        {
+            var point = new Point(e.X, e.Y);
+            var clientPoint = this.treeList1.PointToClient(point);
+            var targetNode = this.treeList1.GetNodeAt(clientPoint);
+            return targetNode;
+        }
+
+        private SysCatalogViewModel GetViewModel(TreeListNode node)
+        {
+            if (node == null)
+                return default;
+            var vm = this.treeList1.GetDataRecordByNode(node) as SysCatalogViewModel;
+            return vm;
+        }
+
         private void barCheckSorter_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
         {
             if (barCheckSorter.Checked)
             {
-                behaviorManager1.Attach<DragDropBehavior>(treeList1, behavior =>
-                {
-                    behavior.BeginDragDrop += Behavior_BeginDragDrop;
-                    behavior.EndDragDrop += Behavior_EndDragDrop;
-                });
+                this.treeList1.OptionsDragAndDrop.DragNodesMode = DragNodesMode.Single;
             }
             else
             {
-                behaviorManager1.Detach<DragDropBehavior>(treeList1);
+                this.treeList1.OptionsDragAndDrop.DragNodesMode = DragNodesMode.None;
             }
-        }
-
-        private void Behavior_BeginDragDrop(object sender, BeginDragDropEventArgs e)
-        {
-        }
-
-        private async void Behavior_EndDragDrop(object sender, EndDragDropEventArgs e)
-        {
-            foreach (var item in _allBindingList)
-            {
-            }
-            //   await _bll.update()
         }
     }
 }
\ No newline at end of file

--
Gitblit v1.9.3