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 Knowledge_BaseRepository : BaseRepository<Knowledge_Base, string>, IKnowledge_BaseRepository
    {
        protected readonly ILogger<BaseRepository<Knowledge_Base, string>> _logger;
        protected readonly IMongoCollection<Knowledge_Type> _collection_knowledge_type;

        public Knowledge_BaseRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Knowledge_Base, string>> logger) : base(settings, logger)
        {
            _logger = logger;
            _collection_knowledge_type = _context.GetCollection<Knowledge_Type>();
        }

        /// <summary>
        /// Linq 关联查询 分页
        /// </summary>
        /// <param name="key"></param>
        /// <param name="typeid"></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 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_knowledge_type.AsQueryable() on p.typeid equals dic_type.id into dic_typeDefa
                        where p.isdelete == 0
                        select new
                        {
                            p.id,
                            p.typeid,
                            typename = dic_typeDefa != null && dic_typeDefa.Count() > 0 ? dic_typeDefa.First().name : null,
                            p.title,
                            p.content,
                            p.remark,
                            p.createtime,
                            p.createuser,
                            p.createusername
                        };

            #region 查询条件
            if (!string.IsNullOrEmpty(key))
                query = query.Where(it => it.title.Contains(key) || it.content.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_knowledge_type.AsQueryable() on p.typeid equals dic_type.id into dic_typeDefa
                        where p.id == id
                        select new
                        {
                            p.id,
                            p.typeid,
                            typename = dic_typeDefa != null && dic_typeDefa.Count() > 0 ? dic_typeDefa.First().name : null,
                            p.title,
                            p.content,
                            p.remark,
                            p.createtime,
                            p.createuser,
                            p.createusername
                        };
            var info = query.FirstOrDefault();
            return info;
        }
    }
}
