using Furion;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.EventBus;
using Microsoft.AspNetCore.Mvc;
using Myshipping.Core.Entity;
using SqlSugar;
using System;
using System.Threading.Tasks;
namespace Myshipping.Core.Service;
///
/// 操作日志服务
///
[ApiDescriptionSettings(Name = "OpLog", Order = 100)]
public class SysOpLogService : ISysOpLogService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _sysOpLogRep; // 操作日志表仓储
public SysOpLogService(SqlSugarRepository sysOpLogRep)
{
_sysOpLogRep = sysOpLogRep;
}
///
/// 分页查询操作日志
///
///
///
[HttpGet("/sysOpLog/page")]
public async Task QueryOpLogPageList([FromQuery] OpLogInput input)
{
var query = _sysOpLogRep.AsQueryable().Filter(null, true);
if (input.BusinessId != null)
{
query.InnerJoin((o, b) => o.Id == b.LogId)
.Where((o, b) => b.BusinessId == input.BusinessId);
}
if (!string.IsNullOrWhiteSpace(input.Url))
{
input.Url = input.Url.Trim();
if (!input.Url.StartsWith('/'))
{
input.Url = $"/{input.Url}";
}
}
var result = await query.Where(o => o.Id > 0) // 如果没有Id > 0这个条件,SELECT Count(*) FROM `sys_log_op` 会执行很长时间,最后报错
.WhereIF(input.Success != null, o => o.Success == input.Success)
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), o => o.Name.Contains(input.Name.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Ip), o => o.Ip == input.Ip.Trim())
.WhereIF(!string.IsNullOrWhiteSpace(input.Url), o => o.Url == input.Url)
.WhereIF(!string.IsNullOrWhiteSpace(input.SearchBeginTime), o => o.OpTime >= DateTime.Parse(input.SearchBeginTime.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.SearchEndTime), o => o.OpTime <= DateTime.Parse(input.SearchEndTime.Trim()))
.OrderByIF(string.IsNullOrWhiteSpace(input.SortField) || string.Equals(input.SortField, "opTime", StringComparison.OrdinalIgnoreCase), o => o.Id, input.DescSort ? OrderByType.Desc : OrderByType.Asc)
//.OrderByIF(input.SortField == "name", o => o.Name, input.DescSort ? OrderByType.Desc : OrderByType.Asc)
//.OrderByIF(input.SortField == "ip", o => o.Ip, input.DescSort ? OrderByType.Desc : OrderByType.Asc)
.Select(o => new OpLogPageListDto()
{
Id = o.Id,
ReqMethod = o.ReqMethod,
Name = o.Name,
Success = o.Success,
Ip = o.Ip,
Url = o.Url,
OpTime = o.OpTime,
Account = o.Account
}).ToPagedListAsync(input.PageNo, input.PageSize);
return result.XnPagedResult();
}
///
/// 查询操作日志详情
///
/// 操作日志记录主键
[HttpGet("/sysOpLog/detail")]
public async Task QueryOpLogDetail([FromQuery] long id)
{
var result = await _sysOpLogRep.AsQueryable()
.Where(o => o.Id == id)
.Select(o => new OpLogDetailDto()
{
Id = o.Id,
Browser = o.Browser,
Os = o.Os,
Param = o.Param,
Result = o.Result
}).FirstAsync();
return result;
}
///
/// 清空操作日志
///
///
[HttpPost("/sysOpLog/delete")]
public async Task ClearOpLog()
{
await _sysOpLogRep.DeleteAsync(m => true);
}
[HttpPost("/sysOpLog/zipClear")]
public async Task ZipClear()
{
var evtPub = App.GetService();
await evtPub.PublishAsync(new ChannelEventSource("LogZipClear:DoWork"));
}
}