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_ClassGroupRepository : BaseRepository<Sys_Work_ClassGroup, string>, ISys_Work_ClassGroupRepository
    {
        protected readonly ILogger<BaseRepository<Sys_Work_ClassGroup, string>> _logger;
        protected readonly IMongoCollection<Pro_Project_Info> _collection_pro_project_info;
        protected readonly IMongoCollection<Sys_DictionaryValue> _collection_sys_dictionaryvalue;
        public Sys_Work_ClassGroupRepository(
            IOptions<MongodbOptions> settings,
            ILogger<BaseRepository<Sys_Work_ClassGroup, 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
                orderby p.starttime ascending
                select new
                {
                    p.id,
                    p.projectid,
                    projectname = projeDefa.Count() > 0 ? projeDefa.First().project_name : "",
                    p.usertype,
                    usertypename = dic_usertypeDefa.Count() > 0 ? dic_usertypeDefa.First().name : null,
                    p.code,
                    p.name,
                    p.starttime,
                    p.endtime,
                    p.remark,
                    p.createby,
                    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.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,
                usertype = p.usertype,
                usertypename = p.usertypename,
                code = p.code,
                name = p.name,
                starttime = p.starttime,
                endtime = p.endtime,
                orderbytime = DateTime.Parse(dayNow + " " + p.starttime),
                remark = p.remark,
                createby = p.createby,
                createtime = p.createtime,
                isdelete = p.isdelete
            }).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
                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.usertype,
                    usertypename = dic_usertypeDefa.Count() > 0 ? dic_usertypeDefa.First().name : null,
                    p.code,
                    p.name,
                    p.starttime,
                    p.endtime,
                    p.remark,
                    p.createby,
                    p.createtime,
                    p.isdelete
                };

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

    }
}
