From 56c4b26258d7a081991b89aae4ac670de201d931 Mon Sep 17 00:00:00 2001 From: "liuchun_0206@163.com" Date: Sat, 22 Oct 2016 14:54:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86Groups=E7=9A=84?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bootstrap.Admin/Web.config | 1 + .../Bootstrap.DataAccess.csproj | 2 + Bootstrap.DataAccess/Group.cs | 25 ++++ Bootstrap.DataAccess/GroupHelper.cs | 113 ++++++++++++++++++ 4 files changed, 141 insertions(+) create mode 100644 Bootstrap.DataAccess/Group.cs create mode 100644 Bootstrap.DataAccess/GroupHelper.cs diff --git a/Bootstrap.Admin/Web.config b/Bootstrap.Admin/Web.config index 9466a92a..4091d0af 100644 --- a/Bootstrap.Admin/Web.config +++ b/Bootstrap.Admin/Web.config @@ -23,6 +23,7 @@ + diff --git a/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj b/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj index 69bd8958..883ff9d6 100644 --- a/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj +++ b/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj @@ -57,6 +57,8 @@ bldver.cs + + diff --git a/Bootstrap.DataAccess/Group.cs b/Bootstrap.DataAccess/Group.cs new file mode 100644 index 00000000..28f2c33e --- /dev/null +++ b/Bootstrap.DataAccess/Group.cs @@ -0,0 +1,25 @@ + +namespace Bootstrap.DataAccess +{ + /// + /// author:liuchun + /// date:2016.10.22 + /// + public class Group + { + /// + /// 获得/设置 群组主键ID + /// + public int ID { get; set; } + + /// + /// 获得/设置 群组名称 + /// + public string GroupName { get; set; } + + /// + /// 获得/设置 群组描述 + /// + public string Description { get; set; } + } +} diff --git a/Bootstrap.DataAccess/GroupHelper.cs b/Bootstrap.DataAccess/GroupHelper.cs new file mode 100644 index 00000000..9d9debd5 --- /dev/null +++ b/Bootstrap.DataAccess/GroupHelper.cs @@ -0,0 +1,113 @@ +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 +{ + /// + /// author:liuchun + /// date:2016.10.22 + /// + /// + public static class GroupHelper + { + private const string GroupDataKey = "GroupData-CodeGroupHelper"; + + /// + /// 查询所有群组信息 + /// + /// + /// + public static IEnumerable RetrieveGroups(string tId = null) + { + string sql = "select t.* from Groups t"; + var ret = CacheManager.GetOrAdd(GroupDataKey, CacheSection.RetrieveIntervalByKey(GroupDataKey), key => + { + List Groups = new List(); + DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); + try + { + using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) + { + while (reader.Read()) + { + Groups.Add(new Group() + { + ID = (int)reader[0], + GroupName = (string)reader[1], + Description = (string)reader[2] + }); + } + } + } + catch (Exception ex) { ExceptionManager.Publish(ex); } + return Groups; + }, CacheSection.RetrieveDescByKey(GroupDataKey)); + return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase)); + } + + + /// + /// 删除群组信息 + /// + /// + public static void DeleteGroup(string ids) + { + if (string.IsNullOrEmpty(ids) || ids.Contains("'")) return; + string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Groups where ID in ({0})", ids); + using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) + { + DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); + ClearCache(); + } + } + + /// + /// 保存新建/更新的群组信息 + /// + /// + /// + public static bool SaveGroup(Group p) + { + if (p == null) throw new ArgumentNullException("p"); + bool ret = false; + if (p.GroupName.Length > 50) p.GroupName.Substring(0, 50); + if (p.Description.Length > 500) p.Description.Substring(0, 500); + string sql = p.ID == 0 ? + "Insert Into Groups (GroupName, Description) Values (@GroupName, @Description)" : + "Update Groups set GroupName = @GroupName, Description = @Description 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("@GroupName", p.GroupName, ParameterDirection.Input)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Description", p.Description, 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.Contains("GroupData-")); + } + + + } +} +