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

namespace EquipmentApi.Repositories
{
    public class Wo_Repair_BaseRepository : BaseRepository<Wo_Repair_Base, string>, IWo_Repair_BaseRepository
    {
        protected readonly ILogger<BaseRepository<Wo_Repair_Base, 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<Sys_Provinces> _collection_sys_provinces;
        protected readonly IMongoCollection<Sys_City> _collection_sys_city;
        protected readonly IMongoCollection<Sys_SystemConfig> _collection_sys_systemconfig;
        protected readonly IMongoCollection<Sys_User_Account> _collection_sys_user_account;
        protected readonly IMongoCollection<Sys_Role_Info> _collection_sysroleinfo;
        protected readonly IMongoCollection<Sys_Work_UserGroup> _collection_sys_work_usergroup;
        protected readonly IMongoCollection<Wo_Maintain_Base> _collection_wo_maintain_base;
        protected readonly IMongoCollection<Wo_EquipmentRunning> _collection_equipmentrunning;
        private readonly IMongoCollection<wxwpstime> _collection_ewxwpstime;

        protected readonly IMongoCollection<Sys_Work_NightGroup> _collection_sys_work_nightgroup;
        protected readonly IMongoCollection<Sys_Work_DayGroup> _collection_sys_work_daygroup;


        public Wo_Repair_BaseRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Wo_Repair_Base, string>> logger) : base(settings, logger)
        {
            _logger = 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_sys_provinces = _context.GetCollection<Sys_Provinces>();
            _collection_sys_city = _context.GetCollection<Sys_City>();
            _collection_sys_systemconfig = _context.GetCollection<Sys_SystemConfig>();
            _collection_sys_user_account = _context.GetCollection<Sys_User_Account>();
            _collection_sysroleinfo = _context.GetCollection<Sys_Role_Info>();
            _collection_sys_work_usergroup = _context.GetCollection<Sys_Work_UserGroup>();
            _collection_wo_maintain_base = _context.GetCollection<Wo_Maintain_Base>();
            _collection_equipmentrunning = _context.GetCollection<Wo_EquipmentRunning>();
            _collection_ewxwpstime = _context.GetCollection<wxwpstime>();
            _collection_sys_work_nightgroup = _context.GetCollection<Sys_Work_NightGroup>();
            _collection_sys_work_daygroup = _context.GetCollection<Sys_Work_DayGroup>();
        }

        #region 联合查询
        /// <summary>
        /// 根据多个条件查询列表 Linq 关联查询
        /// </summary>
        public IEnumerable<object> GetListsByPage(string phone, string wocode, string province, string city, string projectid, string protypeid, string buildid, string floorid,
            string replace, string systemid, string equipmentid, string usercode, string stime, string etime,string channel,string Jwocode, int state, int wxstate,
            string nowusercode, UserAccountInfoModel userinfo,
            //string nowrolecode,
            int pageindex, int pagesize, out int recordcount,
            out int totalcount, out int wzpcount, out int yjdcount, out int dpjcount, out int ygqcount, out int ywjcount, out int yqxcount)
        {
            #region 数据读取
            var query =
                from repairbase in _collection.AsQueryable()
                    //join dic_system in _collection_sys_dictionaryvalue.AsQueryable() on repairbase.systemid equals dic_system.id into dic_systemDefa
                    //join dic_protype in _collection_sys_dictionaryvalue.AsQueryable() on repairbase.protypeid equals dic_protype.id into dic_protypeDefa
                    //join dic_faulttype in _collection_sys_dictionaryvalue.AsQueryable() on repairbase.faulttypeid equals dic_faulttype.id into dic_faulttypeDefa
                join proje in _collection_pro_project_info.AsQueryable() on repairbase.projectid equals proje.id into projeDefa
                //join dic_equ in _collection_equipment_info.AsQueryable() on repairbase.equipmentid equals dic_equ.id into dic_equDefa
                join sysprovinces in _collection_sys_provinces.AsQueryable() on repairbase.province equals sysprovinces.provincecode into sys_provincesDefa
                join syscity in _collection_sys_city.AsQueryable() on repairbase.city equals syscity.citycode into sys_cityDefa
                //join bld in _collection_sys_buliding.AsQueryable() on repairbase.buildid equals bld.id into bldDefa
                //join flr in _collection_sys_floor.AsQueryable() on repairbase.floorid equals flr.id into flrDefa
                where repairbase.isdelete == 0
                orderby repairbase.createtime descending
                select new
                {
                    repairbase.id,
                    repairbase.wocode,
                    repairbase.projectid,
                    projectname = projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
                    repairbase.protypeid,
                    //protypename = dic_protypeDefa.Count() > 0 ? dic_protypeDefa.First().name : null,
                    repairbase.faulttypeid,
                    //faulttypename = dic_faulttypeDefa.Count() > 0 ? dic_faulttypeDefa.First().name : null,
                    repairbase.systemid,
                    //systemname = dic_systemDefa.Count() > 0 ? dic_systemDefa.First().name : null,
                    repairbase.equipmentid,
                    //equipmentname = dic_equDefa.Count() > 0 ? dic_equDefa.First().equipment_name : null,
                    repairbase.channel,
                    provincecode = repairbase.province,
                    province = sys_provincesDefa.Count() > 0 ? sys_provincesDefa.First().provincename : null,
                    citycode = repairbase.city,
                    city = sys_cityDefa.Count() > 0 ? sys_cityDefa.First().cityname : null,
                    repairbase.buildid,
                    //buildname = bldDefa.Count() > 0 ? bldDefa.First().buildname : null,
                    repairbase.floorid,
                    //floorname = flrDefa.Count() > 0 ? flrDefa.First().floorname : null,
                    repairbase.repairreplace,
                    repairbase.repairoom,
                    repairbase.repairtime,
                    repairbase.contactway,
                    repairbase.repairman,
                    repairbase.faultdescripe,
                    repairbase.faultpic,
                    repairbase.appointtime,
                    repairbase.wostate,
                    repairbase.createtime,
                    repairbase.createuser,
                    repairbase.createusername,
                    repairbase.touser,
                    repairbase.tousername,
                    repairbase.totime,
                    repairbase.totype,
                    repairbase.dealuser,
                    repairbase.dealusername,
                    repairbase.dealtime,
                    repairbase.ismaterials,
                    repairbase.materials,
                    repairbase.islosematerials,
                    repairbase.losematerials,
                    repairbase.hanguptime,
                    repairbase.arrivetime,
                    repairbase.evaluatetime,
                    repairbase.canceltime,
                    repairbase.score,
                    repairbase.Jwocode,
                    repairbase.Pwcode,
                    repairbase.CLpeoplefrom
                };
            #endregion
            #region 查询条件
            if (!string.IsNullOrEmpty(phone))
                query = query.Where(it => it.contactway.Contains(phone) || it.wocode.Contains(phone)
                        || it.repairreplace.Contains(phone) || it.repairoom.Contains(phone)
                        || it.Jwocode.Contains(phone)||it.createusername.Contains(phone)
                        ||it.repairman.Contains(phone)||it.faultdescripe.Contains(phone)
                        ||it.CLpeoplefrom.Contains(phone));
            if (!string.IsNullOrEmpty(wocode))
                query = query.Where(it => it.wocode.Contains(wocode));
            if (!string.IsNullOrEmpty(province))
                query = query.Where(it => it.provincecode == province);
            if (!string.IsNullOrEmpty(city))
                query = query.Where(it => it.citycode == city);
            if (!string.IsNullOrEmpty(projectid))
                query = query.Where(it => it.projectid == projectid);
            if (!string.IsNullOrEmpty(protypeid))
                query = query.Where(it => it.protypeid == protypeid);
            if (!string.IsNullOrEmpty(buildid))
                query = query.Where(it => it.buildid == buildid);
            if (!string.IsNullOrEmpty(floorid))
                query = query.Where(it => it.floorid == floorid);
            if (!string.IsNullOrEmpty(replace))
                query = query.Where(it => it.repairreplace.Contains(replace) || it.repairoom.Contains(replace));
            if (!string.IsNullOrEmpty(systemid))
                query = query.Where(it => it.systemid == systemid);
            if (!string.IsNullOrEmpty(equipmentid))
                query = query.Where(it => it.equipmentid == equipmentid);
            if (!string.IsNullOrEmpty(channel))
                query = query.Where(it=>it.channel==Convert.ToInt32(channel));
            if (!string.IsNullOrEmpty(Jwocode))
                query = query.Where(it => it.Jwocode == Jwocode);
            #region 时间搜索
            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 (userinfo.type == 2)//查看自己创建的工单
            {
                query = query.Where(it => it.createuser == userinfo.usercode);
            }
            else
            {
                //if (userinfo.isallproject == 1)
                //{
                    if (!string.IsNullOrEmpty(usercode)) query = query.Where(it => (it.touser.Contains(usercode) || it.dealuser.Contains(usercode)));
                //}
                //else if (userinfo.isallproject == 0)
                //{
                //    query = query.Where(it => userinfo.projectlist.Contains(it.projectid));

                //    //switch (userinfo.level)
                //    //{
                //    //    case 1://查看自己维修的工单
                //    //        query = query.Where(it => (it.touser.Count() == 0 || it.touser.Contains(userinfo.usercode) || it.dealuser.Contains(userinfo.usercode)));
                //    //        break;
                //    //    case 2://查看自己项目的工单
                //    //        if (!string.IsNullOrEmpty(usercode)) query = query.Where(it => (it.touser.Contains(usercode) || it.dealuser.Contains(usercode)));
                //    //        break;
                //    //}

                //    if (userinfo.role_code == "YWYG")
                //    {
                //        query = query.Where(it => (it.touser.Count() == 0 || it.touser.Contains(nowusercode) || it.dealuser.Contains(nowusercode)));
                //    }
                //    else
                //    {
                //        if (!string.IsNullOrEmpty(usercode)) query = query.Where(it => (it.touser.Contains(usercode) || it.dealuser.Contains(usercode)));
                //    }
                //}
            }

            //if (string.IsNullOrEmpty(nowrolecode))
            //{
            //    query = query.Where(it => it.createuser == nowusercode);
            //}
            //else if (nowrolecode == "YWYG")
            //{
            //    query = query.Where(it => (it.touser.Count() == 0 || it.touser.Contains(nowusercode) || it.dealuser.Contains(nowusercode)));
            //}
            //else
            //{
            //    if (!string.IsNullOrEmpty(usercode)) query = query.Where(it => (it.touser.Contains(usercode) || it.dealuser.Contains(usercode)));
            //}

            
            //wzpcount = query.Where(p => p.wostate == (int)EnumWorkOrderState.create).Count();
            //yjdcount = query.Where(p => p.wostate == (int)EnumWorkOrderState.assign).Count();
            //dpjcount = query.Where(p => p.wostate == (int)EnumWorkOrderState.deal).Count();
            //ygqcount = query.Where(p => p.wostate == (int)EnumWorkOrderState.hangup).Count();
            //ywjcount = query.Where(p => p.wostate == (int)EnumWorkOrderState.evaluate).Count();
            //yqxcount = query.Where(p => p.wostate == (int)EnumWorkOrderState.cancle).Count();

            if (userinfo.role_name.Contains("班") || userinfo.role_name == "默认角色")
            {
               wzpcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.create &&   (p.createusername == userinfo.username || p.tousername[0] == userinfo.username||p.repairman==userinfo.username|| p.repairman.Contains(userinfo.usercode)));
               yjdcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.assign &&   (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
               dpjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.deal &&     (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
               ygqcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.hangup &&   (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
               ywjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.evaluate && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
               yqxcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.cancle &&   (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
               
            }
            else
            {
                wzpcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.create);
                yjdcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.assign);
                dpjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.deal);
                ygqcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.hangup);
                ywjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.evaluate);
                yqxcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.cancle);
            }

           

            if (state != -1)
            {
                query = query.Where(it => it.wostate == state);

                if (userinfo.role_name.Contains("班") || userinfo.role_name == "默认角色")
                {
                   

                    if (!string.IsNullOrEmpty(userinfo.username))
                    {
                        query = query.Where(it => it.createusername == userinfo.username||it.tousername[0]==userinfo.username || it.repairman == userinfo.username || it.repairman.Contains(userinfo.usercode));
                    }
                }
                switch (state)
                {
                    case (int)EnumWorkOrderState.assign: query = query.OrderByDescending(p => p.totime); break;
                    case (int)EnumWorkOrderState.deal: query = query.OrderByDescending(p => p.dealtime); break;
                    //case (int)EnumWorkOrderState.evaluate: query = query.OrderBy(p => p.evaluatetime); break;
                    case (int)EnumWorkOrderState.hangup: query = query.OrderByDescending(p => p.hanguptime); break;
                        //case (int)EnumWorkOrderState.cancle: query = query.OrderBy(p => p.canceltime); break;
                }
            }
            else
            {
                if (userinfo.role_name.Contains("班") || userinfo.role_name == "默认角色")
                {
                    if (!string.IsNullOrEmpty(userinfo.username))
                    {
                        query = query.Where(it => it.createusername == userinfo.username || it.tousername[0] == userinfo.username || it.repairman == userinfo.username || it.repairman.Contains(userinfo.usercode));
                    }
                }
            }
            if (wxstate != -1)
            {
                if (wxstate == 1)
                {
                    query = query.Where(it => it.wostate <= wxstate);
                }
                else
                {
                    query = query.Where(it => it.wostate == wxstate);
                }
                switch (wxstate)
                {
                    case (int)EnumWorkOrderState.assign: query = query.OrderByDescending(p => p.totime); break;
                    case (int)EnumWorkOrderState.deal: query = query.OrderByDescending(p => p.dealtime); break;
                    //case (int)EnumWorkOrderState.evaluate: query = query.OrderBy(p => p.evaluatetime); break;
                    case (int)EnumWorkOrderState.hangup: query = query.OrderByDescending(p => p.hanguptime); break;
                        //case (int)EnumWorkOrderState.cancle: query = query.OrderBy(p => p.canceltime); break;
                }
            }
            #endregion
            #endregion
            recordcount = query.Count();
            //总数
            totalcount = query.Count();
            var list = query.Skip((pageindex - 1) * pagesize).Take(pagesize);
            return list;
        }
        /// <summary>
        /// 尝试关联用户
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="wocode"></param>
        /// <param name="province"></param>
        /// <param name="city"></param>
        /// <param name="projectid"></param>
        /// <param name="protypeid"></param>
        /// <param name="buildid"></param>
        /// <param name="floorid"></param>
        /// <param name="replace"></param>
        /// <param name="systemid"></param>
        /// <param name="equipmentid"></param>
        /// <param name="usercode"></param>
        /// <param name="stime"></param>
        /// <param name="etime"></param>
        /// <param name="channel"></param>
        /// <param name="Jwocode"></param>
        /// <param name="state"></param>
        /// <param name="wxstate"></param>
        /// <param name="nowusercode"></param>
        /// <param name="userinfo"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="recordcount"></param>
        /// <param name="totalcount"></param>
        /// <param name="wzpcount"></param>
        /// <param name="yjdcount"></param>
        /// <param name="dpjcount"></param>
        /// <param name="ygqcount"></param>
        /// <param name="ywjcount"></param>
        /// <param name="yqxcount"></param>
        /// <returns></returns>
    
        //报修平台查询的 自己创建的工单
        public IEnumerable<object> GetListsByPageglhsz(string phone, string wocode, string province, string city, string projectid, string protypeid, string buildid, string floorid,
         string replace, string systemid, string equipmentid, string usercode, string stime, string etime, string channel, string Jwocode, int state, int wxstate,
         string nowusercode, UserAccountInfoModel userinfo,
         //string nowrolecode,
         int pageindex, int pagesize, out int recordcount,
         out int totalcount, out int wzpcount, out int yjdcount, out int dpjcount, out int ygqcount, out int ywjcount, out int yqxcount)
        {
            if (userinfo.role_code == "XTWH" || userinfo.role_code == "CJGL") //系统维护和管理查询所有的工单
            {
                #region 数据读取
                var query =
                    from repairbase in _collection.AsQueryable()
                    join proje in _collection_pro_project_info.AsQueryable() on repairbase.projectid equals proje.id into projeDefa
                    join sysprovinces in _collection_sys_provinces.AsQueryable() on repairbase.province equals sysprovinces.provincecode into sys_provincesDefa
                    join syscity in _collection_sys_city.AsQueryable() on repairbase.city equals syscity.citycode into sys_cityDefa
                    join wxtimelis in _collection_ewxwpstime.AsQueryable() on repairbase.wxtimeid equals wxtimelis.id into wxtimelisDefa

                    where repairbase.isdelete == 0
                    orderby repairbase.createtime descending
                    select new
                    {
                        repairbase.id,
                        repairbase.wocode,
                        repairbase.projectid,
                        projectname = projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
                        repairbase.protypeid,
                        repairbase.faulttypeid,
                        repairbase.systemid,
                        repairbase.equipmentid,
                        repairbase.channel,
                        provincecode = repairbase.province,
                        province = sys_provincesDefa.Count() > 0 ? sys_provincesDefa.First().provincename : null,
                        citycode = repairbase.city,
                        city = sys_cityDefa.Count() > 0 ? sys_cityDefa.First().cityname : null,
                        repairbase.buildid,
                        repairbase.floorid,

                        repairbase.repairreplace,
                        repairbase.repairoom,
                        repairbase.repairtime,
                        repairbase.contactway,
                        repairbase.repairman,
                        repairbase.faultdescripe,
                        repairbase.faultpic,
                        repairbase.appointtime,
                        repairbase.wostate,
                        repairbase.createtime,
                        repairbase.createuser,
                        repairbase.createusername,
                        repairbase.touser,
                        repairbase.tousername,
                        repairbase.totime,
                        repairbase.totype,
                        repairbase.dealuser,
                        repairbase.dealusername,
                        repairbase.dealtime,
                        repairbase.ismaterials,
                        repairbase.materials,
                        repairbase.islosematerials,
                        repairbase.losematerials,
                        repairbase.hanguptime,
                        repairbase.arrivetime,
                        repairbase.evaluatetime,
                        repairbase.canceltime,
                        repairbase.score,
                        repairbase.Jwocode,
                        repairbase.Pwcode,
                        repairbase.CLpeoplefrom,
                        repairbase.tobztimes,
                        repairbase.wxtimeid,
                        wxtimeleths = wxtimelisDefa.Count() > 0 ? wxtimelisDefa.First().timecount : 0,//维修时长
                    };
                #endregion

                #region 查询条件
                if (!string.IsNullOrEmpty(phone))
                    query = query.Where(it => it.contactway.Contains(phone) || it.wocode.Contains(phone)
                            || it.repairreplace.Contains(phone) || it.repairoom.Contains(phone)
                            || it.Jwocode.Contains(phone) || it.createusername.Contains(phone)
                            || it.repairman.Contains(phone) || it.faultdescripe.Contains(phone)
                            || it.CLpeoplefrom.Contains(phone)
                            );
                if (!string.IsNullOrEmpty(wocode))
                    query = query.Where(it => it.wocode.Contains(wocode));
                if (!string.IsNullOrEmpty(province))
                    query = query.Where(it => it.provincecode == province);
                if (!string.IsNullOrEmpty(city))
                    query = query.Where(it => it.citycode == city);
                if (!string.IsNullOrEmpty(projectid))
                    query = query.Where(it => it.projectid == projectid);
                if (!string.IsNullOrEmpty(protypeid))
                    query = query.Where(it => it.protypeid == protypeid);
                if (!string.IsNullOrEmpty(buildid))
                    query = query.Where(it => it.buildid == buildid);
                if (!string.IsNullOrEmpty(floorid))
                    query = query.Where(it => it.floorid == floorid);
                if (!string.IsNullOrEmpty(replace))
                    query = query.Where(it => it.repairreplace.Contains(replace) || it.repairoom.Contains(replace));
                if (!string.IsNullOrEmpty(systemid))
                    query = query.Where(it => it.systemid == systemid);
                if (!string.IsNullOrEmpty(equipmentid))
                    query = query.Where(it => it.equipmentid == equipmentid);
                if (!string.IsNullOrEmpty(channel))
                    query = query.Where(it => it.channel == Convert.ToInt32(channel));
                if (!string.IsNullOrEmpty(Jwocode))
                    query = query.Where(it => it.Jwocode == Jwocode);
                #region 时间搜索
                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
                #endregion

                if (userinfo.type == 2)//查看自己创建的工单
                {
                    query = query.Where(it => it.createuser == userinfo.usercode);
                }
                else
                {
                    if (!string.IsNullOrEmpty(usercode)) query = query.Where(it => (it.touser.Contains(usercode) || it.dealuser.Contains(usercode)));
                }
                totalcount = query.Count();
                wzpcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.create);
                yjdcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.assign);
                dpjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.deal);
                ygqcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.hangup);
                ywjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.evaluate);
                yqxcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.cancle);

                //        if (userinfo.role_name.Contains("班") || userinfo.role_name == "默认角色" || userinfo.role_name == "后勤部" || userinfo.role_name == "信息科" || userinfo.role_name == "设备科")
                //        {


                //            wzpcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.create && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
                //            yjdcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.assign && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
                //            dpjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.deal && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
                //            ygqcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.hangup && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
                //            ywjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.evaluate && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
                //            yqxcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.cancle && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));


                //        }
                //        else
                //        {
                //            wzpcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.create);
                //            yjdcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.assign);
                //            dpjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.deal);
                //            ygqcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.hangup);
                //            ywjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.evaluate);
                //            yqxcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.cancle);
                //        }



                if (state != -1)
                {
                    query = query.Where(it => it.wostate == state);

                    if (userinfo.role_name.Contains("班") || userinfo.role_name == "默认角色" || userinfo.role_name == "后勤部" || userinfo.role_name == "信息科" || userinfo.role_name == "设备科")
                    {

                        if (!string.IsNullOrEmpty(userinfo.username))
                        {
                            query = query.Where(it => it.createusername == userinfo.username || it.tousername[0] == userinfo.username || it.repairman == userinfo.username || it.repairman.Contains(userinfo.usercode));
                        }


                    }
                    switch (state)
                    {
                        case (int)EnumWorkOrderState.assign: query = query.OrderByDescending(p => p.totime); break;
                        case (int)EnumWorkOrderState.deal: query = query.OrderByDescending(p => p.dealtime); break;
                        case (int)EnumWorkOrderState.hangup: query = query.OrderByDescending(p => p.hanguptime); break;
                    }
                }
                else
                {
                    if (userinfo.role_name.Contains("班") || userinfo.role_name == "默认角色" || userinfo.role_name == "后勤部" || userinfo.role_name == "信息科" || userinfo.role_name == "设备科")
                    {

                        //20210531注释 if
                        //if (!string.IsNullOrEmpty(userinfo.username))
                        //{
                        //    query = query.Where(it => it.createusername == userinfo.username || it.tousername[0] == userinfo.username || it.repairman == userinfo.username || it.repairman.Contains(userinfo.usercode));
                        //}
                        query = query.Where(it => it.createusername == userinfo.username);

                    }
                }
                if (wxstate != -1)
                {
                    if (wxstate == 1)
                    {
                        query = query.Where(it => it.wostate <= wxstate);
                    }
                    else
                    {
                        query = query.Where(it => it.wostate == wxstate);
                    }
                    switch (wxstate)
                    {
                        case (int)EnumWorkOrderState.assign: query = query.OrderByDescending(p => p.totime); break;
                        case (int)EnumWorkOrderState.deal: query = query.OrderByDescending(p => p.dealtime); break;
                        //case (int)EnumWorkOrderState.evaluate: query = query.OrderBy(p => p.evaluatetime); break;
                        case (int)EnumWorkOrderState.hangup: query = query.OrderByDescending(p => p.hanguptime); break;
                            //case (int)EnumWorkOrderState.cancle: query = query.OrderBy(p => p.canceltime); break;
                    }
                }
                //    #endregion
                //    #endregion
                recordcount = query.Count();
                //总数
              //  totalcount = query.Count();
                var list = query.Skip((pageindex - 1) * pagesize).Take(pagesize);
                return list;
                //    #endregion




            }
            else //只看自己创建的工单
            {
                #region 数据读取
                var query =
                    from repairbase in _collection.AsQueryable()
                    join proje in _collection_pro_project_info.AsQueryable() on repairbase.projectid equals proje.id into projeDefa
                    join sysprovinces in _collection_sys_provinces.AsQueryable() on repairbase.province equals sysprovinces.provincecode into sys_provincesDefa
                    join syscity in _collection_sys_city.AsQueryable() on repairbase.city equals syscity.citycode into sys_cityDefa
                    join wxtimelis in _collection_ewxwpstime.AsQueryable() on repairbase.wxtimeid equals wxtimelis.id into wxtimelisDefa
                    where repairbase.isdelete == 0 &&repairbase.createuser==userinfo.usercode
                    orderby repairbase.createtime descending
                    select new
                    {
                        repairbase.id,
                        repairbase.wocode,
                        repairbase.projectid,
                        projectname = projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
                        repairbase.protypeid,
                            //protypename = dic_protypeDefa.Count() > 0 ? dic_protypeDefa.First().name : null,
                            repairbase.faulttypeid,
                            //faulttypename = dic_faulttypeDefa.Count() > 0 ? dic_faulttypeDefa.First().name : null,
                            repairbase.systemid,
                            //systemname = dic_systemDefa.Count() > 0 ? dic_systemDefa.First().name : null,
                            repairbase.equipmentid,
                            //equipmentname = dic_equDefa.Count() > 0 ? dic_equDefa.First().equipment_name : null,
                            repairbase.channel,
                        provincecode = repairbase.province,
                        province = sys_provincesDefa.Count() > 0 ? sys_provincesDefa.First().provincename : null,
                        citycode = repairbase.city,
                        city = sys_cityDefa.Count() > 0 ? sys_cityDefa.First().cityname : null,
                        repairbase.buildid,
                            //buildname = bldDefa.Count() > 0 ? bldDefa.First().buildname : null,
                            repairbase.floorid,
                            //floorname = flrDefa.Count() > 0 ? flrDefa.First().floorname : null,
                            repairbase.repairreplace,
                        repairbase.repairoom,
                        repairbase.repairtime,
                        repairbase.contactway,
                        repairbase.repairman,
                        repairbase.faultdescripe,
                        repairbase.faultpic,
                        repairbase.appointtime,
                        repairbase.wostate,
                        repairbase.createtime,
                        repairbase.createuser,
                        repairbase.createusername,
                        repairbase.touser,
                        repairbase.tousername,
                        repairbase.totime,
                        repairbase.totype,
                        repairbase.dealuser,
                        repairbase.dealusername,
                        repairbase.dealtime,
                        repairbase.ismaterials,
                        repairbase.materials,
                        repairbase.islosematerials,
                        repairbase.losematerials,
                        repairbase.hanguptime,
                        repairbase.arrivetime,
                        repairbase.evaluatetime,
                        repairbase.canceltime,
                        repairbase.score,
                        repairbase.Jwocode,
                        repairbase.Pwcode,
                        repairbase.CLpeoplefrom,
                        repairbase.tobztimes,
                        repairbase.wxtimeid,
                        wxtimeleths = wxtimelisDefa.Count() > 0 ? wxtimelisDefa.First().timecount : 0,//维修时长
                    };
                #endregion



                #region 查询条件
                if (!string.IsNullOrEmpty(phone))
                    query = query.Where(it => it.contactway.Contains(phone) || it.wocode.Contains(phone)
                            || it.repairreplace.Contains(phone) || it.repairoom.Contains(phone)
                            || it.Jwocode.Contains(phone) || it.createusername.Contains(phone)
                            || it.repairman.Contains(phone) || it.faultdescripe.Contains(phone)
                            || it.CLpeoplefrom.Contains(phone)
                            );
                if (!string.IsNullOrEmpty(wocode))
                    query = query.Where(it => it.wocode.Contains(wocode));
                if (!string.IsNullOrEmpty(province))
                    query = query.Where(it => it.provincecode == province);
                if (!string.IsNullOrEmpty(city))
                    query = query.Where(it => it.citycode == city);
                if (!string.IsNullOrEmpty(projectid))
                    query = query.Where(it => it.projectid == projectid);
                if (!string.IsNullOrEmpty(protypeid))
                    query = query.Where(it => it.protypeid == protypeid);
                if (!string.IsNullOrEmpty(buildid))
                    query = query.Where(it => it.buildid == buildid);
                if (!string.IsNullOrEmpty(floorid))
                    query = query.Where(it => it.floorid == floorid);
                if (!string.IsNullOrEmpty(replace))
                    query = query.Where(it => it.repairreplace.Contains(replace) || it.repairoom.Contains(replace));
                if (!string.IsNullOrEmpty(systemid))
                    query = query.Where(it => it.systemid == systemid);
                if (!string.IsNullOrEmpty(equipmentid))
                    query = query.Where(it => it.equipmentid == equipmentid);
                if (!string.IsNullOrEmpty(channel))
                    query = query.Where(it => it.channel == Convert.ToInt32(channel));
                if (!string.IsNullOrEmpty(Jwocode))
                    query = query.Where(it => it.Jwocode == Jwocode);
                #region 时间搜索
                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
                #endregion
                totalcount = query.Count();
                wzpcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.create);
                yjdcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.assign);
                dpjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.deal);
                ygqcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.hangup);
                ywjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.evaluate);
                yqxcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.cancle);

                if (state != -1)
                {
                    query = query.Where(it => it.wostate == state);
                    switch (state)
                    {
                        case (int)EnumWorkOrderState.assign: query = query.OrderByDescending(p => p.totime); break;
                        case (int)EnumWorkOrderState.deal: query = query.OrderByDescending(p => p.dealtime); break;
                        case (int)EnumWorkOrderState.hangup: query = query.OrderByDescending(p => p.hanguptime); break;
                    }
                }

                if (wxstate != -1)
                {
                    if (wxstate == 1)
                    {
                        query = query.Where(it => it.wostate <= wxstate);
                    }
                    else
                    {
                        query = query.Where(it => it.wostate == wxstate);
                    }
                    switch (wxstate)
                    {
                        case (int)EnumWorkOrderState.assign: query = query.OrderByDescending(p => p.totime); break;
                        case (int)EnumWorkOrderState.deal: query = query.OrderByDescending(p => p.dealtime); break;
                        case (int)EnumWorkOrderState.hangup: query = query.OrderByDescending(p => p.hanguptime); break;
                    }
                }

                    recordcount = query.Count();
                    //总数
                    //totalcount = query.Count();
                    var list = query.Skip((pageindex - 1) * pagesize).Take(pagesize);
                    return list;


            }

            //if (userinfo.role_code == "HFB" || userinfo.role_code == "XTWH")
            //{
            //    #region 护士长权限
            //    #region 数据读取
            //    var query =
            //        from repairbase in _collection.AsQueryable()
            //        join proje in _collection_pro_project_info.AsQueryable() on repairbase.projectid equals proje.id into projeDefa
            //        join sysprovinces in _collection_sys_provinces.AsQueryable() on repairbase.province equals sysprovinces.provincecode into sys_provincesDefa
            //        join syscity in _collection_sys_city.AsQueryable() on repairbase.city equals syscity.citycode into sys_cityDefa
            //        join wxtimelis in _collection_ewxwpstime.AsQueryable() on repairbase.wxtimeid equals wxtimelis.id into wxtimelisDefa
            //        where repairbase.isdelete == 0
            //        orderby repairbase.createtime descending
            //        select new
            //        {
            //            repairbase.id,
            //            repairbase.wocode,
            //            repairbase.projectid,
            //            projectname = projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
            //            repairbase.protypeid,
            //        //protypename = dic_protypeDefa.Count() > 0 ? dic_protypeDefa.First().name : null,
            //        repairbase.faulttypeid,
            //        //faulttypename = dic_faulttypeDefa.Count() > 0 ? dic_faulttypeDefa.First().name : null,
            //        repairbase.systemid,
            //        //systemname = dic_systemDefa.Count() > 0 ? dic_systemDefa.First().name : null,
            //        repairbase.equipmentid,
            //        //equipmentname = dic_equDefa.Count() > 0 ? dic_equDefa.First().equipment_name : null,
            //        repairbase.channel,
            //            provincecode = repairbase.province,
            //            province = sys_provincesDefa.Count() > 0 ? sys_provincesDefa.First().provincename : null,
            //            citycode = repairbase.city,
            //            city = sys_cityDefa.Count() > 0 ? sys_cityDefa.First().cityname : null,
            //            repairbase.buildid,
            //        //buildname = bldDefa.Count() > 0 ? bldDefa.First().buildname : null,
            //        repairbase.floorid,
            //        //floorname = flrDefa.Count() > 0 ? flrDefa.First().floorname : null,
            //        repairbase.repairreplace,
            //            repairbase.repairoom,
            //            repairbase.repairtime,
            //            repairbase.contactway,
            //            repairbase.repairman,
            //            repairbase.faultdescripe,
            //            repairbase.faultpic,
            //            repairbase.appointtime,
            //            repairbase.wostate,
            //            repairbase.createtime,
            //            repairbase.createuser,
            //            repairbase.createusername,
            //            repairbase.touser,
            //            repairbase.tousername,
            //            repairbase.totime,
            //            repairbase.totype,
            //            repairbase.dealuser,
            //            repairbase.dealusername,
            //            repairbase.dealtime,
            //            repairbase.ismaterials,
            //            repairbase.materials,
            //            repairbase.islosematerials,
            //            repairbase.losematerials,
            //            repairbase.hanguptime,
            //            repairbase.arrivetime,
            //            repairbase.evaluatetime,
            //            repairbase.canceltime,
            //            repairbase.score,
            //            repairbase.Jwocode,
            //            repairbase.Pwcode,
            //            repairbase.CLpeoplefrom,
            //            repairbase.tobztimes,
            //            repairbase.wxtimeid,
            //            wxtimeleths = wxtimelisDefa.Count() > 0 ? wxtimelisDefa.First().timecount : 0,//维修时长
            //    };
            //    #endregion
            //    #region 查询条件
            //    if (!string.IsNullOrEmpty(phone))
            //        query = query.Where(it => it.contactway.Contains(phone) || it.wocode.Contains(phone)
            //                || it.repairreplace.Contains(phone) || it.repairoom.Contains(phone)
            //                || it.Jwocode.Contains(phone) || it.createusername.Contains(phone)
            //                || it.repairman.Contains(phone) || it.faultdescripe.Contains(phone)
            //                || it.CLpeoplefrom.Contains(phone)
            //                );
            //    if (!string.IsNullOrEmpty(wocode))
            //        query = query.Where(it => it.wocode.Contains(wocode));
            //    if (!string.IsNullOrEmpty(province))
            //        query = query.Where(it => it.provincecode == province);
            //    if (!string.IsNullOrEmpty(city))
            //        query = query.Where(it => it.citycode == city);
            //    if (!string.IsNullOrEmpty(projectid))
            //        query = query.Where(it => it.projectid == projectid);
            //    if (!string.IsNullOrEmpty(protypeid))
            //        query = query.Where(it => it.protypeid == protypeid);
            //    if (!string.IsNullOrEmpty(buildid))
            //        query = query.Where(it => it.buildid == buildid);
            //    if (!string.IsNullOrEmpty(floorid))
            //        query = query.Where(it => it.floorid == floorid);
            //    if (!string.IsNullOrEmpty(replace))
            //        query = query.Where(it => it.repairreplace.Contains(replace) || it.repairoom.Contains(replace));
            //    if (!string.IsNullOrEmpty(systemid))
            //        query = query.Where(it => it.systemid == systemid);
            //    if (!string.IsNullOrEmpty(equipmentid))
            //        query = query.Where(it => it.equipmentid == equipmentid);
            //    if (!string.IsNullOrEmpty(channel))
            //        query = query.Where(it => it.channel == Convert.ToInt32(channel));
            //    if (!string.IsNullOrEmpty(Jwocode))
            //        query = query.Where(it => it.Jwocode == Jwocode);
            //    #region 时间搜索
            //    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 (userinfo.type == 2)//查看自己创建的工单
            //    {
            //        query = query.Where(it => it.createuser == userinfo.usercode);
            //    }
            //    else
            //    {
            //        if (!string.IsNullOrEmpty(usercode)) query = query.Where(it => (it.touser.Contains(usercode) || it.dealuser.Contains(usercode)));
            //    }

            //   wzpcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.create);
            //   yjdcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.assign);
            //   dpjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.deal);
            //   ygqcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.hangup);
            //   ywjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.evaluate);
            //   yqxcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.cancle);


            //    if (state != -1)
            //    {
            //        query = query.Where(it => it.wostate == state);
            //        switch (state)
            //        {
            //            case (int)EnumWorkOrderState.assign: query = query.OrderByDescending(p => p.totime); break;
            //            case (int)EnumWorkOrderState.deal: query = query.OrderByDescending(p => p.dealtime); break;
            //            //case (int)EnumWorkOrderState.evaluate: query = query.OrderBy(p => p.evaluatetime); break;
            //            case (int)EnumWorkOrderState.hangup: query = query.OrderByDescending(p => p.hanguptime); break;
            //                //case (int)EnumWorkOrderState.cancle: query = query.OrderBy(p => p.canceltime); break;
            //        }
            //    }

            //    if (wxstate != -1)
            //    {
            //        if (wxstate == 1)
            //        {
            //            query = query.Where(it => it.wostate <= wxstate);
            //        }
            //        else
            //        {
            //            query = query.Where(it => it.wostate == wxstate);
            //        }
            //        switch (wxstate)
            //        {
            //            case (int)EnumWorkOrderState.assign: query = query.OrderByDescending(p => p.totime); break;
            //            case (int)EnumWorkOrderState.deal: query = query.OrderByDescending(p => p.dealtime); break;
            //            //case (int)EnumWorkOrderState.evaluate: query = query.OrderBy(p => p.evaluatetime); break;
            //            case (int)EnumWorkOrderState.hangup: query = query.OrderByDescending(p => p.hanguptime); break;
            //                //case (int)EnumWorkOrderState.cancle: query = query.OrderBy(p => p.canceltime); break;
            //        }
            //    }
            //    #endregion
            //    #endregion
            //    recordcount = query.Count();
            //    //总数
            //    totalcount = query.Count();
            //    var list = query.Skip((pageindex - 1) * pagesize).Take(pagesize);
            //    return list;
            //    #endregion
            //}
            //else
            //{
            //    var userpolist = userinfo.postlist;
            //    if (userinfo.remark == "组长")
            //    {
            //        #region 护士长权限
            //        #region 数据读取
            //        var query =
            //            from repairbase in _collection.AsQueryable()
            //            join proje in _collection_pro_project_info.AsQueryable() on repairbase.projectid equals proje.id into projeDefa
            //            join sysprovinces in _collection_sys_provinces.AsQueryable() on repairbase.province equals sysprovinces.provincecode into sys_provincesDefa
            //            join syscity in _collection_sys_city.AsQueryable() on repairbase.city equals syscity.citycode into sys_cityDefa
            //            join wxtimelis in _collection_ewxwpstime.AsQueryable() on repairbase.wxtimeid equals wxtimelis.id into wxtimelisDefa
            //            join userinfolis in _collection_sys_user_account.AsQueryable() on repairbase.createuser equals userinfolis.usercode

            //            where repairbase.isdelete == 0 //&& userinfolis.delete_flag == false
            //            orderby repairbase.createtime descending
            //            select new
            //            {
            //                repairbase.id,
            //                repairbase.wocode,
            //                repairbase.projectid,
            //                projectname = projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
            //                repairbase.protypeid,
            //            //protypename = dic_protypeDefa.Count() > 0 ? dic_protypeDefa.First().name : null,
            //            repairbase.faulttypeid,
            //            //faulttypename = dic_faulttypeDefa.Count() > 0 ? dic_faulttypeDefa.First().name : null,
            //            repairbase.systemid,
            //            //systemname = dic_systemDefa.Count() > 0 ? dic_systemDefa.First().name : null,
            //            repairbase.equipmentid,
            //            //equipmentname = dic_equDefa.Count() > 0 ? dic_equDefa.First().equipment_name : null,
            //            repairbase.channel,
            //                provincecode = repairbase.province,
            //                province = sys_provincesDefa.Count() > 0 ? sys_provincesDefa.First().provincename : null,
            //                citycode = repairbase.city,
            //                city = sys_cityDefa.Count() > 0 ? sys_cityDefa.First().cityname : null,
            //                repairbase.buildid,
            //            //buildname = bldDefa.Count() > 0 ? bldDefa.First().buildname : null,
            //            repairbase.floorid,
            //            //floorname = flrDefa.Count() > 0 ? flrDefa.First().floorname : null,
            //            repairbase.repairreplace,
            //                repairbase.repairoom,
            //                repairbase.repairtime,
            //                repairbase.contactway,
            //                repairbase.repairman,
            //                repairbase.faultdescripe,
            //                repairbase.faultpic,
            //                repairbase.appointtime,
            //                repairbase.wostate,
            //                repairbase.createtime,
            //                repairbase.createuser,
            //                repairbase.createusername,
            //                repairbase.touser,
            //                repairbase.tousername,
            //                repairbase.totime,
            //                repairbase.totype,
            //                repairbase.dealuser,
            //                repairbase.dealusername,
            //                repairbase.dealtime,
            //                repairbase.ismaterials,
            //                repairbase.materials,
            //                repairbase.islosematerials,
            //                repairbase.losematerials,
            //                repairbase.hanguptime,
            //                repairbase.arrivetime,
            //                repairbase.evaluatetime,
            //                repairbase.canceltime,
            //                repairbase.score,
            //                repairbase.Jwocode,
            //                repairbase.Pwcode,
            //                repairbase.CLpeoplefrom,
            //                repairbase.tobztimes,
            //                remark = userinfolis.remark,//备注护士长
            //            temid = userinfolis.team_id,
            //                postlist = userinfolis.postlist,
            //                repairbase.wxtimeid,
            //                wxtimeleths = wxtimelisDefa.Count() > 0 ? wxtimelisDefa.First().timecount : 0,//维修时长
            //        };
            //        #endregion
            //        #region 查询条件
            //        if (!string.IsNullOrEmpty(phone))
            //            query = query.Where(it => it.contactway.Contains(phone) || it.wocode.Contains(phone)
            //                    || it.repairreplace.Contains(phone) || it.repairoom.Contains(phone)
            //                    || it.Jwocode.Contains(phone) || it.createusername.Contains(phone)
            //                    || it.repairman.Contains(phone) || it.faultdescripe.Contains(phone)
            //                    || it.CLpeoplefrom.Contains(phone)
            //                    );
            //        if (!string.IsNullOrEmpty(province))
            //            query = query.Where(it => it.provincecode == province);
            //        if (!string.IsNullOrEmpty(city))
            //            query = query.Where(it => it.citycode == city);
            //        if (!string.IsNullOrEmpty(projectid))
            //            query = query.Where(it => it.projectid == projectid);
            //        if (!string.IsNullOrEmpty(protypeid))
            //            query = query.Where(it => it.protypeid == protypeid);
            //        if (!string.IsNullOrEmpty(buildid))
            //            query = query.Where(it => it.buildid == buildid);
            //        if (!string.IsNullOrEmpty(floorid))
            //            query = query.Where(it => it.floorid == floorid);
            //        if (!string.IsNullOrEmpty(systemid))
            //            query = query.Where(it => it.systemid == systemid);
            //        if (!string.IsNullOrEmpty(equipmentid))
            //            query = query.Where(it => it.equipmentid == equipmentid);
            //        if (!string.IsNullOrEmpty(channel))
            //            query = query.Where(it => it.channel == Convert.ToInt32(channel));
            //        #region 时间搜索
            //        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"));

            //        var f = from p in query select p.postlist;
            //        //if (userinfo.type == 2)//查看自己创建的工单
            //        //{
            //        //    query = query.Where(it => it.createuser == userinfo.usercode);
            //        //}
            //        ////else
            //        ////{
            //        ////    if (!string.IsNullOrEmpty(usercode)) query = query.Where(it => (it.touser.Contains(usercode) || it.dealuser.Contains(usercode)));
            //        ////}
            //        if (userinfo.role_name.Contains("班") || userinfo.role_name == "默认角色" || userinfo.role_name=="后勤部" || userinfo.role_name=="信息科" || userinfo.role_name=="设备科")
            //        {
            //            //去掉这个判断
            //            if (userinfo.remark == "组长")
            //            {

            //                wzpcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.create && (p.postlist == userinfo.postlist || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
            //                yjdcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.assign && (p.postlist == userinfo.postlist || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));//
            //                dpjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.deal && (p.postlist == userinfo.postlist || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));//
            //                ygqcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.hangup && (p.postlist == userinfo.postlist || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));//
            //                ywjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.evaluate && (p.postlist == userinfo.postlist | p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));//|| 
            //                yqxcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.cancle && (p.postlist == userinfo.postlist || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));//|| 


            //                //wzpcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.create && (p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
            //                //yjdcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.assign && (p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));//
            //                //dpjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.deal && (p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));//
            //                //ygqcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.hangup && (p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));//
            //                //ywjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.evaluate && (p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));//|| 
            //                //yqxcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.cancle && (p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));//|| 

            //            }
            //            else
            //            {
            //                wzpcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.create && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
            //                yjdcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.assign && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
            //                dpjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.deal && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
            //                ygqcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.hangup && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
            //                ywjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.evaluate && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
            //                yqxcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.cancle && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));

            //        }


            //    }
            //        else //全部的工单
            //        {
            //            wzpcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.create);
            //            yjdcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.assign);
            //            dpjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.deal);
            //            ygqcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.hangup);
            //            ywjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.evaluate);
            //            yqxcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.cancle);
            //        }



            //        if (state != -1)
            //        {
            //            query = query.Where(it => it.wostate == state);

            //            if (userinfo.role_name.Contains("班") || userinfo.role_name == "默认角色" || userinfo.role_name == "后勤部" || userinfo.role_name == "信息科" || userinfo.role_name == "设备科")
            //            {
            //                //0602zhushi
            //                //if (userinfo.remark == "组长")
            //                //{
            //                //    query = query.Where(it =>  it.repairman.Contains(userinfo.usercode) || it.repairman == userinfo.username || it.postlist == userinfo.postlist);// 

            //                //}
            //                //else
            //                //{
            //                //    if (!string.IsNullOrEmpty(userinfo.username))
            //                //    {
            //                //        query = query.Where(it => it.createusername == userinfo.username || it.tousername[0] == userinfo.username || it.repairman == userinfo.username || it.repairman.Contains(userinfo.usercode));
            //                //    }
            //                //}

            //                if (!string.IsNullOrEmpty(userinfo.username))
            //                {
            //                    query = query.Where(it => it.createusername == userinfo.username || it.tousername[0] == userinfo.username || it.repairman == userinfo.username || it.repairman.Contains(userinfo.usercode));
            //                }


            //            }
            //            switch (state)
            //            {
            //                case (int)EnumWorkOrderState.assign: query = query.OrderByDescending(p => p.totime); break;
            //                case (int)EnumWorkOrderState.deal: query = query.OrderByDescending(p => p.dealtime); break;
            //                //case (int)EnumWorkOrderState.evaluate: query = query.OrderBy(p => p.evaluatetime); break;
            //                case (int)EnumWorkOrderState.hangup: query = query.OrderByDescending(p => p.hanguptime); break;
            //                    //case (int)EnumWorkOrderState.cancle: query = query.OrderBy(p => p.canceltime); break;
            //            }
            //        }
            //        else
            //        {
            //            //query = query.Where(it => it.wostate==0|| it.wostate == 1||it.wostate == 2|| it.wostate == 3|| it.wostate == 7|| it.wostate == 9);
            //            if (userinfo.role_name.Contains("班") || userinfo.role_name == "默认角色" || userinfo.role_name == "后勤部" || userinfo.role_name == "信息科" || userinfo.role_name == "设备科")
            //            {
            //                //if (userinfo.remark == "组长")
            //                //{
            //                //    query = query.Where(it => it.postlist == userinfo.postlist || it.repairman == userinfo.username || it.repairman.Contains(userinfo.usercode));//  
            //                //   // query = query.Where(it =>  it.repairman == userinfo.username || it.repairman.Contains(userinfo.usercode));//  

            //                //}
            //                //else
            //                //{
            //                //    if (!string.IsNullOrEmpty(userinfo.username))
            //                //    {
            //                //        query = query.Where(it => it.createusername == userinfo.username || it.tousername[0] == userinfo.username || it.repairman == userinfo.username || it.repairman.Contains(userinfo.usercode));
            //                //    }
            //                //}

            //                if (!string.IsNullOrEmpty(userinfo.username))
            //                {
            //                    query = query.Where(it => it.createusername == userinfo.username || it.tousername[0] == userinfo.username || it.repairman == userinfo.username || it.repairman.Contains(userinfo.usercode));
            //                }
            //            }
            //            query = query.OrderByDescending(p => p.totime);
            //        }

            //        #endregion
            //        #endregion
            //        recordcount = query.Count();
            //        //总数
            //        totalcount = query.Count();
            //        var list = query.Skip((pageindex - 1) * pagesize).Take(pagesize);
            //        return list;
            //        #endregion
            //    }
            //    else
            //    {
            //         #region 不是护士长的时候
            //    #region 数据读取
            //    var query =
            //        from repairbase in _collection.AsQueryable()
            //        join proje in _collection_pro_project_info.AsQueryable() on repairbase.projectid equals proje.id into projeDefa
            //        join sysprovinces in _collection_sys_provinces.AsQueryable() on repairbase.province equals sysprovinces.provincecode into sys_provincesDefa
            //        join syscity in _collection_sys_city.AsQueryable() on repairbase.city equals syscity.citycode into sys_cityDefa
            //        join wxtimelis in _collection_ewxwpstime.AsQueryable() on repairbase.wxtimeid equals wxtimelis.id into wxtimelisDefa

            //        where repairbase.isdelete == 0 
            //        orderby repairbase.createtime descending
            //        select new
            //        {
            //            repairbase.id,
            //            repairbase.wocode,
            //            repairbase.projectid,
            //            projectname = projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
            //            repairbase.protypeid,
            //        //protypename = dic_protypeDefa.Count() > 0 ? dic_protypeDefa.First().name : null,
            //        repairbase.faulttypeid,
            //        //faulttypename = dic_faulttypeDefa.Count() > 0 ? dic_faulttypeDefa.First().name : null,
            //        repairbase.systemid,
            //        //systemname = dic_systemDefa.Count() > 0 ? dic_systemDefa.First().name : null,
            //        repairbase.equipmentid,
            //        //equipmentname = dic_equDefa.Count() > 0 ? dic_equDefa.First().equipment_name : null,
            //        repairbase.channel,
            //            provincecode = repairbase.province,
            //            province = sys_provincesDefa.Count() > 0 ? sys_provincesDefa.First().provincename : null,
            //            citycode = repairbase.city,
            //            city = sys_cityDefa.Count() > 0 ? sys_cityDefa.First().cityname : null,
            //            repairbase.buildid,
            //        //buildname = bldDefa.Count() > 0 ? bldDefa.First().buildname : null,
            //        repairbase.floorid,
            //        //floorname = flrDefa.Count() > 0 ? flrDefa.First().floorname : null,
            //        repairbase.repairreplace,
            //            repairbase.repairoom,
            //            repairbase.repairtime,
            //            repairbase.contactway,
            //            repairbase.repairman,
            //            repairbase.faultdescripe,
            //            repairbase.faultpic,
            //            repairbase.appointtime,
            //            repairbase.wostate,
            //            repairbase.createtime,
            //            repairbase.createuser,
            //            repairbase.createusername,
            //            repairbase.touser,
            //            repairbase.tousername,
            //            repairbase.totime,
            //            repairbase.totype,
            //            repairbase.dealuser,
            //            repairbase.dealusername,
            //            repairbase.dealtime,
            //            repairbase.ismaterials,
            //            repairbase.materials,
            //            repairbase.islosematerials,
            //            repairbase.losematerials,
            //            repairbase.hanguptime,
            //            repairbase.arrivetime,
            //            repairbase.evaluatetime,
            //            repairbase.canceltime,
            //            repairbase.score,
            //            repairbase.Jwocode,
            //            repairbase.Pwcode,
            //            repairbase.CLpeoplefrom,
            //            repairbase.tobztimes,
            //            repairbase.wxtimeid,
            //            wxtimeleths = wxtimelisDefa.Count() > 0 ? wxtimelisDefa.First().timecount : 0,//维修时长
            //    };
            //    #endregion
            //    #region 查询条件
            //    if (!string.IsNullOrEmpty(phone))
            //        query = query.Where(it => it.contactway.Contains(phone) || it.wocode.Contains(phone)
            //                || it.repairreplace.Contains(phone) || it.repairoom.Contains(phone)
            //                || it.Jwocode.Contains(phone) || it.createusername.Contains(phone)
            //                || it.repairman.Contains(phone) || it.faultdescripe.Contains(phone)
            //                || it.CLpeoplefrom.Contains(phone)
            //                );
            //    if (!string.IsNullOrEmpty(wocode))
            //        query = query.Where(it => it.wocode.Contains(wocode));
            //    if (!string.IsNullOrEmpty(province))
            //        query = query.Where(it => it.provincecode == province);
            //    if (!string.IsNullOrEmpty(city))
            //        query = query.Where(it => it.citycode == city);
            //    if (!string.IsNullOrEmpty(projectid))
            //        query = query.Where(it => it.projectid == projectid);
            //    if (!string.IsNullOrEmpty(protypeid))
            //        query = query.Where(it => it.protypeid == protypeid);
            //    if (!string.IsNullOrEmpty(buildid))
            //        query = query.Where(it => it.buildid == buildid);
            //    if (!string.IsNullOrEmpty(floorid))
            //        query = query.Where(it => it.floorid == floorid);
            //    if (!string.IsNullOrEmpty(replace))
            //        query = query.Where(it => it.repairreplace.Contains(replace) || it.repairoom.Contains(replace));
            //    if (!string.IsNullOrEmpty(systemid))
            //        query = query.Where(it => it.systemid == systemid);
            //    if (!string.IsNullOrEmpty(equipmentid))
            //        query = query.Where(it => it.equipmentid == equipmentid);
            //    if (!string.IsNullOrEmpty(channel))
            //        query = query.Where(it => it.channel == Convert.ToInt32(channel));
            //    if (!string.IsNullOrEmpty(Jwocode))
            //        query = query.Where(it => it.Jwocode == Jwocode);
            //    #region 时间搜索
            //    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 (userinfo.type == 2)//查看自己创建的工单
            //    {
            //        query = query.Where(it => it.createuser == userinfo.usercode);
            //    }
            //    else
            //    {
            //        if (!string.IsNullOrEmpty(usercode)) query = query.Where(it => (it.touser.Contains(usercode) || it.dealuser.Contains(usercode)));
            //    }
            //        if (userinfo.role_name.Contains("班") || userinfo.role_name == "默认角色" || userinfo.role_name == "后勤部" || userinfo.role_name == "信息科" || userinfo.role_name == "设备科")
            //        {


            //            wzpcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.create && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
            //            yjdcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.assign && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
            //            dpjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.deal && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
            //            ygqcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.hangup && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
            //            ywjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.evaluate && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
            //            yqxcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.cancle && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));


            //        }
            //        else
            //        {
            //            wzpcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.create);
            //            yjdcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.assign);
            //            dpjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.deal);
            //            ygqcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.hangup);
            //            ywjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.evaluate);
            //            yqxcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.cancle);
            //        }



            //        if (state != -1)
            //    {
            //        query = query.Where(it => it.wostate == state);

            //        if (userinfo.role_name.Contains("班") || userinfo.role_name == "默认角色" || userinfo.role_name == "后勤部" || userinfo.role_name == "信息科" || userinfo.role_name == "设备科")
            //            {

            //                if (!string.IsNullOrEmpty(userinfo.username))
            //                {
            //                    query = query.Where(it => it.createusername == userinfo.username || it.tousername[0] == userinfo.username || it.repairman == userinfo.username || it.repairman.Contains(userinfo.usercode));
            //                }


            //        }
            //        switch (state)
            //        {
            //            case (int)EnumWorkOrderState.assign: query = query.OrderByDescending(p => p.totime); break;
            //            case (int)EnumWorkOrderState.deal: query = query.OrderByDescending(p => p.dealtime); break;
            //            //case (int)EnumWorkOrderState.evaluate: query = query.OrderBy(p => p.evaluatetime); break;
            //            case (int)EnumWorkOrderState.hangup: query = query.OrderByDescending(p => p.hanguptime); break;
            //                //case (int)EnumWorkOrderState.cancle: query = query.OrderBy(p => p.canceltime); break;
            //        }
            //    }
            //    else
            //    {
            //        if (userinfo.role_name.Contains("班") || userinfo.role_name == "默认角色" || userinfo.role_name == "后勤部" || userinfo.role_name == "信息科" || userinfo.role_name == "设备科")
            //            {

            //                //20210531注释 if
            //                //if (!string.IsNullOrEmpty(userinfo.username))
            //                //{
            //                //    query = query.Where(it => it.createusername == userinfo.username || it.tousername[0] == userinfo.username || it.repairman == userinfo.username || it.repairman.Contains(userinfo.usercode));
            //                //}
            //                query = query.Where(it => it.createusername == userinfo.username);

            //            }
            //    }
            //    if (wxstate != -1)
            //    {
            //        if (wxstate == 1)
            //        {
            //            query = query.Where(it => it.wostate <= wxstate);
            //        }
            //        else
            //        {
            //            query = query.Where(it => it.wostate == wxstate);
            //        }
            //        switch (wxstate)
            //        {
            //            case (int)EnumWorkOrderState.assign: query = query.OrderByDescending(p => p.totime); break;
            //            case (int)EnumWorkOrderState.deal: query = query.OrderByDescending(p => p.dealtime); break;
            //            //case (int)EnumWorkOrderState.evaluate: query = query.OrderBy(p => p.evaluatetime); break;
            //            case (int)EnumWorkOrderState.hangup: query = query.OrderByDescending(p => p.hanguptime); break;
            //                //case (int)EnumWorkOrderState.cancle: query = query.OrderBy(p => p.canceltime); break;
            //        }
            //    }
            //    #endregion
            //    #endregion
            //    recordcount = query.Count();
            //    //总数
            //    totalcount = query.Count();
            //    var list = query.Skip((pageindex - 1) * pagesize).Take(pagesize);
            //    return list;
            //    #endregion
        //}
               
        //    }

        }

        public IEnumerable<object> excelwocodelhsz(string phone, string wocode, string province, string city, string projectid, string protypeid, string buildid, string floorid,
       string replace, string systemid, string equipmentid, string usercode, string stime, string etime, string channel, string Jwocode, int state, int wxstate,
       string nowusercode, UserAccountInfoModel userinfo,
       //string nowrolecode,
       int pageindex, int pagesize, out int recordcount,
       out int totalcount, out int wzpcount, out int yjdcount, out int dpjcount, out int ygqcount, out int ywjcount, out int yqxcount)
        {
            if (userinfo.role_code == "HFB" || userinfo.role_code == "XTWH")
            {
                #region 护士长权限
                #region 数据读取
                var query =
                    from repairbase in _collection.AsQueryable()
                    join proje in _collection_pro_project_info.AsQueryable() on repairbase.projectid equals proje.id into projeDefa
                    join sysprovinces in _collection_sys_provinces.AsQueryable() on repairbase.province equals sysprovinces.provincecode into sys_provincesDefa
                    join syscity in _collection_sys_city.AsQueryable() on repairbase.city equals syscity.citycode into sys_cityDefa
                    join wxtimelis in _collection_ewxwpstime.AsQueryable() on repairbase.wxtimeid equals wxtimelis.id into wxtimelisDefa
                    where repairbase.isdelete == 0
                    orderby repairbase.createtime descending
                    select new
                    {
                        repairbase.id,
                        repairbase.wocode,
                        repairbase.projectid,
                        projectname = projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
                        repairbase.protypeid,
                        //protypename = dic_protypeDefa.Count() > 0 ? dic_protypeDefa.First().name : null,
                        repairbase.faulttypeid,
                        //faulttypename = dic_faulttypeDefa.Count() > 0 ? dic_faulttypeDefa.First().name : null,
                        repairbase.systemid,
                        //systemname = dic_systemDefa.Count() > 0 ? dic_systemDefa.First().name : null,
                        repairbase.equipmentid,
                        //equipmentname = dic_equDefa.Count() > 0 ? dic_equDefa.First().equipment_name : null,
                        repairbase.channel,
                        provincecode = repairbase.province,
                        province = sys_provincesDefa.Count() > 0 ? sys_provincesDefa.First().provincename : null,
                        citycode = repairbase.city,
                        city = sys_cityDefa.Count() > 0 ? sys_cityDefa.First().cityname : null,
                        repairbase.buildid,
                        //buildname = bldDefa.Count() > 0 ? bldDefa.First().buildname : null,
                        repairbase.floorid,
                        //floorname = flrDefa.Count() > 0 ? flrDefa.First().floorname : null,
                        repairbase.repairreplace,
                        repairbase.repairoom,
                        repairbase.repairtime,
                        repairbase.contactway,
                        repairbase.repairman,
                        repairbase.faultdescripe,
                        repairbase.faultpic,
                        repairbase.appointtime,
                        repairbase.wostate,
                        repairbase.createtime,
                        repairbase.createuser,
                        repairbase.createusername,
                        repairbase.touser,
                        repairbase.tousername,
                        repairbase.totime,
                        repairbase.totype,
                        repairbase.dealuser,
                        repairbase.dealusername,
                        repairbase.dealtime,
                        repairbase.ismaterials,
                        repairbase.materials,
                        repairbase.islosematerials,
                        repairbase.losematerials,
                        repairbase.hanguptime,
                        repairbase.arrivetime,
                        repairbase.evaluatetime,
                        repairbase.canceltime,
                        repairbase.score,
                        repairbase.Jwocode,
                        repairbase.Pwcode,
                        repairbase.CLpeoplefrom,
                        repairbase.tobztimes,
                        repairbase.wxtimeid,
                        wxtimeleths = wxtimelisDefa.Count() > 0 ? wxtimelisDefa.First().timecount : 0,//维修时长
                    };
                #endregion
                #region 查询条件
                if (!string.IsNullOrEmpty(phone))
                    query = query.Where(it => it.contactway.Contains(phone) || it.wocode.Contains(phone)
                            || it.repairreplace.Contains(phone) || it.repairoom.Contains(phone)
                            || it.Jwocode.Contains(phone) || it.createusername.Contains(phone)
                            || it.repairman.Contains(phone) || it.faultdescripe.Contains(phone)
                            || it.CLpeoplefrom.Contains(phone)
                            );
                if (!string.IsNullOrEmpty(wocode))
                    query = query.Where(it => it.wocode.Contains(wocode));
                if (!string.IsNullOrEmpty(province))
                    query = query.Where(it => it.provincecode == province);
                if (!string.IsNullOrEmpty(city))
                    query = query.Where(it => it.citycode == city);
                if (!string.IsNullOrEmpty(projectid))
                    query = query.Where(it => it.projectid == projectid);
                if (!string.IsNullOrEmpty(protypeid))
                    query = query.Where(it => it.protypeid == protypeid);
                if (!string.IsNullOrEmpty(buildid))
                    query = query.Where(it => it.buildid == buildid);
                if (!string.IsNullOrEmpty(floorid))
                    query = query.Where(it => it.floorid == floorid);
                if (!string.IsNullOrEmpty(replace))
                    query = query.Where(it => it.repairreplace.Contains(replace) || it.repairoom.Contains(replace));
                if (!string.IsNullOrEmpty(systemid))
                    query = query.Where(it => it.systemid == systemid);
                if (!string.IsNullOrEmpty(equipmentid))
                    query = query.Where(it => it.equipmentid == equipmentid);
                if (!string.IsNullOrEmpty(channel))
                    query = query.Where(it => it.channel == Convert.ToInt32(channel));
                if (!string.IsNullOrEmpty(Jwocode))
                    query = query.Where(it => it.Jwocode == Jwocode);
                #region 时间搜索
                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 (userinfo.type == 2)//查看自己创建的工单
                {
                    query = query.Where(it => it.createuser == userinfo.usercode);
                }
                else
                {
                    if (!string.IsNullOrEmpty(usercode)) query = query.Where(it => (it.touser.Contains(usercode) || it.dealuser.Contains(usercode)));
                }

                wzpcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.create);
                yjdcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.assign);
                dpjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.deal);
                ygqcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.hangup);
                ywjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.evaluate);
                yqxcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.cancle);


                if (state != -1)
                {
                    query = query.Where(it => it.wostate == state);
                    switch (state)
                    {
                        case (int)EnumWorkOrderState.assign: query = query.OrderByDescending(p => p.totime); break;
                        case (int)EnumWorkOrderState.deal: query = query.OrderByDescending(p => p.dealtime); break;
                        //case (int)EnumWorkOrderState.evaluate: query = query.OrderBy(p => p.evaluatetime); break;
                        case (int)EnumWorkOrderState.hangup: query = query.OrderByDescending(p => p.hanguptime); break;
                            //case (int)EnumWorkOrderState.cancle: query = query.OrderBy(p => p.canceltime); break;
                    }
                }

                if (wxstate != -1)
                {
                    if (wxstate == 1)
                    {
                        query = query.Where(it => it.wostate <= wxstate);
                    }
                    else
                    {
                        query = query.Where(it => it.wostate == wxstate);
                    }
                    switch (wxstate)
                    {
                        case (int)EnumWorkOrderState.assign: query = query.OrderByDescending(p => p.totime); break;
                        case (int)EnumWorkOrderState.deal: query = query.OrderByDescending(p => p.dealtime); break;
                        //case (int)EnumWorkOrderState.evaluate: query = query.OrderBy(p => p.evaluatetime); break;
                        case (int)EnumWorkOrderState.hangup: query = query.OrderByDescending(p => p.hanguptime); break;
                            //case (int)EnumWorkOrderState.cancle: query = query.OrderBy(p => p.canceltime); break;
                    }
                }
                #endregion
                #endregion
                recordcount = query.Count();
                //总数
                totalcount = query.Count();
                var list1 = query.ToList();
                return list1;
                #endregion
            }
            else
            {
                var userpolist = userinfo.postlist;
                if (userinfo.remark == "组长")
                {
                    #region 护士长权限
                    #region 数据读取
                    var query =
                        from repairbase in _collection.AsQueryable()
                        join proje in _collection_pro_project_info.AsQueryable() on repairbase.projectid equals proje.id into projeDefa
                        join sysprovinces in _collection_sys_provinces.AsQueryable() on repairbase.province equals sysprovinces.provincecode into sys_provincesDefa
                        join syscity in _collection_sys_city.AsQueryable() on repairbase.city equals syscity.citycode into sys_cityDefa
                        join wxtimelis in _collection_ewxwpstime.AsQueryable() on repairbase.wxtimeid equals wxtimelis.id into wxtimelisDefa
                        join userinfolis in _collection_sys_user_account.AsQueryable() on repairbase.createuser equals userinfolis.usercode

                        where repairbase.isdelete == 0 //&& userinfolis.delete_flag == false
                        orderby repairbase.createtime descending
                        select new
                        {
                            repairbase.id,
                            repairbase.wocode,
                            repairbase.projectid,
                            projectname = projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
                            repairbase.protypeid,
                            //protypename = dic_protypeDefa.Count() > 0 ? dic_protypeDefa.First().name : null,
                            repairbase.faulttypeid,
                            //faulttypename = dic_faulttypeDefa.Count() > 0 ? dic_faulttypeDefa.First().name : null,
                            repairbase.systemid,
                            //systemname = dic_systemDefa.Count() > 0 ? dic_systemDefa.First().name : null,
                            repairbase.equipmentid,
                            //equipmentname = dic_equDefa.Count() > 0 ? dic_equDefa.First().equipment_name : null,
                            repairbase.channel,
                            provincecode = repairbase.province,
                            province = sys_provincesDefa.Count() > 0 ? sys_provincesDefa.First().provincename : null,
                            citycode = repairbase.city,
                            city = sys_cityDefa.Count() > 0 ? sys_cityDefa.First().cityname : null,
                            repairbase.buildid,
                            //buildname = bldDefa.Count() > 0 ? bldDefa.First().buildname : null,
                            repairbase.floorid,
                            //floorname = flrDefa.Count() > 0 ? flrDefa.First().floorname : null,
                            repairbase.repairreplace,
                            repairbase.repairoom,
                            repairbase.repairtime,
                            repairbase.contactway,
                            repairbase.repairman,
                            repairbase.faultdescripe,
                            repairbase.faultpic,
                            repairbase.appointtime,
                            repairbase.wostate,
                            repairbase.createtime,
                            repairbase.createuser,
                            repairbase.createusername,
                            repairbase.touser,
                            repairbase.tousername,
                            repairbase.totime,
                            repairbase.totype,
                            repairbase.dealuser,
                            repairbase.dealusername,
                            repairbase.dealtime,
                            repairbase.ismaterials,
                            repairbase.materials,
                            repairbase.islosematerials,
                            repairbase.losematerials,
                            repairbase.hanguptime,
                            repairbase.arrivetime,
                            repairbase.evaluatetime,
                            repairbase.canceltime,
                            repairbase.score,
                            repairbase.Jwocode,
                            repairbase.Pwcode,
                            repairbase.CLpeoplefrom,
                            repairbase.tobztimes,
                            remark = userinfolis.remark,//备注护士长
                            temid = userinfolis.team_id,
                            postlist = userinfolis.postlist,
                            repairbase.wxtimeid,
                            wxtimeleths = wxtimelisDefa.Count() > 0 ? wxtimelisDefa.First().timecount : 0,//维修时长
                        };
                    #endregion
                    #region 查询条件
                    if (!string.IsNullOrEmpty(phone))
                        query = query.Where(it => it.contactway.Contains(phone) || it.wocode.Contains(phone)
                                || it.repairreplace.Contains(phone) || it.repairoom.Contains(phone)
                                || it.Jwocode.Contains(phone) || it.createusername.Contains(phone)
                                || it.repairman.Contains(phone) || it.faultdescripe.Contains(phone)
                                || it.CLpeoplefrom.Contains(phone)
                                );
                    if (!string.IsNullOrEmpty(province))
                        query = query.Where(it => it.provincecode == province);
                    if (!string.IsNullOrEmpty(city))
                        query = query.Where(it => it.citycode == city);
                    if (!string.IsNullOrEmpty(projectid))
                        query = query.Where(it => it.projectid == projectid);
                    if (!string.IsNullOrEmpty(protypeid))
                        query = query.Where(it => it.protypeid == protypeid);
                    if (!string.IsNullOrEmpty(buildid))
                        query = query.Where(it => it.buildid == buildid);
                    if (!string.IsNullOrEmpty(floorid))
                        query = query.Where(it => it.floorid == floorid);
                    if (!string.IsNullOrEmpty(systemid))
                        query = query.Where(it => it.systemid == systemid);
                    if (!string.IsNullOrEmpty(equipmentid))
                        query = query.Where(it => it.equipmentid == equipmentid);
                    if (!string.IsNullOrEmpty(channel))
                        query = query.Where(it => it.channel == Convert.ToInt32(channel));
                    #region 时间搜索
                    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 (userinfo.role_name.Contains("班") || userinfo.role_name == "默认角色" || userinfo.role_name == "后勤部" || userinfo.role_name == "信息科" || userinfo.role_name == "设备科")
                    {

                        if (userinfo.remark == "组长")
                        {
                            wzpcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.create && (p.postlist == userinfo.postlist || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
                            yjdcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.assign && (p.postlist == userinfo.postlist || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));//
                            dpjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.deal && (p.postlist == userinfo.postlist || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));//
                            ygqcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.hangup && (p.postlist == userinfo.postlist || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));//
                            ywjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.evaluate && (p.postlist == userinfo.postlist | p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));//|| 
                            yqxcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.cancle && (p.postlist == userinfo.postlist || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));//|| 

                        }
                        else
                        {
                            wzpcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.create && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
                            yjdcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.assign && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
                            dpjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.deal && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
                            ygqcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.hangup && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
                            ywjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.evaluate && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
                            yqxcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.cancle && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));

                        }


                    }
                    else
                    {
                        wzpcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.create);
                        yjdcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.assign);
                        dpjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.deal);
                        ygqcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.hangup);
                        ywjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.evaluate);
                        yqxcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.cancle);
                    }



                    if (state != -1)
                    {
                        query = query.Where(it => it.wostate == state);

                        if (userinfo.role_name.Contains("班") || userinfo.role_name == "默认角色" || userinfo.role_name == "后勤部" || userinfo.role_name == "信息科" || userinfo.role_name == "设备科")
                        {
                            if (userinfo.remark == "组长")
                            {
                                query = query.Where(it => it.repairman.Contains(userinfo.usercode) || it.repairman == userinfo.username || it.postlist == userinfo.postlist);// 

                            }
                            else
                            {
                                if (!string.IsNullOrEmpty(userinfo.username))
                                {
                                    query = query.Where(it => it.createusername == userinfo.username || it.tousername[0] == userinfo.username || it.repairman == userinfo.username || it.repairman.Contains(userinfo.usercode));
                                }
                            }


                        }
                        switch (state)
                        {
                            case (int)EnumWorkOrderState.assign: query = query.OrderByDescending(p => p.totime); break;
                            case (int)EnumWorkOrderState.deal: query = query.OrderByDescending(p => p.dealtime); break;
                            //case (int)EnumWorkOrderState.evaluate: query = query.OrderBy(p => p.evaluatetime); break;
                            case (int)EnumWorkOrderState.hangup: query = query.OrderByDescending(p => p.hanguptime); break;
                                //case (int)EnumWorkOrderState.cancle: query = query.OrderBy(p => p.canceltime); break;
                        }
                    }
                    else
                    {
                        //query = query.Where(it => it.wostate==0|| it.wostate == 1||it.wostate == 2|| it.wostate == 3|| it.wostate == 7|| it.wostate == 9);
                        if (userinfo.role_name.Contains("班") || userinfo.role_name == "默认角色" || userinfo.role_name == "后勤部" || userinfo.role_name == "信息科" || userinfo.role_name == "设备科")
                        {
                            if (userinfo.remark == "组长")
                            {
                                query = query.Where(it => it.postlist == userinfo.postlist || it.repairman == userinfo.username || it.repairman.Contains(userinfo.usercode));//  

                            }
                            else
                            {
                                if (!string.IsNullOrEmpty(userinfo.username))
                                {
                                    query = query.Where(it => it.createusername == userinfo.username || it.tousername[0] == userinfo.username || it.repairman == userinfo.username || it.repairman.Contains(userinfo.usercode));
                                }
                            }
                        }
                        query = query.OrderByDescending(p => p.totime);
                    }
                  
                    #endregion
                    #endregion
                    recordcount = query.Count();
                    //总数
                    totalcount = query.Count();
                    
                    var list1 = query.ToList();
                    return list1;
                    #endregion
                }
                else
                {
                    #region 不是护士长的时候
                    #region 数据读取
                    var query =
                        from repairbase in _collection.AsQueryable()
                        join proje in _collection_pro_project_info.AsQueryable() on repairbase.projectid equals proje.id into projeDefa
                        join sysprovinces in _collection_sys_provinces.AsQueryable() on repairbase.province equals sysprovinces.provincecode into sys_provincesDefa
                        join syscity in _collection_sys_city.AsQueryable() on repairbase.city equals syscity.citycode into sys_cityDefa
                        join wxtimelis in _collection_ewxwpstime.AsQueryable() on repairbase.wxtimeid equals wxtimelis.id into wxtimelisDefa

                        where repairbase.isdelete == 0
                        orderby repairbase.createtime descending
                        select new
                        {
                            repairbase.id,
                            repairbase.wocode,
                            repairbase.projectid,
                            projectname = projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
                            repairbase.protypeid,
                        //protypename = dic_protypeDefa.Count() > 0 ? dic_protypeDefa.First().name : null,
                        repairbase.faulttypeid,
                        //faulttypename = dic_faulttypeDefa.Count() > 0 ? dic_faulttypeDefa.First().name : null,
                        repairbase.systemid,
                        //systemname = dic_systemDefa.Count() > 0 ? dic_systemDefa.First().name : null,
                        repairbase.equipmentid,
                        //equipmentname = dic_equDefa.Count() > 0 ? dic_equDefa.First().equipment_name : null,
                        repairbase.channel,
                            provincecode = repairbase.province,
                            province = sys_provincesDefa.Count() > 0 ? sys_provincesDefa.First().provincename : null,
                            citycode = repairbase.city,
                            city = sys_cityDefa.Count() > 0 ? sys_cityDefa.First().cityname : null,
                            repairbase.buildid,
                        //buildname = bldDefa.Count() > 0 ? bldDefa.First().buildname : null,
                        repairbase.floorid,
                        //floorname = flrDefa.Count() > 0 ? flrDefa.First().floorname : null,
                        repairbase.repairreplace,
                            repairbase.repairoom,
                            repairbase.repairtime,
                            repairbase.contactway,
                            repairbase.repairman,
                            repairbase.faultdescripe,
                            repairbase.faultpic,
                            repairbase.appointtime,
                            repairbase.wostate,
                            repairbase.createtime,
                            repairbase.createuser,
                            repairbase.createusername,
                            repairbase.touser,
                            repairbase.tousername,
                            repairbase.totime,
                            repairbase.totype,
                            repairbase.dealuser,
                            repairbase.dealusername,
                            repairbase.dealtime,
                            repairbase.ismaterials,
                            repairbase.materials,
                            repairbase.islosematerials,
                            repairbase.losematerials,
                            repairbase.hanguptime,
                            repairbase.arrivetime,
                            repairbase.evaluatetime,
                            repairbase.canceltime,
                            repairbase.score,
                            repairbase.Jwocode,
                            repairbase.Pwcode,
                            repairbase.CLpeoplefrom,
                            repairbase.tobztimes,
                            repairbase.wxtimeid,
                            wxtimeleths = wxtimelisDefa.Count() > 0 ? wxtimelisDefa.First().timecount : 0,//维修时长
                    };
                    #endregion
                    #region 查询条件
                    if (!string.IsNullOrEmpty(phone))
                        query = query.Where(it => it.contactway.Contains(phone) || it.wocode.Contains(phone)
                                || it.repairreplace.Contains(phone) || it.repairoom.Contains(phone)
                                || it.Jwocode.Contains(phone) || it.createusername.Contains(phone)
                                || it.repairman.Contains(phone) || it.faultdescripe.Contains(phone)
                                || it.CLpeoplefrom.Contains(phone)
                                );
                    if (!string.IsNullOrEmpty(wocode))
                        query = query.Where(it => it.wocode.Contains(wocode));
                    if (!string.IsNullOrEmpty(province))
                        query = query.Where(it => it.provincecode == province);
                    if (!string.IsNullOrEmpty(city))
                        query = query.Where(it => it.citycode == city);
                    if (!string.IsNullOrEmpty(projectid))
                        query = query.Where(it => it.projectid == projectid);
                    if (!string.IsNullOrEmpty(protypeid))
                        query = query.Where(it => it.protypeid == protypeid);
                    if (!string.IsNullOrEmpty(buildid))
                        query = query.Where(it => it.buildid == buildid);
                    if (!string.IsNullOrEmpty(floorid))
                        query = query.Where(it => it.floorid == floorid);
                    if (!string.IsNullOrEmpty(replace))
                        query = query.Where(it => it.repairreplace.Contains(replace) || it.repairoom.Contains(replace));
                    if (!string.IsNullOrEmpty(systemid))
                        query = query.Where(it => it.systemid == systemid);
                    if (!string.IsNullOrEmpty(equipmentid))
                        query = query.Where(it => it.equipmentid == equipmentid);
                    if (!string.IsNullOrEmpty(channel))
                        query = query.Where(it => it.channel == Convert.ToInt32(channel));
                    if (!string.IsNullOrEmpty(Jwocode))
                        query = query.Where(it => it.Jwocode == Jwocode);
                    #region 时间搜索
                    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 (userinfo.type == 2)//查看自己创建的工单
                    {
                        query = query.Where(it => it.createuser == userinfo.usercode);
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(usercode)) query = query.Where(it => (it.touser.Contains(usercode) || it.dealuser.Contains(usercode)));
                    }
                    if (userinfo.role_name.Contains("班") || userinfo.role_name == "默认角色" || userinfo.role_name == "后勤部" || userinfo.role_name == "信息科" || userinfo.role_name == "设备科")
                    {


                        wzpcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.create && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
                        yjdcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.assign && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
                        dpjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.deal && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
                        ygqcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.hangup && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
                        ywjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.evaluate && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));
                        yqxcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.cancle && (p.createusername == userinfo.username || p.tousername[0] == userinfo.username || p.repairman == userinfo.username || p.repairman.Contains(userinfo.usercode)));


                    }
                    else
                    {
                        wzpcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.create);
                        yjdcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.assign);
                        dpjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.deal);
                        ygqcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.hangup);
                        ywjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.evaluate);
                        yqxcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.cancle);
                    }



                    if (state != -1)
                    {
                        query = query.Where(it => it.wostate == state);

                        if (userinfo.role_name.Contains("班") || userinfo.role_name == "默认角色" || userinfo.role_name == "后勤部" || userinfo.role_name == "信息科" || userinfo.role_name == "设备科")
                        {

                            if (!string.IsNullOrEmpty(userinfo.username))
                            {
                                query = query.Where(it => it.createusername == userinfo.username || it.tousername[0] == userinfo.username || it.repairman == userinfo.username || it.repairman.Contains(userinfo.usercode));
                            }


                        }
                        switch (state)
                        {
                            case (int)EnumWorkOrderState.assign: query = query.OrderByDescending(p => p.totime); break;
                            case (int)EnumWorkOrderState.deal: query = query.OrderByDescending(p => p.dealtime); break;
                            //case (int)EnumWorkOrderState.evaluate: query = query.OrderBy(p => p.evaluatetime); break;
                            case (int)EnumWorkOrderState.hangup: query = query.OrderByDescending(p => p.hanguptime); break;
                                //case (int)EnumWorkOrderState.cancle: query = query.OrderBy(p => p.canceltime); break;
                        }
                    }
                    else
                    {
                        if (userinfo.role_name.Contains("班") || userinfo.role_name == "默认角色" || userinfo.role_name == "后勤部" || userinfo.role_name == "信息科" || userinfo.role_name == "设备科")
                        {

                            if (!string.IsNullOrEmpty(userinfo.username))
                            {
                                query = query.Where(it => it.createusername == userinfo.username || it.tousername[0] == userinfo.username || it.repairman == userinfo.username || it.repairman.Contains(userinfo.usercode));
                            }

                        }
                    }
                    if (wxstate != -1)
                    {
                        if (wxstate == 1)
                        {
                            query = query.Where(it => it.wostate <= wxstate);
                        }
                        else
                        {
                            query = query.Where(it => it.wostate == wxstate);
                        }
                        switch (wxstate)
                        {
                            case (int)EnumWorkOrderState.assign: query = query.OrderByDescending(p => p.totime); break;
                            case (int)EnumWorkOrderState.deal: query = query.OrderByDescending(p => p.dealtime); break;
                            //case (int)EnumWorkOrderState.evaluate: query = query.OrderBy(p => p.evaluatetime); break;
                            case (int)EnumWorkOrderState.hangup: query = query.OrderByDescending(p => p.hanguptime); break;
                                //case (int)EnumWorkOrderState.cancle: query = query.OrderBy(p => p.canceltime); break;
                        }
                    }
                    #endregion
                    #endregion
                    recordcount = query.Count();
                    //总数
                    totalcount = query.Count();
                   
                    var list1 = query.ToList();
                    return list1;
                    #endregion
                }

            }

        }

        public IEnumerable<object> GetListsByPagerolebz(string phone, string wocode, string province, string city, string projectid, string protypeid, string buildid, string floorid,
         string replace, string systemid, string equipmentid, string usercode, string stime, string etime, int state, int wxstate,
         string nowusercode, UserAccountInfoModel userinfo,
         //string nowrolecode,
         int pageindex, int pagesize, string rolename, out int recordcount,
         out int totalcount, out int wzpcount, out int yjdcount, out int dpjcount, out int ygqcount, out int ywjcount, out int yqxcount)
        {
            #region 数据读取
            var query =
                from repairbase in _collection.AsQueryable()
                join proje in _collection_pro_project_info.AsQueryable() on repairbase.projectid equals proje.id into projeDefa
                join sysprovinces in _collection_sys_provinces.AsQueryable() on repairbase.province equals sysprovinces.provincecode into sys_provincesDefa
                join syscity in _collection_sys_city.AsQueryable() on repairbase.city equals syscity.citycode into sys_cityDefa

                where repairbase.isdelete == 0 && repairbase.Jwocode == rolename
                //orderby repairbase.tobztimes descending
                orderby repairbase.totime descending
                select new
                {
                    repairbase.id,
                    repairbase.wocode,
                    repairbase.projectid,
                    projectname = projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
                    repairbase.protypeid,
                    //protypename = dic_protypeDefa.Count() > 0 ? dic_protypeDefa.First().name : null,
                    repairbase.faulttypeid,
                    //faulttypename = dic_faulttypeDefa.Count() > 0 ? dic_faulttypeDefa.First().name : null,
                    repairbase.systemid,
                    //systemname = dic_systemDefa.Count() > 0 ? dic_systemDefa.First().name : null,
                    repairbase.equipmentid,
                    //equipmentname = dic_equDefa.Count() > 0 ? dic_equDefa.First().equipment_name : null,
                    repairbase.channel,
                    provincecode = repairbase.province,
                    province = sys_provincesDefa.Count() > 0 ? sys_provincesDefa.First().provincename : null,
                    citycode = repairbase.city,
                    city = sys_cityDefa.Count() > 0 ? sys_cityDefa.First().cityname : null,
                    repairbase.buildid,
                    //buildname = bldDefa.Count() > 0 ? bldDefa.First().buildname : null,
                    repairbase.floorid,
                    //floorname = flrDefa.Count() > 0 ? flrDefa.First().floorname : null,
                    repairbase.repairreplace,
                    repairbase.repairoom,
                    repairbase.repairtime,
                    repairbase.contactway,
                    repairbase.repairman,
                    repairbase.faultdescripe,
                    repairbase.faultpic,
                    repairbase.appointtime,
                    repairbase.wostate,
                    repairbase.createtime,
                    repairbase.createuser,
                    repairbase.createusername,
                    repairbase.touser,
                    repairbase.tousername,
                    repairbase.totime,
                    repairbase.totype,
                    repairbase.dealuser,
                    repairbase.dealusername,
                    repairbase.dealtime,
                    repairbase.ismaterials,
                    repairbase.materials,
                    repairbase.islosematerials,
                    repairbase.losematerials,
                    repairbase.hanguptime,
                    repairbase.arrivetime,
                    repairbase.evaluatetime,
                    repairbase.canceltime,
                    //repairbase.tobztimes,
                    repairbase.score,
                    repairbase.Jwocode
                };
            #endregion
            #region 查询条件
            if (!string.IsNullOrEmpty(phone))
                query = query.Where(it => it.contactway.Contains(phone));
            if (!string.IsNullOrEmpty(wocode))
                query = query.Where(it => it.wocode.Contains(wocode));
            if (!string.IsNullOrEmpty(province))
                query = query.Where(it => it.provincecode == province);
            if (!string.IsNullOrEmpty(city))
                query = query.Where(it => it.citycode == city);
            if (!string.IsNullOrEmpty(projectid))
                query = query.Where(it => it.projectid == projectid);
            if (!string.IsNullOrEmpty(protypeid))
                query = query.Where(it => it.protypeid == protypeid);
            if (!string.IsNullOrEmpty(buildid))
                query = query.Where(it => it.buildid == buildid);
            if (!string.IsNullOrEmpty(floorid))
                query = query.Where(it => it.floorid == floorid);
            if (!string.IsNullOrEmpty(replace))
                query = query.Where(it => it.repairreplace.Contains(replace) || it.repairoom.Contains(replace));
            if (!string.IsNullOrEmpty(systemid))
                query = query.Where(it => it.systemid == systemid);
            if (!string.IsNullOrEmpty(equipmentid))
                query = query.Where(it => it.equipmentid == equipmentid);


            #region 时间搜索
            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 (userinfo.type == 2)//查看自己创建的工单
            {
                query = query.Where(it => it.createuser == userinfo.usercode);
            }
            else
            {
                if (!string.IsNullOrEmpty(usercode)) query = query.Where(it => (it.touser.Contains(usercode) || it.dealuser.Contains(usercode)));
            }

            
            if (userinfo.remark != "组长")
            {
                wzpcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.create  &&(p.touser.Contains(userinfo.usercode) || p.tousername.Contains(userinfo.username)));
                yjdcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.assign  &&(p.touser.Contains(userinfo.usercode) || p.tousername.Contains(userinfo.username)));
                dpjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.deal    &&(p.touser.Contains(userinfo.usercode) || p.tousername.Contains(userinfo.username)));
                ygqcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.hangup  &&(p.touser.Contains(userinfo.usercode) || p.tousername.Contains(userinfo.username)));
                ywjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.evaluate&&(p.touser.Contains(userinfo.usercode) || p.tousername.Contains(userinfo.username)));
                yqxcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.cancle  &&(p.touser.Contains(userinfo.usercode) || p.tousername.Contains(userinfo.username)));
                query = query.Where(it => it.touser.Contains(userinfo.usercode) || it.tousername.Contains(userinfo.username));
            }
            else
            {
                wzpcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.create);
                yjdcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.assign);
                dpjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.deal);
                ygqcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.hangup);
                ywjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.evaluate);
                yqxcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.cancle);
            }
           

            if (state != -1)
            {
                query = query.Where(it => it.wostate == state);

                switch (state)
                {
                    case (int)EnumWorkOrderState.assign: query = query.OrderByDescending(p => p.totime); break;
                    case (int)EnumWorkOrderState.deal: query = query.OrderByDescending(p => p.dealtime); break;
                    //case (int)EnumWorkOrderState.evaluate: query = query.OrderBy(p => p.evaluatetime); break;
                    case (int)EnumWorkOrderState.hangup: query = query.OrderByDescending(p => p.hanguptime); break;
                        //case (int)EnumWorkOrderState.cancle: query = query.OrderBy(p => p.canceltime); break;
                }
            }
            if (wxstate != -1)
            {
                if (wxstate == 1)
                {
                    query = query.Where(it => it.wostate <= wxstate);
                }
                else
                {
                    query = query.Where(it => it.wostate == wxstate);
                }
                switch (wxstate)
                {
                    case (int)EnumWorkOrderState.assign: query = query.OrderByDescending(p => p.totime); break;
                    case (int)EnumWorkOrderState.deal: query = query.OrderByDescending(p => p.dealtime); break;
                    //case (int)EnumWorkOrderState.evaluate: query = query.OrderBy(p => p.evaluatetime); break;
                    case (int)EnumWorkOrderState.hangup: query = query.OrderByDescending(p => p.hanguptime); break;
                        //case (int)EnumWorkOrderState.cancle: query = query.OrderBy(p => p.canceltime); break;
                }
            }
            #endregion
            #endregion
            recordcount = query.Count();
            totalcount = query.Count();
            var list = query.Skip((pageindex - 1) * pagesize).Take(pagesize);
            return list;

        }

        public IEnumerable<object> GetListsByPagerole(string phone, string wocode, string province, string city, string projectid, string protypeid, string buildid, string floorid,
           string replace, string systemid, string equipmentid, string usercode, string stime, string etime, int state, int wxstate,
           string nowusercode, UserAccountInfoModel userinfo,
           //string nowrolecode,
           int pageindex, int pagesize,string rolename, out int recordcount,
           out int totalcount, out int wzpcount, out int yjdcount, out int dpjcount, out int ygqcount, out int ywjcount, out int yqxcount)
        {
             

            #region 数据读取
           var   query =
                from repairbase in _collection.AsQueryable()
                join proje in _collection_pro_project_info.AsQueryable() on repairbase.projectid equals proje.id into projeDefa
                join sysprovinces in _collection_sys_provinces.AsQueryable() on repairbase.province equals sysprovinces.provincecode into sys_provincesDefa
                join syscity in _collection_sys_city.AsQueryable() on repairbase.city equals syscity.citycode into sys_cityDefa
               
                where repairbase.isdelete == 0 //&&repairbase.Jwocode==rolename 
                orderby repairbase.totime descending
                select new
                {
                    repairbase.id,
                    repairbase.wocode,
                    repairbase.projectid,
                    projectname = projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
                    repairbase.protypeid,
                    //protypename = dic_protypeDefa.Count() > 0 ? dic_protypeDefa.First().name : null,
                    repairbase.faulttypeid,
                    //faulttypename = dic_faulttypeDefa.Count() > 0 ? dic_faulttypeDefa.First().name : null,
                    repairbase.systemid,
                    //systemname = dic_systemDefa.Count() > 0 ? dic_systemDefa.First().name : null,
                    repairbase.equipmentid,
                    //equipmentname = dic_equDefa.Count() > 0 ? dic_equDefa.First().equipment_name : null,
                    repairbase.channel,
                    provincecode = repairbase.province,
                    province = sys_provincesDefa.Count() > 0 ? sys_provincesDefa.First().provincename : null,
                    citycode = repairbase.city,
                    city = sys_cityDefa.Count() > 0 ? sys_cityDefa.First().cityname : null,
                    repairbase.buildid,
                    //buildname = bldDefa.Count() > 0 ? bldDefa.First().buildname : null,
                    repairbase.floorid,
                    //floorname = flrDefa.Count() > 0 ? flrDefa.First().floorname : null,
                    repairbase.repairreplace,
                    repairbase.repairoom,
                    repairbase.repairtime,
                    repairbase.contactway,
                    repairbase.repairman,
                    repairbase.faultdescripe,
                    repairbase.faultpic,
                    repairbase.appointtime,
                    repairbase.wostate,
                    repairbase.createtime,
                    repairbase.createuser,
                    repairbase.createusername,
                    repairbase.touser,
                    repairbase.tousername,
                    repairbase.totime,
                    repairbase.totype,
                    repairbase.dealuser,
                    repairbase.dealusername,
                    repairbase.dealtime,
                    repairbase.ismaterials,
                    repairbase.materials,
                    repairbase.islosematerials,
                    repairbase.losematerials,
                    repairbase.hanguptime,
                    repairbase.arrivetime,
                    repairbase.evaluatetime,
                    repairbase.canceltime,
                    //repairbase.tobztimes,
                    repairbase.score,
                    repairbase.Jwocode
                };

            //if ( userinfo.remark!= "组长")
            //{
            //    query = query.Where(it => it.touser.Contains(nowusercode));
            //}
     
            #endregion
            #region 查询条件



            if (!string.IsNullOrEmpty(phone))
                query = query.Where(it => it.contactway.Contains(phone));
            if (!string.IsNullOrEmpty(wocode))
                query = query.Where(it => it.wocode.Contains(wocode));
            if (!string.IsNullOrEmpty(province))
                query = query.Where(it => it.provincecode == province);
            if (!string.IsNullOrEmpty(city))
                query = query.Where(it => it.citycode == city);
            if (!string.IsNullOrEmpty(projectid))
                query = query.Where(it => it.projectid == projectid);
            if (!string.IsNullOrEmpty(protypeid))
                query = query.Where(it => it.protypeid == protypeid);
            if (!string.IsNullOrEmpty(buildid))
                query = query.Where(it => it.buildid == buildid);
            if (!string.IsNullOrEmpty(floorid))
                query = query.Where(it => it.floorid == floorid);
            if (!string.IsNullOrEmpty(replace))
                query = query.Where(it => it.repairreplace.Contains(replace) || it.repairoom.Contains(replace));
            if (!string.IsNullOrEmpty(systemid))
                query = query.Where(it => it.systemid == systemid);
            if (!string.IsNullOrEmpty(equipmentid))
                query = query.Where(it => it.equipmentid == equipmentid);
           
            
            #region 时间搜索
            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 (userinfo.type == 2)//查看自己创建的工单
            {
                query = query.Where(it => it.createuser == userinfo.usercode);
            }
            else
            {
                //if (userinfo.isallproject == 1)
                //{
                if (!string.IsNullOrEmpty(usercode))
                {
                    query = query.Where(it => (it.touser.Contains(usercode) || it.dealuser.Contains(usercode)));
                }
                //}
                //else if (userinfo.isallproject == 0)
                //{
                //    query = query.Where(it => userinfo.projectlist.Contains(it.projectid));


                if (userinfo.level==1)
                {
                    query = query.Where(it => (it.touser.Count() == 0 || it.touser.Contains(nowusercode) || it.dealuser.Contains(nowusercode)));
                }
                else
                {
                    if (!string.IsNullOrEmpty(usercode)) query = query.Where(it => (it.touser.Contains(usercode) || it.dealuser.Contains(usercode)));
                }
                //}
            }
            
            totalcount = query.Count();
          
            wzpcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.create);
            yjdcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.assign);
            dpjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.deal);
            ygqcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.hangup);
            ywjcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.evaluate);
            yqxcount = query.Count(p => p.wostate == (int)EnumWorkOrderState.cancle);

            if (state != -1)
            {
                query = query.Where(it => it.wostate == state);
              
                switch (state)
                {
                    case (int)EnumWorkOrderState.assign: query = query.OrderByDescending(p => p.totime); break;
                    case (int)EnumWorkOrderState.deal: query = query.OrderByDescending(p => p.dealtime); break;
                    //case (int)EnumWorkOrderState.evaluate: query = query.OrderBy(p => p.evaluatetime); break;
                    case (int)EnumWorkOrderState.hangup: query = query.OrderByDescending(p => p.hanguptime); break;
                        //case (int)EnumWorkOrderState.cancle: query = query.OrderBy(p => p.canceltime); break;
                }
            }
            if (wxstate != -1)
            {
                if (wxstate == 1)
                {
                    query = query.Where(it => it.wostate <= wxstate);
                }
                else
                {
                    query = query.Where(it => it.wostate == wxstate);
                }
                switch (wxstate)
                {
                    case (int)EnumWorkOrderState.assign: query = query.OrderByDescending(p => p.totime); break;
                    case (int)EnumWorkOrderState.deal: query = query.OrderByDescending(p => p.dealtime); break;
                    //case (int)EnumWorkOrderState.evaluate: query = query.OrderBy(p => p.evaluatetime); break;
                    case (int)EnumWorkOrderState.hangup: query = query.OrderByDescending(p => p.hanguptime); break;
                        //case (int)EnumWorkOrderState.cancle: query = query.OrderBy(p => p.canceltime); break;
                }
            }
            #endregion
            #endregion
            recordcount = query.Count();

            var list = query.Skip((pageindex - 1) * pagesize).Take(pagesize);
            return list;
        }

        /// <summary>
        /// 根据多个条件查询列表 Linq 关联查询
        /// </summary>
        public IEnumerable<object> GetAppListsByPage(string phone, string wocode, string province, string city, string projectid, string protypeid, string buildid, string floorid,
            string replace, string systemid, string equipmentid, string usercode, string stime, string etime, int state,
            string nowusercode, UserAccountInfoModel userinfo,
            //string nowrolecode,
            int pageindex, int pagesize, out int recordcount,
            out int totalcount, out int wzpcount, out int yjdcount, out int dpjcount, out int ygqcount, out int ywjcount, out int yqxcount)
        {
            #region 数据读取
            var query =
                from repairbase in _collection.AsQueryable()
                join proje in _collection_pro_project_info.AsQueryable() on repairbase.projectid equals proje.id into projeDefa
                where repairbase.isdelete == 0
                orderby repairbase.createtime descending
                select new
                {
                    repairbase.id,
                    repairbase.wocode,
                    repairbase.projectid,
                    projectname = projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
                    repairbase.protypeid,
                    repairbase.faulttypeid,
                    repairbase.systemid,
                    repairbase.equipmentid,
                    repairbase.channel,
                    provincecode = repairbase.province,
                    citycode = repairbase.city,
                    repairbase.buildid,
                    repairbase.floorid,
                    repairbase.repairreplace,
                    repairbase.repairoom,
                    repairbase.repairtime,
                    repairbase.contactway,
                    repairbase.repairman,
                    repairbase.faultdescripe,
                    repairbase.faultpic,
                    repairbase.appointtime,
                    repairbase.wostate,
                    repairbase.createtime,
                    repairbase.createuser,
                    repairbase.createusername,
                    repairbase.touser,
                    repairbase.tousername,
                    repairbase.totime,
                    repairbase.totype,
                    repairbase.dealuser,
                    repairbase.dealusername,
                    repairbase.dealtime,
                    repairbase.ismaterials,
                    repairbase.materials,
                    repairbase.islosematerials,
                    repairbase.losematerials,
                    repairbase.hanguptime,
                    repairbase.arrivetime,
                    repairbase.evaluatetime,
                    repairbase.canceltime,
                    repairbase.score,
                    repairbase.Jwocode,
                    repairbase.Pwcode,
                    repairbase.CLpeoplefrom
                };
            #endregion
            #region 查询条件
            if (!string.IsNullOrEmpty(phone))
                query = query.Where(it => it.contactway.Contains(phone));
            if (!string.IsNullOrEmpty(wocode))
                query = query.Where(it => it.wocode.Contains(wocode));
            if (!string.IsNullOrEmpty(province))
                query = query.Where(it => it.provincecode == province);
            if (!string.IsNullOrEmpty(city))
                query = query.Where(it => it.citycode == city);
            if (!string.IsNullOrEmpty(projectid))
                query = query.Where(it => it.projectid == projectid);
            if (!string.IsNullOrEmpty(protypeid))
                query = query.Where(it => it.protypeid == protypeid);
            if (!string.IsNullOrEmpty(buildid))
                query = query.Where(it => it.buildid == buildid);
            if (!string.IsNullOrEmpty(floorid))
                query = query.Where(it => it.floorid == floorid);
            if (!string.IsNullOrEmpty(replace))
                query = query.Where(it => it.repairreplace.Contains(replace) || it.repairoom.Contains(replace));
            if (!string.IsNullOrEmpty(systemid))
                query = query.Where(it => it.systemid == systemid);
            if (!string.IsNullOrEmpty(equipmentid))
                query = query.Where(it => it.equipmentid == equipmentid);
            //var userrolename = userinfo.role_name;
            //if (!string.IsNullOrEmpty(userrolename))
            //{
            //    if (userrolename.Contains("班")||userrolename=="默认角色")
            //    {
            //        query = query.Where(it => it.Jwocode == userrolename);
            //    }
            //}

            #region 时间搜索
            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 (userinfo.type == 2)//查看自己创建的工单
            {
                query = query.Where(it => it.createuser == userinfo.usercode);
            }
            else
            {
                //if (userinfo.isallproject == 1)
                //{
                    //if (!string.IsNullOrEmpty(usercode)) query = query.Where(it => (it.touser.Contains(usercode) || it.dealuser.Contains(usercode)));
                //}
                //else if (userinfo.isallproject == 0)
                //{
                //    query = query.Where(it => userinfo.projectlist.Contains(it.projectid));

                //    if (userinfo.role_code == "YWYG")
                //    {
                //        query = query.Where(it => (it.touser.Count() == 0 || it.touser.Contains(nowusercode) || it.dealuser.Contains(nowusercode)));
                //    }
                //    else
                //    {
                //        if (!string.IsNullOrEmpty(usercode)) query = query.Where(it => (it.touser.Contains(usercode) || it.dealuser.Contains(usercode)));
                //    }
                //}
                if (userinfo.level==1)
                {
                    query = query.Where(it => (it.touser.Count() == 0 || it.touser.Contains(nowusercode) || it.dealuser.Contains(nowusercode)));
                }
                else
                {
                    if (!string.IsNullOrEmpty(usercode)) query = query.Where(it => (it.touser.Contains(usercode) || it.dealuser.Contains(usercode)));
                }
            }


            totalcount = query.Count();
            //wzpcount = query.Where(p => p.wostate == (int)EnumWorkOrderState.create).Count();
            //yjdcount = query.Where(p => p.wostate == (int)EnumWorkOrderState.assign).Count();
            //dpjcount = query.Where(p => p.wostate == (int)EnumWorkOrderState.deal).Count();
            //ygqcount = query.Where(p => p.wostate == (int)EnumWorkOrderState.hangup).Count();
            //ywjcount = query.Where(p => p.wostate == (int)EnumWorkOrderState.evaluate).Count();
            //yqxcount = query.Where(p => p.wostate == (int)EnumWorkOrderState.cancle).Count();
            var countlist = query.GroupBy(p => p.wostate).Select(p => new { wostate = p.Key, count = p.Count() });  //.ToList();
            wzpcount = countlist.Where(p => p.wostate == (int)EnumWorkOrderState.create).FirstOrDefault()?.count ?? 0;
            yjdcount = countlist.Where(p => p.wostate == (int)EnumWorkOrderState.assign).FirstOrDefault()?.count ?? 0;
            dpjcount = countlist.Where(p => p.wostate == (int)EnumWorkOrderState.deal).FirstOrDefault()?.count ?? 0;
            ygqcount = countlist.Where(p => p.wostate == (int)EnumWorkOrderState.hangup).FirstOrDefault()?.count ?? 0;
            ywjcount = countlist.Where(p => p.wostate == (int)EnumWorkOrderState.evaluate).FirstOrDefault()?.count ?? 0;
            yqxcount = countlist.Where(p => p.wostate == (int)EnumWorkOrderState.cancle).FirstOrDefault()?.count ?? 0;

            if (state != -1)
            {
                query = query.Where(it => it.wostate == state);
                switch (state)
                {
                    case (int)EnumWorkOrderState.assign: query = query.OrderByDescending(p => p.totime); break;
                    case (int)EnumWorkOrderState.deal: query = query.OrderByDescending(p => p.dealtime); break;
                    //case (int)EnumWorkOrderState.evaluate: query = query.OrderBy(p => p.evaluatetime); break;
                    case (int)EnumWorkOrderState.hangup: query = query.OrderByDescending(p => p.hanguptime); break;
                        //case (int)EnumWorkOrderState.cancle: query = query.OrderBy(p => p.canceltime); break;
                }
            }

            #endregion
            #endregion
            recordcount = query.Count();

            var list = query.Skip((pageindex - 1) * pagesize).Take(pagesize);
            return list;
        }

        public IEnumerable<object> GetAppListsByPagemy(string phone, string wocode, string province, string city, string projectid, string protypeid, string buildid, string floorid,
          string replace, string username, string systemid, string equipmentid, string usercode, string stime, string etime, int state,
          string nowusercode, UserAccountInfoModel userinfo,
          //string nowrolecode,
          int pageindex, int pagesize, out int recordcount,
          out int totalcount, out int wzpcount, out int yjdcount, out int dpjcount, out int ygqcount, out int ywjcount, out int yqxcount)
        {
            #region 数据读取
            var query =
                from repairbase in _collection.AsQueryable()
                join proje in _collection_pro_project_info.AsQueryable() on repairbase.projectid equals proje.id into projeDefa
                where repairbase.isdelete == 0
                orderby repairbase.createtime descending
                select new
                {
                    repairbase.id,
                    repairbase.wocode,
                    repairbase.projectid,
                    projectname = projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
                    repairbase.protypeid,
                    repairbase.faulttypeid,
                    repairbase.systemid,
                    repairbase.equipmentid,
                    repairbase.channel,
                    provincecode = repairbase.province,
                    citycode = repairbase.city,
                    repairbase.buildid,
                    repairbase.floorid,
                    repairbase.repairreplace,
                    repairbase.repairoom,
                    repairbase.repairtime,
                    repairbase.contactway,
                    repairbase.repairman,
                    repairbase.faultdescripe,
                    repairbase.faultpic,
                    repairbase.appointtime,
                    repairbase.wostate,
                    repairbase.createtime,
                    repairbase.createuser,
                    repairbase.createusername,
                    repairbase.touser,
                    repairbase.tousername,
                    repairbase.totime,
                    repairbase.totype,
                    repairbase.dealuser,
                    repairbase.dealusername,
                    repairbase.dealtime,
                    repairbase.ismaterials,
                    repairbase.materials,
                    repairbase.islosematerials,
                    repairbase.losematerials,
                    repairbase.hanguptime,
                    repairbase.arrivetime,
                    repairbase.evaluatetime,
                    repairbase.canceltime,
                    repairbase.score,
                    repairbase.Jwocode
                };
            #endregion
            #region 查询条件
            if (!string.IsNullOrEmpty(phone))
                query = query.Where(it => it.contactway.Contains(phone));
            if (!string.IsNullOrEmpty(wocode))
                query = query.Where(it => it.wocode.Contains(wocode));
            if (!string.IsNullOrEmpty(province))
                query = query.Where(it => it.provincecode == province);
            if (!string.IsNullOrEmpty(city))
                query = query.Where(it => it.citycode == city);
            if (!string.IsNullOrEmpty(projectid))
                query = query.Where(it => it.projectid == projectid);
            if (!string.IsNullOrEmpty(protypeid))
                query = query.Where(it => it.protypeid == protypeid);
            if (!string.IsNullOrEmpty(buildid))
                query = query.Where(it => it.buildid == buildid);
            if (!string.IsNullOrEmpty(floorid))
                query = query.Where(it => it.floorid == floorid);
            if (!string.IsNullOrEmpty(replace))
                query = query.Where(it => it.repairreplace.Contains(replace) || it.repairoom.Contains(replace));
            if (!string.IsNullOrEmpty(systemid))
                query = query.Where(it => it.systemid == systemid);
            if (!string.IsNullOrEmpty(equipmentid))
                query = query.Where(it => it.equipmentid == equipmentid);
            if (!string.IsNullOrEmpty(username))
            {
                query = query.Where(it => it.createusername == username || it.repairman.Contains(userinfo.username) || it.repairman.Contains(userinfo.usercode));//|| it.tousername[0] == userinfo.username || it.repairman.Contains(userinfo.username) || it.repairman.Contains(userinfo.usercode)
            }

            #region 时间搜索
            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 (userinfo.type == 2)//查看自己创建的工单
            {
                query = query.Where(it => it.createuser == userinfo.usercode);
            }
            else
            {
                if (userinfo.role_code == "YWYG")
                {
                    query = query.Where(it => (it.touser.Count() == 0 || it.touser.Contains(nowusercode) || it.dealuser.Contains(nowusercode)|| it.repairman.Contains(userinfo.username) || it.repairman.Contains(userinfo.usercode)));
                }
                else
                {
                    if (!string.IsNullOrEmpty(usercode)) query = query.Where(it => (it.touser.Contains(usercode) || it.dealuser.Contains(usercode) || it.repairman.Contains(userinfo.username) || it.repairman.Contains(userinfo.usercode)));
                }
            }


            totalcount = query.Count();
            //wzpcount = query.Where(p => p.wostate == (int)EnumWorkOrderState.create).Count();
            //yjdcount = query.Where(p => p.wostate == (int)EnumWorkOrderState.assign).Count();
            //dpjcount = query.Where(p => p.wostate == (int)EnumWorkOrderState.deal).Count();
            //ygqcount = query.Where(p => p.wostate == (int)EnumWorkOrderState.hangup).Count();
            //ywjcount = query.Where(p => p.wostate == (int)EnumWorkOrderState.evaluate).Count();
            //yqxcount = query.Where(p => p.wostate == (int)EnumWorkOrderState.cancle).Count();
            var countlist = query.GroupBy(p => p.wostate).Select(p => new { wostate = p.Key, count = p.Count() });  //.ToList();
            wzpcount = countlist.Where(p => p.wostate == (int)EnumWorkOrderState.create).FirstOrDefault()?.count ?? 0;
            yjdcount = countlist.Where(p => p.wostate == (int)EnumWorkOrderState.assign).FirstOrDefault()?.count ?? 0;
            dpjcount = countlist.Where(p => p.wostate == (int)EnumWorkOrderState.deal).FirstOrDefault()?.count ?? 0;
            ygqcount = countlist.Where(p => p.wostate == (int)EnumWorkOrderState.hangup).FirstOrDefault()?.count ?? 0;
            ywjcount = countlist.Where(p => p.wostate == (int)EnumWorkOrderState.evaluate).FirstOrDefault()?.count ?? 0;
            yqxcount = countlist.Where(p => p.wostate == (int)EnumWorkOrderState.cancle).FirstOrDefault()?.count ?? 0;

            if (state != -1)
            {
                query = query.Where(it => it.wostate == state);
                switch (state)
                {
                    case (int)EnumWorkOrderState.assign: query = query.OrderByDescending(p => p.totime); break;
                    case (int)EnumWorkOrderState.deal: query = query.OrderByDescending(p => p.dealtime); break;
                    //case (int)EnumWorkOrderState.evaluate: query = query.OrderBy(p => p.evaluatetime); break;
                    case (int)EnumWorkOrderState.hangup: query = query.OrderByDescending(p => p.hanguptime); break;
                        //case (int)EnumWorkOrderState.cancle: query = query.OrderBy(p => p.canceltime); break;
                }
            }

            #endregion
            #endregion
            recordcount = query.Count();

            var list = query.Skip((pageindex - 1) * pagesize).Take(pagesize);
            return list;
        }

        /// <summary>
        /// 获取报修工单详情 by id Linq 关联查询
        /// </summary>
        public object GetDetails(string id)
        {
            #region 数据查询
            var query =
                from repairbase in _collection.AsQueryable()
                join dic_system in _collection_sys_dictionaryvalue.AsQueryable() on repairbase.systemid equals dic_system.id into dic_systemDefa
                join dic_protype in _collection_sys_dictionaryvalue.AsQueryable() on repairbase.protypeid equals dic_protype.id into dic_protypeDefa
                join dic_faulttype in _collection_sys_dictionaryvalue.AsQueryable() on repairbase.faulttypeid equals dic_faulttype.id into dic_faulttypeDefa
                join proje in _collection_pro_project_info.AsQueryable() on repairbase.projectid equals proje.id into projeDefa
                join dic_equ in _collection_equipment_info.AsQueryable() on repairbase.equipmentid equals dic_equ.id into dic_equDefa
                join sysprovinces in _collection_sys_provinces.AsQueryable() on repairbase.province equals sysprovinces.provincecode into sys_provincesDefa
                join syscity in _collection_sys_city.AsQueryable() on repairbase.city equals syscity.citycode into sys_cityDefa
                join bld in _collection_sys_buliding.AsQueryable() on repairbase.buildid equals bld.id into bldDefa
                join flr in _collection_sys_floor.AsQueryable() on repairbase.floorid equals flr.id into flrDefa
                where repairbase.isdelete == 0 && repairbase.id == id
                orderby repairbase.createtime descending
                select new
                {
                    id = repairbase.id,
                    repairbase.wocode,
                    repairbase.projectid,
                    projectname = projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
                    repairbase.protypeid,
                    protypename = dic_protypeDefa.Count() > 0 ? dic_protypeDefa.First().name : null,
                    repairbase.faulttypeid,
                    faulttypename = dic_faulttypeDefa.Count() > 0 ? dic_faulttypeDefa.First().name : null,
                    repairbase.systemid,
                    systemname = dic_systemDefa.Count() > 0 ? dic_systemDefa.First().name : null,
                    repairbase.equipmentid,
                    equipmentname = dic_equDefa.Count() > 0 ? dic_equDefa.First().equipment_name : null,
                    repairbase.channel,
                    provincecode = repairbase.province,
                    province = sys_provincesDefa.Count() > 0 ? sys_provincesDefa.First().provincename : null,
                    citycode = repairbase.city,
                    city = sys_cityDefa.Count() > 0 ? sys_cityDefa.First().cityname : null,
                    repairbase.buildid,
                    buildname = bldDefa.Count() > 0 ? bldDefa.First().buildname : null,
                    repairbase.floorid,
                    floorname = (flrDefa.Count() > 0 ? flrDefa.First().floorname : null) + " " + (flrDefa.Count() > 0 ? flrDefa.First().remark : null),
                    repairbase.repairreplace,
                    repairbase.repairoom,
                    repairbase.repairtime,
                    repairbase.contactway,
                    repairbase.repairman,
                    repairbase.faultdescripe,
                    repairbase.faultpic,
                    repairbase.appointtime,
                    repairbase.wostate,
                    repairbase.createtime,
                    repairbase.createuser,
                    repairbase.createusername,
                    repairbase.touser,
                    repairbase.tousername,
                    repairbase.totime,
                    repairbase.totype,
                    repairbase.dealuser,
                    repairbase.dealusername,
                    repairbase.dealtime,
                    repairbase.ismaterials,
                    repairbase.materials,
                    repairbase.islosematerials,
                    repairbase.losematerials,
                    repairbase.hanguptime,
                    repairbase.arrivetime,
                    repairbase.evaluatetime,
                    repairbase.canceltime,
                    repairbase.score
                };
            #endregion
            var model = query.FirstOrDefault();
            if (model != null)
            {
                return model;
            }
            return null;
        }

        /// <summary>
        /// 获取项目信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public object GetProjectInfo(string id)
        {
            //var model = (from p in _collection_pro_project_info.AsQueryable() where p.id == id select p).FirstOrDefault();
            var model = _collection_pro_project_info.Find(p => p.id == id).FirstOrDefault();
            return model;
        }

        /// <summary>
        /// 获取维修工接单限制数量
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public int GetLimitCount(string usercode)
        {
            int n = 0;
            var model = _collection_sys_systemconfig.Find(p => p.paramcode == "BXJDXZSL").FirstOrDefault();
            if (model != null)
            {
                int m = Int32.Parse(model.paramvalue);
                if (m != 0)
                {
                    long l = _collection.Count(p => (p.touser.Contains(usercode) || p.dealuser.Contains(usercode)) && p.wostate == (int)EnumWorkOrderState.assign && p.isdelete == 0);
                    long rcount = _collection_equipmentrunning.Count(p => (p.checkby.Equals(usercode) || p.dealby.Equals(usercode)) && p.wostate == (int)EnumEquipmentRunningState.assign && p.isdelete == 0);
                    long mcount = _collection_wo_maintain_base.Count(p => ( p.maintainuser.Contains(usercode) || p.dealuser.Contains(usercode)) && p.wostate == (int)EnumWoMaintainState.assign && p.isdelete == false);
                    var t = l + rcount + mcount;
                    if (t >= m) n = Int32.Parse(t.ToString());
                }
            }

            return n;
        }

        /// <summary>
        /// 获取维修工
        /// </summary>
        /// <param name="wocode"></param>
        /// <returns></returns>
        public object GetRepairUser(string wocode,string role_name)
        {
            var model = _collection.Find(p => p.wocode == wocode).FirstOrDefault();
            if (model != null)
            {
                //关联查询
                var query =
                    from usa in _collection_sys_user_account.AsQueryable()
                    join role in _collection_sysroleinfo.AsQueryable() on usa.role_id equals role.id into roleinfo
                    where usa.delete_flag == false && usa.type != 2 && usa.projectlist.Contains(model.projectid)
                    orderby usa.create_time descending
                    select new
                    {
                        usa.id,
                        usa.username,
                        usa.usercode,
                        role_code = roleinfo.Count() > 0 ? roleinfo.First().role_code : null,
                        role_name=roleinfo.Count()>0?roleinfo.First().role_name:null,
                    };
                query = query.Where(it => it.role_code.Equals("YWYG"));
                if (!string.IsNullOrEmpty(role_name))
                {
                    if(role_name!=null)
                        query = query.Where(it => it.role_name.Equals(role_name));
                }
                var list = query.Select(p => new {
                    p.id,
                    p.username,
                    p.usercode
                }).ToList();

                return list;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取协助维修工
        /// </summary>
        /// <param name="wocode"></param>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public object GetAssistRepairUser(string projectid, string usercode,int isxyzg)
        {
            var date = DateTime.Now.ToString("yyyy-MM-dd");
            var groupinfo = _collection_sys_work_usergroup.Find(p => p.usercode == usercode && p.date == date && p.isdelete == 0).FirstOrDefault();

            int n = 0;
            var configmodel = _collection_sys_systemconfig.Find(p => p.paramcode == "BXJDXZSL").FirstOrDefault();
            if (configmodel != null)
            {
                n = Int32.Parse(configmodel.paramvalue);
            }

            //关联查询
            var query = from usa in _collection_sys_user_account.AsQueryable()
                        join role in _collection_sysroleinfo.AsQueryable() on usa.role_id equals role.id into roleinfo
                        where usa.delete_flag == false && usa.type != 2
                        orderby usa.create_time descending
                        select new
                        {
                            usa.id,
                            usa.username,
                            usa.usercode,
                            usa.projectlist,
                            role_code = roleinfo.Count() > 0 ? roleinfo.First().role_code : null,
                        };
            if (isxyzg == 0)
            {
                query = query.Where(it => it.role_code.Equals("YWYG"));
            }
            else
            {
                query = query.Where(it => it.role_code.Equals("YWYG") || it.role_code.Equals("YWZG"));
            }
            //if (string.IsNullOrEmpty(projectid))
            //    query = query.Where(it => it.projectlist.Contains(projectid));

            if (groupinfo != null)
            {
                //关联查询
                var groupuser = from p in _collection_sys_work_usergroup.AsQueryable()
                                where p.date == date && p.usercode != usercode && p.isdelete == 0 && p.groupcode == groupinfo.groupcode
                                orderby p.usercode descending
                                select p;
                var users = groupuser.Select(q => q.usercode).ToList();
                query = query.Where(p => users.Contains(p.usercode));


                var list = query.ToList().Select(p =>
                {
                    var rpcount = _collection.Count(q => (q.touser.Contains(p.usercode) || q.dealuser.Contains(p.usercode)) && q.wostate == (int)EnumWorkOrderState.assign && q.isdelete == 0);
                    long rcount = _collection_equipmentrunning.Count(q => (q.checkby.Equals(usercode) || q.dealby.Equals(usercode)) && q.wostate == (int)EnumEquipmentRunningState.assign && q.isdelete == 0);
                    long mcount = _collection_wo_maintain_base.Count(q => (q.maintainuser.Contains(usercode) || q.dealuser.Contains(usercode)) && q.wostate == (int)EnumWoMaintainState.assign && q.isdelete == false);
                    var assigncount = rpcount + rcount + mcount;
                    return new
                    {
                        p.username,
                        p.usercode,
                        assigncount,
                        isover = n > 0 ? (n > assigncount ? 0 : 1) : 0
                    };
                }).ToList();

                return list;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取评价规定时间间隔
        /// </summary>
        /// <returns></returns>
        public int GetEvaluateHour()
        {
            int n = 0;
            var model = _collection_sys_systemconfig.Find(p => p.paramcode == "PJGDSJJG").FirstOrDefault();
            if (model != null)
            {
                int m = Int32.Parse(model.paramvalue);
                if (m > 0)
                {
                    n = m;
                }
            }

            return n;
        }

        #endregion
    }
}