diff --git a/ZR.Admin.WebApi/Controllers/System/monitor/SqlDiffLogController.cs b/ZR.Admin.WebApi/Controllers/System/monitor/SqlDiffLogController.cs new file mode 100644 index 0000000..effebc8 --- /dev/null +++ b/ZR.Admin.WebApi/Controllers/System/monitor/SqlDiffLogController.cs @@ -0,0 +1,76 @@ +using Microsoft.AspNetCore.Mvc; +using ZR.Admin.WebApi.Filters; +using ZR.Model.System.Dto; +using ZR.Service.System.IService; + +//创建时间:2023-08-17 +namespace ZR.Admin.WebApi.Controllers +{ + /// + /// 数据差异日志 + /// + [Verify] + [Route("monitor/SqlDiffLog")] + public class SqlDiffLogController : BaseController + { + /// + /// 数据差异日志接口 + /// + private readonly ISqlDiffLogService _SqlDiffLogService; + + public SqlDiffLogController(ISqlDiffLogService SqlDiffLogService) + { + _SqlDiffLogService = SqlDiffLogService; + } + + /// + /// 查询数据差异日志列表 + /// + /// + /// + [HttpGet("list")] + [ActionPermissionFilter(Permission = "sqldifflog:list")] + public IActionResult QuerySqlDiffLog([FromQuery] SqlDiffLogQueryDto parm) + { + var response = _SqlDiffLogService.GetList(parm); + return SUCCESS(response); + } + + /// + /// 删除数据差异日志 + /// + /// + [HttpDelete("{ids}")] + [ActionPermissionFilter(Permission = "sqldifflog:delete")] + [Log(Title = "数据差异日志", BusinessType = BusinessType.DELETE)] + public IActionResult DeleteSqlDiffLog(string ids) + { + long[] idsArr = Tools.SpitLongArrary(ids); + if (idsArr.Length <= 0) { return ToResponse(ApiResult.Error($"删除失败Id 不能为空")); } + + var response = _SqlDiffLogService.Delete(idsArr); + + return ToResponse(response); + } + + /// + /// 导出数据差异日志 + /// + /// + [Log(Title = "数据差异日志", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)] + [HttpGet("export")] + [ActionPermissionFilter(Permission = "sqldifflog:export")] + public IActionResult Export([FromQuery] SqlDiffLogQueryDto parm) + { + parm.PageNum = 1; + parm.PageSize = 100000; + var list = _SqlDiffLogService.GetList(parm).Result; + if (list == null || list.Count <= 0) + { + return ToResponse(ResultCode.FAIL, "没有要导出的数据"); + } + var result = ExportExcelMini(list, "数据差异日志", "数据差异日志"); + return ExportExcel(result.Item2, result.Item1); + } + } +} \ No newline at end of file diff --git a/ZR.Admin.WebApi/Extensions/DbExtension.cs b/ZR.Admin.WebApi/Extensions/DbExtension.cs index 4aacff9..efdefac 100644 --- a/ZR.Admin.WebApi/Extensions/DbExtension.cs +++ b/ZR.Admin.WebApi/Extensions/DbExtension.cs @@ -116,29 +116,33 @@ namespace ZR.Admin.WebApi.Extensions var data = it.BusinessData;//这边会显示你传进来的对象 var time = it.Time; var diffType = it.DiffType;//enum insert 、update and delete + string name = App.UserName; - if (diffType == DiffType.delete) + foreach (var item in editBeforeData) { - string name = App.UserName; + var pars = db.Utilities.SerializeObject(item.Columns.ToDictionary(it => it.ColumnName, it => it.Value)); - foreach (var item in editBeforeData) + SqlDiffLog log = new() { - var pars = db.Utilities.SerializeObject(item.Columns.ToDictionary(it => it.ColumnName, it => it.Value)); - - SqlDiffLog log = new() - { - BeforeData = pars, - BusinessData = data?.ToString(), - DiffType = diffType.ToString(), - Sql = sql, - TableName = item.TableName, - UserName = name, - AddTime = DateTime.Now, - ConfigId = configId - }; - //logger.WithProperty("title", data).Info(pars); - db.GetConnectionScope(0).Insertable(log).ExecuteReturnSnowflakeId(); + BeforeData = pars, + BusinessData = data?.ToString(), + DiffType = diffType.ToString(), + Sql = sql, + TableName = item.TableName, + UserName = name, + AddTime = DateTime.Now, + ConfigId = configId + }; + if (editAfterData != null) + { + var afterData = editAfterData?.First(x => x.TableName == item.TableName); + var afterPars = db.Utilities.SerializeObject(afterData?.Columns.ToDictionary(it => it.ColumnName, it => it.Value)); + log.AfterData = afterPars; } + //logger.WithProperty("title", data).Info(pars); + db.GetConnectionScope(0) + .Insertable(log) + .ExecuteReturnSnowflakeId(); } }; db.GetConnectionScope(configId).CurrentConnectionConfig.MoreSettings = new ConnMoreSettings() diff --git a/ZR.Admin.WebApi/wwwroot/data.xlsx b/ZR.Admin.WebApi/wwwroot/data.xlsx index a35adec..574ac2f 100644 Binary files a/ZR.Admin.WebApi/wwwroot/data.xlsx and b/ZR.Admin.WebApi/wwwroot/data.xlsx differ diff --git a/ZR.Model/System/Dto/SqlDiffLogDto.cs b/ZR.Model/System/Dto/SqlDiffLogDto.cs new file mode 100644 index 0000000..6eb8df6 --- /dev/null +++ b/ZR.Model/System/Dto/SqlDiffLogDto.cs @@ -0,0 +1,58 @@ +using System.ComponentModel.DataAnnotations; +using MiniExcelLibs.Attributes; + +namespace ZR.Model.System.Dto +{ + /// + /// 数据差异日志查询对象 + /// + public class SqlDiffLogQueryDto : PagerInfo + { + public string TableName { get; set; } + public string DiffType { get; set; } + public string UserName { get; set; } + public DateTime? BeginAddTime { get; set; } + public DateTime? EndAddTime { get; set; } + } + + /// + /// 数据差异日志输入输出对象 + /// + public class SqlDiffLogDto + { + [Required(ErrorMessage = "主键不能为空")] + [ExcelColumn(Name = "主键")] + [JsonConverter(typeof(ValueToStringConverter))] + public long PId { get; set; } + + [ExcelColumn(Name = "表名")] + public string TableName { get; set; } + + [ExcelColumn(Name = "业务数据内容")] + public string BusinessData { get; set; } + + [ExcelColumn(Name = "差异类型")] + public string DiffType { get; set; } + + [ExcelColumn(Name = "执行sql语句")] + public string Sql { get; set; } + + [ExcelColumn(Name = "变更前数据")] + public string BeforeData { get; set; } + + [ExcelColumn(Name = "变更后数据")] + public string AfterData { get; set; } + + [ExcelColumn(Name = "操作用户名")] + public string UserName { get; set; } + + [ExcelColumn(Name = "AddTime", Format = "yyyy-MM-dd HH:mm:ss")] + public DateTime? AddTime { get; set; } + + [ExcelColumn(Name = "数据库配置id")] + public string ConfigId { get; set; } + + + + } +} \ No newline at end of file diff --git a/ZR.Repository/BaseRepository.cs b/ZR.Repository/BaseRepository.cs index 4f9dc89..01fa179 100644 --- a/ZR.Repository/BaseRepository.cs +++ b/ZR.Repository/BaseRepository.cs @@ -72,9 +72,10 @@ namespace ZR.Repository /// /// /// - public int Update(T entity, bool ignoreNullColumns = false) + public int Update(T entity, bool ignoreNullColumns = false, object data = null) { - return Context.Updateable(entity).IgnoreColumns(ignoreNullColumns).ExecuteCommand(); + return Context.Updateable(entity).IgnoreColumns(ignoreNullColumns) + .EnableDiffLogEventIF(data.IsNotEmpty(), data).ExecuteCommand(); } /// diff --git a/ZR.Repository/IBaseRepository.cs b/ZR.Repository/IBaseRepository.cs index 1e0f204..2e44a8e 100644 --- a/ZR.Repository/IBaseRepository.cs +++ b/ZR.Repository/IBaseRepository.cs @@ -19,7 +19,7 @@ namespace ZR.Repository #endregion add #region update - int Update(T entity, bool ignoreNullColumns = false); + int Update(T entity, bool ignoreNullColumns = false, object data = null); /// /// 只更新表达式的值 diff --git a/ZR.Service/System/IService/ISqlDiffLogService.cs b/ZR.Service/System/IService/ISqlDiffLogService.cs new file mode 100644 index 0000000..2df07ce --- /dev/null +++ b/ZR.Service/System/IService/ISqlDiffLogService.cs @@ -0,0 +1,21 @@ +using ZR.Model; +using ZR.Model.System; +using ZR.Model.System.Dto; + +namespace ZR.Service.System.IService +{ + /// + /// 数据差异日志service接口 + /// + public interface ISqlDiffLogService : IBaseService + { + PagedInfo GetList(SqlDiffLogQueryDto parm); + + SqlDiffLog GetInfo(long PId); + + SqlDiffLog AddSqlDiffLog(SqlDiffLog parm); + + int UpdateSqlDiffLog(SqlDiffLog parm); + + } +} diff --git a/ZR.Service/System/SqlDiffLogService.cs b/ZR.Service/System/SqlDiffLogService.cs new file mode 100644 index 0000000..0b88efd --- /dev/null +++ b/ZR.Service/System/SqlDiffLogService.cs @@ -0,0 +1,91 @@ +using Infrastructure.Attribute; +using Infrastructure.Extensions; +using SqlSugar; +using System; +using ZR.Model; +using ZR.Model.System; +using ZR.Model.System.Dto; +using ZR.Repository; +using ZR.Service.System.IService; + +namespace ZR.Service.System +{ + /// + /// 数据差异日志Service业务层处理 + /// + [AppService(ServiceType = typeof(ISqlDiffLogService), ServiceLifetime = LifeTime.Transient)] + public class SqlDiffLogService : BaseService, ISqlDiffLogService + { + /// + /// 查询数据差异日志列表 + /// + /// + /// + public PagedInfo GetList(SqlDiffLogQueryDto parm) + { + var predicate = Expressionable.Create(); + + predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.TableName), it => it.TableName == parm.TableName); + predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.DiffType), it => it.DiffType == parm.DiffType); + predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.UserName), it => it.UserName == parm.UserName); + predicate = predicate.AndIF(parm.BeginAddTime == null, it => it.AddTime >= DateTime.Now.ToShortDateString().ParseToDateTime()); + predicate = predicate.AndIF(parm.BeginAddTime != null, it => it.AddTime >= parm.BeginAddTime); + predicate = predicate.AndIF(parm.EndAddTime != null, it => it.AddTime <= parm.EndAddTime); + var response = Queryable() + //.OrderBy("PId desc") + .Where(predicate.ToExpression()) + .ToPage(parm); + + return response; + } + + + /// + /// 获取详情 + /// + /// + /// + public SqlDiffLog GetInfo(long PId) + { + var response = Queryable() + .Where(x => x.PId == PId) + .First(); + + return response; + } + + /// + /// 添加数据差异日志 + /// + /// + /// + public SqlDiffLog AddSqlDiffLog(SqlDiffLog model) + { + return Context.Insertable(model).ExecuteReturnEntity(); + } + + /// + /// 修改数据差异日志 + /// + /// + /// + public int UpdateSqlDiffLog(SqlDiffLog model) + { + //var response = Update(w => w.PId == model.PId, it => new SqlDiffLog() + //{ + // TableName = model.TableName, + // BusinessData = model.BusinessData, + // DiffType = model.DiffType, + // Sql = model.Sql, + // BeforeData = model.BeforeData, + // AfterData = model.AfterData, + // UserName = model.UserName, + // AddTime = model.AddTime, + // ConfigId = model.ConfigId, + //}); + //return response; + return Update(model, true); + } + + } +} \ No newline at end of file