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

namespace EquipmentApi.Repositories
{
    public class Wo_Maintain_PlanRepository : BaseRepository<Wo_Maintain_Plan, string>, IWo_Maintain_PlanRepository
    {
        protected readonly ILogger<BaseRepository<Wo_Maintain_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_Maintain_PlanRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Wo_Maintain_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>();
            //_collection_sys_buliding = _context.GetCollection<Sys_Building>();
            //_collection_sys_floor = _context.GetCollection<Sys_Floor>();
            _logger = logger;
        }

        /// <summary>
        /// 获取保养计划列表 Linq 关联查询 分页
        /// </summary>
        /// <param name="proid"></param>
        /// <param name="sysnameid"></param>
        /// <param name="equipmentid"></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<Wo_Maintain_PlanDto> GetListsByPage(string keyword, string projectid, string stime, string etime, string nowusercode, UserAccountInfoModel userinfo, int pageindex, int pagesize, out int recordCount)
        {
            //关联查询
            var query =
                from maintainplan in _collection.AsQueryable()
                //join dic_pro in _collection_sys_dictionaryvalue.AsQueryable() on maintainplan.systemid equals dic_pro.id into dic_proDefa
                join proje in _collection_pro_project_info.AsQueryable() on maintainplan.projectid equals proje.id into projeDefa
                join hangfire in _collection_hangfireplan.AsQueryable() on maintainplan.hangfireid equals hangfire.id into hangfireDefa
                where maintainplan.isdelete == false
                orderby maintainplan.createtime descending
                select new Wo_Maintain_PlanDto
                {
                    id = maintainplan.id,
                    projectid = maintainplan.projectid,
                    projectname = projeDefa != null && projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
                    matterids = maintainplan.matterids,
                    //systemid = maintainplan.systemid,
                    //systemname = dic_proDefa != null && dic_proDefa.Count() > 0 ? dic_proDefa.First().name : null,
                    //equipmentid = maintainplan.equipmentid,
                    planname = maintainplan.planname,
                    plancontent = maintainplan.plancontent,
                    planstate = maintainplan.planstate,
                    planclaim = maintainplan.planclaim,
                    extensionlist=maintainplan.extensionlist,
                    maintaintype = maintainplan.maintaintype,
                    maintainfile = maintainplan.maintainfile,
                    plantype = maintainplan.plantype,
                    hangfireid = maintainplan.hangfireid,
                    startdate = maintainplan.startdate,
                    enddate = maintainplan.enddate,
                    createtime = maintainplan.createtime,
                    createby = maintainplan.createby,
                    isdelete = maintainplan.isdelete,
                    plancycle = hangfireDefa.First().plancycle,
                    planinterval = hangfireDefa.First().planinterval,
                    plantimes = hangfireDefa.First().plantimes,
                    extensiontime = hangfireDefa.First().extensiontime,
                };
            #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 id Linq 关联查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Wo_Maintain_PlanDto GetDetails(string id)
        {

            //关联查询
            var query =
                from maintainplan in _collection.AsQueryable()
                //join dic_pro in _collection_sys_dictionaryvalue.AsQueryable() on maintainplan.systemid equals dic_pro.id into dic_proDefa
                join proje in _collection_pro_project_info.AsQueryable() on maintainplan.projectid equals proje.id into projeDefa
                join hangfire in _collection_hangfireplan.AsQueryable() on maintainplan.hangfireid equals hangfire.id into hangfireDefa
                where maintainplan.isdelete == false && maintainplan.id == id
                orderby maintainplan.createtime descending
                select new Wo_Maintain_PlanDto
                {
                    id = maintainplan.id,
                    projectid = maintainplan.projectid,
                    projectname = projeDefa != null && projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
                    matterids = maintainplan.matterids,
                    //systemid = maintainplan.systemid,
                    //systemname = dic_proDefa != null && dic_proDefa.Count() > 0 ? dic_proDefa.First().name : null,
                    //equipmentid = maintainplan.equipmentid,
                    planname = maintainplan.planname,
                    plancontent = maintainplan.plancontent,
                    planstate = maintainplan.planstate,
                    planclaim = maintainplan.planclaim,
                    extensionlist = maintainplan.extensionlist,
                    maintaintype = maintainplan.maintaintype,
                    maintainfile = maintainplan.maintainfile,
                    plantype = maintainplan.plantype,
                    hangfireid = maintainplan.hangfireid,
                    startdate = maintainplan.startdate,
                    enddate = maintainplan.enddate,
                    createtime = maintainplan.createtime,
                    createby = maintainplan.createby,
                    isdelete = maintainplan.isdelete,
                    plancycle = hangfireDefa.First().plancycle,
                    planinterval = hangfireDefa.First().planinterval,
                    plantimes = hangfireDefa.First().plantimes,
                    extensiontime = hangfireDefa.First().extensiontime,
                };
            var model = query.FirstOrDefault();
            if (model != null)
            {
                return model;
            }
            return null;
        }
    }
}
