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

namespace CallCenterApi.Repositories
{
    public class Call_OutTaskRepository : BaseRepository<Call_OutTask, string>, ICall_OutTaskRepository
    {
        private readonly ILogger<BaseRepository<Call_OutTask, string>> _logger;
        //private readonly IMongoCollection<Call_OutTask>  _call_outtaskRepository;
        private readonly IMongoCollection<Call_OutTaskTelNum> _collection_call_outtasktelnum;
        private readonly IMongoCollection<Call_OutTaskFP> _collection_call_outtaskfp;
        private readonly IMongoCollection<MW_Autocall_Call_Record> _collection_mw_autocall_call_record;
        private readonly IMongoCollection<Call_OutTaskTelNumRecord> _collection_call_outtasktelnumrecord;
        
        public Call_OutTaskRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Call_OutTask, string>> logger) : base(settings, logger)
        {
            _logger = logger;
            //_call_outtaskRepository = _context.GetCollection<Call_OutTask>();// call_outtaskRepository;
            _collection_call_outtasktelnum = _context.GetCollection<Call_OutTaskTelNum>();

            _collection_call_outtaskfp = _context.GetCollection<Call_OutTaskFP>();
            _collection_mw_autocall_call_record = _context.GetCollection<MW_Autocall_Call_Record>();
            _collection_call_outtasktelnumrecord = _context.GetCollection<Call_OutTaskTelNumRecord>();
        }


        /// <summary>
        /// Linq 关联查询 分页
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="provincecode"></param>
        /// <param name="citycode"></param>
        /// <param name="projectid"></param>
        /// <param name="protype"></param>
        /// <param name="systemid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public IEnumerable<object> GetListsByPage(string taskid, int pageindex, int pagesize, out int recordCount)
        {

            //关联查询
            var query =
                from record in _collection_call_outtasktelnumrecord.AsQueryable()
                join cot in _collection.AsQueryable() on record.taskid equals cot.id into cotinfo
                //join role in _collection_sysroleinfo.AsQueryable() on usa.role_id equals role.id into roleinfo
                //where record.delete_flag == false && usa.type != 2
                //orderby usa.create_time descending
                select new
                {
                    record.id,
                    record.taskid ,
                    record.phone,
                    record.userid,
                    record.iscallout,
                    record.iscalloutsuccess,
                    taskname = cotinfo.Count() > 0 ? cotinfo.First().taskname : null,
                    tasktotal= cotinfo.Count() > 0 ? cotinfo.First().totalnum : 0,
                    successnum = cotinfo.Count() > 0 ? cotinfo.First().successnum : 0,
                    surplusnum = cotinfo.Count() > 0 ? cotinfo.First().surplusnum : 0,
                    createtime= cotinfo.Count() > 0 ? cotinfo.First().createtime : null,
                    deleteflag= cotinfo.Count() > 0 ? cotinfo.First().deleteflag : 0
                };
            #region 查询条件
            if (!string.IsNullOrEmpty(taskid))
                query = query.Where(s => s.taskid.Contains(taskid));
            //if (type != -1)
            //    query = query.Where(it => it.type.Equals(type));
            //if (!string.IsNullOrEmpty(deptid))
            //    query = query.Where(it => it.dept_id.Equals(deptid));
            //if (!string.IsNullOrEmpty(roleid))
            //    query = query.Where(it => it.role_id.Equals(roleid));
            //if (!string.IsNullOrEmpty(projectid))
            //    query = query.Where(it => it.projectlist.Contains(projectid));
            #endregion
            recordCount = query.Count();
            var list = query.Skip((pageindex - 1) * pagesize).Take(pagesize);

            return list;
        }

    }
}
