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

namespace ConfigurationApi.Repositories
{
    public class Sys_AuthorizationU3DRepository : BaseRepository<Sys_AuthorizationU3D, string>, ISys_AuthorizationU3DRepository
    {
        protected readonly ILogger<BaseRepository<Sys_AuthorizationU3D, string>> _logger;
        protected readonly IMongoCollection<Pro_Project_Info> _collection_pro_project_info;

        public Sys_AuthorizationU3DRepository(
            IOptions<MongodbOptions> settings,
            ILogger<BaseRepository<Sys_AuthorizationU3D, string>> logger) :
            base(settings, logger)
        {
            _logger = logger;
            _collection_pro_project_info = _context.GetCollection<Pro_Project_Info>();//获取集合
        }
        /// <summary>
        /// Linq 关联查询 分页
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="projectid"></param>
        /// <param name="isopen"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public IEnumerable<U3DDto> GetListsByPage(string keyword, string projectid, string stime, string etime, int isopen, int pageindex, int pagesize, out int recordCount)
        {
            var query =
                from p in _collection.AsQueryable()
                join pro in _collection_pro_project_info.AsQueryable() on p.projectid equals pro.id into projeDefa
                where p.isdelete == 0
                orderby p.createtime descending
                select new U3DDto
                {
                    id = p.id,
                    authorizationcode = p.authorizationcode,
                    projectid = p.projectid,
                    projectname = projeDefa.Count() > 0 ? projeDefa.First().project_name : "",
                    duedate = p.duedate,
                    //duedate = p.duedate.ToString("yyyy-MM-dd"),
                    //duedate_day = (DateTime.Parse(p.duedate.ToString()) - DateTime.Now).Days,    //剩余到期天数
                    ip = p.ip,
                    isopen = p.isopen,
                    createtime = p.createtime,
                    isdelete = p.isdelete,
                    remark = p.remark
                };

            #region 查询条件
            if (isopen > -1)
                query = query.Where(it => it.isopen == isopen);
            if (!string.IsNullOrEmpty(projectid))
                query = query.Where(it => it.projectid.Equals(projectid));
            if (!string.IsNullOrEmpty(keyword))
                query = query.Where(it => it.authorizationcode.Contains(keyword) || it.remark.Contains(keyword));
            if (!string.IsNullOrEmpty(stime))
                query = query.Where(it => it.duedate >= Convert.ToDateTime(stime + " 00:00:00"));
            if (!string.IsNullOrEmpty(etime))
                query = query.Where(it => it.duedate <= Convert.ToDateTime(etime + " 23:59:59"));
            #endregion

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

            return list;
        }
    }
}
