using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization;
using Api.SignToken;
using MadRunFabric.Common;
using System.Threading;
using System.Security.Claims;
using SignTokenApi.IRepositories;
using MongoDB.Driver;
using MongoDB.Bson;
using MadRunFabric.Model;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace SignTokenApi.Controllers
{
    [Authorize]
    [Route("api/[controller]")]
    public class LogController : BaseController
    {
        private readonly ISys_Login_LogsRepository _sys_login_logsRepository;
        private readonly ILogger<PermissionController> _logger;

        public LogController(ISys_Login_LogsRepository sys_login_logsRepository,ILogger<PermissionController> logger)
        {
            _sys_login_logsRepository = sys_login_logsRepository;
            _logger = logger;
        }

        [AllowAnonymous]
        [HttpGet("getlogs")]
        public IActionResult GetLogs()
        {
            var logs = _sys_login_logsRepository.GetLogs();
            return Success("获取成功", logs);
        }

        /// <summary>
        /// 查詢登录记录
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="stime"></param>
        /// <param name="etime"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet("getlistbypage")]
        public async Task<IActionResult> GetListsByPageAsync(string keyword, string stime, string etime, int pageindex = 1, int pagesize = 10)
        {
            #region 条件信息
            ////排序字段
            var sort = Builders<Sys_Login_Logs>.Sort.Descending("time");
            var list = await _sys_login_logsRepository.GetByPage(null, pageindex, pagesize, sort);
            var redCount = await _sys_login_logsRepository.CountAsync(null); //获取总数
            //根据条件查询集合
            var listApp = new List<FilterDefinition<Sys_Login_Logs>>();
            listApp.Add(Builders<Sys_Login_Logs>.Filter.Eq("state", 1));
            if (!string.IsNullOrEmpty(stime))
                listApp.Add(Builders<Sys_Login_Logs>.Filter.Gt("time", stime));
            if (!string.IsNullOrEmpty(etime))
                listApp.Add(Builders<Sys_Login_Logs>.Filter.Lt("time", etime));
            //模糊查询
            if (!string.IsNullOrEmpty(keyword))
                listApp.Add(Builders<Sys_Login_Logs>.Filter.Where(s => s.username.Contains(keyword) || s.usercode.Contains(keyword)));
            #endregion
            int recordCount = 0;
            if (listApp.Count > 0)
            {
                var filter = Builders<Sys_Login_Logs>.Filter.And(listApp);
                list = await _sys_login_logsRepository.GetByPage(filter, pageindex, pagesize, sort);
                redCount = await _sys_login_logsRepository.CountAsync(filter);   //获取总数
            }
            recordCount = int.Parse(redCount.ToString());

            var obj = new
            {
                rows = list,
                total = recordCount
            };
            return Success("成功", obj);
        }
    }
}
