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.

372 lines
13 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 POSV.Bean;
namespace POSV.Component
{
[ToolboxItem(true)]
public partial class GuestShowParameter : 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 GuestShowParameter()
{
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.guestShowPortCom.Items.Clear();
this.guestShowPortCom.Items.AddRange(Constant.COM_PORT_NAME_VALUE);
//是否启用客显
this.enableGuestShowCom.Value = data[ConfigConstant.DEVICE_GUESTSHOW_ENABLE] == "1";
//加载支持的客显
List<GuestShowItem> itemList = new List<GuestShowItem>();
var defaultGuestShowItem = Global.Instance.GetDefaultGuestShowItem();
itemList.Add(defaultGuestShowItem);
List<GuestShowDevice> showDeviceList = null;
using (var db = Global.Instance.OpenDataBase)
{
showDeviceList = db.Fetch<GuestShowDevice>();
}
if (showDeviceList.Count > 0)
{
foreach (var device in showDeviceList)
{
GuestShowItem item = new GuestShowItem();
item.Name = device.Name;
item.Parameter = JsonUtils.Deserialize<POSV.Bean.GuestShowParameter>(device.Param);
itemList.Add(item);
}
}
this.guestShowNameCom.Items.Clear();
this.guestShowNameCom.Items.AddRange(itemList.ToArray());
this.guestShowNameCom.DisplayMember = "name";
if (data.ContainsKey(ConfigConstant.DEVICE_GUESTSHOW_PARAMETER))
{
var json = data[ConfigConstant.DEVICE_GUESTSHOW_PARAMETER];
var item = JsonUtils.Deserialize<GuestShowItem>(json);
if (item != null)
{
this.guestShowNameCom.Tag = item.Parameter;
var index = itemList.FindIndex(x => x.Name == item.Name);
this.guestShowNameCom.SelectedIndex = index < 0 ? 0 : index;
this.guestShowPortCom.Text = item.Port;
}
else
{
this.guestShowNameCom.SelectedIndex = 0;
this.guestShowPortCom.SelectedIndex = 0;
}
}
else
{
this.guestShowNameCom.SelectedIndex = 0;
this.guestShowPortCom.SelectedIndex = 0;
}
//界面加载完成
this._inited = true;
}
public List<Config> NewChanged()
{
var result = new List<Config>();
//尚未初始化完成
if (!this._inited) return result;
//是否启用客显
var enableGuestShow = this.enableGuestShowCom.Value ? "1" : "0";
bool changed = !(this._oldValue.ContainsKey(ConfigConstant.DEVICE_GUESTSHOW_ENABLE) && this._oldValue[ConfigConstant.DEVICE_GUESTSHOW_ENABLE].Equals(enableGuestShow));
if (changed)
{
this._newValue[ConfigConstant.DEVICE_GUESTSHOW_ENABLE] = enableGuestShow;
}
//客显参数
var item = new GuestShowItem();
item.Name = this.guestShowNameCom.Text;
item.Port = this.guestShowPortCom.Text;
var showItem = this.guestShowNameCom.SelectedItem as POSV.Bean.GuestShowItem;
if (showItem != null)
{
item.Parameter = showItem.Parameter;
}
var itemJson = JsonUtils.Serialize(item);
changed = !(this._oldValue.ContainsKey(ConfigConstant.DEVICE_GUESTSHOW_PARAMETER) && this._oldValue[ConfigConstant.DEVICE_GUESTSHOW_PARAMETER].Equals(itemJson));
if (changed)
{
this._newValue[ConfigConstant.DEVICE_GUESTSHOW_PARAMETER] = itemJson;
}
//转换为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);
//更新客显配置
GuestShowUtils.RefreshInstance();
}
}
return new Tuple<bool, string>(isSuccess, message);
}
/// <summary>
/// 修改配置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnModifyParamClick(object sender, EventArgs e)
{
if (this.guestShowNameCom.Text == "None")
{
MessageBox.Show("请选择客显");
return;
}
var item = this.guestShowNameCom.SelectedItem as GuestShowItem;
string param = string.Empty;
if (item != null)
{
param = JsonUtils.Serialize(item.Parameter);
}
GuestShowModifyForm form = new GuestShowModifyForm(param);
form.KxCmdCommit -= OnGuestShowParamModifySubmit;
form.KxCmdCommit += OnGuestShowParamModifySubmit;
form.ShowDialog();
}
private void OnGuestShowParamModifySubmit(string kxParam)
{
var item = this.guestShowNameCom.SelectedItem as GuestShowItem;
string param = string.Empty;
if (item != null)
{
param = JsonUtils.Serialize(item.Parameter);
if (kxParam != param)
{
lock (Global.Instance.SyncLock)
{
using (var db = Global.Instance.OpenDataBase)
{
GuestShowDevice device = new GuestShowDevice();
device.Id = IdWorkerUtils.Instance.NextId();
device.TenantId = Global.Instance.Authc.TenantId;
device.Name = item.Name;
device.Param = kxParam;
device.IsLocal = 1;
db.Save(device);
}
item.Parameter = JsonUtils.Deserialize<POSV.Bean.GuestShowParameter>(kxParam);
}
}
}
}
/// <summary>
/// 客显测试
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnTestClick(object sender, EventArgs e)
{
try
{
GuestShowUtils.RefreshInstance();
GuestShowUtils.Instance.DisplayData(CustomerDisplayType.Price, "1111.11");
System.Threading.Thread.Sleep(1000);
GuestShowUtils.Instance.DisplayData(CustomerDisplayType.Total, "8888.88");
System.Threading.Thread.Sleep(1000);
GuestShowUtils.Instance.DisplayData(CustomerDisplayType.Recive, "6666.66");
System.Threading.Thread.Sleep(1000);
GuestShowUtils.Instance.DisplayData(CustomerDisplayType.Change, "0.01");
System.Threading.Thread.Sleep(1000);
GuestShowUtils.Instance.DisplayData(CustomerDisplayType.Clear, "");
}
catch (Exception ex)
{
LOGGER.Error(ex, "客显测试异常");
MessageBox.Show(ex.Message);
}
}
}
}