using DS.Module.Core;
using DS.Module.Core.Extensions;
using DS.Module.Core.Log;
using DS.Module.SqlSugar;
using DS.Module.UserModule;
using DS.WMS.Core.Sys.Dtos;
using DS.WMS.Core.Sys.Entity;
using DS.WMS.Core.Sys.Interface;
using Google.Cloud.Storage.V1;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using SqlSugar;
namespace DS.WMS.Core.Sys.Method;
public class LogAuditService : ILogAuditService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
private readonly ISaasDbService saasService;
///
///
///
///
public LogAuditService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService();
user = _serviceProvider.GetRequiredService();
saasService = _serviceProvider.GetRequiredService();
}
public DataResult> GetAuditLogList(PageRequest request)
{
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = saasService.GetLogDb().Queryable().Filter(null,true)
.Where(whereList)
.Select().ToQueryPage(request.PageCondition);
return data;
}
public DataResult GetAuditLogInfo(string id)
{
var data = saasService.GetLogDb().Queryable().Filter(null,true)
.Where(a => a.Id == long.Parse(id))
.Select()
.First();
return DataResult.Success(data,MultiLanguageConst.DataQuerySuccess);
}
#region 获取订舱类操作日志列表
///
/// 获取订舱类操作日志列表
///
/// 业务ID
/// 返回操作日志列表
public async Task>> GetAuditLogBookingList(long id)
{
List list = new List();
try
{
var logList = await saasService.GetLogDb().Queryable().Filter(null, true)
.Where(a => a.Id == id).ToListAsync();
if (logList.Count > 0)
{
list = logList.Select(a =>
{
var model = new AuditLogBookingDto
{
Type = a.OperateType,
BookingId = a.KeyId,
CreatedTime = a.CreateTime,
CreatedUserId = a.CreateBy,
FromFunc = "",
Id = a.Id,
TenantId = a.TenantId,
details = new List()
};
DiffLogTableInfo oldObj = null;
DiffLogTableInfo newObj = null;
try
{
oldObj = JsonConvert.DeserializeObject(a.OldValue);
}
catch (Exception e1)
{
throw new Exception(string.Format(MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.BookingSlotAuditLogDeserializeError)), e1.Message));
}
try
{
newObj = JsonConvert.DeserializeObject(a.NewValue);
}
catch (Exception e2)
{
throw new Exception(string.Format(MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.BookingSlotAuditLogDeserializeError)), e2.Message));
}
if (oldObj != null && newObj != null)
{
model.details = oldObj.Columns.Join(newObj.Columns, l => l.ColumnName, r => r.ColumnName, (l, r) =>
{
string left = l.Value != null ? l.Value.ToString() : "";
string right = r.Value != null ? r.Value.ToString() : "";
if (!left.Equals(right))
{
return new
{
IsDiff = true,
Obj = new AuditLogBookingDetailDto
{
Field = !string.IsNullOrWhiteSpace(l.ColumnDescription) ? l.ColumnDescription : l.ColumnName,
OldValue = left,
NewValue = right,
}
};
}
else
{
return new
{
IsDiff = false,
Obj = new AuditLogBookingDetailDto
{
Field = !string.IsNullOrWhiteSpace(l.ColumnDescription) ? l.ColumnDescription : l.ColumnName,
OldValue = left,
NewValue = right,
}
};
}
}).Where(a => a.IsDiff).Select(a => a.Obj).ToList();
}
return model;
}).OrderByDescending(a => a.CreatedTime).ToList();
return DataResult>.Success(list);
}
}
catch (Exception e)
{
//查询日志异常
return DataResult>.FailedData(list, string.Format(MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.BookingSlotAuditLogError)),e.Message));
}
return DataResult>.FailedData(list);
}
#endregion
}