using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Utils;
using IStation.WinFrmUI;
using IStation.Untity;
namespace IStation.WinFrmUI.Product
{
///
/// 设备分组管理控件
///
public partial class ProductGroupMgrCtrl : XtraUserControl
{
public ProductGroupMgrCtrl()
{
InitializeComponent();
this.treeList1.InitialDefaultSettings();
this.treeList1.SelectImageList = CurrentViewModel.Image16Store;
this.layoutControl1.SetupLayoutControl();
}
#region 当前视图
public class CurrentViewModel
{
public CurrentViewModel() { }
public CurrentViewModel(Model.ProductGroup rhs)
{
this.ID = rhs.ID;
this.ParentID = TreeParentIdsHelper.GetLastParentID(rhs.ParentIds);
if (this.ParentID < 1)
{
this.ParentID = -1;
}
this.ParentIds = TreeParentIdsHelper.ToString(rhs.ParentIds);
this.Name = rhs.Name;
this.TagName = rhs.TagName;
this.SortCode = rhs.SortCode;
this.Description = rhs.Description;
this.Model = rhs;
}
public void Reset(Model.ProductGroup rhs)
{
this.ID = rhs.ID;
this.ParentID = TreeParentIdsHelper.GetLastParentID(rhs.ParentIds);
if (this.ParentID < 1)
{
this.ParentID = -1;
}
this.ParentIds = TreeParentIdsHelper.ToString(rhs.ParentIds);
this.Name = rhs.Name;
this.TagName = rhs.TagName;
this.SortCode = rhs.SortCode;
this.Description = rhs.Description;
this.Model = rhs;
}
public long ID { get; set; }
public long ParentID { get; set; }
public string ParentIds { get; set; }
public string Name { get; set; }
public string TagName { get; set; }
public int SortCode { get; set; }
public string Description { get; set; }
public int ImageIndex
{
get { return 0; }
}
public Model.ProductGroup Model { get; set; }
///
/// 图标仓库
///
public static ImageCollection Image16Store
{
get
{
if (_image16Store == null)
{
_image16Store = new ImageCollection();
_image16Store.ImageSize = new Size(16, 16);
_image16Store.Images.Add(WinFrmUI.Properties.Resources.GroupC32, "Group");
}
return _image16Store;
}
}
private static ImageCollection _image16Store;
}
#endregion
///
/// 选择改变事件
///
public event Action SelectChangedEvent;
private BindingList _allBindingList = null;//所有绑定列表
private long _corpId;//当前客户
private string _belongType;//所属类型
private long _belongId;//所属标识
///
/// 绑定数据
///
public void SetBindingData()
{
_allBindingList = null;
this.SelectChangedEvent?.Invoke(null);
}
///
/// 绑定数据
///
public void SetBindingData(long corpId, string belongType, long belongId)
{
_corpId = corpId;
_belongType = belongType;
_belongId = belongId;
_allBindingList = new BindingList();
var all = new BLL.ProductGroup().GetByBelongTypeAndBelongID(_corpId, _belongType, _belongId);
all?.ForEach(x =>
{
var vm = new CurrentViewModel(x);
_allBindingList.Add(vm);
});
var group_none = new Model.ProductGroup();
group_none.ID = 0;
group_none.CorpID = _corpId;
group_none.BelongType = _belongType;
group_none.BelongID = _belongId;
group_none.Name = "未分组";
group_none.SortCode = int.MaxValue;
_allBindingList.Add(new CurrentViewModel(group_none));
this.treeList1.DataSource = _allBindingList;
this.treeList1.RefreshDataSource();
this.treeList1.ExpandAll();
SelectChanged();
}
///
/// 选择改变项
///
public void SelectChanged()
{
var row = this.treeList1.GetFocusedRow() as CurrentViewModel;
this.SelectChangedEvent?.Invoke(row?.Model);
}
#region 基础方法
//添加根
public void AddRoot()
{
if (_allBindingList == null)
return;
var items = _allBindingList.Where(x => x.ID > 0 && x.ParentID == -1).ToList();
int sort_code = items.Count > 0 ? items.Max(x => x.SortCode) + 1 : 1;
WaitFrmHelper.ShowWaitForm();
var dlg = new AddProductGroupDlg();
dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); };
dlg.SetBindingData(_corpId, _belongType, _belongId, sort_code);
dlg.ReloadDataEvent += (rhs) =>
{
var vm = new CurrentViewModel(rhs);
_allBindingList.Add(vm);
this.treeList1.RefreshDataSource();
XtraMessageBox.Show("添加成功!");
};
dlg.ShowDialog();
}
//添加子项
public void AddChild()
{
var vm = this.treeList1.GetCurrentViewModel(_allBindingList);
if (vm == null)
{
XtraMessageBox.Show("请选择数据行!");
return;
}
if (vm.ID < 1)
{
XtraMessageBox.Show("请选择实际分组!");
return;
}
var items = _allBindingList.Where(x => x.ParentID == vm.ID).ToList();
var sort_code = items.Count > 0 ? items.Max(x => x.SortCode) + 1 : 1;
var parent_id_list = TreeParentIdsHelper.GetChildParentIds(vm.ID, vm.Model.ParentIds);
WaitFrmHelper.ShowWaitForm();
var dlg = new AddProductGroupDlg();
dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); };
dlg.SetBindingData(_corpId, _belongType, _belongId, sort_code, parent_id_list);
dlg.ReloadDataEvent += (rhs) =>
{
var vm_rhs = new CurrentViewModel(rhs);
_allBindingList.Add(vm_rhs);
this.treeList1.RefreshDataSource();
XtraMessageBox.Show("添加成功!");
};
dlg.ShowDialog();
}
//编辑
public void Edit()
{
var vm = this.treeList1.GetCurrentViewModel(_allBindingList);
if (vm == null)
{
XtraMessageBox.Show("请选择数据行!");
return;
}
if (vm.ID < 1)
{
XtraMessageBox.Show("请选择实际分组!");
return;
}
WaitFrmHelper.ShowWaitForm();
var dlg = new EditProductGroupDlg();
dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); };
dlg.SetBindingData(vm.Model);
dlg.ReloadDataEvent += (rhs) =>
{
vm.Reset(rhs);
this.treeList1.RefreshNode(this.treeList1.FocusedNode);
XtraMessageBox.Show("更新成功!");
};
dlg.ShowDialog();
}
//删除
public void Delete()
{
var vm = this.treeList1.GetCurrentViewModel(_allBindingList);
if (vm == null)
{
XtraMessageBox.Show("请选择数据行!");
return;
}
if (vm.ID < 1)
{
XtraMessageBox.Show("请选择实际分组!");
return;
}
XtraMessageBox.Show("正在开发中,敬请期待!");
}
//详细信息
public void View()
{
var vm = this.treeList1.GetCurrentViewModel(_allBindingList);
if (vm == null)
{
XtraMessageBox.Show("请选择数据行!");
return;
}
if (vm.ID < 1)
{
XtraMessageBox.Show("请选择实际分组!");
return;
}
WaitFrmHelper.ShowWaitForm();
var dlg = new ViewProductGroupDlg();
dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); };
dlg.SetBindingData(vm.Model);
dlg.ShowDialog();
}
//更新 TagName
public void UpdateTagName()
{
var vm = this.treeList1.GetCurrentViewModel(_allBindingList);
if (vm == null)
{
XtraMessageBox.Show("请选择数据行!");
return;
}
if (vm.ID < 1)
{
XtraMessageBox.Show("请选择实际分组!");
return;
}
WaitFrmHelper.ShowWaitForm();
var dlg = new SetTagNameDlg();
dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); };
dlg.SetBindingData(vm.TagName);
dlg.ReloadDataEvent += (rhs) =>
{
var bll = new BLL.ProductGroup();
var result = bll.UpdateTagName(vm.Model.CorpID, vm.ID, rhs);
if (result)
{
var model = bll.GetByID(vm.Model.CorpID, vm.ID);
vm.Reset(model);
this.treeList1.RefreshNode(this.treeList1.FocusedNode);
}
return result;
};
dlg.ShowDialog();
}
//更新排序码
public void UpdateSortCode()
{
var vm = this.treeList1.GetCurrentViewModel(_allBindingList);
if (vm == null)
{
XtraMessageBox.Show("请选择数据行!");
return;
}
if (vm.ID < 1)
{
XtraMessageBox.Show("请选择实际分组!");
return;
}
var dlg = new SetSortCodeDlg();
dlg.SetBindingData(vm.SortCode);
dlg.ReloadDataEvent += (rhs) =>
{
var bll = new BLL.ProductGroup();
var result = bll.UpdateSortCode(vm.Model.CorpID, vm.ID, rhs);
if (result)
{
var model = bll.GetByID(vm.Model.CorpID, vm.ID);
vm.Reset(model);
this.treeList1.RefreshNode(this.treeList1.FocusedNode);
return true;
}
return false;
};
dlg.ShowDialog();
}
///
/// 获取当前分组
///
public Model.ProductGroup GetFocused()
{
var vm = this.treeList1.GetFocusedRow() as CurrentViewModel;
return vm?.Model;
}
#endregion
#region 事件处理器
//全部展开(menu)
private void barBtnExpandAllInMenu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
this.treeList1.ExpandAll();
}
//全部折叠(menu)
private void barBtnCollpseAllInMenu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
this.treeList1.CollapseAll();
}
//检索(menu)
private void barBtnSearchInMenu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
if (this.layoutControlItem1.Visibility == DevExpress.XtraLayout.Utils.LayoutVisibility.Always)
this.layoutControlItem1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
else
this.layoutControlItem1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
}
//树线
private void barCkTreeLine_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
this.treeList1.OptionsView.ShowTreeLines = this.barCkTreeLine.Checked ? DefaultBoolean.True : DefaultBoolean.False;
}
//拖拽开关
private void barCkDragSort_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
var be = this.behaviorManager1.GetBehavior(this.treeList1);
be.Properties.AllowDrag = this.barCkDragSort.Checked;
}
//根节点添加分组
private void barBtnAddRoot_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
AddRoot();
}
//节点下添加分组
private void barBtnAddChild_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
AddChild();
}
//节点编辑
private void barBtnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Edit();
}
//删除
private void barBtnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Delete();
}
//详细信息
private void barBtnDetail_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
View();
}
//更新标签
private void barBtnTagName_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
UpdateTagName();
}
//更新排序
private void barBtnSortCode_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
UpdateSortCode();
}
//全部折叠 (node)
private void btnCollpaseAllInNode_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
this.treeList1.FocusedNode?.Collapse();
}
//全部展开 (node)
private void btnExpandAllInNode_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
this.treeList1.FocusedNode?.Expand();
}
//右键菜单弹出
private void treeList1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var node = this.treeList1.GetNodeAt(e.X, e.Y);
if (node == null)
{
this.pmRoot.ShowPopup(MousePosition);
return;
}
}
}
//右键菜单弹出
private void treeList1_RowCellClick(object sender, DevExpress.XtraTreeList.RowCellClickEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (e.Column == this.colName)
{
SelectChanged();
}
}
else if (e.Button == MouseButtons.Right)
{
this.pmNode.ShowPopup(MousePosition);
}
}
#endregion
}
}