⚡新增数据库差异化日志记录
This commit is contained in:
parent
09c45c4ba4
commit
240a607875
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据差异日志
|
||||
/// </summary>
|
||||
[Verify]
|
||||
[Route("monitor/SqlDiffLog")]
|
||||
public class SqlDiffLogController : BaseController
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据差异日志接口
|
||||
/// </summary>
|
||||
private readonly ISqlDiffLogService _SqlDiffLogService;
|
||||
|
||||
public SqlDiffLogController(ISqlDiffLogService SqlDiffLogService)
|
||||
{
|
||||
_SqlDiffLogService = SqlDiffLogService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询数据差异日志列表
|
||||
/// </summary>
|
||||
/// <param name="parm"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("list")]
|
||||
[ActionPermissionFilter(Permission = "sqldifflog:list")]
|
||||
public IActionResult QuerySqlDiffLog([FromQuery] SqlDiffLogQueryDto parm)
|
||||
{
|
||||
var response = _SqlDiffLogService.GetList(parm);
|
||||
return SUCCESS(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除数据差异日志
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 导出数据差异日志
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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()
|
||||
|
||||
Binary file not shown.
58
ZR.Model/System/Dto/SqlDiffLogDto.cs
Normal file
58
ZR.Model/System/Dto/SqlDiffLogDto.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using MiniExcelLibs.Attributes;
|
||||
|
||||
namespace ZR.Model.System.Dto
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据差异日志查询对象
|
||||
/// </summary>
|
||||
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; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据差异日志输入输出对象
|
||||
/// </summary>
|
||||
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; }
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@ -72,9 +72,10 @@ namespace ZR.Repository
|
||||
/// <param name="entity"></param>
|
||||
/// <param name="ignoreNullColumns"></param>
|
||||
/// <returns></returns>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -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);
|
||||
|
||||
/// <summary>
|
||||
/// 只更新表达式的值
|
||||
|
||||
21
ZR.Service/System/IService/ISqlDiffLogService.cs
Normal file
21
ZR.Service/System/IService/ISqlDiffLogService.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using ZR.Model;
|
||||
using ZR.Model.System;
|
||||
using ZR.Model.System.Dto;
|
||||
|
||||
namespace ZR.Service.System.IService
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据差异日志service接口
|
||||
/// </summary>
|
||||
public interface ISqlDiffLogService : IBaseService<SqlDiffLog>
|
||||
{
|
||||
PagedInfo<SqlDiffLogDto> GetList(SqlDiffLogQueryDto parm);
|
||||
|
||||
SqlDiffLog GetInfo(long PId);
|
||||
|
||||
SqlDiffLog AddSqlDiffLog(SqlDiffLog parm);
|
||||
|
||||
int UpdateSqlDiffLog(SqlDiffLog parm);
|
||||
|
||||
}
|
||||
}
|
||||
91
ZR.Service/System/SqlDiffLogService.cs
Normal file
91
ZR.Service/System/SqlDiffLogService.cs
Normal file
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据差异日志Service业务层处理
|
||||
/// </summary>
|
||||
[AppService(ServiceType = typeof(ISqlDiffLogService), ServiceLifetime = LifeTime.Transient)]
|
||||
public class SqlDiffLogService : BaseService<SqlDiffLog>, ISqlDiffLogService
|
||||
{
|
||||
/// <summary>
|
||||
/// 查询数据差异日志列表
|
||||
/// </summary>
|
||||
/// <param name="parm"></param>
|
||||
/// <returns></returns>
|
||||
public PagedInfo<SqlDiffLogDto> GetList(SqlDiffLogQueryDto parm)
|
||||
{
|
||||
var predicate = Expressionable.Create<SqlDiffLog>();
|
||||
|
||||
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<SqlDiffLog, SqlDiffLogDto>(parm);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取详情
|
||||
/// </summary>
|
||||
/// <param name="PId"></param>
|
||||
/// <returns></returns>
|
||||
public SqlDiffLog GetInfo(long PId)
|
||||
{
|
||||
var response = Queryable()
|
||||
.Where(x => x.PId == PId)
|
||||
.First();
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加数据差异日志
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
public SqlDiffLog AddSqlDiffLog(SqlDiffLog model)
|
||||
{
|
||||
return Context.Insertable(model).ExecuteReturnEntity();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改数据差异日志
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user