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.Utils; using System.Threading.Tasks; using POSV.Entity; using POSV.Bean; using POSV.MessageEvent; using POS.Language.Language; namespace POSV.Component { [ToolboxItem(true)] public partial class ShoppingCartParameter : AbstractParameter { /// /// 默认参数 /// private string[] rowHeight = { "20" , "25" , "30" , "35" , "40" , "45" }; ///// ///// 修改前的数据 ///// //private Dictionary _oldValue = new Dictionary(); ///// ///// 修改后的数据 ///// //private Dictionary _newValue = new Dictionary(); /// /// 初始化标识 /// private bool _inited = false; private ShoppingCartItem cartItem = null; public ShoppingCartParameter() { 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.comboBoxRowHeight.DataSource = rowHeight; this.InitUi(); } /// /// 初始化界面 /// private void InitUi() { //界面加载开始 this._inited = false; Task.Factory.StartNew(() => { Dictionary data = null; //获取品项分组下的全部参数 using (var db = Global.Instance.OpenDataBase) { data = db.Dictionary(string.Format(SqlConstant.ConfigQueryByGroupToDictionary , ConfigConstant.CART_GROUP)); } //将新增加或者变更的参数重新更新到数据库 var defaultValue = ConfigConstant.CartGroupDefaultValue(); //数据库有配置参数 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.CART_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.CART_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.CART_JSON_TEMPLATE].ToString(); //将JSON转换为行对象 this.cartItem = JsonUtils.Deserialize(json); /***********************点单列表定义****************************/ //将JSON转换为行对象 var rows = cartItem.Rows; foreach (CartRow row in rows) { switch (row.Order) { case 1: this.labelX40.Text = row.Order.ToString(); this.labelX35.Text = LangProxy.ToLang( row.Text); this.textBoxX1.Text = row.Alias; this.checkBoxX62.Checked = row.Display; this.numericTextBox1.Text = row.Width.ToString(); break; case 2: this.labelX41.Text = row.Order.ToString(); this.labelX31.Text = LangProxy.ToLang(row.Text); this.textBoxX2.Text = row.Alias; this.checkBoxX57.Checked = row.Display; this.numericTextBox2.Text = row.Width.ToString(); break; case 3: this.labelX42.Text = row.Order.ToString(); this.labelX32.Text = row.Text; this.textBoxX4.Text = row.Alias; this.checkBoxX58.Checked = row.Display; this.numericTextBox3.Text = row.Width.ToString(); break; case 4: this.labelX43.Text = row.Order.ToString(); this.labelX36.Text = LangProxy.ToLang(row.Text); this.textBoxX3.Text = row.Alias; this.checkBoxX63.Checked = row.Display; this.numericTextBox4.Text = row.Width.ToString(); break; case 5: this.labelX44.Text = row.Order.ToString(); this.labelX33.Text = LangProxy.ToLang(row.Text); this.textBoxX5.Text = row.Alias; this.checkBoxX59.Checked = row.Display; this.numericTextBox5.Text = row.Width.ToString(); break; case 6: this.labelX45.Text = row.Order.ToString(); this.labelX34.Text = LangProxy.ToLang(row.Text); this.textBoxX6.Text = row.Alias; this.checkBoxX61.Checked = row.Display; this.numericTextBox6.Text = row.Width.ToString(); break; case 7: this.labelX101.Text = row.Order.ToString(); this.labelX102.Text = LangProxy.ToLang(row.Text); this.textBoxX34.Text = row.Alias; this.checkBoxX4.Checked = row.Display; this.numericTextBox7.Text = row.Width.ToString(); break; case 8: this.labelX1.Text = row.Order.ToString(); this.labelX2.Text = LangProxy.ToLang(row.Text); this.textBoxX7.Text = row.Alias; this.checkBoxX1.Checked = row.Display; this.numericTextBox8.Text = row.Width.ToString(); break; } } //背景色 this.comboBoxColor1.Color = ColorTranslator.FromHtml(cartItem.TitleColor); //字体大小 SystemFont fontSize = cartItem.TitleFont; this.comboBoxFontSize.SelectedIndex = this.comboBoxFontSize.FindString(fontSize.ToString()); //字体颜色 this.comboBoxTextColor.Color = ColorTranslator.FromHtml(cartItem.TextColor); //高度 this.comboBoxRowHeight.Text = cartItem.RowHeight.ToString(); //界面加载完成 this._inited = true; } public List NewChanged() { var result = new List(); if (!this._inited) return result; var newItem = new ShoppingCartItem(); var rows = new List(); var row = new CartRow(); row.Name = "order"; row.Order = Convert.ToInt32(this.labelX40.Text.Trim()); row.Text = this.labelX35.Text.Trim(); row.Alias = this.textBoxX1.Text.Trim(); row.Display = this.checkBoxX62.Checked; row.Width = this.numericTextBox1.IntegerValue; rows.Add(row); row = new CartRow(); row.Name = "name"; row.Order = Convert.ToInt32(this.labelX41.Text.Trim()); row.Text = this.labelX31.Text.Trim(); row.Alias = this.textBoxX2.Text.Trim(); row.Display = this.checkBoxX57.Checked; row.Width = this.numericTextBox2.IntegerValue; rows.Add(row); row = new CartRow(); row.Name = "quantity"; row.Order = Convert.ToInt32(this.labelX42.Text.Trim()); row.Text = this.labelX32.Text.Trim(); row.Alias = this.textBoxX4.Text.Trim(); row.Display = this.checkBoxX58.Checked; row.Width = this.numericTextBox3.IntegerValue; rows.Add(row); row = new CartRow(); row.Name = "unit"; row.Order = Convert.ToInt32(this.labelX43.Text.Trim()); row.Text = this.labelX36.Text.Trim(); row.Alias = this.textBoxX3.Text.Trim(); row.Display = this.checkBoxX63.Checked; row.Width = this.numericTextBox4.IntegerValue; rows.Add(row); row = new CartRow(); row.Name = "price"; row.Order = Convert.ToInt32(this.labelX44.Text.Trim()); row.Text = this.labelX33.Text.Trim(); row.Alias = this.textBoxX5.Text.Trim(); row.Display = this.checkBoxX59.Checked; row.Width = this.numericTextBox5.IntegerValue; rows.Add(row); row = new CartRow(); row.Name = "amount"; row.Order = Convert.ToInt32(this.labelX45.Text.Trim()); row.Text = this.labelX34.Text.Trim(); row.Alias = this.textBoxX6.Text.Trim(); row.Display = this.checkBoxX61.Checked; row.Width = this.numericTextBox6.IntegerValue; rows.Add(row); row = new CartRow(); row.Name = "discount"; row.Order = Convert.ToInt32(this.labelX101.Text.Trim()); row.Text = this.labelX102.Text.Trim(); row.Alias = this.textBoxX34.Text.Trim(); row.Display = this.checkBoxX4.Checked; row.Width = this.numericTextBox7.IntegerValue; rows.Add(row); row = new CartRow(); row.Name = "scheme"; row.Order = Convert.ToInt32(this.labelX1.Text.Trim()); row.Text = this.labelX2.Text.Trim(); row.Alias = this.textBoxX7.Text.Trim(); row.Display = this.checkBoxX1.Checked; row.Width = this.numericTextBox8.IntegerValue; rows.Add(row); //列表 newItem.Rows = rows; //高度 int rowHeight = 0; int.TryParse(this.comboBoxRowHeight.Text , out rowHeight); newItem.RowHeight = rowHeight; //字体大小 SystemFont fontSize = SystemFont.默认; Enum.TryParse(this.comboBoxFontSize.SelectedItem.ToString() , out fontSize); newItem.TitleFont = fontSize; //字体颜色 newItem.TextColor = ColorTranslator.ToHtml(this.comboBoxTextColor.Color); //背景色1 newItem.TitleColor = ColorTranslator.ToHtml(this.comboBoxColor1.Color); //判断是否更改,如果过更改压入到 NewValue bool isChanged = !(newItem.Equals(this.cartItem)); if (isChanged) { //this._newValue[ConfigConstant.CART_JSON_TEMPLATE] = JsonUtils.Serialize(newItem); var config = new Config(); config.Id = IdWorkerUtils.Instance.NextId(); config.Group = ConfigConstant.CART_GROUP; config.TenantId = Global.Instance.Authc.TenantId; config.Keys = ConfigConstant.CART_JSON_TEMPLATE; config.Values = JsonUtils.Serialize(newItem); result.Add(config); } ////转换为Config对象,便于存储 //foreach (var v in this._newValue) //{ // var config = new Config(); // config.Id = IdWorkerUtils.Instance.NextId(); // config.Group = ConfigConstant.CART_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 { 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 UpdateCartUi(); } } return new Tuple(isSuccess , message); } private void UpdateCartUi() { Config config = null; using (var db = Global.Instance.OpenDataBase) { string sql = string.Format(SqlConstant.ConfigQueryByGroupAndKey , ConfigConstant.CART_GROUP , ConfigConstant.CART_JSON_TEMPLATE); config = db.SingleOrDefault(sql); } if (config != null) { var item = JsonUtils.Deserialize(config.Values); //发送分组通知 MsgEvent.Send(Constant.CART_CHANGED_NOTIFY , item); } } } }