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.

472 lines
16 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.Utils;
using POSV.Entity;
using POSV.Bean;
using POSV.HttpApi;
using POSV.HttpResponse;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Controls;
using DevComponents.Editors;
namespace POSV.Component
{
[ToolboxItem(true)]
public partial class KitchenDisplay : 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;
private List<string> _kdsNoList = new List<string>();
public KitchenDisplay()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.DesignMode) return;
this.BackColor = Color.Transparent;
LoadKDSDevice();
this.InitUi();
}
/// <summary>
/// 加载当前门店的kds
/// </summary>
private void LoadKDSDevice()
{
try
{
OpenApi api = OpenApiUtils.Instance.NextApi(ApiType.Business);
SortedList<string, string> parameters = OpenApiUtils.Instance.NewParameters(api);
parameters.Add("method", "store.pos.list");
parameters.Add("storeId", Global.Instance.Authc.StoreId);
parameters.Add("pageNumber", "1");
parameters.Add("pageSize", "500");
var ignore = new List<string>();
ignore.Add("pageNumber");
ignore.Add("pageSize");
parameters.Add("sign", OpenApiUtils.Instance.Sign(api, parameters, ignore));
string response = HttpClientUtils.PostAsync(api, api.Url, parameters);
if (Constant.IsSuccessful(response))
{
var posResult = JsonUtils.Deserialize<ListPagerResponse<PosInfo>>(response);
if(posResult != null && posResult.List != null)
{
_kdsNoList.Add("None");
var kdsList = posResult.List.FindAll(x => x.TerminalType == "kds_x86").ToList();
_kdsNoList.AddRange(kdsList.ConvertAll<string>(x => x.PosNo));
}
}
else
{
MessageBox.Show("加载当前门店kds失败");
}
}
catch(Exception ex)
{
LOGGER.Error(ex, "查询门店kds异常");
MessageBox.Show("加载当前门店kds异常");
}
}
/// <summary>
/// 初始化界面
/// </summary>
private void InitUi()
{
//界面加载开始
this._inited = false;
Task.Factory.StartNew(() => {
Dictionary<string, string> data = null;
lock (Global.Instance.SyncLock)
{
//获取品类分组下的全部参数
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();
//kds设备
this.kdsNoCom.Items.Clear();
this.kdsNoCom.Items.AddRange(_kdsNoList.ToArray());
var json = data[ConfigConstant.DEVICE_KITCHEN_DISPLAY];
var kitchenDisplayItem = JsonUtils.Deserialize<Dictionary<string, KitchenDisplayItem>>(json);
this.cmbKitDisplayPlan.DataSource = new BindingSource(kitchenDisplayItem, null);
this.cmbKitDisplayPlan.DisplayMember = "Key";
//this.cmbKitPlan.ValueMember = "Value";
//将当前厨显方案临时存储,用于判断是否修改
this.cmbKitDisplayPlan.Tag = kitchenDisplayItem;
//厨显方案
this.cmbKitDisplayPlan.SelectedIndex = 0;
//是否启用厨显
this.checkBoxX1.Checked = data[ConfigConstant.DEVICE_KITCHEN_DISPLAY_ENABLE].ToString().Equals("1");
//CheckBoxX处理
foreach (Control control in this.groupPanel2.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 kitplan = ((KeyValuePair<string, KitchenDisplayItem>)this.cmbKitDisplayPlan.SelectedValue);
var newValue = new KitchenDisplayItem();
newValue.PlanId = kitplan.Value.PlanId;
newValue.KdsNo = this.kdsNoCom.Text;
//判断是否更改,如果过更改压入到 NewValue
bool isChanged = !(newValue.Equals(kitplan.Value));
if (isChanged)
{
var kitchens = this.cmbKitDisplayPlan.Tag as Dictionary<string, KitchenDisplayItem>;
kitchens[kitplan.Key] = newValue;
var config = new Config();
config.Id = IdWorkerUtils.Instance.NextId();
config.Group = ConfigConstant.DEVICE_GROUP;
config.TenantId = Global.Instance.Authc.TenantId;
config.Keys = ConfigConstant.DEVICE_KITCHEN_DISPLAY;
config.Values = JsonUtils.Serialize(kitchens);
result.Add(config);
}
//是否启用厨显
var enableKds = this.checkBoxX1.Checked ? "1" : "0";
if(!(this._oldValue.ContainsKey(ConfigConstant.DEVICE_KITCHEN_DISPLAY_ENABLE) && this._oldValue[ConfigConstant.DEVICE_KITCHEN_DISPLAY_ENABLE].Equals(enableKds)))
{
this._newValue[ConfigConstant.DEVICE_KITCHEN_DISPLAY_ENABLE] = enableKds;
}
//CheckBoxX处理
foreach (Control control in this.groupPanel2.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
bool 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
{
lock (Global.Instance.SyncLock)
{
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内容
foreach (var config in data)
{
string key = config.Keys;
string value = config.Values;
this._oldValue[key] = value;
if (ConfigConstant.DEVICE_KITCHEN_DISPLAY.Equals(key))
{
this.cmbKitDisplayPlan.Tag = JsonUtils.Deserialize<Dictionary<string, KitchenDisplayItem>>(value);
}
}
this._newValue.Clear();
//更新控件UI
UpdatePrinterUi();
}
}
return new Tuple<bool, string>(isSuccess, message);
}
private void UpdatePrinterUi()
{
Global.Instance.ReloadConfig(ConfigConstant.DEVICE_GROUP);
//更新服务中心连接状态
WSSE.Instance.RestartConnect();
}
/// <summary>
/// 更新kds方案
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnUpdateKdsPlans(object sender, EventArgs e)
{
var button = (ButtonX)sender;
bool isException = false;
try
{
button.Text = "更新中...";
button.Enabled = false;
//加载厨打方案列表
var maps = new Dictionary<string, KitchenDisplayItem>();
var kits = new List<KdsPlan>();
lock (Global.Instance.SyncLock)
{
using (var db = Global.Instance.OpenDataBase)
{
kits = db.Query<KdsPlan>().ToList();
if (kits.Count > 0)
{
using (var trans = db.GetTransaction())
{
foreach (var kit in kits)
{
var keys = string.Format("{0}-{1}" , kit.No , kit.Name);
kit.Keys = keys;
db.Save(kit);
}
db.Delete<Config>("where [group] = @0 and [keys] = @1;" , ConfigConstant.DEVICE_GROUP , ConfigConstant.DEVICE_KITCHEN_DISPLAY);
trans.Complete();
}
}
}
}
this.InitUi();
}
catch (Exception ex)
{
isException = true;
LOGGER.Error(ex, "更新厨显方案异常");
}
finally
{
if (isException)
{
}
button.Text = "更新方案";
button.Enabled = true;
}
}
private void OnKdsPlanSelectedIndexChange(object sender, EventArgs e)
{
ComboBoxEx item = (ComboBoxEx)sender;
//变更前的数据
var selected = ((KeyValuePair<string, KitchenDisplayItem>)item.SelectedValue);
//厨显方案对应的kds
this.kdsNoCom.SelectedIndex = this.kdsNoCom.FindString(selected.Value.KdsNo);
}
}
}