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 MWIVRCalloutRecordController : BaseController
    {
        public IActionResult Index()
        {
            return View();
        }

        private readonly ILogger<MWIVRCalloutRecordController> _logger;
        private readonly IMW_IVR_Callout_RecordRepository _mw_ivr_callout_recordRepository;
        private readonly ISys_SystemConfigRepository _sys_systemconfigRepository;

        public MWIVRCalloutRecordController(ILogger<MWIVRCalloutRecordController> logger, IMW_IVR_Callout_RecordRepository mw_ivr_callout_recordRepository, ISys_SystemConfigRepository sys_systemconfigRepository)
        {
            _logger = logger;
            _mw_ivr_callout_recordRepository = mw_ivr_callout_recordRepository;
            _sys_systemconfigRepository = sys_systemconfigRepository;
        }

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

        /// <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, int pageindex = 1, int pagesize = 10)
        {
            #region 条件信息
            //排序字段
            var sort = Builders<MW_IVR_Callout_Record>.Sort.Descending("id");
            var list = await _mw_ivr_callout_recordRepository.GetByPage(null, pageindex, pagesize, sort);
            var redCount = await _mw_ivr_callout_recordRepository.CountAsync(null); //获取总数
            //根据条件查询集合
            var listfilter = new List<FilterDefinition<MW_IVR_Callout_Record>>();
            //模糊查询
            if (!string.IsNullOrEmpty(keyword))
                listfilter.Add(Builders<MW_IVR_Callout_Record>.Filter.Where(s => s.caller.Contains(keyword)));
            #endregion
            int recordCount = 0;
            if (listfilter.Count > 0)
            {
                var filter = Builders<MW_IVR_Callout_Record>.Filter.And(listfilter);
                list = await _mw_ivr_callout_recordRepository.GetByPage(filter, pageindex, pagesize, sort);
                redCount = await _mw_ivr_callout_recordRepository.CountAsync(filter);   //获取总数
            }
            recordCount = int.Parse(redCount.ToString());
            
            var obj = new
            {
                rows = list,
                total = recordCount
                
            };
            return Success("成功", obj);
            //return Content(obj.ToJson());
        }
    }
}