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 POSV.Utils; using POSV.Entity; using POSV.Bean; using POSV.MessageEvent; namespace POSV.Component { [ToolboxItem(true)] public partial class ViceParameter : AbstractParameter { /// /// 修改前的数据 /// private Dictionary _oldValue = new Dictionary(); /// /// 修改后的数据 /// private Dictionary _newValue = new Dictionary(); /// /// 是否初始化 /// private bool _inited = false; private int[] _numArr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0}; public ViceParameter() { InitializeComponent(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (this.DesignMode) return; this.BackColor = Color.Transparent; lunboIntervalCom.DataSource = (int[])_numArr.Clone(); fullScreenWaitCom.DataSource = (int[])_numArr.Clone(); //字体大小 this.txtFontCom.DataSource = System.Enum.GetNames(typeof(SystemFont)); this.txtFontCom.SelectedIndex = 0; //字体轮播速度 this.txtSpeedCom.DataSource = System.Enum.GetNames(typeof(SpeedEnum)); this.txtFontCom.SelectedIndex = 0; //字体显示位置 this.txtShowPositionCom.DataSource = System.Enum.GetNames(typeof(ShowPositionEnum)); this.txtShowPositionCom.SelectedIndex = 0; //图片显示方式 this.imageSizeModeCom.DataSource = System.Enum.GetNames(typeof(ImageSizeModeEnum)); this.imageSizeModeCom.SelectedIndex = 0; 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.DEVICE_GROUP)); } //将新增加或者变更的参数重新更新到数据库 var defaultValue = ConfigConstant.DeviceGroupDefaultValue(); //数据库有配置参数 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.DEVICE_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 { //加载系统默认参数 data = defaultValue; var list = new List(); foreach (var v in defaultValue) { var config = new Config(); config.Id = IdWorkerUtils.Instance.NextId(); config.Group = ConfigConstant.DEVICE_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(); //是否启用副屏 this.enableViceCom.Value = data[ConfigConstant.DEVICE_VICE_ENABLE] == "1"; //副屏参数 var paramStr = data[ConfigConstant.DEVICE_VICE_PARAM]; if (!string.IsNullOrEmpty(paramStr)) { var param = JsonUtils.Deserialize(paramStr); //无操作,多长时间开始全屏轮播 this.fullScreenWaitCom.Text = param.PicLunboFullScreenSec.ToString(); //轮播时间间隔 this.lunboIntervalCom.Text = param.PicLunboInterval.ToString(); //图片显示方式 this.imageSizeModeCom.Text = param.PicSizeMode.ToString(); //文本轮播速度 this.txtSpeedCom.Text = param.TxtSpeed.ToString(); //文本颜色 this.txtColorCom.Color = ColorTranslator.FromHtml(param.TxtColor); //文本字体 this.txtFontCom.Text = param.TxtFont.ToString(); //文本背景颜色 this.txtBgColorCom.Color = ColorTranslator.FromHtml(param.TxtBgColor); //文本显示位置 this.txtShowPositionCom.Text = param.TxtShowPosition.ToString(); //是否显示商品图片 this.chkShowProductImage.Checked = param.ShowProductImage; } //界面加载完成 this._inited = true; } public List NewChanged() { var result = new List(); //尚未初始化完成 if (!this._inited) return result; //是否启用副屏 var enableVice = this.enableViceCom.Value ? "1" : "0"; bool changed = !(this._oldValue.ContainsKey(ConfigConstant.DEVICE_VICE_ENABLE) && this._oldValue[ConfigConstant.DEVICE_VICE_ENABLE].Equals(enableVice)); if (changed) { this._newValue[ConfigConstant.DEVICE_VICE_ENABLE] = enableVice; } ViceSettingParam param = new ViceSettingParam(); //无操作,多长时间开始全屏轮播 param.PicLunboFullScreenSec = StringUtils.GetInt(this.fullScreenWaitCom.Text); //轮播时间间隔 param.PicLunboInterval = StringUtils.GetInt(this.lunboIntervalCom.Text); //图片显示方式 param.PicSizeMode = (ImageSizeModeEnum)Enum.Parse(typeof(ImageSizeModeEnum), this.imageSizeModeCom.Text); //文本轮播速度 param.TxtSpeed = (SpeedEnum)Enum.Parse(typeof(SpeedEnum), this.txtSpeedCom.Text); //文本颜色 param.TxtColor = ColorTranslator.ToHtml(this.txtColorCom.Color); //文本字体 param.TxtFont = (SystemFont)Enum.Parse(typeof(SystemFont), this.txtFontCom.Text); //文本背景颜色 param.TxtBgColor = ColorTranslator.ToHtml(this.txtBgColorCom.Color); //文本显示位置 param.TxtShowPosition = (ShowPositionEnum)Enum.Parse(typeof(ShowPositionEnum), this.txtShowPositionCom.Text); //显示商品图片 param.ShowProductImage = this.chkShowProductImage.Checked; var p = JsonUtils.Serialize(param); changed = !(this._oldValue.ContainsKey(ConfigConstant.DEVICE_VICE_PARAM) && this._oldValue[ConfigConstant.DEVICE_VICE_PARAM].Equals(p)); if (changed) { this._newValue[ConfigConstant.DEVICE_VICE_PARAM] = p; } //转换为Config对象,便于存储 foreach (var v in this._newValue) { var config = new Config(); config.Id = IdWorkerUtils.Instance.NextId(); config.Group = ConfigConstant.DEVICE_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内容 this._newValue.Clear(); //更新控件UI Global.Instance.ReloadConfig(ConfigConstant.DEVICE_GROUP); //通知更新 MsgEvent.Send(Constant.VICE_CHANGED_NOTIFY, null); } } return new Tuple(isSuccess, message); } } }