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

namespace CallCenterApi.Repositories
{
    public class Ask_QuestionPagerRepository : BaseRepository<Ask_QuestionPager, string>, IAsk_QuestionPagerRepository
    {
        protected readonly ILogger<BaseRepository<Ask_QuestionPager, string>> _logger;
        //protected readonly IMongoCollection<mw_call_records> _collection_mw_call_records;
        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_QuestionPagerRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Ask_QuestionPager, string>> logger) : base(settings, logger)
        {
            _logger = logger;
            //_collection_mw_call_records = _context.GetCollection<mw_call_records>();
            _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> GetPageListsByPage(string keyword, string usercode, string stime, string etime, int pageindex, int pagesize, out int recordcount)
        {
            var query = from p in _collection_ask_questionpager.AsQueryable()
                        where p.isdelete == 0
                        select new
                        {
                            p.id,
                            p.title,
                            p.totalnum,
                            p.remark,
                            p.createtime,
                            p.createuser,
                            p.isopen
                        };

            #region 查询条件
            if (!string.IsNullOrEmpty(keyword))
                query = query.Where(it => it.title.Contains(keyword) || it.remark.Contains(keyword));
            if (!string.IsNullOrEmpty(usercode))
                query = query.Where(it => it.createuser == usercode);
            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;
        }



    }
}
