duheng
2024-10-22 a95dc42495472cf931db97f8ccbe9d075708aa0b
WinFrmUI/HStation.WinFrmUI.Basic/01-SysCatalogManage/SysCatalogManageMainPanel.cs
@@ -1,4 +1,13 @@
namespace HStation.WinFrmUI.Basic
using DevExpress.Utils.DragDrop;
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
{
    public partial class SysCatalogManageMainPanel : DocumentPage
    {
@@ -6,14 +15,17 @@
        {
            InitializeComponent();
            this.PageTitle.Caption = "分类管理";
            this.PageTitle.HeaderSvgImage = this.svgImage32[1];
            this.treeList1.InitialMultiColSettings();
            this.PageTitle.HeaderSvgImage = this.svgImage32[0];
            this.PageTitle.SvgImageSize = new Size(24, 24);
            this.typeTreeListCtrl2.FocusedChangedEvent += TypeTreeListCtrl2_FocusedChangedEvent;
        }
        private List<SysCatalogViewModel> _allBindingList = null;
        private List<SysCatalogViewModel> _allBindingList;
        private Yw.BLL.SysCatalog _bll = null;
        private Yw.BLL.SysCatalog _bll;
        private List<SysCatalogViewModel> _IndexList;
        public override void InitialDataSource()
        {
@@ -31,9 +43,10 @@
            {
                _allBindingList.Add(new SysCatalogViewModel(item));
            }
            _allBindingList = _allBindingList.OrderBy(x => x.SortCode).ToList();
            this.catlogViewModelBindingSource.DataSource = _allBindingList;
            this.catlogViewModelBindingSource.ResetBindings(false);
            this.catalogViewModelBindingSource.DataSource = _allBindingList;
            _IndexList = _allBindingList.Select(x => new SysCatalogViewModel(x)).ToList();
            this.catalogViewModelBindingSource.ResetBindings(false);
            this.treeList1.ExpandAll();
        }
        //添加
@@ -51,7 +64,7 @@
               {
                   var model = await _bll.GetByID(id);
                   _allBindingList.Add(new SysCatalogViewModel(model));
                   this.catlogViewModelBindingSource.ResetBindings(false);
                   this.catalogViewModelBindingSource.ResetBindings(false);
                   return true;
               }
               return false;
@@ -59,37 +72,10 @@
            dlg.ShowDialog();
        }
        //添加子项
        private void BtnAddChild_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var dlg = new AddSysCatalogDlg();
            var vm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (vm == null)
            {
                MessageBoxHelper.ShowWarning("请选择数据行!");
                return;
            }
            var typeID = this.typeTreeListCtrl2.GetCurrentID();
            dlg.SetBindingData(typeID, vm.ID);
            dlg.ReloadDataEvent += async (rhs) =>
            {
                var id = await _bll.Insert(rhs);
                if (id > 0)
                {
                    var model = await _bll.GetByID(id);
                    _allBindingList.Add(new SysCatalogViewModel(model));
                    this.catlogViewModelBindingSource.ResetBindings(false);
                    return true;
                }
                return false;
            };
            dlg.ShowDialog();
        }
        //编辑
        private void barBtnEditPumpCurve_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var dlg = new EditSysCatlaogDlg();
            var dlg = new EditSysCatalogDlg();
            var vm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (vm == null)
            {
@@ -102,7 +88,7 @@
                if (await _bll.Update(rhs))
                {
                    vm.Reset(rhs);
                    this.catlogViewModelBindingSource.ResetBindings(false);
                    this.catalogViewModelBindingSource.ResetBindings(false);
                    return true;
                }
                return false;
@@ -127,7 +113,7 @@
                if (result)
                {
                    _allBindingList.Remove(currentVm);
                    this.catlogViewModelBindingSource.ResetBindings(false);
                    this.catalogViewModelBindingSource.ResetBindings(false);
                    MessageBoxHelper.ShowSuccess($"删除成功!");
                }
                else
@@ -158,5 +144,124 @@
                return;
            }
        }
        /// <summary>
        ///设置父级
        /// </summary>
        private void barBtnUpdateParent_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (currentVm == null)
            {
                MessageBoxHelper.ShowWarning("请选择数据行!");
                return;
            }
            var dlg = new UpdateCatalogParentDlg();
            dlg.SetBindingData(currentVm.TypeID, currentVm.ID, currentVm.ParentID);
            dlg.ReloadEvent += async (parentId) =>
            {
                var bol = await _bll.UpdateTreeSortCode(currentVm.ID, parentId, 1);
                if (bol)
                {
                    this.InitialDataSource();
                    return true;
                }
                return false;
            };
            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)
            {
                this.treeList1.OptionsDragAndDrop.DragNodesMode = DragNodesMode.Single;
            }
            else
            {
                this.treeList1.OptionsDragAndDrop.DragNodesMode = DragNodesMode.None;
            }
        }
    }
}