2017-04-02 16:32:26 +08:00
|
|
|
|
using Bootstrap.Security;
|
|
|
|
|
using Longbow.Caching;
|
2016-11-03 21:28:19 +08:00
|
|
|
|
using Longbow.Caching.Configuration;
|
|
|
|
|
using Longbow.ExceptionManagement;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Data.Common;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace Bootstrap.DataAccess
|
|
|
|
|
{
|
2017-03-30 16:15:45 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2016-11-04 16:06:40 +08:00
|
|
|
|
public static class DictHelper
|
2016-11-03 21:28:19 +08:00
|
|
|
|
{
|
2017-04-02 16:32:26 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2017-03-30 15:01:34 +08:00
|
|
|
|
private const string RetrieveCategoryDataKey = "DictHelper-RetrieveDictsCategory";
|
2016-11-03 21:28:19 +08:00
|
|
|
|
/// <summary>
|
2017-04-02 16:32:26 +08:00
|
|
|
|
///
|
2016-11-03 21:28:19 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2017-04-02 16:32:26 +08:00
|
|
|
|
public static IEnumerable<BootstrapDict> RetrieveDicts()
|
2016-11-03 21:28:19 +08:00
|
|
|
|
{
|
2017-04-02 16:32:26 +08:00
|
|
|
|
return BootstrapDict.RetrieveDicts();
|
2016-11-03 21:28:19 +08:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 删除字典中的数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ids">需要删除的IDs</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool DeleteDict(string ids)
|
|
|
|
|
{
|
2017-03-30 15:01:34 +08:00
|
|
|
|
if (string.IsNullOrEmpty(ids) || ids.Contains("'")) return false;
|
2016-11-03 21:28:19 +08:00
|
|
|
|
var ret = false;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Dicts where ID in ({0})", ids);
|
|
|
|
|
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
|
|
|
|
|
{
|
|
|
|
|
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
2016-11-05 12:11:16 +08:00
|
|
|
|
CacheCleanUtility.ClearCache(dictIds: ids);
|
2016-11-03 21:28:19 +08:00
|
|
|
|
ret = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ExceptionManager.Publish(ex);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2017-03-30 15:01:34 +08:00
|
|
|
|
|
2016-11-03 21:28:19 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 保存新建/更新的字典信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="p"></param>
|
|
|
|
|
/// <returns></returns>
|
2017-04-02 16:32:26 +08:00
|
|
|
|
public static bool SaveDict(BootstrapDict p)
|
2016-11-03 21:28:19 +08:00
|
|
|
|
{
|
|
|
|
|
bool ret = false;
|
2017-03-30 15:01:34 +08:00
|
|
|
|
if (p.Category.Length > 50) p.Category = p.Category.Substring(0, 50);
|
|
|
|
|
if (p.Name.Length > 50) p.Name = p.Name.Substring(0, 50);
|
|
|
|
|
if (p.Code.Length > 50) p.Code = p.Code.Substring(0, 50);
|
2017-03-30 16:15:45 +08:00
|
|
|
|
string sql = p.Id == 0 ?
|
2016-11-03 21:28:19 +08:00
|
|
|
|
"Insert Into Dicts (Category, Name, Code ,Define) Values (@Category, @Name, @Code, @Define)" :
|
2016-11-05 18:44:41 +08:00
|
|
|
|
"Update Dicts set Category = @Category, Name = @Name, Code = @Code, Define = @Define where ID = @ID";
|
2016-11-03 21:28:19 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
|
|
|
|
|
{
|
2017-03-30 16:15:45 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ID", p.Id));
|
2017-03-30 15:01:34 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Category", p.Category));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Name", p.Name));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Code", p.Code));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Define", p.Define));
|
2016-11-03 21:28:19 +08:00
|
|
|
|
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
|
|
|
|
}
|
|
|
|
|
ret = true;
|
2017-03-30 16:15:45 +08:00
|
|
|
|
CacheCleanUtility.ClearCache(dictIds: p.Id == 0 ? string.Empty : p.Id.ToString());
|
2016-11-03 21:28:19 +08:00
|
|
|
|
}
|
|
|
|
|
catch (DbException ex)
|
|
|
|
|
{
|
|
|
|
|
ExceptionManager.Publish(ex);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2016-12-09 20:13:22 +08:00
|
|
|
|
/// <summary>
|
2016-11-08 15:35:14 +08:00
|
|
|
|
/// 保存网站个性化设置
|
|
|
|
|
/// </summary>
|
2016-11-10 00:47:23 +08:00
|
|
|
|
/// <param name="name"></param>
|
|
|
|
|
/// <param name="code"></param>
|
|
|
|
|
/// <param name="category"></param>
|
2016-11-08 15:35:14 +08:00
|
|
|
|
/// <returns></returns>
|
2017-03-23 17:07:18 +08:00
|
|
|
|
public static bool SaveSettings(string name, string code, string category)
|
2016-11-08 15:35:14 +08:00
|
|
|
|
{
|
|
|
|
|
var ret = false;
|
2016-11-10 00:47:23 +08:00
|
|
|
|
string sql = "Update Dicts set Code = @Code where Category = @Category and Name = @Name";
|
2016-11-08 15:35:14 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
|
|
|
|
|
{
|
2017-03-30 15:01:34 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Name", name));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Code", code));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Category", category));
|
2016-11-08 15:35:14 +08:00
|
|
|
|
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
|
|
|
|
}
|
2017-04-02 16:32:26 +08:00
|
|
|
|
CacheCleanUtility.ClearCache(dictIds: string.Empty);
|
2016-11-08 15:35:14 +08:00
|
|
|
|
ret = true;
|
|
|
|
|
}
|
|
|
|
|
catch (DbException ex)
|
|
|
|
|
{
|
|
|
|
|
ExceptionManager.Publish(ex);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2016-11-15 11:15:38 +08:00
|
|
|
|
/// <summary>
|
2016-11-17 20:01:28 +08:00
|
|
|
|
/// 获取字典分类名称
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2017-04-02 16:32:26 +08:00
|
|
|
|
public static IEnumerable<BootstrapDict> RetrieveCategories()
|
2016-11-17 20:01:28 +08:00
|
|
|
|
{
|
|
|
|
|
return CacheManager.GetOrAdd(RetrieveCategoryDataKey, CacheSection.RetrieveIntervalByKey(RetrieveCategoryDataKey), key =>
|
|
|
|
|
{
|
2017-04-02 16:32:26 +08:00
|
|
|
|
var ret = new List<BootstrapDict>();
|
2016-11-17 20:01:28 +08:00
|
|
|
|
string sql = "select distinct Category from Dicts";
|
|
|
|
|
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
|
|
|
|
|
{
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
2017-04-02 16:32:26 +08:00
|
|
|
|
ret.Add(new BootstrapDict() { Category = (string)reader[0] });
|
2016-11-17 20:01:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) { ExceptionManager.Publish(ex); }
|
|
|
|
|
return ret;
|
|
|
|
|
}, CacheSection.RetrieveDescByKey(RetrieveCategoryDataKey));
|
|
|
|
|
}
|
2016-12-20 13:06:54 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string RetrieveWebTitle()
|
|
|
|
|
{
|
|
|
|
|
var settings = RetrieveDicts();
|
2017-04-02 16:32:26 +08:00
|
|
|
|
return (settings.FirstOrDefault(d => d.Name == "网站标题" && d.Category == "网站设置" && d.Define == 0) ?? new BootstrapDict() { Code = "后台管理系统" }).Code;
|
2016-12-20 13:06:54 +08:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string RetrieveWebFooter()
|
|
|
|
|
{
|
|
|
|
|
var settings = RetrieveDicts();
|
2017-04-02 16:32:26 +08:00
|
|
|
|
return (settings.FirstOrDefault(d => d.Name == "网站页脚" && d.Category == "网站设置" && d.Define == 0) ?? new BootstrapDict() { Code = "2016 © 通用后台管理系统" }).Code;
|
2016-12-20 13:06:54 +08:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2017-04-02 16:32:26 +08:00
|
|
|
|
public static IEnumerable<BootstrapDict> RetrieveWebCss()
|
2016-12-20 13:06:54 +08:00
|
|
|
|
{
|
|
|
|
|
var data = RetrieveDicts();
|
|
|
|
|
return data.Where(d => d.Category == "网站样式");
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2017-04-02 16:32:26 +08:00
|
|
|
|
public static IEnumerable<BootstrapDict> RetrieveActiveCss()
|
2016-12-20 13:06:54 +08:00
|
|
|
|
{
|
|
|
|
|
var data = RetrieveDicts();
|
|
|
|
|
return data.Where(d => d.Name == "使用样式" && d.Category == "当前样式" && d.Define == 0 && !d.Code.Equals("site.css", StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取头像路径
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2017-04-02 16:32:26 +08:00
|
|
|
|
public static BootstrapDict RetrieveIconFolderPath()
|
2016-12-20 13:06:54 +08:00
|
|
|
|
{
|
|
|
|
|
var data = RetrieveDicts();
|
2017-04-02 16:32:26 +08:00
|
|
|
|
return data.FirstOrDefault(d => d.Name == "头像路径" && d.Category == "头像地址" && d.Define == 0) ?? new BootstrapDict() { Code = "~/Content/images/uploader/" };
|
2016-12-20 13:06:54 +08:00
|
|
|
|
}
|
2016-12-20 13:22:33 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获得默认的前台首页地址,默认为~/Home/Index
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string RetrieveHomeUrl()
|
|
|
|
|
{
|
|
|
|
|
var settings = RetrieveDicts();
|
2017-04-02 16:32:26 +08:00
|
|
|
|
return (settings.FirstOrDefault(d => d.Name == "前台首页" && d.Category == "网站设置" && d.Define == 0) ?? new BootstrapDict() { Code = "~/Home/Index" }).Code;
|
2016-12-20 13:22:33 +08:00
|
|
|
|
}
|
2017-03-03 13:34:53 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static IEnumerable<KeyValuePair<string, string>> RetrieveApps()
|
|
|
|
|
{
|
|
|
|
|
var settings = RetrieveDicts();
|
|
|
|
|
return settings.Where(d => d.Category == "应用程序" && d.Define == 0).Select(d => new KeyValuePair<string, string>(d.Code, d.Name)).OrderBy(d => d.Key);
|
|
|
|
|
}
|
2016-11-03 21:28:19 +08:00
|
|
|
|
}
|
|
|
|
|
}
|