using Ewide.Core.Extension;
using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Ewide.Core.Service
{
    /// <summary>
    /// 访问日志服务
    /// </summary>
    [ApiDescriptionSettings(Name = "VisLog", Order = 100)]
    public class SysVisLogService : ISysVisLogService, IDynamicApiController, ITransient
    {
        private readonly IRepository<SysLogVis> _sysVisLogRep;  // 访问日志表仓储 

        public SysVisLogService(IRepository<SysLogVis> sysVisLogRep)
        {
            _sysVisLogRep = sysVisLogRep;
        }

        /// <summary>
        /// 分页查询访问日志
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/sysVisLog/page")]
        public async Task<dynamic> QueryVisLogPageList([FromBody] VisLogInput input)
        {
            var name = !string.IsNullOrEmpty(input.Name?.Trim());
            var success = input.Success.HasValue;
            var searchBeginTime = !string.IsNullOrEmpty(input.SearchBeginTime?.Trim());
            var visLogs = await _sysVisLogRep.DetachedEntities
                                             .Where((name, u => EF.Functions.Like(u.Name, $"%{input.Name.Trim()}%")))
                                             .Where(input.VisType > 0, u => u.VisType == input.VisType)
                                             .Where(success, u => u.Success == input.Success.Value)
                                             .Where(searchBeginTime, u => u.VisTime >= DateTime.Parse(input.SearchBeginTime.Trim()) &&
                                                                          u.VisTime <= DateTime.Parse(input.SearchEndTime.Trim()))
                                             .ToPageData<SysLogVis, VisLogOutput>(input);
            return PageDataResult<VisLogOutput>.PageResult(visLogs);
        }

        /// <summary>
        /// 清空访问日志
        /// </summary>
        /// <returns></returns>
        [HttpPost("/sysVisLog/delete")]
        public async Task ClearVisLog()
        {
            var visLogs = await _sysVisLogRep.Entities.ToListAsync();
            visLogs.ForEach(u =>
            {
                u.Delete();
            });
        }
    }
}
