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

namespace CallCenterApi.Repositories
{
    public class Sys_IVRWordsRepository : BaseRepository<Sys_IVRWords, string>, ISys_IVRWordsRepository
    {
        protected readonly ILogger<BaseRepository<Sys_IVRWords, string>> _logger;
        protected readonly IMongoCollection<Sys_SeatGroup> _collection_sys_seatgroup;
        protected readonly IMongoCollection<Sys_User_Account> _collection_sys_user_account;
        public Sys_IVRWordsRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Sys_IVRWords, string>> logger) : base(settings, logger)
        {
            _collection_sys_seatgroup = _context.GetCollection<Sys_SeatGroup>();
            _collection_sys_user_account = _context.GetCollection<Sys_User_Account>();
            _logger = logger;
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public object GetDetails(string id)
        {
            var query =
                from ivrwords in _collection.AsQueryable()
                join groupinfo in _collection_sys_seatgroup.AsQueryable() on ivrwords.groupcode equals groupinfo.zxzcode into groupDefa
                join users in _collection_sys_user_account.AsQueryable() on ivrwords.createby equals users.usercode into createuserDefa
                where ivrwords.isdelete == false && ivrwords.id == id
                orderby ivrwords.createtime descending
                select new
                {
                    ivrwords.id,
                    ivrwords.title,
                    ivrwords.groupcode,
                    groupname = groupDefa != null && groupDefa.Count() > 0 ? groupDefa.First().zxzname : null,
                    ivrwords.content,
                    ivrwords.type,
                    ivrwords.wavfile,
                    ivrwords.startdate,
                    ivrwords.enddate,
                    ivrwords.remark,
                    ivrwords.isstate,
                    ivrwords.createtime,
                    ivrwords.createby,
                    createbyuser = createuserDefa != null && createuserDefa.Count() > 0 ? createuserDefa.First().username : null,
                    ivrwords.isdelete,
                };
            query = query.Where(p => p.id == id);
            var model = query.FirstOrDefault();
            if (model != null)
            {
                return model;
            }
            return null;
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="groupcode"></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> GetListByPage(string key, string state, string stime, string etime,int pageIndex, int pageSize, out int recordCount)
        {
            var query =
                from ivrwords in _collection.AsQueryable()
                join groupinfo in _collection_sys_seatgroup.AsQueryable() on ivrwords.groupcode equals groupinfo.zxzcode into groupDefa
                join users in _collection_sys_user_account.AsQueryable() on ivrwords.createby equals users.usercode into createuserDefa
                where ivrwords.isdelete == false
                orderby ivrwords.createtime descending
                select new
                {
                    ivrwords.id,
                    ivrwords.title,
                    ivrwords.groupcode,
                    groupname = groupDefa != null && groupDefa.Count() > 0 ? groupDefa.First().zxzname : null,
                    ivrwords.content,
                    ivrwords.type,
                    ivrwords.wavfile,
                    ivrwords.startdate,
                    ivrwords.enddate,
                    ivrwords.remark,
                    ivrwords.isstate,
                    ivrwords.createtime,
                    ivrwords.createby,
                    createbyuser = createuserDefa != null && createuserDefa.Count() > 0 ? createuserDefa.First().username : null,
                    ivrwords.isdelete,
                };
            #region 查询条件

            if (!string.IsNullOrEmpty(key))
                query = query.Where(it => it.content.Equals(key) || it.title.Contains(key));
            

            if (!string.IsNullOrWhiteSpace(stime) || !string.IsNullOrWhiteSpace(etime))
            {
                DateTime dt1 = DateTime.Now;
                DateTime.TryParse(stime, out dt1);
                DateTime dt2 = DateTime.Now;
                DateTime.TryParse(etime, out dt2);

                query = query.Where(it => (it.startdate <= dt1 && it.enddate <= dt1) || it.startdate <= dt2 && it.enddate <= dt2);
            }
            //状态：1启动，0不启动
            if (!string.IsNullOrWhiteSpace(state))                
                query = query.Where(it => it.isstate.Equals(state));            

            #endregion
            recordCount = query.Count();
            var list = query.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return list;
        }


    }
}
