2016-11-03 21:28:19 +08:00
|
|
|
|
using Longbow.Caching;
|
|
|
|
|
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
|
|
|
|
|
{
|
2016-11-04 16:06:40 +08:00
|
|
|
|
public static class DictHelper
|
2016-11-03 21:28:19 +08:00
|
|
|
|
{
|
2016-11-05 12:11:16 +08:00
|
|
|
|
internal const string RetrieveDictsDataKey = "DictHelper-RetrieveDicts";
|
2016-11-05 18:44:41 +08:00
|
|
|
|
internal const string RetrieveWebSettingsDataKey = "DictHelper-RetrieveDictsWebSettings";
|
2016-11-16 17:27:03 +08:00
|
|
|
|
internal const string RetrieveIconPathSettingsDataKey = "DictHelper-RetrieveDictsIconPathSettings";
|
2016-11-17 20:01:28 +08:00
|
|
|
|
internal const string RetrieveCategoryDataKey = "DictHelper-RetrieveDictsCategory";
|
2016-11-03 21:28:19 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询所有字典信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="tId"></param>
|
|
|
|
|
/// <returns></returns>
|
2016-11-16 17:27:03 +08:00
|
|
|
|
public static IEnumerable<Dict> RetrieveDicts()
|
2016-11-03 21:28:19 +08:00
|
|
|
|
{
|
2016-11-16 17:27:03 +08:00
|
|
|
|
return CacheManager.GetOrAdd(RetrieveDictsDataKey, CacheSection.RetrieveIntervalByKey(RetrieveDictsDataKey), key =>
|
2016-11-03 21:28:19 +08:00
|
|
|
|
{
|
2016-12-09 20:13:22 +08:00
|
|
|
|
string sql = "select ID, Category, Name, Code, Define from Dicts";
|
2016-11-03 21:28:19 +08:00
|
|
|
|
List<Dict> Dicts = new List<Dict>();
|
|
|
|
|
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
|
|
|
|
|
{
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
Dicts.Add(new Dict()
|
|
|
|
|
{
|
|
|
|
|
ID = (int)reader[0],
|
|
|
|
|
Category = (string)reader[1],
|
|
|
|
|
Name = (string)reader[2],
|
|
|
|
|
Code = (string)reader[3],
|
2016-12-09 20:13:22 +08:00
|
|
|
|
Define = (int)reader[4]
|
2016-11-03 21:28:19 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) { ExceptionManager.Publish(ex); }
|
|
|
|
|
return Dicts;
|
2016-11-04 16:06:40 +08:00
|
|
|
|
}, CacheSection.RetrieveDescByKey(RetrieveDictsDataKey));
|
2016-11-03 21:28:19 +08:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 删除字典中的数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ids">需要删除的IDs</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool DeleteDict(string ids)
|
|
|
|
|
{
|
|
|
|
|
var ret = false;
|
|
|
|
|
if (string.IsNullOrEmpty(ids) || ids.Contains("'")) return ret;
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 保存新建/更新的字典信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="p"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool SaveDict(Dict p)
|
|
|
|
|
{
|
|
|
|
|
if (p == null) throw new ArgumentNullException("p");
|
|
|
|
|
bool ret = false;
|
|
|
|
|
if (p.Category.Length > 50) p.Category.Substring(0, 50);
|
|
|
|
|
if (p.Name.Length > 50) p.Name.Substring(0, 50);
|
|
|
|
|
if (p.Code.Length > 50) p.Code.Substring(0, 50);
|
|
|
|
|
string sql = p.ID == 0 ?
|
|
|
|
|
"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))
|
|
|
|
|
{
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ID", p.ID, ParameterDirection.Input));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Category", p.Category, ParameterDirection.Input));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Name", p.Name, ParameterDirection.Input));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Code", p.Code, ParameterDirection.Input));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Define", p.Define, ParameterDirection.Input));
|
|
|
|
|
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
|
|
|
|
}
|
|
|
|
|
ret = true;
|
2016-11-11 14:32:52 +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-11-05 18:44:41 +08:00
|
|
|
|
public static IEnumerable<Dict> RetrieveWebSettings()
|
|
|
|
|
{
|
|
|
|
|
return CacheManager.GetOrAdd(RetrieveWebSettingsDataKey, CacheSection.RetrieveIntervalByKey(RetrieveWebSettingsDataKey), key =>
|
|
|
|
|
{
|
2016-12-09 20:13:22 +08:00
|
|
|
|
string sql = "select ID, Category, Name, Code, Define from Dicts where Category = N'网站设置' and Define = 0";
|
2016-11-05 18:44:41 +08:00
|
|
|
|
List<Dict> Dicts = new List<Dict>();
|
|
|
|
|
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
|
|
|
|
|
{
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
Dicts.Add(new Dict()
|
|
|
|
|
{
|
|
|
|
|
ID = (int)reader[0],
|
|
|
|
|
Category = (string)reader[1],
|
|
|
|
|
Name = (string)reader[2],
|
|
|
|
|
Code = (string)reader[3],
|
2016-12-09 20:13:22 +08:00
|
|
|
|
Define = (int)reader[4]
|
2016-11-05 18:44:41 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) { ExceptionManager.Publish(ex); }
|
|
|
|
|
return Dicts;
|
|
|
|
|
}, CacheSection.RetrieveDescByKey(RetrieveWebSettingsDataKey));
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string RetrieveWebTitle()
|
|
|
|
|
{
|
|
|
|
|
var settings = DictHelper.RetrieveWebSettings();
|
|
|
|
|
return (settings.FirstOrDefault(d => d.Name == "网站标题") ?? new Dict() { Code = "后台管理系统" }).Code;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string RetrieveWebFooter()
|
|
|
|
|
{
|
|
|
|
|
var settings = DictHelper.RetrieveWebSettings();
|
|
|
|
|
return (settings.FirstOrDefault(d => d.Name == "网站页脚") ?? new Dict() { Code = "2016 © 通用后台管理系统" }).Code;
|
|
|
|
|
}
|
2016-11-08 15:35:14 +08:00
|
|
|
|
/// <summary>
|
2016-12-09 20:13:22 +08:00
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static IEnumerable<Dict> RetrieveWebCss()
|
|
|
|
|
{
|
|
|
|
|
var data = RetrieveDicts();
|
|
|
|
|
return data.Where(d => d.Category == "网站样式");
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static IEnumerable<Dict> RetrieveActiveCss()
|
|
|
|
|
{
|
|
|
|
|
var data = RetrieveDicts();
|
2016-12-16 10:34:36 +08:00
|
|
|
|
return data.Where(d => d.Category == "当前样式" && !d.Code.Equals("site.css", StringComparison.OrdinalIgnoreCase));
|
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>
|
2016-11-10 00:47:23 +08:00
|
|
|
|
public static bool SaveProfiles(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))
|
|
|
|
|
{
|
2016-11-10 00:47:23 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Name", name, ParameterDirection.Input));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Code", code, ParameterDirection.Input));
|
2016-11-08 15:35:14 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Category", category, ParameterDirection.Input));
|
|
|
|
|
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
|
|
|
|
}
|
2016-12-09 20:13:22 +08:00
|
|
|
|
CacheManager.Clear(key => key.Contains(RetrieveDictsDataKey));
|
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>
|
|
|
|
|
/// 获取头像路径
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2016-11-16 17:27:03 +08:00
|
|
|
|
public static Dict RetrieveIconFolderPath()
|
2016-11-15 11:15:38 +08:00
|
|
|
|
{
|
2016-11-16 17:27:03 +08:00
|
|
|
|
return CacheManager.GetOrAdd(RetrieveIconPathSettingsDataKey, CacheSection.RetrieveIntervalByKey(RetrieveIconPathSettingsDataKey), key =>
|
|
|
|
|
{
|
2016-12-09 20:13:22 +08:00
|
|
|
|
string sql = "select ID, Category, Name, Code, Define from Dicts where Category = N'头像地址' and Name = N'头像路径' and Define = 0";
|
2016-11-16 17:27:03 +08:00
|
|
|
|
var dict = new Dict() { Code = "~/Content/images/uploader/" };
|
|
|
|
|
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
|
|
|
|
|
{
|
|
|
|
|
if (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
dict = new Dict()
|
|
|
|
|
{
|
|
|
|
|
ID = (int)reader[0],
|
|
|
|
|
Category = (string)reader[1],
|
|
|
|
|
Name = (string)reader[2],
|
|
|
|
|
Code = (string)reader[3],
|
2016-12-09 20:13:22 +08:00
|
|
|
|
Define = (int)reader[4]
|
2016-11-16 17:27:03 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) { ExceptionManager.Publish(ex); }
|
|
|
|
|
return dict;
|
|
|
|
|
}, CacheSection.RetrieveDescByKey(RetrieveIconPathSettingsDataKey));
|
2016-11-15 11:15:38 +08:00
|
|
|
|
}
|
2016-11-17 20:01:28 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取字典分类名称
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2016-12-09 20:13:22 +08:00
|
|
|
|
public static IEnumerable<Dict> RetrieveCategories()
|
2016-11-17 20:01:28 +08:00
|
|
|
|
{
|
|
|
|
|
return CacheManager.GetOrAdd(RetrieveCategoryDataKey, CacheSection.RetrieveIntervalByKey(RetrieveCategoryDataKey), key =>
|
|
|
|
|
{
|
2016-12-09 20:13:22 +08:00
|
|
|
|
var ret = new List<Dict>();
|
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())
|
|
|
|
|
{
|
2016-12-09 20:13:22 +08:00
|
|
|
|
ret.Add(new Dict() { Category = (string)reader[0] });
|
2016-11-17 20:01:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) { ExceptionManager.Publish(ex); }
|
|
|
|
|
return ret;
|
|
|
|
|
}, CacheSection.RetrieveDescByKey(RetrieveCategoryDataKey));
|
|
|
|
|
}
|
2016-11-03 21:28:19 +08:00
|
|
|
|
}
|
|
|
|
|
}
|