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 QC_CheckScoreRepository : BaseRepository<QC_CheckScore, string>, IQC_CheckScoreRepository
    {
        protected readonly ILogger<BaseRepository<QC_CheckScore, string>> _logger;
        protected readonly IMongoCollection<QC_CheckType> _collection_qc_checktype;

        public QC_CheckScoreRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<QC_CheckScore, string>> logger) : base(settings, logger)
        {
            _logger = logger;
            _collection_qc_checktype = _context.GetCollection<QC_CheckType>();
        }

        /// <summary>
        /// Linq 关联查询
        /// </summary>
        /// <param name="key"></param>
        /// <param name="typeid"></param>
        /// <returns></returns>
        public IEnumerable<object> GetList(string key, string typeid)
        {
            var query = from p in _collection.AsQueryable()
                        join dic_type in _collection_qc_checktype.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.score,
                            p.sort,
                            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);

            #endregion

            return query;
        }

        /// <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_qc_checktype.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.score,
                            p.sort,
                            p.remark,
                            p.createtime,
                            p.createuser,
                            p.createusername
                        };
            var info = query.FirstOrDefault();
            return info;
        }
    }
}
