using Bootstrap.Security;
using Longbow.Cache;
using Longbow.Data;
using Longbow.Web;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess
{
///
///
///
public static class DictHelper
{
///
///
///
///
/// 缓存索引,BootstrapAdmin后台清理缓存时使用
///
public const string RetrieveDictsDataKey = "BootstrapDict-RetrieveDicts";
///
///
///
public const string RetrieveCategoryDataKey = "DictHelper-RetrieveDictsCategory";
///
///
///
///
public static IEnumerable RetrieveDicts() => CacheManager.GetOrAdd(RetrieveDictsDataKey, key => DbContextManager.Create().RetrieveDicts());
private static IEnumerable RetrieveProtectedDicts() => RetrieveDicts().Where(d => d.Define == 0 || d.Category == "测试平台");
///
/// 删除字典中的数据
///
/// 需要删除的IDs
///
public static bool Delete(IEnumerable value)
{
if (RetrieveSystemModel())
{
// 禁止删除系统数据与测试平台数据
var systemDicts = RetrieveProtectedDicts();
value = value.Where(v => !systemDicts.Any(d => d.Id == v));
if (!value.Any()) return true;
}
if (!value.Any()) return true;
var ret = DbContextManager.Create().Delete(value);
CacheCleanUtility.ClearCache(dictIds: value);
return ret;
}
///
/// 保存新建/更新的字典信息
///
///
///
public static bool Save(BootstrapDict p)
{
if (RetrieveSystemModel() && RetrieveProtectedDicts().Any(m => m.Id == p.Id)) return true;
var ret = DbContextManager.Create().Save(p);
if (ret) CacheCleanUtility.ClearCache(dictIds: new List());
return ret;
}
///
///
///
///
public static void ConfigIPLocator(IPLocatorOption op)
{
var name = RetrieveLocaleIPSvr();
if (!string.IsNullOrEmpty(name) && !name.Equals("None", StringComparison.OrdinalIgnoreCase))
{
var url = RetrieveLocaleIPSvrUrl(name);
op.Locator = string.IsNullOrEmpty(url) ? null : DefaultIPLocatorProvider.CreateLocator(name);
op.Url = string.IsNullOrEmpty(url) ? string.Empty : $"{url}{op.IP}";
if (int.TryParse(RetrieveLocaleIPSvrCachePeriod(), out var period) && period > 0) op.Period = period * 60 * 1000;
}
}
///
/// 保存网站个性化设置
///
///
///
public static bool SaveSettings(BootstrapDict dict)
{
var ret = DbContextManager.Create().SaveSettings(dict);
if (ret) CacheCleanUtility.ClearCache(dictIds: new List());
return ret;
}
///
/// 获取字典分类名称
///
///
public static IEnumerable RetrieveCategories() => CacheManager.GetOrAdd(RetrieveCategoryDataKey, key => DbContextManager.Create().RetrieveCategories());
///
///
///
///
public static string RetrieveWebTitle() => DbContextManager.Create().RetrieveWebTitle();
///
///
///
///
public static string RetrieveWebFooter() => DbContextManager.Create().RetrieveWebFooter();
///
/// 获得系统中配置的可以使用的网站样式
///
///
public static IEnumerable RetrieveThemes() => DbContextManager.Create().RetrieveThemes();
///
/// 获得网站设置中的当前样式
///
///
public static string RetrieveActiveTheme() => DbContextManager.Create().RetrieveActiveTheme();
///
/// 获取头像路径
///
///
public static string RetrieveIconFolderPath() => DbContextManager.Create().RetrieveIconFolderPath();
///
/// 获得默认的前台首页地址,默认为~/Home/Index
///
/// 应用程序编码
///
public static string RetrieveHomeUrl(string appCode) => DbContextManager.Create().RetrieveHomeUrl(appCode);
///
///
///
///
public static IEnumerable> RetrieveApps() => DbContextManager.Create().RetrieveApps();
///
/// 程序异常时长 默认1月
///
///
public static int RetrieveExceptionsLogPeriod() => DbContextManager.Create().RetrieveExceptionsLogPeriod();
///
///
///
///
public static int RetrieveLogsPeriod() => DbContextManager.Create().RetrieveLogsPeriod();
///
///
///
///
public static int RetrieveLoginLogsPeriod() => DbContextManager.Create().RetrieveLoginLogsPeriod();
///
///
///
///
public static int RetrieveCookieExpiresPeriod() => DbContextManager.Create().RetrieveCookieExpiresPeriod();
///
///
///
///
public static string RetrieveLocaleIPSvr() => DbContextManager.Create().RetrieveLocaleIPSvr();
///
///
///
/// ip地址请求服务名称
///
public static string RetrieveLocaleIPSvrUrl(string ipSvr) => DbContextManager.Create().RetrieveLocaleIPSvrUrl(ipSvr);
///
///
///
///
public static string RetrieveLocaleIPSvrCachePeriod() => DbContextManager.Create().RetrieveLocaleIPSvrCachePeriod();
///
/// 访问日志保留时长 默认一个月
///
///
public static int RetrieveAccessLogPeriod() => DbContextManager.Create().RetrieveAccessLogPeriod();
///
/// 获得 是否为演示系统 默认为 false 不是演示系统
///
///
public static bool RetrieveSystemModel() => DbContextManager.Create().RetrieveSystemModel();
///
/// 获得验证码图床地址
///
///
public static string RetrieveImagesLibUrl() => DbContextManager.Create().RetrieveImagesLibUrl();
}
}