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.Bean; using System.Threading.Tasks; using POSV.Utils; using POSV.Entity; using POSV.MessageEvent; using DevComponents.DotNetBar.Metro; namespace POSV.Component { [ToolboxItem(true)] public partial class ProductParameter : AbstractParameter { /// /// 默认参数 /// private string[] itemHeight = { "40" , "45" , "50" , "55" , "60" , "70" , "80" }; private string[] cols = { "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "11" , "12" , "13" , "14" , "15" }; private string[] itemWidth = { "0" , "70" , "80" , "90" , "100" , "110" , "120" , "130"}; private string[] pagerItemWidth = { "0" , "60" , "70" , "80" ,"90" , "100" , "120" }; private ProductItem productItem = null; private bool _inited = false; public ProductParameter() { InitializeComponent(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (this.DesignMode) return; this.BackColor = Color.Transparent; //字体支持列表 this.comboBoxFontSize.DataSource = System.Enum.GetNames(typeof(SystemFont)); //每行显示的品类数量 this.comboBoxColumns.DataSource = cols; //设置系统默认的行高度 this.comboBoxItemHeight.DataSource = itemHeight; //设置系统默认的自定义宽度 this.comboBoxItemWidth.DataSource = itemWidth; //品项的显示样式 this.comboBoxCornerType.DataSource = System.Enum.GetNames(typeof(CornerType)); //沽清字体支持列表 this.comboBoxSaleClearFont.DataSource = System.Enum.GetNames(typeof(SystemFont)); //选中字体支持列表 this.comboBoxEx1.DataSource = System.Enum.GetNames(typeof(SystemFont)); this.InitUi(); } /// /// 初始化界面 /// private void InitUi() { //界面加载开始 this._inited = false; Task.Factory.StartNew(() => { Dictionary data = null; lock (Global.Instance.SyncLock) { //获取品项分组下的全部参数 using (var db = Global.Instance.OpenDataBase) { data = db.Dictionary(string.Format(SqlConstant.ConfigQueryByGroupToDictionary , ConfigConstant.PRODUCT_GROUP)); } } //将新增加或者变更的参数重新更新到数据库 var defaultValue = ConfigConstant.ProductGroupDefaultValue(); //数据库有配置参数 if (data != null) { var diff = defaultValue.Where(kvp => !data.ContainsKey(kvp.Key)).ToDictionary(kvp => kvp.Key , kvp => kvp.Value); if (diff != null && diff.Keys.Count > 0) { var list = new List(); foreach (var v in diff) { var config = new Config(); config.Id = IdWorkerUtils.Instance.NextId(); config.Group = ConfigConstant.PRODUCT_GROUP; config.TenantId = Global.Instance.Authc.TenantId; config.Keys = v.Key; config.Values = v.Value; list.Add(config); } //如果数据表中没有配置group相关的信息,更新差异 this.SaveChanged(list); //将差异合并到data,避免再次访问数据库 data = data.Concat(diff).ToDictionary(x => x.Key , x => x.Value); } } else { //加载系统默认参数 var list = new List(); foreach (var v in defaultValue) { var config = new Config(); config.Id = IdWorkerUtils.Instance.NextId(); config.Group = ConfigConstant.PRODUCT_GROUP; config.TenantId = Global.Instance.Authc.TenantId; config.Keys = v.Key; config.Values = v.Value; list.Add(config); } //如果数据表中没有配置group相关的信息,保存 this.SaveChanged(list); } return data; }).ContinueWith(task => { if (task.IsFaulted) { LOGGER.Error(task.Exception.GetBaseException() , "加载品项设置参数异常"); } else { this.Invoke(new Action(() => { BindUi(task.Result); })); } }); } private void BindUi(Dictionary data) { ////变更前的数据单独存储 //_oldValue.Clear(); //_oldValue = ObjectUtils.Copy>(data); ////清理历史变更数据 //_newValue.Clear(); //点单列表显示定义 string json = data[ConfigConstant.PRODUCT_JSON_TEMPLATE].ToString(); //将JSON转换为行对象 this.productItem = JsonUtils.Deserialize(json); ////////////////////////////品项模版数据///////////////////////////////////// switch (productItem.ShowType) { case ProductShowType.品项全称: this.checkBoxX43.Checked = true; break; case ProductShowType.品项简称: this.checkBoxX44.Checked = true; break; case ProductShowType.全称和零售价: this.checkBoxX1.Checked = true; break; case ProductShowType.简称和零售价: this.checkBoxX2.Checked = true; break; } switch (productItem.ShowCase) { case ProductShowCase.分辨率1920X1080: metroTileItem60.Checked = true; break; case ProductShowCase.分辨率1024X768: metroTileItem61.Checked = true; break; case ProductShowCase.自定义: metroTileItem63.Checked = true; break; } //每行显示的数量 this.comboBoxColumns.SelectedIndex = this.comboBoxColumns.FindString(productItem.Columns.ToString()); //宽度 this.comboBoxItemWidth.Text = productItem.ItemWidth.ToString(); //高度 this.comboBoxItemHeight.Text = productItem.ItemHeight.ToString(); //品项显示的样式 this.comboBoxCornerType.SelectedIndex = this.comboBoxCornerType.FindString(productItem.ProductCornerType.ToString()); //字体大小 SystemFont fontSize = productItem.ProductFont; this.comboBoxFontSize.SelectedIndex = this.comboBoxFontSize.FindString(fontSize.ToString()); //字体颜色 this.comboBoxTextColor.Color = ColorTranslator.FromHtml(productItem.TextColor); //背景色1 this.comboBoxColor1.Color = ColorTranslator.FromHtml(productItem.BackColor1); //背景色2 this.comboBoxColor2.Color = ColorTranslator.FromHtml(productItem.BackColor2); //分页按钮 this.comboBoxPagerColor1.Color = ColorTranslator.FromHtml(productItem.PagerColor1); this.comboBoxPagerColor2.Color = ColorTranslator.FromHtml(productItem.PagerColor2); //沽清背景色 this.comboBoxColorPicker1.Color = ColorTranslator.FromHtml(productItem.SaleClearColor); //沽清字体颜色 this.comboBoxColorPicker2.Color = ColorTranslator.FromHtml(productItem.SaleClearTextColor); //沽清字体大小 SystemFont saleClearFontSize = productItem.SaleClearFont; this.comboBoxSaleClearFont.SelectedIndex = this.comboBoxSaleClearFont.FindString(saleClearFontSize.ToString()); //沽清提醒颜色 this.comboBoxColorPicker3.Color = ColorTranslator.FromHtml(productItem.SaleClearNotifyColor); //沽清停售颜色 this.comboBoxColorPicker4.Color = ColorTranslator.FromHtml(productItem.SaleStopColor); //选中背景色 this.comboBoxColorPicker5.Color = ColorTranslator.FromHtml(productItem.SelBackColor); //选中字体 SystemFont selFontSize = productItem.SelProductFont; this.comboBoxEx1.SelectedIndex = this.comboBoxEx1.FindString(selFontSize.ToString()); //选中字体颜色 this.comboBoxColorPicker6.Color = ColorTranslator.FromHtml(productItem.SelTextColor); //界面加载完成 this._inited = true; } public List NewChanged() { var result = new List(); if (!this._inited) return result; var newItem = new ProductItem(); if (this.checkBoxX43.Checked) { newItem.ShowType = ProductShowType.品项全称; } else if (this.checkBoxX44.Checked) { newItem.ShowType = ProductShowType.品项简称; } else if (this.checkBoxX1.Checked) { newItem.ShowType = ProductShowType.全称和零售价; } else { newItem.ShowType = ProductShowType.简称和零售价; } if (metroTileItem60.Checked) { newItem.ShowCase = ProductShowCase.分辨率1920X1080; } else if (metroTileItem61.Checked) { newItem.ShowCase = ProductShowCase.分辨率1024X768; } else { newItem.ShowCase = ProductShowCase.自定义; } //每行显示的数量 newItem.Columns = Convert.ToInt32(this.comboBoxColumns.SelectedItem.ToString()); //宽度 int itemWidth = 0; int.TryParse(this.comboBoxItemWidth.Text , out itemWidth); newItem.ItemWidth = itemWidth; //高度 int itemHeight = 0; int.TryParse(this.comboBoxItemHeight.Text , out itemHeight); newItem.ItemHeight = itemHeight; //显示样式 CornerType cornerType = CornerType.默认; Enum.TryParse(this.comboBoxCornerType.SelectedItem.ToString() , out cornerType); newItem.ProductCornerType = cornerType; //字体大小 SystemFont fontSize = SystemFont.默认; Enum.TryParse(this.comboBoxFontSize.SelectedItem.ToString() , out fontSize); newItem.ProductFont = fontSize; //字体颜色 newItem.TextColor = ColorTranslator.ToHtml(this.comboBoxTextColor.Color); //背景色1 newItem.BackColor1 = ColorTranslator.ToHtml(this.comboBoxColor1.Color); //背景色2 newItem.BackColor2 = ColorTranslator.ToHtml(this.comboBoxColor2.Color); //分页按钮 newItem.PagerColor1 = ColorTranslator.ToHtml(this.comboBoxPagerColor1.Color); newItem.PagerColor2 = ColorTranslator.ToHtml(this.comboBoxPagerColor2.Color); //选中背景色 newItem.SelBackColor = ColorTranslator.ToHtml(this.comboBoxColorPicker5.Color); //选中字体 SystemFont selFontSize = SystemFont.默认; Enum.TryParse(this.comboBoxEx1.SelectedItem.ToString(), out selFontSize); newItem.SelProductFont = selFontSize; //选中字体颜色 newItem.SelTextColor = ColorTranslator.ToHtml(this.comboBoxColorPicker6.Color); //沽清背景色 newItem.SaleClearColor = ColorTranslator.ToHtml(this.comboBoxColorPicker1.Color); //沽清字体颜色 newItem.SaleClearTextColor = ColorTranslator.ToHtml(this.comboBoxColorPicker2.Color); //沽清字体大小 SystemFont saleClearFontSize = SystemFont.默认; Enum.TryParse(this.comboBoxSaleClearFont.SelectedItem.ToString() , out saleClearFontSize); newItem.SaleClearFont = saleClearFontSize; //沽清提醒颜色 newItem.SaleClearNotifyColor = ColorTranslator.ToHtml(this.comboBoxColorPicker3.Color); //沽清停售颜色 newItem.SaleStopColor = ColorTranslator.ToHtml(this.comboBoxColorPicker4.Color); //判断是否更改,如果过更改压入到 NewValue bool isChanged = !(newItem.Equals(this.productItem)); if (isChanged) { var config = new Config(); config.Id = IdWorkerUtils.Instance.NextId(); config.Group = ConfigConstant.PRODUCT_GROUP; config.TenantId = Global.Instance.Authc.TenantId; config.Keys = ConfigConstant.PRODUCT_JSON_TEMPLATE; config.Values = JsonUtils.Serialize(newItem); result.Add(config); //this._newValue[ConfigConstant.PRODUCT_JSON_TEMPLATE] = JsonUtils.Serialize(newItem); } ////转换为Config对象,便于存储 //foreach (var v in this._newValue) //{ // var config = new Config(); // config.Id = IdWorkerUtils.Instance.NextId(); // config.Group = ConfigConstant.PRODUCT_GROUP; // config.TenantId = Global.Instance.Authc.TenantId; // config.Keys = v.Key; // config.Values = v.Value; // result.Add(config); //} return result; } public Tuple SaveChanged(List data) { bool isSuccess = true; string message = "参数更新成功"; try { lock (Global.Instance.SyncLock) { using (var db = Global.Instance.OpenDataBase) { using (var trans = db.GetTransaction()) { foreach (var config in data) { db.Save(config); } trans.Complete(); } } } } catch (Exception ex) { isSuccess = false; message = "参数更新异常"; LOGGER.Error(ex , message); } finally { if (isSuccess) { ////新修改后的参数应用后,重置OldValue,避免用户在没有切换Tab情况下,直接修改当前Tab内容 //foreach (var config in data) //{ // string key = config.Keys; // string value = config.Values; // this._oldValue[key] = value; //} //this._newValue.Clear(); //更新控件UI UpdateProductUi(); } } return new Tuple(isSuccess , message); } private void UpdateProductUi() { Config config = null; lock (Global.Instance.SyncLock) { using (var db = Global.Instance.OpenDataBase) { string sql = string.Format(SqlConstant.ConfigQueryByGroupAndKey , ConfigConstant.PRODUCT_GROUP , ConfigConstant.PRODUCT_JSON_TEMPLATE); config = db.SingleOrDefault(sql); } } if (config != null) { ProductItem item = JsonUtils.Deserialize(config.Values); this.productItem = item; //发送分组通知 MsgEvent.Send(Constant.PRODUCT_CHANGED_NOTIFY , item); } } private void OnShowCaseMouseDown(object sender , MouseEventArgs e) { MetroTileItem item = (MetroTileItem)sender; item.Checked = true; } private void OnShowCaseCheckedChanged(object sender , EventArgs e) { MetroTileItem item = (MetroTileItem)sender; if (item.Checked) { ProductShowCase showCase = ProductShowCase.分辨率1920X1080; Enum.TryParse(item.Tag.ToString() , out showCase); switch (showCase) { case ProductShowCase.分辨率1920X1080: groupBoxItem.Enabled = false; break; case ProductShowCase.分辨率1024X768: groupBoxItem.Enabled = false; break; case ProductShowCase.自定义: groupBoxItem.Enabled = true; break; } } } private void OnProductCornerTypeSelectedChanged(object sender , EventArgs e) { //显示样式 CornerType cornerType = CornerType.默认; Enum.TryParse(this.comboBoxCornerType.SelectedItem.ToString() , out cornerType); switch (cornerType) { case CornerType.默认: { this.metroTileItem60.TileStyle.CornerType = DevComponents.DotNetBar.eCornerType.Square; this.metroTileItem60.TileStyle.CornerDiameter = 8; this.metroTileItem61.TileStyle.CornerType = DevComponents.DotNetBar.eCornerType.Square; this.metroTileItem61.TileStyle.CornerDiameter = 8; this.metroTileItem63.TileStyle.CornerType = DevComponents.DotNetBar.eCornerType.Square; this.metroTileItem63.TileStyle.CornerDiameter = 8; } break; case CornerType.圆角: { this.metroTileItem60.TileStyle.CornerType = DevComponents.DotNetBar.eCornerType.Rounded; this.metroTileItem60.TileStyle.CornerDiameter = 8; this.metroTileItem61.TileStyle.CornerType = DevComponents.DotNetBar.eCornerType.Rounded; this.metroTileItem61.TileStyle.CornerDiameter = 8; this.metroTileItem63.TileStyle.CornerType = DevComponents.DotNetBar.eCornerType.Rounded; this.metroTileItem63.TileStyle.CornerDiameter = 8; } break; case CornerType.棱角: { this.metroTileItem60.TileStyle.CornerType = DevComponents.DotNetBar.eCornerType.Diagonal; this.metroTileItem60.TileStyle.CornerDiameter = 8; this.metroTileItem61.TileStyle.CornerType = DevComponents.DotNetBar.eCornerType.Diagonal; this.metroTileItem61.TileStyle.CornerDiameter = 8; this.metroTileItem63.TileStyle.CornerType = DevComponents.DotNetBar.eCornerType.Diagonal; this.metroTileItem63.TileStyle.CornerDiameter = 8; } break; } } } }