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.

604 lines
19 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 JwKdsV.Core.Utils;
using JwKdsV.Entity.Common;
using JwKdsV.Entity.Product;
using NLog;
using NPoco;
using POSV.Common;
using POSV.Service;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace JwKdsV.Core
{
public class Global
{
private static ILogger logger = NLog.LogManager.GetCurrentClassLogger();
private static object _lock = new object();
private Dictionary<string , string> _globalConfig = null;
private static Global _product = null;
public List<ProductExt> _productList = null;
private static Global _instance = null;
public static Global Instance
{
get
{
if (_instance == null)
{
lock (_lock)
{
_instance = new Global();
_instance.InitConfig();
logger.Info("加载全局配置参数......");
//初始化kds类型
_instance.InstallKdsCategory();
//收集设备信息
_instance.CollectDeviceInfo();
}
}
return _instance;
}
}
public static Global Product
{
get
{
if (_product == null)
{
lock (_lock)
{
_product = new Global();
//加载菜品
_product.InitProductExt();
logger.Info("加载菜品资料......");
}
}
return _product;
}
}
public string CacheFullPath
{
get
{
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"data\kdsCachev1.s3db");
}
}
public string DataBaseFullPath
{
get { return Path.Combine(AppDomain.CurrentDomain.BaseDirectory , @"data\kdsDatav1.s3db"); }
}
public Database OpenDataBase
{
get
{
return new Database(Global.Instance.SQLiteConnectionString, DatabaseType.SQLite, SQLiteFactory.Instance);
}
}
/// <summary>
/// 是否联机
/// </summary>
public bool Online { get; set; }
public KDSCategory kdsCategory;
/// <summary>
/// LiteDB数据库操作锁
/// </summary>
public object SyncLiteLock = new object();
/// <summary>
/// 通用操作锁
/// </summary>
public object SyncLock = new object();
/// <summary>
/// 缓存操作锁
/// </summary>
public object CacheLock = new object();
/// <summary>
/// 门店信息
/// </summary>
public StoreInfo StoreInfo { get; set; }
/// <summary>
/// 设备信息
/// </summary>
public DeviceInfo DeviceInfo { get; set; }
/// <summary>
/// 当前登陆操作员信息
/// </summary>
public Worker Worker { get; set; }
/// <summary>
/// 确定当前设置的kds类型
/// </summary>
private void InstallKdsCategory()
{
var type = this.GlobalConfigStringValue(ConfigConstant.CONFIG_BUSINESS_KDSCATEGORY);
var kdsType = KDSCategory.;
Enum.TryParse(type, out kdsType);
this.kdsCategory = kdsType;
}
/// <summary>
/// 全部重载
/// </summary>
public void ReloadConfig()
{
this.InitConfig();
}
private void InitConfig()
{
try
{
using (IDatabase db = Instance.OpenDataBase)
{
this._globalConfig = db.Dictionary<string, string>(SqlConstant.ConfigQueryAllToDictionary);
}
if (this._globalConfig != null)
{
logger.Debug("获取系统默认参数成功...");
}
else
{
this._globalConfig = new Dictionary<string, string>();
}
}
catch (Exception ex)
{
logger.Error(ex, "加载系统全局配置异常");
}
}
public void ReloadConfig(string group)
{
lock (Instance.SyncLock)
{
try
{
using (var db = Instance.OpenDataBase)
{
string sql = string.Format(SqlConstant.ConfigQueryByGroupToDictionary, group);
var config = db.Dictionary<string, string>(sql);
if (config != null)
{
foreach (var c in config)
{
this._globalConfig[c.Key] = c.Value;
if (logger.IsDebugEnabled)
{
logger.Debug(c.Key + "=" + c.Value);
}
}
logger.Debug("获取<" + group + ">参数成功...");
}
}
}
catch (Exception ex)
{
logger.Error(ex, "加载系统<" + group + ">组配置异常");
}
}
}
private void InitProductExt()
{
try
{
using (var db = Instance.OpenDataBase)
{
//获取菜品排序方式
var productOrder = Global.Instance.GlobalConfigStringValue(ConfigConstant.CASHIER_PRODUCTORDER, "不排序");
var productOrderType = ProductOrderType.;
Enum.TryParse(productOrder, out productOrderType);
switch (productOrderType)
{
case ProductOrderType.:
{
this._productList = db.Fetch<ProductExt>(SqlConstant.ProductExtAll);
}
break;
case ProductOrderType.:
{
this._productList = db.Fetch<ProductExt>(SqlConstant.ProductExtAll + " order by p.no");
}
break;
case ProductOrderType.:
{
this._productList = db.Fetch<ProductExt>(SqlConstant.ProductExtAll + " order by p.price");
}
break;
case ProductOrderType.:
{
this._productList = db.Fetch<ProductExt>(SqlConstant.ProductExtAll + " order by p.name");
}
break;
}
}
if (this._productList != null)
{
logger.Debug("获取菜品成功");
}
else
{
this._productList = new List<ProductExt>();
}
}
catch (Exception ex)
{
logger.Error(ex, "加载菜品失败");
}
}
public string GlobalConfigValue(string keys)
{
if (this._globalConfig.ContainsKey(keys))
{
return this._globalConfig[keys];
}
else
{
return null;
}
}
/// <summary>
/// 加载配置参数返回Bool类型
/// </summary>
/// <param name="keys"></param>
/// <returns></returns>
public bool GlobalConfigBoolValue(string keys)
{
string value = GlobalConfigStringValue(keys, string.Empty);
return "1".Equals(value);
}
public bool GlobalConfigBoolValue(string keys, bool defaultValue)
{
if (this._globalConfig.ContainsKey(keys))
{
string value = this._globalConfig[keys];
return "1".Equals(value);
}
else
{
return defaultValue;
}
}
public string GlobalConfigStringValue(string keys)
{
return GlobalConfigStringValue(keys , string.Empty);
}
public string GlobalConfigStringValue(string keys,string defaultValue)
{
if (this._globalConfig.ContainsKey(keys))
{
return this._globalConfig[keys];
}
else
{
return defaultValue;
}
}
/// <summary>
/// 加载配置参数返回Int类型
/// </summary>
/// <param name="keys"></param>
/// <returns></returns>
public int GlobalConfigIntValue(string keys)
{
return GlobalConfigIntValue(keys, 0);
}
public int GlobalConfigIntValue(string keys, int defaultValue)
{
if (this._globalConfig.ContainsKey(keys))
{
int result = defaultValue;
int.TryParse(this._globalConfig[keys], out result);
return result;
}
else
{
return defaultValue;
}
}
public Tuple<bool, string, string, int> EnableServiceCenter
{
get
{
bool enabled = true;
if (enabled)
{
string host = Instance.GlobalConfigStringValue(ConfigConstant.CONFIG_BUSINESS_SERVICECENTER_IP, string.Empty);
int port = Instance.GlobalConfigIntValue(ConfigConstant.CONFIG_BUSINESS_SERVICECENTER_PORT, 0);
if (string.IsNullOrEmpty(host) || port == 0)
{
return new Tuple<bool, string, string, int>(false, "消息中心参数配置不正确!", string.Empty, 0);
}
else
{
return new Tuple<bool, string, string, int>(true, "消息中心参数配置正确!", host, port);
}
}
else
{
return new Tuple<bool, string, string, int>(false, "消息中心尚未启用", string.Empty, 0);
}
}
}
public string SQLiteConnectionString
{
get
{
SQLiteConnectionStringBuilder builder = new SQLiteConnectionStringBuilder
{
DataSource = Global.Instance.DataBaseFullPath,
//Password = "passwd",
Version = 3,
CacheSize = 4000,
DefaultTimeout = 5000,
FailIfMissing = false,
Pooling = true,
SyncMode = SynchronizationModes.Normal,
JournalMode = SQLiteJournalModeEnum.Wal
};
return builder.ToString();
}
}
public Font GetFont(SystemFont font)
{
Font result = Constant.DEFAULT_FONT;
switch (font)
{
case SystemFont.:
{
result = Constant.DEFAULT_FONT;
}
break;
case SystemFont.:
{
result = Constant.NORMAL_FONT;
}
break;
case SystemFont.:
{
result = Constant.SMALL_FONT;
}
break;
case SystemFont.:
{
result = Constant.BIG_FONT;
}
break;
case SystemFont.:
{
result = Constant.BEST_BIG_FONT;
}
break;
case SystemFont.:
{
result = Constant.SUBURBAN_FONT;
}
break;
case SystemFont.:
{
result = Constant.SUBURBAN_FONT_BIG;
}
break;
}
return result;
}
/// <summary>
/// 收集设备信息
/// </summary>
private void CollectDeviceInfo()
{
string computerName = null;
try
{
computerName = System.Environment.GetEnvironmentVariable("ComputerName");
}
catch (Exception ex)
{
NLog.LogManager.GetCurrentClassLogger().Error(ex, "获取计算机名称失败!");
}
//mac
string macAddressStr = null;
try
{
string[] macAddressArray = HWiNFO.HWiNFO.WMI_DeviceQuery(0);
macAddressStr = string.Join(",", macAddressArray);
}
catch (Exception ex)
{
NLog.LogManager.GetCurrentClassLogger().Error(ex, "获取mac地址失败");
}
// 硬盘序列号
string diskStr = null;
try
{
string[] diskArray = HWiNFO.HWiNFO.WDK_QueryDiskDrive();
diskStr = string.Join(",", diskArray);
}
catch (Exception)
{
try
{
string[] diskArray = HWiNFO.HWiNFO.WMI_DeviceQuery(2);
diskStr = string.Join(",", diskArray);
}
catch (Exception ex)
{
NLog.LogManager.GetCurrentClassLogger().Error(ex, "获取硬盘序列号失败!");
}
}
//cpu
string cpuStr = null;
try
{
string[] cpuArray = HWiNFO.HWiNFO.WMI_DeviceQuery(4);// cpu序列号
cpuStr = string.Join(",", cpuArray);
}
catch (Exception ex)
{
NLog.LogManager.GetCurrentClassLogger().Error(ex, "获取cpu序列号失败");
}
//主板
//string masterleafStr = null;
//try
//{
// string[] masterleafArray = HWiNFO.HWiNFO.WMI_DeviceQuery(3);// 主板序列号
// masterleafStr = string.Join(",", masterleafArray);
//}
//catch (Exception ex)
//{
// NLog.LogManager.GetCurrentClassLogger().Error(ex, "获取主板序列号失败!");
//}
DeviceInfo obj = new DeviceInfo
{
ComputerName = computerName,
Cpu = cpuStr,
Disk = diskStr,
Mac = macAddressStr,
//Masterleaf = masterleafStr
};
this.DeviceInfo = obj;
}
public void BugReport(Exception e)
{
string errorLog = GetExceptionInfo(e);
logger.Error(errorLog);
if (!Global.Instance.Online)
{
return;
}
try
{
string tenantId = Global.Instance.StoreInfo.TenantId;
string branchNo = Global.Instance.StoreInfo.StoreNo;
string posNo = Global.Instance.StoreInfo.PosNo;
//调用业务系统开放平台
var api = OpenApiUtils.Instance.NextApi(ApiType.Business);
//构建请求参数
SortedList<string, string> parameters = new SortedList<string, string>();
parameters.Add("type", "poserrorlog");
parameters.Add("terminalType", Constant.TERMINAL_TYPE);
parameters.Add("appSign", Constant.APP_SIGN);
parameters.Add("versionType", "1");
parameters.Add("versionNum", Application.ProductVersion);
parameters.Add("tenantId", Global.Instance.StoreInfo.TenantId);
parameters.Add("storeNo", Global.Instance.StoreInfo.StoreNo);
parameters.Add("osName", HttpClientUtils.GetSystemType());
parameters.Add("posNo", Global.Instance.StoreInfo.PosNo);
parameters.Add("info", "自动收集错误");
parameters.Add("errorLog", errorLog);
HttpClientUtils.PostAsync(api, api.Open, parameters);
}
catch (Exception ex)
{
logger.Error(ex, "异常上报错误");
}
}
static string GetExceptionInfo(Exception e)
{
string ExceptionName = e.GetType().Name;
StringBuilder sb = new StringBuilder();
sb.Append("---------------------Header-----------------");
sb.Append(Environment.NewLine);
sb.Append("<" + ExceptionName + ">\n");
sb.Append(Environment.NewLine);
sb.Append("<LogDateTime>" + DateTime.Now.ToString() + "</LogDateTime>\n");
sb.Append(Environment.NewLine);
sb.Append("<Message>" + System.Net.WebUtility.HtmlEncode(e.Message) + "</Message>\n");
sb.Append(Environment.NewLine);
if (e.Source != null)
{
sb.Append("<Source>" + e.Source + "</Source>\n");
sb.Append(Environment.NewLine);
}
if (e.StackTrace != null)
{
sb.Append("<StackTrace>" + e.StackTrace + "</StackTrace>\n");
sb.Append(Environment.NewLine);
}
if (e.InnerException != null)
{
sb.Append("<InnerException>" + System.Net.WebUtility.HtmlEncode(e.InnerException.ToString()) + "</InnerException>\n");
sb.Append(Environment.NewLine);
}
sb.Append("<TargetSite>" + e.TargetSite + "</TargetSite>\n");
sb.Append(Environment.NewLine);
sb.Append("</" + ExceptionName + ">\n");
sb.Append(Environment.NewLine);
sb.Append("---------------------Footer-----------------");
sb.Append(Environment.NewLine);
return sb.ToString();
}
#region subin 2023-08-04 添加语音播报队列
/// <summary>
/// 语音播报信息队列
/// </summary>
public static System.Collections.Queue VoPlayQueue = new System.Collections.Queue();
#endregion
}
}