using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using EquipmentApi.IRepositories;
using MadRunFabric.Model;
using MadRunFabric.Common.Options;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MongoDB.Bson;
using MongoDB.Driver;
using MadRunFabric.Common;
using System.Linq;
using EquipmentApi.Model.Dto;
using MadRunFabric.Common.Enums;

namespace EquipmentApi.Repositories
{
    public class Pro_Project_UserRepository : BaseRepository<Pro_Project_User, string>, IPro_Project_UserRepository
    {
        protected readonly ILogger<BaseRepository<Pro_Project_User, string>> _logger;
        protected readonly IMongoCollection<Pro_Project_Info> _collection_pro_project_info;
        public Pro_Project_UserRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Pro_Project_User, string>> logger) : base(settings, logger)
        {
            _collection_pro_project_info = _context.GetCollection<Pro_Project_Info>();
            _logger = logger;
        }

        /// <summary>
        /// 列表 -  Linq 关联查询 分页
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="stime"></param>
        /// <param name="etime"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public IEnumerable<object> GetListsByPage(string keyword, string projectid, string stime, string etime,int type, UserAccountInfoModel userinfo, 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 proDefa
                where p.isdelete == 0
                orderby p.createtime descending
                select new
                {
                    p.id,
                    p.type,
                    p.projectid,
                    projectname= proDefa != null && proDefa.Count() > 0 ? proDefa.First().project_name : null,
                    projectdelete = proDefa != null && proDefa.Count() > 0 ? proDefa.First().isdelete : 0,
                    p.name,
                    p.sex,
                    p.mobilephone,
                    p.telephone,
                    p.imgurl,
                    p.imgsmallurl,
                    p.department,
                    p.position,
                    p.remark,
                    p.createby,
                    p.createtime
                };
            #region 查询条件
            query = query.Where(it => it.projectdelete == 0);
            if (!string.IsNullOrEmpty(keyword))
                query = query.Where(it => it.name.Contains(keyword) || it.mobilephone.Contains(keyword) || it.telephone.Contains(keyword));
            if (!string.IsNullOrEmpty(projectid))
                query = query.Where(it => it.projectid == projectid);
            if (!string.IsNullOrEmpty(stime))
                query = query.Where(it => it.createtime >= Convert.ToDateTime(stime + " 00:00:00"));
            if (!string.IsNullOrEmpty(etime))
                query = query.Where(it => it.createtime <= Convert.ToDateTime(etime + " 23:59:59"));
            if (type > 0)
                query = query.Where(it => it.type == type);

            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>
        /// 列表 - Linq 关联查询
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="stime"></param>
        /// <param name="etime"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public IEnumerable<object> GetAppLists(string keyword, int type, UserAccountInfoModel userinfo)
        {
            var query =
                from p in _collection.AsQueryable()
                join pro in _collection_pro_project_info.AsQueryable() on p.projectid equals pro.id into proDefa
                where p.isdelete == 0
                orderby p.createtime descending
                select new
                {
                    p.id,
                    p.type,
                    p.projectid,
                    projectname = proDefa != null && proDefa.Count() > 0 ? proDefa.First().project_name : null,
                    projectdelete = proDefa != null && proDefa.Count() > 0 ? proDefa.First().isdelete : 0,
                    p.name,
                    p.sex,
                    p.mobilephone,
                    p.telephone,
                    p.imgurl,
                    p.imgsmallurl,
                    p.department,
                    p.position,
                    p.remark,
                    p.createby,
                    p.createtime
                };
            #region 查询条件
            query = query.Where(it => it.projectdelete == 0);
            if (!string.IsNullOrEmpty(keyword))
                query = query.Where(it => it.name.Contains(keyword) || it.mobilephone.Contains(keyword) || it.telephone.Contains(keyword));
            if (type > 0)
                query = query.Where(it => it.type == type);

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

            return query;
        }

        /// <summary>
        /// Linq 关联查询详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public object GetDetails(string id)
        {
            //关联查询
            var query =
                from p in _collection.AsQueryable()
                join pro in _collection_pro_project_info.AsQueryable() on p.projectid equals pro.id into proDefa
                where p.isdelete == 0 && p.id == id
                orderby p.createtime descending
                select new
                {
                    p.id,
                    p.type,
                    p.projectid,
                    projectname = proDefa != null && proDefa.Count() > 0 ? proDefa.First().project_name : null,
                    p.name,
                    p.sex,
                    p.mobilephone,
                    p.telephone,
                    p.imgurl,
                    p.imgsmallurl,
                    p.department,
                    p.position,
                    p.remark,
                    p.createby,
                    p.createtime
                };

            var info = query.FirstOrDefault();
            return info;
        }
    }
}