using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using VOL.Core.CacheManager; using VOL.Core.DBManager; using VOL.Core.Extensions.AutofacManager; using VOL.Core.Services; using VOL.Entity.DomainModels; namespace VOL.Core.Infrastructure { public static class DictionaryManager { private static List _dictionaries { get; set; } private static object _dicObj = new object(); private static string _dicVersionn = ""; public const string Key = "inernalDic"; public static List Dictionaries { get { return GetAllDictionary(); } } public static Sys_Dictionary GetDictionary(string dicNo) { return GetDictionaries(new string[] { dicNo }).FirstOrDefault(); } /// /// /// /// /// 是否执行自定义sql /// public static IEnumerable GetDictionaries(IEnumerable dicNos, bool executeSql = true) { static List query(string sql) { try { return DBServerProvider.SqlDapper.QueryList(sql, null).Select(s => new Sys_DictionaryList() { DicName = s.Value, DicValue = s.Key.ToString() }).ToList(); } catch (Exception ex) { Logger.Error($"字典执行sql异常,sql:{sql},异常信息:{ex.Message + ex.StackTrace}"); throw ex; // Console.WriteLine(ex.Message); // return null; } } foreach (var item in Dictionaries.Where(x => dicNos.Contains(x.DicNo))) { if (executeSql) { // 2020.05.01增加根据用户信息加载字典数据源sql string sql = DictionaryHandler.GetCustomDBSql(item.DicNo, item.DbSql,item.CompanyField); if (!string.IsNullOrEmpty(item.DbSql)) { item.Sys_DictionaryList = query(sql); } } yield return item; } } /// /// 每次变更字典配置的时候会重新拉取所有配置进行缓存(自行根据实际处理) /// /// private static List GetAllDictionary() { ICacheService cacheService = AutofacContainerModule.GetService(); //每次比较缓存是否更新过,如果更新则重新获取数据 if (_dictionaries != null && _dicVersionn == cacheService.Get(Key)) { return _dictionaries; } lock (_dicObj) { if (_dicVersionn != "" && _dictionaries != null && _dicVersionn == cacheService.Get(Key)) return _dictionaries; _dictionaries = DBServerProvider.DbContext .Set() .Where(x => x.Enable == 1) .Include(c => c.Sys_DictionaryList).ToList(); string cacheVersion = cacheService.Get(Key); if (string.IsNullOrEmpty(cacheVersion)) { cacheVersion = DateTime.Now.ToString("yyyyMMddHHMMssfff"); cacheService.Add(Key, cacheVersion); } else { _dicVersionn = cacheVersion; } } return _dictionaries; } } public class SourceKeyVaule { public object Key { get; set; } public string Value { get; set; } } public class CompanyField { public object GID { get; set; } public string CODENAME { get; set; } public string NAME { get; set; } public string FULLNAME { get; set; } public string TAXCODE { get; set; } public string ADDRESS { get; set; } public string MANAGER { get; set; } public string OFFICEPHONE { get; set; } public string TRADE_CODE { get; set; } public string LICENSECODE { get; set; } } }