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 _globalConfig = null; private static Global _product = null; public List _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); } } /// /// 是否联机 /// public bool Online { get; set; } public KDSCategory kdsCategory; /// /// LiteDB数据库操作锁 /// public object SyncLiteLock = new object(); /// /// 通用操作锁 /// public object SyncLock = new object(); /// /// 缓存操作锁 /// public object CacheLock = new object(); /// /// 门店信息 /// public StoreInfo StoreInfo { get; set; } /// /// 设备信息 /// public DeviceInfo DeviceInfo { get; set; } /// /// 当前登陆操作员信息 /// public Worker Worker { get; set; } /// /// 确定当前设置的kds类型 /// private void InstallKdsCategory() { var type = this.GlobalConfigStringValue(ConfigConstant.CONFIG_BUSINESS_KDSCATEGORY); var kdsType = KDSCategory.厨显; Enum.TryParse(type, out kdsType); this.kdsCategory = kdsType; } /// /// 全部重载 /// public void ReloadConfig() { this.InitConfig(); } private void InitConfig() { try { using (IDatabase db = Instance.OpenDataBase) { this._globalConfig = db.Dictionary(SqlConstant.ConfigQueryAllToDictionary); } if (this._globalConfig != null) { logger.Debug("获取系统默认参数成功..."); } else { this._globalConfig = new Dictionary(); } } 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(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(SqlConstant.ProductExtAll); } break; case ProductOrderType.按编号: { this._productList = db.Fetch(SqlConstant.ProductExtAll + " order by p.no"); } break; case ProductOrderType.按价格: { this._productList = db.Fetch(SqlConstant.ProductExtAll + " order by p.price"); } break; case ProductOrderType.按名称: { this._productList = db.Fetch(SqlConstant.ProductExtAll + " order by p.name"); } break; } } if (this._productList != null) { logger.Debug("获取菜品成功"); } else { this._productList = new List(); } } catch (Exception ex) { logger.Error(ex, "加载菜品失败"); } } public string GlobalConfigValue(string keys) { if (this._globalConfig.ContainsKey(keys)) { return this._globalConfig[keys]; } else { return null; } } /// /// 加载配置参数,返回Bool类型 /// /// /// 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; } } /// /// 加载配置参数,返回Int类型 /// /// /// 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 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(false, "消息中心参数配置不正确!", string.Empty, 0); } else { return new Tuple(true, "消息中心参数配置正确!", host, port); } } else { return new Tuple(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; } /// /// 收集设备信息 /// 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 parameters = new SortedList(); 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("" + DateTime.Now.ToString() + "\n"); sb.Append(Environment.NewLine); sb.Append("" + System.Net.WebUtility.HtmlEncode(e.Message) + "\n"); sb.Append(Environment.NewLine); if (e.Source != null) { sb.Append("" + e.Source + "\n"); sb.Append(Environment.NewLine); } if (e.StackTrace != null) { sb.Append("" + e.StackTrace + "\n"); sb.Append(Environment.NewLine); } if (e.InnerException != null) { sb.Append("" + System.Net.WebUtility.HtmlEncode(e.InnerException.ToString()) + "\n"); sb.Append(Environment.NewLine); } sb.Append("" + e.TargetSite + "\n"); sb.Append(Environment.NewLine); sb.Append("\n"); sb.Append(Environment.NewLine); sb.Append("---------------------Footer-----------------"); sb.Append(Environment.NewLine); return sb.ToString(); } #region subin 2023-08-04 添加语音播报队列 /// /// 语音播报信息队列 /// public static System.Collections.Queue VoPlayQueue = new System.Collections.Queue(); #endregion } }