using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using MadRunFabric.Common;
using Microsoft.Extensions.Logging;
using MessageApi.IRepositories;
using MongoDB.Driver;
using MadRunFabric.Model;

namespace MessageApi.Controllers
{
    /// <summary>
    /// 会员登录日志 - 暂停
    /// </summary> 
    [ApiVersion("6.0")]
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class UserLoginLogsController : BaseController
    {
        private readonly ILogger<UserLoginLogsController> _logger;
        private readonly ISys_User_LoginLogsRepository _sys_user_loginlogsrepository;

        public UserLoginLogsController(
            ILogger<UserLoginLogsController> logger,
            ISys_User_LoginLogsRepository sys_user_loginlogsrepository
            )
        {
            _logger = logger;
            _sys_user_loginlogsrepository = sys_user_loginlogsrepository;
        }

        /// <summary>
        /// 获取登录日志列表 by page
        /// </summary>
        /// <param name="keyword">版本编号</param>
        /// <param name="createtime">添加开始时间</param>
        /// <param name="createtime">添加结束时间</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_User_LoginLogs>.Sort.Descending("logintime");
            var list = await _sys_user_loginlogsrepository.GetByPage(null, pageindex, pagesize, sort);
            var redCount = await _sys_user_loginlogsrepository.CountAsync(null); //获取总数
            //根据条件查询集合
            var listApp = new List<FilterDefinition<Sys_User_LoginLogs>>();
            listApp.Add(Builders<Sys_User_LoginLogs>.Filter.Eq("statetype", 0));
            if (!string.IsNullOrEmpty(stime))
                listApp.Add(Builders<Sys_User_LoginLogs>.Filter.Gt("logintime", stime));
            if (!string.IsNullOrEmpty(etime))
                listApp.Add(Builders<Sys_User_LoginLogs>.Filter.Lt("logintime", etime));
            //模糊查询
            if (!string.IsNullOrEmpty(keyword))
                listApp.Add(Builders<Sys_User_LoginLogs>.Filter.Where(s => s.result.Contains(keyword)));
            #endregion

            int recordCount = 0;
            if (listApp.Count > 0)
            {
                var filter = Builders<Sys_User_LoginLogs>.Filter.And(listApp);
                list = await _sys_user_loginlogsrepository.GetByPage(filter, pageindex, pagesize, sort);
                redCount = await _sys_user_loginlogsrepository.CountAsync(filter);   //获取总数
            }
            recordCount = int.Parse(redCount.ToString());

            var obj = new
            {
                state = "success",
                message = "成功",
                rows = list,
                total = recordCount,
            };
            return Content(obj.ToJson());
        }

        /// <summary>
        /// 获取登录日志详情 by id
        /// </summary>
        /// <param name="id">登录日志id</param>
        /// <returns></returns>
        public async Task<IActionResult> GetDetailsAsync(string id)
        {
            if (string.IsNullOrEmpty(id))
                return Error("参数错误");
            var model = await _sys_user_loginlogsrepository.GetSingle(id);
            if (model == null)
                return Error("获取失败");
            var result = new
            {
                id = model.id,

                loginid = model.loginid,
                loginname = model.loginname,
                result = model.result,
                loginip = model.loginip,
                loginhostname = model.loginhostname,
                logintime = model.logintime.ToLocalTime(),
                loginouttime=model.loginouttime,
                statetype=model.statetype,
                content=model.content,
            };
            return Success("获取成功", result);
        }

        /// <summary>
        /// 删除App版本信息 by ids
        /// </summary>
        /// <param name="ids">string[] id</param>
        /// <returns></returns>
        [HttpPost("delete")]
        public async Task<IActionResult> DeleteAsync(string[] ids)
        {
            int num = 0;
            if (ids == null && ids.Length <= 0)
                return Error("请选择要删除的记录");
            foreach (var item in ids)
            {
                var model = await _sys_user_loginlogsrepository.GetSingle(item);
                model.isdelete = -1;
                if (await _sys_user_loginlogsrepository.Update(model))
                    num += 1;
            }
            if (num == ids.Length)
                return Success("删除成功");
            if (num > 0 && num < ids.Length)
                return Error("部分删除失败，请查看后重新操作");
            return Error("删除失败，请查看后重新操作");
        }
        

    }
}