diff --git a/Bootstrap.Admin/Web.config b/Bootstrap.Admin/Web.config index bd047db9..5ee79f09 100644 --- a/Bootstrap.Admin/Web.config +++ b/Bootstrap.Admin/Web.config @@ -31,6 +31,7 @@ + diff --git a/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj b/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj index 44f09ccf..07245110 100644 --- a/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj +++ b/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj @@ -61,6 +61,8 @@ + + diff --git a/Bootstrap.DataAccess/Log.cs b/Bootstrap.DataAccess/Log.cs new file mode 100644 index 00000000..e3c29f88 --- /dev/null +++ b/Bootstrap.DataAccess/Log.cs @@ -0,0 +1,51 @@ +using System; +namespace Bootstrap.DataAccess +{ + public class Log + { + /// + /// 获得/设置 操作日志主键ID + /// + public int ID { get; set; } + + /// + /// 获得/设置 操作类型 + /// + public int OperationType { get; set; } + + /// + /// 获得/设置 用户ID + /// + public int UserID { get; set; } + + /// + /// 获得/设置 操作时间 + /// + public DateTime OperationTime { get; set; } + + /// + /// 获得/设置 操作表表名 + /// + public string TableName { get; set; } + + /// + /// 获得/设置 操作内容 + /// + public string BusinessName { get; set; } + + /// + /// 获得/设置 操作表的主键 + /// + public string PrimaryKey { get; set; } + + /// + /// 获得/设置 sql语句 + /// + public string SqlText { get; set; } + + /// + /// 获得/设置 操作者Ip + /// + public string OperationIp { get; set; } + } +} diff --git a/Bootstrap.DataAccess/LogHelper.cs b/Bootstrap.DataAccess/LogHelper.cs new file mode 100644 index 00000000..bb3286b3 --- /dev/null +++ b/Bootstrap.DataAccess/LogHelper.cs @@ -0,0 +1,118 @@ +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 LogHelper + { + private const string LogDataKey = "LogData-CodeLogHelper"; + /// + /// 查询所有日志信息 + /// + /// + /// + public static IEnumerable RetrieveLogs(string tId = null) + { + string sql = "select * from Logs"; + var ret = CacheManager.GetOrAdd(LogDataKey, CacheSection.RetrieveIntervalByKey(LogDataKey), key => + { + List Logs = new List(); + DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); + try + { + using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) + { + while (reader.Read()) + { + Logs.Add(new Log() + { + ID = (int)reader[0], + OperationType = (int)reader[1], + UserID = (int)reader[2], + OperationTime = (DateTime)reader[3], + TableName = (string)reader[4], + BusinessName = (string)reader[5], + PrimaryKey = (string)reader[6], + SqlText = (string)reader[7], + OperationIp = (string)reader[8], + }); + } + } + } + catch (Exception ex) { ExceptionManager.Publish(ex); } + return Logs; + }, CacheSection.RetrieveDescByKey(LogDataKey)); + return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase)); + } + /// + /// 删除日志信息 + /// + /// + /// + public static bool DeleteLog(string ids) + { + bool ret = false; + if (string.IsNullOrEmpty(ids) || ids.Contains("'")) return ret; + try + { + string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Logs 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 saveLog(Log p) + { + if (p == null) throw new ArgumentNullException("p"); + bool ret = false; + string sql = "Insert Into Logs (OperationType, UserID,OperationTime,TableName,BusinessName,PrimaryKey,SqlText,OperationIp) Values (@OperationType, @UserID,@OperationTime,@TableName,@BusinessName,@PrimaryKey,@SqlText,@OperationIp)"; + try + { + using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) + { + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@OperationType", p.OperationType, ParameterDirection.Input)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserID", p.UserID, ParameterDirection.Input)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@OperationTime", p.OperationTime, ParameterDirection.Input)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@TableName", p.TableName, ParameterDirection.Input)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@BusinessName", p.BusinessName, ParameterDirection.Input)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@PrimaryKey", p.PrimaryKey, ParameterDirection.Input)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@SqlText", p.SqlText, ParameterDirection.Input)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@OperationIp", p.OperationIp, 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 == LogDataKey); + } + } +} diff --git a/Bootstrap.DataAccessTests/Bootstrap.DataAccessTests.csproj b/Bootstrap.DataAccessTests/Bootstrap.DataAccessTests.csproj index 8e1742bb..d623012a 100644 --- a/Bootstrap.DataAccessTests/Bootstrap.DataAccessTests.csproj +++ b/Bootstrap.DataAccessTests/Bootstrap.DataAccessTests.csproj @@ -59,6 +59,7 @@ + diff --git a/Bootstrap.DataAccessTests/LogHelperTests.cs b/Bootstrap.DataAccessTests/LogHelperTests.cs new file mode 100644 index 00000000..54abd1bd --- /dev/null +++ b/Bootstrap.DataAccessTests/LogHelperTests.cs @@ -0,0 +1,40 @@ +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Bootstrap.DataAccess.Tests +{ + [TestClass] + public class LogHelperTests + { + [TestMethod] + public void RetrieveLogsTest() + { + var result = LogHelper.RetrieveLogs("1"); + Assert.IsTrue((result.Count() == 0 || result.Count() == 1), "带有参数的LogHelper.RetrieveLogs方法调用失败,请检查数据库连接或者数据库SQL语句"); + result = LogHelper.RetrieveLogs(); + Assert.IsTrue(result.Count() >= 0, "带有参数的LogHelper.RetrieveLogs方法调用失败,请检查数据库连接或者数据库SQL语句"); + } + [TestMethod] + public void saveLogTest() + { + Log p = new Log(); + p.OperationType = 1; + p.UserID = 1; + p.OperationTime = System.DateTime.Now; + p.TableName = "日志"; + p.BusinessName = "新增日志信息"; + p.PrimaryKey = "ID"; + p.SqlText = "Insert Into Logs"; + p.OperationIp = "0.0.0.0"; + + var result = LogHelper.saveLog(p); + Assert.IsTrue(result, "新增日志信息出错"); + } + [TestMethod] + public void DeleteLogTest() + { + string p = "2"; + Assert.IsTrue(LogHelper.DeleteLog(p),"删除日志信息出错"); + } + } +} diff --git a/DatabaseScripts/Install.sql b/DatabaseScripts/Install.sql index c80f84b2..7900e8cc 100644 --- a/DatabaseScripts/Install.sql +++ b/DatabaseScripts/Install.sql @@ -144,6 +144,33 @@ EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'部门名称' GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'描述' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Groups', @level2type=N'COLUMN',@level2name=N'Description' GO + +/****** Object: Table [dbo].[Logs] Script Date: 10/28/2016 16:39:11 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[Logs]( + [ID] [int] IDENTITY(1,1) NOT NULL, + [OperationType] [int] NULL, + [UserID] [int] NULL, + [OperationTime] [datetime] NULL, + [TableName] [nvarchar](50) NULL, + [BusinessName] [nvarchar](50) NULL, + [PrimaryKey] [nvarchar](50) NULL, + [SqlText] [nvarchar](max) NULL, + [OperationIp] [nvarchar](50) NULL, + CONSTRAINT [PK_Logs] 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 +EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Logs', @level2type=N'COLUMN',@level2name=N'ID' +GO +EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Logs', @level2type=N'COLUMN',@level2name=N'OperationType' +GO + CREATE TABLE [dbo].[Navigations]( [ID] [int] IDENTITY(1,1) NOT NULL, [ParentId] [int] NOT NULL,