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

namespace SignTokenApi.Repositories
{
    public class Sys_Work_GroupRepository : BaseRepository<Sys_Work_Group, string>, ISys_Work_GroupRepository
    {
        protected readonly ILogger<BaseRepository<Sys_Work_Group, string>> _logger;
        protected readonly IMongoCollection<Pro_Project_Info> _collection_pro_project_info;
        public Sys_Work_GroupRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Sys_Work_Group, string>> logger) : base(settings, logger)
        {
            _logger = logger;
            _collection_pro_project_info = _context.GetCollection<Pro_Project_Info>();
        }

        /// <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 projectid, string keyword, 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
                where p.isdelete == 0
                select new
                {
                    p.id,
                    p.projectid,
                    projectname = projeDefa.Count() > 0 ? projeDefa.First().project_name : "",
                    p.code,
                    p.name,
                    p.starttime,
                    p.endtime,
                    p.remark,
                    p.createby,
                    p.createtime
                };


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

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

            recordcount = query.Count();
            var list = query.OrderByDescending(it => it.createtime).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
                where p.isdelete == 0 && p.id== id
                select new
                {
                    p.id,
                    p.projectid,
                    projectname = projeDefa.Count() > 0 ? projeDefa.First().project_name : "",
                    p.code,
                    p.name,
                    p.starttime,
                    p.endtime,
                    p.remark,
                    p.createby,
                    p.createtime
                };

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