using CallCenterApi.IRepositories;
using MadRunFabric.Common;
using MadRunFabric.Common.Options;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using MadRunFabric.Model.CallCenterApi;
using MadRunFabric.Model;

namespace CallCenterApi.Repositories
{
    public class Ask_QuestionListRepository : BaseRepository<Ask_QuestionList, string>, IAsk_QuestionListRepository
    {
        protected readonly ILogger<BaseRepository<Ask_QuestionList, string>> _logger;
        //protected readonly IMongoCollection<mw_call_records> _collection_mw_call_records;
        protected readonly IMongoCollection<MW_Autocall_Call_Record> _collection_mw_autocall_call_record;
        protected readonly IMongoCollection<Sys_SystemConfig> _collection_sys_systemconfig;
        protected readonly IMongoCollection<Ask_QuestionPager> _collection_ask_questionpager;
        protected readonly IMongoCollection<Ask_Question> _collection_ask_question;
        protected readonly IMongoCollection<Ask_QuestionItems> _collection_ask_questionitems;
        public Ask_QuestionListRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Ask_QuestionList, string>> logger) : base(settings, logger)
        {
            _logger = logger;
            //_collection_mw_call_records = _context.GetCollection<mw_call_records>();
            _collection_mw_autocall_call_record = _context.GetCollection<MW_Autocall_Call_Record>();
            _collection_sys_systemconfig = _context.GetCollection<Sys_SystemConfig>();
            _collection_ask_questionpager = _context.GetCollection<Ask_QuestionPager>();
            _collection_ask_question = _context.GetCollection<Ask_Question>();
            _collection_ask_questionitems = _context.GetCollection<Ask_QuestionItems>();
        }

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="usercode"></param>
        /// <param name="calltype"></param>
        /// <param name="telephone"></param>
        /// <param name="checktype"></param>
        /// <param name="stime"></param>
        /// <param name="etime"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="recordcount"></param>
        /// <returns></returns>
        public IEnumerable<object> GetCallPageListsByPageAsync(string keyword, int calltype, int isqc_check, string callresultid, string stime, string etime, int pageindex, int pagesize, out int recordcount)
        {
            var config = from sysconfig in _collection_sys_systemconfig.AsQueryable()
                         where sysconfig.isdelete == false && sysconfig.paramcode.Equals("PlayPath")
                         select sysconfig;
            var configvalue = config != null && config.Count() > 0 ? config.First().paramvalue : null;

            var query = from p in _collection.AsQueryable()
                        join auto in _collection_mw_autocall_call_record.AsQueryable() on p.recordid equals auto.id into autocall_Defa
                        where p.isdelete == 0
                        select new
                        {
                            p.id,
                            p.recordid,
                            record_path = autocall_Defa != null && autocall_Defa.Count() > 0 ? autocall_Defa.First().record_path : null,
                            url = configvalue,
                            p.fullname,
                            p.mobile,
                            p.address,
                            p.pageid,
                            p.pagetitle,
                            p.pagecontent,
                            p.describe,
                            p.callresult,
                            p.taskid,
                            p.remark,
                            p.createuser,
                            p.createusername,
                            p.createtime,
                            p.isdelete
                        };

            #region 查询条件
            if (!string.IsNullOrEmpty(keyword))
                query = query.Where(it => it.mobile.Contains(keyword));
            if (!string.IsNullOrEmpty(callresultid))
                query = query.Where(it => it.callresult == callresultid);
            if (!string.IsNullOrEmpty(stime))
                query = query.Where(it => it.createtime >= Convert.ToDateTime(stime + " 00:00:00"));
            if (!string.IsNullOrEmpty(etime))
                query = query.Where(it => it.createtime <= Convert.ToDateTime(etime + " 23:59:59"));
            #endregion

            recordcount = query.Count();
            var list = query.Skip((pageindex - 1) * pagesize).Take(pagesize).ToList();

            return list;
        }
    }
}
