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

namespace SignTokenApi.Repositories
{
    public class Sys_ClassGroupRepository : BaseRepository<Sys_ClassGroup, string>, ISys_ClassGroupRepository
    {
        protected readonly ILogger<BaseRepository<Sys_ClassGroup, string>> _logger;
        protected readonly IMongoCollection<Pro_Project_Info> _collection_pro_project_info;
        public Sys_ClassGroupRepository(
            IOptions<MongodbOptions> settings, 
            ILogger<BaseRepository<Sys_ClassGroup, string>> logger) : base(settings, logger)
        {
            _collection_pro_project_info = _context.GetCollection<Pro_Project_Info>();  //获取集合
            _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
                where p.isdelete == 0
                orderby p.starttime ascending
                select new
                {
                    p.id,
                    p.projectid,
                    projectname = projeDefa.Count() > 0 ? projeDefa.First().project_name : "",
                    p.code,
                    p.name,
                    p.starttime,
                    p.endtime,
                    p.roleList,
                    p.remark,
                    p.createby,
                    p.createtime,
                    p.isdelete,
                    p.iswork
                };


            #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

            //排班排序
            DateTime dtNow = DateTime.Now;
            string dayNow = dtNow.ToString("yyyy-MM-dd");
            var lists = query.ToList();
            var result = lists.Select(p => new
            {
                id = p.id,
                projectid = p.projectid,
                projectname = p.projectname,
                code = p.code,
                name = p.name,
                starttime=p.starttime,
                endtime=p.endtime,
                orderbytime = DateTime.Parse(dayNow + " " + p.starttime),
                roleList = p.roleList,
                remark = p.remark,
                createby = p.createby,
                createtime = p.createtime,
                isdelete = p.isdelete,
                iswork = p.iswork
            }).ToList();
            recordCount = result.Count();
            var list = result.OrderBy(it => it.orderbytime).Skip((pageIndex - 1) * pageSize).Take(pageSize);

            //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.roleList,
                    p.remark,
                    p.createby,
                    p.createtime,
                    p.isdelete,
                    p.iswork
                };

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