Shuxia Ning
2024-12-17 907a1579fecf2c160852cf99b3ea77c08eb481cc
WinFrmUI/HStation.WinFrmUI.Assets.Core/22-cooling/02-main/AssetsCoolingMainMgrPage.cs
@@ -1,6 +1,8 @@
using DevExpress.Drawing;
using DevExpress.Utils.DragDrop;
using DevExpress.Utils.Svg;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList.Nodes;
using HStation.Vmo;
using Yw;
using Yw.WinFrmUI.Page;
@@ -32,6 +34,7 @@
                return;
            }
            _series = series;
            InitialData();
        }
        /// <summary>
@@ -46,24 +49,25 @@
        //初始化数据
        private async void InitialData()
        {
            //if (_series == null)
            //{
            //    return;
            //}
            //var overlay = this.ShowOverlay();
            //var allBindingList = await BLLFactory<HStation.BLL.AssetsCoolingFactor>.Instance.GetAll();
            //_allBindingList = new List<AssetsCoolingFactorMgrViewModel>();
            //if (allBindingList != null && allBindingList.Count > 0)
            //{
            //    foreach (var item in allBindingList)
            //    {
            //        var vm = new AssetsCoolingFactorMgrViewModel(item);
            //        _allBindingList.Add(vm);
            //    }
            //}
            //this.assetsCoolingFactorMgrViewModelBindingSource.DataSource = _allBindingList;
            //this.assetsCoolingFactorMgrViewModelBindingSource.ResetBindings(false);
            //overlay.Close();
            if (_series == null)
            {
                return;
            }
            var overlay = this.ShowOverlay();
            var allBindingList = await BLLFactory<HStation.BLL.AssetsCoolingMain>.Instance.GetBySeriesID(_series.ID);
            _allBindingList = new List<AssetsCoolingMainMgrViewModel>();
            if (allBindingList != null && allBindingList.Count > 0)
            {
                foreach (var item in allBindingList)
                {
                    var vm = new AssetsCoolingMainMgrViewModel(item);
                    _allBindingList.Add(vm);
                }
            }
            this.assetsCoolingMainMgrViewModelBindingSource.DataSource = _allBindingList;
            this.assetsCoolingMainMgrViewModelBindingSource.ResetBindings(false);
            overlay.Close();
            SetDragEnable(this.barCkDrag.Checked);
        }
        /// <summary>
@@ -78,116 +82,193 @@
        //添加
        private void Add()
        {
            //if (_allBindingList == null)
            //{
            //    TipFormHelper.ShowError("数据初始化失败!");
            //    return;
            //}
            //var dlg = new AddAssetsCoolingMainDlg();
            //dlg.ReloadDataEvent += (rhs) =>
            //{
            //    var vm = new AssetsCoolingMainMgrViewModel(rhs);
            //    _allBindingList.Add(vm);
            //    this.assetsCoolingFactorMgrViewModelBindingSource.ResetBindings(false);
            //    TipFormHelper.ShowSucceed("添加成功!");
            //};
            //dlg.SetBindingData();
            //dlg.ShowDialog();
            if (_series == null)
            {
                return;
            }
            if (_allBindingList == null)
            {
                TipFormHelper.ShowError("数据初始化失败!");
                return;
            }
            var dlg = new AddAssetsCoolingMainDlg();
            dlg.ReloadDataEvent += (rhs) =>
            {
                var vm = new AssetsCoolingMainMgrViewModel(rhs);
                _allBindingList.Add(vm);
                this.assetsCoolingMainMgrViewModelBindingSource.ResetBindings(false);
                TipFormHelper.ShowSucceed("添加成功!");
            };
            dlg.SetBindingData(_series);
            dlg.ShowDialog();
        }
        //编辑
        private void Edit()
        {
            //var vm = GetCurrentViewModel();
            //if (vm == null)
            //{
            //    return;
            //}
            //var dlg = new EditAssetsCoolingFactorDlg();
            //dlg.ReloadDataEvent += (rhs) =>
            //{
            //    vm.Reset(rhs);
            //    this.gridView1.RefreshRow(this.gridView1.FocusedRowHandle);
            //    TipFormHelper.ShowSucceed("更新成功");
            //};
            //dlg.SetBindingData(vm.Vmo);
            //dlg.ShowDialog();
            var vm = GetCurrentViewModel();
            if (vm == null)
            {
                return;
            }
            var dlg = new EditAssetsCoolingMainDlg();
            dlg.ReloadDataEvent += (rhs) =>
            {
                vm.Reset(rhs);
                this.gridView1.RefreshRow(this.gridView1.FocusedRowHandle);
                TipFormHelper.ShowSucceed("更新成功");
            };
            dlg.SetBindingData(vm.Vmo);
            dlg.ShowDialog();
        }
        //删除
        private async void Delete()
        {
            //var vm = GetCurrentViewModel();
            //if (vm == null)
            //{
            //    return;
            //}
            //var result = XtraMessageBox.Show("请问确认删除当前数据吗?", "询问", MessageBoxButtons.YesNo) == DialogResult.Yes;
            //if (!result)
            //{
            //    return;
            //}
            //var bol = await BLLFactory<HStation.BLL.AssetsCoolingFactor>.Instance.DeleteByID(vm.ID);
            //if (!bol)
            //{
            //    TipFormHelper.ShowError("删除失败!");
            //    return;
            //}
            //_allBindingList.Remove(vm);
            //this.assetsCoolingFactorMgrViewModelBindingSource.ResetBindings(false);
            //TipFormHelper.ShowSucceed("删除成功!");
            var vm = GetCurrentViewModel();
            if (vm == null)
            {
                return;
            }
            var result = XtraMessageBox.Show("请问确认删除当前数据吗?", "询问", MessageBoxButtons.YesNo) == DialogResult.Yes;
            if (!result)
            {
                return;
            }
            var bol = await BLLFactory<HStation.BLL.AssetsCoolingMain>.Instance.DeleteByID(vm.ID);
            if (!bol)
            {
                TipFormHelper.ShowError("删除失败!");
                return;
            }
            _allBindingList.Remove(vm);
            this.assetsCoolingMainMgrViewModelBindingSource.ResetBindings(false);
            TipFormHelper.ShowSucceed("删除成功!");
        }
        //查看
        private void View()
        {
            //var vm = GetCurrentViewModel();
            //if (vm == null)
            //{
            //    return;
            //}
            //var dlg = new ViewAssetsCoolingFactorDlg();
            //dlg.SetBindingData(vm.Vmo);
            //dlg.ShowDialog();
            var vm = GetCurrentViewModel();
            if (vm == null)
            {
                return;
            }
            var dlg = new ViewAssetsCoolingMainDlg();
            dlg.SetBindingData(vm.Vmo);
            dlg.ShowDialog();
        }
        //上移
        private void Up()
        private async void Up()
        {
            var vm = GetCurrentViewModel();
            if (vm == null)
            {
                return;
            }
            var rowHandle = this.gridView1.FocusedRowHandle;
            if (rowHandle == 0)
            {
                TipFormHelper.ShowWarn("上移失败!");
                return;
            }
            var current = this.gridView1.GetRow(rowHandle) as AssetsCoolingMainMgrViewModel;
            if (current == null)
            {
                return;
            }
            var prevHandle = rowHandle - 1;
            var prev = this.gridView1.GetRow(prevHandle) as AssetsCoolingMainMgrViewModel;
            if (prev == null)
            {
                return;
            }
            var sorters = new List<Yw.Vmo.Sorter>();
            sorters.Add(new Yw.Vmo.Sorter() { ID = current.ID, SortCode = prev.SortCode });
            sorters.Add(new Yw.Vmo.Sorter() { ID = prev.ID, SortCode = current.SortCode });
            var bol = await BLLFactory<HStation.BLL.AssetsCoolingMain>.Instance.UpdateSorter(sorters);
            if (!bol)
            {
                TipFormHelper.ShowError("上移失败!");
                return;
            }
            var sortCode = current.SortCode;
            current.SortCode = prev.SortCode;
            prev.SortCode = sortCode;
            _allBindingList = _allBindingList.OrderBy(x => x.SortCode).ToList();
            this.assetsCoolingMainMgrViewModelBindingSource.DataSource = _allBindingList;
            this.assetsCoolingMainMgrViewModelBindingSource.ResetBindings(false);
            this.gridView1.FocusedRowHandle = prevHandle;
        }
        //下移
        private void Down()
        private async void Down()
        {
            var vm = GetCurrentViewModel();
            if (vm == null)
            {
                return;
            }
            var rowHandle = this.gridView1.FocusedRowHandle;
            if (rowHandle == _allBindingList.Count - 1)
            {
                TipFormHelper.ShowWarn("下移失败!");
                return;
            }
            var current = this.gridView1.GetRow(rowHandle) as AssetsCoolingMainMgrViewModel;
            if (current == null)
            {
                return;
            }
            var nextHandle = rowHandle + 1;
            var next = this.gridView1.GetRow(nextHandle) as AssetsCoolingMainMgrViewModel;
            if (next == null)
            {
                return;
            }
            var sorters = new List<Yw.Vmo.Sorter>();
            sorters.Add(new Yw.Vmo.Sorter() { ID = current.ID, SortCode = next.SortCode });
            sorters.Add(new Yw.Vmo.Sorter() { ID = next.ID, SortCode = current.SortCode });
            var bol = await BLLFactory<HStation.BLL.AssetsCoolingMain>.Instance.UpdateSorter(sorters);
            if (!bol)
            {
                TipFormHelper.ShowError("下移失败!");
                return;
            }
            var sortCode = current.SortCode;
            current.SortCode = next.SortCode;
            next.SortCode = sortCode;
            _allBindingList = _allBindingList.OrderBy(x => x.SortCode).ToList();
            this.assetsCoolingMainMgrViewModelBindingSource.DataSource = _allBindingList;
            this.assetsCoolingMainMgrViewModelBindingSource.ResetBindings(false);
            this.gridView1.FocusedRowHandle = nextHandle;
        }
        #region 当前
        ////获取当前
        //private AssetsCoolingFactorMgrViewModel GetCurrentViewModel()
        //{
        //    if (_allBindingList == null)
        //    {
        //        TipFormHelper.ShowError("数据初始化错误!");
        //        return null;
        //    }
        //    if (_allBindingList.Count < 1)
        //    {
        //        TipFormHelper.ShowInfo("无数据!");
        //        return null;
        //    }
        //    var vm = this.gridView1.GetCurrentViewModel(_allBindingList);
        //    if (vm == null)
        //    {
        //        TipFormHelper.ShowWarn("请选择数据行!");
        //        return null;
        //    }
        //    return vm;
        //}
        //获取当前
        private AssetsCoolingMainMgrViewModel GetCurrentViewModel()
        {
            if (_allBindingList == null)
            {
                TipFormHelper.ShowError("数据初始化错误!");
                return null;
            }
            if (_allBindingList.Count < 1)
            {
                TipFormHelper.ShowInfo("无数据!");
                return null;
            }
            var vm = this.gridView1.GetCurrentViewModel(_allBindingList);
            if (vm == null)
            {
                TipFormHelper.ShowWarn("请选择数据行!");
                return null;
            }
            return vm;
        }
        #endregion
@@ -220,13 +301,13 @@
        //上移
        private void barBtnUp_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Up();
        }
        //下移
        private void barBtnDown_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Down();
        }
        //查询
@@ -262,5 +343,101 @@
                }
            }
        }
        private async void dragDropEvents1_DragDrop(object sender, DevExpress.Utils.DragDrop.DragDropEventArgs e)
        {
            var indexs = e.Data as int[];
            if (indexs == null || indexs.Length < 1)
            {
                e.Handled = true;
                return;
            }
            var sourceIndex = indexs[0];
            var sourceObj = this.gridView1.GetRow(sourceIndex) as AssetsCoolingMainMgrViewModel;
            if (sourceObj == null)
            {
                e.Handled = true;
                return;
            }
            var destIndex = this.gridView1.GetRowHandleByCP(e.Location);
            if (destIndex < 0)
            {
                e.Handled = true;
                return;
            }
            var destObj = this.gridView1.GetRow(destIndex) as AssetsCoolingMainMgrViewModel;
            if (destObj == null)
            {
                e.Handled = true;
                return;
            }
            var sorters = new List<Yw.Vmo.Sorter>();
            if (e.InsertType == InsertType.Before)
            {
                sorters.Add(new Yw.Vmo.Sorter() { ID = sourceObj.ID, SortCode = destObj.SortCode });
                _allBindingList.ForEach(x =>
                {
                    if (x.SortCode >= destObj.SortCode)
                    {
                        if (x != sourceObj)
                        {
                            sorters.Add(new Yw.Vmo.Sorter() { ID = x.ID, SortCode = x.SortCode + 1 });
                        }
                    }
                });
            }
            else if (e.InsertType == InsertType.After)
            {
                sorters.Add(new Yw.Vmo.Sorter() { ID = sourceObj.ID, SortCode = destObj.SortCode + 1 });
                _allBindingList.ForEach(x =>
                {
                    if (x.SortCode > destObj.SortCode)
                    {
                        if (x != sourceObj)
                        {
                            sorters.Add(new Yw.Vmo.Sorter() { ID = x.ID, SortCode = x.SortCode + 1 });
                        }
                    }
                });
            }
            else
            {
                e.Handled = true;
                return;
            }
            var bll = BLLFactory<HStation.BLL.AssetsCoolingMain>.Instance;
            var bol = await bll.UpdateSorter(sorters);
            if (!bol)
            {
                e.Handled = true;
                return;
            }
            _allBindingList.ForEach(x =>
            {
                var sorter = sorters.Find(t => t.ID == x.ID);
                if (sorter != null)
                {
                    x.SortCode = sorter.SortCode;
                }
            });
        }
        private void barCkDrag_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetDragEnable(this.barCkDrag.Checked);
        }
        //设置拖拽可用性
        private void SetDragEnable(bool allowArag)
        {
            var be = this.behaviorManager1.GetBehavior<DevExpress.Utils.DragDrop.DragDropBehavior>(this.gridView1);
            be.Properties.AllowDrag = allowArag;
        }
    }
}