You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

363 lines
13 KiB
C#

9 months ago
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.Entity;
using POSV.Utils;
using POSV.Bean;
using POSV.MessageEvent;
namespace POSV.Component
{
[ToolboxItem(true)]
public partial class SpecMakeParameter : AbstractParameter
{
/// <summary>
/// 默认参数
/// </summary>
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" };
/// <summary>
/// 修改前的数据
/// </summary>
private Dictionary<string, string> _oldValue = new Dictionary<string, string>();
/// <summary>
/// 修改后的数据
/// </summary>
private Dictionary<string, string> _newValue = new Dictionary<string, string>();
/// <summary>
/// 是否初始化
/// </summary>
private bool _inited = false;
private ProductMakeItem _makeItem = null;
private ProductSpecItem _specItem = null;
public SpecMakeParameter()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.DesignMode) return;
this.BackColor = Color.Transparent;
//==============做法============
//字体大小
this.comboBoxEx5.DataSource = Enum.GetValues(typeof(SystemFont));
//cols
this.comboBoxEx8.DataSource = (string[])cols.Clone();
//宽度
this.comboBoxEx7.DataSource = (string[])itemWidth.Clone();
//高度
this.comboBoxEx6.DataSource = (string[])itemHeight.Clone();
//==============规格============
//字体大小
this.comboBoxEx9.DataSource = Enum.GetValues(typeof(SystemFont));
//cols
this.comboBoxEx1.DataSource = (string[])cols.Clone();
//宽度
this.comboBoxEx3.DataSource = (string[])itemWidth.Clone();
//高度
this.comboBoxEx2.DataSource = (string[])itemHeight.Clone();
this.InitUi();
}
/// <summary>
/// 初始化界面
/// </summary>
private void InitUi()
{
//界面加载开始
this._inited = false;
Task.Factory.StartNew(() => {
Dictionary<string, string> data = null;
//获取品类分组下的全部参数
using (var db = Global.Instance.OpenDataBase)
{
data = db.Dictionary<string, string>(string.Format(SqlConstant.ConfigQueryByGroupToDictionary, ConfigConstant.SPECMAKE_GROUP));
}
//将新增加或者变更的参数重新更新到数据库
var defaultValue = ConfigConstant.SpecMakeGroupDefaultValue();
//数据库有配置参数
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<Config>();
foreach (var v in diff)
{
var config = new Config();
config.Id = IdWorkerUtils.Instance.NextId();
config.Group = ConfigConstant.SPECMAKE_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<Config>();
foreach (var v in defaultValue)
{
var config = new Config();
config.Id = IdWorkerUtils.Instance.NextId();
config.Group = ConfigConstant.SPECMAKE_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<string, string> data)
{
//变更前的数据单独存储
_oldValue.Clear();
_oldValue = ObjectUtils.Copy<Dictionary<string, string>>(data);
//清理历史变更数据
_newValue.Clear();
//做法
var makeJson = data[ConfigConstant.MAKE_JSON_TEMPLATE];
if (!string.IsNullOrEmpty(makeJson))
{
_makeItem = JsonUtils.Deserialize<ProductMakeItem>(makeJson);
if (_makeItem != null)
{
//是否显示售价
this.checkBoxX4.Checked = _makeItem.ShowPrice;
//背景颜色
this.comboBoxColorPicker6.Color = ColorTranslator.FromHtml(_makeItem.BackColor1);
this.comboBoxColorPicker5.Color = ColorTranslator.FromHtml(_makeItem.BackColor2);
//字体颜色
this.comboBoxColorPicker4.Color = ColorTranslator.FromHtml(_makeItem.TxtColor);
//字体大小
this.comboBoxEx5.Text = _makeItem.MakeFont.ToString();
//每行显示列数
this.comboBoxEx8.Text = _makeItem.Cols.ToString();
//宽度
this.comboBoxEx7.Text = _makeItem.ItemWidth.ToString();
//高度
this.comboBoxEx6.Text = _makeItem.ItemHeight.ToString();
}
}
//规格
var specJson = data[ConfigConstant.SPEC_JSON_TEMPLATE];
if (!string.IsNullOrEmpty(specJson))
{
var _specItem = JsonUtils.Deserialize<ProductSpecItem>(specJson);
if (_specItem != null)
{
//是否显示售价
this.checkBoxX1.Checked = _specItem.ShowPrice;
//背景颜色
this.comboBoxColorPicker9.Color = ColorTranslator.FromHtml(_specItem.BackColor1);
this.comboBoxColorPicker8.Color = ColorTranslator.FromHtml(_specItem.BackColor2);
//字体颜色
this.comboBoxColorPicker4.Color = ColorTranslator.FromHtml(_specItem.TxtColor);
//字体大小
this.comboBoxEx9.Text = _specItem.SpecFont.ToString();
//每行显示列数
this.comboBoxEx1.Text = _specItem.Cols.ToString();
//宽度
this.comboBoxEx3.Text = _specItem.ItemWidth.ToString();
//高度
this.comboBoxEx2.Text = _specItem.ItemHeight.ToString();
}
}
//界面加载完成
this._inited = true;
}
public List<Config> NewChanged()
{
var result = new List<Config>();
//尚未初始化完成
if (!this._inited) return result;
//做法
ProductMakeItem newMake = new ProductMakeItem();
newMake.ShowPrice = this.checkBoxX4.Checked;
newMake.BackColor1 = ColorTranslator.ToHtml(this.comboBoxColorPicker6.Color);
newMake.BackColor2 = ColorTranslator.ToHtml(this.comboBoxColorPicker5.Color);
newMake.TxtColor = ColorTranslator.ToHtml(this.comboBoxColorPicker4.Color);
newMake.MakeFont = (SystemFont)Enum.Parse(typeof(SystemFont), this.comboBoxEx5.Text);
newMake.Cols = StringUtils.GetInt(this.comboBoxEx8.Text);
newMake.ItemWidth = StringUtils.GetInt(this.comboBoxEx7.Text);
newMake.ItemHeight = StringUtils.GetInt(this.comboBoxEx6.Text);
//判断是否更改,如果过更改压入到 NewValue
bool isChanged = !(newMake.Equals(this._makeItem));
if (isChanged)
{
var config = new Config();
config.Id = IdWorkerUtils.Instance.NextId();
config.Group = ConfigConstant.SPECMAKE_GROUP;
config.TenantId = Global.Instance.Authc.TenantId;
config.Keys = ConfigConstant.MAKE_JSON_TEMPLATE;
config.Values = JsonUtils.Serialize(newMake);
result.Add(config);
}
//规格
ProductSpecItem newSpec = new ProductSpecItem();
newSpec.ShowPrice = this.checkBoxX1.Checked;
newSpec.BackColor1 = ColorTranslator.ToHtml(this.comboBoxColorPicker9.Color);
newSpec.BackColor2 = ColorTranslator.ToHtml(this.comboBoxColorPicker8.Color);
newSpec.TxtColor = ColorTranslator.ToHtml(this.comboBoxColorPicker4.Color);
newSpec.SpecFont = (SystemFont)Enum.Parse(typeof(SystemFont), this.comboBoxEx9.Text);
newSpec.Cols = StringUtils.GetInt(this.comboBoxEx1.Text);
newSpec.ItemWidth = StringUtils.GetInt(this.comboBoxEx3.Text);
newSpec.ItemHeight = StringUtils.GetInt(this.comboBoxEx2.Text);
//判断是否更改,如果过更改压入到 NewValue
isChanged = !(newSpec.Equals(this._specItem));
if (isChanged)
{
var config = new Config();
config.Id = IdWorkerUtils.Instance.NextId();
config.Group = ConfigConstant.SPECMAKE_GROUP;
config.TenantId = Global.Instance.Authc.TenantId;
config.Keys = ConfigConstant.SPEC_JSON_TEMPLATE;
config.Values = JsonUtils.Serialize(newSpec);
result.Add(config);
}
return result;
}
public Tuple<bool, string> SaveChanged(List<Config> 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)
{
UpdateSpecMakeUi();
}
}
return new Tuple<bool, string>(isSuccess, message);
}
private void UpdateSpecMakeUi()
{
Config specConfig = null;
Config flavorConfig = null;
lock (Global.Instance.SyncLock)
{
using (var db = Global.Instance.OpenDataBase)
{
string sql = string.Format(SqlConstant.ConfigQueryByGroupAndKey, ConfigConstant.SPECMAKE_GROUP, ConfigConstant.SPEC_JSON_TEMPLATE);
specConfig = db.SingleOrDefault<Config>(sql);
sql = string.Format(SqlConstant.ConfigQueryByGroupAndKey, ConfigConstant.SPECMAKE_GROUP, ConfigConstant.MAKE_JSON_TEMPLATE);
flavorConfig = db.SingleOrDefault<Config>(sql);
}
}
if (specConfig != null)
{
ProductSpecItem item = JsonUtils.Deserialize<ProductSpecItem>(specConfig.Values);
//发送分组通知
MsgEvent.Send(Constant.SPEC_CHANGED_NOTIFY, item);
}
if (flavorConfig != null)
{
ProductMakeItem item = JsonUtils.Deserialize<ProductMakeItem>(flavorConfig.Values);
//发送分组通知
MsgEvent.Send(Constant.FLAVOR_CHANGED_NOTIFY, item);
}
}
}
}