using EquipmentApi.IRepositories;
using MadRunFabric.Common;
using MadRunFabric.Common.Options;
using MadRunFabric.Model;
using MadRunFabric.Model.EquipmentApi.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 EquipmentApi.Model.Dto;
using MadRunFabric.Model.HangfireApi.Model;

namespace EquipmentApi.Repositories
{
    /// <summary>
    /// 设备运行计划
    /// </summary>
    public class Wo_EquipmentRunning_PlanRepository : BaseRepository<Wo_EquipmentRunning_Plan, string>, IWo_EquipmentRunning_PlanRepository
    {
        protected readonly ILogger<BaseRepository<Wo_EquipmentRunning_Plan, string>> _logger;
        protected readonly IMongoCollection<Pro_Project_Info> _collection_pro_project_info;
        protected readonly IMongoCollection<Eqp_Equipment_Info> _collection_equipment_info;
        protected readonly IMongoCollection<Sys_DictionaryValue> _collection_sys_dictionaryvalue;
        protected readonly IMongoCollection<HangfirePlan> _collection_hangfireplan;
        public Wo_EquipmentRunning_PlanRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Wo_EquipmentRunning_Plan, string>> logger) : base(settings, logger)
        {
            _collection_pro_project_info = _context.GetCollection<Pro_Project_Info>();
            _collection_equipment_info = _context.GetCollection<Eqp_Equipment_Info>();
            _collection_sys_dictionaryvalue = _context.GetCollection<Sys_DictionaryValue>();
            _collection_hangfireplan = _context.GetCollection<HangfirePlan>();
            _logger = logger;
        }

       
        /// <summary>
        ///获取设备运行列表 Linq 关联查询 分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IEnumerable<Wo_EquipmentRunning_PlanDto> GetListsByPage(string keyword, string projectid, string stime, string etime, string nowusercode, UserAccountInfoModel userinfo, int pageIndex, int pageSize, out int recordCount)
        {
            //关联查询
            var query =
                    from equplan in _collection.AsQueryable()
                    //join dic_pro in _collection_sys_dictionaryvalue.AsQueryable() on equplan.systemid equals dic_pro.id into dic_proDefa
                    join proje in _collection_pro_project_info.AsQueryable() on equplan.projectid equals proje.id into projeDefa
                    join hangfire in _collection_hangfireplan.AsQueryable() on equplan.hangfireid equals hangfire.id into hangfireDefa
                    where equplan.isdelete == 0
                    orderby equplan.createtime descending
                    select new Wo_EquipmentRunning_PlanDto
                    {
                        id = equplan.id,
                        projectid = equplan.projectid,
                        projectname = projeDefa != null && projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
                        planname = equplan.planname,
                        plancontent = equplan.plancontent,
                        planstate = equplan.planstate,
                        matterids = equplan.matterids,
                        //systemid = equplan.systemid,
                        //systemname = dic_proDefa != null && dic_proDefa.Count() > 0 ? dic_proDefa.First().name : null,
                        //equipmentid = equplan.equipmentid,
                        extensionlist = equplan.extensionlist,
                        hangfireid = equplan.hangfireid,
                        startdate = equplan.startdate,
                        enddate = equplan.enddate,
                        createtime = equplan.createtime,
                        createby = equplan.createby,
                        isdelete = equplan.isdelete,
                        plancycle = hangfireDefa.First().plancycle,
                        planinterval = hangfireDefa.First().planinterval,
                        plantimes = hangfireDefa.First().plantimes,
                        extensiontime = hangfireDefa.First().extensiontime,
                        files=equplan.files,
                        isreturnmsg = equplan.isreturnmsg
                    };
            #region 查询条件
            if (!string.IsNullOrEmpty(keyword))
                query = query.Where(it => it.planname.Contains(keyword));
            if (!string.IsNullOrEmpty(projectid))
                query = query.Where(it => it.projectid.Equals(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"));

            #region
            if (userinfo.type == 2)//查看自己创建的工单
            {
                query = query.Where(it => it.createby == userinfo.usercode);
            }
            else
            {
                if (userinfo.isallproject == 1)
                {
                }
                else if (userinfo.isallproject == 0)
                {
                    query = query.Where(it => userinfo.projectlist.Contains(it.projectid));
                    
                }
            }
            #endregion
            #endregion
            recordCount = query.Count();
            var list = query.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return list;
        }

        /// <summary>
        /// 获取设备运行列表 by page Linq 关联查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IEnumerable<Wo_EquipmentRunning_PlanDto> GetAppLists(string keyword, string projectid, string stime, string etime)
        {
            //关联查询
            var query =
                    from equplan in _collection.AsQueryable()
                    //join dic_pro in _collection_sys_dictionaryvalue.AsQueryable() on equplan.systemid equals dic_pro.id into dic_proDefa
                    join proje in _collection_pro_project_info.AsQueryable() on equplan.projectid equals proje.id into projeDefa
                    where equplan.isdelete == 0
                    orderby equplan.createtime descending
                    select new Wo_EquipmentRunning_PlanDto
                    {
                        id = equplan.id,
                        projectid = equplan.projectid,
                        projectname = projeDefa != null && projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
                        planname = equplan.planname,
                        plancontent = equplan.plancontent,
                        planstate = equplan.planstate,
                        matterids = equplan.matterids,
                        //systemid = equplan.systemid,
                        //systemname = dic_proDefa.Count() > 0 ? dic_proDefa.First().name : "",
                        //equipmentid = equplan.equipmentid,
                        extensionlist = equplan.extensionlist,
                        hangfireid = equplan.hangfireid,
                        startdate = equplan.startdate,
                        enddate = equplan.enddate,
                        createtime = equplan.createtime,
                        createby = equplan.createby,
                        isdelete = equplan.isdelete,
                        files = equplan.files,
                        isreturnmsg = equplan.isreturnmsg
                    };
            #region 查询条件
            if (!string.IsNullOrEmpty(keyword))
                query = query.Where(it => it.planname.Contains(keyword));
            if (!string.IsNullOrEmpty(projectid))
                query = query.Where(it => it.projectid.Equals(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"));
            #endregion
            var list = query;
            return list;
        }

        /// <summary>
        /// 获取设备运行详情 by id Linq 关联查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Wo_EquipmentRunning_PlanDto GetDetails(string id)
        {
            //关联查询
            var query =
                from equplan in _collection.AsQueryable()
                //join dic_pro in _collection_sys_dictionaryvalue.AsQueryable() on equplan.systemid equals dic_pro.id into dic_proDefa
                join proje in _collection_pro_project_info.AsQueryable() on equplan.projectid equals proje.id into projeDefa
                where equplan.isdelete == 0 && equplan.id == id
                orderby equplan.createtime descending
                select new Wo_EquipmentRunning_PlanDto
                {
                    id = equplan.id,
                    projectid = equplan.projectid,
                    projectname = projeDefa != null && projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
                    planname = equplan.planname,
                    plancontent = equplan.plancontent,
                    planstate = equplan.planstate,
                    matterids = equplan.matterids,
                    //systemid = equplan.systemid,
                    //systemname = dic_proDefa.Count() > 0 ? dic_proDefa.First().name : "",
                    //equipmentid = equplan.equipmentid,
                    extensionlist = equplan.extensionlist,
                    hangfireid = equplan.hangfireid,
                    startdate = equplan.startdate,
                    enddate = equplan.enddate,
                    createtime = equplan.createtime,
                    createby = equplan.createby,
                    isdelete = equplan.isdelete,
                    files = equplan.files,
                    isreturnmsg = equplan.isreturnmsg
                };
            query = query.Where(p => p.id == id);
            var model = query.FirstOrDefault();
            if (model != null)
            {
                return model;
            }
            return null;
        }
        #region
        ///// <summary>
        /////获取设备运行列表 Linq 关联查询 分页
        ///// </summary>
        ///// <param name="pageIndex"></param>
        ///// <param name="pageSize"></param>
        ///// <returns></returns>
        //public IEnumerable<Wo_EquipmentRunning_PlanDto> GetListsByPage(string keyword, string stime, string etime, int pageIndex, int pageSize, out int recordCount)
        //{
        //    //关联查询
        //    var query =
        //        from equplan in _collection.AsQueryable()
        //        //join dic_pro in _collection_sys_dictionaryvalue.AsQueryable() on equplan.sysnameid equals dic_pro.id into dic_proDefa
        //        join proje in _collection_pro_project_info.AsQueryable() on equplan.projectid equals proje.id into projeDefa
        //        //join dic_equ in _collection_equipment_info.AsQueryable() on equplan.equipmentid equals dic_equ.id into dic_equDefa
        //        where equplan.isdelete == 0
        //        orderby equplan.createtime descending
        //        select new Wo_EquipmentRunning_PlanDto
        //        {
        //            id = equplan.id,
        //            projectid = equplan.projectid,
        //            projectname = projeDefa != null && projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
        //            //sysnameid = equplan.sysnameid,
        //            //sysnamename = dic_proDefa != null && dic_proDefa.Count() > 0 ? dic_proDefa.First().name : null,
        //            equipmentid = equplan.equipmentid,
        //            //equipmentname = dic_equDefa != null && dic_equDefa.Count() > 0 ? dic_equDefa.First().equipment_name : null,
        //            planname = equplan.planname,
        //            files = equplan.files,
        //            plancontent = equplan.plancontent,
        //            expirytime = equplan.expirytime,
        //            advancelong = equplan.advancelong,
        //            planid = equplan.planid,
        //            plantype = equplan.plantype,
        //            plancycle = equplan.plancycle,
        //            plandate = equplan.plandate,
        //            planhour = equplan.planhour,
        //            planinterval = equplan.planinterval,
        //            planstate = equplan.planstate,
        //            //planstart = equplan.planstart,
        //            //planend = equplan.planend,
        //            createtime = equplan.createtime,
        //            createby = equplan.createby,
        //            isdelete = equplan.isdelete
        //        };
        //    #region 查询条件
        //    if (!string.IsNullOrEmpty(keyword))
        //        query = query.Where(it => it.planname.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>
        ///// 获取设备运行列表 by page Linq 关联查询
        ///// </summary>
        ///// <param name="pageIndex"></param>
        ///// <param name="pageSize"></param>
        ///// <returns></returns>
        //public IEnumerable<Wo_EquipmentRunning_PlanDto> GetAppLists(string keyword, string stime, string etime)
        //{
        //    //关联查询
        //    var query =
        //        from equplan in _collection.AsQueryable()
        //        //join dic_pro in _collection_sys_dictionaryvalue.AsQueryable() on equplan.sysnameid equals dic_pro.id into dic_proDefa
        //        join proje in _collection_pro_project_info.AsQueryable() on equplan.projectid equals proje.id into projeDefa
        //        //join dic_equ in _collection_equipment_info.AsQueryable() on equplan.equipmentid equals dic_equ.id into dic_equDefa
        //        where equplan.isdelete == 0
        //        orderby equplan.createtime descending
        //        select new Wo_EquipmentRunning_PlanDto
        //        {
        //            id = equplan.id,
        //            projectid = equplan.projectid,
        //            projectname = projeDefa != null && projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
        //            //sysnameid = equplan.sysnameid,
        //            //sysnamename = dic_proDefa != null && dic_proDefa.Count() > 0 ? dic_proDefa.First().name : null,
        //            equipmentid = equplan.equipmentid,
        //            //equipmentname = dic_equDefa != null && dic_equDefa.Count() > 0 ? dic_equDefa.First().equipment_name : null,
        //            planname = equplan.planname,
        //            files = equplan.files,
        //            plancontent = equplan.plancontent,
        //            expirytime = equplan.expirytime,
        //            advancelong = equplan.advancelong,
        //            planid = equplan.planid,
        //            plantype = equplan.plantype,
        //            plancycle = equplan.plancycle,
        //            plandate = equplan.plandate,
        //            planhour = equplan.planhour,
        //            planinterval = equplan.planinterval,
        //            planstate = equplan.planstate,
        //            //planstart = equplan.planstart,
        //            //planend = equplan.planend,
        //            createtime = equplan.createtime,
        //            createby = equplan.createby,
        //            isdelete = equplan.isdelete
        //        };
        //    #region 查询条件
        //    if (!string.IsNullOrEmpty(keyword))
        //        query = query.Where(it => it.planname.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
        //    var list = query;
        //    return list;
        //}

        ///// <summary>
        ///// 获取设备运行详情 by id Linq 关联查询
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //public Wo_EquipmentRunning_PlanDto GetDetails(string id)
        //{
        //    //关联查询
        //    var query =
        //        from equplan in _collection.AsQueryable()
        //        //join dic_pro in _collection_sys_dictionaryvalue.AsQueryable() on equplan.sysnameid equals dic_pro.id into dic_proDefa
        //        join proje in _collection_pro_project_info.AsQueryable() on equplan.projectid equals proje.id into projeDefa
        //        //join dic_equ in _collection_equipment_info.AsQueryable() on equplan.equipmentid equals dic_equ.id into dic_equDefa
        //        where equplan.isdelete == 0 && equplan.id == id
        //        orderby equplan.createtime descending
        //        select new Wo_EquipmentRunning_PlanDto
        //        {
        //            id = equplan.id,
        //            projectid = equplan.projectid,
        //            projectname = projeDefa != null && projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
        //            //sysnameid = equplan.sysnameid,
        //            //sysnamename = dic_proDefa != null && dic_proDefa.Count() > 0 ? dic_proDefa.First().name : null,
        //            equipmentid = equplan.equipmentid,
        //            //equipmentname = dic_equDefa != null && dic_equDefa.Count() > 0 ? dic_equDefa.First().equipment_name : null,
        //            planname = equplan.planname,
        //            files = equplan.files,
        //            expirytime=equplan.expirytime,
        //            advancelong=equplan.advancelong,
        //            plancontent = equplan.plancontent,
        //            planid = equplan.planid,
        //            plantype = equplan.plantype,
        //            plancycle = equplan.plancycle,
        //            plandate = equplan.plandate,
        //            planhour = equplan.planhour,
        //            planinterval = equplan.planinterval,
        //            planstate = equplan.planstate,
        //            //planstart = equplan.planstart,
        //            //planend = equplan.planend,
        //            createtime = equplan.createtime,
        //            createby = equplan.createby,
        //            isdelete = equplan.isdelete
        //        };
        //    var model = query.FirstOrDefault();
        //    if (model != null)
        //    {
        //        return model;
        //    }
        //    return null;
        //}
        #endregion
    }

}
