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

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

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

        private readonly ILogger<MWExtCallinRecordController> _logger;
        private readonly IMW_Ext_Callin_RecordRepository _mw_ext_callin_recordRepository;
        private readonly ISys_SystemConfigRepository _sys_systemconfigRepository;

        public MWExtCallinRecordController(ILogger<MWExtCallinRecordController> logger, IMW_Ext_Callin_RecordRepository mw_ext_callin_recordRepository, ISys_SystemConfigRepository sys_systemconfigRepository)
        {
            _logger = logger;
            _mw_ext_callin_recordRepository = mw_ext_callin_recordRepository;
            _sys_systemconfigRepository = sys_systemconfigRepository;
        }

        [Authorize]
        [HttpGet("getall")]
        public async Task<IActionResult> GetAllListsAsync()
        {
            try
            {
                var recordlist = await _mw_ext_callin_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, int pageindex = 1, int pagesize = 10)
        {
            #region 条件信息
            //排序字段
            var sort = Builders<MW_Ext_Callin_Record>.Sort.Descending("id");
            var list = await _mw_ext_callin_recordRepository.GetByPage(null, pageindex, pagesize, sort);
            var redCount = await _mw_ext_callin_recordRepository.CountAsync(null); //获取总数
            //根据条件查询集合
            var listfilter = new List<FilterDefinition<MW_Ext_Callin_Record>>();
            //模糊查询
            if (!string.IsNullOrEmpty(keyword))
                listfilter.Add(Builders<MW_Ext_Callin_Record>.Filter.Where(s => s.caller.Contains(keyword)));
            #endregion
            int recordCount = 0;
            if (listfilter.Count > 0)
            {
                var filter = Builders<MW_Ext_Callin_Record>.Filter.And(listfilter);
                list = await _mw_ext_callin_recordRepository.GetByPage(filter, pageindex, pagesize, sort);
                redCount = await _mw_ext_callin_recordRepository.CountAsync(filter);   //获取总数
            }
            recordCount = int.Parse(redCount.ToString());
            //2018-7-21 获取录音文件路径
            #region 获取路径

            var dModel = await _sys_systemconfigRepository.GetSingle(s => s.paramcode.Equals("PlayPath") & s.isdelete == false, null);
            string recordpath = dModel.paramvalue;

            #endregion

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