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.

310 lines
10 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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 DevComponents.Editors;
using DevComponents.DotNetBar.Controls;
namespace POSV.Component
{
[ToolboxItem(true)]
public partial class ServiceCenterParameter : AbstractParameter
{
/// <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;
public ServiceCenterParameter()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.DesignMode) return;
this.BackColor = Color.Transparent;
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.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<Config>();
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<Config>();
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<string, string> data)
{
//变更前的数据单独存储
_oldValue.Clear();
_oldValue = ObjectUtils.Copy<Dictionary<string, string>>(data);
//清理历史变更数据
_newValue.Clear();
//是否启用服务中心
this.enableServiceCenter.Value = data[ConfigConstant.DEVICE_SERVICECENTER_ENABLE] == "1";
//服务中心IP
this.txtIpAddressInput.Value = data[ConfigConstant.DEVICE_SERVICECENTER_IP];
//服务中心端口
int port = 2018;
int.TryParse(data[ConfigConstant.DEVICE_SERVICECENTER_PORT], out port);
this.txtPortInput.Value = port;
//CheckBoxX处理
foreach (Control control in tabPanel.Controls)
{
if (control is CheckBoxX)
{
var item = control as CheckBoxX;
var key = item.Tag.ToString();
if (data.ContainsKey(key))
{
string _value = data[key].ToString();
item.Checked = _value.Equals("1");
}
else
{
item.Checked = false;
}
}
}
//界面加载完成
this._inited = true;
}
public List<Config> NewChanged()
{
var result = new List<Config>();
//尚未初始化完成
if (!this._inited) return result;
//是否启用服务中心
var enableService = this.enableServiceCenter.Value ? "1" : "0";
bool changed = !(this._oldValue.ContainsKey(ConfigConstant.DEVICE_SERVICECENTER_ENABLE) && this._oldValue[ConfigConstant.DEVICE_SERVICECENTER_ENABLE].Equals(enableService));
if (changed)
{
this._newValue[ConfigConstant.DEVICE_SERVICECENTER_ENABLE] = enableService;
}
//服务中心IP
var ip = this.txtIpAddressInput.Value;
changed = !(this._oldValue.ContainsKey(ConfigConstant.DEVICE_SERVICECENTER_IP) && this._oldValue[ConfigConstant.DEVICE_SERVICECENTER_IP].Equals(ip));
if (changed)
{
this._newValue[ConfigConstant.DEVICE_SERVICECENTER_IP] = ip;
}
//服务中心端口
var port = this.txtPortInput.Value.ToString();
changed = !(this._oldValue.ContainsKey(ConfigConstant.DEVICE_SERVICECENTER_PORT) && this._oldValue[ConfigConstant.DEVICE_SERVICECENTER_PORT].Equals(port));
if (changed)
{
this._newValue[ConfigConstant.DEVICE_SERVICECENTER_PORT] = port;
}
//CheckBoxX处理
foreach (Control control in tabPanel.Controls)
{
if (control is CheckBoxX)
{
var item = control as CheckBoxX;
var key = item.Tag.ToString();
//忽略为空的
if (string.IsNullOrEmpty(key)) continue;
var value = item.Checked ? "1" : "0";
//判断是否更改,如果过更改压入到 NewValue
changed = !(this._oldValue.ContainsKey(key) && this._oldValue[key].Equals(value));
if (changed)
{
this._newValue[key] = value;
}
}
}
//转换为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<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)
{
//新修改后的参数应用后重置OldValue避免用户在没有切换Tab情况下直接修改当前Tab内容
this._newValue.Clear();
//更新控件UI
Global.Instance.ReloadConfig(ConfigConstant.DEVICE_GROUP);
bool enabled = Global.Instance.GlobalConfigBoolValue(ConfigConstant.DEVICE_SERVICECENTER_ENABLE,false);
if (enabled)
{
//更新服务中心连接状态
WSSE.Instance.RestartConnect();
//更新局域网MQTT服务连接
MqttUtils.Instance.Restart();
}
}
}
return new Tuple<bool, string>(isSuccess, message);
}
private void OnIpAddressCustomClick(object sender, EventArgs e)
{
var item = sender as IpAddressInput;
NumericKeyboard.ShowKeyboard(this, item);
}
private void OnServicePortClick(object sender, EventArgs e)
{
var item = sender as IntegerInput;
NumericKeyboard.ShowKeyboard(this, item);
}
}
}