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

namespace DistributionApi.Repositories
{
    public class Distri_Workorder_EvaluateRepository : BaseRepository<Distri_Workorder_Evaluate, string>, IDistri_Workorder_EvaluateRepository
    {
        protected readonly ILogger<BaseRepository<Distri_Workorder_Evaluate, string>> _logger;
        protected readonly IMongoCollection<Distri_Workorder> _collection_distri_workorder;
        protected readonly IMongoCollection<Distri_Workorder_Evaluate> _collection_distri_workorder_evaluate;
        protected readonly IMongoCollection<Pro_Project_Info> _collection_pro_project_info;
        public Distri_Workorder_EvaluateRepository(IOptions<MongodbOptions> settings, 
            ILogger<BaseRepository<Distri_Workorder_Evaluate, string>> logger) : base(settings, logger)
        {
            _collection_distri_workorder = _context.GetCollection<Distri_Workorder>();  //获取集合
            _collection_distri_workorder_evaluate = _context.GetCollection<Distri_Workorder_Evaluate>();  //获取集合
            _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, UserAccountInfoModel userinfo,string usercode,string stime,string etime, int pageIndex, int pageSize, out int recordCount)
        {
            //关联查询
            var query =
                 from evaluate in _collection.AsQueryable()
                 join proje in _collection_pro_project_info.AsQueryable() on evaluate.projectid equals proje.id into proje_Defa
                 where evaluate.isdelete == 0
                 orderby evaluate.createtime descending
                 select new
                 {
                     evaluate.id,
                     evaluate.woid,
                     evaluate.wocode,
                     evaluate.channel,
                     evaluate.starnum,
                     evaluate.evaluate,
                     evaluate.label,
                     evaluate.files,
                     evaluate.projectid,
                     projectname = proje_Defa != null && proje_Defa.Count() > 0 ? proje_Defa.First().project_name : null,
                     evaluate.createuser,
                     evaluate.createusername,
                     evaluate.createtime
                 };
            #region 查询条件
            if (!string.IsNullOrEmpty(projectid))
                query = query.Where(it => it.projectid == projectid);
            if (userinfo.isallproject == 0)
                query = query.Where(it => userinfo.projectlist.Contains(it.projectid));
            if (!string.IsNullOrEmpty(keyword))
                query = query.Where(it => it.evaluate == keyword || it.wocode == keyword || it.label.Contains(keyword));
            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"));
            #endregion

            recordCount = query.Count();
            var list = query.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return list;
        }        

        /// <summary>
        /// Linq 关联查询详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public object GetDetails(string id)
        {
            //关联查询
            var query =
                 from evaluate in _collection.AsQueryable()
                 join proje in _collection_pro_project_info.AsQueryable() on evaluate.projectid equals proje.id into proje_Defa
                 where evaluate.isdelete == 0 && evaluate.id == id
                 orderby evaluate.createtime descending
                 select new
                 {
                     evaluate.id,
                     evaluate.woid,
                     evaluate.wocode,
                     evaluate.channel,
                     evaluate.starnum,
                     evaluate.evaluate,
                     evaluate.label,
                     evaluate.files,
                     evaluate.projectid,
                     projectname = proje_Defa != null && proje_Defa.Count() > 0 ? proje_Defa.First().project_name : null,
                     evaluate.createuser,
                     evaluate.createusername,
                     evaluate.createtime
                 };
            var model = query.FirstOrDefault();
            if (model != null)
            {
                return model;
            }
            return null;
        }

        #region 我的工单评价
        /// <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> GetMyListsByPage(string keyword, string projectid, UserAccountInfoModel userinfo, string usercode, string stime, string etime, int pageIndex, int pageSize, out int recordCount)
        {
            //关联查询
            var query =
                 from evaluate in _collection.AsQueryable()
                 join diswo in _collection_distri_workorder.AsQueryable() on evaluate.woid equals diswo.id into diswo_Defa
                 join proje in _collection_pro_project_info.AsQueryable() on evaluate.projectid equals proje.id into proje_Defa
                 where evaluate.isdelete == 0
                 orderby evaluate.createtime descending
                 select new
                 {
                     evaluate.id,
                     evaluate.woid,
                     evaluate.wocode,
                     evaluate.channel,
                     evaluate.starnum,
                     evaluate.evaluate,
                     evaluate.label,
                     evaluate.files,
                     touser = diswo_Defa != null && diswo_Defa.Count() > 0 ? diswo_Defa.First().touser : null,
                     evaluate.projectid,
                     projectname = proje_Defa != null && proje_Defa.Count() > 0 ? proje_Defa.First().project_name : null,
                     evaluate.createuser,
                     evaluate.createusername,
                     evaluate.createtime
                 };
            #region 查询条件
            if (!string.IsNullOrEmpty(projectid))
                query = query.Where(it => it.projectid == projectid);
            if (userinfo.isallproject == 0)
                query = query.Where(it => userinfo.projectlist.Contains(it.projectid));
            if (!string.IsNullOrEmpty(keyword))
                query = query.Where(it => it.evaluate == keyword || it.wocode == keyword || it.label.Contains(keyword));
            if (!string.IsNullOrEmpty(usercode))
                query = query.Where(it => it.touser.Contains(usercode));
            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"));
            #endregion

            recordCount = query.Count();
            var list = query.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return list;
        }
        #endregion
    }
}
