using YBEE.EQM.Core;

namespace YBEE.EQM.Application
{
    /// <summary>
    /// 日志管理服务
    /// </summary>
    public class SysLogService : ISysLogService, ITransient
    {
        private readonly IRepository _rep;  // 操作日志表仓储

        public SysLogService(IRepository rep)
        {
            _rep = rep;
        }

        #region 异常日志
        /// <summary>
        /// 分页查询异常日志
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        //[HttpGet("sysExLog/page")]
        public async Task<PageResult<SysLogExOutput>> QueryLogExPageList(SysLogExPageInput input)
        {
            var name = !string.IsNullOrEmpty(input.Name?.Trim());
            var className = !string.IsNullOrEmpty(input.ClassName?.Trim());
            var methodName = !string.IsNullOrEmpty(input.MethodName?.Trim());
            var exceptionMsg = !string.IsNullOrEmpty(input.ExceptionMsg?.Trim());
            var searchBeginTime = !string.IsNullOrEmpty(input.SearchBeginTime?.Trim());
            var exLogs = await _rep.Change<SysLogEx>().DetachedEntities
                                           .Where((name, u => EF.Functions.Like(u.Name, $"%{input.Name.Trim()}%")))
                                           .Where(className, u => u.ClassName == input.ClassName)
                                           .Where(methodName, u => u.MethodName == input.MethodName)
                                           .Where(exceptionMsg, u => EF.Functions.Like(u.ExceptionMsg, $"%{input.ExceptionMsg.Trim()}%"))
                                           .Where(searchBeginTime, u => u.ExceptionTime >= DateTime.Parse(input.SearchBeginTime.Trim()) &&
                                                                   u.ExceptionTime <= DateTime.Parse(input.SearchEndTime.Trim()))
                                           .OrderByDescending(u => u.Id)
                                           .ProjectToType<SysLogExOutput>()
                                           .ToADPagedListAsync(input.PageIndex, input.PageSize);
            return exLogs;
        }
        /// <summary>
        /// 清空异常日志
        /// </summary>
        /// <returns></returns>
        public async Task ClearLogEx()
        {
            var rep = _rep.Change<SysLogEx>();
            await rep.SqlNonQueryAsync("TRUNCATE TABLE sys_log_ex");
            await rep.SaveNowAsync();
        }
        #endregion

        #region 操作日志
        /// <summary>
        /// 分页查询操作日志
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageResult<SysLogOpOutput>> QueryLogOpPageList(SysLogOpPageInput input)
        {
            var name = !string.IsNullOrEmpty(input.Name?.Trim());
            var searchBeginTime = !string.IsNullOrEmpty(input.SearchBeginTime?.Trim());
            var searchEndTime = !string.IsNullOrEmpty(input.SearchEndTime?.Trim());
            var ReqMethod = !string.IsNullOrEmpty(input.ReqMethod?.Trim());
            var opLogs = await _rep.Change<SysLogOp>().DetachedEntities
                                           .Where((name, u => EF.Functions.Like(u.Name, $"%{input.Name.Trim()}%")))
                                           .Where(input.Success.HasValue, u => u.Success == input.Success)
                                           .Where(ReqMethod, u => u.ReqMethod == input.ReqMethod)
                                           .Where(searchBeginTime, u => u.OpTime >= DateTime.Parse(input.SearchBeginTime.Trim()))
                                           .Where(searchEndTime, u => u.OpTime <= DateTime.Parse(input.SearchEndTime.Trim()))
                                           .OrderByDescending(u => u.Id)
                                           .ProjectToType<SysLogOpOutput>()
                                           .ToADPagedListAsync(input.PageIndex, input.PageSize);
            return opLogs;
        }

        /// <summary>
        /// 清空操作日志
        /// </summary>
        /// <returns></returns>
        public async Task ClearLogOp()
        {
            var rep = _rep.Change<SysLogOp>();
            await rep.SqlNonQueryAsync("TRUNCATE TABLE sys_log_op");
            await rep.SaveNowAsync();
        }
        #endregion

        #region 访问日志
        /// <summary>
        /// 分页查询访问日志
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageResult<SysLogVisOutput>> QueryLogVisPageList(SysLogVisPageInput input)
        {
            var name = !string.IsNullOrEmpty(input.Name?.Trim());
            var searchBeginTime = !string.IsNullOrEmpty(input.SearchBeginTime?.Trim());
            var visLogs = await _rep.Change<SysLogVis>().DetachedEntities
                                             .Where((name, u => EF.Functions.Like(u.Name, $"%{input.Name.Trim()}%")))
                                             .Where(input.VisType >= 0, u => u.VisType == input.VisType)
                                             .Where(input.Success.HasValue, u => u.Success == input.Success)
                                             .Where(searchBeginTime, u => u.VisTime >= DateTime.Parse(input.SearchBeginTime.Trim()) &&
                                                                     u.VisTime <= DateTime.Parse(input.SearchEndTime.Trim()))
                                             .OrderByDescending(u => u.Id)
                                             .ProjectToType<SysLogVisOutput>()
                                             .ToADPagedListAsync(input.PageIndex, input.PageSize);
            return visLogs;
        }

        /// <summary>
        /// 清空访问日志
        /// </summary>
        /// <returns></returns>
        public async Task ClearLogVis()
        {
            var rep = _rep.Change<SysLogVis>();
            await rep.SqlNonQueryAsync("TRUNCATE TABLE sys_log_vis");
            await rep.SaveNowAsync();
        }
        #endregion
    }
}