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

namespace SignTokenApi.Repositories
{
    public class Sys_Work_UserRepository : BaseRepository<Sys_Work_User, string>, ISys_Work_UserRepository
    {
        protected readonly ILogger<BaseRepository<Sys_Work_User, string>> _logger;
        protected readonly IMongoCollection<Pro_Project_Info> _collection_pro_project_info;
        protected readonly IMongoCollection<Sys_DictionaryValue> _collection_sys_dictionaryvalue;

        public Sys_Work_UserRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Sys_Work_User, string>> logger) : base(settings, logger)
        {
            _collection_pro_project_info = _context.GetCollection<Pro_Project_Info>();  //获取集合
            _collection_sys_dictionaryvalue = _context.GetCollection<Sys_DictionaryValue>();
            _logger = logger;
        }

        /// <summary>
        /// 获取分页
        /// </summary>
        /// <param name="projectid"></param>
        /// <param name="keyword"></param>
        /// <param name="userinfo"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="recordcount"></param>
        /// <returns></returns>
        public IEnumerable<object> GetListsByPage(string keyword, string projectid, UserInfoModel userinfo, int pageIndex, int pageSize, out int recordCount)
        {
            var query =
                from p in _collection.AsQueryable()
                join proje in _collection_pro_project_info.AsQueryable() on p.projectid equals proje.id into projeDefa
                join dic_protype in _collection_sys_dictionaryvalue.AsQueryable() on p.usertype equals dic_protype.id into dic_usertypeDefa
                where p.isdelete == 0
                select new
                {
                    p.id,
                    p.projectid,
                    projectname = projeDefa.Count() > 0 ? projeDefa.First().project_name : "",
                    p.mobile,
                    p.usertype,
                    usertypename = dic_usertypeDefa.Count() > 0 ? dic_usertypeDefa.First().name : null,
                    p.username,
                    p.sex,
                    p.remark,
                    p.createby,
                    p.createname,
                    p.createtime,
                    p.isdelete
                };


            #region 查询条件
            if (!string.IsNullOrEmpty(projectid))
                query = query.Where(it => it.projectid.Equals(projectid));
            if (!string.IsNullOrEmpty(keyword))
                query = query.Where(it => it.username.Contains(keyword) || it.username.Contains(keyword));

            if (userinfo.isallproject == 0)
                query = query.Where(it => userinfo.projectlist.Contains(it.projectid));
            #endregion

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

            return list;
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public object GetDetails(string id)
        {
            var query =
                from p in _collection.AsQueryable()
                join proje in _collection_pro_project_info.AsQueryable() on p.projectid equals proje.id into projeDefa
                join dic_protype in _collection_sys_dictionaryvalue.AsQueryable() on p.usertype equals dic_protype.id into dic_usertypeDefa
                where p.isdelete == 0 && p.id == id
                select new
                {
                    p.id,
                    p.projectid,
                    projectname = projeDefa.Count() > 0 ? projeDefa.First().project_name : "",
                    p.mobile,
                    p.usertype,
                    usertypename = dic_usertypeDefa.Count() > 0 ? dic_usertypeDefa.First().name : null,
                    p.username,
                    p.sex,
                    p.remark,
                    p.createby,
                    p.createname,
                    p.createtime,
                    p.isdelete
                };

            var model = query.FirstOrDefault();
            if (model != null)
            {
                return model;
            }
            return null;
        }



    }
}
