diff --git a/Bootstrap.Admin/Web.config b/Bootstrap.Admin/Web.config index 902add1e..bd047db9 100644 --- a/Bootstrap.Admin/Web.config +++ b/Bootstrap.Admin/Web.config @@ -30,7 +30,8 @@ - + + diff --git a/Bootstrap.DataAccess/Dict.cs b/Bootstrap.DataAccess/Dict.cs new file mode 100644 index 00000000..7df76a2e --- /dev/null +++ b/Bootstrap.DataAccess/Dict.cs @@ -0,0 +1,32 @@ +namespace Bootstrap.DataAccess +{ + /// + /// 字典表实体 + /// Category与Code自关联 + /// author:renshuo + /// date:2016.10.27 + /// + public class Dict + { + /// + /// 字典主键 + /// 数据库自增 + /// + public int ID { get; set; } + + /// + /// 分类 + /// + public string Category { get; set; } + + /// + /// 名称 + /// + public string Name { get; set; } + + /// + /// 代号 + /// + public string Code { get; set; } + } +} diff --git a/Bootstrap.DataAccess/DictHelper.cs b/Bootstrap.DataAccess/DictHelper.cs new file mode 100644 index 00000000..05e76379 --- /dev/null +++ b/Bootstrap.DataAccess/DictHelper.cs @@ -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"; + + /// + /// 查询所有字典信息 + /// + /// + /// + public static IEnumerable RetrieveDicts(string tId = null) + { + string sql = "select * from Dicts"; + var ret = CacheManager.GetOrAdd(DictDataKey, CacheSection.RetrieveIntervalByKey(DictDataKey), key => + { + List Dicts = new List(); + 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)); + } + + /// + /// 删除字典中的数据 + /// + /// 需要删除的IDs + /// + 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; + } + + /// + /// 保存新建/更新的字典信息 + /// + /// + /// + 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; + } + + /// + /// 更新缓存 + /// + private static void ClearCache() + { + CacheManager.Clear(key => key == DictDataKey); + } + } +} diff --git a/Bootstrap.DataAccessTests/DictHelperTests.cs b/Bootstrap.DataAccessTests/DictHelperTests.cs new file mode 100644 index 00000000..93783d09 --- /dev/null +++ b/Bootstrap.DataAccessTests/DictHelperTests.cs @@ -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, "删除用户出错"); + } + } + } +} diff --git a/DatabaseScripts/Install.sql b/DatabaseScripts/Install.sql index e0ff9bd0..6db08bbd 100644 --- a/DatabaseScripts/Install.sql +++ b/DatabaseScripts/Install.sql @@ -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')