using Magicodes.ExporterAndImporter.Excel;
namespace Admin.NET.Core.Service;
///
/// 系统异常日志服务
///
[ApiDescriptionSettings(Order = 350)]
public class SysLogExService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _sysLogExRep;
public SysLogExService(SqlSugarRepository sysLogExRep)
{
_sysLogExRep = sysLogExRep;
}
///
/// 获取异常日志分页列表
///
///
[SuppressMonitor]
[DisplayName("获取异常日志分页列表")]
public async Task> Page(PageLogInput input)
{
return await _sysLogExRep.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.StartTime.ToString()), u => u.CreateTime >= input.StartTime)
.WhereIF(!string.IsNullOrWhiteSpace(input.EndTime.ToString()), u => u.CreateTime <= input.EndTime)
//.OrderBy(u => u.CreateTime, OrderByType.Desc)
.OrderBuilder(input)
.ToPagedListAsync(input.Page, input.PageSize);
}
///
/// 清空异常日志
///
///
[ApiDescriptionSettings(Name = "Clear"), HttpPost]
[DisplayName("清空异常日志")]
public async Task Clear()
{
return await _sysLogExRep.DeleteAsync(u => u.Id > 0);
}
///
/// 导出异常日志
///
///
[ApiDescriptionSettings(Name = "Export"), NonUnify]
[DisplayName("导出异常日志")]
public async Task ExportLogEx(LogInput input)
{
var logExList = await _sysLogExRep.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.StartTime.ToString()) && !string.IsNullOrWhiteSpace(input.EndTime.ToString()),
u => u.CreateTime >= input.StartTime && u.CreateTime <= input.EndTime)
.OrderBy(u => u.CreateTime, OrderByType.Desc)
.Select().ToListAsync();
IExcelExporter excelExporter = new ExcelExporter();
var res = await excelExporter.ExportAsByteArray(logExList);
return new FileStreamResult(new MemoryStream(res), "application/octet-stream") { FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + "异常日志.xlsx" };
}
}