using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading.Tasks; using JwKdsV.Entity.Normal; using JwKdsV.Core.MessageEvent; using JwKdsV.Core; using JwKdsV.Core.Bean; using JwKdsV.Core.Utils; namespace JwKdsV.Component { [ToolboxItem(true)] public partial class CategoryControl : BaseUserControl { /// /// 数据源 /// private List _dataSource = null; public CategoryControl() { InitializeComponent(); this.BackColor = Color.Transparent; } protected override void OnPaint(PaintEventArgs e) { //base.OnPaint(e); Rectangle rect = new Rectangle(0, 0, this.Width, this.Height); ControlPaint.DrawBorder(e.Graphics, rect, Color.Transparent, ButtonBorderStyle.Solid);//画个边框 //ControlPaint.DrawBorder(e.Graphics , rect , // Color.Transparent , 1 , ButtonBorderStyle.Solid , // Color.Transparent , 1 , ButtonBorderStyle.Solid , // Color.Transparent , 1 , ButtonBorderStyle.Solid , // Color.Transparent , 1 , ButtonBorderStyle.Solid //); } protected override void OnResize(EventArgs e) { // this.Visible = false; switch (this.ShowCagegoryType) { case CategoryShowType.仅显示全部大类: { base.Height = this.MainCategoryRows * this.MainCategoryItemHeight; } break; case CategoryShowType.仅显示全部小类: { base.Height = this.SubCategoryRows * this.SubCategoryItemHeight; } break; case CategoryShowType.系统默认: { base.Height = this.MainCategoryRows * this.MainCategoryItemHeight + this.SubCategoryRows * this.SubCategoryItemHeight; } break; } base.OnResize(e); // this.Visible = true; } protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (this.DesignMode) return; this.mainCategory.CategoryCheckedChanged += OnMainCategoryChecked; this.subCategory.CategoryCheckedChanged += OnSubCategoryChecked; InitProductType(); } private void InitProductType() { try { var categoryItem = new CategoryItem(); this.RefreshUi(categoryItem); var dataSource = new List(); //呈现品类下包含商品的 using (var db = Global.Instance.OpenDataBase) { dataSource = db.Query(" where products > @0 order by no", 0).ToList(); } if (categoryItem.ShowAllProduct) { this.ShowAllProduct(categoryItem, dataSource); } this.BindDataSource(dataSource); } catch (Exception ex) { LOGGER.Error(ex, "加载品类异常"); } } private void ShowAllProduct(CategoryItem categoryItem, List dataSource) { if (categoryItem.ShowAllProduct) { if (!dataSource.Exists(x => x.No == Constant.ALL_PRODUCT_TAG)) { var type = new ProductType(); type.Id = IdWorkerUtils.Instance.NextId(); type.ParentId = string.Empty; type.No = Constant.ALL_PRODUCT_TAG; type.Name = "全部"; type.Products = 99999; switch (this.ShowCagegoryType) { case CategoryShowType.系统默认: case CategoryShowType.仅显示全部大类: { type.Path = type.Id; } break; case CategoryShowType.仅显示全部小类: { type.Path = string.Format("{0},{1}", type.Id, type.Id); } break; } dataSource.Insert(0, type); } } else { if (dataSource.Exists(x => x.No == Constant.ALL_PRODUCT_TAG)) { var productType = dataSource.Find(x => x.No == Constant.ALL_PRODUCT_TAG); dataSource.Remove(productType); } } } public void RefreshUi(CategoryItem item) { this.ShowCagegoryType = item.ShowType; this.CategoryCornerType = item.CategoryCornerType; this.MainCategoryRows = item.MainCategoryRows; this.MainCategoryColumns = item.MainCategoryColumns; this.MainCategoryFont = item.MainCategoryFont; this.MainCategoryCode = item.MainCategoryCode; this.MainCategoryBackColor1 = item.MainCategoryBackColor1; this.MainCategoryBackColor2 = item.MainCategoryBackColor2; this.MainCategorySelectedColor = item.MainCategorySelectedColor; this.MainCategoryTextColor = item.MainCategoryTextColor; this.MainAutoPager = item.MainAutoPager; this.MainAutoItemWidth = item.MainAutoItemWidth; this.MainCustomItemWidth = item.MainCustomItemWidth; this.MainCategoryItemHeight = item.MainCategoryItemHeight; this.SubCategoryRows = item.SubCategoryRows; this.SubCategoryColumns = item.SubCategoryColumns; this.SubCategoryFont = item.SubCategoryFont; this.SubCategoryCode = item.SubCategoryCode; this.SubCategoryBackColor1 = item.SubCategoryBackColor1; this.SubCategoryBackColor2 = item.SubCategoryBackColor2; this.SubCategorySelectedColor = item.SubCategorySelectedColor; this.SubCategoryTextColor = item.SubCategoryTextColor; this.SubAutoPager = item.SubAutoPager; this.SubAutoItemWidth = item.SubAutoItemWidth; this.SubCustomItemWidth = item.SubCustomItemWidth; this.SubCategoryItemHeight = item.SubCategoryItemHeight; this.RefreshUi(); } public void BindDataSource(List dataSource) { this._dataSource = dataSource; switch (this.ShowCagegoryType) { case CategoryShowType.仅显示全部大类: { //仅绑定大类 BindAllCategory(); } break; case CategoryShowType.仅显示全部小类: { //仅绑定小类 BindAllSubCategory(); } break; case CategoryShowType.系统默认: { //绑定大类 BindDefaultCategory(); } break; } } private void RefreshUi() { switch (this.ShowCagegoryType) { case CategoryShowType.仅显示全部大类: { //隐藏大类 this.mainCategory.Visible = false; this.mainCategory.Height = 0; //全部用小类呈现 this.subCategory.Visible = true; this.subCategory.Height = this.MainCategoryRows * this.MainCategoryItemHeight; this.subCategory.Rows = this.MainCategoryRows; this.subCategory.Columns = this.MainCategoryColumns; this.subCategory.FontSize = this.MainCategoryFont; this.subCategory.DisplayCategoryCode = this.MainCategoryCode; this.subCategory.BackColor1 = this.MainCategoryBackColor1; this.subCategory.BackColor2 = this.MainCategoryBackColor2; this.subCategory.SelectedColor = this.MainCategorySelectedColor; this.subCategory.TextColor = this.MainCategoryTextColor; this.subCategory.AutoPager = this.MainAutoPager; this.subCategory.AutoItemWidth = this.MainAutoItemWidth; this.subCategory.ItemWidth = this.MainCustomItemWidth; this.subCategory.ItemHeight = this.MainCategoryItemHeight; this.Height = this.MainCategoryRows * this.MainCategoryItemHeight; } break; case CategoryShowType.仅显示全部小类: { //隐藏大类 this.mainCategory.Visible = false; this.mainCategory.Height = 0; //全部用小类呈现 this.subCategory.Visible = true; this.subCategory.Height = this.SubCategoryRows * this.SubCategoryItemHeight; this.subCategory.Rows = this.SubCategoryRows; this.subCategory.Columns = this.SubCategoryColumns; this.subCategory.FontSize = this.SubCategoryFont; this.subCategory.DisplayCategoryCode = this.SubCategoryCode; this.subCategory.BackColor1 = this.SubCategoryBackColor1; this.subCategory.BackColor2 = this.SubCategoryBackColor2; this.subCategory.SelectedColor = this.SubCategorySelectedColor; this.subCategory.TextColor = this.SubCategoryTextColor; this.subCategory.AutoPager = this.SubAutoPager; this.subCategory.AutoItemWidth = this.SubAutoItemWidth; this.subCategory.ItemWidth = this.SubCustomItemWidth; this.subCategory.ItemHeight = this.SubCategoryItemHeight; this.Height = this.SubCategoryRows * this.SubCategoryItemHeight; } break; case CategoryShowType.系统默认: { //隐藏大类 this.mainCategory.Visible = true; this.mainCategory.Height = this.MainCategoryRows * this.MainCategoryItemHeight; //全部用小类呈现 this.subCategory.Visible = true; this.subCategory.Height = this.SubCategoryRows * this.SubCategoryItemHeight; this.mainCategory.Rows = this.MainCategoryRows; this.mainCategory.Columns = this.MainCategoryColumns; this.mainCategory.FontSize = this.MainCategoryFont; this.mainCategory.DisplayCategoryCode = this.MainCategoryCode; this.mainCategory.BackColor1 = this.MainCategoryBackColor1; this.mainCategory.BackColor2 = this.MainCategoryBackColor2; this.mainCategory.SelectedColor = this.MainCategorySelectedColor; this.mainCategory.TextColor = this.MainCategoryTextColor; this.mainCategory.AutoPager = this.MainAutoPager; this.mainCategory.AutoItemWidth = this.MainAutoItemWidth; this.mainCategory.ItemWidth = this.MainCustomItemWidth; this.mainCategory.ItemHeight = this.MainCategoryItemHeight; this.subCategory.Rows = this.SubCategoryRows; this.subCategory.Columns = this.SubCategoryColumns; this.subCategory.FontSize = this.SubCategoryFont; this.subCategory.DisplayCategoryCode = this.SubCategoryCode; this.subCategory.BackColor1 = this.SubCategoryBackColor1; this.subCategory.BackColor2 = this.SubCategoryBackColor2; this.subCategory.SelectedColor = this.SubCategorySelectedColor; this.subCategory.TextColor = this.SubCategoryTextColor; this.subCategory.AutoPager = this.SubAutoPager; this.subCategory.AutoItemWidth = this.SubAutoItemWidth; this.subCategory.ItemWidth = this.SubCustomItemWidth; this.subCategory.ItemHeight = this.SubCategoryItemHeight; this.Height = this.MainCategoryRows * this.MainCategoryItemHeight + this.SubCategoryRows * this.SubCategoryItemHeight; } break; } this.mainCategory.CategoryCornerType = this.CategoryCornerType; this.subCategory.CategoryCornerType = this.CategoryCornerType; this.mainCategory.Invalidate(); this.subCategory.Invalidate(); } private int _subCategoryRows = 1; /// /// 显示内容的呈现方式 /// [Category("小类设置"), DisplayName("显示行数"), Browsable(true)] public int SubCategoryRows { get { return this._subCategoryRows; } set { this._subCategoryRows = (value <= 0 ? 1 : value); } } private int _subCategoryItemHeight = 45; /// /// 显示内容的呈现方式 /// [Category("小类设置"), DisplayName("每行高度"), Browsable(true)] public int SubCategoryItemHeight { get { return this._subCategoryItemHeight; } set { this._subCategoryItemHeight = (value <= 0 ? 1 : value); } } private int _subCategoryColumns = 1; /// /// 显示内容的呈现方式 /// [Category("小类设置"), DisplayName("每行数量"), Browsable(true)] public int SubCategoryColumns { get { return this._subCategoryColumns; } set { this._subCategoryColumns = (value <= 0 ? 1 : value); } } private bool _subCategoryCode = false; /// /// 是否显示小类编码 /// [Category("小类设置"), DisplayName("显示编码"), Browsable(true)] public bool SubCategoryCode { get { return this._subCategoryCode; } set { this._subCategoryCode = value; } } private SystemFont _subCategoryFont = SystemFont.默认; /// /// 显示内容的呈现方式 /// [Category("小类设置"), DisplayName("字体大小"), Browsable(true)] public SystemFont SubCategoryFont { get { return this._subCategoryFont; } set { this._subCategoryFont = value; } } private string _subCategoryBackColor1 = ColorTranslator.ToHtml(Color.DimGray); /// /// 品类显示颜色 /// [Category("小类设置"), DisplayName("背景色1"), Browsable(true)] public string SubCategoryBackColor1 { get { return this._subCategoryBackColor1; } set { this._subCategoryBackColor1 = value; } } private string _subCategoryBackColor2 = ColorTranslator.ToHtml(Color.DimGray); /// /// 品类显示颜色 /// [Category("小类设置"), DisplayName("背景色2"), Browsable(true)] public string SubCategoryBackColor2 { get { return this._subCategoryBackColor2; } set { this._subCategoryBackColor2 = value; } } private string _subCategorySelectedColor = ColorTranslator.ToHtml(Color.DimGray); /// /// 品类选中的颜色 /// [Category("小类设置"), DisplayName("选中颜色"), Browsable(true)] public string SubCategorySelectedColor { get { return this._subCategorySelectedColor; } set { this._subCategorySelectedColor = value; } } private string _subCategoryTextColor = ColorTranslator.ToHtml(Color.White); /// /// 品类显示颜色 /// [Category("小类设置"), DisplayName("字体颜色"), Browsable(true)] public string SubCategoryTextColor { get { return this._subCategoryTextColor; } set { this._subCategoryTextColor = value; } } private bool _subAutoPager = true; /// /// 是否自动隐藏分页 /// [Category("小类设置"), DisplayName("智能分页"), Browsable(true)] public bool SubAutoPager { get { return this._subAutoPager; } set { this._subAutoPager = value; } } private bool _subAutoItemWidth = true; /// /// 是否自动计算宽度 /// [Category("小类设置"), DisplayName("自动适配"), Browsable(true)] public bool SubAutoItemWidth { get { return this._subAutoItemWidth; } set { this._subAutoItemWidth = value; } } private int _subItemWidth = 80; /// /// 自定义宽度 /// [Category("小类设置"), DisplayName("自定义宽度"), Browsable(true)] public int SubCustomItemWidth { get { return this._subItemWidth; } set { this._subItemWidth = value; } } private bool _mainAutoItemWidth = true; /// /// 是否自动计算宽度 /// [Category("大类设置"), DisplayName("自动适配"), Browsable(true)] public bool MainAutoItemWidth { get { return this._mainAutoItemWidth; } set { this._mainAutoItemWidth = value; } } private int _mainItemWidth = 80; /// /// 自定义宽度 /// [Category("大类设置"), DisplayName("自定义宽度"), Browsable(true)] public int MainCustomItemWidth { get { return this._mainItemWidth; } set { this._mainItemWidth = value; } } private int _mainCategoryRows = 1; /// /// 显示内容的呈现方式 /// [Category("大类设置"), DisplayName("显示行数"), Browsable(true)] public int MainCategoryRows { get { return this._mainCategoryRows; } set { this._mainCategoryRows = (value <= 0 ? 1 : value); } } private int _mainCategoryItemHeight = 45; /// /// 显示内容的呈现方式 /// [Category("大类设置"), DisplayName("每行高度"), Browsable(true)] public int MainCategoryItemHeight { get { return this._mainCategoryItemHeight; } set { this._mainCategoryItemHeight = (value <= 0 ? 1 : value); } } private int _mainCategoryColumns = 1; /// /// 显示内容的呈现方式 /// [Category("大类设置"), DisplayName("每行数量"), Browsable(true)] public int MainCategoryColumns { get { return this._mainCategoryColumns; } set { this._mainCategoryColumns = (value <= 0 ? 1 : value); } } private SystemFont _mainCategoryFont = SystemFont.默认; /// /// 显示内容的呈现方式 /// [Category("大类设置"), DisplayName("字体大小"), Browsable(true)] public SystemFont MainCategoryFont { get { return this._mainCategoryFont; } set { this._mainCategoryFont = value; } } private bool _mainAutoPager = true; /// /// 是否自动隐藏分页 /// [Category("大类设置"), DisplayName("智能分页"), Browsable(true)] public bool MainAutoPager { get { return this._mainAutoPager; } set { this._mainAutoPager = value; } } private bool _mainCategoryCode = false; /// /// 是否显示大类编码 /// [Category("大类设置"), DisplayName("显示编码"), Browsable(true)] public bool MainCategoryCode { get { return this._mainCategoryCode; } set { this._mainCategoryCode = value; } } private string _mainCategoryBackColor1 = ColorTranslator.ToHtml(Color.DimGray); /// /// 品类显示颜色 /// [Category("大类设置"), DisplayName("背景色1"), Browsable(true)] public string MainCategoryBackColor1 { get { return this._mainCategoryBackColor1; } set { this._mainCategoryBackColor1 = value; } } private string _mainCategoryBackColor2 = ColorTranslator.ToHtml(Color.DimGray); /// /// 品类显示颜色 /// [Category("大类设置"), DisplayName("背景色2"), Browsable(true)] public string MainCategoryBackColor2 { get { return this._mainCategoryBackColor2; } set { this._mainCategoryBackColor2 = value; } } private string _mainCategorySelectedColor = ColorTranslator.ToHtml(Color.DimGray); /// /// 品类选中的颜色 /// [Category("大类设置"), DisplayName("选中颜色"), Browsable(true)] public string MainCategorySelectedColor { get { return this._mainCategorySelectedColor; } set { this._mainCategorySelectedColor = value; } } private string _mainCategoryTextColor = ColorTranslator.ToHtml(Color.White); /// /// 品类显示颜色 /// [Category("大类设置"), DisplayName("字体颜色"), Browsable(true)] public string MainCategoryTextColor { get { return this._mainCategoryTextColor; } set { this._mainCategoryTextColor = value; } } private CategoryShowType _showCagegoryType = CategoryShowType.系统默认; /// /// 显示内容的呈现方式 /// [Category("品类控件"), DisplayName("呈现方式"), Browsable(true)] public CategoryShowType ShowCagegoryType { get { return this._showCagegoryType; } set { this._showCagegoryType = value; } } /// /// 显示样式 /// [Category("品类控件"), DisplayName("显示样式"), Browsable(true)] public CornerType CategoryCornerType { get; set; } = CornerType.默认; /// /// 显示大类和小类 /// private void BindDefaultCategory() { Task.Factory.StartNew(() => { List lists = null; if (this._dataSource != null) { //大类小类显示,不支持全部标签 if (this._dataSource.Exists(x => x.No == Constant.ALL_PRODUCT_TAG)) { var productType = this._dataSource.Find(x => x.No == Constant.ALL_PRODUCT_TAG); //this._dataSource.Remove(productType); } lists = this._dataSource.FindAll(x => string.IsNullOrEmpty(x.ParentId)); } return lists; }).ContinueWith(task => { if (task.IsFaulted) { LOGGER.Error(task.Exception.GetBaseException(), "过滤大类异常"); } else { this.Invoke(new Action(() => { var data = task.Result; if (data != null) { this.mainCategory.BindDataSource(data); //默认加载小类,避免首次打开界面不呈现小类 if (data.Count > 0) { var entity = data[0]; var subs = new List(); if (this._dataSource != null) { subs = this._dataSource.FindAll(x => !string.IsNullOrEmpty(x.ParentId) && x.ParentId.Equals(entity.Id)); } this.subCategory.BindDataSource(subs); } } })); } }); } private void BindSubCategory(ProductType entity) { Task.Factory.StartNew(() => { List lists = null; if (this._dataSource != null) { lists = this._dataSource.FindAll(x => !string.IsNullOrEmpty(x.ParentId) && x.ParentId.Equals(entity.Id)); } return lists; }).ContinueWith(task => { if (task.IsFaulted) { LOGGER.Error(task.Exception.GetBaseException(), "大类点击操作异常"); } else { this.Invoke(new Action(() => { var data = task.Result; if (data != null) { //初始化页码 this.subCategory.PageNumber = 1; this.subCategory.BindDataSource(data); } })); } }); } /// /// 绑定大类 /// private void BindAllCategory() { Task.Factory.StartNew(() => { List lists = null; if (this._dataSource != null) { //仅绑定大类 lists = this._dataSource.FindAll(x => string.IsNullOrEmpty(x.ParentId)); } return lists; }).ContinueWith(task => { if (task.IsFaulted) { LOGGER.Error(task.Exception.GetBaseException(), "过滤大类异常"); } else { this.Invoke(new Action(() => { var data = task.Result; if (data != null) { this.subCategory.BindDataSource(data); } })); } }); } /// /// 绑定小类 /// private void BindAllSubCategory() { Task.Factory.StartNew(() => { List lists = null; if (this._dataSource != null) { lists = new List(); //仅绑定小类 //lists.AddRange(this._dataSource.FindAll(x => x.Path.Length == (x.Id.Length * 2 + 1))); //zhangy 2020-03-07 Edit 修改分类显示的Bug,引发Bug的核心问题在IdWorkerUtils生成的数字长度变为19位了 lists.AddRange(this._dataSource.FindAll(x => x.Path.Contains(",") && x.Path.Split(',').Length == 2)); } return lists; }).ContinueWith(task => { if (task.IsFaulted) { LOGGER.Error(task.Exception.GetBaseException(), "过滤小类异常"); } else { this.Invoke(new Action(() => { var data = task.Result; if (data != null) { this.subCategory.BindDataSource(data); } })); } }); } public event CategoryEventHandler CategoryCheckedChanged; protected virtual void OnCategoryCheckedChanged(CategoryEventArgs e) { CategoryCheckedChanged?.Invoke(this, e); } private void OnMainCategoryChecked(object sender, CategoryEventArgs e) { BindSubCategory(e.Data); OnCategoryCheckedChanged(new CategoryEventArgs(e.Data)); } private void OnSubCategoryChecked(object sender, CategoryEventArgs e) { OnCategoryCheckedChanged(new CategoryEventArgs(e.Data)); } } }