1
This commit is contained in:
76
iMES.Core/Infrastructure/DictionaryHandler.cs
Normal file
76
iMES.Core/Infrastructure/DictionaryHandler.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using iMES.Core.Const;
|
||||
using iMES.Core.Enums;
|
||||
using iMES.Core.ManageUser;
|
||||
using iMES.Core.UserManager;
|
||||
|
||||
namespace iMES.Core.Infrastructure
|
||||
{
|
||||
public static class DictionaryHandler
|
||||
{
|
||||
/*2020.05.01增加根据用户信息加载字典数据源sql*/
|
||||
|
||||
/// <summary>
|
||||
/// 获取自定义数据源sql
|
||||
/// </summary>
|
||||
/// <param name="dicNo"></param>
|
||||
/// <param name="originalSql"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetCustomDBSql(string dicNo, string originalSql)
|
||||
{
|
||||
switch (dicNo)
|
||||
{
|
||||
case "roles":
|
||||
originalSql = GetRolesSql(originalSql);
|
||||
break;
|
||||
//2020.05.24增加绑定table表时,获取所有的角色列表
|
||||
//注意,如果是2020.05.24之前获取的数据库脚本
|
||||
//请在菜单【下拉框绑定设置】添加一个字典编号【t_roles】,除了字典编号,其他内容随便填写
|
||||
case "t_roles":
|
||||
originalSql = GetRolesSql();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return originalSql;
|
||||
}
|
||||
/// <summary>
|
||||
/// 2020.05.24增加绑定table表时,获取所有的角色列表
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="originalSql"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetRolesSql()
|
||||
{
|
||||
if (DBType.Name == DbCurrentType.PgSql.ToString())
|
||||
{
|
||||
return "SELECT \"Role_Id\" as key,\"RoleName\" as value from Sys_Role";
|
||||
}
|
||||
return $@"SELECT Role_Id as 'key',RoleName as 'value' FROM Sys_Role
|
||||
WHERE Enable=1 ";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取解决的数据源,只能看到自己与下级所有角色
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="originalSql"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetRolesSql(string originalSql)
|
||||
{
|
||||
if (UserContext.Current.IsSuperAdmin)
|
||||
{
|
||||
return originalSql;
|
||||
}
|
||||
int currnetRoleId = UserContext.Current.RoleId;
|
||||
List<int> roleIds = RoleContext.GetAllChildrenIds(currnetRoleId);
|
||||
roleIds.Add(currnetRoleId);
|
||||
string sql = $@"SELECT Role_Id as 'key',RoleName as 'value' FROM Sys_Role
|
||||
WHERE Enable=1 and Role_Id in ({string.Join(',', roleIds)})";
|
||||
return sql;
|
||||
}
|
||||
}
|
||||
}
|
||||
132
iMES.Core/Infrastructure/DictionaryManager.cs
Normal file
132
iMES.Core/Infrastructure/DictionaryManager.cs
Normal file
@@ -0,0 +1,132 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using iMES.Core.CacheManager;
|
||||
using iMES.Core.DBManager;
|
||||
using iMES.Core.Extensions.AutofacManager;
|
||||
using iMES.Core.Services;
|
||||
using iMES.Entity.DomainModels;
|
||||
|
||||
namespace iMES.Core.Infrastructure
|
||||
{
|
||||
public static class DictionaryManager
|
||||
{
|
||||
private static List<Sys_Dictionary> _dictionaries { get; set; }
|
||||
|
||||
private static object _dicObj = new object();
|
||||
private static string _dicVersionn = "";
|
||||
public const string Key = "inernalDic";
|
||||
|
||||
public static List<Sys_Dictionary> Dictionaries
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetAllDictionary();
|
||||
}
|
||||
}
|
||||
|
||||
public static Sys_Dictionary GetDictionary(string dicNo)
|
||||
{
|
||||
return GetDictionaries(new string[] { dicNo }).FirstOrDefault();
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="dicNos"></param>
|
||||
/// <param name="executeSql">是否执行自定义sql</param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Sys_Dictionary> GetDictionaries(IEnumerable<string> dicNos, bool executeSql = true)
|
||||
{
|
||||
static List<Sys_DictionaryList> query(string sql)
|
||||
{
|
||||
try
|
||||
{
|
||||
return DBServerProvider.SqlDapper.QueryList<SourceKeyVaule>(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);
|
||||
if (!string.IsNullOrEmpty(item.DbSql))
|
||||
{
|
||||
item.Sys_DictionaryList = query(sql);
|
||||
}
|
||||
}
|
||||
yield return item;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据数据字典名称和键值获取文本
|
||||
/// </summary>
|
||||
/// <param name="dicNo">数据字典名称</param>
|
||||
/// <param name="key">键值</param>
|
||||
/// <returns></returns>
|
||||
public static string GetDictionaryList(string dicNo, string key)
|
||||
{
|
||||
Sys_Dictionary sd = GetDictionary(dicNo);
|
||||
if (sd.Sys_DictionaryList.Find(x => x.DicValue == key) != null)
|
||||
{
|
||||
return sd.Sys_DictionaryList.Find(x => x.DicValue == key).DicName;
|
||||
}
|
||||
else
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 每次变更字典配置的时候会重新拉取所有配置进行缓存(自行根据实际处理)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private static List<Sys_Dictionary> GetAllDictionary()
|
||||
{
|
||||
ICacheService cacheService = AutofacContainerModule.GetService<ICacheService>();
|
||||
//每次比较缓存是否更新过,如果更新则重新获取数据
|
||||
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<Sys_Dictionary>()
|
||||
.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; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user