using DevExpress.UIAutomation;
|
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
|
{
|
public SysCatalogManageMainPanel()
|
{
|
InitializeComponent();
|
this.PageTitle.Caption = "分类管理";
|
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;
|
|
private Yw.BLL.SysCatalog _bll;
|
|
private List<SysCatalogViewModel> _IndexList;
|
|
public override void InitialDataSource()
|
{
|
this.typeTreeListCtrl2.SetBindingData();
|
_bll = new Yw.BLL.SysCatalog();
|
}
|
|
//聚焦切换
|
private async void TypeTreeListCtrl2_FocusedChangedEvent(long typeID)
|
{
|
_allBindingList = new List<SysCatalogViewModel>();
|
var alllist = await _bll.GetByTypeID(typeID);
|
_allBindingList.Clear();
|
foreach (var item in alllist)
|
{
|
_allBindingList.Add(new SysCatalogViewModel(item));
|
}
|
this.catalogViewModelBindingSource.DataSource = _allBindingList;
|
_IndexList = _allBindingList.Select(x => new SysCatalogViewModel(x)).ToList();
|
//_IndexList.Sort((x, y) => x.ParentID.CompareTo(y.ParentID));
|
this.catalogViewModelBindingSource.ResetBindings(false);
|
this.treeList1.ExpandAll();
|
}
|
|
//添加
|
private void BtnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
|
{
|
var dlg = new AddSysCatalogDlg();
|
var typeID = this.typeTreeListCtrl2.GetCurrentID();
|
if (typeID == 0)
|
return;
|
dlg.SetBindingData(typeID);
|
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.catalogViewModelBindingSource.ResetBindings(false);
|
return true;
|
}
|
return false;
|
};
|
dlg.ShowDialog();
|
}
|
|
//编辑
|
private void barBtnEditPumpCurve_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
|
{
|
var dlg = new EditSysCatalogDlg();
|
var vm = this.treeList1.GetCurrentViewModel(_allBindingList);
|
if (vm == null)
|
{
|
MessageBoxHelper.ShowWarning("请选择数据行!");
|
return;
|
}
|
dlg.SetBindingData(vm.ID);
|
dlg.ReloadDataEvent += async (rhs) =>
|
{
|
if (await _bll.Update(rhs))
|
{
|
vm.Reset(rhs);
|
this.catalogViewModelBindingSource.ResetBindings(false);
|
return true;
|
}
|
return false;
|
};
|
dlg.ShowDialog();
|
}
|
|
//删除
|
private async void BtnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
|
{
|
var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
|
if (currentVm == null)
|
{
|
MessageBoxHelper.ShowWarning("请选择数据行!");
|
return;
|
}
|
if (MessageBoxHelper.IsClickOk($"确认删除数据行?", "提示"))
|
return;
|
try
|
{
|
var result = await _bll.DeleteByID(currentVm.ID);
|
if (result)
|
{
|
_allBindingList.Remove(currentVm);
|
this.catalogViewModelBindingSource.ResetBindings(false);
|
MessageBoxHelper.ShowSuccess($"删除成功!");
|
}
|
else
|
{
|
MessageBoxHelper.ShowError($"删除失败!");
|
return;
|
}
|
}
|
catch (Yw.Dto.InternalException ex)
|
{
|
MessageBoxHelper.ShowError(ex.ErrorMsg);
|
}
|
}
|
|
//相应属性编辑
|
private async void BtnPropEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
|
{
|
var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
|
if (currentVm != null)
|
{
|
var dlg = new SetSysPropForCatalogDlg();
|
dlg.SetBindingData(currentVm.ID);
|
dlg.ShowDialog();
|
}
|
else
|
{
|
MessageBoxHelper.ShowWarning("选择数据行!");
|
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 sortCode = GetDataCountByNodeID(parentId);
|
var bol = await _bll.UpdateTreeSortCode(currentVm.ID, parentId, sortCode + 1);
|
if (bol)
|
{
|
this.InitialDataSource();
|
return true;
|
}
|
return false;
|
};
|
dlg.ShowDialog();
|
}
|
|
private TreeListNode currentDragNode = null;
|
|
private async 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 = GetIndexOf(currentvm, _IndexList); //当前位置索引
|
var targetIndex = GetIndexOf(targetVm, _IndexList); //目标位置索引
|
if (dragInsertPosition == DragInsertPosition.AsChild || targetVm == null || currentvm == null)
|
{
|
e.Effect = DragDropEffects.None;
|
return;
|
}
|
if (dragInsertPosition == DragInsertPosition.After)
|
{
|
_IndexList.RemoveAt(currentIndex);
|
_IndexList.Insert(targetIndex, currentvm);
|
}
|
else if (dragInsertPosition == DragInsertPosition.Before)
|
{
|
_IndexList.RemoveAt(currentIndex);
|
_IndexList.Insert(targetIndex, currentvm);
|
}
|
if (currentvm.ParentID == targetVm.ParentID)
|
{
|
e.Effect = await SetTreeListSorter(currentvm.ParentID) ? DragDropEffects.Move : DragDropEffects.None;
|
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 Task<bool> 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("修改排序失败!");
|
return false;
|
}
|
return true;
|
// 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;
|
}
|
}
|
|
//返回节点下所有子节点个数
|
private int GetDataCountByNodeID(long nodeID)
|
{
|
TreeListNode node = treeList1.FindNodeByFieldValue("ID", nodeID);
|
if (node == null)
|
{
|
return 0;
|
}
|
return node.Nodes.Count;
|
}
|
|
private CurrentTreeList GetTreeModel(TreeListNode node)
|
{
|
if (node == null)
|
return default;
|
|
var vm = GetViewModel(node);
|
var model = new CurrentTreeList();
|
model.ID = vm.ID;
|
if (node.Nodes != null && node.Nodes.Any())
|
{
|
model.Children = new List<CurrentTreeList>();
|
foreach (TreeListNode childNode in node.Nodes.Cast<TreeListNode>())
|
{
|
var childModel = GetTreeModel(childNode);
|
model.Children.Add(childModel);
|
}
|
}
|
return model;
|
}
|
|
private class CurrentTreeList
|
{
|
public CurrentTreeList()
|
{
|
}
|
|
public long ID { get; set; }
|
public List<CurrentTreeList> Children { get; set; }
|
}
|
|
private int GetIndexOf(SysCatalogViewModel vm, List<SysCatalogViewModel> dataSourse)
|
{
|
int i = 0;
|
foreach (var item in dataSourse)
|
{
|
if (item.ID == vm.ID)
|
{
|
return i;
|
}
|
i++;
|
}
|
return i;
|
}
|
}
|
}
|