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

namespace CallCenterApi.Controllers.telbusiness
{
    [ApiVersion("6.0")]
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class MWAgentReposeRecordController : BaseController
    {
        public IActionResult Index()
        {
            return View();
        }

        private readonly ILogger<MWAgentReposeRecordController> _logger;
        private readonly IMW_Agent_Repose_RecordRepository _mw_agent_repose_recordRepository;

        public MWAgentReposeRecordController(ILogger<MWAgentReposeRecordController> logger, IMW_Agent_Repose_RecordRepository mw_agent_repose_recordRepository)
        {
            _logger = logger;
            _mw_agent_repose_recordRepository = mw_agent_repose_recordRepository;
        }

        [Authorize]
        [HttpGet("getall")]
        public async Task<IActionResult> GetAllListsAsync()
        {
            try
            {
                var recordlist = await _mw_agent_repose_recordRepository.GetAll();
                return Success("获取全部数据成功", recordlist);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取坐席小休全部数据异常");
                return Error("获取坐席小休全部数据失败");
            }
        }

        /// <summary>
        /// 获取列表 by page
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet("getlistbypage")]
        public async Task<IActionResult> GetListByPageAsync(string keyword, string stime, string etime, int pageindex = 1, int pagesize = 10)
        {
            #region 条件信息
            //排序字段
            var sort = Builders<MW_Agent_Repose_Record>.Sort.Descending("id");
            var list = await _mw_agent_repose_recordRepository.GetByPage(null, pageindex, pagesize, sort);
            var redCount = await _mw_agent_repose_recordRepository.CountAsync(null); //获取总数
            //根据条件查询集合
            var listfilter = new List<FilterDefinition<MW_Agent_Repose_Record>>();
            if (!string.IsNullOrWhiteSpace(stime))
            {
                DateTime dt2 = DateTime.Now.ToLocalTime();
                if (DateTime.TryParse(stime.Trim() + " 00:00:00", out dt2))
                    listfilter.Add(Builders<MW_Agent_Repose_Record>.Filter.Gt("time_begin", DateTimeConvert.GetTimeStampTen(dt2)));
            }
            if (!string.IsNullOrWhiteSpace(etime))
            {
                DateTime dt2 = DateTime.Now.ToLocalTime();
                if (DateTime.TryParse(etime.Trim() + " 23:59:59", out dt2))
                    listfilter.Add(Builders<MW_Agent_Repose_Record>.Filter.Lt("time_begin", DateTimeConvert.GetTimeStampTen(dt2)));
            }

            //模糊查询
            if (!string.IsNullOrEmpty(keyword))
                listfilter.Add(Builders<MW_Agent_Repose_Record>.Filter.Where(s => s.agent_id.Equals(keyword)));
            #endregion
            int recordCount = 0;
            if (listfilter.Count > 0)
            {
                var filter = Builders<MW_Agent_Repose_Record>.Filter.And(listfilter);
                list = await _mw_agent_repose_recordRepository.GetByPage(filter, pageindex, pagesize, sort);
                redCount = await _mw_agent_repose_recordRepository.CountAsync(filter);   //获取总数
            }
            recordCount = int.Parse(redCount.ToString());

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