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

namespace EquipmentApi.Repositories
{
    /// <summary>
    /// 设备运行
    /// </summary>
    public class Wo_EquipmentRunningRepository : BaseRepository<Wo_EquipmentRunning, string>, IWo_EquipmentRunningRepository
    {
        protected readonly ILogger<BaseRepository<Wo_EquipmentRunning, 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<Sys_Building> _collection_sys_buliding;
        protected readonly IMongoCollection<Sys_Floor> _collection_sys_floor;
        protected readonly IMongoCollection<Wo_EquipmentRunning_Plan> _collection_wo_equipmentrunning_plan;
        protected readonly IMongoCollection<Sys_User_Account> _collection_sys_user_account;
        public Wo_EquipmentRunningRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Wo_EquipmentRunning, 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_sys_buliding = _context.GetCollection<Sys_Building>();
            _collection_sys_floor = _context.GetCollection<Sys_Floor>();
            _collection_wo_equipmentrunning_plan = _context.GetCollection<Wo_EquipmentRunning_Plan>();
            _collection_sys_user_account = _context.GetCollection<Sys_User_Account>();
            _logger = logger;
        }

        /// <summary>
        ///获取设备运行列表 Linq 关联查询 分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IEnumerable<Wo_EquipmentRunningDto> GetListsByPage(string projectid, string matterid, string wocode, string checkby, string daytype, string stime, string etime, int state, string nowusercode, UserAccountInfoModel userinfo, int pageIndex, int pageSize, out int recordCount)
        {
            //关联查询
            var query =
                from eqrun in _collection.AsQueryable()
                //join dic_pro in _collection_sys_dictionaryvalue.AsQueryable() on eqrun.systemid equals dic_pro.id into dic_proDefa
                join plan in _collection_wo_equipmentrunning_plan.AsQueryable() on eqrun.planid equals plan.id into planDefa
                join proje in _collection_pro_project_info.AsQueryable() on eqrun.projectid equals proje.id into projeDefa
                join users in _collection_sys_user_account.AsQueryable() on eqrun.createby equals users.usercode into createuserDefa
                join users in _collection_sys_user_account.AsQueryable() on eqrun.checkby equals users.usercode into checkuserDefa
                join users in _collection_sys_user_account.AsQueryable() on eqrun.dealby equals users.usercode into dealuserDefa
                where eqrun.isdelete == 0
                orderby eqrun.createtime descending
                select new Wo_EquipmentRunningDto
                {
                    id = eqrun.id,
                    wocode = eqrun.wocode,
                    woname = eqrun.woname,
                    wocontent = eqrun.wocontent,
                    projectid = eqrun.projectid,
                    projectname = projeDefa != null && projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
                    matterids = eqrun.matterids,
                    //systemid = eqrun.systemid,
                    //systemname = dic_proDefa != null && dic_proDefa.Count() > 0 ? dic_proDefa.First().name : null,
                    //equipmentid = eqrun.equipmentid,
                    planid = eqrun.planid,
                    planname = planDefa != null && planDefa.Count() > 0 ? planDefa.First().planname : null,
                    lastchecktime = eqrun.lastchecktime,
                    lastcheckresultid = eqrun.lastcheckresultid,
                    starttime=eqrun.starttime,
                    endtime = eqrun.endtime,
                    createby = eqrun.createby,
                    createbyuser = createuserDefa != null && createuserDefa.Count() > 0 ? createuserDefa.Where(c=>c.delete_flag==false).First().username : null,
                    createtime = eqrun.createtime,
                    isdelete = eqrun.isdelete,
                    checkby = eqrun.checkby,
                    checkbyuser = checkuserDefa != null && checkuserDefa.Count() > 0 ? checkuserDefa.Where(c => c.delete_flag == false).First().username : null,
                    quipmenttype = eqrun.quipmenttype,
                    checkcontent = eqrun.checkcontent,
                    checkpic = eqrun.checkpic,
                    extensionlist = eqrun.extensionlist,
                    wostate = eqrun.wostate,
                    dealby = eqrun.dealby,
                    dealbyuser = dealuserDefa != null && dealuserDefa.Count() > 0 ? dealuserDefa.Where(c => c.delete_flag == false).First().username : null,
                    dealtime = eqrun.dealtime,
                    isreturnmsg = eqrun.isreturnmsg
                };
            #region 查询条件
            if (!string.IsNullOrEmpty(projectid))
                query = query.Where(it => it.projectid == projectid);
            //if (!string.IsNullOrEmpty(sysnameid))
            //    query = query.Where(it => it.systemid == sysnameid);
            //if (!string.IsNullOrEmpty(equipmentid))
            //    query = query.Where(it => it.equipmentid!=null & string.Join(",", it.equipmentid).Contains(equipmentid));
            if (!string.IsNullOrEmpty(wocode))
                query = query.Where(it => it.wocode.Contains(wocode));
            //if (!string.IsNullOrEmpty(checkby))
            //    query = query.Where(it => it.checkby == checkby);
            if (!string.IsNullOrEmpty(daytype))
            {
                var datenow = DateTime.Now;
                var datestart = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
                var dateend = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));
                switch (daytype)
                {
                    case "week":
                        datestart = datenow.AddDays(1 - Convert.ToInt32(datenow.DayOfWeek.ToString("d")));//本周周一
                        dateend = datestart.AddDays(6);//本周周日
                        break;
                    case "month":
                        datestart = datenow.AddDays(1 - datenow.Day);  //本月月初  
                        dateend = datestart.AddMonths(1).AddDays(-1);  //本月月末  
                        break;
                    case "year":
                        datestart = new DateTime(datenow.Year, 1, 1);  //本年年初  
                        dateend = new DateTime(datenow.Year, 12, 31);  //本年年末  
                        break;
                    default:
                        break;
                }
                query = query.Where(it => it.createtime >= datestart);
                query = query.Where(it => it.createtime <= dateend);
            }
            if (state >= 0)
            {
                switch (state)
                {
                    case 10:
                        //未指派
                        query = query.Where(it => it.wostate == (int)EnumEquipmentRunningState.create);
                        break;
                    case 11:
                        //未处理
                        query = query.Where(it => it.wostate < (int)EnumEquipmentRunningState.deal & it.wostate > (int)EnumEquipmentRunningState.create);
                        break;
                    case 12:
                        //已处理
                        query = query.Where(it => it.wostate == (int)EnumEquipmentRunningState.deal);
                        break;
                    case 13:
                        //已挂起
                        query = query.Where(it => it.wostate == (int)EnumEquipmentRunningState.hangup);
                        break;
                    default:
                        query = query.Where(it => it.wostate == state);
                        break;
                }
            }
            if (!string.IsNullOrWhiteSpace(stime))
            {
                DateTime dt2 = DateTime.Now;
                if (DateTime.TryParse(stime.Trim() + " 00:00:00", out dt2))
                    query = query.Where(it => it.createtime >= dt2);
            }
            if (!string.IsNullOrWhiteSpace(etime))
            {
                DateTime dt2 = DateTime.Now;
                if (DateTime.TryParse(etime.Trim() + " 23:59:59", out dt2))
                    query = query.Where(it => it.createtime <= dt2);
            }
            #region
            if (userinfo.type == 2)//查看自己创建的工单
            {
                query = query.Where(it => it.createby == userinfo.usercode);
            }
            else
            {
                if (userinfo.isallproject == 1)
                {
                    if (!string.IsNullOrEmpty(checkby))
                        query = query.Where(it => (it.checkby.Equals(checkby) || it.dealby.Equals(checkby)));
                }
                else if (userinfo.isallproject == 0)
                {
                    query = query.Where(it => userinfo.projectlist.Contains(it.projectid));

                    if (userinfo.role_code == "YWYG")
                    {
                        query = query.Where(it => (it.checkby.Count() == 0 || it.checkby.Equals(nowusercode) || it.dealby.Equals(nowusercode)));
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(checkby))
                            query = query.Where(it => (it.checkby.Equals(checkby) || it.dealby.Equals(checkby)));
                    }
                }
            }
            //if (string.IsNullOrWhiteSpace(rolecode))
            //{
            //    //运维员工只能看到自己的工单
            //    //且只能看到当天及当天之前的工单
            //    if (rolecode.Equals("YWYG"))
            //    {
            //        query = query.Where(it => it.starttime <= DateTime.Now && it.endtime >= DateTime.Now);
            //        query = query.Where(it => it.checkby == usercode);
            //    }
            //}
            #endregion
            #endregion

            //var list = query.ToList();
            //var listnew = new List<Wo_EquipmentRunningDto>();
            //if (!string.IsNullOrEmpty(matterid))
            //{
            //    foreach (var item in list)
            //    {
            //        if (item.matterids != null)
            //        {
            //            var eqid = string.Join(",", item.matterids);
            //            if (!string.IsNullOrWhiteSpace(eqid))
            //            {
            //                if (eqid.Contains(matterid))
            //                {
            //                    listnew.Add(item);
            //                }
            //            }
            //        }
            //    }
            //}else
            //{
            //    listnew = list;
            //}
            //recordCount = listnew.Count();
            //var listNN = listnew.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            //return listNN;

            recordCount = query.Count();

            var list = query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var listnew = new List<Wo_EquipmentRunningDto>();
            if (!string.IsNullOrEmpty(matterid))
            {
                foreach (var item in list)
                {
                    if (item.matterids != null)
                    {
                        var eqid = string.Join(",", item.matterids);
                        if (!string.IsNullOrWhiteSpace(eqid))
                        {
                            if (eqid.Contains(matterid))
                            {
                                listnew.Add(item);
                            }
                        }
                    }
                }
            }
            else
            {
                listnew = list;
            }

            return listnew;
        }

        public IEnumerable<Wo_EquipmentRunningDto> GetLists()
        {
            //关联查询
            var query =
                from eqrun in _collection.AsQueryable()
                //join dic_pro in _collection_sys_dictionaryvalue.AsQueryable() on eqrun.systemid equals dic_pro.id into dic_proDefa
                join plan in _collection_wo_equipmentrunning_plan.AsQueryable() on eqrun.planid equals plan.id into planDefa
                join proje in _collection_pro_project_info.AsQueryable() on eqrun.projectid equals proje.id into projeDefa
                join users in _collection_sys_user_account.AsQueryable() on eqrun.createby equals users.usercode into createuserDefa
                join users in _collection_sys_user_account.AsQueryable() on eqrun.checkby equals users.usercode into checkuserDefa
                join users in _collection_sys_user_account.AsQueryable() on eqrun.dealby equals users.usercode into dealuserDefa
                where eqrun.isdelete == 0
                orderby eqrun.createtime descending
                select new Wo_EquipmentRunningDto
                {
                    id = eqrun.id,
                    wocode = eqrun.wocode,
                    woname = eqrun.woname,
                    wocontent = eqrun.wocontent,
                    projectid = eqrun.projectid,
                    projectname = projeDefa != null && projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
                    matterids = eqrun.matterids,
                    //systemid = eqrun.systemid,
                    //systemname = dic_proDefa != null && dic_proDefa.Count() > 0 ? dic_proDefa.First().name : null,
                    //equipmentid = eqrun.equipmentid,
                    planid = eqrun.planid,
                    planname = planDefa != null && planDefa.Count() > 0 ? planDefa.First().planname : null,
                    lastchecktime = eqrun.lastchecktime,
                    lastcheckresultid = eqrun.lastcheckresultid,
                    starttime = eqrun.starttime,
                    endtime = eqrun.endtime,
                    createby = eqrun.createby,
                    createbyuser = createuserDefa != null && createuserDefa.Count() > 0 ? createuserDefa.Where(c => c.delete_flag == false).First().username : null,
                    createtime = eqrun.createtime,
                    isdelete = eqrun.isdelete,
                    checkby = eqrun.checkby,
                    checkbyuser = checkuserDefa != null && checkuserDefa.Count() > 0 ? checkuserDefa.Where(c => c.delete_flag == false).First().username : null,
                    quipmenttype = eqrun.quipmenttype,
                    checkcontent = eqrun.checkcontent,
                    checkpic = eqrun.checkpic,
                    extensionlist = eqrun.extensionlist,
                    wostate = eqrun.wostate,
                    dealby = eqrun.dealby,
                    dealbyuser = dealuserDefa != null && dealuserDefa.Count() > 0 ? dealuserDefa.Where(c => c.delete_flag == false).First().username : null,
                    dealtime = eqrun.dealtime
                };
            var list = query;
            return list;
        }

        /// <summary>
        /// 获取设备运行列表 by page Linq 关联查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IEnumerable<Wo_EquipmentRunningDto> GetAppLists(string projectid, string matterid, string checkby, string daytype, int state, string stime, string etime, string rolecode, string usercode)
        {
            //关联查询
            var query =
                from eqrun in _collection.AsQueryable()
                    //join dic_pro in _collection_sys_dictionaryvalue.AsQueryable() on eqrun.systemid equals dic_pro.id into dic_proDefa
                join plan in _collection_wo_equipmentrunning_plan.AsQueryable() on eqrun.planid equals plan.id into planDefa
                join proje in _collection_pro_project_info.AsQueryable() on eqrun.projectid equals proje.id into projeDefa
                join users in _collection_sys_user_account.AsQueryable() on eqrun.createby equals users.usercode into createuserDefa
                join users in _collection_sys_user_account.AsQueryable() on eqrun.checkby equals users.usercode into checkuserDefa
                join users in _collection_sys_user_account.AsQueryable() on eqrun.dealby equals users.usercode into dealuserDefa
                where eqrun.isdelete == 0
                orderby eqrun.createtime descending
                select new Wo_EquipmentRunningDto
                {
                    id = eqrun.id,
                    wocode = eqrun.wocode,
                    woname = eqrun.woname,
                    wocontent = eqrun.wocontent,
                    projectid = eqrun.projectid,
                    projectname = projeDefa != null && projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
                    matterids = eqrun.matterids,
                    //systemid = eqrun.systemid,
                    //systemname = dic_proDefa != null && dic_proDefa.Count() > 0 ? dic_proDefa.First().name : null,
                    //equipmentid = eqrun.equipmentid,
                    planid = eqrun.planid,
                    planname = planDefa != null && planDefa.Count() > 0 ? planDefa.First().planname : null,
                    lastchecktime = eqrun.lastchecktime,
                    lastcheckresultid = eqrun.lastcheckresultid,
                    starttime = eqrun.starttime,
                    endtime = eqrun.endtime,
                    createby = eqrun.createby,
                    createbyuser = createuserDefa != null && createuserDefa.Count() > 0 ? createuserDefa.Where(c => c.delete_flag == false).First().username : null,
                    createtime = eqrun.createtime,
                    isdelete = eqrun.isdelete,
                    checkby = eqrun.checkby,
                    checkbyuser = checkuserDefa != null && checkuserDefa.Count() > 0 ? checkuserDefa.Where(c => c.delete_flag == false).First().username : null,
                    quipmenttype = eqrun.quipmenttype,
                    checkcontent = eqrun.checkcontent,
                    checkpic = eqrun.checkpic,
                    extensionlist = eqrun.extensionlist,
                    wostate = eqrun.wostate,
                    dealby = eqrun.dealby,
                    dealbyuser = dealuserDefa != null && dealuserDefa.Count() > 0 ? dealuserDefa.Where(c => c.delete_flag == false).First().username : null,
                    dealtime = eqrun.dealtime
                };
            #region 查询条件
            if (!string.IsNullOrEmpty(projectid))
                query = query.Where(it => it.projectid == projectid);
            //if (!string.IsNullOrEmpty(sysnameid))
            //    query = query.Where(it => it.systemid == sysnameid);
            //if (!string.IsNullOrEmpty(equipmentid))
            //    query = query.Where(it => it.equipmentid != null & string.Join(",", it.equipmentid).Contains(equipmentid));
            if (!string.IsNullOrEmpty(checkby))
                query = query.Where(it => it.checkby == checkby);
            if (!string.IsNullOrEmpty(daytype))
            {
                var datenow = DateTime.Now;
                var datestart = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
                var dateend = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));
                switch (daytype)
                {
                    case "week":
                        datestart = datenow.AddDays(1 - Convert.ToInt32(datenow.DayOfWeek.ToString("d")));//本周周一
                        dateend = datestart.AddDays(6);//本周周日
                        break;
                    case "month":
                        datestart = datenow.AddDays(1 - datenow.Day);  //本月月初  
                        dateend = datestart.AddMonths(1).AddDays(-1);  //本月月末  
                        break;
                    case "year":
                        datestart = new DateTime(datenow.Year, 1, 1);  //本年年初  
                        dateend = new DateTime(datenow.Year, 12, 31);  //本年年末  
                        break;
                    default:
                        break;
                }
                query = query.Where(it => it.createtime >= datestart);
                query = query.Where(it => it.createtime <= dateend);
            }
            if (state >= 0)
            {
                switch (state)
                {
                    case 11:
                        //未完成
                        query = query.Where(it => it.wostate == (int)EnumEquipmentRunningState.create);
                        break;
                    case 12:
                        //已完成
                        query = query.Where(it => it.wostate == (int)EnumEquipmentRunningState.deal);
                        break;
                    case 13:
                        //已挂起
                        query = query.Where(it => it.wostate == (int)EnumEquipmentRunningState.hangup);
                        break;
                    default:
                        query = query.Where(it => it.wostate == state);
                        break;
                }
            }
            if (!string.IsNullOrWhiteSpace(stime))
            {
                DateTime dt2 = DateTime.Now;
                if (DateTime.TryParse(stime.Trim() + " 00:00:00", out dt2))
                    query = query.Where(it => it.createtime >= dt2);
            }
            if (!string.IsNullOrWhiteSpace(etime))
            {
                DateTime dt2 = DateTime.Now;
                if (DateTime.TryParse(etime.Trim() + " 23:59:59", out dt2))
                    query = query.Where(it => it.createtime <= dt2);
            }
            if (string.IsNullOrWhiteSpace(rolecode))
            {
                //运维员工只能看到自己的工单
                //且只能看到当天及当天之前的工单
                if (rolecode.Equals("YWYG"))
                {
                    query = query.Where(it => it.starttime <= DateTime.Now && it.endtime >= DateTime.Now);
                    query = query.Where(it => it.checkby == usercode);
                }
            }
            #endregion
            var list = query.ToList();
            var listnew = new List<Wo_EquipmentRunningDto>();
            if (!string.IsNullOrEmpty(matterid))
            {
                foreach (var item in list)
                {
                    var eqid = string.Join(",", item.matterids);
                    if (string.IsNullOrWhiteSpace(eqid))
                    {
                        if (eqid.Contains(matterid))
                        {
                            listnew.Add(item);
                        }
                    }
                }
            }
            else
            {
                listnew = list;
            }
            
            return listnew;
        }

        /// <summary>
        /// 获取设备运行详情 by id Linq 关联查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Wo_EquipmentRunningDto GetDetails(string id)
        {
            var queryequipment = from equipment in _collection_equipment_info.AsQueryable() select equipment;
            var querybulid = from bulidin in _collection_sys_buliding.AsQueryable() select bulidin;
            var queryfloor = from floor in _collection_sys_floor.AsQueryable() select floor;
            //关联查询
            var query =
                from eqrun in _collection.AsQueryable()
                //join dic_pro in _collection_sys_dictionaryvalue.AsQueryable() on eqrun.systemid equals dic_pro.id into dic_proDefa
                join plan in _collection_wo_equipmentrunning_plan.AsQueryable() on eqrun.planid equals plan.id into planDefa
                join proje in _collection_pro_project_info.AsQueryable() on eqrun.projectid equals proje.id into projeDefa
                join users in _collection_sys_user_account.AsQueryable() on eqrun.createby equals users.usercode into createuserDefa
                join users in _collection_sys_user_account.AsQueryable() on eqrun.checkby equals users.usercode into checkuserDefa
                join users in _collection_sys_user_account.AsQueryable() on eqrun.dealby equals users.usercode into dealuserDefa
                where eqrun.isdelete == 0 && eqrun.id == id
                orderby eqrun.createtime descending
                select new Wo_EquipmentRunningDto
                {
                    id = eqrun.id,
                    wocode = eqrun.wocode,
                    woname = eqrun.woname,
                    wocontent = eqrun.wocontent,
                    projectid = eqrun.projectid,
                    projectname = projeDefa != null && projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
                    matterids = eqrun.matterids,
                    //systemid = eqrun.systemid,
                    //systemname = dic_proDefa != null && dic_proDefa.Count() > 0 ? dic_proDefa.First().name : null,
                    //equipmentid = eqrun.equipmentid,
                    planid = eqrun.planid,
                    planname = planDefa != null && planDefa.Count() > 0 ? planDefa.First().planname : null,
                    lastchecktime = eqrun.lastchecktime,
                    lastcheckresultid = eqrun.lastcheckresultid,
                    starttime = eqrun.starttime,
                    endtime = eqrun.endtime,
                    createby = eqrun.createby,
                    createbyuser = createuserDefa != null && createuserDefa.Count() > 0 ? createuserDefa.Where(c => c.delete_flag == false).First().username : null,
                    createtime = eqrun.createtime,
                    isdelete = eqrun.isdelete,
                    checkby = eqrun.checkby,
                    checkbyuser = checkuserDefa != null && checkuserDefa.Count() > 0 ? checkuserDefa.Where(c => c.delete_flag == false).First().username : null,
                    quipmenttype = eqrun.quipmenttype,
                    checkcontent = eqrun.checkcontent,
                    checkpic = eqrun.checkpic,
                    extensionlist = eqrun.extensionlist,
                    wostate = eqrun.wostate,
                    dealby = eqrun.dealby,
                    dealbyuser = dealuserDefa != null && dealuserDefa.Count() > 0 ? dealuserDefa.Where(c => c.delete_flag == false).First().username : null,
                    dealtime = eqrun.dealtime,
                    isreturnmsg = eqrun.isreturnmsg
                };
            var info = query.FirstOrDefault();
            if (info != null)
            {
                var floorname = string.Empty;
                var bulidname = string.Empty;
                //var modelequipment = queryequipment.FirstOrDefault(x => x.id.Equals(info.equipmentid));
                //if (modelequipment != null)
                //{
                //    var modelfloor = queryfloor.FirstOrDefault(x => x.id.Equals(modelequipment.floorid));
                //    if (modelfloor != null)
                //    {
                //        floorname = modelfloor.floorname;
                //        var modelbulid = querybulid.FirstOrDefault(x => x.id.Equals(modelfloor.buildid));
                //        if (modelbulid != null)
                //        {
                //            bulidname = modelbulid.buildname;
                //        }
                //    }
                //}
                double timeoutlong = 0;
                if (info.wostate == (int)EnumEquipmentRunningState.deal || info.wostate == (int)EnumEquipmentRunningState.hangup)
                {
                    if (info.dealtime > info.endtime)
                        timeoutlong = (info.dealtime - info.endtime).TotalSeconds;
                }
                else
                {
                    if (DateTime.Now > info.endtime)
                        timeoutlong = (DateTime.Now - info.endtime).TotalSeconds;
                }

                var model = info;
                model.floorname = bulidname + floorname;    //楼号+楼层（巡检地点）
                model.timeoutlong = (int)Math.Ceiling(timeoutlong);

                return model;
            }
            return null;
        }

        public IEnumerable<Wo_EquipmentRunningChartDto> GetChart(string projectid)
        {
            var query = from eqrun in _collection.AsQueryable()
                        where eqrun.projectid.Equals(projectid)
                        group eqrun by eqrun.wodate into eqrunc
                        select new Wo_EquipmentRunningChartDto
                        {
                            wodate=eqrunc.Key,
                            counts = eqrunc.Count(),
                        };
            var list = query.ToList();
            
            return list;
        }
        #region
        ///// <summary>
        /////获取设备运行列表 Linq 关联查询 分页
        ///// </summary>
        ///// <param name="pageIndex"></param>
        ///// <param name="pageSize"></param>
        ///// <returns></returns>
        //public IEnumerable<Wo_EquipmentRunningDto> GetListsByPage(string sysnameid, string equipmentid, string wocode, string checkby, string daytype, string stime, string etime, int state, string rolecode, string usercode, int pageIndex, int pageSize, out int recordCount)
        //{
        //    //关联查询
        //    var query =
        //        from eqrun in _collection.AsQueryable()
        //        //join dic_pro in _collection_sys_dictionaryvalue.AsQueryable() on eqrun.sysnameid equals dic_pro.id into dic_proDefa
        //        join plan in _collection_wo_equipmentrunning_plan.AsQueryable() on eqrun.planid equals plan.id into planDefa
        //        join proje in _collection_pro_project_info.AsQueryable() on eqrun.projectid equals proje.id into projeDefa
        //        join dic_equ in _collection_equipment_info.AsQueryable() on eqrun.equipmentid equals dic_equ.id into dic_equDefa
        //        where eqrun.isdelete == 0
        //        orderby eqrun.createtime descending
        //        select new Wo_EquipmentRunningDto
        //        {
        //            id = eqrun.id,
        //            wocode = eqrun.wocode,
        //            projectid = eqrun.projectid,
        //            projectname = projeDefa != null && projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
        //            //sysnameid = eqrun.sysnameid,
        //            //sysnamename = dic_proDefa != null && dic_proDefa.Count() > 0 ? dic_proDefa.First().name : null,
        //            equipmentid = eqrun.equipmentid,
        //            planid = eqrun.planid,
        //            planname = planDefa != null && planDefa.Count() > 0 ? planDefa.First().planname : null,
        //            equipmentname = dic_equDefa != null && dic_equDefa.Count() > 0 ? dic_equDefa.First().equipment_name : null,
        //            sysnameid = dic_equDefa != null && dic_equDefa.Count() > 0 ? dic_equDefa.First().systemid : null,
        //            lastchecktime = eqrun.lastchecktime,
        //            thischeckime = eqrun.thischeckime,
        //            lastcheckresultid = eqrun.lastcheckresultid,
        //            abnormalcontent = eqrun.abnormalcontent,
        //            expirytime = eqrun.expirytime,
        //            advancelong = eqrun.advancelong,
        //            advancetime = eqrun.advancetime,
        //            createby = eqrun.createby,
        //            createtime = eqrun.createtime,
        //            isdelete = eqrun.isdelete,
        //            checkby = eqrun.checkby,
        //            quipmenttype = eqrun.quipmenttype,
        //            checkcontent = eqrun.checkcontent,
        //            checkpic = eqrun.checkpic,
        //            extensionlist = eqrun.extensionlist,
        //            wostate = eqrun.wostate,
        //            dealby = eqrun.dealby,
        //            dealtime=eqrun.dealtime
        //        };
        //    #region 查询条件
        //    if (!string.IsNullOrEmpty(sysnameid))
        //        query = query.Where(it => it.sysnameid == sysnameid);
        //    if (!string.IsNullOrEmpty(equipmentid))
        //        query = query.Where(it => it.equipmentid == equipmentid);
        //    if (!string.IsNullOrEmpty(wocode))
        //        query = query.Where(it => it.wocode.Contains(wocode));
        //    if (!string.IsNullOrEmpty(checkby))
        //        query = query.Where(it => it.checkby == checkby);
        //    if (!string.IsNullOrEmpty(daytype))
        //    {
        //        var datenow = DateTime.Now;
        //        var datestart = DateTime.Now;
        //        var dateend = DateTime.Now;
        //        switch (daytype)
        //        {
        //            case "week":
        //                datestart = datenow.AddDays(1 - Convert.ToInt32(datenow.DayOfWeek.ToString("d")));//本周周一
        //                dateend = datestart.AddDays(6);//本周周日
        //                break;
        //            case "month":
        //                datestart = datenow.AddDays(1 - datenow.Day);  //本月月初  
        //                dateend = datestart.AddMonths(1).AddDays(-1);  //本月月末  
        //                break;
        //            case "year":
        //                datestart = new DateTime(datenow.Year, 1, 1);  //本年年初  
        //                dateend = new DateTime(datenow.Year, 12, 31);  //本年年末  
        //                break;
        //            default:
        //                break;
        //        }
        //        query = query.Where(it => it.createtime >= datestart);
        //        query = query.Where(it => it.createtime <= dateend);
        //    }
        //    if (state >= 0)
        //    {
        //        switch (state)
        //        {
        //            case 11:
        //                //未完成
        //                query = query.Where(it => it.wostate < (int)EnumEquipmentRunningState.deal);
        //                break;
        //            case 12:
        //                //已完成
        //                query = query.Where(it => it.wostate == (int)EnumEquipmentRunningState.deal);
        //                break;
        //            case 13:
        //                //已挂起
        //                query = query.Where(it => it.wostate == (int)EnumEquipmentRunningState.hangup);
        //                break;
        //            default:
        //                query = query.Where(it => it.wostate == state);
        //                break;
        //        }
        //    }
        //    if (!string.IsNullOrWhiteSpace(stime))
        //    {
        //        DateTime dt2 = DateTime.Now;
        //        if (DateTime.TryParse(stime.Trim() + " 00:00:00", out dt2))
        //            query = query.Where(it => it.createtime >= dt2);
        //    }
        //    if (!string.IsNullOrWhiteSpace(etime))
        //    {
        //        DateTime dt2 = DateTime.Now;
        //        if (DateTime.TryParse(etime.Trim() + " 23:59:59", out dt2))
        //            query = query.Where(it => it.createtime <= dt2);
        //    }
        //    if (string.IsNullOrWhiteSpace(rolecode))
        //    {
        //        //运维员工只能看到自己的工单
        //        //且只能看到当天及当天之前的工单
        //        if (rolecode.Equals("YWYG"))
        //        {
        //            query = query.Where(it => it.advancetime <= DateTime.Now);
        //            query = query.Where(it => it.checkby == usercode);
        //        }
        //    }
        //    #endregion
        //    recordCount = query.Count();
        //    var list = query.Skip((pageIndex - 1) * pageSize).Take(pageSize);
        //    return list;
        //}

        //public IEnumerable<Wo_EquipmentRunningDto> GetLists()
        //{
        //    //关联查询
        //    var query =
        //        from eqrun in _collection.AsQueryable()
        //        //join dic_pro in _collection_sys_dictionaryvalue.AsQueryable() on eqrun.sysnameid equals dic_pro.id into dic_proDefa
        //        join plan in _collection_wo_equipmentrunning_plan.AsQueryable() on eqrun.planid equals plan.id into planDefa
        //        join proje in _collection_pro_project_info.AsQueryable() on eqrun.projectid equals proje.id into projeDefa
        //        join dic_equ in _collection_equipment_info.AsQueryable() on eqrun.equipmentid equals dic_equ.id into dic_equDefa
        //        where eqrun.isdelete == 0
        //        orderby eqrun.createtime descending
        //        select new Wo_EquipmentRunningDto
        //        {
        //            id = eqrun.id,
        //            wocode = eqrun.wocode,
        //            projectid = eqrun.projectid,
        //            projectname = projeDefa != null && projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
        //            //sysnameid = eqrun.sysnameid,
        //            //sysnamename = dic_proDefa != null && dic_proDefa.Count() > 0 ? dic_proDefa.First().name : null,
        //            equipmentid = eqrun.equipmentid,
        //            planid = eqrun.planid,
        //            planname = planDefa != null && planDefa.Count() > 0 ? planDefa.First().planname : null,
        //            equipmentname = dic_equDefa != null && dic_equDefa.Count() > 0 ? dic_equDefa.First().equipment_name : null,
        //            sysnameid = dic_equDefa != null && dic_equDefa.Count() > 0 ? dic_equDefa.First().systemid : null,
        //            lastchecktime = eqrun.lastchecktime,
        //            thischeckime = eqrun.thischeckime,
        //            lastcheckresultid = eqrun.lastcheckresultid,
        //            abnormalcontent = eqrun.abnormalcontent,
        //            expirytime = eqrun.expirytime,
        //            advancelong = eqrun.advancelong,
        //            advancetime = eqrun.advancetime,
        //            createby = eqrun.createby,
        //            createtime = eqrun.createtime,
        //            isdelete = eqrun.isdelete,
        //            checkby = eqrun.checkby,
        //            quipmenttype = eqrun.quipmenttype,
        //            checkcontent = eqrun.checkcontent,
        //            checkpic = eqrun.checkpic,
        //            extensionlist = eqrun.extensionlist,
        //            wostate = eqrun.wostate,
        //            dealby = eqrun.dealby,
        //            dealtime = eqrun.dealtime
        //        };
        //    var list = query;
        //    return list;
        //}

        ///// <summary>
        ///// 获取设备运行列表 by page Linq 关联查询
        ///// </summary>
        ///// <param name="pageIndex"></param>
        ///// <param name="pageSize"></param>
        ///// <returns></returns>
        //public IEnumerable<Wo_EquipmentRunningDto> GetAppLists(string sysnameid, string equipmentid, string checkby, string daytype, int state, string stime, string etime, string rolecode, string usercode)
        //{
        //    //关联查询
        //    var query =
        //        from eqrun in _collection.AsQueryable()
        //        //join dic_pro in _collection_sys_dictionaryvalue.AsQueryable() on eqrun.sysnameid equals dic_pro.id into dic_proDefa
        //        join plan in _collection_wo_equipmentrunning_plan.AsQueryable() on eqrun.planid equals plan.id into planDefa
        //        join proje in _collection_pro_project_info.AsQueryable() on eqrun.projectid equals proje.id into projeDefa
        //        join dic_equ in _collection_equipment_info.AsQueryable() on eqrun.equipmentid equals dic_equ.id into dic_equDefa
        //        where eqrun.isdelete == 0
        //        orderby eqrun.createtime descending
        //        select new Wo_EquipmentRunningDto
        //        {
        //            id = eqrun.id,
        //            wocode = eqrun.wocode,
        //            projectid = eqrun.projectid,
        //            projectname = projeDefa != null && projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
        //            //sysnameid = eqrun.sysnameid,
        //            //sysnamename = dic_proDefa != null && dic_proDefa.Count() > 0 ? dic_proDefa.First().name : null,
        //            equipmentid = eqrun.equipmentid,
        //            planid = eqrun.planid,
        //            planname = planDefa != null && planDefa.Count() > 0 ? planDefa.First().planname : null,
        //            equipmentname = dic_equDefa != null && dic_equDefa.Count() > 0 ? dic_equDefa.First().equipment_name : null,
        //            sysnameid = dic_equDefa != null && dic_equDefa.Count() > 0 ? dic_equDefa.First().systemid : null,
        //            lastchecktime = eqrun.lastchecktime,
        //            thischeckime = eqrun.thischeckime,
        //            lastcheckresultid = eqrun.lastcheckresultid,
        //            abnormalcontent = eqrun.abnormalcontent,
        //            expirytime = eqrun.expirytime,
        //            advancelong = eqrun.advancelong,
        //            advancetime = eqrun.advancetime,
        //            createby = eqrun.createby,
        //            createtime = eqrun.createtime,
        //            isdelete = eqrun.isdelete,
        //            checkby = eqrun.checkby,
        //            quipmenttype = eqrun.quipmenttype,
        //            checkcontent = eqrun.checkcontent,
        //            checkpic = eqrun.checkpic,
        //            extensionlist = eqrun.extensionlist,
        //            wostate = eqrun.wostate,
        //            dealby = eqrun.dealby,
        //            dealtime = eqrun.dealtime
        //        };
        //    #region 查询条件
        //    if (!string.IsNullOrEmpty(sysnameid))
        //        query = query.Where(it => it.sysnameid == sysnameid);
        //    if (!string.IsNullOrEmpty(equipmentid))
        //        query = query.Where(it => it.equipmentid == equipmentid);
        //    if (!string.IsNullOrEmpty(checkby))
        //        query = query.Where(it => it.checkby == checkby);
        //    if (!string.IsNullOrEmpty(daytype))
        //    {
        //        var datenow = DateTime.Now;
        //        var datestart = DateTime.Now;
        //        var dateend = DateTime.Now;
        //        switch (daytype)
        //        {
        //            case "week":
        //                datestart = datenow.AddDays(1 - Convert.ToInt32(datenow.DayOfWeek.ToString("d")));//本周周一
        //                dateend = datestart.AddDays(6);//本周周日
        //                break;
        //            case "month":
        //                datestart = datenow.AddDays(1 - datenow.Day);  //本月月初  
        //                dateend = datestart.AddMonths(1).AddDays(-1);  //本月月末  
        //                break;
        //            case "year":
        //                datestart = new DateTime(datenow.Year, 1, 1);  //本年年初  
        //                dateend = new DateTime(datenow.Year, 12, 31);  //本年年末  
        //                break;
        //            default:
        //                break;
        //        }
        //        query = query.Where(it => it.createtime >= datestart);
        //        query = query.Where(it => it.createtime <= dateend);
        //    }
        //    if (state >= 0)
        //    {
        //        switch (state)
        //        {
        //            case 11:
        //                //未完成
        //                query = query.Where(it => it.wostate == (int)EnumEquipmentRunningState.create);
        //                break;
        //            case 12:
        //                //已完成
        //                query = query.Where(it => it.wostate == (int)EnumEquipmentRunningState.deal);
        //                break;
        //            case 13:
        //                //已挂起
        //                query = query.Where(it => it.wostate == (int)EnumEquipmentRunningState.hangup);
        //                break;
        //            default:
        //                query = query.Where(it => it.wostate == state);
        //                break;
        //        }
        //    }
        //    if (!string.IsNullOrWhiteSpace(stime))
        //    {
        //        DateTime dt2 = DateTime.Now;
        //        if (DateTime.TryParse(stime.Trim() + " 00:00:00", out dt2))
        //            query = query.Where(it => it.createtime >= dt2);
        //    }
        //    if (!string.IsNullOrWhiteSpace(etime))
        //    {
        //        DateTime dt2 = DateTime.Now;
        //        if (DateTime.TryParse(etime.Trim() + " 23:59:59", out dt2))
        //            query = query.Where(it => it.createtime <= dt2);
        //    }
        //    if (string.IsNullOrWhiteSpace(rolecode))
        //    {
        //        //运维员工只能看到自己的工单
        //        //且只能看到当天及当天之前的工单
        //        if (rolecode.Equals("YWYG"))
        //        {
        //            query = query.Where(it => it.advancetime <= DateTime.Now);
        //            query = query.Where(it => it.checkby == usercode);
        //        }
        //    }
        //    #endregion
        //    var list = query;
        //    return list;
        //}

        ///// <summary>
        ///// 获取设备运行详情 by id Linq 关联查询
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //public Wo_EquipmentRunningDto GetDetails(string id)
        //{
        //    var queryequipment = from equipment in _collection_equipment_info.AsQueryable() select equipment;
        //    var querybulid = from bulidin in _collection_sys_buliding.AsQueryable() select bulidin;
        //    var queryfloor = from floor in _collection_sys_floor.AsQueryable() select floor;
        //    //关联查询
        //    var query =
        //        from eqrun in _collection.AsQueryable()
        //        //join dic_pro in _collection_sys_dictionaryvalue.AsQueryable() on eqrun.sysnameid equals dic_pro.id into dic_proDefa
        //        join plan in _collection_wo_equipmentrunning_plan.AsQueryable() on eqrun.planid equals plan.id into planDefa
        //        join proje in _collection_pro_project_info.AsQueryable() on eqrun.projectid equals proje.id into projeDefa
        //        join dic_equ in _collection_equipment_info.AsQueryable() on eqrun.equipmentid equals dic_equ.id into dic_equDefa
        //        where eqrun.isdelete == 0 && eqrun.id == id
        //        orderby eqrun.createtime descending
        //        select new Wo_EquipmentRunningDto
        //        {
        //            id = eqrun.id,
        //            wocode = eqrun.wocode,
        //            projectid = eqrun.projectid,
        //            projectname = projeDefa != null && projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
        //            //sysnameid = eqrun.sysnameid,
        //            //sysnamename = dic_proDefa != null && dic_proDefa.Count() > 0 ? dic_proDefa.First().name : null,
        //            equipmentid = eqrun.equipmentid,
        //            planid = eqrun.planid,
        //            planname = planDefa != null && planDefa.Count() > 0 ? planDefa.First().planname : null,
        //            equipmentname = dic_equDefa != null && dic_equDefa.Count() > 0 ? dic_equDefa.First().equipment_name : null,
        //            sysnameid = dic_equDefa != null && dic_equDefa.Count() > 0 ? dic_equDefa.First().systemid : null,
        //            lastchecktime = eqrun.lastchecktime,
        //            thischeckime = eqrun.thischeckime,
        //            lastcheckresultid = eqrun.lastcheckresultid,
        //            abnormalcontent = eqrun.abnormalcontent,
        //            expirytime = eqrun.expirytime,
        //            advancelong = eqrun.advancelong,
        //            advancetime = eqrun.advancetime,
        //            createby = eqrun.createby,
        //            createtime = eqrun.createtime,
        //            isdelete = eqrun.isdelete,
        //            checkby = eqrun.checkby,
        //            quipmenttype = eqrun.quipmenttype,
        //            checkcontent = eqrun.checkcontent,
        //            checkpic = eqrun.checkpic,
        //            extensionlist = eqrun.extensionlist,
        //            wostate = eqrun.wostate,
        //            dealby = eqrun.dealby,
        //            dealtime = eqrun.dealtime
        //        };
        //    var info = query.FirstOrDefault();
        //    if (info != null)
        //    {
        //        var floorname = string.Empty;
        //        var bulidname = string.Empty;
        //        var modelequipment = queryequipment.FirstOrDefault(x => x.id.Equals(info.equipmentid));
        //        if (modelequipment != null)
        //        {
        //            var modelfloor = queryfloor.FirstOrDefault(x => x.id.Equals(modelequipment.floorid));
        //            if (modelfloor != null)
        //            {
        //                floorname = modelfloor.floorname;
        //                var modelbulid = querybulid.FirstOrDefault(x => x.id.Equals(modelfloor.buildid));
        //                if (modelbulid != null)
        //                {
        //                    bulidname = modelbulid.buildname;
        //                }
        //            }
        //        }
        //        double timeoutlong = 0;
        //        if (info.wostate == (int)EnumEquipmentRunningState.deal || info.wostate == (int)EnumEquipmentRunningState.hangup)
        //        {
        //            if (info.dealtime <= info.expirytime)
        //                timeoutlong = (info.dealtime - info.expirytime).TotalSeconds;
        //        }
        //        else
        //        {
        //            if (DateTime.Now > info.expirytime)
        //                timeoutlong = (DateTime.Now - info.expirytime).TotalSeconds;
        //        }

        //        var model = info;
        //        model.floorname = bulidname + floorname;    //楼号+楼层（巡检地点）
        //        model.timeoutlong = (int)Math.Ceiling(timeoutlong);

        //        return model;
        //    }
        //    return null;
        //}
        #endregion

    }
}
