完成字典表Dict后台基本操作
This commit is contained in:
parent
83bdfe3b71
commit
bed26d405e
|
@ -30,7 +30,8 @@
|
|||
<add key="RoleData-CodeRoleHelper" interval="600" desc="角色信息缓存" />
|
||||
<add key="RoleData-CodeRoleHelper-" interval="600" desc="角色信息缓存" />
|
||||
<add key="RoleData-CodeRoleHelper-Navigation-" interval="600" desc="菜单角色信息缓存"/>
|
||||
<add key="MenuData-CodeMenuHelper" interval="600" desc="菜单信息缓存" />
|
||||
<add key="MenuData-CodeMenuHelper" interval="600" desc="菜单信息缓存" />
|
||||
<add key="DictData-CodeDictHelper" interval="600" desc="字典信息缓存" />
|
||||
</cacheManager>
|
||||
|
||||
<cacheManagerList>
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
namespace Bootstrap.DataAccess
|
||||
{
|
||||
/// <summary>
|
||||
/// 字典表实体
|
||||
/// Category与Code自关联
|
||||
/// author:renshuo
|
||||
/// date:2016.10.27
|
||||
/// </summary>
|
||||
public class Dict
|
||||
{
|
||||
/// <summary>
|
||||
/// 字典主键
|
||||
/// 数据库自增
|
||||
/// </summary>
|
||||
public int ID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 分类
|
||||
/// </summary>
|
||||
public string Category { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 名称
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 代号
|
||||
/// </summary>
|
||||
public string Code { get; set; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,120 @@
|
|||
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
|
||||
{
|
||||
public class DictHelper
|
||||
{
|
||||
private const string DictDataKey = "DictData-CodeDictHelper";
|
||||
|
||||
/// <summary>
|
||||
/// 查询所有字典信息
|
||||
/// </summary>
|
||||
/// <param name="tId"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Dict> RetrieveDicts(string tId = null)
|
||||
{
|
||||
string sql = "select * from Dicts";
|
||||
var ret = CacheManager.GetOrAdd(DictDataKey, CacheSection.RetrieveIntervalByKey(DictDataKey), key =>
|
||||
{
|
||||
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]
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex) { ExceptionManager.Publish(ex); }
|
||||
return Dicts;
|
||||
}, CacheSection.RetrieveDescByKey(DictDataKey));
|
||||
return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
/// <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);
|
||||
ClearCache();
|
||||
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) Values (@Category, @Name, @Code)" :
|
||||
"Update Dicts set Category = @Category, Name = @Name, @Code = Code where ID = @ID";
|
||||
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));
|
||||
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
||||
}
|
||||
ret = true;
|
||||
ClearCache();
|
||||
}
|
||||
catch (DbException ex)
|
||||
{
|
||||
ExceptionManager.Publish(ex);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新缓存
|
||||
/// </summary>
|
||||
private static void ClearCache()
|
||||
{
|
||||
CacheManager.Clear(key => key == DictDataKey);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
using System.Linq;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace Bootstrap.DataAccess.Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class DictHelperTests
|
||||
{
|
||||
|
||||
[TestMethod]
|
||||
public void RetrieveDictsTest()
|
||||
{
|
||||
SaveDictTest();
|
||||
var result = DictHelper.RetrieveDicts("1");
|
||||
Assert.IsTrue((result.Count() == 0 || result.Count() == 1), "带有参数的DictHelper.RetrieveDicts方法调用失败,请检查数据库连接或者数据库SQL语句");
|
||||
result = DictHelper.RetrieveDicts();
|
||||
Assert.IsTrue(result.Count() >= 0, "不带参数的DictHelper.RetrieveDicts方法调用失败,请检查数据库连接或者数据库SQL语句");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SaveDictTest()
|
||||
{
|
||||
Dict p = new Dict();
|
||||
p.Category = "测试省份";
|
||||
p.Name = "测试城市";
|
||||
p.Code = "测试字典";
|
||||
var result = DictHelper.SaveDict(p);
|
||||
Assert.IsTrue(result, "增加用户出错");
|
||||
|
||||
p.ID = 1;
|
||||
p.Category = "测试省份22";
|
||||
p.Name = "测试城市22";
|
||||
p.Code = "测试字典22";
|
||||
result = DictHelper.SaveDict(p);
|
||||
Assert.IsTrue(result, "更新用户出错");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DeleteDictTest()
|
||||
{
|
||||
SaveDictTest();
|
||||
string p = "1";
|
||||
try
|
||||
{
|
||||
DictHelper.DeleteDict(p);
|
||||
Assert.IsTrue(true);
|
||||
}
|
||||
catch
|
||||
{
|
||||
Assert.IsTrue(false, "删除用户出错");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -175,6 +175,28 @@ GO
|
|||
ALTER TABLE [dbo].[Navigations] ADD CONSTRAINT [DF_Navigations_Category] DEFAULT ((0)) FOR [Category]
|
||||
GO
|
||||
|
||||
/****** Object: Table [dbo].[Dicts] Script Date: 2016/10/27 星期四 16:35:54 ******/
|
||||
SET ANSI_NULLS ON
|
||||
GO
|
||||
SET QUOTED_IDENTIFIER ON
|
||||
GO
|
||||
SET ANSI_PADDING ON
|
||||
GO
|
||||
CREATE TABLE [dbo].[Dicts](
|
||||
[ID] [int] IDENTITY(1,1) NOT NULL,
|
||||
[Category] [nvarchar](50) NOT NULL,
|
||||
[Name] [nvarchar](50) NOT NULL,
|
||||
[Code] [varchar](50) NOT NULL,
|
||||
CONSTRAINT [PK_dbo.Dict] PRIMARY KEY CLUSTERED
|
||||
(
|
||||
[ID] ASC
|
||||
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
|
||||
) ON [PRIMARY]
|
||||
|
||||
GO
|
||||
SET ANSI_PADDING OFF
|
||||
GO
|
||||
|
||||
SET IDENTITY_INSERT Navigations ON
|
||||
insert into Navigations (ID, Name, [Order], Icon, Url) values (1, '菜单管理', 10, 'fa fa-dashboard', '~/Admin/Menus')
|
||||
insert into Navigations (ID, Name, [Order], Icon, Url) values (2, '用户管理', 20, 'fa fa-user', '~/Admin/Users')
|
||||
|
|
Loading…
Reference in New Issue