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;
namespace POSV.Component
{
[ToolboxItem(true)]
public partial class LayoutParameter : AbstractParameter
{
///
/// 修改前的数据
///
private Dictionary _oldValue = new Dictionary();
///
/// 修改后的数据
///
private Dictionary _newValue = new Dictionary();
///
/// 是否初始化
///
private bool _inited = false;
public LayoutParameter()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.DesignMode) return;
this.BackColor = Color.Transparent;
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.LAYOUT_GROUP));
}
//将新增加或者变更的参数重新更新到数据库
var defaultValue = ConfigConstant.LayoutGroupDefaultValue();
//数据库有配置参数
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.LAYOUT_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.LAYOUT_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();
//启用的主题
var enableTheme = (ThemeEnum)Enum.Parse(typeof(ThemeEnum), data[ConfigConstant.LAYOUT_THEME]);
switch (enableTheme)
{
case ThemeEnum.食堂:
this.shiTangCom.Checked = true;
break;
case ThemeEnum.茶饮:
this.teaCom.Checked = true;
break;
case ThemeEnum.称重:
this.weightCom.Checked = true;
break;
case ThemeEnum.新食堂:
this.newShiTangCom.Checked = true;
break;
case ThemeEnum.快餐:
default:
this.kuaiCanCom.Checked = true;
break;
}
//界面加载完成
this._inited = true;
}
public List NewChanged()
{
var result = new List();
//尚未初始化完成
if (!this._inited) return result;
//启用的版本
ThemeEnum enableTheme = ThemeEnum.快餐;
if (this.teaCom.Checked)
{
enableTheme = ThemeEnum.茶饮;
}
else if (this.weightCom.Checked)
{
enableTheme = ThemeEnum.称重;
}
else if (this.shiTangCom.Checked)
{
enableTheme = ThemeEnum.食堂;
}
else if(this.kuaiCanCom.Checked)
{
enableTheme = ThemeEnum.快餐;
}
else if (this.newShiTangCom.Checked)
{
enableTheme = ThemeEnum.新食堂;
}
//是否启用服务中心
bool changed = !(this._oldValue.ContainsKey(ConfigConstant.LAYOUT_THEME) && this._oldValue[ConfigConstant.LAYOUT_THEME].Equals(enableTheme.ToString()));
if (changed)
{
this._newValue[ConfigConstant.LAYOUT_THEME] = enableTheme.ToString();
}
//转换为Config对象,便于存储
foreach (var v in this._newValue)
{
var config = new Config();
config.Id = IdWorkerUtils.Instance.NextId();
config.Group = ConfigConstant.LAYOUT_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();
}
}
return new Tuple(isSuccess, message);
}
}
}