using DevExpress.XtraEditors;
using DevExpress.XtraTreeList.Nodes;
using HStation.WinFrmUI.Assets;
using System.ComponentModel;
using Yw;
namespace HStation.WinFrmUI.Xhs.PumpProduct
{
///
///
///
public partial class PumpProductSeriesTreeListCtrl : XtraUserControl
{
public PumpProductSeriesTreeListCtrl()
{
InitializeComponent();
this.treeList1.InitialDefaultSettings();
this.treeList1.SelectImageList = ImageLib.Lib;
}
///
/// 聚焦改变事件
///
public event Action FocusedChangedEvent;
private BindingList _allBindingList = null;//所有绑定列表
private BLL.AssetsPumpSeries _seriesbll = null;
private BLL.AssetsPumpGroup _groupbll = null;
private BLL.AssetsPumpType _typebll = null;
private BLL.AssetsPumpTypeSeriesMapSeriesMap _typeSeriesMapBll = null;
///
/// 绑定数据
///
public void Clear()
{
_allBindingList = new BindingList();
this.treeList1.DataSource = _allBindingList;
this.FocusedChangedEvent?.Invoke(default, default);
}
///
/// 绑定数据
///
public async void SetBindingData()
{
_seriesbll = new BLL.AssetsPumpSeries();
_groupbll = new BLL.AssetsPumpGroup();
_typebll = new BLL.AssetsPumpType();
_typeSeriesMapBll = new BLL.AssetsPumpTypeSeriesMapSeriesMap();
_allBindingList = new BindingList();
var allType = await _typebll.GetAll();
_allBindingList.Add(new CurrentTreeViewModel
{
ID = 1,
Name = "泵列表",
ImageIndex = ImageLib.Listview
});
foreach (var item in allType)
{
var type = new CurrentTreeViewModel(item);
_allBindingList.Add(type);
var allseriesid = await _typeSeriesMapBll.GetSeriesIDByTypeID(item.ID);
if (allseriesid.Count > 0 && allseriesid != null)
{
var allseries = await _seriesbll.GetByIds(allseriesid);
foreach (var series in allseries)
{
var vm = new CurrentTreeViewModel(series, item.ID);
_allBindingList.Add(vm);
var allgroup = await _groupbll.GetBySeriesID(series.ID);
foreach (var group in allgroup)
{
var groupvm = new CurrentTreeViewModel(group);
_allBindingList.Add(groupvm);
}
}
}
}
this.treeList1.DataSource = _allBindingList;
this.treeList1.RefreshDataSource();
TreeListNode firstParentNode = treeList1.Nodes[0]; // 假设这是你要展开的父节点
firstParentNode.Expand();
}
//聚焦改变
private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
{
var vm = this.treeList1.GetCurrentViewModel(_allBindingList);
if (vm != null)
{
var series = vm.Model as Vmo.AssetsPumpSeriesVmo;
if (series != null)
{
this.FocusedChangedEvent?.Invoke(series.ID, true);
}
else
{
var group = vm.Model as Vmo.AssetsPumpGroupVmo;
if (group != null)
{
this.FocusedChangedEvent?.Invoke(group.ID, false);
}
}
}
}
#region 菜单事件
//检索
private void barCkSearch_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
if (this.barCkSearch.Checked)
this.layoutControlItemSearch.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
else
this.layoutControlItemSearch.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
}
#endregion 菜单事件
//添加分类
private void barBtnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
if (_allBindingList == null)
return;
var dlg = new AddAssetsPumpTypeDlg();
dlg.ReloadDataEvent += async (rhs) =>
{
var bll = new BLL.AssetsPumpType();
var id = await bll.Insert(rhs);
if (id > 0)
{
rhs.ID = id;
_allBindingList.Add(new CurrentTreeViewModel(rhs));
return true;
}
return false;
};
dlg.ShowDialog();
}
///
/// 临时 (添加泵型号)
///
/* private void get()
{
if (_allBindingList == null)
return;
var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
var dlg = new AddPumpProductSeriesDlg();
dlg.ReloadDataEvent += async (model) =>
{
var allchildList = await _seriesbll.GetAll();
var id = await _seriesbll.Insert(model);
if (id > 0)
{
var vm = await _seriesbll.GetByID(id);
_allBindingList.Add(new CurrentTreeViewModel(vm));
return true;
}
return false;
};
dlg.ShowDialog();
}*/
//全部折叠
private void barBtnCollapseAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
this.treeList1.CollapseAll();
}
//全部展开
private void barBtnExpandAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
this.treeList1.ExpandAll();
}
//编辑
private async void barBtnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
if (currentVm == null)
{
TipFormHelper.ShowWarn("请选择数据行!");
return;
}
if (currentVm.IsGroup == false)
{
var series = currentVm.Model as Model.AssetsPumpSeries;
var dlg = new EditPumpProductSeriesDlg();
var model = await new BLL.AssetsPumpSeries().GetByID(currentVm.ID);
if (model == null)
return;
dlg.SetBindingData(model);
dlg.ReloadDataEvent += async (model) =>
{
if (await _seriesbll.Update(model))
{
currentVm.Reset(model);
this.treeList1.RefreshDataSource();
return true;
}
return false;
};
dlg.ShowDialog();
}
else
{
var dlg = new EditPumpProductGroupDlg();
var GroupDto = await new BLL.AssetsPumpGroup().GetByID(currentVm.ID);
if (GroupDto == null)
return;
dlg.SetBindingData(GroupDto);
dlg.ReloadDataEvent += async (model) =>
{
if (await _groupbll.Update(model))
{
currentVm.Reset(model);
this.treeList1.RefreshDataSource();
return true;
}
return false;
};
dlg.ShowDialog();
}
}
//删除
private async void barBtnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
if (currentVm == null)
{
MessageBoxHelper.ShowWarning("请选择数据行!");
return;
}
if (MessageBoxHelper.IsClickOk($"确认删除数据行?", "提示"))
return;
if (currentVm.IsGroup)
{
var groupresult = await _groupbll.DeleteEx(currentVm.ID);
if (groupresult)
{
_allBindingList.Remove(currentVm);
}
else
{
MessageBoxHelper.ShowError($"删除失败!");
return;
}
}
else
{
var result = await BLLFactory.Instance.DeleteEx(currentVm.ID);
if (result)
{
_allBindingList.Remove(currentVm);
var children = _allBindingList.Where(x => x.ParentID == currentVm.ID).ToList();
foreach (var child in children)
{
_allBindingList.Remove(child);
}
MessageBoxHelper.ShowSuccess($"删除成功!");
this.treeList1.Refresh();
return;
}
else
{
MessageBoxHelper.ShowError($"删除失败!");
return;
}
}
}
//添加型号组
private void barBtnAddChild_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
if (currentVm == null)
{
MessageBoxHelper.ShowWarning("请选择数据行!");
return;
}
if (_allBindingList == null)
return;
var dlg = new AddPumpProductGroupDlg();
dlg.SetBindingData(currentVm.ID);
dlg.ReloadDataEvent += async (model) =>
{
var allchildList = await BLLFactory.Instance.GetAll();
model.SortCode = allchildList.Count == 0 ? 1 : allchildList.Max(x => x.SortCode) + 1;
var id = await _groupbll.Insert(model);
if (id > 0)
{
var vm = await BLLFactory.Instance.GetByID(id);
_allBindingList.Add(new CurrentTreeViewModel(vm));
this.treeList1.Refresh();
return true;
}
return false;
};
dlg.ShowDialog();
}
public long GetCurrentID()
{
var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
if (currentVm == null)
{
MessageBoxHelper.ShowWarning("请选择数据行!");
return default;
}
if (currentVm.IsGroup)
{
return currentVm.ParentID;
}
else if (currentVm.IsType)
{
MessageBoxHelper.ShowWarning("请选择系列再进行添加操作!");
return default;
}
else
{
return currentVm.ID;
}
}
public long GetCurrentGroupID()
{
var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
if (currentVm == null)
{
MessageBoxHelper.ShowWarning("请选择数据行!");
return default;
}
if (currentVm.IsGroup)
{
return currentVm.ID;
}
else
{
return default;
}
}
//右击显示菜单
private void treeList1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var vm = this.treeList1.GetCurrentViewModel(_allBindingList);
if (vm != null)
{
if (vm.ID == 1)
{
Point screenPointType = Cursor.Position;
popupMenuListView.ShowPopup(screenPointType);
return;
}
if (vm.IsType)
{
Point screenPointType = Cursor.Position;
popupMenuType.ShowPopup(screenPointType);
}
else if (!vm.IsGroup)
{
Point screenPoint = Cursor.Position;
popupMenuSeries.ShowPopup(screenPoint);
}
else
{
Point screenPoint = Cursor.Position;
popupMenuGroup.ShowPopup(screenPoint);
}
}
}
}
#region 分类菜单
private void BarBtnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
this.SetBindingData();
}
#endregion 分类菜单
#region 类型菜单
//添加泵系列
private void BarButtonAddAssetsPumpSeries_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
if (_allBindingList == null)
return;
var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
if (currentVm == null)
{
MessageBoxHelper.ShowWarning("请选择数据行!");
return;
}
var dlg = new AddPumpProductSeriesDlg();
dlg.ReloadDataEvent += async (model) =>
{
//var allchildList = await _seriesbll.GetAll();
var id = await _seriesbll.Insert(model);
if (id > 0)
{
var typemapid = await _typeSeriesMapBll.Insert(
new Vmo.AssetsPumpTypeSeriesMap
{
PumpSeriesID = id,
PumpTypeID = currentVm.ID
}
);
var vm = await _seriesbll.GetByID(id);
_allBindingList.Add(new CurrentTreeViewModel(vm, currentVm.ID));
return true;
}
return false;
};
dlg.ShowDialog();
}
//删除分类
private async void BarButtonDeleteAssetsPumpType_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
if (currentVm == null)
{
MessageBoxHelper.ShowWarning("请选择数据行!");
return;
}
if (MessageBoxHelper.IsClickOk($"确认删除数据行?", "提示"))
return;
var typeresult = await _typebll.DeleteByID(currentVm.ID);
if (typeresult)
{
_allBindingList.Remove(currentVm);
TipFormHelper.ShowSucceed("删除成功!");
return;
}
else
{
TipFormHelper.ShowSucceed("删除失败!");
}
}
//编辑分类
private void BarButtonEditAssetsPumpType_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
if (currentVm == null)
{
TipFormHelper.ShowWarn("请选择数据行!");
return;
}
var dlg = new EditAssetsPumpTypeDlg();
dlg.SetBindingData(currentVm.ID);
dlg.ReloadDataEvent += async (rhs) =>
{
var bll = new BLL.AssetsPumpType();
if (await bll.Update(rhs))
{
currentVm.Reset(rhs);
this.treeList1.RefreshDataSource();
return true;
}
return false;
};
dlg.ShowDialog();
}
#endregion 类型菜单
#region 型号菜单
public event Action