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

namespace OnLineChatApi.Repositories
{
    public class Chat_ProcessRepository : BaseRepository<Chat_Process, string>, IChat_ProcessRepository
    {
        protected readonly ILogger<BaseRepository<Chat_Process, string>> _logger;
        protected readonly IMongoCollection<Sys_DictionaryBase> _collection_sys_dictionarybase;
        protected readonly IMongoCollection<Sys_DictionaryValue> _collection_sys_dictionaryvalue;
        public Chat_ProcessRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Chat_Process, string>> logger) : base(settings, logger)
        {
            _logger = logger;
            _collection_sys_dictionarybase = _context.GetCollection<Sys_DictionaryBase>();
            _collection_sys_dictionaryvalue = _context.GetCollection<Sys_DictionaryValue>();
        }

        /// <summary>
        /// Linq 关联查询 分页
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="provincecode"></param>
        /// <param name="citycode"></param>
        /// <param name="projectid"></param>
        /// <param name="protype"></param>
        /// <param name="systemid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public IEnumerable<object> GetListsByPage(string keyword, int pageIndex, int pageSize, out int recordCount)
        {
            var query = from p in _collection.AsQueryable()
                        join dic_system in _collection_sys_dictionarybase.AsQueryable() on p.dictionarycode equals dic_system.dictionarycode into dic_systemDefa
                        where p.isdelete == 0
                        select new
                        {
                            p.id,
                            p.step,
                            p.content,
                            p.optiontype,
                            p.dictionarycode,
                            dictionaryname = dic_systemDefa.Count() > 0 ? dic_systemDefa.First().dictionaryname : "",
                            p.options,
                            p.createtime
                        };


            if (!string.IsNullOrEmpty(keyword))
                query = query.Where(it => it.content.Contains(keyword));

            recordCount = query.Count();
            var list = query.OrderByDescending(it => it.createtime).Skip((pageIndex - 1) * pageSize).Take(pageSize);

            return list;
        }

        /// <summary>
        /// Linq 关联查询详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public object GetDetails(string id)
        {
            var query = from p in _collection.AsQueryable()
                        join dic_system in _collection_sys_dictionarybase.AsQueryable() on p.dictionarycode equals dic_system.dictionarycode into dic_systemDefa
                        where p.isdelete == 0 && p.id== id
                        select new
                        {
                            p.id,
                            p.step,
                            p.content,
                            p.optiontype,
                            p.dictionarycode,
                            dictionaryname = dic_systemDefa.Count() > 0 ? dic_systemDefa.First().dictionaryname : "",
                            p.options,
                            p.createtime
                        };

            var model = query.FirstOrDefault();
            if (model != null)
            {
                var dictionaryvalues = (from p in _collection_sys_dictionaryvalue.AsQueryable()
                                        where p.dictionarycode == model.dictionarycode && p.statetype == true
                                        select new
                                        {
                                            p.id,
                                            p.name,
                                            p.sort
                                        }).OrderBy(p => p.sort).ToList();
                var obj = new
                {
                    model.id,
                    model.step,
                    model.content,
                    model.optiontype,
                    model.dictionarycode,
                    model.dictionaryname,
                    dictionaryvalues,
                    model.options,
                    model.createtime
                };
                return obj;
            }
            return null;
        }
    }
}
