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

namespace CallCenterApi.Repositories
{
    public class Cus_User_BaseRepository : BaseRepository<Cus_User_Base, string>, ICus_User_BaseRepository
    {
        protected readonly ILogger<BaseRepository<Cus_User_Base, string>> _logger;
        protected readonly IMongoCollection<Sys_Provinces> _collection_sys_provinces;
        protected readonly IMongoCollection<Sys_City> _collection_sys_city;
        protected readonly IMongoCollection<Sys_DictionaryValue> _collection_sys_dictionaryvalue;
        protected readonly IMongoCollection<Sys_Department> _collection_sys_department;

        public Cus_User_BaseRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Cus_User_Base, string>> logger) : base(settings, logger)
        {
            _logger = logger;
            _collection_sys_provinces = _context.GetCollection<Sys_Provinces>();
            _collection_sys_city = _context.GetCollection<Sys_City>();
            _collection_sys_dictionaryvalue = _context.GetCollection<Sys_DictionaryValue>();
        }

        /// <summary>
        /// Linq 关联查询 分页
        /// </summary>
        /// <param name="ordercode"></param>
        /// <param name="key"></param>
        /// <param name="phone"></param>
        /// <param name="province"></param>
        /// <param name="city"></param>
        /// <param name="sourceid"></param>
        /// <param name="typeid"></param>
        /// <param name="deptid"></param>
        /// <param name="stime"></param>
        /// <param name="etime"></param>
        /// <param name="state"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public IEnumerable<object> GetListsByPage(string key, string typeid, string stime, string etime, int pageindex, int pagesize, out int recordCount)
        {
            var query = from p in _collection.AsQueryable()
                        join dic_type in _collection_sys_dictionaryvalue.AsQueryable() on p.typeid equals dic_type.id into dic_typeDefa
                        join sysprovinces in _collection_sys_provinces.AsQueryable() on p.province equals sysprovinces.provincecode into sys_provincesDefa
                        join syscity in _collection_sys_city.AsQueryable() on p.city equals syscity.citycode into sys_cityDefa
                        where p.isdelete == 0
                        select new
                        {
                            p.id,
                            p.typeid,
                            typename = dic_typeDefa != null && dic_typeDefa.Count() > 0 ? dic_typeDefa.First().name : null,
                            p.name,
                            p.mobilephone,
                            p.telephone,
                            p.sex,
                            p.province,
                            provincename = sys_provincesDefa != null && sys_provincesDefa.Count() > 0 ? sys_provincesDefa.First().provincename : null,
                            p.city,
                            cityname = sys_cityDefa != null && sys_cityDefa.Count() > 0 ? sys_cityDefa.First().cityname : null,
                            p.address,
                            p.qq,
                            p.email,
                            p.companyname,
                            p.companyphone,
                            p.companyposition,
                            p.createtime,
                            p.createuser,
                            p.createusername
                        };

            #region 查询条件
            if (!string.IsNullOrEmpty(key))
                query = query.Where(it => it.name.Contains(key) || it.mobilephone.Contains(key) || it.telephone.Contains(key));
            if (!string.IsNullOrEmpty(typeid))
                query = query.Where(it => it.typeid == typeid);

            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;
        }

        /// <summary>
        /// 详情 - 获取设备详情 by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public object GetDetails(string id)
        {
            var query = from p in _collection.AsQueryable()
                        join dic_type in _collection_sys_dictionaryvalue.AsQueryable() on p.typeid equals dic_type.id into dic_typeDefa
                        join sysprovinces in _collection_sys_provinces.AsQueryable() on p.province equals sysprovinces.provincecode into sys_provincesDefa
                        join syscity in _collection_sys_city.AsQueryable() on p.city equals syscity.citycode into sys_cityDefa
                        where p.id == id
                        select new
                        {
                            p.id,
                            p.typeid,
                            typename = dic_typeDefa != null && dic_typeDefa.Count() > 0 ? dic_typeDefa.First().name : null,
                            p.name,
                            p.mobilephone,
                            p.telephone,
                            p.sex,
                            p.province,
                            provincename = sys_provincesDefa != null && sys_provincesDefa.Count() > 0 ? sys_provincesDefa.First().provincename : null,
                            p.city,
                            cityname = sys_cityDefa != null && sys_cityDefa.Count() > 0 ? sys_cityDefa.First().cityname : null,
                            p.address,
                            p.qq,
                            p.email,
                            p.companyname,
                            p.companyphone,
                            p.companyposition,
                            p.createtime,
                            p.createuser,
                            p.createusername
                        };
            var info = query.FirstOrDefault();
            return info;
        }
    }
}
