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 POSV.Entity; using POSV.MessageEvent; using POSV.Bean; using POSV.Utils; using System.Threading.Tasks; namespace POSV.Component { [ToolboxItem(true)] public partial class CategoryXControl : BaseUserControl { /// /// 数据源 /// private List _dataSource = null; public CategoryXControl() { InitializeComponent(); this.BackColor = Color.Transparent; } protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (this.DesignMode) return; this.mainCategory.CategoryXCheckedChanged += OnMainCategoryXCheckedChanged; this.subCategory.CategoryXCheckedChanged += OnSubCategoryXCheckedChanged; InitProductType(); } private void InitProductType() { try { var dataSource = new List(); //呈现品类下包含商品的 using (var db = Global.Instance.OpenDataBase) { dataSource = db.Query(" where products > @0 order by no", 0).ToList(); } this.BindDataSource(dataSource); } catch (Exception ex) { LOGGER.Error(ex, "加载品类异常"); } } public void BindDataSource(List dataSource) { this._dataSource = dataSource; BindMainCategory(); } /// /// 绑定大小类 /// private void BindMainCategory() { Task.Factory.StartNew(() => { List list = null; if (this._dataSource != null) { list = this._dataSource.FindAll(x => string.IsNullOrEmpty(x.ParentId)); } return list; }).ContinueWith(task => { if (task.IsFaulted) { LOGGER.Error(task.Exception.GetBaseException(), "过滤大类异常"); } else { if (this.IsDisposed || !this.IsHandleCreated) return; this.Invoke(new Action(() => { var data = task.Result; if (data != null) { this.mainCategory.PageNumber = 1; this.mainCategory.BindDataSource(data); //默认加载小类,避免首次打开界面不呈现小类 if (data.Count > 0) { var entity = data[0]; BindSubCategory(entity); } } })); } }); } private void BindSubCategory(ProductType entity) { Task.Factory.StartNew(() => { List lists = null; if (this._dataSource != null) { lists = this._dataSource.FindAll(x => x.Path.Contains(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 OnSubCategoryXCheckedChanged(object sender, CategoryXEventArgs e) { OnCategoryXCheckedChanged(new CategoryXEventArgs(e.Data)); } private void OnMainCategoryXCheckedChanged(object sender, CategoryXEventArgs e) { BindSubCategory(e.Data); OnCategoryXCheckedChanged(new CategoryXEventArgs(e.Data)); } public event CategoryXEventHandler CategoryXCheckedChanged; protected virtual void OnCategoryXCheckedChanged(CategoryXEventArgs e) { CategoryXCheckedChanged?.Invoke(this, e); } } }