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

namespace CallCenterApi.Repositories
{
    public class Sys_IVRConfigsRepository : BaseRepository<Sys_IVRConfigs, string>, ISys_IVRConfigsRepository
    {
        protected readonly ILogger<BaseRepository<Sys_IVRConfigs, string>> _logger;
        protected readonly IMongoCollection<Sys_SeatGroup> _collection_sys_seatgroup;
        protected readonly IMongoCollection<Sys_User_Account> _collection_sys_user_account;
        public Sys_IVRConfigsRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Sys_IVRConfigs, 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 confinfo in _collection.AsQueryable()
                join groupinfo in _collection_sys_seatgroup.AsQueryable() on confinfo.groupcode equals groupinfo.zxzcode into groupDefa
                join users in _collection_sys_user_account.AsQueryable() on confinfo.createby equals users.usercode into createuserDefa
                where confinfo.isdelete == false && confinfo.id == id
                orderby confinfo.createtime descending
                select new
                {
                    confinfo.id,
                    confinfo.groupcode,
                    groupname = groupDefa != null && groupDefa.Count() > 0 ? groupDefa.First().zxzname : null,
                    confinfo.ivrname,
                    confinfo.ivrtype,
                    confinfo.ivrcontent,
                    confinfo.createtime,
                    confinfo.createby,
                    createbyuser = createuserDefa != null && createuserDefa.Count() > 0 ? createuserDefa.First().username : null,
                    confinfo.isdelete,
                    confinfo.isenable,
                };
            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> GetListsByPage(string groupcode, string stime, string etime, int pageIndex, int pageSize, out int recordCount)
        {
            var query =
                from confinfo in _collection.AsQueryable()
                join groupinfo in _collection_sys_seatgroup.AsQueryable() on confinfo.groupcode equals groupinfo.zxzcode into groupDefa
                join users in _collection_sys_user_account.AsQueryable() on confinfo.createby equals users.usercode into createuserDefa
                where confinfo.isdelete == false
                orderby confinfo.createtime descending
                select new 
                {
                    confinfo.id,
                    confinfo.groupcode,
                    groupname= groupDefa != null && groupDefa.Count() > 0 ? groupDefa.First().zxzname : null,
                    confinfo.ivrname,
                    confinfo.ivrtype,
                    confinfo.ivrcontent,
                    confinfo.createtime,
                    confinfo.createby,
                    createbyuser = createuserDefa != null && createuserDefa.Count() > 0 ? createuserDefa.First().username : null,
                    confinfo.isdelete,
                    confinfo.isenable,
                };
            #region 查询条件
            if (!string.IsNullOrEmpty(groupcode))
                query = query.Where(it => it.groupcode.Equals(groupcode));
            if (!string.IsNullOrWhiteSpace(stime))
            {
                DateTime dt2 = DateTime.Now;
                if (DateTime.TryParse(stime.Trim() + " 00:00:00", out dt2))
                    query = query.Where(it => it.createtime >= dt2);
            }
            if (!string.IsNullOrWhiteSpace(etime))
            {
                DateTime dt2 = DateTime.Now;
                if (DateTime.TryParse(etime.Trim() + " 23:59:59", out dt2))
                    query = query.Where(it => it.createtime <= dt2);
            }
            #endregion
            recordCount = query.Count();
            var list = query.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return list;
        }
    }
}
