using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using EquipmentApi.IRepositories;
using MadRunFabric.Model;
using EquipmentApi.Model.Input;
using MadRunFabric.Common;
using MadRunFabric.Common.Enums;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using Api.SignToken;
using AutoMapper;
using MadRunFabric.Model.MessageApi;
using Microsoft.Extensions.Configuration;
using MadRunFabric.Model.EquipmentApi.Model;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.UserModel;
using System.Reflection;
using System.IO;
using NPOI.SS.Util;
using com.igetui.api.openservice;
using com.igetui.api.openservice.igetui;
using com.igetui.api.openservice.igetui.template;
using System.Text.RegularExpressions;
using Newtonsoft.Json;

namespace EquipmentApi.Controllers.workordermanage
{
    /// <summary>
    /// 报修工单控制器
    /// </summary>
    [Authorize]
    [ApiVersion("6.0")]
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class WoRepairBaseController : BaseController
    {
        private readonly ILogger<WoRepairBaseController> _logger;
        private readonly IConfiguration _configuration;
        private readonly ISys_User_AccountRepository _sys_user_accountRepository;
        private readonly IWo_Repair_BaseRepository _wo_repair_baseRepository;
        private readonly IWo_Repair_SendRepository _wo_repair_sendRepository;
        private readonly IWo_Repair_DealRepository _wo_repair_dealRepository;
        private readonly IWo_Repair_ItemsRepository _wo_repair_itemsRepository;
        private readonly IWo_Repair_EvaluateRepository _wo_repair_evaluateRepository;
        private readonly ISignTokenService _signTokenService;
        private readonly IWechatService _wechatservice;
        private readonly IMapper _mapper;
        private readonly IWo_Order_User_MapRepository _wo_order_user_maprepository;
        private readonly IinfotxsRepository _infotxsRepository;
        private readonly ISys_Role_InfoRepository _ISys_Role_InfoRepository;
        private readonly IusercoldandclintidsRepository _IusercoldandclintidsRepository;
        private readonly IwxwpstimeRepository _wxwpstimeRepository;
        private readonly ISys_Work_DayGroupRepository _sys_work_datgroupRepository;

        public WoRepairBaseController(ILogger<WoRepairBaseController> logger,
            IConfiguration configuration,
            ISys_User_AccountRepository sys_user_accountRepository,
            IWo_Repair_BaseRepository wo_repair_baseRepository,
            IWo_Repair_SendRepository wo_repair_sendRepository, IWo_Repair_DealRepository wo_repair_dealRepository,
            IWo_Repair_ItemsRepository wo_repair_itemsRepository, IWo_Repair_EvaluateRepository wo_repair_evaluateRepository,
            ISignTokenService signTokenService, IWechatService wechatservice, IMapper mapper,
            IWo_Order_User_MapRepository wo_order_user_maprepository, IinfotxsRepository infotxsRepository,
            ISys_Role_InfoRepository Sys_Role_InfoRepository,
            IusercoldandclintidsRepository usercoldandclintidsRepository,
            IwxwpstimeRepository wxwpstimeRepository,
          ISys_Work_DayGroupRepository sys_work_datgroupRepository
            )
        {
            _logger = logger;
            _configuration = configuration;
            _sys_user_accountRepository = sys_user_accountRepository;
            _wo_repair_baseRepository = wo_repair_baseRepository;
            _wo_repair_sendRepository = wo_repair_sendRepository;
            _wo_repair_dealRepository = wo_repair_dealRepository;
            _wo_repair_itemsRepository = wo_repair_itemsRepository;
            _wo_repair_evaluateRepository = wo_repair_evaluateRepository;
            _signTokenService = signTokenService;
            _wechatservice = wechatservice;
            _mapper = mapper;
            _wo_order_user_maprepository = wo_order_user_maprepository;
            _infotxsRepository = infotxsRepository;
            _ISys_Role_InfoRepository = Sys_Role_InfoRepository;
            _IusercoldandclintidsRepository = usercoldandclintidsRepository;
            _wxwpstimeRepository = wxwpstimeRepository;
            _sys_work_datgroupRepository = sys_work_datgroupRepository;
        }

        #region 工单分页获取接口

        /// <summary>
        /// 分页获取
        /// </summary>
        [HttpGet("getlistsbypage")]
        public async Task<IActionResult> GetListsByPageAsync(string phone, string wocode, string province, string city, string protypeid, string projectid,
            string buildid, string floorid, string replace, string systemid, string equipmentid, string usercode, string stime, string etime,
            int datetype = -1, int state = -1, int wxstate = -1, int pageindex = 1, int pagesize = 10)
        {
            string nowusercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;

            string nowrolecode = User.Claims.FirstOrDefault(c => c.Type == "RoleCode").Value;

            if (datetype != -1)
            {
                GetStartEndTime(datetype, out stime, out etime);
            }

            //排序字段
            var sort = Builders<Wo_Repair_Base>.Sort.Descending("createtime");

            //根据条件查询集合
            var flist = new List<FilterDefinition<Wo_Repair_Base>>();
            flist.Add(Builders<Wo_Repair_Base>.Filter.Eq("isdelete", 0));

            if (!string.IsNullOrEmpty(phone))
                flist.Add(Builders<Wo_Repair_Base>.Filter.Where(it => it.contactway.Contains(phone)));
            if (!string.IsNullOrEmpty(wocode))
                flist.Add(Builders<Wo_Repair_Base>.Filter.Where(it => it.wocode.Contains(wocode)));
            if (!string.IsNullOrEmpty(province))
                flist.Add(Builders<Wo_Repair_Base>.Filter.Eq("provincecode", province));
            if (!string.IsNullOrEmpty(city))
                flist.Add(Builders<Wo_Repair_Base>.Filter.Eq("citycode", city));
            if (!string.IsNullOrEmpty(projectid))
                flist.Add(Builders<Wo_Repair_Base>.Filter.Eq("projectid", projectid));
            if (!string.IsNullOrEmpty(protypeid))
                flist.Add(Builders<Wo_Repair_Base>.Filter.Eq("protypeid", protypeid));
            if (!string.IsNullOrEmpty(buildid))
                flist.Add(Builders<Wo_Repair_Base>.Filter.Eq("buildid", buildid));
            if (!string.IsNullOrEmpty(floorid))
                flist.Add(Builders<Wo_Repair_Base>.Filter.Eq("floorid", floorid));
            if (!string.IsNullOrEmpty(replace))
                flist.Add(Builders<Wo_Repair_Base>.Filter.Where(it => it.repairreplace.Contains(replace)));
            if (!string.IsNullOrEmpty(systemid))
                flist.Add(Builders<Wo_Repair_Base>.Filter.Eq("systemid", systemid));
            if (!string.IsNullOrEmpty(equipmentid))
                flist.Add(Builders<Wo_Repair_Base>.Filter.Eq("equipmentid", equipmentid));

            if (!string.IsNullOrWhiteSpace(stime))
            {
                flist.Add(Builders<Wo_Repair_Base>.Filter.Gt("createtime", DateTime.Parse(stime.Trim() + " 00:00:00")));
            }
            if (!string.IsNullOrWhiteSpace(etime))
            {
                flist.Add(Builders<Wo_Repair_Base>.Filter.Lt("createtime", DateTime.Parse(etime.Trim() + " 23:59:59")));
            }

            if (string.IsNullOrEmpty(nowrolecode))
            {
                flist.Add(Builders<Wo_Repair_Base>.Filter.Eq("createuser", nowusercode));
            }
            else if (nowrolecode == "WXYG")
            {
                flist.Add(Builders<Wo_Repair_Base>.Filter.Where(it => (it.touser.Count() == 0 || it.touser.Contains(nowusercode) || it.dealuser.Contains(nowusercode))));
            }
            else
            {
                if (!string.IsNullOrEmpty(usercode))
                    flist.Add(Builders<Wo_Repair_Base>.Filter.Where(it => (it.touser.Contains(usercode) || it.dealuser.Contains(usercode))));
            }

            var filter = Builders<Wo_Repair_Base>.Filter.And(flist);
            var list = await _wo_repair_baseRepository.GetByPage(filter, pageindex, pagesize, sort);
            var count = await _wo_repair_baseRepository.CountAsync(filter);
            var obj = new
            {
                rows = list,
                total = count,
            };
            return Success("获取成功", obj);
        }

        /// <summary>
        /// 分页获取
        /// </summary>
        [HttpGet("getlistbypage")]
        public IActionResult GetListByPageAsync(string phone, string wocode, string province, string city, string protypeid, string projectid,
            string buildid, string floorid, string replace, string systemid, string equipmentid, string usercode, string stime, string etime, string channel, string Jwocode,
            int datetype = -1, int state = -1, int wxstate = -1, int pageindex = 1, int pagesize = 10)
        {
            string nowusercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            //var userstr = _signTokenService.GetUserInfoAsync(nowusercode).Result;
            //var userinfo = userstr.ToJObject()["data"].ToString().ToObject<UserInfoModel>();

            
            var userinfos = _sys_user_accountRepository.GetUserInfo(nowusercode);
            var userinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());
            //string nowrolecode = User.Claims.FirstOrDefault(c => c.Type == "RoleCode").Value;

            if (datetype != -1)
            {
                GetStartEndTime(datetype, out stime, out etime);
            }

            int recordCount = 0;
            int totalcount = 0;
            int wzpcount = 0;
            int yjdcount = 0;
            int dpjcount = 0;
            int ygqcount = 0;
            int ywjcount = 0;
            int yqxcount = 0;
            var result = _wo_repair_baseRepository.GetListsByPageglhsz(phone, wocode, province, city, projectid, protypeid, buildid, floorid, replace,
                systemid, equipmentid, usercode, stime, etime, channel, Jwocode, state, wxstate, nowusercode, userinfo, pageindex, pagesize,
                out recordCount, out totalcount, out wzpcount, out yjdcount, out dpjcount, out ygqcount, out ywjcount, out yqxcount);

            List<Wo_Repair_Base> lists = new List<Wo_Repair_Base>();


            #region 时效显示
            foreach (var item in result)
            {
                Wo_Repair_Base woobj = new Wo_Repair_Base();
                #region 获取值

                //var id = GetPropertyValue(item, "id");
                //var wocode1 = GetPropertyValue(item, "wocode");
                //var projectid1 = GetPropertyValue(item, "projectid");
                //var projectname = GetPropertyValue(item, "projectname");
                //var protypeid1 = GetPropertyValue(item, "protypeid");
                //var faulttypeid = GetPropertyValue(item, "faulttypeid");
                //var systemid1 = GetPropertyValue(item, "systemid");
                //var equipmentid1 = GetPropertyValue(item, "equipmentid");
                //var channel1 = GetPropertyValue(item, "channel");
                //var provincecode = GetPropertyValue(item, "provincecode");
                //var province1 = GetPropertyValue(item, "province");
                //var citycode = GetPropertyValue(item, "citycode");
                //var city1 = GetPropertyValue(item, "city");
                //var buildid1 = GetPropertyValue(item, "buildid");
                //var floorid1 = GetPropertyValue(item, "floorid");
                //var repairreplace = GetPropertyValue(item, "repairreplace");
                //var repairoom = GetPropertyValue(item, "repairoom");
                //var repairtime = GetPropertyValue(item, "repairtime");
                //var contactway = GetPropertyValue(item, "contactway");
                //var repairman = GetPropertyValue(item, "repairman");
                //var faultdescripe = GetPropertyValue(item, "faultdescripe");
                //var faultpic = GetPropertyValue(item, "faultpic");
                //var appointtime = GetPropertyValue(item, "appointtime");
                //var wostate = GetPropertyValue(item, "wostate");
                //var createtime = GetPropertyValue(item, "createtime");
                //var createuser = GetPropertyValue(item, "createuser");
                //var createusername = GetPropertyValue(item, "createusername");
                //var touser = GetPropertyValue(item, "touser");
                //var tousername = GetPropertyValue(item, "tousername");
                //var totime = GetPropertyValue(item, "totime");
                //var totype = GetPropertyValue(item, "totype");
                //var dealuser = GetPropertyValue(item, "dealuser");
                //var dealusername = GetPropertyValue(item, "dealusername");
                //var dealtime = GetPropertyValue(item, "dealtime");
                //var ismaterials = GetPropertyValue(item, "ismaterials");
                //var materials = GetPropertyValue(item, "materials");
                //var islosematerials = GetPropertyValue(item, "islosematerials");
                //var losematerials = GetPropertyValue(item, "losematerials");
                //var hanguptime = GetPropertyValue(item, "hanguptime");
                //var arrivetime = GetPropertyValue(item, "arrivetime");
                //var evaluatetime = GetPropertyValue(item, "evaluatetime");
                //var canceltime = GetPropertyValue(item, "canceltime");
                //var score = GetPropertyValue(item, "score");
                //var Jwocode1 = GetPropertyValue(item, "Jwocode");
                //var Pwcode = GetPropertyValue(item, "Pwcode");
                //var CLpeoplefrom = GetPropertyValue(item, "CLpeoplefrom");
                //var tobztimes = GetPropertyValue(item, "tobztimes");
                //var remark = GetPropertyValue(item, "remark");
                //var temid = GetPropertyValue(item, "temid");
                //var postlist = GetPropertyValue(item, "postlist");
                //var wxtimeleths = GetPropertyValue(item, "wxtimeleths");
                #endregion

            }

            #endregion


            var obj = new
            {
                rows = result,
                total = recordCount,
                totalcounts = totalcount,//全部
                wzpcounts = wzpcount,//未指派
                yjdcounts = yjdcount,//已接单
                dpjcounts = dpjcount,//待评价
                ygqcounts = ygqcount,//已挂起
                ywjcounts = ywjcount,//已完结
                yqxcounts = yqxcount//已取消
            };

            return Success("获取成功", obj);
        }

        /// <summary>
        /// 导处工单数据
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="wocode"></param>
        /// <param name="province"></param>
        /// <param name="city"></param>
        /// <param name="protypeid"></param>
        /// <param name="projectid"></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="datetype"></param>
        /// <param name="state"></param>
        /// <param name="wxstate"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet("excelwocodeinfo")]
        [AllowAnonymous]
        public async Task<IActionResult> excelwocodeinfo(string useraccount, string phone, string wocode, string province, string city, string protypeid, string projectid,
            string buildid, string floorid, string replace, string systemid, string equipmentid, string usercode, string stime, string etime, string channel, string Jwocode,
            int datetype = -1, int state = -1, int wxstate = -1, int pageindex = 1, int pagesize = 10)
        {

            string nowusercode = useraccount;// User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
                                             //var userstr = _signTokenService.GetUserInfoAsync(nowusercode).Result;
                                             //var userinfo = userstr.ToJObject()["data"].ToString().ToObject<UserInfoModel>();

            
            var userinfos = _sys_user_accountRepository.GetUserInfo(nowusercode);
            var userinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());

            if (datetype != -1)
            {
                GetStartEndTime(datetype, out stime, out etime);
            }

            int recordCount = 0;
            int totalcount = 0;
            int wzpcount = 0;
            int yjdcount = 0;
            int dpjcount = 0;
            int ygqcount = 0;
            int ywjcount = 0;
            int yqxcount = 0;

            var result = _wo_repair_baseRepository.excelwocodelhsz(phone, wocode, province, city, projectid, protypeid, buildid, floorid, replace,
               systemid, equipmentid, usercode, stime, etime, channel, Jwocode, state, wxstate, nowusercode, userinfo, pageindex, pagesize,
               out recordCount, out totalcount, out wzpcount, out yjdcount, out dpjcount, out ygqcount, out ywjcount, out yqxcount);

            List<Model.Dto.ExcelWo_Repair_Base> lists = new List<Model.Dto.ExcelWo_Repair_Base>();
            List<Wo_Repair_Base> wolist = new List<Wo_Repair_Base>();

            #region 时效显示
            foreach (var item in result)
            {
                //var item = @"+item+";
                //Object obj = JsonConvert.DeserializeObject(item.ToString());

                var basemodel = ConvertObject<Wo_Repair_Base>(item);

                //Newtonsoft.Json.Linq.JObject js = obj as Newtonsoft.Json.Linq.JObject;//把上面的obj转换为 Jobject对象
                //string data = js["tousername"].ToString();

                //var serializer = new JavaScriptSerializer();
                ////将object对象转换为json字符
                //var json = serializer.Serialize(asObject);
                ////将json字符转换为实体对象
                //var t = serializer.Deserialize<T>(json);

                Model.Dto.ExcelWo_Repair_Base woobj = new Model.Dto.ExcelWo_Repair_Base();
                #region 获取值
                #region 导出赋值
                if (GetPropertyValue(item, "wostate").ToString() == "0")
                {
                    woobj.wostate = "新工单";
                }
                if (GetPropertyValue(item, "wostate").ToString() == "1")
                {
                    woobj.wostate = "已接单";
                }
                if (GetPropertyValue(item, "wostate").ToString() == "2")
                {
                    woobj.wostate = "已处理";
                }
                if (GetPropertyValue(item, "wostate").ToString() == "3")
                {
                    woobj.wostate = "已完结";
                }
                if (GetPropertyValue(item, "wostate").ToString() == "7")
                {
                    woobj.wostate = "挂起";
                }
                woobj.wocode = GetPropertyValue(item, "wocode").ToString();
                if (GetPropertyValue(item, "channel").ToString() == "1")
                {
                    woobj.channel = "电话";
                }
                if (GetPropertyValue(item, "channel").ToString() == "2")
                {
                    woobj.channel = "安卓";
                }
                if (GetPropertyValue(item, "channel").ToString() == "3")
                {
                    woobj.channel = "IOS";
                }
                if (GetPropertyValue(item, "channel").ToString() == "4")
                {
                    woobj.channel = "PC端";
                }
         
                woobj.projectname = GetPropertyValue(item, "projectname") != null ? GetPropertyValue(item, "projectname").ToString() : "";
                woobj.repairreplace = GetPropertyValue(item, "repairreplace") != null ? GetPropertyValue(item, "repairreplace").ToString() : "";
                woobj.repairoom = GetPropertyValue(item, "repairoom") != null ? GetPropertyValue(item, "repairoom").ToString() : "";
                woobj.repairtime = GetPropertyValue(item, "repairtime") != null ? Convert.ToDateTime(GetPropertyValue(item, "repairtime").ToString()).ToString("yyyy - MM - dd  HH: mm:ss") : "";
                woobj.bxddcenter = GetPropertyValue(item, "createusername") != null ? GetPropertyValue(item, "createusername").ToString() : "";
                woobj.contactway = GetPropertyValue(item, "contactway") != null ? GetPropertyValue(item, "contactway").ToString() : "";
                woobj.faultdescripe = GetPropertyValue(item, "faultdescripe") != null ? GetPropertyValue(item, "faultdescripe").ToString() : "";
                woobj.Pwcode = GetPropertyValue(item, "Pwcode") != null ? GetPropertyValue(item, "Pwcode").ToString() : "";
                woobj.Jwocode = GetPropertyValue(item, "Jwocode") != null ? GetPropertyValue(item, "Jwocode").ToString() : "";
                woobj.formatterToUser = basemodel.tousername.Count >0 ?  basemodel.tousername[0]:"";


              //  woobj.formatterToUser =  GetPropertyValue(item, "tousername") != null ? (GetPropertyValue(item, "tousername")).ToString() : "";
                woobj.tobztimes = GetPropertyValue(item, "tobztimes") != null ? Convert.ToDateTime(GetPropertyValue(item, "tobztimes").ToString()).ToString("yyyy - MM - dd  HH: mm:ss") : "";
                woobj.dealtime = GetPropertyValue(item, "dealtime") != null ? Convert.ToDateTime(GetPropertyValue(item, "dealtime")).ToString("yyyy - MM - dd  HH: mm:ss").ToString() : "";
                woobj.CLpeoplefrom = GetPropertyValue(item, "CLpeoplefrom") != null ? GetPropertyValue(item, "CLpeoplefrom").ToString() : "";
                #endregion
                #region 无用
                //woobj.score                 = GetPropertyValue(item, "score").ToString();
                //woobj.cdealtime             = GetPropertyValue(item, "cdealtime").ToString();
                //woobj.wxtimeleths = GetPropertyValue(item, "wxtimeleths").ToString();
                //var id = GetPropertyValue(item, "id");
                //var wocode1 = GetPropertyValue(item, "wocode");
                //var projectid1 = GetPropertyValue(item, "projectid");
                //var projectname = GetPropertyValue(item, "projectname");
                //var protypeid1 = GetPropertyValue(item, "protypeid");
                //var faulttypeid = GetPropertyValue(item, "faulttypeid");
                //var systemid1 = GetPropertyValue(item, "systemid");
                //var equipmentid1 = GetPropertyValue(item, "equipmentid");
                //var channel1 = GetPropertyValue(item, "channel");
                //var provincecode = GetPropertyValue(item, "provincecode");
                //var province1 = GetPropertyValue(item, "province");
                //var citycode = GetPropertyValue(item, "citycode");
                //var city1 = GetPropertyValue(item, "city");
                //var buildid1 = GetPropertyValue(item, "buildid");
                //var floorid1 = GetPropertyValue(item, "floorid");
                //var repairreplace = GetPropertyValue(item, "repairreplace");
                //var repairoom = GetPropertyValue(item, "repairoom");
                //var repairtime = GetPropertyValue(item, "repairtime");
                //var contactway = GetPropertyValue(item, "contactway");
                //var repairman = GetPropertyValue(item, "repairman");
                //var faultdescripe = GetPropertyValue(item, "faultdescripe");
                //var faultpic = GetPropertyValue(item, "faultpic");
                //var appointtime = GetPropertyValue(item, "appointtime");
                //var wostate = GetPropertyValue(item, "wostate");
                //var createtime = GetPropertyValue(item, "createtime");
                //var createuser = GetPropertyValue(item, "createuser");
                //var createusername = GetPropertyValue(item, "createusername");
                //var touser = GetPropertyValue(item, "touser");
                //var tousername = GetPropertyValue(item, "tousername");
                //var totime = GetPropertyValue(item, "totime");
                //var totype = GetPropertyValue(item, "totype");
                //var dealuser = GetPropertyValue(item, "dealuser");
                //var dealusername = GetPropertyValue(item, "dealusername");
                //var dealtime = GetPropertyValue(item, "dealtime");
                //var ismaterials = GetPropertyValue(item, "ismaterials");
                //var materials = GetPropertyValue(item, "materials");
                //var islosematerials = GetPropertyValue(item, "islosematerials");
                //var losematerials = GetPropertyValue(item, "losematerials");
                //var hanguptime = GetPropertyValue(item, "hanguptime");
                //var arrivetime = GetPropertyValue(item, "arrivetime");
                //var evaluatetime = GetPropertyValue(item, "evaluatetime");
                //var canceltime = GetPropertyValue(item, "canceltime");
                //var score = GetPropertyValue(item, "score");
                //var Jwocode1 = GetPropertyValue(item, "Jwocode");
                //var Pwcode = GetPropertyValue(item, "Pwcode");
                //var CLpeoplefrom = GetPropertyValue(item, "CLpeoplefrom");
                //var tobztimes = GetPropertyValue(item, "tobztimes");
                //var remark = GetPropertyValue(item, "remark");
                //var temid = GetPropertyValue(item, "temid");
                //var postlist = GetPropertyValue(item, "postlist");
                //var wxtimeleths = GetPropertyValue(item, "wxtimeleths"); 
                #endregion
                #endregion
                lists.Add(woobj);
            }

            #endregion

            NPOIHelper npoi = new NPOIHelper();
            var tb = ModelConvertHelper<Model.Dto.ExcelWo_Repair_Base>.ListToTable(lists, null);
            if (tb != null)
            {
                //导出dtnew 
                string[] cols = { "工单状态", "工单编号", "渠道来源", "项目名称", "报修科室", "报修地点", "报修时间", "报修人", "联系方式", "设备名称", "原接单班组", "接单班组", "接单人", "接单时间", "处理时间", "来源" };

                byte[] sm = npoi.ExportToExcel1(tb, cols);
                if (sm != null)
                {
                    return File(sm, "application/vnd.ms-excel", "报修工单.xlsx");
                }
                else
                {
                    return Error("导出失败");
                }
            }
            return Error("没有数据");

        }
        private T ConvertObject<T>(object asObject) where T : new()
        {
            //创建实体对象实例
            var t = Activator.CreateInstance<T>();
            if (asObject != null)
            {
                Type type = asObject.GetType();
                //遍历实体对象属性
                foreach (var info in typeof(T).GetProperties())
                {
                    object obj = null;
                    //取得object对象中此属性的值
                    var val = type.GetProperty(info.Name)?.GetValue(asObject);
                    if (val != null)
                    {
                        //非泛型
                        if (!info.PropertyType.IsGenericType)
                            obj = Convert.ChangeType(val, info.PropertyType);
                        else//泛型Nullable<>
                        {
                            Type genericTypeDefinition = info.PropertyType.GetGenericTypeDefinition();
                            if (genericTypeDefinition == typeof(Nullable<>))
                            {
                                obj = Convert.ChangeType(val, Nullable.GetUnderlyingType(info.PropertyType));
                            }
                            else
                            {
                                obj = Convert.ChangeType(val, info.PropertyType);
                            }
                        }
                        info.SetValue(t, obj, null);
                    }
                }
            }
            return t;
        }

        /// 获取一个类指定的属性值
        /// </summary>
        /// <param name="info">object对象</param>
        /// <param name="field">属性名称</param>
        /// <returns></returns>
        public static object GetPropertyValue(object info, string field)
        {
            if (info == null) return null;
            Type t = info.GetType();
            IEnumerable<System.Reflection.PropertyInfo> property = from pi in t.GetProperties() where pi.Name.ToLower() == field.ToLower() select pi;
            return property.First().GetValue(info, null);
        }
        /// <summary>
        /// 分页获取当前角色的工单 GetListsByPagerole
        /// </summary>
       
        //报修工单访问的
        [HttpGet("getlistbypagerole")]
        public IActionResult GetListByPageAsync1(string phone, string wocode, string province, string city, string protypeid, string projectid,
            string buildid, string floorid, string replace, string systemid, string equipmentid, string usercode, string stime, string etime,
            int datetype = -1, int state = -1, int wxstate = -1, int pageindex = 1, int pagesize = 10)
        {

            string nowusercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            //var userstr = _signTokenService.GetUserInfoAsync(nowusercode).Result;
            //var userinfo = userstr.ToJObject()["data"].ToString().ToObject<UserInfoModel>();

            
            var userinfos = _sys_user_accountRepository.GetUserInfo(nowusercode);
            var userinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());

            //string nowrolecode = User.Claims.FirstOrDefault(c => c.Type == "RoleCode").Value;

            if (datetype != -1)
            {
                GetStartEndTime(datetype, out stime, out etime);
            }
            var rolename = userinfo.role_name;
            int recordCount = 0;
            int totalcount = 0;
            int wzpcount = 0;
            int yjdcount = 0;
            int dpjcount = 0;
            int ygqcount = 0;
            int ywjcount = 0;
            int yqxcount = 0;
            var result = _wo_repair_baseRepository.GetListsByPagerole(phone, wocode, province, city, projectid, protypeid, buildid, floorid, replace,
                systemid, equipmentid, usercode, stime, etime, state, wxstate, nowusercode, userinfo, pageindex, pagesize, rolename,
                out recordCount, out totalcount, out wzpcount, out yjdcount, out dpjcount, out ygqcount, out ywjcount, out yqxcount);

            var obj = new
            {
                rows = result,
                total = recordCount,
                totalcounts = totalcount,//全部
                wzpcounts = wzpcount,//未指派
                yjdcounts = yjdcount,//已接单
                dpjcounts = dpjcount,//待评价
                ygqcounts = ygqcount,//已挂起
                ywjcounts = ywjcount,//已完结
                yqxcounts = yqxcount//已取消
            };

            return Success("获取成功", obj);
        }
        #endregion
        #region 日接单追踪报表
        /// <summary>
        /// 日接单追踪统计
        /// </summary>
        /// <param name="stime"></param>
        /// <param name="etime"></param>
        /// <returns></returns>
        [HttpGet("getdaywxtjlist")]
        public async Task<IActionResult> GetDaywxtjlist(string stime, string etime, string daytype, string isdjtype)
        {
            #region 天 周 月 年
            //var datenow = DateTime.Now;
            //DateTime? startime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
            //DateTime? endtime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));
            //if (!string.IsNullOrEmpty(daytype))
            //{
            //    switch (daytype)
            //    {
            //        case "week":
            //            startime = Convert.ToDateTime(datenow.AddDays(1 - Convert.ToInt32(datenow.DayOfWeek.ToString("d"))).ToString("yyyy-MM-dd 00:00:00"));//本周周一
            //            endtime = Convert.ToDateTime(datenow.AddDays(6).ToString("yyyy-MM-dd  23:59:59"));//本周周日
            //            break;
            //        case "month":
            //            startime = Convert.ToDateTime(datenow.AddDays(1 - datenow.Day).ToString("yyyy-MM-dd 00:00:00"));  //本月月初  
            //            endtime = Convert.ToDateTime(datenow.AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd 23:59:59"));  //本月月末  
            //            break;
            //        case "year":
            //            startime = Convert.ToDateTime(new DateTime(datenow.Year, 1, 1).ToString("yyyy-MM-dd 00:00:00"));  //本年年初  
            //            endtime = Convert.ToDateTime(new DateTime(datenow.Year, 12, 31).ToString("yyyy-MM-dd  23:59:59"));  //本年年末  
            //            break;
            //        default:
            //            break;
            //    }
            //}
            //else
            //{
            //    //先确认时间是否为空 空则用当前时间天来判断
            //    if (!string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
            //    {

            //        startime = DateTime.Parse(stime.Trim() + " 00:00:00");
            //        endtime = DateTime.Parse(etime.Trim() + " 23:59:59");
            //    }
            //    else
            //    {
            //        startime = null;
            //        endtime = null;
            //    }

            //}
            #endregion
            var datenow = DateTime.Now;
            DateTime? startime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
            DateTime? endtime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));
            if (!string.IsNullOrEmpty(daytype))
            {
                switch (daytype)
                {
                    case "week":
                        startime = Convert.ToDateTime(datenow.AddDays(1 - Convert.ToInt32(datenow.DayOfWeek.ToString("d"))).ToString("yyyy-MM-dd 00:00:00"));//本周周一
                        endtime = Convert.ToDateTime(datenow.AddDays(6).ToString("yyyy-MM-dd  23:59:59"));//本周周日
                        break;
                    case "month":
                        startime = Convert.ToDateTime(datenow.AddDays(1 - datenow.Day).ToString("yyyy-MM-dd 00:00:00"));  //本月月初  
                        endtime = Convert.ToDateTime(datenow.AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd 23:59:59"));  //本月月末  
                        break;
                    case "year":
                        startime = Convert.ToDateTime(new DateTime(datenow.Year, 1, 1).ToString("yyyy-MM-dd 00:00:00"));  //本年年初  
                        endtime = Convert.ToDateTime(new DateTime(datenow.Year, 12, 31).ToString("yyyy-MM-dd  23:59:59"));  //本年年末  
                        break;
                    default:
                        break;
                }
            }
            else
            {
                //先确认时间是否为空 空则用当前时间天来判断
                if (!string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                {

                    startime = DateTime.Parse(stime.Trim() + " 00:00:00");
                    endtime = DateTime.Parse(etime.Trim() + " 23:59:59");
                }
                else
                {
                    startime = null;
                    endtime = null;
                }

            }

            List<Wxzzrbblists> list = new List<Wxzzrbblists>();
            #region 动态

            if (isdjtype == "静态")
            {
                if (startime != null && endtime != null)
                {
                    #region 时间不空
                    //获取所有班组信息
                    var bzinfolist = await _ISys_Role_InfoRepository.Get(p => p.role_code == "YWYG" && p.role_name.Contains("班") && p.state_flag == 1);
                    foreach (var item in bzinfolist)
                    {
                        Wxzzrbblists wxzzrbb = new Wxzzrbblists();
                        wxzzrbb.deptname = item.role_name;//班组名称先确定


                        //获取日接单量
                        wxzzrbb.daycounts = _wo_repair_baseRepository.Count(p => p.Jwocode == item.role_name
                                                                                && p.isdelete == 0
                                                                                && p.createtime >= startime
                                                                                && p.createtime <= endtime).Result.ToString();
                        //获取本班组接的所有单子 已完结的 状态等于3和2的
                        var codefinilist = await _wo_repair_baseRepository.Get(p => p.Jwocode == item.role_name
                                                                             && p.isdelete == 0
                                                                             && p.createtime >= startime
                                                                             && p.createtime <= endtime
                                                                             && (p.wostate == 3 || p.wostate == 2));
                        //赋值
                        //已完成的数据 在时间段内创建并且处理时间在这个时间段内 才算静态完成单子 
                        List<finishcentlists> finisherlist = new List<finishcentlists>();

                        List<nofinish> nofinishlist = new List<nofinish>();

                        foreach (var codeitem in codefinilist)
                        {
                            if (codeitem.dealtime >= startime && codeitem.dealtime <= endtime)
                            {
                                finishcentlists finishlist = new finishcentlists();
                                finishlist.wxcenters = codeitem.repairreplace + "     " + codeitem.repairoom + "     " + codeitem.faultdescripe;//维修物品
                                if (codeitem.CLpeoplefrom != null)
                                {
                                    if (codeitem.CLpeoplefrom == "本部")
                                    {
                                        finishlist.wxrfrom = "本院";//维修人来源                        }

                                    }
                                    else
                                    {
                                        finishlist.wxrfrom = codeitem.CLpeoplefrom;//维修人来源
                                    }
                                }
                                else
                                {
                                    finishlist.wxrfrom = "";//维修人来源
                                }

                                #region 满意不满意
                                switch (codeitem.score)
                                {
                                    case 1:
                                        finishlist.myfens = "";
                                        finishlist.nomyfens = "差";
                                        finishlist.dpjcenter = "";
                                        break;
                                    case 2:
                                        finishlist.myfens = "";
                                        finishlist.nomyfens = "不满意";
                                        finishlist.dpjcenter = "";
                                        break;
                                    case 3:
                                        finishlist.myfens = "一般";
                                        finishlist.nomyfens = "";
                                        finishlist.dpjcenter = "";
                                        break;
                                    case 4:
                                        finishlist.myfens = "满意";
                                        finishlist.nomyfens = "";
                                        finishlist.dpjcenter = "";
                                        break;
                                    case 5:
                                        finishlist.myfens = "很满意";
                                        finishlist.dpjcenter = "";
                                        finishlist.nomyfens = "";
                                        break;
                                    default:
                                        finishlist.myfens = "";
                                        finishlist.nomyfens = "";
                                        finishlist.dpjcenter = "待评价";
                                        break;
                                }
                                #endregion

                                #region 超时时长
                                //超时时长
                                //根据id获取维修项目时效时间
                                var wxxmtime = await _wxwpstimeRepository.GetSingle(p => p.id == codeitem.wxtimeid);
                                if (wxxmtime != null)
                                {

                                    if (codeitem.dealtime != null && codeitem.tobztimes != null)
                                    {
                                        TimeSpan ts = Convert.ToDateTime(codeitem.dealtime).Subtract(Convert.ToDateTime(codeitem.tobztimes));
                                        double hours = ts.TotalHours;//24.0

                                        if ((hours - wxxmtime.timecount) > 0)
                                        {

                                            finishlist.cstimecount = Math.Round(hours - wxxmtime.timecount, 2).ToString();// ""; 
                                        }
                                        else
                                        {
                                            finishlist.cstimecount = "";
                                        }
                                    }


                                }
                                else
                                {
                                    finishlist.cstimecount = "";
                                }

                                #endregion
                                finisherlist.Add(finishlist);
                            }
                            else
                            {
                                nofinish nofilist = new nofinish();
                                nofilist.nofinishcenter = codeitem.repairreplace + "     " + codeitem.repairoom + "     " + codeitem.faultdescripe;//未完成维修的东西
                                nofilist.nofinibecause = "未处理";
                                nofinishlist.Add(nofilist);
                            }

                        }
                        if (finisherlist.Count() == 0)
                        {
                            finishcentlists finishlist = new finishcentlists();
                            finishlist.wxcenters = "";//维修物品
                            finishlist.wxrfrom = "";//维修人来源
                            finishlist.myfens = "";//满意
                            finishlist.nomyfens = "";//不满意
                            finishlist.dpjcenter = "";//待评价
                            finishlist.cstimecount = "";
                            finisherlist.Add(finishlist);
                        }
                        wxzzrbb.finishlistcenter = finisherlist;
                        //获取未完成的单子
                        var codenofinilist = await _wo_repair_baseRepository.Get(p => p.Jwocode == item.role_name
                                                                               && p.isdelete == 0
                                                                               && p.createtime >= startime
                                                                               && p.createtime <= endtime
                                                                               && (p.wostate != 3 && p.wostate != 2));

                        foreach (var nocodeitem in codenofinilist)
                        {
                            nofinish nofilist = new nofinish();
                            nofilist.nofinishcenter = nocodeitem.repairreplace + "     " + nocodeitem.repairoom + "     " + nocodeitem.faultdescripe;//未完成维修的东西
                            if (nocodeitem.wostate == 7)
                            {
                                var dmodel = await _wo_repair_dealRepository.GetSingle(p => p.wocode == nocodeitem.wocode);
                                if (dmodel != null)
                                {
                                    nofilist.nofinibecause = "挂起:" + dmodel.detail;
                                }
                                else
                                {
                                    nofilist.nofinibecause = "挂起";
                                }
                            }
                            else
                            {
                                nofilist.nofinibecause = "未处理";
                            }
                            nofinishlist.Add(nofilist);
                        }
                        if (codenofinilist.Count() == 0)
                        {
                            nofinish nofilist = new nofinish();
                            nofilist.nofinishcenter = "";//未完成维修的东西
                            nofilist.nofinibecause = "";//未完成原因
                            nofinishlist.Add(nofilist);
                        }
                        wxzzrbb.nofinishcenter = nofinishlist;//未完成内容
                                                              //都加到最终表中
                        list.Add(wxzzrbb);
                    }
                    #endregion
                }
                else
                {
                    #region 时间wei空
                    //获取所有班组信息
                    var bzinfolist = await _ISys_Role_InfoRepository.Get(p => p.role_code == "YWYG" && p.role_name.Contains("班") && p.state_flag == 1);
                    foreach (var item in bzinfolist)
                    {
                        Wxzzrbblists wxzzrbb = new Wxzzrbblists();
                        wxzzrbb.deptname = item.role_name;//班组名称先确定


                        //获取日接单量
                        wxzzrbb.daycounts = _wo_repair_baseRepository.Count(p => p.Jwocode == item.role_name
                                                                                && p.isdelete == 0
                                                                               ).Result.ToString();
                        //获取本班组接的所有单子 已完结的 状态等于3和2的
                        var codefinilist = await _wo_repair_baseRepository.Get(p => p.Jwocode == item.role_name
                                                                             && p.isdelete == 0
                                                                             && (p.wostate == 3 || p.wostate == 2));
                        //赋值
                        //已完成的数据 在时间段内创建并且处理时间在这个时间段内 才算静态完成单子 
                        List<finishcentlists> finisherlist = new List<finishcentlists>();

                        List<nofinish> nofinishlist = new List<nofinish>();

                        foreach (var codeitem in codefinilist)
                        {
                            if (codeitem.dealtime >= startime && codeitem.dealtime <= endtime)
                            {
                                finishcentlists finishlist = new finishcentlists();
                                finishlist.wxcenters = codeitem.repairreplace + "     " + codeitem.repairoom + "     " + codeitem.faultdescripe;//维修物品
                                if (codeitem.CLpeoplefrom != null)
                                {
                                    if (codeitem.CLpeoplefrom == "本部")
                                    {
                                        finishlist.wxrfrom = "本院";//维修人来源                        }

                                    }
                                    else
                                    {
                                        finishlist.wxrfrom = codeitem.CLpeoplefrom;//维修人来源
                                    }
                                }
                                else
                                {
                                    finishlist.wxrfrom = "";//维修人来源
                                }

                                #region 满意不满意
                                switch (codeitem.score)
                                {
                                    case 1:
                                        finishlist.myfens = "";
                                        finishlist.nomyfens = "差";
                                        finishlist.dpjcenter = "";
                                        break;
                                    case 2:
                                        finishlist.myfens = "";
                                        finishlist.nomyfens = "不满意";
                                        finishlist.dpjcenter = "";
                                        break;
                                    case 3:
                                        finishlist.myfens = "一般";
                                        finishlist.nomyfens = "";
                                        finishlist.dpjcenter = "";
                                        break;
                                    case 4:
                                        finishlist.myfens = "满意";
                                        finishlist.nomyfens = "";
                                        finishlist.dpjcenter = "";
                                        break;
                                    case 5:
                                        finishlist.myfens = "很满意";
                                        finishlist.dpjcenter = "";
                                        finishlist.nomyfens = "";
                                        break;
                                    default:
                                        finishlist.myfens = "";
                                        finishlist.nomyfens = "";
                                        finishlist.dpjcenter = "待评价";
                                        break;
                                }
                                #endregion

                                #region 超时时长
                                //超时时长
                                //根据id获取维修项目时效时间
                                var wxxmtime = await _wxwpstimeRepository.GetSingle(p => p.id == codeitem.wxtimeid);
                                if (wxxmtime != null)
                                {

                                    if (codeitem.dealtime != null && codeitem.tobztimes != null)
                                    {
                                        TimeSpan ts = Convert.ToDateTime(codeitem.dealtime).Subtract(Convert.ToDateTime(codeitem.tobztimes));
                                        double hours = ts.TotalHours;//24.0

                                        if ((hours - wxxmtime.timecount) > 0)
                                        {

                                            finishlist.cstimecount = Math.Round(hours - wxxmtime.timecount, 2).ToString();// ""; 
                                        }
                                        else
                                        {
                                            finishlist.cstimecount = "";
                                        }
                                    }


                                }
                                else
                                {
                                    finishlist.cstimecount = "";
                                }

                                #endregion
                                finisherlist.Add(finishlist);
                            }
                            else
                            {
                                nofinish nofilist = new nofinish();
                                nofilist.nofinishcenter = codeitem.repairreplace + "     " + codeitem.repairoom + "     " + codeitem.faultdescripe;//未完成维修的东西
                                nofilist.nofinibecause = "未处理";
                                nofinishlist.Add(nofilist);
                            }

                        }
                        if (finisherlist.Count() == 0)
                        {
                            finishcentlists finishlist = new finishcentlists();
                            finishlist.wxcenters = "";//维修物品
                            finishlist.wxrfrom = "";//维修人来源
                            finishlist.myfens = "";//满意
                            finishlist.nomyfens = "";//不满意
                            finishlist.dpjcenter = "";//待评价
                            finishlist.cstimecount = "";
                            finisherlist.Add(finishlist);
                        }
                        wxzzrbb.finishlistcenter = finisherlist;
                        //获取未完成的单子
                        var codenofinilist = await _wo_repair_baseRepository.Get(p => p.Jwocode == item.role_name
                                                                               && p.isdelete == 0
                                                                               && (p.wostate != 3 && p.wostate != 2));

                        foreach (var nocodeitem in codenofinilist)
                        {
                            nofinish nofilist = new nofinish();
                            nofilist.nofinishcenter = nocodeitem.repairreplace + "     " + nocodeitem.repairoom + "     " + nocodeitem.faultdescripe;//未完成维修的东西
                            if (nocodeitem.wostate == 7)
                            {
                                var dmodel = await _wo_repair_dealRepository.GetSingle(p => p.wocode == nocodeitem.wocode);
                                if (dmodel != null)
                                {
                                    nofilist.nofinibecause = "挂起:" + dmodel.detail;
                                }
                                else
                                {
                                    nofilist.nofinibecause = "挂起";
                                }
                            }
                            else
                            {
                                nofilist.nofinibecause = "未处理";
                            }
                            nofinishlist.Add(nofilist);
                        }
                        if (codenofinilist.Count() == 0)
                        {
                            nofinish nofilist = new nofinish();
                            nofilist.nofinishcenter = "";//未完成维修的东西
                            nofilist.nofinibecause = "";//未完成原因
                            nofinishlist.Add(nofilist);
                        }
                        wxzzrbb.nofinishcenter = nofinishlist;//未完成内容
                                                              //都加到最终表中
                        list.Add(wxzzrbb);
                    }
                    #endregion
                }

            }
            else
            {
                if (startime != null && endtime != null)
                {
                    #region 时间不为空
                    //获取所有班组信息
                    var bzinfolist = await _ISys_Role_InfoRepository.Get(p => p.role_code == "YWYG" && p.role_name.Contains("班") && p.state_flag == 1);
                    foreach (var item in bzinfolist)
                    {
                        Wxzzrbblists wxzzrbb = new Wxzzrbblists();
                        wxzzrbb.deptname = item.role_name;//班组名称先确定


                        //获取日接单量
                        wxzzrbb.daycounts = _wo_repair_baseRepository.Count(p => p.Jwocode == item.role_name
                                                                                && p.isdelete == 0
                                                                                && p.createtime >= startime
                                                                                && p.createtime <= endtime).Result.ToString();
                        //获取本班组接的所有单子 已完结的 状态等于3和2的
                        var codefinilist = await _wo_repair_baseRepository.Get(p => p.Jwocode == item.role_name
                                                                             && p.isdelete == 0
                                                                             && p.createtime >= startime
                                                                             && p.createtime <= endtime
                                                                             && (p.wostate == 3 || p.wostate == 2));
                        //赋值
                        List<finishcentlists> finisherlist = new List<finishcentlists>();

                        foreach (var codeitem in codefinilist)
                        {
                            finishcentlists finishlist = new finishcentlists();
                            finishlist.wxcenters = codeitem.repairreplace + "     " + codeitem.repairoom + "     " + codeitem.faultdescripe;//维修物品
                            if (codeitem.CLpeoplefrom != null)
                            {
                                if (codeitem.CLpeoplefrom == "本部")
                                {
                                    finishlist.wxrfrom = "本院";//维修人来源                        }

                                }
                                else
                                {
                                    finishlist.wxrfrom = codeitem.CLpeoplefrom;//维修人来源
                                }
                            }
                            else
                            {
                                finishlist.wxrfrom = "";//维修人来源
                            }

                            #region 满意不满意
                            switch (codeitem.score)
                            {
                                case 1:
                                    finishlist.myfens = "";
                                    finishlist.nomyfens = "差";
                                    finishlist.dpjcenter = "";
                                    break;
                                case 2:
                                    finishlist.myfens = "";
                                    finishlist.nomyfens = "不满意";
                                    finishlist.dpjcenter = "";
                                    break;
                                case 3:
                                    finishlist.myfens = "一般";
                                    finishlist.nomyfens = "";
                                    finishlist.dpjcenter = "";
                                    break;
                                case 4:
                                    finishlist.myfens = "满意";
                                    finishlist.nomyfens = "";
                                    finishlist.dpjcenter = "";
                                    break;
                                case 5:
                                    finishlist.myfens = "很满意";
                                    finishlist.dpjcenter = "";
                                    finishlist.nomyfens = "";
                                    break;
                                default:
                                    finishlist.myfens = "";
                                    finishlist.nomyfens = "";
                                    finishlist.dpjcenter = "待评价";
                                    break;
                            }
                            #endregion

                            #region 超时时长
                            //超时时长
                            //根据id获取维修项目时效时间
                            var wxxmtime = await _wxwpstimeRepository.GetSingle(p => p.id == codeitem.wxtimeid);
                            if (wxxmtime != null)
                            {

                                if (codeitem.dealtime != null && codeitem.tobztimes != null)
                                {
                                    TimeSpan ts = Convert.ToDateTime(codeitem.dealtime).Subtract(Convert.ToDateTime(codeitem.tobztimes));
                                    double hours = ts.TotalHours;//24.0

                                    if ((hours - wxxmtime.timecount) > 0)
                                    {

                                        finishlist.cstimecount = Math.Round(hours - wxxmtime.timecount, 2).ToString();// ""; 
                                    }
                                    else
                                    {
                                        finishlist.cstimecount = "";
                                    }
                                }


                            }
                            else
                            {
                                finishlist.cstimecount = "";
                            }

                            #endregion



                            finisherlist.Add(finishlist);

                        }
                        if (finisherlist.Count() == 0)
                        {
                            finishcentlists finishlist = new finishcentlists();
                            finishlist.wxcenters = "";//维修物品
                            finishlist.wxrfrom = "";//维修人来源
                            finishlist.myfens = "";//满意
                            finishlist.nomyfens = "";//不满意
                            finishlist.dpjcenter = "";//待评价
                            finishlist.cstimecount = "";
                            finisherlist.Add(finishlist);
                        }
                        wxzzrbb.finishlistcenter = finisherlist;
                        //获取未完成的单子
                        var codenofinilist = await _wo_repair_baseRepository.Get(p => p.Jwocode == item.role_name
                                                                               && p.isdelete == 0
                                                                               && p.createtime >= startime
                                                                               && p.createtime <= endtime
                                                                               && (p.wostate != 3 && p.wostate != 2));
                        List<nofinish> nofinishlist = new List<nofinish>();
                        foreach (var nocodeitem in codenofinilist)
                        {
                            nofinish nofilist = new nofinish();
                            nofilist.nofinishcenter = nocodeitem.repairreplace + "     " + nocodeitem.repairoom + "     " + nocodeitem.faultdescripe;//未完成维修的东西
                            if (nocodeitem.wostate == 7)
                            {
                                var dmodel = await _wo_repair_dealRepository.GetSingle(p => p.wocode == nocodeitem.wocode);
                                if (dmodel != null)
                                {
                                    nofilist.nofinibecause = "挂起:" + dmodel.detail;
                                }
                                else
                                {
                                    nofilist.nofinibecause = "挂起";
                                }
                            }
                            else
                            {
                                nofilist.nofinibecause = "未处理";
                            }
                            nofinishlist.Add(nofilist);
                        }
                        if (codenofinilist.Count() == 0)
                        {
                            nofinish nofilist = new nofinish();
                            nofilist.nofinishcenter = "";//未完成维修的东西
                            nofilist.nofinibecause = "";//未完成原因
                            nofinishlist.Add(nofilist);
                        }
                        wxzzrbb.nofinishcenter = nofinishlist;//未完成内容
                                                              //都加到最终表中
                        list.Add(wxzzrbb);
                    }
                    #endregion
                }
                else
                {
                    #region 时间为空
                    //获取所有班组信息
                    var bzinfolist = await _ISys_Role_InfoRepository.Get(p => p.role_code == "YWYG" && p.role_name.Contains("班") && p.state_flag == 1);
                    foreach (var item in bzinfolist)
                    {
                        Wxzzrbblists wxzzrbb = new Wxzzrbblists();
                        wxzzrbb.deptname = item.role_name;//班组名称先确定


                        //获取日接单量
                        wxzzrbb.daycounts = _wo_repair_baseRepository.Count(p => p.Jwocode == item.role_name
                                                                                && p.isdelete == 0).Result.ToString();
                        //获取本班组接的所有单子 已完结的 状态等于3和2的
                        var codefinilist = await _wo_repair_baseRepository.Get(p => p.Jwocode == item.role_name
                                                                             && p.isdelete == 0
                                                                             && (p.wostate == 3 || p.wostate == 2));
                        //赋值
                        List<finishcentlists> finisherlist = new List<finishcentlists>();

                        foreach (var codeitem in codefinilist)
                        {
                            finishcentlists finishlist = new finishcentlists();
                            finishlist.wxcenters = codeitem.repairreplace + "     " + codeitem.repairoom + "     " + codeitem.faultdescripe;//维修物品
                            if (codeitem.CLpeoplefrom != null)
                            {
                                if (codeitem.CLpeoplefrom == "本部")
                                {
                                    finishlist.wxrfrom = "本院";//维修人来源                        }

                                }
                                else
                                {
                                    finishlist.wxrfrom = codeitem.CLpeoplefrom;//维修人来源
                                }
                            }
                            else
                            {
                                finishlist.wxrfrom = "";//维修人来源
                            }

                            #region 满意不满意
                            switch (codeitem.score)
                            {
                                case 1:
                                    finishlist.myfens = "";
                                    finishlist.nomyfens = "差";
                                    finishlist.dpjcenter = "";
                                    break;
                                case 2:
                                    finishlist.myfens = "";
                                    finishlist.nomyfens = "不满意";
                                    finishlist.dpjcenter = "";
                                    break;
                                case 3:
                                    finishlist.myfens = "一般";
                                    finishlist.nomyfens = "";
                                    finishlist.dpjcenter = "";
                                    break;
                                case 4:
                                    finishlist.myfens = "满意";
                                    finishlist.nomyfens = "";
                                    finishlist.dpjcenter = "";
                                    break;
                                case 5:
                                    finishlist.myfens = "很满意";
                                    finishlist.dpjcenter = "";
                                    finishlist.nomyfens = "";
                                    break;
                                default:
                                    finishlist.myfens = "";
                                    finishlist.nomyfens = "";
                                    finishlist.dpjcenter = "待评价";
                                    break;
                            }
                            #endregion

                            #region 超时时长
                            //超时时长
                            //根据id获取维修项目时效时间
                            var wxxmtime = await _wxwpstimeRepository.GetSingle(p => p.id == codeitem.wxtimeid);
                            if (wxxmtime != null)
                            {

                                if (codeitem.dealtime != null && codeitem.tobztimes != null)
                                {
                                    TimeSpan ts = Convert.ToDateTime(codeitem.dealtime).Subtract(Convert.ToDateTime(codeitem.tobztimes));
                                    double hours = ts.TotalHours;//24.0

                                    if ((hours - wxxmtime.timecount) > 0)
                                    {

                                        finishlist.cstimecount = Math.Round(hours - wxxmtime.timecount, 2).ToString();// ""; 
                                    }
                                    else
                                    {
                                        finishlist.cstimecount = "";
                                    }
                                }


                            }
                            else
                            {
                                finishlist.cstimecount = "";
                            }

                            #endregion



                            finisherlist.Add(finishlist);

                        }
                        if (finisherlist.Count() == 0)
                        {
                            finishcentlists finishlist = new finishcentlists();
                            finishlist.wxcenters = "";//维修物品
                            finishlist.wxrfrom = "";//维修人来源
                            finishlist.myfens = "";//满意
                            finishlist.nomyfens = "";//不满意
                            finishlist.dpjcenter = "";//待评价
                            finishlist.cstimecount = "";
                            finisherlist.Add(finishlist);
                        }
                        wxzzrbb.finishlistcenter = finisherlist;
                        //获取未完成的单子
                        var codenofinilist = await _wo_repair_baseRepository.Get(p => p.Jwocode == item.role_name
                                                                               && p.isdelete == 0
                                                                               && (p.wostate != 3 && p.wostate != 2));
                        List<nofinish> nofinishlist = new List<nofinish>();
                        foreach (var nocodeitem in codenofinilist)
                        {
                            nofinish nofilist = new nofinish();
                            nofilist.nofinishcenter = nocodeitem.repairreplace + "     " + nocodeitem.repairoom + "     " + nocodeitem.faultdescripe;//未完成维修的东西
                            if (nocodeitem.wostate == 7)
                            {
                                var dmodel = await _wo_repair_dealRepository.GetSingle(p => p.wocode == nocodeitem.wocode);
                                if (dmodel != null)
                                {
                                    nofilist.nofinibecause = "挂起:" + dmodel.detail;
                                }
                                else
                                {
                                    nofilist.nofinibecause = "挂起";
                                }
                            }
                            else
                            {
                                nofilist.nofinibecause = "未处理";
                            }
                            nofinishlist.Add(nofilist);
                        }
                        if (codenofinilist.Count() == 0)
                        {
                            nofinish nofilist = new nofinish();
                            nofilist.nofinishcenter = "";//未完成维修的东西
                            nofilist.nofinibecause = "";//未完成原因
                            nofinishlist.Add(nofilist);
                        }
                        wxzzrbb.nofinishcenter = nofinishlist;//未完成内容
                                                              //都加到最终表中
                        list.Add(wxzzrbb);
                    }
                    #endregion
                }

            }
            #endregion
            var obj = new
            {
                lists = list,
            };
            return Success("成功", obj);
        }
        /// <summary>
        /// 导出维修追踪日接单量
        /// </summary>
        /// <param name="stime"></param>
        /// <param name="etime"></param>
        /// <returns></returns>
        //[Authorize]
        [HttpGet("exportexcelwxzz")]
        [AllowAnonymous]
        public async Task<IActionResult> ExportExcelAsyncwxzz(string stime, string etime, string daytype, string isdjtype)
        {
            var datenow = DateTime.Now;
            DateTime? startime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
            DateTime? endtime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));
            if (!string.IsNullOrEmpty(daytype))
            {
                switch (daytype)
                {
                    case "week":
                        startime = Convert.ToDateTime(datenow.AddDays(1 - Convert.ToInt32(datenow.DayOfWeek.ToString("d"))).ToString("yyyy-MM-dd 00:00:00"));//本周周一
                        endtime = Convert.ToDateTime(datenow.AddDays(6).ToString("yyyy-MM-dd  23:59:59"));//本周周日
                        break;
                    case "month":
                        startime = Convert.ToDateTime(datenow.AddDays(1 - datenow.Day).ToString("yyyy-MM-dd 00:00:00"));  //本月月初  
                        endtime = Convert.ToDateTime(datenow.AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd 23:59:59"));  //本月月末  
                        break;
                    case "year":
                        startime = Convert.ToDateTime(new DateTime(datenow.Year, 1, 1).ToString("yyyy-MM-dd 00:00:00"));  //本年年初  
                        endtime = Convert.ToDateTime(new DateTime(datenow.Year, 12, 31).ToString("yyyy-MM-dd  23:59:59"));  //本年年末  
                        break;
                    default:
                        break;
                }
            }
            else
            {
                //先确认时间是否为空 空则用当前时间天来判断
                if (!string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                {

                    startime = DateTime.Parse(stime.Trim() + " 00:00:00");
                    endtime = DateTime.Parse(etime.Trim() + " 23:59:59");
                }
                else
                {
                    startime = null;
                    endtime = null;
                }

            }
            List<Wxzzrbblists> list = new List<Wxzzrbblists>();


            if (isdjtype == "静态")
            {
                if (startime != null && endtime != null)
                {
                    #region 时间不空
                    //获取所有班组信息
                    var bzinfolist = await _ISys_Role_InfoRepository.Get(p => p.role_code == "YWYG" && p.role_name.Contains("班") && p.state_flag == 1);
                    foreach (var item in bzinfolist)
                    {
                        Wxzzrbblists wxzzrbb = new Wxzzrbblists();
                        wxzzrbb.deptname = item.role_name;//班组名称先确定


                        //获取日接单量
                        wxzzrbb.daycounts = _wo_repair_baseRepository.Count(p => p.Jwocode == item.role_name
                                                                                && p.isdelete == 0
                                                                                && p.createtime >= startime
                                                                                && p.createtime <= endtime).Result.ToString();
                        //获取本班组接的所有单子 已完结的 状态等于3和2的
                        var codefinilist = await _wo_repair_baseRepository.Get(p => p.Jwocode == item.role_name
                                                                             && p.isdelete == 0
                                                                             && p.createtime >= startime
                                                                             && p.createtime <= endtime
                                                                             && (p.wostate == 3 || p.wostate == 2));
                        //赋值
                        //已完成的数据 在时间段内创建并且处理时间在这个时间段内 才算静态完成单子 
                        List<finishcentlists> finisherlist = new List<finishcentlists>();

                        List<nofinish> nofinishlist = new List<nofinish>();

                        foreach (var codeitem in codefinilist)
                        {
                            if (codeitem.dealtime >= startime && codeitem.dealtime <= endtime)
                            {
                                finishcentlists finishlist = new finishcentlists();
                                finishlist.wxcenters = codeitem.repairreplace + "     " + codeitem.repairoom + "     " + codeitem.faultdescripe;//维修物品
                                if (codeitem.CLpeoplefrom != null)
                                {
                                    if (codeitem.CLpeoplefrom == "本部")
                                    {
                                        finishlist.wxrfrom = "本院";//维修人来源                        }

                                    }
                                    else
                                    {
                                        finishlist.wxrfrom = codeitem.CLpeoplefrom;//维修人来源
                                    }
                                }
                                else
                                {
                                    finishlist.wxrfrom = "";//维修人来源
                                }

                                #region 满意不满意
                                switch (codeitem.score)
                                {
                                    case 1:
                                        finishlist.myfens = "";
                                        finishlist.nomyfens = "差";
                                        finishlist.dpjcenter = "";
                                        break;
                                    case 2:
                                        finishlist.myfens = "";
                                        finishlist.nomyfens = "不满意";
                                        finishlist.dpjcenter = "";
                                        break;
                                    case 3:
                                        finishlist.myfens = "一般";
                                        finishlist.nomyfens = "";
                                        finishlist.dpjcenter = "";
                                        break;
                                    case 4:
                                        finishlist.myfens = "满意";
                                        finishlist.nomyfens = "";
                                        finishlist.dpjcenter = "";
                                        break;
                                    case 5:
                                        finishlist.myfens = "很满意";
                                        finishlist.dpjcenter = "";
                                        finishlist.nomyfens = "";
                                        break;
                                    default:
                                        finishlist.myfens = "";
                                        finishlist.nomyfens = "";
                                        finishlist.dpjcenter = "待评价";
                                        break;
                                }
                                #endregion

                                #region 超时时长
                                //超时时长
                                //根据id获取维修项目时效时间
                                var wxxmtime = await _wxwpstimeRepository.GetSingle(p => p.id == codeitem.wxtimeid);
                                if (wxxmtime != null)
                                {

                                    if (codeitem.dealtime != null && codeitem.tobztimes != null)
                                    {
                                        TimeSpan ts = Convert.ToDateTime(codeitem.dealtime).Subtract(Convert.ToDateTime(codeitem.tobztimes));
                                        double hours = ts.TotalHours;//24.0

                                        if ((hours - wxxmtime.timecount) > 0)
                                        {

                                            finishlist.cstimecount = Math.Round(hours - wxxmtime.timecount, 2).ToString();// ""; 
                                        }
                                        else
                                        {
                                            finishlist.cstimecount = "";
                                        }
                                    }


                                }
                                else
                                {
                                    finishlist.cstimecount = "";
                                }

                                #endregion
                                finisherlist.Add(finishlist);
                            }
                            else
                            {
                                nofinish nofilist = new nofinish();
                                nofilist.nofinishcenter = codeitem.repairreplace + "     " + codeitem.repairoom + "     " + codeitem.faultdescripe;//未完成维修的东西
                                nofilist.nofinibecause = "未处理";
                                nofinishlist.Add(nofilist);
                            }

                        }
                        if (finisherlist.Count() == 0)
                        {
                            finishcentlists finishlist = new finishcentlists();
                            finishlist.wxcenters = "";//维修物品
                            finishlist.wxrfrom = "";//维修人来源
                            finishlist.myfens = "";//满意
                            finishlist.nomyfens = "";//不满意
                            finishlist.dpjcenter = "";//待评价
                            finishlist.cstimecount = "";
                            finisherlist.Add(finishlist);
                        }
                        wxzzrbb.finishlistcenter = finisherlist;
                        //获取未完成的单子
                        var codenofinilist = await _wo_repair_baseRepository.Get(p => p.Jwocode == item.role_name
                                                                               && p.isdelete == 0
                                                                               && p.createtime >= startime
                                                                               && p.createtime <= endtime
                                                                               && (p.wostate != 3 && p.wostate != 2));

                        foreach (var nocodeitem in codenofinilist)
                        {
                            nofinish nofilist = new nofinish();
                            nofilist.nofinishcenter = nocodeitem.repairreplace + "     " + nocodeitem.repairoom + "     " + nocodeitem.faultdescripe;//未完成维修的东西
                            if (nocodeitem.wostate == 7)
                            {
                                var dmodel = await _wo_repair_dealRepository.GetSingle(p => p.wocode == nocodeitem.wocode);
                                if (dmodel != null)
                                {
                                    nofilist.nofinibecause = "挂起:" + dmodel.detail;
                                }
                                else
                                {
                                    nofilist.nofinibecause = "挂起";
                                }
                            }
                            else
                            {
                                nofilist.nofinibecause = "未处理";
                            }
                            nofinishlist.Add(nofilist);
                        }
                        if (codenofinilist.Count() == 0)
                        {
                            nofinish nofilist = new nofinish();
                            nofilist.nofinishcenter = "";//未完成维修的东西
                            nofilist.nofinibecause = "";//未完成原因
                            nofinishlist.Add(nofilist);
                        }
                        wxzzrbb.nofinishcenter = nofinishlist;//未完成内容
                                                              //都加到最终表中
                        list.Add(wxzzrbb);
                    }
                    #endregion
                }
                else
                {
                    #region 时间wei空
                    //获取所有班组信息
                    var bzinfolist = await _ISys_Role_InfoRepository.Get(p => p.role_code == "YWYG" && p.role_name.Contains("班") && p.state_flag == 1);
                    foreach (var item in bzinfolist)
                    {
                        Wxzzrbblists wxzzrbb = new Wxzzrbblists();
                        wxzzrbb.deptname = item.role_name;//班组名称先确定


                        //获取日接单量
                        wxzzrbb.daycounts = _wo_repair_baseRepository.Count(p => p.Jwocode == item.role_name
                                                                                && p.isdelete == 0
                                                                               ).Result.ToString();
                        //获取本班组接的所有单子 已完结的 状态等于3和2的
                        var codefinilist = await _wo_repair_baseRepository.Get(p => p.Jwocode == item.role_name
                                                                             && p.isdelete == 0
                                                                             && (p.wostate == 3 || p.wostate == 2));
                        //赋值
                        //已完成的数据 在时间段内创建并且处理时间在这个时间段内 才算静态完成单子 
                        List<finishcentlists> finisherlist = new List<finishcentlists>();

                        List<nofinish> nofinishlist = new List<nofinish>();

                        foreach (var codeitem in codefinilist)
                        {
                            if (codeitem.dealtime >= startime && codeitem.dealtime <= endtime)
                            {
                                finishcentlists finishlist = new finishcentlists();
                                finishlist.wxcenters = codeitem.repairreplace + "     " + codeitem.repairoom + "     " + codeitem.faultdescripe;//维修物品
                                if (codeitem.CLpeoplefrom != null)
                                {
                                    if (codeitem.CLpeoplefrom == "本部")
                                    {
                                        finishlist.wxrfrom = "本院";//维修人来源                        }

                                    }
                                    else
                                    {
                                        finishlist.wxrfrom = codeitem.CLpeoplefrom;//维修人来源
                                    }
                                }
                                else
                                {
                                    finishlist.wxrfrom = "";//维修人来源
                                }

                                #region 满意不满意
                                switch (codeitem.score)
                                {
                                    case 1:
                                        finishlist.myfens = "";
                                        finishlist.nomyfens = "差";
                                        finishlist.dpjcenter = "";
                                        break;
                                    case 2:
                                        finishlist.myfens = "";
                                        finishlist.nomyfens = "不满意";
                                        finishlist.dpjcenter = "";
                                        break;
                                    case 3:
                                        finishlist.myfens = "一般";
                                        finishlist.nomyfens = "";
                                        finishlist.dpjcenter = "";
                                        break;
                                    case 4:
                                        finishlist.myfens = "满意";
                                        finishlist.nomyfens = "";
                                        finishlist.dpjcenter = "";
                                        break;
                                    case 5:
                                        finishlist.myfens = "很满意";
                                        finishlist.dpjcenter = "";
                                        finishlist.nomyfens = "";
                                        break;
                                    default:
                                        finishlist.myfens = "";
                                        finishlist.nomyfens = "";
                                        finishlist.dpjcenter = "待评价";
                                        break;
                                }
                                #endregion

                                #region 超时时长
                                //超时时长
                                //根据id获取维修项目时效时间
                                var wxxmtime = await _wxwpstimeRepository.GetSingle(p => p.id == codeitem.wxtimeid);
                                if (wxxmtime != null)
                                {

                                    if (codeitem.dealtime != null && codeitem.tobztimes != null)
                                    {
                                        TimeSpan ts = Convert.ToDateTime(codeitem.dealtime).Subtract(Convert.ToDateTime(codeitem.tobztimes));
                                        double hours = ts.TotalHours;//24.0

                                        if ((hours - wxxmtime.timecount) > 0)
                                        {

                                            finishlist.cstimecount = Math.Round(hours - wxxmtime.timecount, 2).ToString();// ""; 
                                        }
                                        else
                                        {
                                            finishlist.cstimecount = "";
                                        }
                                    }


                                }
                                else
                                {
                                    finishlist.cstimecount = "";
                                }

                                #endregion
                                finisherlist.Add(finishlist);
                            }
                            else
                            {
                                nofinish nofilist = new nofinish();
                                nofilist.nofinishcenter = codeitem.repairreplace + "     " + codeitem.repairoom + "     " + codeitem.faultdescripe;//未完成维修的东西
                                nofilist.nofinibecause = "未处理";
                                nofinishlist.Add(nofilist);
                            }

                        }
                        if (finisherlist.Count() == 0)
                        {
                            finishcentlists finishlist = new finishcentlists();
                            finishlist.wxcenters = "";//维修物品
                            finishlist.wxrfrom = "";//维修人来源
                            finishlist.myfens = "";//满意
                            finishlist.nomyfens = "";//不满意
                            finishlist.dpjcenter = "";//待评价
                            finishlist.cstimecount = "";
                            finisherlist.Add(finishlist);
                        }
                        wxzzrbb.finishlistcenter = finisherlist;
                        //获取未完成的单子
                        var codenofinilist = await _wo_repair_baseRepository.Get(p => p.Jwocode == item.role_name
                                                                               && p.isdelete == 0
                                                                               && (p.wostate != 3 && p.wostate != 2));

                        foreach (var nocodeitem in codenofinilist)
                        {
                            nofinish nofilist = new nofinish();
                            nofilist.nofinishcenter = nocodeitem.repairreplace + "     " + nocodeitem.repairoom + "     " + nocodeitem.faultdescripe;//未完成维修的东西
                            if (nocodeitem.wostate == 7)
                            {
                                var dmodel = await _wo_repair_dealRepository.GetSingle(p => p.wocode == nocodeitem.wocode);
                                if (dmodel != null)
                                {
                                    nofilist.nofinibecause = "挂起:" + dmodel.detail;
                                }
                                else
                                {
                                    nofilist.nofinibecause = "挂起";
                                }
                            }
                            else
                            {
                                nofilist.nofinibecause = "未处理";
                            }
                            nofinishlist.Add(nofilist);
                        }
                        if (codenofinilist.Count() == 0)
                        {
                            nofinish nofilist = new nofinish();
                            nofilist.nofinishcenter = "";//未完成维修的东西
                            nofilist.nofinibecause = "";//未完成原因
                            nofinishlist.Add(nofilist);
                        }
                        wxzzrbb.nofinishcenter = nofinishlist;//未完成内容
                                                              //都加到最终表中
                        list.Add(wxzzrbb);
                    }
                    #endregion
                }

            }
            else
            {
                if (startime != null && endtime != null)
                {
                    #region 时间不为空
                    //获取所有班组信息
                    var bzinfolist = await _ISys_Role_InfoRepository.Get(p => p.role_code == "YWYG" && p.role_name.Contains("班") && p.state_flag == 1);
                    foreach (var item in bzinfolist)
                    {
                        Wxzzrbblists wxzzrbb = new Wxzzrbblists();
                        wxzzrbb.deptname = item.role_name;//班组名称先确定


                        //获取日接单量
                        wxzzrbb.daycounts = _wo_repair_baseRepository.Count(p => p.Jwocode == item.role_name
                                                                                && p.isdelete == 0
                                                                                && p.createtime >= startime
                                                                                && p.createtime <= endtime).Result.ToString();
                        //获取本班组接的所有单子 已完结的 状态等于3和2的
                        var codefinilist = await _wo_repair_baseRepository.Get(p => p.Jwocode == item.role_name
                                                                             && p.isdelete == 0
                                                                             && p.createtime >= startime
                                                                             && p.createtime <= endtime
                                                                             && (p.wostate == 3 || p.wostate == 2));
                        //赋值
                        List<finishcentlists> finisherlist = new List<finishcentlists>();

                        foreach (var codeitem in codefinilist)
                        {
                            finishcentlists finishlist = new finishcentlists();
                            finishlist.wxcenters = codeitem.repairreplace + "     " + codeitem.repairoom + "     " + codeitem.faultdescripe;//维修物品
                            if (codeitem.CLpeoplefrom != null)
                            {
                                if (codeitem.CLpeoplefrom == "本部")
                                {
                                    finishlist.wxrfrom = "本院";//维修人来源                        }

                                }
                                else
                                {
                                    finishlist.wxrfrom = codeitem.CLpeoplefrom;//维修人来源
                                }
                            }
                            else
                            {
                                finishlist.wxrfrom = "";//维修人来源
                            }

                            #region 满意不满意
                            switch (codeitem.score)
                            {
                                case 1:
                                    finishlist.myfens = "";
                                    finishlist.nomyfens = "差";
                                    finishlist.dpjcenter = "";
                                    break;
                                case 2:
                                    finishlist.myfens = "";
                                    finishlist.nomyfens = "不满意";
                                    finishlist.dpjcenter = "";
                                    break;
                                case 3:
                                    finishlist.myfens = "一般";
                                    finishlist.nomyfens = "";
                                    finishlist.dpjcenter = "";
                                    break;
                                case 4:
                                    finishlist.myfens = "满意";
                                    finishlist.nomyfens = "";
                                    finishlist.dpjcenter = "";
                                    break;
                                case 5:
                                    finishlist.myfens = "很满意";
                                    finishlist.dpjcenter = "";
                                    finishlist.nomyfens = "";
                                    break;
                                default:
                                    finishlist.myfens = "";
                                    finishlist.nomyfens = "";
                                    finishlist.dpjcenter = "待评价";
                                    break;
                            }
                            #endregion

                            #region 超时时长
                            //超时时长
                            //根据id获取维修项目时效时间
                            var wxxmtime = await _wxwpstimeRepository.GetSingle(p => p.id == codeitem.wxtimeid);
                            if (wxxmtime != null)
                            {

                                if (codeitem.dealtime != null && codeitem.tobztimes != null)
                                {
                                    TimeSpan ts = Convert.ToDateTime(codeitem.dealtime).Subtract(Convert.ToDateTime(codeitem.tobztimes));
                                    double hours = ts.TotalHours;//24.0

                                    if ((hours - wxxmtime.timecount) > 0)
                                    {

                                        finishlist.cstimecount = Math.Round(hours - wxxmtime.timecount, 2).ToString();// ""; 
                                    }
                                    else
                                    {
                                        finishlist.cstimecount = "";
                                    }
                                }


                            }
                            else
                            {
                                finishlist.cstimecount = "";
                            }

                            #endregion



                            finisherlist.Add(finishlist);

                        }
                        if (finisherlist.Count() == 0)
                        {
                            finishcentlists finishlist = new finishcentlists();
                            finishlist.wxcenters = "";//维修物品
                            finishlist.wxrfrom = "";//维修人来源
                            finishlist.myfens = "";//满意
                            finishlist.nomyfens = "";//不满意
                            finishlist.dpjcenter = "";//待评价
                            finishlist.cstimecount = "";
                            finisherlist.Add(finishlist);
                        }
                        wxzzrbb.finishlistcenter = finisherlist;
                        //获取未完成的单子
                        var codenofinilist = await _wo_repair_baseRepository.Get(p => p.Jwocode == item.role_name
                                                                               && p.isdelete == 0
                                                                               && p.createtime >= startime
                                                                               && p.createtime <= endtime
                                                                               && (p.wostate != 3 && p.wostate != 2));
                        List<nofinish> nofinishlist = new List<nofinish>();
                        foreach (var nocodeitem in codenofinilist)
                        {
                            nofinish nofilist = new nofinish();
                            nofilist.nofinishcenter = nocodeitem.repairreplace + "     " + nocodeitem.repairoom + "     " + nocodeitem.faultdescripe;//未完成维修的东西
                            if (nocodeitem.wostate == 7)
                            {
                                var dmodel = await _wo_repair_dealRepository.GetSingle(p => p.wocode == nocodeitem.wocode);
                                if (dmodel != null)
                                {
                                    nofilist.nofinibecause = "挂起:" + dmodel.detail;
                                }
                                else
                                {
                                    nofilist.nofinibecause = "挂起";
                                }
                            }
                            else
                            {
                                nofilist.nofinibecause = "未处理";
                            }
                            nofinishlist.Add(nofilist);
                        }
                        if (codenofinilist.Count() == 0)
                        {
                            nofinish nofilist = new nofinish();
                            nofilist.nofinishcenter = "";//未完成维修的东西
                            nofilist.nofinibecause = "";//未完成原因
                            nofinishlist.Add(nofilist);
                        }
                        wxzzrbb.nofinishcenter = nofinishlist;//未完成内容
                                                              //都加到最终表中
                        list.Add(wxzzrbb);
                    }
                    #endregion
                }
                else
                {
                    #region 时间为空
                    //获取所有班组信息
                    var bzinfolist = await _ISys_Role_InfoRepository.Get(p => p.role_code == "YWYG" && p.role_name.Contains("班") && p.state_flag == 1);
                    foreach (var item in bzinfolist)
                    {
                        Wxzzrbblists wxzzrbb = new Wxzzrbblists();
                        wxzzrbb.deptname = item.role_name;//班组名称先确定


                        //获取日接单量
                        wxzzrbb.daycounts = _wo_repair_baseRepository.Count(p => p.Jwocode == item.role_name
                                                                                && p.isdelete == 0).Result.ToString();
                        //获取本班组接的所有单子 已完结的 状态等于3和2的
                        var codefinilist = await _wo_repair_baseRepository.Get(p => p.Jwocode == item.role_name
                                                                             && p.isdelete == 0
                                                                             && (p.wostate == 3 || p.wostate == 2));
                        //赋值
                        List<finishcentlists> finisherlist = new List<finishcentlists>();

                        foreach (var codeitem in codefinilist)
                        {
                            finishcentlists finishlist = new finishcentlists();
                            finishlist.wxcenters = codeitem.repairreplace + "     " + codeitem.repairoom + "     " + codeitem.faultdescripe;//维修物品
                            if (codeitem.CLpeoplefrom != null)
                            {
                                if (codeitem.CLpeoplefrom == "本部")
                                {
                                    finishlist.wxrfrom = "本院";//维修人来源                        }

                                }
                                else
                                {
                                    finishlist.wxrfrom = codeitem.CLpeoplefrom;//维修人来源
                                }
                            }
                            else
                            {
                                finishlist.wxrfrom = "";//维修人来源
                            }

                            #region 满意不满意
                            switch (codeitem.score)
                            {
                                case 1:
                                    finishlist.myfens = "";
                                    finishlist.nomyfens = "差";
                                    finishlist.dpjcenter = "";
                                    break;
                                case 2:
                                    finishlist.myfens = "";
                                    finishlist.nomyfens = "不满意";
                                    finishlist.dpjcenter = "";
                                    break;
                                case 3:
                                    finishlist.myfens = "一般";
                                    finishlist.nomyfens = "";
                                    finishlist.dpjcenter = "";
                                    break;
                                case 4:
                                    finishlist.myfens = "满意";
                                    finishlist.nomyfens = "";
                                    finishlist.dpjcenter = "";
                                    break;
                                case 5:
                                    finishlist.myfens = "很满意";
                                    finishlist.dpjcenter = "";
                                    finishlist.nomyfens = "";
                                    break;
                                default:
                                    finishlist.myfens = "";
                                    finishlist.nomyfens = "";
                                    finishlist.dpjcenter = "待评价";
                                    break;
                            }
                            #endregion

                            #region 超时时长
                            //超时时长
                            //根据id获取维修项目时效时间
                            var wxxmtime = await _wxwpstimeRepository.GetSingle(p => p.id == codeitem.wxtimeid);
                            if (wxxmtime != null)
                            {

                                if (codeitem.dealtime != null && codeitem.tobztimes != null)
                                {
                                    TimeSpan ts = Convert.ToDateTime(codeitem.dealtime).Subtract(Convert.ToDateTime(codeitem.tobztimes));
                                    double hours = ts.TotalHours;//24.0

                                    if ((hours - wxxmtime.timecount) > 0)
                                    {

                                        finishlist.cstimecount = Math.Round(hours - wxxmtime.timecount, 2).ToString();// ""; 
                                    }
                                    else
                                    {
                                        finishlist.cstimecount = "";
                                    }
                                }


                            }
                            else
                            {
                                finishlist.cstimecount = "";
                            }

                            #endregion



                            finisherlist.Add(finishlist);

                        }
                        if (finisherlist.Count() == 0)
                        {
                            finishcentlists finishlist = new finishcentlists();
                            finishlist.wxcenters = "";//维修物品
                            finishlist.wxrfrom = "";//维修人来源
                            finishlist.myfens = "";//满意
                            finishlist.nomyfens = "";//不满意
                            finishlist.dpjcenter = "";//待评价
                            finishlist.cstimecount = "";
                            finisherlist.Add(finishlist);
                        }
                        wxzzrbb.finishlistcenter = finisherlist;
                        //获取未完成的单子
                        var codenofinilist = await _wo_repair_baseRepository.Get(p => p.Jwocode == item.role_name
                                                                               && p.isdelete == 0
                                                                               && (p.wostate != 3 && p.wostate != 2));
                        List<nofinish> nofinishlist = new List<nofinish>();
                        foreach (var nocodeitem in codenofinilist)
                        {
                            nofinish nofilist = new nofinish();
                            nofilist.nofinishcenter = nocodeitem.repairreplace + "     " + nocodeitem.repairoom + "     " + nocodeitem.faultdescripe;//未完成维修的东西
                            if (nocodeitem.wostate == 7)
                            {
                                var dmodel = await _wo_repair_dealRepository.GetSingle(p => p.wocode == nocodeitem.wocode);
                                if (dmodel != null)
                                {
                                    nofilist.nofinibecause = "挂起:" + dmodel.detail;
                                }
                                else
                                {
                                    nofilist.nofinibecause = "挂起";
                                }
                            }
                            else
                            {
                                nofilist.nofinibecause = "未处理";
                            }
                            nofinishlist.Add(nofilist);
                        }
                        if (codenofinilist.Count() == 0)
                        {
                            nofinish nofilist = new nofinish();
                            nofilist.nofinishcenter = "";//未完成维修的东西
                            nofilist.nofinibecause = "";//未完成原因
                            nofinishlist.Add(nofilist);
                        }
                        wxzzrbb.nofinishcenter = nofinishlist;//未完成内容
                                                              //都加到最终表中
                        list.Add(wxzzrbb);
                    }
                    #endregion
                }

            }
            List<Daywxzzcodelistall> lista = new List<Daywxzzcodelistall>();
            NPOIHelper npoi = new NPOIHelper();

            //var tb = ModelConvertHelper<Daywxzzcodelistall>.ConvertListToDataTable(lista);
            //if (tb != null)
            //{
            if (list.Count > 0)
            {
                //导出dtnew 
                string[] cols = { "班组", "日接单量", "完成内容", "维修人来源", "满意", "不满意", "待评价", "超时时长", "未完成内容", "未完成原因" };

                byte[] sm = ExportToExcelcode(list, cols);

                if (sm != null)
                {
                    return File(sm, "application/vnd.ms-excel", "维修追踪日报表.xlsx");
                }
                else
                {
                    return Error("导出失败");
                }

            }

            // }
            return Error("导出失败");
        }


        /// <summary>
        /// 下载excel
        /// </summary>
        /// <param name="cols"></param>
        /// <returns></returns>
        public byte[] ExportToExcelcode(List<Wxzzrbblists> objs, string[] cols = null, string[] colname = null)
        {
            IWorkbook workbook = new XSSFWorkbook();
            ISheet sheet = workbook.CreateSheet("Sheet1");

            ICellStyle HeadercellStyle = workbook.CreateCellStyle();
            //HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
            HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
            HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
            HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
            HeadercellStyle.Alignment = HorizontalAlignment.Center;
            HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
            HeadercellStyle.FillPattern = FillPattern.SolidForeground;
            HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;

            //字体
            NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
            headerfont.Boldweight = (short)FontBoldWeight.Bold;
            headerfont.FontHeightInPoints = 12;
            HeadercellStyle.SetFont(headerfont);

            var shang = 1;//开始行
            var ehang = 0;//结束行
            if (objs.Count > 0)
            {
                ICellStyle cellStyle = workbook.CreateCellStyle();

                //为避免日期格式被Excel自动替换，所以设定 format 为 『@』 表示一率当成text來看
                cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
                //cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
                //cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
                //cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
                //cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;


                NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
                cellfont.Boldweight = (short)FontBoldWeight.Normal;
                cellStyle.SetFont(cellfont);

                int icolIndex = 0;
                IRow headerRow = sheet.CreateRow(0);

                if (cols == null)
                {
                    //var pis = ((Newtonsoft.Json.Linq.JObject)objs[0]).GetType().GetProperties();
                    var pis = objs[0].GetType().GetProperties();
                    cols = new string[pis.Length];
                    for (int i = 0; i < pis.Length; i++)
                    {
                        //cols[i] = pis[i].Name;
                        cols[i] = pis[i].GetValue(objs[0], null).ToString();
                    }
                }
                foreach (string dc in cols)
                {
                    ICell cell = headerRow.CreateCell(icolIndex);
                    cell.SetCellValue(dc);
                    cell.CellStyle = HeadercellStyle;

                    sheet.SetDefaultColumnStyle(icolIndex, cellStyle);

                    icolIndex++;
                }

                //建立内容行
                int iRowIndex = 0;

                foreach (var obj in objs)
                {
                    var zongcount = 0;
                    List<Daywxzzcodelistall> lista = new List<Daywxzzcodelistall>();
                    if (obj.finishlistcenter.Count() > obj.nofinishcenter.Count())
                    {
                        zongcount = obj.finishlistcenter.Count();
                        if (obj.finishlistcenter.Count() > 0)
                        {
                            var a = 0;
                            foreach (var item1 in obj.finishlistcenter)
                            {
                                Daywxzzcodelistall daylisa = new Daywxzzcodelistall();
                                daylisa.deptname = obj.deptname;
                                daylisa.daycounts = obj.daycounts;
                                daylisa.wxcenters = item1.wxcenters;
                                daylisa.wxrfrom = item1.wxrfrom;
                                daylisa.myfens = item1.myfens;
                                daylisa.nomyfens = item1.nomyfens;
                                daylisa.dpjcenter = item1.dpjcenter;
                                daylisa.cstimecount = item1.cstimecount;
                                if (obj.nofinishcenter.Count() > 0)
                                {
                                    if (a < obj.nofinishcenter.Count())
                                    {
                                        daylisa.nofinishcenter = obj.nofinishcenter[a].nofinishcenter;
                                        daylisa.nofinibecause = obj.nofinishcenter[a].nofinibecause;
                                    }
                                    else
                                    {
                                        daylisa.nofinishcenter = "";
                                        daylisa.nofinibecause = "";
                                    }
                                }
                                else
                                {
                                    daylisa.nofinishcenter = "";
                                    daylisa.nofinibecause = "";
                                }
                                lista.Add(daylisa);
                                a++;

                            }
                        }
                    }
                    else if (obj.finishlistcenter.Count() < obj.nofinishcenter.Count())
                    {
                        zongcount = obj.nofinishcenter.Count();
                        if (obj.nofinishcenter.Count() > 0)
                        {
                            var a = 0;
                            foreach (var item2 in obj.nofinishcenter)
                            {
                                Daywxzzcodelistall daylisa = new Daywxzzcodelistall();
                                daylisa.deptname = obj.deptname;
                                daylisa.daycounts = obj.daycounts;
                                daylisa.nofinishcenter = item2.nofinishcenter;
                                daylisa.nofinibecause = item2.nofinibecause;
                                if (obj.finishlistcenter.Count() > 0)
                                {
                                    if (a < obj.finishlistcenter.Count())
                                    {

                                        daylisa.wxcenters = obj.finishlistcenter[a].wxcenters;
                                        daylisa.wxrfrom = obj.finishlistcenter[a].wxrfrom;
                                        daylisa.myfens = obj.finishlistcenter[a].myfens;
                                        daylisa.nomyfens = obj.finishlistcenter[a].nomyfens;
                                        daylisa.dpjcenter = obj.finishlistcenter[a].dpjcenter;
                                        daylisa.cstimecount = obj.finishlistcenter[a].cstimecount;
                                    }
                                    else
                                    {
                                        daylisa.wxcenters = "";
                                        daylisa.wxrfrom = "";
                                        daylisa.myfens = "";
                                        daylisa.nomyfens = "";
                                        daylisa.dpjcenter = "";
                                        daylisa.cstimecount = "";
                                    }
                                }
                                else
                                {
                                    daylisa.wxcenters = "";
                                    daylisa.wxrfrom = "";
                                    daylisa.myfens = "";
                                    daylisa.nomyfens = "";
                                    daylisa.dpjcenter = "";
                                    daylisa.cstimecount = "";
                                }
                                lista.Add(daylisa);
                                a++;
                            }
                        }
                    }
                    else if (obj.finishlistcenter.Count() == obj.nofinishcenter.Count())
                    {
                        zongcount = obj.finishlistcenter.Count();
                        if (obj.finishlistcenter.Count() > 0)
                        {
                            var a = 0;
                            foreach (var item1 in obj.finishlistcenter)
                            {
                                Daywxzzcodelistall daylisa = new Daywxzzcodelistall();
                                daylisa.deptname = obj.deptname;
                                daylisa.daycounts = obj.daycounts;
                                daylisa.wxcenters = item1.wxcenters;
                                daylisa.wxrfrom = item1.wxrfrom;
                                daylisa.myfens = item1.myfens;
                                daylisa.nomyfens = item1.nomyfens;
                                daylisa.dpjcenter = item1.dpjcenter;
                                daylisa.cstimecount = item1.cstimecount;
                                if (obj.nofinishcenter.Count() > 0)
                                {
                                    if (a < obj.nofinishcenter.Count())
                                    {
                                        daylisa.nofinishcenter = obj.nofinishcenter[a].nofinishcenter;
                                        daylisa.nofinibecause = obj.nofinishcenter[a].nofinibecause;
                                    }
                                    else
                                    {
                                        daylisa.nofinishcenter = "";
                                        daylisa.nofinibecause = "";
                                    }
                                }
                                lista.Add(daylisa);
                                a++;
                            }
                        }
                        else
                        {
                            Daywxzzcodelistall daylisa = new Daywxzzcodelistall();
                            daylisa.deptname = obj.deptname;
                            daylisa.daycounts = obj.daycounts;
                            daylisa.nofinibecause = "";
                            daylisa.dpjcenter = "";
                            daylisa.nofinishcenter = "";
                            daylisa.wxcenters = "";
                            daylisa.wxrfrom = "";
                            daylisa.myfens = "";
                            daylisa.nomyfens = "";
                            daylisa.cstimecount = "";

                            lista.Add(daylisa);
                        }
                    }



                    foreach (var item in lista)
                    {
                        PropertyInfo[] jobj = item.GetType().GetProperties();
                        var dd = item.ToJson();
                        //var jobj = (Newtonsoft.Json.Linq.JObject)obj;
                        int iCellIndex = 0;
                        IRow irow = sheet.CreateRow(iRowIndex + 1);
                        for (int i = 0; i < cols.Length; i++)
                        {
                            string strsj = string.Empty;


                            if (jobj[i].GetValue(item, null) != null)
                            {
                                strsj = jobj[i].GetValue(item, null).ToString();
                            }

                            ICell cell = irow.CreateCell(iCellIndex);
                            cell.SetCellValue(strsj);
                            //cell.CellStyle = cellStyle;
                            iCellIndex++;
                        }
                        iRowIndex++;
                    }
                    if (zongcount > 0)
                    {
                        ehang = shang + zongcount - 1;
                    }
                    else
                    {
                        ehang = shang + zongcount;
                    }
                    CellRangeAddress region = new CellRangeAddress(shang, ehang, 0, 0);
                    CellRangeAddress region1 = new CellRangeAddress(shang, ehang, 1, 1);
                    sheet.AddMergedRegion(region);
                    sheet.AddMergedRegion(region1);
                    shang = ehang + 1;
                }
                //}
            }

            using (MemoryStream ms = new MemoryStream())
            {
                workbook.Write(ms);
                byte[] bs = ms.ToArray();

                workbook = null;
                ms.Close();
                ms.Dispose();

                return bs;
            }
        }

        #endregion
        #region 报表统计
        /// <summary>
        /// 三个参数为 起止时间和接单班组
        /// </summary>
        /// <param name="stime"></param>
        /// <param name="etime"></param>
        /// <param name="Jwocode"></param>
        /// <returns></returns>
        [HttpGet("gettjlist")]
        public async Task<IActionResult> gettjlist(string stime, string etime, string Jwocode, string daytype, string isdjtype)
        {
            var datenow = DateTime.Now;
            DateTime? startime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
            DateTime? endtime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));
            if (!string.IsNullOrEmpty(daytype))
            {
                switch (daytype)
                {
                    case "week":
                        startime = Convert.ToDateTime(datenow.AddDays(1 - Convert.ToInt32(datenow.DayOfWeek.ToString("d"))).ToString("yyyy-MM-dd 00:00:00"));//本周周一
                        endtime = Convert.ToDateTime(datenow.AddDays(6).ToString("yyyy-MM-dd  23:59:59"));//本周周日
                        break;
                    case "month":
                        startime = Convert.ToDateTime(datenow.AddDays(1 - datenow.Day).ToString("yyyy-MM-dd 00:00:00"));  //本月月初  
                        endtime = Convert.ToDateTime(datenow.AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd 23:59:59"));  //本月月末  
                        break;
                    case "year":
                        startime = Convert.ToDateTime(new DateTime(datenow.Year, 1, 1).ToString("yyyy-MM-dd 00:00:00"));  //本年年初  
                        endtime = Convert.ToDateTime(new DateTime(datenow.Year, 12, 31).ToString("yyyy-MM-dd  23:59:59"));  //本年年末  
                        break;
                    default:
                        break;
                }
            }
            else
            {
                //先确认时间是否为空 空则用当前时间天来判断
                if (!string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                {

                    startime = DateTime.Parse(stime.Trim() + " 00:00:00");
                    endtime = DateTime.Parse(etime.Trim() + " 23:59:59");
                }
                else
                {
                    startime = null;
                    endtime = null;
                }

            }
            // wostate工单状态:0创建工单，1已转派（转派或者抢单），2已处理，3已评价，7挂起，8已取消，9已完成
            #region 定义字段
            var allcount = "";//总接单量
            var finishcount = "";//已完成的数量
            var finishlv = ""; /// 完结单率
            var jsfinishcount = "";//// 及时完成
            var csfinishcount = "";// 超时完成
            var nofinishcount = "";//未完成的数量
            var nofinnishlv = "";//未完成率
            var mywocode = "";//满意
            var ybwocode = "";//一般
            var cwocode = "";//差
            var mydblcount = "";//满意度 
            var comfrombl = "";//本院率
            var comfromsbl = "";//第三方率
            #endregion
            List<DayTjlists> list = new List<DayTjlists>();

            if (isdjtype == "静态")
            {
                #region 静态
                #region 获取list集合数据
                if (Jwocode != null)
                {
                    #region 班组不为空时
                    var byformcount = "";//本院
                    var dsffromcount = "";//第三方
                    var changepcount = "";//改派数量
                    #region 判断时间是否为空
                    if (startime != null && endtime != null)
                    {
                        #region 班组不为空  时间段不为空时
                        #region 总接单量   已完成数量   未完成数量  
                        allcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                               && p.createtime <= endtime
                                                                               && p.Jwocode == Jwocode && p.isdelete == 0).Result.ToString();
                        //finishcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                        //                                             && p.createtime <= endtime
                        //                                             && p.Jwocode == Jwocode && p.isdelete == 0
                        //                                             && (p.wostate == 3 || p.wostate == 2)).Result.ToString();
                        nofinishcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                     && p.createtime <= endtime
                                                                     && p.Jwocode == Jwocode && p.isdelete == 0
                                                                     && (p.wostate == 0 || p.wostate == 1 || p.wostate == 7)).Result.ToString();
                        #endregion

                        #region 静态数据汇总
                        //获取在这个时间段内完成的数据
                        var finishlists = await _wo_repair_baseRepository.Get(p => p.createtime >= startime
                                                                       && p.createtime <= endtime
                                                                       && p.Jwocode == Jwocode && p.isdelete == 0
                                                                       && (p.wostate == 3 || p.wostate == 2));
                        //获取在这个时间段内未完成的数据
                        var nofinishlists = await _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                      && p.createtime <= endtime
                                                                      && p.Jwocode == Jwocode && p.isdelete == 0
                                                                      && (p.wostate == 0 || p.wostate == 1 || p.wostate == 7));
                        //完成数量
                        int wccount = 0;
                        //未完成数量
                        int nowccount = 0;
                        //完成的单子中的满意数量
                        int allmywocodecounts = 0;
                        //不满意数量
                        int allnomywocodecounts = 0;
                        //获取完成单子中的本院量
                        int byformcounts = 0;
                        //获取完成单子中的第三方量
                        int dsffromcounts = 0;
                        //完结单量
                        int wfinishcounts = 0;
                        List<Wo_Repair_Base> finishlist = new List<Wo_Repair_Base>();
                        foreach (var item in finishlists)
                        {
                            if (item.dealtime >= startime && item.dealtime <= endtime)
                            {
                                wccount++;
                                if (item.score == 4 || item.score == 5 || item.score == 3)
                                {
                                    allmywocodecounts++;
                                }
                                if (item.score == 1 || item.score == 2)
                                {
                                    allnomywocodecounts++;
                                }
                                if (item.CLpeoplefrom == "第三方")
                                {
                                    dsffromcounts++;
                                }
                                if (item.CLpeoplefrom == "本院" || item.CLpeoplefrom == "本部")
                                {
                                    byformcounts++;
                                }
                                if (item.wostate == 3)
                                {
                                    wfinishcounts++;
                                }
                                finishlist.Add(item);
                            }
                            else
                            {
                                if (item.CLpeoplefrom == "第三方")
                                {
                                    dsffromcounts++;
                                }
                                if (item.CLpeoplefrom == "本院" || item.CLpeoplefrom == "本部")
                                {
                                    byformcounts++;
                                }
                                nowccount++;
                            }
                        }

                        #region 获取完结率

                        finishcount = wccount.ToString();
                        if (Convert.ToDouble(finishcount) == 0 || Convert.ToDouble(allcount) == 0)
                        {
                            finishlv = "0%";
                        }
                        else
                        {
                            double percent = Convert.ToDouble(finishcount) / Convert.ToDouble(allcount);
                            finishlv = percent.ToString("0.00%");//完结率
                        }
                        #endregion
                        #region 获取未完成率
                        nofinishcount = (Convert.ToInt32(nofinishcount) + nowccount).ToString();
                        if (Convert.ToDouble(nofinishcount) == 0 || Convert.ToDouble(allcount) == 0)
                        {
                            finishlv = "0%";
                        }
                        else
                        {
                            double percent = Convert.ToDouble(nofinishcount) / Convert.ToDouble(allcount);
                            nofinnishlv = percent.ToString("0.00%");//完结率
                        }
                        #endregion
                        #region 获取满意率
                        //var wfinishcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                        //                                             && p.createtime <= endtime
                        //                                             && p.Jwocode == Jwocode && p.isdelete == 0
                        //                                             && p.wostate == 3).Result.ToString();
                        var wfinishcount = wfinishcounts;
                        var allmywocodecount = allmywocodecounts.ToString();
                        if (Convert.ToDouble(wfinishcount) == 0 || allmywocodecounts == 0)
                        {
                            mydblcount = "0%";
                        }
                        else
                        {
                            double percent = Convert.ToDouble(allmywocodecount) / Convert.ToDouble(wfinishcount);
                            mydblcount = percent.ToString("0.00%");//满意度
                        }
                        #endregion
                        //满意数
                        mywocode = allmywocodecounts.ToString();
                        cwocode = allnomywocodecounts.ToString();
                        #region 本院量
                        //byformcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                        //                                                       && p.createtime <= endtime
                        //                                                      && p.Jwocode == Jwocode
                        //                                                      && p.isdelete == 0
                        //                                                      && (p.wostate == 3 || p.wostate == 2)
                        //                                                      && (p.CLpeoplefrom == "本院" || p.CLpeoplefrom == "本部")).Result.ToString();//本院 
                        byformcount = byformcounts.ToString();//本院
                        #endregion


                        #region 第三方
                        //dsffromcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                        //                                                       && p.createtime <= endtime
                        //                                                      && p.Jwocode == Jwocode
                        //                                                      && p.isdelete == 0
                        //                                                      && (p.wostate == 3 || p.wostate == 2)
                        //                                                      && p.CLpeoplefrom == "第三方").Result.ToString();//第三方 
                        dsffromcount = dsffromcounts.ToString();//第三方 
                        #endregion
                        if (Convert.ToDouble(finishcount) > 0)
                        {
                            //本院率
                            comfrombl = (Convert.ToDouble(byformcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                            //第三方率
                            comfromsbl = (Convert.ToDouble(dsffromcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                        }
                        #region 改派量
                        changepcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                                && p.createtime <= endtime
                                                                               && p.isdelete == 0
                                                                               && p.Pwcode == Jwocode).Result.ToString();//改派数量 
                        #endregion
                        #region 获取超时及时完成的单子数量
                        //获取完成的总单子信息
                        //var finishlist = await _wo_repair_baseRepository.Get(p => p.createtime >= startime
                        //                                             && p.createtime <= endtime
                        //                                             && p.Jwocode == Jwocode && p.isdelete == 0
                        //                                             && (p.wostate == 3 || p.wostate == 2));
                        //超时数量
                        int fcount = 0;
                        foreach (var item in finishlist)
                        {
                            if (item.wxtimeid != null || item.wxtimeid != "")
                            {
                                //根据id获取维修项目时效时间
                                var wxxmtime = await _wxwpstimeRepository.GetSingle(p => p.id == item.wxtimeid);
                                if (wxxmtime != null)
                                {
                                    if (item.dealtime == null || item.tobztimes == null)
                                    {
                                        continue;
                                    }
                                    DateTime dt1 = item.dealtime != null ? Convert.ToDateTime(item.dealtime) : Convert.ToDateTime(item.dealtime);
                                    DateTime dt2 = item.tobztimes != null ? Convert.ToDateTime(item.tobztimes) : Convert.ToDateTime(item.tobztimes);

                                    TimeSpan ts = dt1.Subtract(dt2);
                                    double hours = ts.TotalHours;//24.0

                                    if ((hours - wxxmtime.timecount) > 0)
                                    {
                                        fcount++;
                                    }

                                }
                            }
                        }
                        ///超时数量
                        csfinishcount = fcount.ToString();
                        //及时完成数量
                        jsfinishcount = (Convert.ToInt32(finishcount) - fcount).ToString();
                        #endregion 
                        #endregion
                        #endregion
                    }
                    else
                    {
                        #region 班组为空  时间段为空
                        #region 总接单量  已完结单量  未完结单量
                        allcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0).Result.ToString();
                        //finishcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0 && (p.wostate == 3 || p.wostate == 2)).Result.ToString();
                        nofinishcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0
                                                                        && (p.wostate == 0 || p.wostate == 1 || p.wostate == 7)).Result.ToString();
                        #endregion

                        #region 静态数据汇总
                        //获取在这个时间段内完成的数据
                        var finishlists = await _wo_repair_baseRepository.Get(p => p.Jwocode == Jwocode && p.isdelete == 0 && (p.wostate == 3 || p.wostate == 2));
                        //获取在这个时间段内未完成的数据
                        var nofinishlists = await _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0
                                                                      && (p.wostate == 0 || p.wostate == 1 || p.wostate == 7));
                        //完成数量
                        int wccount = 0;
                        //未完成数量
                        int nowccount = 0;
                        //完成的单子中的满意数量
                        int allmywocodecounts = 0;
                        //不满意数量
                        int allnomywocodecounts = 0;
                        //获取完成单子中的本院量
                        int byformcounts = 0;
                        //获取完成单子中的第三方量
                        int dsffromcounts = 0;
                        //完结单量
                        int wfinishcounts = 0;
                        List<Wo_Repair_Base> finishlist = new List<Wo_Repair_Base>();
                        foreach (var item in finishlists)
                        {
                            if (item.dealtime >= startime && item.dealtime <= endtime)
                            {
                                wccount++;
                                if (item.score == 4 || item.score == 5 || item.score == 3)
                                {
                                    allmywocodecounts++;
                                }
                                if (item.score == 1 || item.score == 2)
                                {
                                    allnomywocodecounts++;
                                }
                                if (item.CLpeoplefrom == "第三方")
                                {
                                    dsffromcounts++;
                                }
                                if (item.CLpeoplefrom == "本院" || item.CLpeoplefrom == "本部")
                                {
                                    byformcounts++;
                                }
                                if (item.wostate == 3)
                                {
                                    wfinishcounts++;
                                }
                                finishlist.Add(item);
                            }
                            else
                            {
                                nowccount++;
                                if (item.CLpeoplefrom == "第三方")
                                {
                                    dsffromcounts++;
                                }
                                if (item.CLpeoplefrom == "本院" || item.CLpeoplefrom == "本部")
                                {
                                    byformcounts++;
                                }
                            }
                        }

                        #region 获取完结率
                        finishcount = wccount.ToString();
                        if (Convert.ToDouble(finishcount) == 0 || Convert.ToDouble(allcount) == 0)
                        {
                            finishlv = "0%";
                        }
                        else
                        {
                            double percent = Convert.ToDouble(finishcount) / Convert.ToDouble(allcount);
                            finishlv = percent.ToString("0.00%");//完结率
                        }
                        #endregion
                        #region 获取未完成率
                        nofinishcount = (Convert.ToInt32(nofinishcount) + nowccount).ToString();
                        if (Convert.ToDouble(nofinishcount) == 0 || Convert.ToDouble(allcount) == 0)
                        {
                            nofinnishlv = "0%";
                        }
                        else
                        {
                            double percent = Convert.ToDouble(nofinishcount) / Convert.ToDouble(allcount);
                            nofinnishlv = percent.ToString("0.00%");//完结率
                        }
                        #endregion
                        #region 获取满意率
                        var allmywocodecount = allmywocodecounts.ToString();
                        var wfinishcount = wfinishcounts;
                        if (Convert.ToDouble(wfinishcount) == 0 || allmywocodecounts == 0)
                        {
                            mydblcount = "0%";
                        }
                        else
                        {
                            double percent = Convert.ToDouble(allmywocodecount) / Convert.ToDouble(wfinishcount);
                            mydblcount = percent.ToString("0.00%");//满意度
                        }
                        #endregion
                        //满意数
                        mywocode = allmywocodecounts.ToString();
                        cwocode = allnomywocodecounts.ToString();
                        #region 本院量
                        byformcount = byformcounts.ToString();//本院
                        #endregion
                        #region 第三方
                        //dsffromcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                        //                                                       && p.createtime <= endtime
                        //                                                      && p.Jwocode == Jwocode
                        //                                                      && p.isdelete == 0
                        //                                                      && (p.wostate == 3 || p.wostate == 2)
                        //                                                      && p.CLpeoplefrom == "第三方").Result.ToString();//第三方 
                        dsffromcount = dsffromcounts.ToString();//第三方 
                        #endregion
                        if (Convert.ToDouble(finishcount) > 0)
                        {
                            //本院率
                            comfrombl = (Convert.ToDouble(byformcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                            //第三方率
                            comfromsbl = (Convert.ToDouble(dsffromcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                        }
                        #region 改派量
                        changepcount = _wo_repair_baseRepository.Count(p => p.isdelete == 0
                                                                               && p.Pwcode == Jwocode).Result.ToString();//改派数量 
                        #endregion
                        #region 获取超时及时完成的单子数量
                        //超时数量
                        int fcount = 0;
                        foreach (var item in finishlist)
                        {
                            if (item.wxtimeid != null || item.wxtimeid != "")
                            {
                                //根据id获取维修项目时效时间
                                var wxxmtime = await _wxwpstimeRepository.GetSingle(p => p.id == item.wxtimeid);
                                if (wxxmtime != null)
                                {
                                    if (item.dealtime == null || item.tobztimes == null)
                                    {
                                        continue;
                                    }
                                    DateTime dt1 = item.dealtime != null ? Convert.ToDateTime(item.dealtime) : Convert.ToDateTime(item.dealtime);
                                    DateTime dt2 = item.tobztimes != null ? Convert.ToDateTime(item.tobztimes) : Convert.ToDateTime(item.tobztimes);

                                    TimeSpan ts = dt1.Subtract(dt2);
                                    double hours = ts.TotalHours;//24.0

                                    if ((hours - wxxmtime.timecount) > 0)
                                    {
                                        fcount++;
                                    }

                                }
                            }
                        }
                        ///超时数量
                        csfinishcount = fcount.ToString();
                        //及时完成数量
                        jsfinishcount = (Convert.ToInt32(finishcount) - fcount).ToString();
                        #endregion 
                        #endregion

                        #endregion
                    }
                    #endregion
                    #region 赋值填数据
                    DayTjlists daytjinfo = new DayTjlists();
                    daytjinfo.deptname = Jwocode;//接单班组
                    daytjinfo.daycounts = allcount;//总接单量
                    daytjinfo.finishcount = finishcount;//完结单量
                    daytjinfo.jsfinishcount = jsfinishcount; //及时完成
                    daytjinfo.csfinishcount = csfinishcount; //超时完成
                    daytjinfo.nfinishcount = nofinishcount;//未完结单量
                    daytjinfo.finishlv = finishlv;         //完成单率
                    daytjinfo.nofinnishlv = nofinnishlv;    //未完成率
                    daytjinfo.mywocode = mywocode;//满意
                    //daytjinfo.ybwocode = ybwocode;//一般
                    daytjinfo.cwocode = cwocode;//差
                    daytjinfo.mydinfo = mydblcount;//满意度
                    daytjinfo.comfrom = byformcount;//本院
                    daytjinfo.comfrombl = comfrombl;//本院率
                    daytjinfo.comfroms = dsffromcount;//第三方
                    daytjinfo.comfromsbl = comfromsbl;//第三方率
                    daytjinfo.changecount = changepcount;//改派数量
                    list.Add(daytjinfo);
                    #endregion
                    #endregion
                }
                else
                {
                    #region 定义总数信息字段
                    int lastallcount = 0;
                    int lastfinishcount = 0;
                    int lastnofinishcount = 0;
                    #endregion
                    #region 获取所有班组信息
                    string Jwocdes = "";
                    //获取所有班组信息
                    var bzinfolist = await _ISys_Role_InfoRepository.Get(p => p.role_code == "YWYG" && p.role_name.Contains("班") && p.state_flag == 1);
                    var bzcount = 1;
                    foreach (var item in bzinfolist)
                    {
                        Jwocdes += item.role_name;
                        if (bzcount != bzinfolist.Count())
                        {
                            Jwocdes += ",";
                        }
                        bzcount++;
                    }
                    #endregion
                    #region 获取到所有班组后进行单个获取数据
                    string[] arrStr = Jwocdes.Split(',');
                    foreach (var item in arrStr)
                    {
                        var byformcount = "";//本院
                        var dsffromcount = "";//第三方
                        var changepcount = "";//改派数量
                                              //传的参数中如果没有班组信息就用这些定义的全部显示
                        Jwocode = item;
                        #region 判断时间是否为空
                        if (startime != null && endtime != null)
                        {
                            #region 时间不为空时
                            #region 总接单量  已完成量  未完成量
                            allcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                                      && p.createtime <= endtime
                                                                                      && p.Jwocode == Jwocode && p.isdelete == 0).Result.ToString();
                            //finishcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                            //                                             && p.createtime <= endtime
                            //                                             && p.Jwocode == Jwocode && p.isdelete == 0
                            //                                             && (p.wostate == 3 || p.wostate == 2)).Result.ToString();
                            nofinishcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                         && p.createtime <= endtime
                                                                         && p.Jwocode == Jwocode && p.isdelete == 0
                                                                         && (p.wostate == 0 || p.wostate == 1 || p.wostate == 7)).Result.ToString();
                            #endregion
                            #region 静态数据汇总
                            //获取在这个时间段内完成的数据
                            var finishlists = await _wo_repair_baseRepository.Get(p => p.createtime >= startime
                                                                           && p.createtime <= endtime
                                                                           && p.Jwocode == Jwocode && p.isdelete == 0
                                                                           && (p.wostate == 3 || p.wostate == 2));
                            //获取在这个时间段内未完成的数据
                            var nofinishlists = await _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                          && p.createtime <= endtime
                                                                          && p.Jwocode == Jwocode && p.isdelete == 0
                                                                          && (p.wostate == 0 || p.wostate == 1 || p.wostate == 7));
                            //完成数量
                            int wccount = 0;
                            //未完成数量
                            int nowccount = 0;
                            //完成的单子中的满意数量
                            int allmywocodecounts = 0;
                            //获取完成单子中的本院量
                            int byformcounts = 0;
                            //不满意数量
                            int allnomywocodecounts = 0;
                            //获取完成单子中的第三方量
                            int dsffromcounts = 0;
                            int wfinishcounts = 0;
                            List<Wo_Repair_Base> finishlist = new List<Wo_Repair_Base>();
                            foreach (var finitem in finishlists)
                            {
                                if (finitem.dealtime >= startime && finitem.dealtime <= endtime)
                                {
                                    wccount++;
                                    if (finitem.score == 4 || finitem.score == 5 || finitem.score == 3)
                                    {
                                        allmywocodecounts++;
                                    }
                                    if (finitem.score == 1 || finitem.score == 2)
                                    {
                                        allnomywocodecounts++;
                                    }
                                    if (finitem.CLpeoplefrom == "第三方")
                                    {
                                        dsffromcounts++;
                                    }
                                    if (finitem.CLpeoplefrom == "本院" || finitem.CLpeoplefrom == "本部")
                                    {
                                        byformcounts++;
                                    }
                                    if (finitem.wostate == 3)
                                    {
                                        wfinishcounts++;
                                    }
                                    finishlist.Add(finitem);
                                }
                                else
                                {
                                    nowccount++;
                                    if (finitem.CLpeoplefrom == "第三方")
                                    {
                                        dsffromcounts++;
                                    }
                                    if (finitem.CLpeoplefrom == "本院" || finitem.CLpeoplefrom == "本部")
                                    {
                                        byformcounts++;
                                    }
                                }
                            }

                            #region 获取完结率
                            finishcount = wccount.ToString();
                            if (Convert.ToDouble(finishcount) == 0 || Convert.ToDouble(allcount) == 0)
                            {
                                finishlv = "0%";
                            }
                            else
                            {
                                double percent = Convert.ToDouble(finishcount) / Convert.ToDouble(allcount);
                                finishlv = percent.ToString("0.00%");//完结率
                            }
                            #endregion
                            #region 获取未完成率
                            nofinishcount = (Convert.ToInt32(nofinishcount) + nowccount).ToString();
                            if (Convert.ToDouble(nofinishcount) == 0 || Convert.ToDouble(allcount) == 0)
                            {
                                nofinnishlv = "0%";
                            }
                            else
                            {
                                double percent = Convert.ToDouble(nofinishcount) / Convert.ToDouble(allcount);
                                nofinnishlv = percent.ToString("0.00%");//完结率
                            }
                            #endregion
                            #region 获取满意率
                            var allmywocodecount = allmywocodecounts.ToString();
                            var wfinishcount = wfinishcounts;
                            if (Convert.ToDouble(wfinishcount) == 0 || allmywocodecounts == 0)
                            {
                                mydblcount = "0%";
                            }
                            else
                            {
                                double percent = Convert.ToDouble(allmywocodecount) / Convert.ToDouble(wfinishcount);
                                mydblcount = percent.ToString("0.00%");//满意度
                            }
                            #endregion
                            //满意数
                            mywocode = allmywocodecounts.ToString();
                            cwocode = allnomywocodecounts.ToString();
                            #region 本院量

                            byformcount = byformcounts.ToString();//本院
                            #endregion
                            #region 第三方
                            dsffromcount = dsffromcounts.ToString();//第三方 
                            #endregion
                            if (Convert.ToDouble(finishcount) > 0)
                            {
                                //本院率
                                comfrombl = (Convert.ToDouble(byformcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                                //第三方率
                                comfromsbl = (Convert.ToDouble(dsffromcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                            }
                            #region 改派量
                            changepcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                                    && p.createtime <= endtime
                                                                                   && p.isdelete == 0
                                                                                   && p.Pwcode == Jwocode).Result.ToString();//改派数量 
                            #endregion
                            #region 获取超时及时完成的单子数量
                            //超时数量
                            int fcount = 0;
                            foreach (var finitem in finishlist)
                            {
                                if (finitem.wxtimeid != null || finitem.wxtimeid != "")
                                {
                                    //根据id获取维修项目时效时间
                                    var wxxmtime = await _wxwpstimeRepository.GetSingle(p => p.id == finitem.wxtimeid);
                                    if (wxxmtime != null)
                                    {
                                        if (finitem.dealtime == null || finitem.tobztimes == null)
                                        {
                                            continue;
                                        }
                                        DateTime dt1 = finitem.dealtime != null ? Convert.ToDateTime(finitem.dealtime) : Convert.ToDateTime(finitem.dealtime);
                                        DateTime dt2 = finitem.tobztimes != null ? Convert.ToDateTime(finitem.tobztimes) : Convert.ToDateTime(finitem.tobztimes);

                                        TimeSpan ts = dt1.Subtract(dt2);
                                        double hours = ts.TotalHours;//24.0

                                        if ((hours - wxxmtime.timecount) > 0)
                                        {
                                            fcount++;
                                        }

                                    }
                                }
                            }
                            ///超时数量
                            csfinishcount = fcount.ToString();
                            //及时完成数量
                            jsfinishcount = (Convert.ToInt32(finishcount) - fcount).ToString();
                            #endregion
                            #endregion
                            #endregion

                        }
                        else
                        {
                            #region 时间为空时
                            #region 总接单量  完成量 为完成量
                            allcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0).Result.ToString();

                            //finishcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0 && (p.wostate == 3 || p.wostate == 2)).Result.ToString();
                            nofinishcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0
                                                                            && (p.wostate == 0 || p.wostate == 1 || p.wostate == 7)).Result.ToString();
                            #endregion
                            #region 静态数据汇总
                            //获取在这个时间段内完成的数据
                            var finishlists = await _wo_repair_baseRepository.Get(p => p.Jwocode == Jwocode && p.isdelete == 0 && (p.wostate == 3 || p.wostate == 2));
                            //获取在这个时间段内未完成的数据
                            var nofinishlists = await _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0
                                                                          && (p.wostate == 0 || p.wostate == 1 || p.wostate == 7));
                            //完成数量
                            int wccount = 0;
                            //未完成数量
                            int nowccount = 0;
                            //完成的单子中的满意数量
                            int allmywocodecounts = 0;
                            //不满意数量
                            int allnomywocodecounts = 0;
                            //获取完成单子中的本院量
                            int byformcounts = 0;
                            //获取完成单子中的第三方量
                            int dsffromcounts = 0;
                            int wfinishcounts = 0;
                            List<Wo_Repair_Base> finishlist = new List<Wo_Repair_Base>();
                            foreach (var finiitem in finishlists)
                            {
                                if (finiitem.dealtime >= startime && finiitem.dealtime <= endtime)
                                {
                                    wccount++;
                                    if (finiitem.score == 4 || finiitem.score == 5 || finiitem.score == 3)
                                    {
                                        allmywocodecounts++;
                                    }
                                    if (finiitem.score == 1 || finiitem.score == 2)
                                    {
                                        allnomywocodecounts++;
                                    }
                                    if (finiitem.CLpeoplefrom == "第三方")
                                    {
                                        dsffromcounts++;
                                    }
                                    if (finiitem.CLpeoplefrom == "本院" || finiitem.CLpeoplefrom == "本部")
                                    {
                                        byformcounts++;
                                    }
                                    if (finiitem.wostate == 3)
                                    {
                                        wfinishcounts++;
                                    }
                                    finishlist.Add(finiitem);
                                }
                                else
                                {
                                    nowccount++;
                                    if (finiitem.CLpeoplefrom == "第三方")
                                    {
                                        dsffromcounts++;
                                    }
                                    if (finiitem.CLpeoplefrom == "本院" || finiitem.CLpeoplefrom == "本部")
                                    {
                                        byformcounts++;
                                    }
                                }
                            }

                            #region 获取完结率
                            finishcount = wccount.ToString();
                            if (Convert.ToDouble(finishcount) == 0 || Convert.ToDouble(allcount) == 0)
                            {
                                finishlv = "0%";
                            }
                            else
                            {
                                double percent = Convert.ToDouble(finishcount) / Convert.ToDouble(allcount);
                                finishlv = percent.ToString("0.00%");//完结率
                            }
                            #endregion
                            #region 获取未完成率
                            nofinishcount = (Convert.ToInt32(nofinishcount) + nowccount).ToString();
                            if (Convert.ToDouble(nofinishcount) == 0 || Convert.ToDouble(allcount) == 0)
                            {
                                nofinnishlv = "0%";
                            }
                            else
                            {
                                double percent = Convert.ToDouble(nofinishcount) / Convert.ToDouble(allcount);
                                nofinnishlv = percent.ToString("0.00%");//完结率
                            }
                            #endregion
                            #region 获取满意率
                            var allmywocodecount = allmywocodecounts.ToString();
                            var wfinishcount = wfinishcounts;
                            if (Convert.ToDouble(wfinishcount) == 0 || allmywocodecounts == 0)
                            {
                                mydblcount = "0%";
                            }
                            else
                            {
                                double percent = Convert.ToDouble(allmywocodecount) / Convert.ToDouble(wfinishcount);
                                mydblcount = percent.ToString("0.00%");//满意度
                            }
                            #endregion
                            //满意数
                            mywocode = allmywocodecounts.ToString();
                            cwocode = allnomywocodecounts.ToString();
                            #region 本院量
                            //byformcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                            //                                                       && p.createtime <= endtime
                            //                                                      && p.Jwocode == Jwocode
                            //                                                      && p.isdelete == 0
                            //                                                      && (p.wostate == 3 || p.wostate == 2)
                            //                                                      && (p.CLpeoplefrom == "本院" || p.CLpeoplefrom == "本部")).Result.ToString();//本院 
                            byformcount = byformcounts.ToString();//本院
                            #endregion
                            #region 第三方
                            //dsffromcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                            //                                                       && p.createtime <= endtime
                            //                                                      && p.Jwocode == Jwocode
                            //                                                      && p.isdelete == 0
                            //                                                      && (p.wostate == 3 || p.wostate == 2)
                            //                                                      && p.CLpeoplefrom == "第三方").Result.ToString();//第三方 
                            dsffromcount = dsffromcounts.ToString();//第三方 
                            #endregion
                            if (Convert.ToDouble(finishcount) > 0)
                            {
                                //本院率
                                comfrombl = (Convert.ToDouble(byformcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                                //第三方率
                                comfromsbl = (Convert.ToDouble(dsffromcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                            }
                            #region 改派量
                            changepcount = _wo_repair_baseRepository.Count(p => p.isdelete == 0
                                                                                   && p.Pwcode == Jwocode).Result.ToString();//改派数量 
                            #endregion
                            #region 获取超时及时完成的单子数量
                            //超时数量
                            int fcount = 0;
                            foreach (var finiitem in finishlist)
                            {
                                if (finiitem.wxtimeid != null || finiitem.wxtimeid != "")
                                {
                                    //根据id获取维修项目时效时间
                                    var wxxmtime = await _wxwpstimeRepository.GetSingle(p => p.id == finiitem.wxtimeid);
                                    if (wxxmtime != null)
                                    {
                                        if (finiitem.dealtime == null || finiitem.tobztimes == null)
                                        {
                                            continue;
                                        }
                                        DateTime dt1 = finiitem.dealtime != null ? Convert.ToDateTime(finiitem.dealtime) : Convert.ToDateTime(finiitem.dealtime);
                                        DateTime dt2 = finiitem.tobztimes != null ? Convert.ToDateTime(finiitem.tobztimes) : Convert.ToDateTime(finiitem.tobztimes);

                                        TimeSpan ts = dt1.Subtract(dt2);
                                        double hours = ts.TotalHours;//24.0

                                        if ((hours - wxxmtime.timecount) > 0)
                                        {
                                            fcount++;
                                        }

                                    }
                                }
                            }

                            ///超时数量
                            csfinishcount = fcount.ToString();
                            //及时完成数量
                            jsfinishcount = (Convert.ToInt32(finishcount) - fcount).ToString();
                            #endregion
                            #endregion
                            #endregion
                        }
                        #endregion
                        #region 赋值添加数据
                        DayTjlists daytjinfo = new DayTjlists();
                        daytjinfo.deptname = Jwocode;//接单班组
                        daytjinfo.daycounts = allcount;//总接单量
                        daytjinfo.finishcount = finishcount;//完结单量
                        daytjinfo.jsfinishcount = jsfinishcount; //及时完成
                        daytjinfo.csfinishcount = csfinishcount; //超时完成
                        daytjinfo.nfinishcount = nofinishcount;//未完结单量
                        daytjinfo.finishlv = finishlv;         //完成单率
                        daytjinfo.nofinnishlv = nofinnishlv;    //未完成率
                        daytjinfo.mywocode = mywocode;//满意
                        //daytjinfo.ybwocode = ybwocode;//一般
                        daytjinfo.cwocode = cwocode;//差
                        daytjinfo.mydinfo = mydblcount;//满意度
                        daytjinfo.comfrom = byformcount;//本院
                        daytjinfo.comfrombl = comfrombl;//本院率   daytjinfo.comfromsbl = comfromsbl;//第三方率
                        daytjinfo.comfroms = dsffromcount;//第三方
                        daytjinfo.comfromsbl = comfromsbl;//第三方率
                        daytjinfo.changecount = changepcount;//改派数量
                        list.Add(daytjinfo);
                        #endregion
                        #region 计算总数
                        lastallcount += Convert.ToInt32(allcount);
                        lastfinishcount += Convert.ToInt32(finishcount);
                        lastnofinishcount += Convert.ToInt32(nofinishcount);
                        #endregion
                    }
                    #endregion
                    #region 最后一行总数
                    DayTjlists daytjinfo1 = new DayTjlists();
                    daytjinfo1.deptname = "总数";//接单班组
                    daytjinfo1.daycounts = lastallcount.ToString();// allcount;//总接单量
                    daytjinfo1.finishcount = lastfinishcount.ToString();// finishcount;//完结单量
                    daytjinfo1.jsfinishcount = jsfinishcount; //及时完成
                    daytjinfo1.csfinishcount = csfinishcount; //超时完成
                    daytjinfo1.nfinishcount = lastnofinishcount.ToString();// nofinishcount;//未完结单量
                    daytjinfo1.finishlv = "";         //完成单率
                    daytjinfo1.nofinnishlv = "";    //未完成率
                    daytjinfo1.mywocode = mywocode;//满意
                    //daytjinfo1.ybwocode = ybwocode;//一般
                    daytjinfo1.cwocode = cwocode;//差
                    daytjinfo1.mydinfo = "";//满意度
                    daytjinfo1.comfrom = "";//本院
                    daytjinfo1.comfroms = "";//第三方
                    daytjinfo1.changecount = "";//改派数量
                    list.Add(daytjinfo1);
                    #endregion
                }
                #endregion
                #endregion
            }
            else
            {
                #region 动态
                #region 获取list集合数据
                if (Jwocode != null)
                {
                    #region 班组不为空时
                    var byformcount = "";//本院
                    var dsffromcount = "";//第三方
                    var changepcount = "";//改派数量
                    #region 判断时间是否为空
                    if (startime != null && endtime != null)
                    {
                        #region 班组不为空  时间段不为空时
                        #region 总接单量   已完成数量   未完成数量  
                        allcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                               && p.createtime <= endtime
                                                                               && p.Jwocode == Jwocode && p.isdelete == 0).Result.ToString();
                        finishcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                     && p.createtime <= endtime
                                                                     && p.Jwocode == Jwocode && p.isdelete == 0
                                                                     && (p.wostate == 3 || p.wostate == 2)).Result.ToString();
                        nofinishcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                     && p.createtime <= endtime
                                                                     && p.Jwocode == Jwocode && p.isdelete == 0
                                                                     && (p.wostate == 0 || p.wostate == 1 || p.wostate == 7)).Result.ToString();
                        #endregion
                        #region 获取完结率
                        if (Convert.ToDouble(finishcount) == 0 || Convert.ToDouble(allcount) == 0)
                        {
                            finishlv = "0%";
                        }
                        else
                        {
                            double percent = Convert.ToDouble(finishcount) / Convert.ToDouble(allcount);
                            finishlv = percent.ToString("0.00%");//完结率
                        }
                        #endregion
                        #region 获取未完成率
                        if (Convert.ToDouble(nofinishcount) == 0 || Convert.ToDouble(allcount) == 0)
                        {
                            nofinnishlv = "0%";
                        }
                        else
                        {
                            double percent = Convert.ToDouble(nofinishcount) / Convert.ToDouble(allcount);
                            nofinnishlv = percent.ToString("0.00%");//完结率
                        }
                        #endregion
                        #region 获取满意率
                        //先获取所有满意的班组内的工单数量
                        //double percent = Convert.ToDouble(2) / Convert.ToDouble(34);
                        //string result = percent.ToString("0.000%");//得到5.882%
                        var allmywocodecount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                     && p.createtime <= endtime
                                                                      && p.Jwocode == Jwocode
                                                                      && p.isdelete == 0
                                                                      && p.wostate == 3
                                                                      && (p.score == 4 || p.score == 5 || p.score == 3)).Result.ToString();
                        var wfinishcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                              && p.createtime <= endtime
                                                              && p.Jwocode == Jwocode && p.isdelete == 0
                                                              && p.wostate == 3).Result.ToString();
                        if (Convert.ToDouble(wfinishcount) == 0)
                        {
                            mydblcount = "0.00%";
                        }
                        else
                        {
                            double percent = Convert.ToDouble(allmywocodecount) / Convert.ToDouble(wfinishcount);
                            mydblcount = percent.ToString("0.00%");//满意度
                        }

                        #endregion
                        #region 本院量
                        byformcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                               && p.createtime <= endtime
                                                                              && p.Jwocode == Jwocode
                                                                              && p.isdelete == 0
                                                                              && (p.wostate == 3 || p.wostate == 2)
                                                                              && (p.CLpeoplefrom == "本院" || p.CLpeoplefrom == "本部")).Result.ToString();//本院 
                        #endregion
                        #region 第三方
                        dsffromcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                               && p.createtime <= endtime
                                                                              && p.Jwocode == Jwocode
                                                                              && p.isdelete == 0
                                                                              && (p.wostate == 3 || p.wostate == 2)
                                                                              && p.CLpeoplefrom == "第三方").Result.ToString();//第三方 
                        #endregion
                        var allnomywocodecounts = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                     && p.createtime <= endtime
                                                                      && p.Jwocode == Jwocode
                                                                      && p.isdelete == 0
                                                                      && p.wostate == 3
                                                                      && (p.score == 1 || p.score == 2)).Result.ToString();
                        //满意数
                        mywocode = allmywocodecount;
                        cwocode = allnomywocodecounts;
                        if (Convert.ToDouble(finishcount) > 0)
                        {
                            //本院率
                            comfrombl = (Convert.ToDouble(byformcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                            //第三方率
                            comfromsbl = (Convert.ToDouble(dsffromcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                        }
                        #region 改派量
                        changepcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                                && p.createtime <= endtime
                                                                               && p.isdelete == 0
                                                                               && p.Pwcode == Jwocode).Result.ToString();//改派数量 
                        #endregion
                        #region 获取超时及时完成的单子数量
                        //获取完成的总单子信息
                        var finishlist = await _wo_repair_baseRepository.Get(p => p.createtime >= startime
                                                                     && p.createtime <= endtime
                                                                     && p.Jwocode == Jwocode && p.isdelete == 0
                                                                     && (p.wostate == 3 || p.wostate == 2));
                        //超时数量
                        int fcount = 0;
                        foreach (var item in finishlist)
                        {
                            if (item.wxtimeid != null || item.wxtimeid != "")
                            {
                                //根据id获取维修项目时效时间
                                var wxxmtime = await _wxwpstimeRepository.GetSingle(p => p.id == item.wxtimeid);
                                if (wxxmtime != null)
                                {
                                    if (item.dealtime == null || item.tobztimes == null)
                                    {
                                        continue;
                                    }
                                    DateTime dt1 = item.dealtime != null ? Convert.ToDateTime(item.dealtime) : Convert.ToDateTime(item.dealtime);
                                    DateTime dt2 = item.tobztimes != null ? Convert.ToDateTime(item.tobztimes) : Convert.ToDateTime(item.tobztimes);

                                    TimeSpan ts = dt1.Subtract(dt2);
                                    double hours = ts.TotalHours;//24.0

                                    if ((hours - wxxmtime.timecount) > 0)
                                    {
                                        fcount++;
                                    }

                                }
                            }
                        }
                        ///超时数量
                        csfinishcount = fcount.ToString();
                        //及时完成数量
                        jsfinishcount = (Convert.ToInt32(finishcount) - fcount).ToString();
                        #endregion
                        #endregion
                    }
                    else
                    {
                        #region 班组为空  时间段为空
                        #region 总接单量  已完结单量  未完结单量
                        allcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0).Result.ToString();
                        finishcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0 && (p.wostate == 3 || p.wostate == 2)).Result.ToString();
                        nofinishcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0
                                                                        && (p.wostate == 0 || p.wostate == 1 || p.wostate == 7)).Result.ToString();
                        #endregion
                        #region 获取完结率
                        if (Convert.ToDouble(finishcount) == 0 || Convert.ToDouble(allcount) == 0)
                        {
                            finishlv = "0%";
                        }
                        else
                        {
                            double percent = Convert.ToDouble(finishcount) / Convert.ToDouble(allcount);
                            finishlv = percent.ToString("0.00%");//完结率
                        }
                        #endregion
                        #region 获取未完成率
                        if (Convert.ToDouble(nofinishcount) == 0 || Convert.ToDouble(allcount) == 0)
                        {
                            nofinnishlv = "0%";
                        }
                        else
                        {
                            double percent = Convert.ToDouble(nofinishcount) / Convert.ToDouble(allcount);
                            nofinnishlv = percent.ToString("0.00%");//完结率
                        }
                        #endregion
                        #region 满意度
                        //先获取所有满意的班组内的工单数量
                        //double percent = Convert.ToDouble(2) / Convert.ToDouble(34);
                        //string result = percent.ToString("0.000%");//得到5.882%
                        var allmywocodecount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode
                                                                      && p.isdelete == 0
                                                                      && p.wostate == 3
                                                                      && (p.score == 4 || p.score == 5 || p.score == 3)).Result.ToString();
                        var wfinishcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0
                                                             && p.wostate == 3).Result.ToString();
                        if (Convert.ToDouble(wfinishcount) == 0)
                        {
                            mydblcount = "0%";
                        }
                        else
                        {
                            double percent = Convert.ToDouble(allmywocodecount) / Convert.ToDouble(wfinishcount);
                            mydblcount = percent.ToString("0.00%");//满意度
                        }
                        #endregion
                        #region 本院量
                        byformcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode
                                                                               && p.isdelete == 0
                                                                               && (p.wostate == 3 || p.wostate == 2)
                                                                               && (p.CLpeoplefrom == "本院" || p.CLpeoplefrom == "本部")).Result.ToString();//本院 
                        #endregion
                        #region 第三方量
                        dsffromcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode
                                                                               && p.isdelete == 0
                                                                               && (p.wostate == 3 || p.wostate == 2)
                                                                               && p.CLpeoplefrom == "第三方").Result.ToString();//第三方 
                        #endregion
                        var allnomywocodecounts = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode
                                                                     && p.isdelete == 0
                                                                     && p.wostate == 3
                                                                     && (p.score == 1 || p.score == 2)).Result.ToString();
                        //满意数
                        mywocode = allmywocodecount;
                        cwocode = allnomywocodecounts;
                        if (Convert.ToDouble(finishcount) > 0)
                        {
                            //本院率
                            comfrombl = (Convert.ToDouble(byformcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                            //第三方率
                            comfromsbl = (Convert.ToDouble(dsffromcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                        }
                        #region 改派量
                        changepcount = _wo_repair_baseRepository.Count(p => p.isdelete == 0
                                                                               && p.Pwcode == Jwocode).Result.ToString();//改派数量 
                        #endregion
                        #region 获取超时及时完成的单子数量
                        //获取完成的总单子信息
                        var finishlist = await _wo_repair_baseRepository.Get(p => p.Jwocode == Jwocode && p.isdelete == 0 && (p.wostate == 3 || p.wostate == 2));
                        //超时数量
                        int fcount = 0;
                        foreach (var item in finishlist)
                        {
                            if (item.wxtimeid != null || item.wxtimeid != "")
                            {
                                if (item.dealtime == null || item.tobztimes == null)
                                {
                                    continue;
                                }
                                //根据id获取维修项目时效时间
                                var wxxmtime = await _wxwpstimeRepository.GetSingle(p => p.id == item.wxtimeid);
                                if (wxxmtime != null)
                                {
                                    DateTime dt1 = item.dealtime != null ? Convert.ToDateTime(item.dealtime) : Convert.ToDateTime(item.dealtime);
                                    DateTime dt2 = item.tobztimes != null ? Convert.ToDateTime(item.tobztimes) : Convert.ToDateTime(item.tobztimes);

                                    TimeSpan ts = dt1.Subtract(dt2);
                                    double hours = ts.TotalHours;//24.0

                                    if ((hours - wxxmtime.timecount) > 0)
                                    {
                                        fcount++;
                                    }

                                }
                            }
                        }
                        ///超时数量
                        csfinishcount = fcount.ToString();
                        //及时完成数量
                        jsfinishcount = (Convert.ToInt32(finishcount) - fcount).ToString();
                        #endregion
                        #endregion
                    }
                    #endregion
                    #region 赋值填数据
                    DayTjlists daytjinfo = new DayTjlists();
                    daytjinfo.deptname = Jwocode;//接单班组
                    daytjinfo.daycounts = allcount;//总接单量
                    daytjinfo.finishcount = finishcount;//完结单量
                    daytjinfo.jsfinishcount = jsfinishcount; //及时完成
                    daytjinfo.csfinishcount = csfinishcount; //超时完成
                    daytjinfo.nfinishcount = nofinishcount;//未完结单量
                    daytjinfo.finishlv = finishlv;         //完成单率
                    daytjinfo.nofinnishlv = nofinnishlv;    //未完成率
                    daytjinfo.mywocode = mywocode;//满意
                    //daytjinfo.ybwocode = ybwocode;//一般
                    daytjinfo.cwocode = cwocode;//差
                    daytjinfo.mydinfo = mydblcount;//满意度
                    daytjinfo.comfrom = byformcount;//本院
                    daytjinfo.comfrombl = comfrombl;//本院率  
                    daytjinfo.comfroms = dsffromcount;//第三方
                    daytjinfo.comfromsbl = comfromsbl;//第三方率
                    daytjinfo.changecount = changepcount;//改派数量
                    list.Add(daytjinfo);
                    #endregion
                    #endregion
                }
                else
                {
                    #region 定义总数信息字段
                    int lastallcount = 0;
                    int lastfinishcount = 0;
                    int lastnofinishcount = 0;
                    #endregion
                    #region 获取所有班组信息
                    string Jwocdes = "";
                    //获取所有班组信息
                    var bzinfolist = await _ISys_Role_InfoRepository.Get(p => p.role_code == "YWYG" && p.role_name.Contains("班") && p.state_flag == 1);
                    var bzcount = 1;
                    foreach (var item in bzinfolist)
                    {
                        Jwocdes += item.role_name;
                        if (bzcount != bzinfolist.Count())
                        {
                            Jwocdes += ",";
                        }
                        bzcount++;
                    }
                    #endregion
                    #region 获取到所有班组后进行单个获取数据
                    string[] arrStr = Jwocdes.Split(',');
                    foreach (var item in arrStr)
                    {
                        var byformcount = "";//本院
                        var dsffromcount = "";//第三方
                        var changepcount = "";//改派数量
                                              //传的参数中如果没有班组信息就用这些定义的全部显示
                        Jwocode = item;
                        #region 判断时间是否为空
                        if (startime != null && endtime != null)
                        {
                            #region 时间不为空时
                            #region 总接单量  已完成量  未完成量
                            allcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                                      && p.createtime <= endtime
                                                                                      && p.Jwocode == Jwocode && p.isdelete == 0).Result.ToString();
                            finishcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                         && p.createtime <= endtime
                                                                         && p.Jwocode == Jwocode && p.isdelete == 0
                                                                         && (p.wostate == 3 || p.wostate == 2)).Result.ToString();
                            nofinishcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                         && p.createtime <= endtime
                                                                         && p.Jwocode == Jwocode && p.isdelete == 0
                                                                         && (p.wostate == 0 || p.wostate == 1 || p.wostate == 7)).Result.ToString();
                            #endregion
                            #region 获取完结率
                            if (Convert.ToDouble(finishcount) == 0 || Convert.ToDouble(allcount) == 0)
                            {
                                finishlv = "0%";
                            }
                            else
                            {
                                double percent = Convert.ToDouble(finishcount) / Convert.ToDouble(allcount);
                                finishlv = percent.ToString("0.00%");//完结率
                            }
                            #endregion
                            #region 获取未完成率
                            if (Convert.ToDouble(nofinishcount) == 0 || Convert.ToDouble(allcount) == 0)
                            {
                                nofinnishlv = "0%";
                            }
                            else
                            {
                                double percent = Convert.ToDouble(nofinishcount) / Convert.ToDouble(allcount);
                                nofinnishlv = percent.ToString("0.00%");//完结率
                            }
                            #endregion
                            #region 满意度
                            //先获取所有满意的班组内的工单数量
                            //double percent = Convert.ToDouble(2) / Convert.ToDouble(34);
                            //string result = percent.ToString("0.000%");//得到5.882%
                            var allmywocodecount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                         && p.createtime <= endtime
                                                                          && p.Jwocode == Jwocode
                                                                          && p.isdelete == 0
                                                                          && p.wostate == 3
                                                                          && (p.score == 4 || p.score == 5 || p.score == 3)).Result.ToString();
                            var wfinishcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                             && p.createtime <= endtime
                                                             && p.Jwocode == Jwocode && p.isdelete == 0
                                                             && p.wostate == 3).Result.ToString();
                            if (Convert.ToDouble(wfinishcount) == 0)
                            {
                                mydblcount = "0%";
                            }
                            else
                            {
                                double percent = Convert.ToDouble(allmywocodecount) / Convert.ToDouble(wfinishcount);
                                mydblcount = percent.ToString("0.00%");//满意度
                            }
                            #endregion
                            #region 本院量
                            byformcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                                       && p.createtime <= endtime
                                                                                      && p.Jwocode == Jwocode
                                                                                      && p.isdelete == 0
                                                                                      && (p.wostate == 3 || p.wostate == 2)
                                                                                      && (p.CLpeoplefrom == "本院" || p.CLpeoplefrom == "本部")).Result.ToString();//本院 
                            #endregion
                            #region 第三方量
                            dsffromcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                                        && p.createtime <= endtime
                                                                                       && p.Jwocode == Jwocode
                                                                                       && p.isdelete == 0
                                                                                       && (p.wostate == 3 || p.wostate == 2)
                                                                                       && p.CLpeoplefrom == "第三方").Result.ToString();//第三方 
                            #endregion
                            var allnomywocodecounts = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                   && p.createtime <= endtime
                                                                    && p.Jwocode == Jwocode
                                                                    && p.isdelete == 0
                                                                    && p.wostate == 3
                                                                    && (p.score == 1 || p.score == 2)).Result.ToString();
                            //满意数
                            mywocode = allmywocodecount;
                            cwocode = allnomywocodecounts;
                            if (Convert.ToDouble(finishcount) > 0)
                            {
                                //本院率
                                comfrombl = (Convert.ToDouble(byformcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                                //第三方率
                                comfromsbl = (Convert.ToDouble(dsffromcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                            }
                            #region 改派量
                            changepcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                                        && p.createtime <= endtime
                                                                                       && p.isdelete == 0
                                                                                       && p.Pwcode == Jwocode).Result.ToString();//改派数量 
                            #endregion
                            #region 获取超时及时完成的单子数量
                            //获取完成的总单子信息
                            var finishlist = await _wo_repair_baseRepository.Get(p => p.createtime >= startime
                                                                         && p.createtime <= endtime
                                                                         && p.Jwocode == Jwocode && p.isdelete == 0
                                                                         && (p.wostate == 3 || p.wostate == 2));
                            //超时数量
                            int fcount = 0;
                            foreach (var itemfin in finishlist)
                            {
                                if (itemfin.wxtimeid != null || itemfin.wxtimeid != "")
                                {
                                    //根据id获取维修项目时效时间
                                    var wxxmtime = await _wxwpstimeRepository.GetSingle(p => p.id == itemfin.wxtimeid);
                                    if (wxxmtime != null)
                                    {
                                        if (itemfin.dealtime == null || itemfin.tobztimes == null)
                                        {
                                            continue;
                                        }
                                        DateTime dt1 = itemfin.dealtime != null ? Convert.ToDateTime(itemfin.dealtime) : Convert.ToDateTime(itemfin.dealtime);
                                        DateTime dt2 = itemfin.tobztimes != null ? Convert.ToDateTime(itemfin.tobztimes) : Convert.ToDateTime(itemfin.tobztimes);

                                        TimeSpan ts = dt1.Subtract(dt2);
                                        double hours = ts.TotalHours;//24.0

                                        if ((hours - wxxmtime.timecount) > 0)
                                        {
                                            fcount++;
                                        }

                                    }
                                }
                            }
                            ///超时数量
                            csfinishcount = fcount.ToString();
                            //及时完成数量
                            jsfinishcount = (Convert.ToInt32(finishcount) - fcount).ToString();
                            #endregion
                            #endregion

                        }
                        else
                        {
                            #region 时间为空时
                            #region 总接单量  完成量 为完成量
                            allcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0).Result.ToString();
                            finishcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0 && (p.wostate == 3 || p.wostate == 2)).Result.ToString();
                            nofinishcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0
                                                                            && (p.wostate == 0 || p.wostate == 1 || p.wostate == 7)).Result.ToString();
                            #endregion
                            #region 获取完结率
                            if (Convert.ToDouble(finishcount) == 0 || Convert.ToDouble(allcount) == 0)
                            {
                                finishlv = "0%";
                            }
                            else
                            {
                                double percent = Convert.ToDouble(finishcount) / Convert.ToDouble(allcount);
                                finishlv = percent.ToString("0.00%");//完结率
                            }
                            #endregion
                            #region 获取未完成率
                            if (Convert.ToDouble(nofinishcount) == 0 || Convert.ToDouble(allcount) == 0)
                            {
                                nofinnishlv = "0%";
                            }
                            else
                            {
                                double percent = Convert.ToDouble(nofinishcount) / Convert.ToDouble(allcount);
                                nofinnishlv = percent.ToString("0.00%");//完结率
                            }
                            #endregion
                            #region 满意度
                            //先获取所有满意的班组内的工单数量
                            //double percent = Convert.ToDouble(2) / Convert.ToDouble(34);
                            //string result = percent.ToString("0.000%");//得到5.882%
                            var allmywocodecount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode
                                                                          && p.isdelete == 0
                                                                          && p.wostate == 3
                                                                          && (p.score == 4 || p.score == 5 || p.score == 3)).Result.ToString();
                            var wfinishcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0
                                                             && p.wostate == 3).Result.ToString();
                            if (Convert.ToDouble(wfinishcount) == 0)
                            {
                                mydblcount = "0%";
                            }
                            else
                            {
                                double percent = Convert.ToDouble(allmywocodecount) / Convert.ToDouble(wfinishcount);
                                mydblcount = percent.ToString("0.00%");//满意度
                            }
                            #endregion
                            #region 本院量
                            byformcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode
                                                                                       && p.isdelete == 0
                                                                                       && (p.wostate == 3 || p.wostate == 2)
                                                                                       && (p.CLpeoplefrom == "本院" || p.CLpeoplefrom == "本部")).Result.ToString();//本院 
                            #endregion
                            #region 第三方
                            dsffromcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode
                                                                                       && p.isdelete == 0
                                                                                       && (p.wostate == 3 || p.wostate == 2)
                                                                                       && p.CLpeoplefrom == "第三方").Result.ToString();//第三方 
                            #endregion
                            var allnomywocodecounts = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode
                                                                    && p.isdelete == 0
                                                                    && p.wostate == 3
                                                                    && (p.score == 1 || p.score == 2)).Result.ToString();
                            //满意数
                            mywocode = allmywocodecount;
                            cwocode = allnomywocodecounts;
                            if (Convert.ToDouble(finishcount) > 0)
                            {
                                //本院率
                                comfrombl = (Convert.ToDouble(byformcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                                //第三方率
                                comfromsbl = (Convert.ToDouble(dsffromcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                            }
                            #region 改派量
                            changepcount = _wo_repair_baseRepository.Count(p => p.isdelete == 0
                                                                                       && p.Pwcode == Jwocode).Result.ToString();//改派数量 
                            #endregion
                            #region 获取超时及时完成的单子数量
                            //获取完成的总单子信息
                            var finishlist = await _wo_repair_baseRepository.Get(p => p.Jwocode == Jwocode && p.isdelete == 0 && (p.wostate == 3 || p.wostate == 2));
                            //超时数量
                            int fcount = 0;
                            foreach (var itemfin in finishlist)
                            {
                                if (itemfin.wxtimeid != null || itemfin.wxtimeid != "")
                                {
                                    //根据id获取维修项目时效时间
                                    var wxxmtime = await _wxwpstimeRepository.GetSingle(p => p.id == itemfin.wxtimeid);
                                    if (wxxmtime != null)
                                    {
                                        if (itemfin.dealtime == null || itemfin.tobztimes == null)
                                        {
                                            continue;
                                        }
                                        DateTime dt1 = itemfin.dealtime != null ? Convert.ToDateTime(itemfin.dealtime) : Convert.ToDateTime(itemfin.dealtime);
                                        DateTime dt2 = itemfin.tobztimes != null ? Convert.ToDateTime(itemfin.tobztimes) : Convert.ToDateTime(itemfin.tobztimes);

                                        TimeSpan ts = dt1.Subtract(dt2);
                                        double hours = ts.TotalHours;//24.0

                                        if ((hours - wxxmtime.timecount) > 0)
                                        {
                                            fcount++;
                                        }

                                    }
                                }
                            }
                            ///超时数量
                            csfinishcount = fcount.ToString();
                            //及时完成数量
                            jsfinishcount = (Convert.ToInt32(finishcount) - fcount).ToString();
                            #endregion
                            #endregion
                        }
                        #endregion
                        #region 赋值添加数据
                        DayTjlists daytjinfo = new DayTjlists();
                        daytjinfo.deptname = Jwocode;//接单班组
                        daytjinfo.daycounts = allcount;//总接单量
                        daytjinfo.finishcount = finishcount;//完结单量
                        daytjinfo.jsfinishcount = jsfinishcount; //及时完成
                        daytjinfo.csfinishcount = csfinishcount; //超时完成
                        daytjinfo.nfinishcount = nofinishcount;//未完结单量
                        daytjinfo.finishlv = finishlv;         //完成单率
                        daytjinfo.nofinnishlv = nofinnishlv;    //未完成率
                        daytjinfo.mywocode = mywocode;//满意
                        //daytjinfo.ybwocode = ybwocode;//一般
                        daytjinfo.cwocode = cwocode;//差
                        daytjinfo.mydinfo = mydblcount;//满意度
                        daytjinfo.comfrom = byformcount;//本院
                        daytjinfo.comfrombl = comfrombl;//本院率  
                        daytjinfo.comfroms = dsffromcount;//第三方
                        daytjinfo.comfromsbl = comfromsbl;//第三方率
                        daytjinfo.changecount = changepcount;//改派数量
                        list.Add(daytjinfo);
                        #endregion
                        #region 计算总数
                        lastallcount += Convert.ToInt32(allcount);
                        lastfinishcount += Convert.ToInt32(finishcount);
                        lastnofinishcount += Convert.ToInt32(nofinishcount);
                        #endregion
                    }
                    #endregion
                    #region 最后一行总数
                    DayTjlists daytjinfo1 = new DayTjlists();
                    daytjinfo1.deptname = "总数";//接单班组
                    daytjinfo1.daycounts = lastallcount.ToString();// allcount;//总接单量
                    daytjinfo1.finishcount = lastfinishcount.ToString();// finishcount;//完结单量
                    daytjinfo1.jsfinishcount = jsfinishcount; //及时完成
                    daytjinfo1.csfinishcount = csfinishcount; //超时完成
                    daytjinfo1.nfinishcount = lastnofinishcount.ToString();// nofinishcount;//未完结单量
                    daytjinfo1.finishlv = "";         //完成单率
                    daytjinfo1.nofinnishlv = "";    //未完成率
                    daytjinfo1.mywocode = mywocode;//满意
                    //daytjinfo1.ybwocode = ybwocode;//一般
                    daytjinfo1.cwocode = cwocode;//差
                    daytjinfo1.mydinfo = "";//满意度
                    daytjinfo1.comfrom = "";//本院
                    daytjinfo1.comfroms = "";//第三方
                    daytjinfo1.changecount = "";//改派数量
                    list.Add(daytjinfo1);
                    #endregion
                }
                #endregion
                #endregion
            }
            var obj = new
            {
                lists = list,
            };
            return Success("成功", obj);
        }
        /// <summary>
        /// 导出统计信息
        /// </summary>
        /// <param name="stime"></param>
        /// <param name="etime"></param>
        /// <param name="Jwocode"></param>
        /// <returns></returns>
       // [Authorize]
        [HttpGet("exportexcel")]
        [AllowAnonymous]
        public async Task<IActionResult> ExportExcelAsync(string stime, string etime, string Jwocode, string daytype, string isdjtype)
        {
            try
            {
                var datenow = DateTime.Now;
                DateTime? startime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
                DateTime? endtime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));
                if (!string.IsNullOrEmpty(daytype))
                {
                    switch (daytype)
                    {
                        case "week":
                            startime = Convert.ToDateTime(datenow.AddDays(1 - Convert.ToInt32(datenow.DayOfWeek.ToString("d"))).ToString("yyyy-MM-dd 00:00:00"));//本周周一
                            endtime = Convert.ToDateTime(datenow.AddDays(6).ToString("yyyy-MM-dd  23:59:59"));//本周周日
                            break;
                        case "month":
                            startime = Convert.ToDateTime(datenow.AddDays(1 - datenow.Day).ToString("yyyy-MM-dd 00:00:00"));  //本月月初  
                            endtime = Convert.ToDateTime(datenow.AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd 23:59:59"));  //本月月末  
                            break;
                        case "year":
                            startime = Convert.ToDateTime(new DateTime(datenow.Year, 1, 1).ToString("yyyy-MM-dd 00:00:00"));  //本年年初  
                            endtime = Convert.ToDateTime(new DateTime(datenow.Year, 12, 31).ToString("yyyy-MM-dd  23:59:59"));  //本年年末  
                            break;
                        default:
                            break;
                    }
                }
                else
                {
                    //先确认时间是否为空 空则用当前时间天来判断
                    if (!string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                    {

                        startime = DateTime.Parse(stime.Trim() + " 00:00:00");
                        endtime = DateTime.Parse(etime.Trim() + " 23:59:59");
                    }
                    else
                    {
                        startime = null;
                        endtime = null;
                    }

                }
                // wostate工单状态:0创建工单，1已转派（转派或者抢单），2已处理，3已评价，7挂起，8已取消，9已完成
                #region 定义字段
                var allcount = "";//总接单量
                var finishcount = "";//已完成的数量
                var finishlv = ""; /// 完结单率
                var jsfinishcount = "";//// 及时完成
                var csfinishcount = "";// 超时完成
                var nofinishcount = "";//未完成的数量
                var nofinnishlv = "";//未完成率
                var mywocode = "";//满意
                var ybwocode = "";//一般
                var cwocode = "";//差
                var mydblcount = "";//满意度 
                var comfrombl = "";//本院率
                var comfromsbl = "";//第三方率
                #endregion

                List<DayTjlists> list = new List<DayTjlists>();

                if (isdjtype == "静态")
                {
                    #region 静态
                    #region 获取list集合数据
                    if (Jwocode != null)
                    {
                        #region 班组不为空时
                        var byformcount = "";//本院
                        var dsffromcount = "";//第三方
                        var changepcount = "";//改派数量
                        #region 判断时间是否为空
                        if (startime != null && endtime != null)
                        {
                            #region 班组不为空  时间段不为空时
                            #region 总接单量   已完成数量   未完成数量  
                            allcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                                   && p.createtime <= endtime
                                                                                   && p.Jwocode == Jwocode && p.isdelete == 0).Result.ToString();
                            //finishcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                            //                                             && p.createtime <= endtime
                            //                                             && p.Jwocode == Jwocode && p.isdelete == 0
                            //                                             && (p.wostate == 3 || p.wostate == 2)).Result.ToString();
                            nofinishcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                         && p.createtime <= endtime
                                                                         && p.Jwocode == Jwocode && p.isdelete == 0
                                                                         && (p.wostate == 0 || p.wostate == 1 || p.wostate == 7)).Result.ToString();
                            #endregion

                            #region 静态数据汇总
                            //获取在这个时间段内完成的数据
                            var finishlists = await _wo_repair_baseRepository.Get(p => p.createtime >= startime
                                                                           && p.createtime <= endtime
                                                                           && p.Jwocode == Jwocode && p.isdelete == 0
                                                                           && (p.wostate == 3 || p.wostate == 2));
                            //获取在这个时间段内未完成的数据
                            var nofinishlists = await _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                          && p.createtime <= endtime
                                                                          && p.Jwocode == Jwocode && p.isdelete == 0
                                                                          && (p.wostate == 0 || p.wostate == 1 || p.wostate == 7));
                            //完成数量
                            int wccount = 0;
                            //未完成数量
                            int nowccount = 0;
                            //完成的单子中的满意数量
                            int allmywocodecounts = 0;
                            //不满意数量
                            int allnomywocodecounts = 0;
                            //获取完成单子中的本院量
                            int byformcounts = 0;
                            //获取完成单子中的第三方量
                            int dsffromcounts = 0;
                            //完结单量
                            int wfinishcounts = 0;
                            List<Wo_Repair_Base> finishlist = new List<Wo_Repair_Base>();
                            foreach (var item in finishlists)
                            {
                                if (item.dealtime >= startime && item.dealtime <= endtime)
                                {
                                    wccount++;
                                    if (item.score == 4 || item.score == 5 || item.score == 3)
                                    {
                                        allmywocodecounts++;
                                    }
                                    if (item.score == 1 || item.score == 2)
                                    {
                                        allnomywocodecounts++;
                                    }
                                    if (item.CLpeoplefrom == "第三方")
                                    {
                                        dsffromcounts++;
                                    }
                                    if (item.CLpeoplefrom == "本院" || item.CLpeoplefrom == "本部")
                                    {
                                        byformcounts++;
                                    }
                                    if (item.wostate == 3)
                                    {
                                        wfinishcounts++;
                                    }
                                    finishlist.Add(item);
                                }
                                else
                                {
                                    if (item.CLpeoplefrom == "第三方")
                                    {
                                        dsffromcounts++;
                                    }
                                    if (item.CLpeoplefrom == "本院" || item.CLpeoplefrom == "本部")
                                    {
                                        byformcounts++;
                                    }
                                    nowccount++;
                                }
                            }

                            #region 获取完结率

                            finishcount = wccount.ToString();
                            if (Convert.ToDouble(finishcount) == 0 || Convert.ToDouble(allcount) == 0)
                            {
                                finishlv = "0%";
                            }
                            else
                            {
                                double percent = Convert.ToDouble(finishcount) / Convert.ToDouble(allcount);
                                finishlv = percent.ToString("0.00%");//完结率
                            }
                            #endregion
                            #region 获取未完成率
                            nofinishcount = (Convert.ToInt32(nofinishcount) + nowccount).ToString();
                            if (Convert.ToDouble(nofinishcount) == 0 || Convert.ToDouble(allcount) == 0)
                            {
                                finishlv = "0%";
                            }
                            else
                            {
                                double percent = Convert.ToDouble(nofinishcount) / Convert.ToDouble(allcount);
                                nofinnishlv = percent.ToString("0.00%");//完结率
                            }
                            #endregion
                            #region 获取满意率
                            //var wfinishcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                            //                                             && p.createtime <= endtime
                            //                                             && p.Jwocode == Jwocode && p.isdelete == 0
                            //                                             && p.wostate == 3).Result.ToString();
                            var wfinishcount = wfinishcounts;
                            var allmywocodecount = allmywocodecounts.ToString();
                            if (Convert.ToDouble(wfinishcount) == 0 || allmywocodecounts == 0)
                            {
                                mydblcount = "0%";
                            }
                            else
                            {
                                double percent = Convert.ToDouble(allmywocodecount) / Convert.ToDouble(wfinishcount);
                                mydblcount = percent.ToString("0.00%");//满意度
                            }
                            #endregion
                            //满意数
                            mywocode = allmywocodecounts.ToString();
                            cwocode = allnomywocodecounts.ToString();
                            #region 本院量
                            //byformcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                            //                                                       && p.createtime <= endtime
                            //                                                      && p.Jwocode == Jwocode
                            //                                                      && p.isdelete == 0
                            //                                                      && (p.wostate == 3 || p.wostate == 2)
                            //                                                      && (p.CLpeoplefrom == "本院" || p.CLpeoplefrom == "本部")).Result.ToString();//本院 
                            byformcount = byformcounts.ToString();//本院
                            #endregion


                            #region 第三方
                            //dsffromcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                            //                                                       && p.createtime <= endtime
                            //                                                      && p.Jwocode == Jwocode
                            //                                                      && p.isdelete == 0
                            //                                                      && (p.wostate == 3 || p.wostate == 2)
                            //                                                      && p.CLpeoplefrom == "第三方").Result.ToString();//第三方 
                            dsffromcount = dsffromcounts.ToString();//第三方 
                            #endregion
                            if (Convert.ToDouble(finishcount) > 0)
                            {
                                //本院率
                                comfrombl = (Convert.ToDouble(byformcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                                //第三方率
                                comfromsbl = (Convert.ToDouble(dsffromcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                            }
                            #region 改派量
                            changepcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                                    && p.createtime <= endtime
                                                                                   && p.isdelete == 0
                                                                                   && p.Pwcode == Jwocode).Result.ToString();//改派数量 
                            #endregion
                            #region 获取超时及时完成的单子数量
                            //获取完成的总单子信息
                            //var finishlist = await _wo_repair_baseRepository.Get(p => p.createtime >= startime
                            //                                             && p.createtime <= endtime
                            //                                             && p.Jwocode == Jwocode && p.isdelete == 0
                            //                                             && (p.wostate == 3 || p.wostate == 2));
                            //超时数量
                            int fcount = 0;
                            foreach (var item in finishlist)
                            {
                                if (item.wxtimeid != null || item.wxtimeid != "")
                                {
                                    //根据id获取维修项目时效时间
                                    var wxxmtime = await _wxwpstimeRepository.GetSingle(p => p.id == item.wxtimeid);
                                    if (wxxmtime != null)
                                    {
                                        if (item.dealtime == null || item.tobztimes == null)
                                        {
                                            continue;
                                        }
                                        DateTime dt1 = item.dealtime != null ? Convert.ToDateTime(item.dealtime) : Convert.ToDateTime(item.dealtime);
                                        DateTime dt2 = item.tobztimes != null ? Convert.ToDateTime(item.tobztimes) : Convert.ToDateTime(item.tobztimes);

                                        TimeSpan ts = dt1.Subtract(dt2);
                                        double hours = ts.TotalHours;//24.0

                                        if ((hours - wxxmtime.timecount) > 0)
                                        {
                                            fcount++;
                                        }

                                    }
                                }
                            }
                            ///超时数量
                            csfinishcount = fcount.ToString();
                            //及时完成数量
                            jsfinishcount = (Convert.ToInt32(finishcount) - fcount).ToString();
                            #endregion
                            #endregion
                            #endregion
                        }
                        else
                        {
                            #region 班组为空  时间段为空
                            #region 总接单量  已完结单量  未完结单量
                            allcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0).Result.ToString();
                            //finishcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0 && (p.wostate == 3 || p.wostate == 2)).Result.ToString();
                            nofinishcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0
                                                                            && (p.wostate == 0 || p.wostate == 1 || p.wostate == 7)).Result.ToString();
                            #endregion

                            #region 静态数据汇总
                            //获取在这个时间段内完成的数据
                            var finishlists = await _wo_repair_baseRepository.Get(p => p.Jwocode == Jwocode && p.isdelete == 0 && (p.wostate == 3 || p.wostate == 2));
                            //获取在这个时间段内未完成的数据
                            var nofinishlists = await _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0
                                                                          && (p.wostate == 0 || p.wostate == 1 || p.wostate == 7));
                            //完成数量
                            int wccount = 0;
                            //未完成数量
                            int nowccount = 0;
                            //完成的单子中的满意数量
                            int allmywocodecounts = 0;
                            //不满意数量
                            int allnomywocodecounts = 0;
                            //获取完成单子中的本院量
                            int byformcounts = 0;
                            //获取完成单子中的第三方量
                            int dsffromcounts = 0;
                            //完结单量
                            int wfinishcounts = 0;
                            List<Wo_Repair_Base> finishlist = new List<Wo_Repair_Base>();
                            foreach (var item in finishlists)
                            {
                                if (item.dealtime >= startime && item.dealtime <= endtime)
                                {
                                    wccount++;
                                    if (item.score == 4 || item.score == 5 || item.score == 3)
                                    {
                                        allmywocodecounts++;
                                    }
                                    if (item.score == 1 || item.score == 2)
                                    {
                                        allnomywocodecounts++;
                                    }
                                    if (item.CLpeoplefrom == "第三方")
                                    {
                                        dsffromcounts++;
                                    }
                                    if (item.CLpeoplefrom == "本院" || item.CLpeoplefrom == "本部")
                                    {
                                        byformcounts++;
                                    }
                                    if (item.wostate == 3)
                                    {
                                        wfinishcounts++;
                                    }
                                    finishlist.Add(item);
                                }
                                else
                                {
                                    nowccount++;
                                    if (item.CLpeoplefrom == "第三方")
                                    {
                                        dsffromcounts++;
                                    }
                                    if (item.CLpeoplefrom == "本院" || item.CLpeoplefrom == "本部")
                                    {
                                        byformcounts++;
                                    }
                                }
                            }

                            #region 获取完结率
                            finishcount = wccount.ToString();
                            if (Convert.ToDouble(finishcount) == 0 || Convert.ToDouble(allcount) == 0)
                            {
                                finishlv = "0%";
                            }
                            else
                            {
                                double percent = Convert.ToDouble(finishcount) / Convert.ToDouble(allcount);
                                finishlv = percent.ToString("0.00%");//完结率
                            }
                            #endregion
                            #region 获取未完成率
                            nofinishcount = (Convert.ToInt32(nofinishcount) + nowccount).ToString();
                            if (Convert.ToDouble(nofinishcount) == 0 || Convert.ToDouble(allcount) == 0)
                            {
                                nofinnishlv = "0%";
                            }
                            else
                            {
                                double percent = Convert.ToDouble(nofinishcount) / Convert.ToDouble(allcount);
                                nofinnishlv = percent.ToString("0.00%");//完结率
                            }
                            #endregion
                            #region 获取满意率
                            var allmywocodecount = allmywocodecounts.ToString();
                            var wfinishcount = wfinishcounts;
                            if (Convert.ToDouble(wfinishcount) == 0 || allmywocodecounts == 0)
                            {
                                mydblcount = "0%";
                            }
                            else
                            {
                                double percent = Convert.ToDouble(allmywocodecount) / Convert.ToDouble(wfinishcount);
                                mydblcount = percent.ToString("0.00%");//满意度
                            }
                            #endregion
                            //满意数
                            mywocode = allmywocodecounts.ToString();
                            cwocode = allnomywocodecounts.ToString();
                            #region 本院量
                            byformcount = byformcounts.ToString();//本院
                            #endregion
                            #region 第三方
                            //dsffromcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                            //                                                       && p.createtime <= endtime
                            //                                                      && p.Jwocode == Jwocode
                            //                                                      && p.isdelete == 0
                            //                                                      && (p.wostate == 3 || p.wostate == 2)
                            //                                                      && p.CLpeoplefrom == "第三方").Result.ToString();//第三方 
                            dsffromcount = dsffromcounts.ToString();//第三方 
                            #endregion
                            if (Convert.ToDouble(finishcount) > 0)
                            {
                                //本院率
                                comfrombl = (Convert.ToDouble(byformcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                                //第三方率
                                comfromsbl = (Convert.ToDouble(dsffromcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                            }
                            #region 改派量
                            changepcount = _wo_repair_baseRepository.Count(p => p.isdelete == 0
                                                                                   && p.Pwcode == Jwocode).Result.ToString();//改派数量 
                            #endregion
                            #region 获取超时及时完成的单子数量
                            //超时数量
                            int fcount = 0;
                            foreach (var item in finishlist)
                            {
                                if (item.wxtimeid != null || item.wxtimeid != "")
                                {
                                    //根据id获取维修项目时效时间
                                    var wxxmtime = await _wxwpstimeRepository.GetSingle(p => p.id == item.wxtimeid);
                                    if (wxxmtime != null)
                                    {
                                        if (item.dealtime == null || item.tobztimes == null)
                                        {
                                            continue;
                                        }
                                        DateTime dt1 = item.dealtime != null ? Convert.ToDateTime(item.dealtime) : Convert.ToDateTime(item.dealtime);
                                        DateTime dt2 = item.tobztimes != null ? Convert.ToDateTime(item.tobztimes) : Convert.ToDateTime(item.tobztimes);

                                        TimeSpan ts = dt1.Subtract(dt2);
                                        double hours = ts.TotalHours;//24.0

                                        if ((hours - wxxmtime.timecount) > 0)
                                        {
                                            fcount++;
                                        }

                                    }
                                }
                            }
                            ///超时数量
                            csfinishcount = fcount.ToString();
                            //及时完成数量
                            jsfinishcount = (Convert.ToInt32(finishcount) - fcount).ToString();
                            #endregion
                            #endregion

                            #endregion
                        }
                        #endregion
                        #region 赋值填数据
                        DayTjlists daytjinfo = new DayTjlists();
                        daytjinfo.deptname = Jwocode;//接单班组
                        daytjinfo.daycounts = allcount;//总接单量
                        daytjinfo.finishcount = finishcount;//完结单量
                        daytjinfo.jsfinishcount = jsfinishcount; //及时完成
                        daytjinfo.csfinishcount = csfinishcount; //超时完成
                        daytjinfo.nfinishcount = nofinishcount;//未完结单量
                        daytjinfo.finishlv = finishlv;         //完成单率
                        daytjinfo.nofinnishlv = nofinnishlv;    //未完成率
                        daytjinfo.mywocode = mywocode;//满意
                                                      //daytjinfo.ybwocode = ybwocode;//一般
                        daytjinfo.cwocode = cwocode;//差
                        daytjinfo.mydinfo = mydblcount;//满意度
                        daytjinfo.comfrom = byformcount;//本院
                        daytjinfo.comfrombl = comfrombl;//本院率
                        daytjinfo.comfroms = dsffromcount;//第三方
                        daytjinfo.comfromsbl = comfromsbl;//第三方率
                        daytjinfo.changecount = changepcount;//改派数量
                        list.Add(daytjinfo);
                        #endregion
                        #endregion
                    }
                    else
                    {
                        #region 定义总数信息字段
                        int lastallcount = 0;
                        int lastfinishcount = 0;
                        int lastnofinishcount = 0;
                        #endregion
                        #region 获取所有班组信息
                        string Jwocdes = "";
                        //获取所有班组信息
                        var bzinfolist = await _ISys_Role_InfoRepository.Get(p => p.role_code == "YWYG" && p.role_name.Contains("班") && p.state_flag == 1);
                        var bzcount = 1;
                        foreach (var item in bzinfolist)
                        {
                            Jwocdes += item.role_name;
                            if (bzcount != bzinfolist.Count())
                            {
                                Jwocdes += ",";
                            }
                            bzcount++;
                        }
                        #endregion
                        #region 获取到所有班组后进行单个获取数据
                        string[] arrStr = Jwocdes.Split(',');
                        foreach (var item in arrStr)
                        {
                            var byformcount = "";//本院
                            var dsffromcount = "";//第三方
                            var changepcount = "";//改派数量
                                                  //传的参数中如果没有班组信息就用这些定义的全部显示
                            Jwocode = item;
                            #region 判断时间是否为空
                            if (startime != null && endtime != null)
                            {
                                #region 时间不为空时
                                #region 总接单量  已完成量  未完成量
                                allcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                                          && p.createtime <= endtime
                                                                                          && p.Jwocode == Jwocode && p.isdelete == 0).Result.ToString();
                                //finishcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                //                                             && p.createtime <= endtime
                                //                                             && p.Jwocode == Jwocode && p.isdelete == 0
                                //                                             && (p.wostate == 3 || p.wostate == 2)).Result.ToString();
                                nofinishcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                             && p.createtime <= endtime
                                                                             && p.Jwocode == Jwocode && p.isdelete == 0
                                                                             && (p.wostate == 0 || p.wostate == 1 || p.wostate == 7)).Result.ToString();
                                #endregion
                                #region 静态数据汇总
                                //获取在这个时间段内完成的数据
                                var finishlists = await _wo_repair_baseRepository.Get(p => p.createtime >= startime
                                                                               && p.createtime <= endtime
                                                                               && p.Jwocode == Jwocode && p.isdelete == 0
                                                                               && (p.wostate == 3 || p.wostate == 2));
                                //获取在这个时间段内未完成的数据
                                var nofinishlists = await _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                              && p.createtime <= endtime
                                                                              && p.Jwocode == Jwocode && p.isdelete == 0
                                                                              && (p.wostate == 0 || p.wostate == 1 || p.wostate == 7));
                                //完成数量
                                int wccount = 0;
                                //未完成数量
                                int nowccount = 0;
                                //完成的单子中的满意数量
                                int allmywocodecounts = 0;
                                //获取完成单子中的本院量
                                int byformcounts = 0;
                                //不满意数量
                                int allnomywocodecounts = 0;
                                //获取完成单子中的第三方量
                                int dsffromcounts = 0;
                                int wfinishcounts = 0;
                                List<Wo_Repair_Base> finishlist = new List<Wo_Repair_Base>();
                                foreach (var finitem in finishlists)
                                {
                                    if (finitem.dealtime >= startime && finitem.dealtime <= endtime)
                                    {
                                        wccount++;
                                        if (finitem.score == 4 || finitem.score == 5 || finitem.score == 3)
                                        {
                                            allmywocodecounts++;
                                        }
                                        if (finitem.score == 1 || finitem.score == 2)
                                        {
                                            allnomywocodecounts++;
                                        }
                                        if (finitem.CLpeoplefrom == "第三方")
                                        {
                                            dsffromcounts++;
                                        }
                                        if (finitem.CLpeoplefrom == "本院" || finitem.CLpeoplefrom == "本部")
                                        {
                                            byformcounts++;
                                        }
                                        if (finitem.wostate == 3)
                                        {
                                            wfinishcounts++;
                                        }
                                        finishlist.Add(finitem);
                                    }
                                    else
                                    {
                                        nowccount++;
                                        if (finitem.CLpeoplefrom == "第三方")
                                        {
                                            dsffromcounts++;
                                        }
                                        if (finitem.CLpeoplefrom == "本院" || finitem.CLpeoplefrom == "本部")
                                        {
                                            byformcounts++;
                                        }
                                    }
                                }

                                #region 获取完结率
                                finishcount = wccount.ToString();
                                if (Convert.ToDouble(finishcount) == 0 || Convert.ToDouble(allcount) == 0)
                                {
                                    finishlv = "0%";
                                }
                                else
                                {
                                    double percent = Convert.ToDouble(finishcount) / Convert.ToDouble(allcount);
                                    finishlv = percent.ToString("0.00%");//完结率
                                }
                                #endregion
                                #region 获取未完成率
                                nofinishcount = (Convert.ToInt32(nofinishcount) + nowccount).ToString();
                                if (Convert.ToDouble(nofinishcount) == 0 || Convert.ToDouble(allcount) == 0)
                                {
                                    nofinnishlv = "0%";
                                }
                                else
                                {
                                    double percent = Convert.ToDouble(nofinishcount) / Convert.ToDouble(allcount);
                                    nofinnishlv = percent.ToString("0.00%");//完结率
                                }
                                #endregion
                                #region 获取满意率
                                var allmywocodecount = allmywocodecounts.ToString();
                                var wfinishcount = wfinishcounts;
                                if (Convert.ToDouble(wfinishcount) == 0 || allmywocodecounts == 0)
                                {
                                    mydblcount = "0%";
                                }
                                else
                                {
                                    double percent = Convert.ToDouble(allmywocodecount) / Convert.ToDouble(wfinishcount);
                                    mydblcount = percent.ToString("0.00%");//满意度
                                }
                                #endregion
                                //满意数
                                mywocode = allmywocodecounts.ToString();
                                cwocode = allnomywocodecounts.ToString();
                                #region 本院量

                                byformcount = byformcounts.ToString();//本院
                                #endregion
                                #region 第三方
                                dsffromcount = dsffromcounts.ToString();//第三方 
                                #endregion
                                if (Convert.ToDouble(finishcount) > 0)
                                {
                                    //本院率
                                    comfrombl = (Convert.ToDouble(byformcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                                    //第三方率
                                    comfromsbl = (Convert.ToDouble(dsffromcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                                }
                                #region 改派量
                                changepcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                                        && p.createtime <= endtime
                                                                                       && p.isdelete == 0
                                                                                       && p.Pwcode == Jwocode).Result.ToString();//改派数量 
                                #endregion
                                #region 获取超时及时完成的单子数量
                                //超时数量
                                int fcount = 0;
                                foreach (var finitem in finishlist)
                                {
                                    if (finitem.wxtimeid != null || finitem.wxtimeid != "")
                                    {
                                        //根据id获取维修项目时效时间
                                        var wxxmtime = await _wxwpstimeRepository.GetSingle(p => p.id == finitem.wxtimeid);
                                        if (wxxmtime != null)
                                        {
                                            if (finitem.dealtime == null || finitem.tobztimes == null)
                                            {
                                                continue;
                                            }
                                            DateTime dt1 = finitem.dealtime != null ? Convert.ToDateTime(finitem.dealtime) : Convert.ToDateTime(finitem.dealtime);
                                            DateTime dt2 = finitem.tobztimes != null ? Convert.ToDateTime(finitem.tobztimes) : Convert.ToDateTime(finitem.tobztimes);

                                            TimeSpan ts = dt1.Subtract(dt2);
                                            double hours = ts.TotalHours;//24.0

                                            if ((hours - wxxmtime.timecount) > 0)
                                            {
                                                fcount++;
                                            }

                                        }
                                    }
                                }
                                ///超时数量
                                csfinishcount = fcount.ToString();
                                //及时完成数量
                                jsfinishcount = (Convert.ToInt32(finishcount) - fcount).ToString();
                                #endregion
                                #endregion
                                #endregion

                            }
                            else
                            {
                                #region 时间为空时
                                #region 总接单量  完成量 为完成量
                                allcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0).Result.ToString();

                                //finishcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0 && (p.wostate == 3 || p.wostate == 2)).Result.ToString();
                                nofinishcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0
                                                                                && (p.wostate == 0 || p.wostate == 1 || p.wostate == 7)).Result.ToString();
                                #endregion
                                #region 静态数据汇总
                                //获取在这个时间段内完成的数据
                                var finishlists = await _wo_repair_baseRepository.Get(p => p.Jwocode == Jwocode && p.isdelete == 0 && (p.wostate == 3 || p.wostate == 2));
                                //获取在这个时间段内未完成的数据
                                var nofinishlists = await _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0
                                                                              && (p.wostate == 0 || p.wostate == 1 || p.wostate == 7));
                                //完成数量
                                int wccount = 0;
                                //未完成数量
                                int nowccount = 0;
                                //完成的单子中的满意数量
                                int allmywocodecounts = 0;
                                //不满意数量
                                int allnomywocodecounts = 0;
                                //获取完成单子中的本院量
                                int byformcounts = 0;
                                //获取完成单子中的第三方量
                                int dsffromcounts = 0;
                                int wfinishcounts = 0;
                                List<Wo_Repair_Base> finishlist = new List<Wo_Repair_Base>();
                                foreach (var finiitem in finishlists)
                                {
                                    if (finiitem.dealtime >= startime && finiitem.dealtime <= endtime)
                                    {
                                        wccount++;
                                        if (finiitem.score == 4 || finiitem.score == 5 || finiitem.score == 3)
                                        {
                                            allmywocodecounts++;
                                        }
                                        if (finiitem.score == 1 || finiitem.score == 2)
                                        {
                                            allnomywocodecounts++;
                                        }
                                        if (finiitem.CLpeoplefrom == "第三方")
                                        {
                                            dsffromcounts++;
                                        }
                                        if (finiitem.CLpeoplefrom == "本院" || finiitem.CLpeoplefrom == "本部")
                                        {
                                            byformcounts++;
                                        }
                                        if (finiitem.wostate == 3)
                                        {
                                            wfinishcounts++;
                                        }
                                        finishlist.Add(finiitem);
                                    }
                                    else
                                    {
                                        nowccount++;
                                        if (finiitem.CLpeoplefrom == "第三方")
                                        {
                                            dsffromcounts++;
                                        }
                                        if (finiitem.CLpeoplefrom == "本院" || finiitem.CLpeoplefrom == "本部")
                                        {
                                            byformcounts++;
                                        }
                                    }
                                }

                                #region 获取完结率
                                finishcount = wccount.ToString();
                                if (Convert.ToDouble(finishcount) == 0 || Convert.ToDouble(allcount) == 0)
                                {
                                    finishlv = "0%";
                                }
                                else
                                {
                                    double percent = Convert.ToDouble(finishcount) / Convert.ToDouble(allcount);
                                    finishlv = percent.ToString("0.00%");//完结率
                                }
                                #endregion
                                #region 获取未完成率
                                nofinishcount = (Convert.ToInt32(nofinishcount) + nowccount).ToString();
                                if (Convert.ToDouble(nofinishcount) == 0 || Convert.ToDouble(allcount) == 0)
                                {
                                    nofinnishlv = "0%";
                                }
                                else
                                {
                                    double percent = Convert.ToDouble(nofinishcount) / Convert.ToDouble(allcount);
                                    nofinnishlv = percent.ToString("0.00%");//完结率
                                }
                                #endregion
                                #region 获取满意率
                                var allmywocodecount = allmywocodecounts.ToString();
                                var wfinishcount = wfinishcounts;
                                if (Convert.ToDouble(wfinishcount) == 0 || allmywocodecounts == 0)
                                {
                                    mydblcount = "0%";
                                }
                                else
                                {
                                    double percent = Convert.ToDouble(allmywocodecount) / Convert.ToDouble(wfinishcount);
                                    mydblcount = percent.ToString("0.00%");//满意度
                                }
                                #endregion
                                //满意数
                                mywocode = allmywocodecounts.ToString();
                                cwocode = allnomywocodecounts.ToString();
                                #region 本院量
                                //byformcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                //                                                       && p.createtime <= endtime
                                //                                                      && p.Jwocode == Jwocode
                                //                                                      && p.isdelete == 0
                                //                                                      && (p.wostate == 3 || p.wostate == 2)
                                //                                                      && (p.CLpeoplefrom == "本院" || p.CLpeoplefrom == "本部")).Result.ToString();//本院 
                                byformcount = byformcounts.ToString();//本院
                                #endregion
                                #region 第三方
                                //dsffromcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                //                                                       && p.createtime <= endtime
                                //                                                      && p.Jwocode == Jwocode
                                //                                                      && p.isdelete == 0
                                //                                                      && (p.wostate == 3 || p.wostate == 2)
                                //                                                      && p.CLpeoplefrom == "第三方").Result.ToString();//第三方 
                                dsffromcount = dsffromcounts.ToString();//第三方 
                                #endregion
                                if (Convert.ToDouble(finishcount) > 0)
                                {
                                    //本院率
                                    comfrombl = (Convert.ToDouble(byformcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                                    //第三方率
                                    comfromsbl = (Convert.ToDouble(dsffromcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                                }
                                #region 改派量
                                changepcount = _wo_repair_baseRepository.Count(p => p.isdelete == 0
                                                                                       && p.Pwcode == Jwocode).Result.ToString();//改派数量 
                                #endregion
                                #region 获取超时及时完成的单子数量
                                //超时数量
                                int fcount = 0;
                                foreach (var finiitem in finishlist)
                                {
                                    if (finiitem.wxtimeid != null || finiitem.wxtimeid != "")
                                    {
                                        //根据id获取维修项目时效时间
                                        var wxxmtime = await _wxwpstimeRepository.GetSingle(p => p.id == finiitem.wxtimeid);
                                        if (wxxmtime != null)
                                        {
                                            if (finiitem.dealtime == null || finiitem.tobztimes == null)
                                            {
                                                continue;
                                            }
                                            DateTime dt1 = finiitem.dealtime != null ? Convert.ToDateTime(finiitem.dealtime) : Convert.ToDateTime(finiitem.dealtime);
                                            DateTime dt2 = finiitem.tobztimes != null ? Convert.ToDateTime(finiitem.tobztimes) : Convert.ToDateTime(finiitem.tobztimes);

                                            TimeSpan ts = dt1.Subtract(dt2);
                                            double hours = ts.TotalHours;//24.0

                                            if ((hours - wxxmtime.timecount) > 0)
                                            {
                                                fcount++;
                                            }

                                        }
                                    }
                                }

                                ///超时数量
                                csfinishcount = fcount.ToString();
                                //及时完成数量
                                jsfinishcount = (Convert.ToInt32(finishcount) - fcount).ToString();
                                #endregion
                                #endregion
                                #endregion
                            }
                            #endregion
                            #region 赋值添加数据
                            DayTjlists daytjinfo = new DayTjlists();
                            daytjinfo.deptname = Jwocode;//接单班组
                            daytjinfo.daycounts = allcount;//总接单量
                            daytjinfo.finishcount = finishcount;//完结单量
                            daytjinfo.jsfinishcount = jsfinishcount; //及时完成
                            daytjinfo.csfinishcount = csfinishcount; //超时完成
                            daytjinfo.nfinishcount = nofinishcount;//未完结单量
                            daytjinfo.finishlv = finishlv;         //完成单率
                            daytjinfo.nofinnishlv = nofinnishlv;    //未完成率
                            daytjinfo.mywocode = mywocode;//满意
                                                          //daytjinfo.ybwocode = ybwocode;//一般
                            daytjinfo.cwocode = cwocode;//差
                            daytjinfo.mydinfo = mydblcount;//满意度
                            daytjinfo.comfrom = byformcount;//本院
                            daytjinfo.comfrombl = comfrombl;//本院率   daytjinfo.comfromsbl = comfromsbl;//第三方率
                            daytjinfo.comfroms = dsffromcount;//第三方
                            daytjinfo.comfromsbl = comfromsbl;//第三方率
                            daytjinfo.changecount = changepcount;//改派数量
                            list.Add(daytjinfo);
                            #endregion
                            #region 计算总数
                            lastallcount += Convert.ToInt32(allcount);
                            lastfinishcount += Convert.ToInt32(finishcount);
                            lastnofinishcount += Convert.ToInt32(nofinishcount);
                            #endregion
                        }
                        #endregion
                        #region 最后一行总数
                        DayTjlists daytjinfo1 = new DayTjlists();
                        daytjinfo1.deptname = "总数";//接单班组
                        daytjinfo1.daycounts = lastallcount.ToString();// allcount;//总接单量
                        daytjinfo1.finishcount = lastfinishcount.ToString();// finishcount;//完结单量
                        daytjinfo1.jsfinishcount = jsfinishcount; //及时完成
                        daytjinfo1.csfinishcount = csfinishcount; //超时完成
                        daytjinfo1.nfinishcount = lastnofinishcount.ToString();// nofinishcount;//未完结单量
                        daytjinfo1.finishlv = "";         //完成单率
                        daytjinfo1.nofinnishlv = "";    //未完成率
                        daytjinfo1.mywocode = mywocode;//满意
                                                       //daytjinfo1.ybwocode = ybwocode;//一般
                        daytjinfo1.cwocode = cwocode;//差
                        daytjinfo1.mydinfo = "";//满意度
                        daytjinfo1.comfrom = "";//本院
                        daytjinfo1.comfroms = "";//第三方
                        daytjinfo1.changecount = "";//改派数量
                        list.Add(daytjinfo1);
                        #endregion
                    }
                    #endregion
                    #endregion
                }
                else
                {
                    #region 动态
                    #region 获取list集合数据
                    if (Jwocode != null)
                    {
                        #region 班组不为空时
                        var byformcount = "";//本院
                        var dsffromcount = "";//第三方
                        var changepcount = "";//改派数量
                        #region 判断时间是否为空
                        if (startime != null && endtime != null)
                        {
                            #region 班组不为空  时间段不为空时
                            #region 总接单量   已完成数量   未完成数量  
                            allcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                                   && p.createtime <= endtime
                                                                                   && p.Jwocode == Jwocode && p.isdelete == 0).Result.ToString();
                            finishcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                         && p.createtime <= endtime
                                                                         && p.Jwocode == Jwocode && p.isdelete == 0
                                                                         && (p.wostate == 3 || p.wostate == 2)).Result.ToString();
                            nofinishcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                         && p.createtime <= endtime
                                                                         && p.Jwocode == Jwocode && p.isdelete == 0
                                                                         && (p.wostate == 0 || p.wostate == 1 || p.wostate == 7)).Result.ToString();
                            #endregion
                            #region 获取完结率
                            if (Convert.ToDouble(finishcount) == 0 || Convert.ToDouble(allcount) == 0)
                            {
                                finishlv = "0%";
                            }
                            else
                            {
                                double percent = Convert.ToDouble(finishcount) / Convert.ToDouble(allcount);
                                finishlv = percent.ToString("0.00%");//完结率
                            }
                            #endregion
                            #region 获取未完成率
                            if (Convert.ToDouble(nofinishcount) == 0 || Convert.ToDouble(allcount) == 0)
                            {
                                nofinnishlv = "0%";
                            }
                            else
                            {
                                double percent = Convert.ToDouble(nofinishcount) / Convert.ToDouble(allcount);
                                nofinnishlv = percent.ToString("0.00%");//完结率
                            }
                            #endregion
                            #region 获取满意率
                            //先获取所有满意的班组内的工单数量
                            //double percent = Convert.ToDouble(2) / Convert.ToDouble(34);
                            //string result = percent.ToString("0.000%");//得到5.882%
                            var allmywocodecount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                         && p.createtime <= endtime
                                                                          && p.Jwocode == Jwocode
                                                                          && p.isdelete == 0
                                                                          && p.wostate == 3
                                                                          && (p.score == 4 || p.score == 5 || p.score == 3)).Result.ToString();
                            var wfinishcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                  && p.createtime <= endtime
                                                                  && p.Jwocode == Jwocode && p.isdelete == 0
                                                                  && p.wostate == 3).Result.ToString();
                            if (Convert.ToDouble(wfinishcount) == 0)
                            {
                                mydblcount = "0.00%";
                            }
                            else
                            {
                                double percent = Convert.ToDouble(allmywocodecount) / Convert.ToDouble(wfinishcount);
                                mydblcount = percent.ToString("0.00%");//满意度
                            }

                            #endregion
                            #region 本院量
                            byformcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                                   && p.createtime <= endtime
                                                                                  && p.Jwocode == Jwocode
                                                                                  && p.isdelete == 0
                                                                                  && (p.wostate == 3 || p.wostate == 2)
                                                                                  && (p.CLpeoplefrom == "本院" || p.CLpeoplefrom == "本部")).Result.ToString();//本院 
                            #endregion
                            #region 第三方
                            dsffromcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                                   && p.createtime <= endtime
                                                                                  && p.Jwocode == Jwocode
                                                                                  && p.isdelete == 0
                                                                                  && (p.wostate == 3 || p.wostate == 2)
                                                                                  && p.CLpeoplefrom == "第三方").Result.ToString();//第三方 
                            #endregion
                            var allnomywocodecounts = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                         && p.createtime <= endtime
                                                                          && p.Jwocode == Jwocode
                                                                          && p.isdelete == 0
                                                                          && p.wostate == 3
                                                                          && (p.score == 1 || p.score == 2)).Result.ToString();
                            //满意数
                            mywocode = allmywocodecount;
                            cwocode = allnomywocodecounts;
                            if (Convert.ToDouble(finishcount) > 0)
                            {
                                //本院率
                                comfrombl = (Convert.ToDouble(byformcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                                //第三方率
                                comfromsbl = (Convert.ToDouble(dsffromcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                            }
                            #region 改派量
                            changepcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                                    && p.createtime <= endtime
                                                                                   && p.isdelete == 0
                                                                                   && p.Pwcode == Jwocode).Result.ToString();//改派数量 
                            #endregion
                            #region 获取超时及时完成的单子数量
                            //获取完成的总单子信息
                            var finishlist = await _wo_repair_baseRepository.Get(p => p.createtime >= startime
                                                                         && p.createtime <= endtime
                                                                         && p.Jwocode == Jwocode && p.isdelete == 0
                                                                         && (p.wostate == 3 || p.wostate == 2));
                            //超时数量
                            int fcount = 0;
                            foreach (var item in finishlist)
                            {
                                if (item.wxtimeid != null || item.wxtimeid != "")
                                {
                                    //根据id获取维修项目时效时间
                                    var wxxmtime = await _wxwpstimeRepository.GetSingle(p => p.id == item.wxtimeid);
                                    if (wxxmtime != null)
                                    {
                                        if (item.dealtime == null || item.tobztimes == null)
                                        {
                                            continue;
                                        }
                                        DateTime dt1 = item.dealtime != null ? Convert.ToDateTime(item.dealtime) : Convert.ToDateTime(item.dealtime);
                                        DateTime dt2 = item.tobztimes != null ? Convert.ToDateTime(item.tobztimes) : Convert.ToDateTime(item.tobztimes);

                                        TimeSpan ts = dt1.Subtract(dt2);
                                        double hours = ts.TotalHours;//24.0

                                        if ((hours - wxxmtime.timecount) > 0)
                                        {
                                            fcount++;
                                        }

                                    }
                                }
                            }
                            ///超时数量
                            csfinishcount = fcount.ToString();
                            //及时完成数量
                            jsfinishcount = (Convert.ToInt32(finishcount) - fcount).ToString();
                            #endregion
                            #endregion
                        }
                        else
                        {
                            #region 班组为空  时间段为空
                            #region 总接单量  已完结单量  未完结单量
                            allcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0).Result.ToString();
                            finishcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0 && (p.wostate == 3 || p.wostate == 2)).Result.ToString();
                            nofinishcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0
                                                                            && (p.wostate == 0 || p.wostate == 1 || p.wostate == 7)).Result.ToString();
                            #endregion
                            #region 获取完结率
                            if (Convert.ToDouble(finishcount) == 0 || Convert.ToDouble(allcount) == 0)
                            {
                                finishlv = "0%";
                            }
                            else
                            {
                                double percent = Convert.ToDouble(finishcount) / Convert.ToDouble(allcount);
                                finishlv = percent.ToString("0.00%");//完结率
                            }
                            #endregion
                            #region 获取未完成率
                            if (Convert.ToDouble(nofinishcount) == 0 || Convert.ToDouble(allcount) == 0)
                            {
                                nofinnishlv = "0%";
                            }
                            else
                            {
                                double percent = Convert.ToDouble(nofinishcount) / Convert.ToDouble(allcount);
                                nofinnishlv = percent.ToString("0.00%");//完结率
                            }
                            #endregion
                            #region 满意度
                            //先获取所有满意的班组内的工单数量
                            //double percent = Convert.ToDouble(2) / Convert.ToDouble(34);
                            //string result = percent.ToString("0.000%");//得到5.882%
                            var allmywocodecount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode
                                                                          && p.isdelete == 0
                                                                          && p.wostate == 3
                                                                          && (p.score == 4 || p.score == 5 || p.score == 3)).Result.ToString();
                            var wfinishcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0
                                                                 && p.wostate == 3).Result.ToString();
                            if (Convert.ToDouble(wfinishcount) == 0)
                            {
                                mydblcount = "0%";
                            }
                            else
                            {
                                double percent = Convert.ToDouble(allmywocodecount) / Convert.ToDouble(wfinishcount);
                                mydblcount = percent.ToString("0.00%");//满意度
                            }
                            #endregion
                            #region 本院量
                            byformcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode
                                                                                   && p.isdelete == 0
                                                                                   && (p.wostate == 3 || p.wostate == 2)
                                                                                   && (p.CLpeoplefrom == "本院" || p.CLpeoplefrom == "本部")).Result.ToString();//本院 
                            #endregion
                            #region 第三方量
                            dsffromcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode
                                                                                   && p.isdelete == 0
                                                                                   && (p.wostate == 3 || p.wostate == 2)
                                                                                   && p.CLpeoplefrom == "第三方").Result.ToString();//第三方 
                            #endregion
                            var allnomywocodecounts = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode
                                                                         && p.isdelete == 0
                                                                         && p.wostate == 3
                                                                         && (p.score == 1 || p.score == 2)).Result.ToString();
                            //满意数
                            mywocode = allmywocodecount;
                            cwocode = allnomywocodecounts;
                            if (Convert.ToDouble(finishcount) > 0)
                            {
                                //本院率
                                comfrombl = (Convert.ToDouble(byformcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                                //第三方率
                                comfromsbl = (Convert.ToDouble(dsffromcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                            }
                            #region 改派量
                            changepcount = _wo_repair_baseRepository.Count(p => p.isdelete == 0
                                                                                   && p.Pwcode == Jwocode).Result.ToString();//改派数量 
                            #endregion
                            #region 获取超时及时完成的单子数量
                            //获取完成的总单子信息
                            var finishlist = await _wo_repair_baseRepository.Get(p => p.Jwocode == Jwocode && p.isdelete == 0 && (p.wostate == 3 || p.wostate == 2));
                            //超时数量
                            int fcount = 0;
                            foreach (var item in finishlist)
                            {
                                if (item.wxtimeid != null || item.wxtimeid != "")
                                {
                                    if (item.dealtime == null || item.tobztimes == null)
                                    {
                                        continue;
                                    }
                                    //根据id获取维修项目时效时间
                                    var wxxmtime = await _wxwpstimeRepository.GetSingle(p => p.id == item.wxtimeid);
                                    if (wxxmtime != null)
                                    {
                                        DateTime dt1 = item.dealtime != null ? Convert.ToDateTime(item.dealtime) : Convert.ToDateTime(item.dealtime);
                                        DateTime dt2 = item.tobztimes != null ? Convert.ToDateTime(item.tobztimes) : Convert.ToDateTime(item.tobztimes);

                                        TimeSpan ts = dt1.Subtract(dt2);
                                        double hours = ts.TotalHours;//24.0

                                        if ((hours - wxxmtime.timecount) > 0)
                                        {
                                            fcount++;
                                        }

                                    }
                                }
                            }
                            ///超时数量
                            csfinishcount = fcount.ToString();
                            //及时完成数量
                            jsfinishcount = (Convert.ToInt32(finishcount) - fcount).ToString();
                            #endregion
                            #endregion
                        }
                        #endregion
                        #region 赋值填数据
                        DayTjlists daytjinfo = new DayTjlists();
                        daytjinfo.deptname = Jwocode;//接单班组
                        daytjinfo.daycounts = allcount;//总接单量
                        daytjinfo.finishcount = finishcount;//完结单量
                        daytjinfo.jsfinishcount = jsfinishcount; //及时完成
                        daytjinfo.csfinishcount = csfinishcount; //超时完成
                        daytjinfo.nfinishcount = nofinishcount;//未完结单量
                        daytjinfo.finishlv = finishlv;         //完成单率
                        daytjinfo.nofinnishlv = nofinnishlv;    //未完成率
                        daytjinfo.mywocode = mywocode;//满意
                                                      //daytjinfo.ybwocode = ybwocode;//一般
                        daytjinfo.cwocode = cwocode;//差
                        daytjinfo.mydinfo = mydblcount;//满意度
                        daytjinfo.comfrom = byformcount;//本院
                        daytjinfo.comfrombl = comfrombl;//本院率  
                        daytjinfo.comfroms = dsffromcount;//第三方
                        daytjinfo.comfromsbl = comfromsbl;//第三方率
                        daytjinfo.changecount = changepcount;//改派数量
                        list.Add(daytjinfo);
                        #endregion
                        #endregion
                    }
                    else
                    {
                        #region 定义总数信息字段
                        int lastallcount = 0;
                        int lastfinishcount = 0;
                        int lastnofinishcount = 0;
                        #endregion
                        #region 获取所有班组信息
                        string Jwocdes = "";
                        //获取所有班组信息
                        var bzinfolist = await _ISys_Role_InfoRepository.Get(p => p.role_code == "YWYG" && p.role_name.Contains("班") && p.state_flag == 1);
                        var bzcount = 1;
                        foreach (var item in bzinfolist)
                        {
                            Jwocdes += item.role_name;
                            if (bzcount != bzinfolist.Count())
                            {
                                Jwocdes += ",";
                            }
                            bzcount++;
                        }
                        #endregion
                        #region 获取到所有班组后进行单个获取数据
                        string[] arrStr = Jwocdes.Split(',');
                        foreach (var item in arrStr)
                        {
                            var byformcount = "";//本院
                            var dsffromcount = "";//第三方
                            var changepcount = "";//改派数量
                                                  //传的参数中如果没有班组信息就用这些定义的全部显示
                            Jwocode = item;
                            #region 判断时间是否为空
                            if (startime != null && endtime != null)
                            {
                                #region 时间不为空时
                                #region 总接单量  已完成量  未完成量
                                allcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                                          && p.createtime <= endtime
                                                                                          && p.Jwocode == Jwocode && p.isdelete == 0).Result.ToString();
                                finishcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                             && p.createtime <= endtime
                                                                             && p.Jwocode == Jwocode && p.isdelete == 0
                                                                             && (p.wostate == 3 || p.wostate == 2)).Result.ToString();
                                nofinishcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                             && p.createtime <= endtime
                                                                             && p.Jwocode == Jwocode && p.isdelete == 0
                                                                             && (p.wostate == 0 || p.wostate == 1 || p.wostate == 7)).Result.ToString();
                                #endregion
                                #region 获取完结率
                                if (Convert.ToDouble(finishcount) == 0 || Convert.ToDouble(allcount) == 0)
                                {
                                    finishlv = "0%";
                                }
                                else
                                {
                                    double percent = Convert.ToDouble(finishcount) / Convert.ToDouble(allcount);
                                    finishlv = percent.ToString("0.00%");//完结率
                                }
                                #endregion
                                #region 获取未完成率
                                if (Convert.ToDouble(nofinishcount) == 0 || Convert.ToDouble(allcount) == 0)
                                {
                                    nofinnishlv = "0%";
                                }
                                else
                                {
                                    double percent = Convert.ToDouble(nofinishcount) / Convert.ToDouble(allcount);
                                    nofinnishlv = percent.ToString("0.00%");//完结率
                                }
                                #endregion
                                #region 满意度
                                //先获取所有满意的班组内的工单数量
                                //double percent = Convert.ToDouble(2) / Convert.ToDouble(34);
                                //string result = percent.ToString("0.000%");//得到5.882%
                                var allmywocodecount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                             && p.createtime <= endtime
                                                                              && p.Jwocode == Jwocode
                                                                              && p.isdelete == 0
                                                                              && p.wostate == 3
                                                                              && (p.score == 4 || p.score == 5 || p.score == 3)).Result.ToString();
                                var wfinishcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                 && p.createtime <= endtime
                                                                 && p.Jwocode == Jwocode && p.isdelete == 0
                                                                 && p.wostate == 3).Result.ToString();
                                if (Convert.ToDouble(wfinishcount) == 0)
                                {
                                    mydblcount = "0%";
                                }
                                else
                                {
                                    double percent = Convert.ToDouble(allmywocodecount) / Convert.ToDouble(wfinishcount);
                                    mydblcount = percent.ToString("0.00%");//满意度
                                }
                                #endregion
                                #region 本院量
                                byformcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                                           && p.createtime <= endtime
                                                                                          && p.Jwocode == Jwocode
                                                                                          && p.isdelete == 0
                                                                                          && (p.wostate == 3 || p.wostate == 2)
                                                                                          && (p.CLpeoplefrom == "本院" || p.CLpeoplefrom == "本部")).Result.ToString();//本院 
                                #endregion
                                #region 第三方量
                                dsffromcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                                            && p.createtime <= endtime
                                                                                           && p.Jwocode == Jwocode
                                                                                           && p.isdelete == 0
                                                                                           && (p.wostate == 3 || p.wostate == 2)
                                                                                           && p.CLpeoplefrom == "第三方").Result.ToString();//第三方 
                                #endregion
                                var allnomywocodecounts = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                       && p.createtime <= endtime
                                                                        && p.Jwocode == Jwocode
                                                                        && p.isdelete == 0
                                                                        && p.wostate == 3
                                                                        && (p.score == 1 || p.score == 2)).Result.ToString();
                                //满意数
                                mywocode = allmywocodecount;
                                cwocode = allnomywocodecounts;
                                if (Convert.ToDouble(finishcount) > 0)
                                {
                                    //本院率
                                    comfrombl = (Convert.ToDouble(byformcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                                    //第三方率
                                    comfromsbl = (Convert.ToDouble(dsffromcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                                }
                                #region 改派量
                                changepcount = _wo_repair_baseRepository.Count(p => p.createtime >= startime
                                                                                            && p.createtime <= endtime
                                                                                           && p.isdelete == 0
                                                                                           && p.Pwcode == Jwocode).Result.ToString();//改派数量 
                                #endregion
                                #region 获取超时及时完成的单子数量
                                //获取完成的总单子信息
                                var finishlist = await _wo_repair_baseRepository.Get(p => p.createtime >= startime
                                                                             && p.createtime <= endtime
                                                                             && p.Jwocode == Jwocode && p.isdelete == 0
                                                                             && (p.wostate == 3 || p.wostate == 2));
                                //超时数量
                                int fcount = 0;
                                foreach (var itemfin in finishlist)
                                {
                                    if (itemfin.wxtimeid != null || itemfin.wxtimeid != "")
                                    {
                                        //根据id获取维修项目时效时间
                                        var wxxmtime = await _wxwpstimeRepository.GetSingle(p => p.id == itemfin.wxtimeid);
                                        if (wxxmtime != null)
                                        {
                                            if (itemfin.dealtime == null || itemfin.tobztimes == null)
                                            {
                                                continue;
                                            }
                                            DateTime dt1 = itemfin.dealtime != null ? Convert.ToDateTime(itemfin.dealtime) : Convert.ToDateTime(itemfin.dealtime);
                                            DateTime dt2 = itemfin.tobztimes != null ? Convert.ToDateTime(itemfin.tobztimes) : Convert.ToDateTime(itemfin.tobztimes);

                                            TimeSpan ts = dt1.Subtract(dt2);
                                            double hours = ts.TotalHours;//24.0

                                            if ((hours - wxxmtime.timecount) > 0)
                                            {
                                                fcount++;
                                            }

                                        }
                                    }
                                }
                                ///超时数量
                                csfinishcount = fcount.ToString();
                                //及时完成数量
                                jsfinishcount = (Convert.ToInt32(finishcount) - fcount).ToString();
                                #endregion
                                #endregion

                            }
                            else
                            {
                                #region 时间为空时
                                #region 总接单量  完成量 为完成量
                                allcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0).Result.ToString();
                                finishcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0 && (p.wostate == 3 || p.wostate == 2)).Result.ToString();
                                nofinishcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0
                                                                                && (p.wostate == 0 || p.wostate == 1 || p.wostate == 7)).Result.ToString();
                                #endregion
                                #region 获取完结率
                                if (Convert.ToDouble(finishcount) == 0 || Convert.ToDouble(allcount) == 0)
                                {
                                    finishlv = "0%";
                                }
                                else
                                {
                                    double percent = Convert.ToDouble(finishcount) / Convert.ToDouble(allcount);
                                    finishlv = percent.ToString("0.00%");//完结率
                                }
                                #endregion
                                #region 获取未完成率
                                if (Convert.ToDouble(nofinishcount) == 0 || Convert.ToDouble(allcount) == 0)
                                {
                                    nofinnishlv = "0%";
                                }
                                else
                                {
                                    double percent = Convert.ToDouble(nofinishcount) / Convert.ToDouble(allcount);
                                    nofinnishlv = percent.ToString("0.00%");//完结率
                                }
                                #endregion
                                #region 满意度
                                //先获取所有满意的班组内的工单数量
                                //double percent = Convert.ToDouble(2) / Convert.ToDouble(34);
                                //string result = percent.ToString("0.000%");//得到5.882%
                                var allmywocodecount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode
                                                                              && p.isdelete == 0
                                                                              && p.wostate == 3
                                                                              && (p.score == 4 || p.score == 5 || p.score == 3)).Result.ToString();
                                var wfinishcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode && p.isdelete == 0
                                                                 && p.wostate == 3).Result.ToString();
                                if (Convert.ToDouble(wfinishcount) == 0)
                                {
                                    mydblcount = "0%";
                                }
                                else
                                {
                                    double percent = Convert.ToDouble(allmywocodecount) / Convert.ToDouble(wfinishcount);
                                    mydblcount = percent.ToString("0.00%");//满意度
                                }
                                #endregion
                                #region 本院量
                                byformcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode
                                                                                           && p.isdelete == 0
                                                                                           && (p.wostate == 3 || p.wostate == 2)
                                                                                           && (p.CLpeoplefrom == "本院" || p.CLpeoplefrom == "本部")).Result.ToString();//本院 
                                #endregion
                                #region 第三方
                                dsffromcount = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode
                                                                                           && p.isdelete == 0
                                                                                           && (p.wostate == 3 || p.wostate == 2)
                                                                                           && p.CLpeoplefrom == "第三方").Result.ToString();//第三方 
                                #endregion
                                var allnomywocodecounts = _wo_repair_baseRepository.Count(p => p.Jwocode == Jwocode
                                                                        && p.isdelete == 0
                                                                        && p.wostate == 3
                                                                        && (p.score == 1 || p.score == 2)).Result.ToString();
                                //满意数
                                mywocode = allmywocodecount;
                                cwocode = allnomywocodecounts;
                                if (Convert.ToDouble(finishcount) > 0)
                                {
                                    //本院率
                                    comfrombl = (Convert.ToDouble(byformcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                                    //第三方率
                                    comfromsbl = (Convert.ToDouble(dsffromcount) / Convert.ToDouble(finishcount)).ToString("0.00%");
                                }
                                #region 改派量
                                changepcount = _wo_repair_baseRepository.Count(p => p.isdelete == 0
                                                                                           && p.Pwcode == Jwocode).Result.ToString();//改派数量 
                                #endregion
                                #region 获取超时及时完成的单子数量
                                //获取完成的总单子信息
                                var finishlist = await _wo_repair_baseRepository.Get(p => p.Jwocode == Jwocode && p.isdelete == 0 && (p.wostate == 3 || p.wostate == 2));
                                //超时数量
                                int fcount = 0;
                                foreach (var itemfin in finishlist)
                                {
                                    if (itemfin.wxtimeid != null || itemfin.wxtimeid != "")
                                    {
                                        //根据id获取维修项目时效时间
                                        var wxxmtime = await _wxwpstimeRepository.GetSingle(p => p.id == itemfin.wxtimeid);
                                        if (wxxmtime != null)
                                        {
                                            if (itemfin.dealtime == null || itemfin.tobztimes == null)
                                            {
                                                continue;
                                            }
                                            DateTime dt1 = itemfin.dealtime != null ? Convert.ToDateTime(itemfin.dealtime) : Convert.ToDateTime(itemfin.dealtime);
                                            DateTime dt2 = itemfin.tobztimes != null ? Convert.ToDateTime(itemfin.tobztimes) : Convert.ToDateTime(itemfin.tobztimes);

                                            TimeSpan ts = dt1.Subtract(dt2);
                                            double hours = ts.TotalHours;//24.0

                                            if ((hours - wxxmtime.timecount) > 0)
                                            {
                                                fcount++;
                                            }

                                        }
                                    }
                                }
                                ///超时数量
                                csfinishcount = fcount.ToString();
                                //及时完成数量
                                jsfinishcount = (Convert.ToInt32(finishcount) - fcount).ToString();
                                #endregion
                                #endregion
                            }
                            #endregion
                            #region 赋值添加数据
                            DayTjlists daytjinfo = new DayTjlists();
                            daytjinfo.deptname = Jwocode;//接单班组
                            daytjinfo.daycounts = allcount;//总接单量
                            daytjinfo.finishcount = finishcount;//完结单量
                            daytjinfo.jsfinishcount = jsfinishcount; //及时完成
                            daytjinfo.csfinishcount = csfinishcount; //超时完成
                            daytjinfo.nfinishcount = nofinishcount;//未完结单量
                            daytjinfo.finishlv = finishlv;         //完成单率
                            daytjinfo.nofinnishlv = nofinnishlv;    //未完成率
                            daytjinfo.mywocode = mywocode;//满意
                                                          //daytjinfo.ybwocode = ybwocode;//一般
                            daytjinfo.cwocode = cwocode;//差
                            daytjinfo.mydinfo = mydblcount;//满意度
                            daytjinfo.comfrom = byformcount;//本院
                            daytjinfo.comfrombl = comfrombl;//本院率  
                            daytjinfo.comfroms = dsffromcount;//第三方
                            daytjinfo.comfromsbl = comfromsbl;//第三方率
                            daytjinfo.changecount = changepcount;//改派数量
                            list.Add(daytjinfo);
                            #endregion
                            #region 计算总数
                            lastallcount += Convert.ToInt32(allcount);
                            lastfinishcount += Convert.ToInt32(finishcount);
                            lastnofinishcount += Convert.ToInt32(nofinishcount);
                            #endregion
                        }
                        #endregion
                        #region 最后一行总数
                        DayTjlists daytjinfo1 = new DayTjlists();
                        daytjinfo1.deptname = "总数";//接单班组
                        daytjinfo1.daycounts = lastallcount.ToString();// allcount;//总接单量
                        daytjinfo1.finishcount = lastfinishcount.ToString();// finishcount;//完结单量
                        daytjinfo1.jsfinishcount = jsfinishcount; //及时完成
                        daytjinfo1.csfinishcount = csfinishcount; //超时完成
                        daytjinfo1.nfinishcount = lastnofinishcount.ToString();// nofinishcount;//未完结单量
                        daytjinfo1.finishlv = "";         //完成单率
                        daytjinfo1.nofinnishlv = "";    //未完成率
                        daytjinfo1.mywocode = mywocode;//满意
                                                       //daytjinfo1.ybwocode = ybwocode;//一般
                        daytjinfo1.cwocode = cwocode;//差
                        daytjinfo1.mydinfo = "";//满意度
                        daytjinfo1.comfrom = "";//本院
                        daytjinfo1.comfroms = "";//第三方
                        daytjinfo1.changecount = "";//改派数量
                        list.Add(daytjinfo1);
                        #endregion
                    }
                    #endregion
                    #endregion
                }


                NPOIHelper npoi = new NPOIHelper();
                var tb = ModelConvertHelper<DayTjlists>.ConvertListToDataTable(list);
                if (tb != null)
                {
                    //导出dtnew 
                    string[] cols = { "班组", "总接单量", "已完结量", "及时完成量", "超时完成量", "未完结量", "完成单率", "未完成率", "满意", "差", "满意度", "本院量", "本院率", "第三方量", "第三方率", "改派量" };

                    byte[] sm = npoi.ExportToExcel1(tb, cols);
                    if (sm != null)
                    {
                        return File(sm, "application/vnd.ms-excel", "后勤维修工单统计.xlsx");
                    }
                    else
                    {
                        return Error("导出失败");
                    }
                }
                return Error("导出失败");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "导出异常");
                return Error("导出失败");
            }
        }
        #endregion
        #region 工单操作接口
        /// <summary>
        /// 获取班组得到的所有
        /// </summary>
        /// <returns></returns>
        [HttpGet("getjwcodelist")]
        public async Task<IActionResult> Getjwcodelist(string stime, string etime, string Jwocode, int pageindex = 1, int pagesize = 10)
        {
            //排序字段
            var sort = Builders<Wo_Repair_Base>.Sort.Descending("createtime");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<Wo_Repair_Base>>();
            listApp.Add(Builders<Wo_Repair_Base>.Filter.Eq("isdelete", 0));
            if (!string.IsNullOrWhiteSpace(stime))
            {
                listApp.Add(Builders<Wo_Repair_Base>.Filter.Gt("createtime", DateTime.Parse(stime.Trim() + " 00:00:00")));
            }
            if (!string.IsNullOrWhiteSpace(etime))
            {
                listApp.Add(Builders<Wo_Repair_Base>.Filter.Lt("createtime", DateTime.Parse(etime.Trim() + " 23:59:59")));
            }
            if (!string.IsNullOrWhiteSpace(Jwocode))
            {
                listApp.Add(Builders<Wo_Repair_Base>.Filter.Lt("Jwocode", Jwocode));
            }

            var filter = Builders<Wo_Repair_Base>.Filter.And(listApp);

            var list = await _wo_repair_baseRepository.GetByPage(filter, pageindex, pagesize, sort);
            var redCount = await _wo_repair_baseRepository.CountAsync(filter);   //获取总数
            var obj = new
            {
                rows = list,
                total = redCount
            };
            return Success("成功", obj);
        }

        /// <summary>
        /// 导出班组数据
        /// </summary>
        /// <param name="stime"></param>
        /// <param name="etime"></param>
        /// <param name="Jwocode"></param>
        /// <returns></returns>
        [Authorize]
        [HttpGet("exportexcelwcode")]
        [AllowAnonymous]
        public async Task<IActionResult> ExportExcelAsyncwcode(string stime, string etime, string Jwocode, int pageindex = 1, int pagesize = 10)
        {
            try
            {
                //根据条件查询集合
                //排序字段
                var sort = Builders<Wo_Repair_Base>.Sort.Descending("createtime");
                //根据条件查询集合

                List<Wo_Repair_Base> list = new List<Wo_Repair_Base>();
                #region 获取list集合数据
                //if (!string.IsNullOrWhiteSpace(stime) && !string.IsNullOrWhiteSpace(etime) && !string.IsNullOrWhiteSpace(Jwocode))
                //{
                //    var lists = await _wo_repair_baseRepository.Get(p => p.createtime >= Convert.ToDateTime(DateTime.Parse(stime.Trim() + " 00:00:00")) && p.createtime <= Convert.ToDateTime(DateTime.Parse(etime.Trim() + " 23:59:59")) && p.Jwocode == Jwocode);
                //    foreach (var item in lists)
                //    {
                //        list.Add(item);
                //    }
                //}
                //else if (!string.IsNullOrWhiteSpace(stime) && !string.IsNullOrWhiteSpace(etime))
                //{
                //    var lists = await _wo_repair_baseRepository.Get(p => p.createtime >= Convert.ToDateTime(DateTime.Parse(stime.Trim() + " 00:00:00")) && p.createtime <= Convert.ToDateTime(DateTime.Parse(etime.Trim() + " 23:59:59")));
                //    foreach (var item in lists)
                //    {
                //        list.Add(item);
                //    }
                //}
                //else if (!string.IsNullOrWhiteSpace(Jwocode))
                //{
                //    var lists = await _wo_repair_baseRepository.Get(p => p.Jwocode == Jwocode);
                //    foreach (var item in lists)
                //    {
                //        list.Add(item);
                //    }
                //}

                #endregion



                //排序字段
                // var sort = Builders<Wo_Repair_Base>.Sort.Descending("createtime");
                //根据条件查询集合
                var listApp = new List<FilterDefinition<Wo_Repair_Base>>();
                listApp.Add(Builders<Wo_Repair_Base>.Filter.Eq("isdelete", 0));
                if (!string.IsNullOrWhiteSpace(stime))
                {
                    listApp.Add(Builders<Wo_Repair_Base>.Filter.Gt("createtime", DateTime.Parse(stime.Trim() + " 00:00:00")));
                }
                if (!string.IsNullOrWhiteSpace(etime))
                {
                    listApp.Add(Builders<Wo_Repair_Base>.Filter.Lt("createtime", DateTime.Parse(etime.Trim() + " 23:59:59")));
                }
                if (!string.IsNullOrWhiteSpace(Jwocode))
                {
                    listApp.Add(Builders<Wo_Repair_Base>.Filter.Lt("Jwocode", Jwocode));
                }

                var filter = Builders<Wo_Repair_Base>.Filter.And(listApp);

                var lists = await _wo_repair_baseRepository.GetByPage(filter, pageindex, pagesize, sort);

                foreach (var item in lists)
                {

                    list.Add(item);
                }


                NPOIHelper npoi = new NPOIHelper();
                var tb = ModelConvertHelper<Wo_Repair_Base>.ConvertListToDataTable(list);
                if (tb != null)
                {
                    //导出dtnew 
                    string[] cols = { };

                    byte[] sm = npoi.ExportToExcel1(tb, cols);
                    if (sm != null)
                    {
                        return File(sm, "application/vnd.ms-excel", "班组工单统计.xlsx");
                    }
                    else
                    {
                        return Error("导出失败");
                    }
                }
                return Error("没有数据");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "导出异常");
                return Error("导出失败");
            }
        }

        /// <summary>
        /// 获取第一条信息是否为未处理工单
        /// </summary>
        /// <returns></returns>
        [HttpGet("getnewmassage")]
        public async Task<IActionResult> Getnewmassage()
        {
            //massagetx=0的时候不弹框  =1的时候要弹框
            string massagetx = "0";
            var firstlist = await _wo_repair_baseRepository.GetAll();
            var modelmap = firstlist.LastOrDefault();
            if (modelmap != null)
            {
                if (modelmap.Jwocode == "" && modelmap.isdelete == 0)
                {
                    massagetx = "1";
                }
            }
            var obj = new
            {
                data = massagetx
            };
            return Success("成功", obj);
        }


        /// <summary>
        /// 获取工单
        /// </summary>
        [HttpGet("getsingle")]
        public async Task<IActionResult> GetSingleAsync(string id, int channelcode = 0)
        {
            //操作人
            string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            if (id != null && id.Trim() != "")
            {
                var dModel = await _wo_repair_baseRepository.GetSingle(id.Trim());
                var dmodel = _wo_repair_baseRepository.GetDetails(id.Trim());
                if (dModel != null)
                {
                    var send = await _wo_repair_sendRepository.Get(s => s.wocode.Equals(dModel.wocode) & s.isdelete == 0);
                    var deal = await _wo_repair_dealRepository.Get(s => s.wocode.Equals(dModel.wocode) & s.isdelete == 0);
                    var evalua = await _wo_repair_evaluateRepository.Get(s => s.wocode.Equals(dModel.wocode) & s.isdelete == 0);
                    var item = await _wo_repair_itemsRepository.Get(s => s.wocode.Equals(dModel.wocode) & s.isdelete == 0);
                    var obj = new
                    {
                        model = dmodel,
                        items = item,
                        sends = send,
                        deals = deal,
                        evaluate = evalua
                    };
                    #region 标记消息提醒工单（消息提醒，App推送）
                    if (channelcode > 0)
                    {
                        //int wostatemap = -1;
                        //if (dModel.wostate == 0)
                        //{
                        //    wostatemap = dModel.wostate; // 0;
                        //    listmap = listmap.Where(x => x.wostate == 0);
                        //}
                        if (dModel.wostate == 0 || dModel.wostate == 1)
                        {
                            //维修工单号
                            var listmap = await _wo_order_user_maprepository.Get(x => x.woid == dModel.id && x.wocode == dModel.wocode && x.channelcode == channelcode && x.readby == usercode);
                            var modelmap = listmap.FirstOrDefault();
                            if (modelmap == null)
                            {
                                //添加是否已读关系表
                                modelmap = new Wo_Order_User_Map();
                                modelmap.woid = dModel.id;
                                modelmap.wocode = dModel.wocode;
                                modelmap.wostate = dModel.wostate;
                                modelmap.readby = usercode;
                                modelmap.channelcode = channelcode;   //工单类型（枚举EnumOrderState） 维修工单 = 1, 保养工单 = 2, 运行工单 = 3, 超时工单 = 4
                                modelmap.isread = 1;
                                modelmap.isnotice = 1;
                                modelmap.readtime = DateTime.Now.ToLocalTime();
                                modelmap.note = "故障说明:" + dModel.faultdescripe;
                                //modelmap.note = dModel.faultdescripe;
                                bool b = await _wo_order_user_maprepository.Add(modelmap);
                            }
                            else
                            {
                                modelmap.isread = 1;
                                modelmap.isnotice = 1;
                                modelmap.note = "故障说明:" + dModel.faultdescripe;
                                // modelmap.note = dModel.faultdescripe;
                                bool b = await _wo_order_user_maprepository.UpdateOne(modelmap);
                            }
                        }
                    }
                    #endregion
                    return Success("获取工单成功", obj);
                }
                else
                {
                    return Error("获取工单失败");
                }
            }
            else
            {
                return Error("获取参数失败");
            }
        }

        /// <summary>
        /// 分页获取
        /// </summary>
        [HttpGet("appgetlistbypage")]
        public IActionResult APPGetListsByPageAsync(string phone, string wocode, string province, string city, string protypeid, string projectid,
            string buildid, string floorid, string replace, string systemid, string equipmentid, string usercode, string stime, string etime,
            int datetype = 4, int state = -1, int pageindex = 1, int pagesize = 10)
        {
            string nowusercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            //var userstr = _signTokenService.GetUserInfoAsync(nowusercode).Result;
            //var userinfo = userstr.ToJObject()["data"].ToString().ToObject<UserInfoModel>();

            
            var userinfos = _sys_user_accountRepository.GetUserInfo(nowusercode);
            var userinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());

            //string nowrolecode = User.Claims.FirstOrDefault(c => c.Type == "RoleCode").Value;

            GetStartEndTime(datetype, out stime, out etime);

            int recordCount = 0;
            int totalcount = 0;
            int wzpcount = 0;
            int yjdcount = 0;
            int dpjcount = 0;
            int ygqcount = 0;
            int ywjcount = 0;
            int yqxcount = 0;
            var result = _wo_repair_baseRepository.GetAppListsByPage(phone, wocode, province, city, projectid, protypeid, buildid, floorid, replace,
                systemid, equipmentid, usercode, stime, etime, state, nowusercode, userinfo, pageindex, pagesize,
                out recordCount, out totalcount, out wzpcount, out yjdcount, out dpjcount, out ygqcount, out ywjcount, out yqxcount);

            var obj = new
            {
                rows = result.ToList(),
                total = recordCount,
                totalcounts = recordCount,//全部
                wzpcounts = wzpcount,//未指派
                yjdcounts = yjdcount,//已接单
                dpjcounts = dpjcount,//待评价
                ygqcounts = ygqcount,//已挂起
                ywjcounts = ywjcount,//已完结
                yqxcounts = yqxcount//已取消
            };
            return Success("获取成功", obj);
        }
        /// <summary>
        /// 获取自己保修的工单
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="wocode"></param>
        /// <param name="province"></param>
        /// <param name="city"></param>
        /// <param name="protypeid"></param>
        /// <param name="projectid"></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="datetype"></param>
        /// <param name="state"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet("appgetlistbypagemy")]
        public IActionResult APPGetListsByPageAsyncmy(string phone, string wocode, string province, string city, string protypeid, string projectid,
          string buildid, string floorid, string replace, string systemid, string equipmentid, string usercode, string stime, string etime,
          int datetype = 4, int state = -1, int pageindex = 1, int pagesize = 10)
        {
            string nowusercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            //var userstr = _signTokenService.GetUserInfoAsync(nowusercode).Result;
            //var userinfo = userstr.ToJObject()["data"].ToString().ToObject<UserInfoModel>();

            
            var userinfos = _sys_user_accountRepository.GetUserInfo(nowusercode);
            var userinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());
            var username = userinfo.username;
            //string nowrolecode = User.Claims.FirstOrDefault(c => c.Type == "RoleCode").Value;

            GetStartEndTime(datetype, out stime, out etime);

            int recordCount = 0;
            int totalcount = 0;
            int wzpcount = 0;
            int yjdcount = 0;
            int dpjcount = 0;
            int ygqcount = 0;
            int ywjcount = 0;
            int yqxcount = 0;
            var result = _wo_repair_baseRepository.GetAppListsByPagemy(phone, wocode, province, city, projectid, protypeid, buildid, floorid, replace, username,
                systemid, equipmentid, usercode, stime, etime, state, nowusercode, userinfo, pageindex, pagesize,
                out recordCount, out totalcount, out wzpcount, out yjdcount, out dpjcount, out ygqcount, out ywjcount, out yqxcount);

            var obj = new
            {
                rows = result.ToList(),
                total = recordCount,
                totalcounts = recordCount,//全部
                wzpcounts = wzpcount,//未指派
                yjdcounts = yjdcount,//已接单
                dpjcounts = dpjcount,//待评价
                ygqcounts = ygqcount,//已挂起
                ywjcounts = ywjcount,//已完结
                yqxcounts = yqxcount//已取消
            };
            return Success("获取成功", obj);
        }

        /// <summary>
        /// app获取工单
        /// </summary>
        [HttpGet("appgetsingle")]
        public async Task<IActionResult> APPGetSingleAsync(string id)
        {
            if (id != null && id.Trim() != "")
            {
                var dModel = await _wo_repair_baseRepository.GetSingle(id.Trim());
                var dmodel = _wo_repair_baseRepository.GetDetails(id.Trim());
                if (dModel != null)
                {
                    var send = await _wo_repair_sendRepository.Get(s => s.wocode.Equals(dModel.wocode) & s.isdelete == 0);
                    var deal = await _wo_repair_dealRepository.Get(s => s.wocode.Equals(dModel.wocode) & s.isdelete == 0);
                    var evalua = await _wo_repair_evaluateRepository.Get(s => s.wocode.Equals(dModel.wocode) & s.isdelete == 0);
                    var item = await _wo_repair_itemsRepository.Get(s => s.wocode.Equals(dModel.wocode) & s.isdelete == 0);
                    var obj = new
                    {
                        model = dmodel,
                        items = item,
                        sends = send,
                        deals = deal,
                        evaluate = evalua
                    };
                    return Success("获取工单成功", obj);
                }
                else
                {
                    return Error("获取工单失败");
                }
            }
            else
            {
                return Error("获取参数失败");
            }

        }

        /// <summary>
        /// 添加工单
        /// </summary>
        [HttpPost("add")]
        public async Task<IActionResult> AddAsync(WoRepairBaseInput input)
        {
            if (string.IsNullOrWhiteSpace(input.projectid))
            {
                return Error("请选择项目");
            }
            else
            {
                var pro = _wo_repair_baseRepository.GetProjectInfo(input.projectid);
                if (pro != null)
                {
                    var promodel = pro as Pro_Project_Info;
                    if (promodel.isend == 1)
                    {
                        return Error("暂停报修");
                    }
                }
            }
            if (string.IsNullOrWhiteSpace(input.repairreplace))
            {
                return Error("请选择保修地点");
            }
            if (string.IsNullOrWhiteSpace(input.repairman))
            {
                return Error("报修人不能为空");
            }
            if (string.IsNullOrWhiteSpace(input.contactway))
            {
                return Error("联系方式不能为空");
            }
            if (string.IsNullOrWhiteSpace(input.faultdescripe))
            {
                return Error("设备名称不能为空");
            }

            Wo_Repair_Base clmodel = new Wo_Repair_Base();
            //时间戳作为工单编号
            clmodel.wocode = DateTime.Now.ToString("yyyyMMddHHmmssfff");

            string optuser = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            string optusername = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;

            clmodel.province = input.province;//省
            clmodel.city = input.city;//市
            clmodel.projectid = input.projectid;//项目名称
            clmodel.protypeid = input.protypeid;//项目类型
            clmodel.buildid = input.buildid;
            clmodel.floorid = input.floorid;
            clmodel.repairreplace = input.repairreplace;//报修地点
            clmodel.repairoom = input.repairoom;//报修地点-房间号
            //if (!string.IsNullOrEmpty(input.repairtime))
            //{
            //    clmodel.repairtime = DateTime.Parse( input.repairtime);//报修时间
            //}
            clmodel.repairtime = DateTime.Now;
            clmodel.repairman = input.repairman;//报修人
            clmodel.contactway = input.contactway;//联系方式
            clmodel.faultdescripe = input.faultdescripe;//故障说明
            clmodel.systemid = input.systemid;
            clmodel.faultpic = input.faultpic;//故障图片
            if (!string.IsNullOrEmpty(input.appointtime))
            {
                clmodel.appointtime = DateTime.Parse(input.appointtime);//预约时间
            }

            clmodel.wostate = (int)EnumWorkOrderState.create;
            clmodel.materials = new List<MaterialModel>();
            clmodel.losematerials = new List<MaterialModel>();
            clmodel.touser = new List<string>();
            clmodel.tousername = new List<string>();
            clmodel.dealuser = new List<string>();
            clmodel.dealusername = new List<string>();

            clmodel.channel = input.channel;

            clmodel.createuser = optuser;
            clmodel.createusername = optusername;
            clmodel.isdelete = 0;
            clmodel.createtime = DateTime.Now;

            clmodel.Jwocode = "";//接班组科室
            clmodel.Pwcode = "";//派单组科室
            clmodel.CLpeoplefrom = "";//处理人来源

            string res = await _wo_repair_baseRepository.AddRetID(clmodel);
            if (!string.IsNullOrEmpty(res))
            {
                infotxs intxinfo = new infotxs();
                intxinfo.zpusern = optusername;
                intxinfo.jsuser = input.assignman;
                intxinfo.wocode = clmodel.wocode;
                intxinfo.istxinfo = 0;
                intxinfo.isdelete = 0;
                await _infotxsRepository.Add(intxinfo);

                string users = optuser;
                if (clmodel.channel == 4)
                {
                    users = optusername;
                    await SendWechatMsg(clmodel.wocode, "故障报修成功！", "故障现象：" + clmodel.faultdescripe, res, optuser);
                    //SendWechatMessage(clmodel.wocode, "故障报修成功！", "编号：" + clmodel.wocode + "\n故障现象：" + clmodel.faultdescripe, res, optuser);
                }
                AddItems(clmodel.wocode, clmodel.wostate, input.channel, users + " 创建工单", clmodel.faultpic, optuser, optusername, "");

                return Success("工单保存成功");
            }
            else
                return Error("工单保存失败");
        }

        /// <summary>
        /// 添加工单
        /// </summary>
        [HttpPost("addbak")]
        public async Task<IActionResult> AddAsyncBak(WoRepairBaseInput input)
        {
            if (string.IsNullOrWhiteSpace(input.projectid))
            {
                return Error("请选择项目");
            }
            else
            {
                var pro = _wo_repair_baseRepository.GetProjectInfo(input.projectid);
                if (pro != null)
                {
                    var promodel = pro as Pro_Project_Info;
                    if (promodel.isend == 1)
                    {
                        return Error("暂停报修");
                    }
                }
            }
            if (string.IsNullOrWhiteSpace(input.repairreplace))
            {
                return Error("请选择保修地点");
            }
            if (string.IsNullOrWhiteSpace(input.repairman))
            {
                return Error("报修人不能为空");
            }
            if (string.IsNullOrWhiteSpace(input.contactway))
            {
                return Error("联系方式不能为空");
            }
            if (string.IsNullOrWhiteSpace(input.faultdescripe))
            {
                return Error("故障说明不能为空");
            }
            if (string.IsNullOrEmpty(input.assignman))
            {
                return Error("请选择维修班组");
            }

            string zdusercode = ""; string zdusername = "";
            if (input.assignman != "后勤部") //自动派单
            {
                //获取当前工单的科室名称
                var index = input.repairreplace.LastIndexOf(' ');
                string keshiname = input.repairreplace.Substring(index + 1, input.repairreplace.Length - index - 1);
                string grouptype = "";
                string pbshijian = DateTime.Now.ToString("yyyy-MM-dd");
                #region 判断当前的时间是白班还是夜班
                var stime = _configuration["watchtime:stime"].ToString();
                var etime = _configuration["watchtime:etime"].ToString();

                DateTime startdt = DateTime.Parse(stime);
                DateTime enddt = DateTime.Parse(etime);
                int startdtH = startdt.Hour;
                int enddth = enddt.Hour;

                int startdtm = startdt.Minute;
                int enddtm = enddt.Minute;
                int currenthour = DateTime.Now.Hour;
                int currentmin = DateTime.Now.Minute;
                //假设stime 08:30 etime 17:30
                if (currenthour > startdtH && currenthour < enddth)
                {
                    //当前时间大于开始的小时小于结束的小时
                    //baiban
                    grouptype = "白班";
                }
                else if (currenthour == startdtH && currentmin >= startdtm)
                {
                    grouptype = "白班";
                }
                else if (currenthour == enddth && currentmin <= enddtm)
                {
                    grouptype = "白班";
                }
                else
                {
                    grouptype = "夜班";
                }
                #endregion

                #region 白班的时候根据科室名找到对应的人
                if (grouptype == "白班")
                {
                    var list = _sys_work_datgroupRepository.GetPBUserList(grouptype, keshiname, "").ToList();
                    if (list.Count > 0)
                    {
                        var pbuser = list.First();
                        var pbusermodel = JsonHelper.JsonToEntity<Sys_Work_DayGroup>(pbuser.ToJson());
                        zdusercode = pbusermodel.usercode;
                    }
                    else
                    {
                        return Error("先添加值班信息");
                    }
                }
                //夜班的时候根据当前日期找到对应的人
                if (grouptype == "夜班")
                {
                    var list = _sys_work_datgroupRepository.GetPBUserList(grouptype, "", pbshijian).ToList();
                    if (list.Count > 0)
                    {
                        var pbuser = list.First();
                        var pbusermodel = JsonHelper.JsonToEntity<Sys_Work_DayGroup>(pbuser.ToJson());
                        zdusercode = pbusermodel.usercode;
                    }
                    else
                    {
                        return Error("先添加值班信息");
                    }

                }
                #endregion

                
                var userinfos = _sys_user_accountRepository.GetUserInfo(zdusercode);
                var touserinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());
                if (touserinfo.level != 1)
                {
                    zdusername = touserinfo.username;
                    return Error(input.assignman + "不是维修工");
                }
            }

            Wo_Repair_Base clmodel = new Wo_Repair_Base();
            //时间戳作为工单编号
            clmodel.wocode = DateTime.Now.ToString("yyyyMMddHHmmssfff");

            string optuser = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            string optusername = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;

            clmodel.province = input.province;//省
            clmodel.city = input.city;//市
            clmodel.projectid = input.projectid;//项目名称
            clmodel.protypeid = input.protypeid;//项目类型
            clmodel.buildid = input.buildid;
            clmodel.floorid = input.floorid;
            clmodel.repairreplace = input.repairreplace;//报修地点
            clmodel.repairoom = input.repairoom;//报修地点-房间号
            //if (!string.IsNullOrEmpty(input.repairtime))
            //{
            //    clmodel.repairtime = DateTime.Parse( input.repairtime);//报修时间
            //}
            clmodel.repairtime = DateTime.Now;
            clmodel.repairman = input.repairman;//报修人
            clmodel.contactway = input.contactway;//联系方式
            clmodel.faultdescripe = input.faultdescripe;//故障说明
            clmodel.systemid = input.systemid;
            clmodel.faultpic = input.faultpic;//故障图片
            if (!string.IsNullOrEmpty(input.appointtime))
            {
                clmodel.appointtime = DateTime.Parse(input.appointtime);//预约时间
            }

            clmodel.wostate = (int)EnumWorkOrderState.create;
            clmodel.materials = new List<MaterialModel>();
            clmodel.losematerials = new List<MaterialModel>();
            clmodel.touser = new List<string>();
            clmodel.tousername = new List<string>();
            clmodel.dealuser = new List<string>();
            clmodel.dealusername = new List<string>();

            clmodel.channel = input.channel;

            clmodel.createuser = optuser;
            clmodel.createusername = optusername;
            clmodel.isdelete = 0;
            clmodel.createtime = DateTime.Now;

            clmodel.Jwocode = "";//接班组科室
            clmodel.Pwcode = "";//派单组科室
            clmodel.CLpeoplefrom = "";//处理人来源

            string res = string.Empty;
            if (!string.IsNullOrEmpty(zdusercode))
            {
                clmodel.touser = new List<string>() { zdusercode };
                clmodel.tousername = new List<string>() { zdusername };
                clmodel.wostate = (int)EnumWorkOrderState.assign;
                clmodel.totime = DateTime.Now;

                res = await _wo_repair_baseRepository.AddRetID(clmodel);
                if (!string.IsNullOrEmpty(res))
                {
                    string users = optuser;
                    if (clmodel.channel == 4)
                    {
                        users = optusername;
                        await SendWechatMsg(clmodel.wocode, "故障报修成功！", "故障现象：" + clmodel.faultdescripe, res, optuser);
                        //SendWechatMessage(clmodel.wocode, "故障报修成功！", "编号：" + clmodel.wocode + "\n故障现象：" + clmodel.faultdescripe, res, optuser);
                    }
                    AddItems(clmodel.wocode, clmodel.wostate, input.channel, users + " 创建工单", clmodel.faultpic, optuser, optusername, "");

                    Wo_Repair_Send wrs = new Wo_Repair_Send();
                    wrs.wocode = clmodel.wocode;
                    wrs.wostate = clmodel.wostate;
                    wrs.channel = input.channel;
                    wrs.totype = 1;
                    wrs.touser = new List<string>() { zdusercode };
                    wrs.tousername = new List<string>() { zdusername };
                    wrs.dealuser = new List<string>();
                    wrs.dealusername = new List<string>();
                    wrs.detail = input.assignremark;
                    wrs.isdeal = 0;
                    wrs.files = new List<FileBaseModel>();
                    wrs.isdelete = 0;
                    wrs.createuser = optuser;
                    wrs.createusername = optusername;
                    wrs.createtime = DateTime.Now;
                    bool bl = await _wo_repair_sendRepository.Add(wrs);
                    if (bl)
                    {
                        string msg = string.IsNullOrEmpty(input.assignremark) ? "" : " ，备注：" + input.assignremark;
                        AddItems(clmodel.wocode, clmodel.wostate, input.channel, optuser + "-" + optusername + " 指派工单给 " + input.assignman + "-" + zdusercode + "-" + zdusername + msg, new List<FileBaseModel>(), optuser, optusername, "");
                    }
                }
            }
            else
            {

                res = await _wo_repair_baseRepository.AddRetID(clmodel);
                if (!string.IsNullOrEmpty(res))
                {
                    infotxs intxinfo = new infotxs();
                    intxinfo.zpusern = optusername;
                    intxinfo.jsuser = input.assignman;
                    intxinfo.wocode = clmodel.wocode;
                    intxinfo.istxinfo = 0;
                    intxinfo.isdelete = 0;
                    await _infotxsRepository.Add(intxinfo);

                    string users = optuser;
                    if (clmodel.channel == 4)
                    {
                        users = optusername;
                        await SendWechatMsg(clmodel.wocode, "故障报修成功！", "故障现象：" + clmodel.faultdescripe, res, optuser);
                        //SendWechatMessage(clmodel.wocode, "故障报修成功！", "编号：" + clmodel.wocode + "\n故障现象：" + clmodel.faultdescripe, res, optuser);
                    }
                    AddItems(clmodel.wocode, clmodel.wostate, input.channel, users + " 创建工单", clmodel.faultpic, optuser, optusername, "");
                }
            }

            if (!string.IsNullOrEmpty(res))
            {
                return Success("工单保存成功");
            }
            else
                return Error("工单保存失败");

        }

        /// <summary>
        /// 修改工单
        /// </summary>
        [HttpPost("update")]
        public async Task<IActionResult> UpdateAsync(WoRepairBaseInput input)
        {
            string optuser = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            string optusername = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;

            //var userstr = _signTokenService.GetUserInfoAsync(optuser).Result;
            //var userinfo = userstr.ToJObject()["data"].ToString().ToObject<UserInfoModel>();

            
            var userinfos = _sys_user_accountRepository.GetUserInfo(optuser);
            var userinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());

            var clmodel = await _wo_repair_baseRepository.GetSingle(input.id);
            if (clmodel != null && clmodel.wostate == 0)// && clmodel.createuser == optuser  (int)EnumWorkOrderState.create
            {
                //if (string.IsNullOrWhiteSpace(input.projectid))
                //{
                //    return Error("请选择项目");
                //}
                if (string.IsNullOrWhiteSpace(input.repairreplace))
                {
                    return Error("请选择保修地点");
                }
                if (string.IsNullOrWhiteSpace(input.repairman))
                {
                    return Error("报修人不能为空");
                }
                if (string.IsNullOrWhiteSpace(input.contactway))
                {
                    return Error("联系方式不能为空");
                }
                if (string.IsNullOrWhiteSpace(input.faultdescripe))
                {
                    return Error("设备名称不能为空");
                }

                //clmodel.province = input.province;//省
                //clmodel.city = input.city;//市
                //clmodel.projectid = input.projectid;//项目名称
                //clmodel.protypeid = input.protypeid;//项目类型
                //clmodel.buildid = input.buildid;
                //clmodel.floorid = input.floorid;

                clmodel.repairreplace = input.repairreplace;//报修地点
                clmodel.repairoom = input.repairoom;//报修地点-房间号
                if (!string.IsNullOrEmpty(input.repairtime))
                {
                    clmodel.repairtime = DateTime.Parse(input.repairtime);//报修时间
                }
                clmodel.repairman = input.repairman;//报修人
                clmodel.contactway = input.contactway;//联系方式
                clmodel.faultdescripe = input.faultdescripe;//详细地点
                //clmodel.faultpic = input.faultpic;//故障图片
                //if (!string.IsNullOrEmpty(input.appointtime))
                //{
                //    clmodel.appointtime = DateTime.Parse(input.appointtime);//预约时间
                //}
                //clmodel.wostate = (int)EnumWorkOrderState.create;

                var res = await _wo_repair_baseRepository.UpdateOne(clmodel);
                if (res)
                {
                    string users = optuser;
                    if (input.channel == 4) { users = optusername; }
                    AddItems(clmodel.wocode, clmodel.wostate, input.channel, optuser + " 修改工单", clmodel.faultpic, optuser, optusername, "");
                    return Success("工单保存成功");
                }
                else
                    return Error("工单保存失败");
            }

            return Error("工单不存在或工单不为新工单");
        }

        /// <summary>
        /// 处理工单
        /// </summary>
        [HttpPost("deal")]
        public async Task<IActionResult> UpdateDealAsync(string wocode, string systemid, string equipmentid, string faulttypeid, string detail,
            List<string> dealusers,
            List<FileBaseModel> exfiles, List<FileBaseModel> files, string wxtimeid, int state, int channel = 1)
        //public async Task<IActionResult> UpdateDealAsync(string wocode, string systemid, string equipmentid, string faulttypeid, string detail,
        //    int isusematerial, List<MaterialModel> materialinfo, int islosematerial, List<MaterialModel> losematerialinfo, List<string> dealusers,
        //    List<FileBaseModel> exfiles, List<FileBaseModel> files, string CLpeoplefrom, string wxtimeid, int state, int channel = 1)
        {
            //if (string.IsNullOrWhiteSpace(systemid))
            //{
            //    return Error("请选择系统");
            //}
            if (string.IsNullOrWhiteSpace(faulttypeid))
            {
                return Error("请选择故障类型");
            }
            if (string.IsNullOrWhiteSpace(detail))
            {
                return Error("请输入异常说明");
            }

            string optuser = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            string optusername = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;

            var cls = await _wo_repair_baseRepository.Get(p => p.wocode == wocode && p.isdelete == 0);
            var clmodel = cls.FirstOrDefault();
            if (clmodel != null && clmodel.wostate == (int)EnumWorkOrderState.assign && clmodel.touser.Contains(optuser))
            {
                var sends = await _wo_repair_sendRepository.Get(p => p.wocode == wocode && p.isdeal == 0 && p.isdelete == 0);
                var send = sends.OrderByDescending(p => p.createtime).FirstOrDefault();
                if (send.touser.Contains(optuser))
                {
                    string dealuserinfos = optuser + "-" + optusername;
                    //List<string> dealcodes = new List<string>() { optuser };
                    //List<string> dealnames = new List<string>() { optusername };
                    clmodel.dealuser.Add(optuser);
                    clmodel.dealusername.Add(optusername);

                    if (dealusers != null)
                    {
                        foreach (string struser in dealusers)
                        {
                            if (!clmodel.dealuser.Contains(struser))
                            {
                                ////var dealuserstr = _signTokenService.GetUserInfoAsync(optuser).Result;
                                //var dealuserstr = _signTokenService.GetUserInfoAsync(optuser).Result;
                                //var dealuserinfo = struser.ToJObject()["data"].ToString().ToObject<UserInfoModel>();

                                var xtruserinfo = await _sys_user_accountRepository.GetSingle(p => p.usercode == struser);

                                //dealcodes.Add(dealuserinfo.usercode);
                                //dealnames.Add(dealuserinfo.username);
                                clmodel.dealuser.Add(xtruserinfo.usercode);
                                clmodel.dealusername.Add(xtruserinfo.username);

                                dealuserinfos = dealuserinfos + "、" + xtruserinfo.usercode + "-" + xtruserinfo.username;
                            }
                        }
                    }

                    //   if (clmodel.ismaterials != 1) { clmodel.ismaterials = isusematerial; }
                    //if (materialinfo != null && materialinfo.Count > 0)
                    //{
                    //    if (clmodel.materials == null || clmodel.materials.Count == 0)
                    //    {
                    //        clmodel.materials = materialinfo;
                    //    }
                    //    else
                    //    {
                    //        clmodel.materials = clmodel.materials.Union(materialinfo).ToList();
                    //    }
                    //}

                    string msg = "处理";
                    if (state == (int)EnumWorkOrderState.hangup)
                    {
                        msg = "挂起";
                        clmodel.hanguptime = DateTime.Now;


                        //clmodel.islosematerials = islosematerial;
                        //if (islosematerial == 1)
                        //{
                        //    if (losematerialinfo != null && losematerialinfo.Count > 0)
                        //    {
                        //        clmodel.losematerials = losematerialinfo;
                        //    }
                        //}
                        //else
                        //{
                        //    clmodel.losematerials = new List<MaterialModel>();
                        //}
                        clmodel.losematerials = new List<MaterialModel>();
                    }
                    if (state == (int)EnumWorkOrderState.deal)
                    {
                        msg = "处理";
                        clmodel.dealtime = DateTime.Now;
                        clmodel.islosematerials = 0;
                        clmodel.losematerials = new List<MaterialModel>();
                    }

                    //clmodel.dealuser = dealcodes;
                    //clmodel.dealusername = dealnames;
                    clmodel.wostate = state;
                    clmodel.faulttypeid = faulttypeid;
                    clmodel.systemid = systemid;
                    //clmodel.equipmentid = equipmentid;

                    //if (userinfo.username.Contains("第三方"))
                    //{
                    //    clmodel.CLpeoplefrom = "第三方";
                    //}
                    //else
                    //{
                    //    clmodel.CLpeoplefrom = CLpeoplefrom;
                    //}

                    if (wxtimeid != null && wxtimeid != "")
                    {
                        clmodel.wxtimeid = wxtimeid;
                    }
                    await _wo_repair_baseRepository.UpdateOne(clmodel);

                    send.isdeal = 1;
                    send.dealuser = clmodel.dealuser;
                    send.dealusername = clmodel.dealusername;
                    send.dealtime = DateTime.Now;
                    await _wo_repair_sendRepository.UpdateOne(send);

                    Wo_Repair_Deal wrd = new Wo_Repair_Deal();
                    wrd.wostate = state;
                    wrd.wocode = wocode;
                    wrd.systemid = systemid;
                    wrd.equipmentid = equipmentid;
                    wrd.faulttypeid = faulttypeid;
                    wrd.dealuser = clmodel.dealuser;
                    wrd.dealusername = clmodel.dealusername;
                    wrd.detail = detail;
                    //wrd.ismaterials = isusematerial;
                    //wrd.materials = materialinfo;
                    //wrd.losematerials = losematerialinfo;
                    if (files != null)
                    {
                        wrd.files = files;
                    }
                    else
                    {
                        wrd.files = new List<FileBaseModel>();
                    }
                    if (exfiles != null)
                    {
                        wrd.exfiles = exfiles;
                    }
                    else
                    {
                        wrd.exfiles = new List<FileBaseModel>();
                    }

                    wrd.isdelete = 0;
                    wrd.createuser = optuser;
                    wrd.createusername = optusername;
                    wrd.createtime = DateTime.Now;

                    await _wo_repair_dealRepository.Add(wrd);

                    AddItems(clmodel.wocode, clmodel.wostate, clmodel.channel, dealuserinfos + " " + msg + "工单，异常说明：" + detail, wrd.files.Concat(wrd.exfiles).ToList(), optuser, optusername, "");

                    if (clmodel.channel == 4 && state == (int)EnumWorkOrderState.deal)
                    {
                        await SendWechatMsg(clmodel.wocode, "故障处理成功！", "异常说明：" + wrd.detail, clmodel.id, clmodel.createuser);
                    }

                    return Success("处理成功");
                }
            }

            return Error("处理失败");
        }

        /// <summary>
        /// 抢单
        /// </summary>
        [HttpPost("grab")]
        public async Task<IActionResult> UpdateGrabAsync(string wocode, int channel = 1)
        {
            string optuser = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            string optusername = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;

            
            var userinfos = _sys_user_accountRepository.GetUserInfo(optuser);
            var userinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());

            var cls = await _wo_repair_baseRepository.Get(p => p.wocode == wocode && p.isdelete == 0);
            var clmodel = cls.FirstOrDefault();
            if (clmodel != null && clmodel.wostate == (int)EnumWorkOrderState.create)
            {
                int limitcount = _wo_repair_baseRepository.GetLimitCount(optuser);
                if (limitcount > 0)
                {
                    return Error("你已经有" + limitcount + "个未处理工单，已超上限");
                }

                clmodel.wostate = (int)EnumWorkOrderState.assign;
                clmodel.totype = 2;
                clmodel.touser = new List<string>() { optuser };
                clmodel.tousername = new List<string>() { optusername };
                clmodel.totime = DateTime.Now;
                clmodel.dealuser = new List<string>();
                clmodel.dealusername = new List<string>();
                await _wo_repair_baseRepository.UpdateOne(clmodel);

                Wo_Repair_Send wrs = new Wo_Repair_Send();
                wrs.wocode = clmodel.wocode;
                wrs.wostate = clmodel.wostate;
                wrs.channel = channel;
                wrs.totype = 2;
                wrs.touser = new List<string>() { optuser };
                wrs.tousername = new List<string>() { optusername };
                wrs.dealuser = new List<string>();
                wrs.dealusername = new List<string>();
                wrs.detail = "";
                wrs.isdeal = 0;
                wrs.files = new List<FileBaseModel>();
                wrs.isdelete = 0;
                wrs.createuser = optuser;
                wrs.createusername = optusername;
                wrs.createtime = DateTime.Now;
                await _wo_repair_sendRepository.Add(wrs);

                string mobile = string.IsNullOrEmpty(userinfo.mobile) ? "" : "（" + userinfo.mobile + "）";
                AddItems(clmodel.wocode, clmodel.wostate, channel, optuser + "-" + optusername + mobile + " 抢单", new List<FileBaseModel>(), optuser, optusername, "");

                if (clmodel.channel == 4)
                {
                    string content = "维修人：" + optuser + "-" + optusername + "\n电话：" + userinfo.mobile;
                    await SendWechatMsg(clmodel.wocode, "维修人员接单！", content, clmodel.id, clmodel.createuser);
                }

                return Success("抢单成功");
            }

            return Error("抢单失败");

        }
        /// <summary>
        /// 转派给班组
        /// </summary>
        /// <param name="wocode"></param>
        /// <param name="assignman"></param>
        /// <param name="assignremark"></param>
        /// <param name="channel"></param>
        /// <returns></returns>
        [HttpPost("assignbz")]
        public async Task<IActionResult> UpdateAssignAsync1(string wocode, string assignman, string assignremark, int channel = 1)
        {


            if (string.IsNullOrEmpty(assignman))
            {
                return Error("请选择维修班组");
            }
            string optuser = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            string optusername = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;
            var cls = await _wo_repair_baseRepository.Get(p => p.wocode == wocode && p.isdelete == 0);
            var clmodel = cls.FirstOrDefault();
            if (optuser == "8000" || clmodel.createuser == optuser || optuser == "8008")
            {
            }
            else {
                return Error("这不是你提交的报修工单，无法进行派单操作");
            }

            if (clmodel != null && (clmodel.wostate == (int)EnumWorkOrderState.create || clmodel.wostate == (int)EnumWorkOrderState.assign || clmodel.wostate == (int)EnumWorkOrderState.hangup))
            {
                clmodel.wostate = 0;
                clmodel.totype = 1;
                clmodel.touser = new List<string>();
                clmodel.tousername = new List<string>();
                clmodel.tobztimes = DateTime.Now;
               
                clmodel.dealuser = new List<string>();
                clmodel.dealusername = new List<string>();
                ///判断是否派过班组
                if (clmodel.Jwocode != null && clmodel.Jwocode != assignman && (clmodel.Pwcode == null || clmodel.Pwcode == ""))
                {
                    clmodel.Pwcode = clmodel.Jwocode;
                }
                clmodel.Jwocode = assignman;//接单单班组科室
                string zdusercode = "";
                if (assignman != "后勤部") //自动派单
                {
                    //获取当前工单的科室名称
                    var index = clmodel.repairreplace.LastIndexOf(' ');
                    string keshiname = clmodel.repairreplace.Substring(index + 1, clmodel.repairreplace.Length - index - 1);
                    string grouptype = "";
                    string pbshijian = DateTime.Now.ToString("yyyy-MM-dd");
                    #region 判断当前的时间是白班还是夜班
                    var stime = _configuration["watchtime:stime"].ToString();
                    var etime = _configuration["watchtime:etime"].ToString();

                    DateTime startdt = DateTime.Parse(stime);
                    DateTime enddt = DateTime.Parse(etime);
                    int startdtH = startdt.Hour;
                    int enddth = enddt.Hour;

                    int startdtm = startdt.Minute;
                    int enddtm = enddt.Minute;
                    int currenthour = DateTime.Now.Hour;
                    int currentmin = DateTime.Now.Minute;
                    //假设stime 08:30 etime 17:30
                    if (currenthour > startdtH && currenthour < enddth)
                    {
                        //当前时间大于开始的小时小于结束的小时
                        //baiban
                        grouptype = "白班";
                    }
                    else if (currenthour == startdtH && currentmin >= startdtm)
                    {
                        grouptype = "白班";
                    }
                    else if (currenthour == enddth && currentmin <= enddtm)
                    {
                        grouptype = "白班";
                    }
                    else {

                        grouptype = "夜班";
                    }
                    #endregion

                    

                    #region 白班的时候根据科室名找到对应的人
                    if (grouptype == "白班")
                    {
                        var list = _sys_work_datgroupRepository.GetPBUserList(grouptype, keshiname, "").ToList();
                        if (list.Count > 0)
                        {
                            var pbuser = list.First();
                            var pbusermodel = JsonHelper.JsonToEntity<Sys_Work_DayGroup>(pbuser.ToJson());
                            zdusercode = pbusermodel.usercode;

                        }
                        else {
                            return Error("先添加值班信息");
                        }
                    }
                    //夜班的时候根据当前日期找到对应的人
                    if (grouptype == "夜班")
                    {
                        var list = _sys_work_datgroupRepository.GetPBUserList(grouptype, "", pbshijian).ToList();
                        if (list.Count > 0)
                        {
                            var pbuser = list.First();
                            var pbusermodel = JsonHelper.JsonToEntity<Sys_Work_DayGroup>(pbuser.ToJson());
                            zdusercode = pbusermodel.usercode;
                        }
                        else
                        {
                            return Error("先添加值班信息");
                        }

                    }
                    #endregion


                    
                    var userinfos = _sys_user_accountRepository.GetUserInfo(zdusercode);
                    var touserinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());
                    if (touserinfo.level != 1)
                    {
                        return Error(assignman + "不是维修工");
                    }

                    var userinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());

                    clmodel.touser = new List<string>() { zdusercode };
                    clmodel.tousername = new List<string>() { touserinfo.username };
                    clmodel.wostate = (int)EnumWorkOrderState.assign;
                    clmodel.totime = DateTime.Now;

                    Wo_Repair_Send wrs = new Wo_Repair_Send();
                    wrs.wocode = clmodel.wocode;
                    wrs.wostate = clmodel.wostate;
                    wrs.channel = channel;
                    wrs.totype = 1;
                    wrs.touser = new List<string>() { zdusercode };
                    wrs.tousername = new List<string>() { touserinfo.username };
                    wrs.dealuser = new List<string>();
                    wrs.dealusername = new List<string>();
                    wrs.detail = assignremark;
                    wrs.isdeal = 0;
                    wrs.files = new List<FileBaseModel>();
                    wrs.isdelete = 0;
                    wrs.createuser = optuser;
                    wrs.createusername = optusername;
                    wrs.createtime = DateTime.Now;
                    await _wo_repair_sendRepository.Add(wrs);
                }

                await _wo_repair_baseRepository.UpdateOne(clmodel);


                //定义一个表 添加一条数据进行提醒
                if (assignman == "后勤部") //自动派单
                {
                    infotxs infotx = await _infotxsRepository.GetSingle(p => p.wocode == wocode);
                    if (infotx != null)
                    {
                        infotx.istxinfo = 0;
                        await _infotxsRepository.UpdateOne(infotx);
                    }
                    else
                    {
                        infotxs intxinfo = new infotxs();
                        intxinfo.zpusern = optusername;
                        intxinfo.jsuser = assignman;
                        intxinfo.wocode = wocode;
                        intxinfo.istxinfo = 0;
                        intxinfo.isdelete = 0;
                        await _infotxsRepository.Add(intxinfo);
                    }
                }
                string msg = string.IsNullOrEmpty(assignremark) ? "" : " ，备注：" + assignremark;
                if (assignman != "后勤部") //自动派单
                {
                    
                    var userinfos = _sys_user_accountRepository.GetUserInfo(zdusercode);
                    var touserinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());
                    AddItems(clmodel.wocode, clmodel.wostate, channel, optuser + "-" + optusername + " 指派工单给 " + assignman + "-"  + zdusercode+ "-"+touserinfo.username + msg, new List<FileBaseModel>(), optuser, optusername, "");
                }
                else
                { 
                    AddItems(clmodel.wocode, clmodel.wostate, channel, optuser + "-" + optusername + " 指派工单给 " + assignman  + msg, new List<FileBaseModel>(), optuser, optusername, "");

                }
                if (assignman == "后勤部") //自动派单
                {
                    //根据接单班组名称获取班组账号的code
                    var usercoudemode = await _sys_user_accountRepository.GetSingle(p => p.username == assignman);
                    if (usercoudemode != null)
                    {
                        var clintidlist1 = await _IusercoldandclintidsRepository.GetSingle(p => p.usercode == usercoudemode.usercode);
                        if (clintidlist1 != null)
                        {
                            PushMessageToList("通知", "您有新的工单需要处理", clintidlist1.clintid);
                        }
                    }
                }
                return Success("派单成功");
            }
            return Error("派单失败");

        }
        /// <summary>
        /// 获取当前登录角色 有数据提醒 没数据不提醒
        /// </summary>
        /// <returns></returns>
        [HttpPost("getinfotx")]
        public async Task<IActionResult> Getinfotx()
        {

            string optuser = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            // string optusername = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;

            //var userstr = _signTokenService.GetUserInfoAsync(optuser).Result;
            //var userinfo = userstr.ToJObject()["data"].ToString().ToObject<UserInfoModel>();

            
            var userinfos = _sys_user_accountRepository.GetUserInfo(optuser);
            var userinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());

            var jsusername = "";
            if (userinfo != null)
            {
                jsusername = userinfo.role_name;
            }
            var totals = 0;
            var countlist = await _infotxsRepository.Get(p => p.istxinfo == 0 && p.jsuser == jsusername);
            var listmodel = new List<infotxs>();
            if (countlist != null)
            {
                totals = countlist.Count();
                #region 数据
                //foreach (var item in countlist)
                //{
                //    ///定义选项
                //    var model = new infotxs();
                //    model.id = item.id;

                //    model.wocode = item.wocode;// 工单号

                //    model.zpusern = item.zpusern;// 指派人姓名
                //    model.jsuser = item.jsuser;// 接收班组
                //    model.istxinfo = item.istxinfo;// 是否提醒(0提醒 1不提醒)
                //    model.isdelete = item.isdelete;// 是否删除(0正常1删除)
                //    listmodel.Add(model);
                //} 
                #endregion
            }
            var obj = new
            {
                rows = listmodel,
                total = totals
            };
            return Success("成功", obj);
        }

        ////获取当前登录人角色 有未评价的单子时进行评价
        [HttpPost("getnopjwocode")]
        public async Task<IActionResult> GetnoPjwocode()
        {
            string optuser = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            // string optusername = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;

            //var userstr = _signTokenService.GetUserInfoAsync(optuser).Result;
            //var userinfo = userstr.ToJObject()["data"].ToString().ToObject<UserInfoModel>();

            
            var userinfos = _sys_user_accountRepository.GetUserInfo(optuser);
            var userinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());
            var jsusername = "";//登录人姓名
            var jsusercode = "";//登录人编号
            var codecount = 0;
            var flist = new List<FilterDefinition<Wo_Repair_Base>>();
            flist.Add(Builders<Wo_Repair_Base>.Filter.Eq("isdelete", 0));
            flist.Add(Builders<Wo_Repair_Base>.Filter.Where(s => s.wostate == 2));
            if (userinfo != null)
            {
                jsusername = userinfo.username;
                jsusercode = userinfo.usercode;
            }


            if (jsusername != "" && jsusername != "")
            {
                var lismode = await _wo_repair_baseRepository.Get(p => p.isdelete == 0 && p.wostate == 2 && (p.repairman.Contains(jsusername) || p.repairman.Contains(jsusercode)));
                if (lismode != null)
                {
                    codecount = lismode.Count();
                }
            }
            var obj = new
            {
                total = codecount
            };
            return Success("成功", obj);
        }
        /// <summary>
        /// 退单
        /// </summary>
        /// <returns></returns>
        [HttpPost("backwocode")]
        public async Task<IActionResult> Backwocode(string wocode)
        {

            var clmodel = await _wo_repair_baseRepository.GetSingle(p => p.wocode == wocode);
            clmodel.wostate = 0;
            clmodel.touser = new List<string>();
            clmodel.tousername = new List<string>();
            clmodel.dealuser = new List<string>();
            clmodel.dealusername = new List<string>();
            clmodel.totime = null;
            await _wo_repair_baseRepository.UpdateOne(clmodel);
            infotxs infotx = await _infotxsRepository.GetSingle(p => p.wocode == wocode);
            if (infotx != null)
            {
                infotx.istxinfo = 0;
                await _infotxsRepository.UpdateOne(infotx);
            }
            string optuser = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            string optusername = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;
            AddItems(clmodel.wocode, clmodel.wostate, 1, optuser+"-"+ optusername + "退回了工单", new List<FileBaseModel>(), optuser, optusername, "");



            return Success("退单成功");
        }


        /// <summary>
        /// 派单
        /// </summary>
        [HttpPost("assign")]
        public async Task<IActionResult> UpdateAssignAsync(string wocode, string assignman, string assignremark, string pwocode, int channel = 1)
        {

            if (string.IsNullOrEmpty(assignman))
            {
                return Error("请选择维修工");
            }
            //int limitcount = _wo_repair_baseRepository.GetLimitCount(assignman);
            //if (limitcount > 0)
            //{
            //    return Error(assignman + "已经有" + limitcount + "个未处理工单，已超上限");
            //}
    
            
            var userinfos = _sys_user_accountRepository.GetUserInfo(assignman);
            var touserinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());
            if (touserinfo.level != 1)
            {
                return Error(assignman + "不是维修工");
            }

            string optuser = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            string optusername = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;
            //var userstr = _signTokenService.GetUserInfoAsync(optuser).Result;
            //var userinfo = userstr.ToJObject()["data"].ToString().ToObject<UserInfoModel>();

            var userinfos2 = _sys_user_accountRepository.GetUserInfo(optuser);
            var userinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());

            var cls = await _wo_repair_baseRepository.Get(p => p.wocode == wocode && p.isdelete == 0);
            var clmodel = cls.FirstOrDefault();

            infotxs infotx = await _infotxsRepository.GetSingle(p => p.wocode == wocode);
            if (infotx != null)
            {
                infotx.istxinfo = 1;
                await _infotxsRepository.Update(infotx);
            }
            if (clmodel != null && (clmodel.wostate == (int)EnumWorkOrderState.create || clmodel.wostate == (int)EnumWorkOrderState.assign || clmodel.wostate == (int)EnumWorkOrderState.hangup))
            {
                if (clmodel.wostate == 7)
                {
                    clmodel.tobztimes = DateTime.Now;
                }
                clmodel.wostate = (int)EnumWorkOrderState.assign;
                clmodel.totype = 1;
                clmodel.touser = new List<string>() { assignman };
                clmodel.tousername = new List<string>() { touserinfo.username };
                clmodel.totime = DateTime.Now;
                clmodel.dealuser = new List<string>();
                clmodel.dealusername = new List<string>();
                //clmodel.Pwcode = pwocode;//派单班组科室
                await _wo_repair_baseRepository.UpdateOne(clmodel);

                if (clmodel.wostate == (int)EnumWorkOrderState.assign)
                {
                    var sends = await _wo_repair_sendRepository.Get(p => p.wocode == wocode && p.isdeal == 0 && p.isdelete == 0);
                    var send = sends.OrderByDescending(p => p.createtime).FirstOrDefault();
                    if (send != null)
                    {
                        send.isdelete = 1;
                        send.deleteuser = optuser;
                        send.deletetime = DateTime.Now;
                        await _wo_repair_sendRepository.UpdateOne(send);
                    }
                }

                Wo_Repair_Send wrs = new Wo_Repair_Send();
                wrs.wocode = clmodel.wocode;
                wrs.wostate = clmodel.wostate;
                wrs.channel = channel;
                wrs.totype = 1;
                wrs.touser = new List<string>() { assignman };
                wrs.tousername = new List<string>() { touserinfo.username };
                wrs.dealuser = new List<string>();
                wrs.dealusername = new List<string>();
                wrs.detail = assignremark;
                wrs.isdeal = 0;
                wrs.files = new List<FileBaseModel>();
                wrs.isdelete = 0;
                wrs.createuser = optuser;
                wrs.createusername = optusername;
                wrs.createtime = DateTime.Now;
                await _wo_repair_sendRepository.Add(wrs);

                string msg = string.IsNullOrEmpty(assignremark) ? "" : " ，备注：" + assignremark;
                string mobile = string.IsNullOrEmpty(touserinfo.mobile) ? "" : "（" + touserinfo.mobile + "）";

                AddItems(clmodel.wocode, clmodel.wostate, channel, optuser + "-" + optusername + " 指派工单给 " + assignman + "-" + touserinfo.username + mobile + msg, new List<FileBaseModel>(), optuser, optusername, "");

                //if (clmodel.channel == 4)
                //{
                //    string content = "维修人：" + assignman + "-" + touserinfo.username + "\n电话：" + touserinfo.mobile;
                //    SendWechatMsg(clmodel.wocode, "维修人员接单！", content, clmodel.id, clmodel.createuser);
                //    //SendWechatMessage(clmodel.wocode, "维修人员接单！", content, clmodel.id, clmodel.createuser);
                //}
                #region 标记消息提醒工单（消息提醒，App推送）

                var clintidlist = await _IusercoldandclintidsRepository.GetSingle(p => p.usercode == userinfo.usercode);
                if (clintidlist != null)
                {
                    PushMessageToList("通知", "您有新的工单需要处理", clintidlist.clintid);
                }
                var clintidlist1 = await _IusercoldandclintidsRepository.GetSingle(p => p.usercode == touserinfo.usercode);
                if (clintidlist1 != null)
                {
                    PushMessageToList("通知", "您有新的工单需要处理", clintidlist1.clintid);
                }

                //工单信息 工单/用户关联表 - 记录每个用户是否已读信息
                var listmap = await _wo_order_user_maprepository.Get(x => x.wocode == clmodel.wocode && x.channelcode == 1 && x.readby == assignman);
                var modelmap = listmap.FirstOrDefault();
                if (modelmap != null)
                {
                    //删除工单关系表中对应工单
                    bool boo = await _wo_order_user_maprepository.Remove(modelmap.id);
                }
                #endregion
                return Success("派单成功");
            }
            return Error("派单失败");

        }

        /// <summary>
        /// 更新处理人员
        /// </summary>
        [HttpPost("updatedealuser")]
        public async Task<IActionResult> UpdateDealUserAsync(string wocode, List<string> dealusers, int channel = 1)
        {
            if (string.IsNullOrEmpty(wocode))
            {
                return Error("操作失败");
            }
            if (dealusers == null)
            {
                dealusers = new List<string>();
            }
            if (dealusers != null && dealusers.Count() > 4)
            {
                return Error("协助人员不能超过4人");
            }

            string optuser =  User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            string optusername =  User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;

            //var userstr = _signTokenService.GetUserInfoAsync(optuser).Result;
            //var userinfo = userstr.ToJObject()["data"].ToString().ToObject<UserInfoModel>();

            
            var userinfos = _sys_user_accountRepository.GetUserInfo(optuser);
            var userinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());

            var cls = await _wo_repair_baseRepository.Get(p => p.wocode == wocode && p.isdelete == 0);
            var clmodel = cls.FirstOrDefault();
            if (clmodel != null && clmodel.wostate == (int)EnumWorkOrderState.assign && clmodel.touser.Contains(optuser))
            {
                dealusers = dealusers.Distinct().ToList();//去重
                List<string> dealusernames = new List<string>();

                string adduserinfos = string.Empty;
                string deluserinfos = string.Empty;
                var addusers = dealusers.Except(clmodel.dealuser).ToList();
                var delusers = clmodel.dealuser.Except(dealusers).ToList();
                var allusers = dealusers.Union(clmodel.dealuser).ToList();

                //foreach (var dealuser in allusers)
                foreach (var dealuser in addusers) 
                {
                    //var touserstr = _signTokenService.GetUserInfoAsync(dealuser).Result;
                    //var toresult = touserstr.ToJObject();
                    //if (toresult["state"].ToString() != "success")
                    //{
                    //    return Error(dealuser + "工号不存在");
                    //}
                    //var touserinfo = toresult["data"].ToString().ToObject<UserInfoModel>();

                    var userinfos2 = _sys_user_accountRepository.GetUserInfo(dealuser);
                    var touserinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos2.ToJson());
                    if (touserinfo == null)
                        return Error(dealuser + "工号不存在");
                    if (touserinfo.level != 1)
                    {
                        return Error(dealuser + "不是维修工");
                    }

                    if (addusers.Contains(dealuser))//新增人员
                    {
                        int limitcount = _wo_repair_baseRepository.GetLimitCount(dealuser);
                        if (limitcount > 0)
                        {
                            return Error(dealuser + "已经有" + limitcount + "个未处理工单，已超上限");
                        }
                        if (string.IsNullOrEmpty(adduserinfos))
                            adduserinfos = touserinfo.usercode + "-" + touserinfo.username;
                        else
                            adduserinfos = adduserinfos + "、" + touserinfo.usercode + "-" + touserinfo.username;
                    }
                    if (delusers.Contains(dealuser))//删除人员
                    {
                        if (string.IsNullOrEmpty(deluserinfos))
                            deluserinfos = touserinfo.usercode + "-" + touserinfo.username;
                        else
                            deluserinfos = deluserinfos + "、" + touserinfo.usercode + "-" + touserinfo.username;
                    }
                    else
                    {
                        dealusernames.Add(dealuser); 
                        //dealusernames.Add(touserinfo.username);
                    }
                }

                clmodel.dealuser = dealusers;
                clmodel.dealusername = dealusernames;
                await _wo_repair_baseRepository.UpdateOne(clmodel);

                var sends = await _wo_repair_sendRepository.Get(p => p.wocode == wocode && p.isdeal == 0 && p.isdelete == 0);
                var send = sends.OrderByDescending(p => p.createtime).FirstOrDefault();
                if (send != null)
                {
                    send.dealuser = dealusers;
                    send.dealusername = dealusernames;
                    await _wo_repair_sendRepository.UpdateOne(send);
                }
                string dealuserinfos = string.Empty;
                if (!string.IsNullOrEmpty(adduserinfos))
                    dealuserinfos = dealuserinfos + " 新增协助人员：" + adduserinfos;
                if (!string.IsNullOrEmpty(deluserinfos))
                    dealuserinfos = (string.IsNullOrEmpty(dealuserinfos) ? " " : "，") + "删除协助人员：" + deluserinfos;
                if (!string.IsNullOrEmpty(dealuserinfos))
                    AddItems(clmodel.wocode, clmodel.wostate, channel, optuser + "-" + optusername + dealuserinfos, new List<FileBaseModel>(), optuser, optusername, "");

                return Success("操作成功");
            }
            return Error("操作失败");
        }

        /// <summary>
        /// 评价
        /// </summary>
        [HttpPost("evaluate")]
        public async Task<IActionResult> UpdateEvaluateAsync(string wocode, int score, string evaluate, List<string> label, DateTime arrivetime,
            List<FileBaseModel> files, int channel = 1)
        {

            if (score <= 0)
            {
                return Error("请选择分数");
            }
            //if (arrivetime == DateTime.MinValue)
            //{
            //    return Error("请选择到场时间");
            //}

            string optuser = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            string optusername = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;
            //var userstr = _signTokenService.GetUserInfoAsync(optuser).Result;
            //var userinfo = userstr.ToJObject()["data"].ToString().ToObject<UserInfoModel>();

            var cls = await _wo_repair_baseRepository.Get(p => p.wocode == wocode && p.isdelete == 0);
          
            var clmodel = cls.FirstOrDefault();

            
            if (clmodel != null && clmodel.wostate == (int)EnumWorkOrderState.deal && clmodel.createuser==optuser)
            {
                //if (clmodel.createuser == optuser)
                //{
                clmodel.wostate = (int)EnumWorkOrderState.evaluate;
                clmodel.arrivetime = arrivetime;
                clmodel.evaluatetime = DateTime.Now;
                clmodel.score = score;
                await _wo_repair_baseRepository.UpdateOne(clmodel);

                var sends = await _wo_repair_sendRepository.Get(p => p.wocode == wocode && p.isdelete == 0);
                var send = sends.OrderByDescending(p => p.createtime).FirstOrDefault();
                send.arrivetime = arrivetime;
                await _wo_repair_sendRepository.UpdateOne(send);

                Wo_Repair_Evaluate wre = new Wo_Repair_Evaluate();
                wre.wocode = wocode;
                wre.channel = channel;
                wre.evaluate = evaluate;
                wre.label = label;
                wre.score = score;
                wre.arrivetime = arrivetime;
                wre.files = files;
                wre.createuser = optuser;
                wre.createusername = optusername;
                wre.isdelete = 0;
                wre.createtime = DateTime.Now;

                await _wo_repair_evaluateRepository.Add(wre);

                string users = optuser;
                if (channel == 4) { users = optusername; }
                AddItems(clmodel.wocode, clmodel.wostate, channel, users + " 评价工单，分数：" + score, files, optuser, optusername, "");

                if (clmodel.channel == 4)
                {
                    await SendWechatMsg(clmodel.wocode, "评价成功！", "评价内容：" + wre.evaluate, clmodel.id, clmodel.repairman);
                    //SendWechatMessage(clmodel.wocode, "评价成功！", "编号：" + clmodel.wocode + "\n评价内容：" + wre.evaluate, clmodel.id, clmodel.createuser);
                }

                return Success("评价成功");
                //}
            }

            return Error("评价失败");
        }


        /// <summary>
        /// 超时系统评价
        /// </summary>
        [AllowAnonymous]
        [HttpPost("timeoutevaluate")]
        public async Task<IActionResult> UpdateTimeOutEvaluateAsync(int score = 5, string createusername = "系统", int channel = 5)
        {
            int hour = _wo_repair_baseRepository.GetEvaluateHour();
            if (hour > 0)
            {
                //处理时间从2019-03-01 00：00：00开始，如果在时间间隔内没有评价，系统自动评价5分
                var cls = await _wo_repair_baseRepository.Get(p => p.isdelete == 0 && p.wostate == (int)EnumWorkOrderState.deal
                && p.dealtime.Value < DateTime.Now.AddHours(-hour)
                && p.dealtime.Value >= DateTime.Parse("2019-03-01 00:00:00"));
                foreach (var clmodel in cls)
                {
                    clmodel.wostate = (int)EnumWorkOrderState.evaluate;
                    clmodel.arrivetime = clmodel.dealtime;
                    clmodel.evaluatetime = DateTime.Now;
                    clmodel.score = score;
                    await _wo_repair_baseRepository.UpdateOne(clmodel);

                    var sends = await _wo_repair_sendRepository.Get(p => p.wocode == clmodel.wocode && p.isdelete == 0);
                    var send = sends.OrderByDescending(p => p.createtime).FirstOrDefault();
                    send.arrivetime = clmodel.dealtime;
                    await _wo_repair_sendRepository.UpdateOne(send);

                    Wo_Repair_Evaluate wre = new Wo_Repair_Evaluate();
                    wre.wocode = clmodel.wocode;
                    wre.channel = channel;
                    wre.evaluate = "";
                    wre.label = new List<string>();
                    wre.score = score;
                    wre.arrivetime = clmodel.dealtime;
                    wre.files = new List<FileBaseModel>();
                    wre.createuser = "";
                    wre.createusername = createusername;
                    wre.isdelete = 0;
                    wre.createtime = DateTime.Now;

                    await _wo_repair_evaluateRepository.Add(wre);

                    string users = "系统";

                    AddItems(clmodel.wocode, clmodel.wostate, channel, users + " 评价工单，分数：" + score, new List<FileBaseModel>(), "", createusername, "");
                }

                return Success("评价成功");
            }
            else
            {
                return Error("未设置时间间隔");
            }
        }

        /// <summary>
        /// 取消工单
        /// </summary>
        [HttpPost("cancle")]
        public async Task<IActionResult> UpdateCancleAsync(string wocode, int channel = 1)
        {
            string optuser = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            string optusername = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;
            //var userstr = _signTokenService.GetUserInfoAsync(optuser).Result;
            //var userinfo = userstr.ToJObject()["data"].ToString().ToObject<UserInfoModel>();

            
            var userinfos = _sys_user_accountRepository.GetUserInfo(optuser);
            var userinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());

            var cls = await _wo_repair_baseRepository.Get(p => p.wocode == wocode && p.isdelete == 0);
            var clmodel = cls.FirstOrDefault();
            if (clmodel != null && clmodel.createuser == optuser && clmodel.wostate == (int)EnumWorkOrderState.create)
            {
                clmodel.wostate = (int)EnumWorkOrderState.cancle;
                clmodel.canceltime = DateTime.Now;

                var res = await _wo_repair_baseRepository.UpdateOne(clmodel);
                if (res)
                {
                    string users = optuser;
                    if (channel == 4) { users = optusername; }
                    AddItems(clmodel.wocode, clmodel.wostate, channel, users + " 取消工单", new List<FileBaseModel>(), optuser, optusername, "");
                    if (clmodel.channel == 4)
                    {
                        await SendWechatMsg(clmodel.wocode, "取消成功！", "", clmodel.id, clmodel.createuser);
                        //SendWechatMessage(clmodel.wocode, "取消成功！", "编号：" + clmodel.wocode , clmodel.id, clmodel.createuser);
                    }
                    return Success("取消工单成功");
                }
            }
            return Error("取消工单失败");
        }

        /// <summary>
        /// 删除工单
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("delete")]
        public async Task<IActionResult> RemoveAsync(string[] ids, int channel = 1)
        {
            string optuser = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            string optusername = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;
            //var userstr = _signTokenService.GetUserInfoAsync(optuser).Result;
            //var userinfo = userstr.ToJObject()["data"].ToString().ToObject<UserInfoModel>();

            
            var userinfos = _sys_user_accountRepository.GetUserInfo(optuser);
            var userinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());

            //使用逻辑删除
            //物理删除的数据无法恢复
            var res = 0;
            if (ids != null && ids.Length > 0)
            {
                foreach (var id in ids)
                {
                    var ml = await _wo_repair_baseRepository.GetSingle(id);
                    ml.isdelete = 1;
                    ml.deleteuser = optuser;
                    ml.deletetime = DateTime.Now;
                    if (await _wo_repair_baseRepository.UpdateOne(ml))
                    {
                        #region 删除相关表数据
                        var sends = await _wo_repair_sendRepository.Get(p => p.wocode == ml.wocode && p.isdelete == 0);
                        foreach (var send in sends)
                        {
                            send.isdelete = 1;
                            send.deleteuser = optuser;
                            send.deletetime = DateTime.Now;
                            await _wo_repair_sendRepository.UpdateOne(send);
                        }
                        var deals = await _wo_repair_dealRepository.Get(p => p.wocode == ml.wocode && p.isdelete == 0);
                        foreach (var deal in deals)
                        {
                            deal.isdelete = 1;
                            deal.deleteuser = optuser;
                            deal.deletetime = DateTime.Now;
                            await _wo_repair_dealRepository.UpdateOne(deal);
                        }
                        var evaluates = await _wo_repair_evaluateRepository.Get(p => p.wocode == ml.wocode && p.isdelete == 0);
                        foreach (var evaluate in evaluates)
                        {
                            evaluate.isdelete = 1;
                            evaluate.deleteuser = optuser;
                            evaluate.deletetime = DateTime.Now;
                            await _wo_repair_evaluateRepository.UpdateOne(evaluate);
                        }
                        var items = await _wo_repair_itemsRepository.Get(p => p.wocode == ml.wocode && p.isdelete == 0);
                        foreach (var item in items)
                        {
                            item.isdelete = 1;
                            item.deleteuser = optuser;
                            item.deletetime = DateTime.Now;
                            await _wo_repair_itemsRepository.UpdateOne(item);
                        }
                        #endregion

                        res += 1;
                        AddItems(ml.wocode, ml.wostate, channel, optuser + " 删除工单", new List<FileBaseModel>(), optuser, optusername, "");
                    }
                }
                if (res == ids.Length)
                    return Success("工单删除成功");
                else if (res > 0 && res < ids.Length)
                    return Error("部分工单删除失败");
                else
                    return Error("工单删除失败");
            }
            else
                return Error("请选择要删除的记录");
        }

        /// <summary>
        /// 获取当前维修工有多少未处理工单
        /// </summary>
        [HttpPost("getservicecount")]
        public async Task<IActionResult> GetServiceCount(string serviceman, string wocode)
        {
            string optuser = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;

            //var userstr = _signTokenService.GetUserInfoAsync(optuser).Result;
            //var userinfo = userstr.ToJObject()["data"].ToString().ToObject<UserInfoModel>();

            
            var userinfos = _sys_user_accountRepository.GetUserInfo(optuser);
            var userinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());

            var flist = new List<FilterDefinition<Wo_Repair_Base>>();
            flist.Add(Builders<Wo_Repair_Base>.Filter.Eq("isdelete", false));
            flist.Add(Builders<Wo_Repair_Base>.Filter.Where(s => s.wostate == 1));
            flist.Add(Builders<Wo_Repair_Base>.Filter.Eq("touser", serviceman));
            if (!string.IsNullOrEmpty(wocode))
            {
                flist.Add(Builders<Wo_Repair_Base>.Filter.Lt("wocode", wocode));
            }
            var count = await _wo_repair_baseRepository.CountAsync(Builders<Wo_Repair_Base>.Filter.And(flist));
            if (count > 0)
                return Success("获取未处理工单量成功", count);
            else
                return Success("获取未处理工单量成功", 0);
        }

        /// <summary>
        /// 获取维修工
        /// </summary>
        [HttpGet("getrepairuser")]
        public IActionResult GetRepairUser(string wocode)
        {
            string optuser = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;

            //var userstr = _signTokenService.GetUserInfoAsync(optuser).Result;
            //var userinfo = userstr.ToJObject()["data"].ToString().ToObject<UserInfoModel>();

            
            var userinfos = _sys_user_accountRepository.GetUserInfo(optuser);
            var userinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());

            var userrolename = userinfo.role_name;

            var result = _wo_repair_baseRepository.GetRepairUser(wocode, userrolename);
            return Success("获取成功", result);
        }

        /// <summary>
        /// 获取协助维修工
        /// </summary>
        [HttpGet("getassistrepairuser")]
        public IActionResult GetAssistRepairUser(string projectid, string usercode, int isxyzg = 0)
        {
            var result = _wo_repair_baseRepository.GetAssistRepairUser(projectid, usercode, isxyzg);
            return Success("获取成功", result);
        }

        /// <summary>
        /// 添加工单记录
        /// </summary>
        /// <param name="wocode">工单编号</param>
        /// <param name="wostate">工单状态</param>
        /// <param name="channel">渠道来源 1表示PC 2表示安卓 3表示IOS 4表示微信</param>
        /// <param name="islast">是否最后一步 0否 1是</param>
        /// <param name="touser">指派下一步人员</param>
        /// <param name="todept">指派下一步部门</param>
        /// <param name="detail">处理说明</param>
        /// <param name="files">附件</param>
        /// <param name="optuser">当前操作人</param>
        /// <param name="callid"></param>
        private async void AddItems(string wocode, int wostate, int channel, string detail, List<FileBaseModel> files, string optuser, string optusername, string callid)
        {
            Wo_Repair_Items itemsmodel = new Wo_Repair_Items();
            itemsmodel.wocode = wocode;
            itemsmodel.wostate = wostate;
            itemsmodel.channel = channel;
            itemsmodel.callid = callid;
            itemsmodel.isdelete = 0;
            itemsmodel.createuser = optuser;
            itemsmodel.createusername = optusername;
            itemsmodel.createtime = DateTime.Now;
            itemsmodel.detail = detail;
            itemsmodel.files = files;
            await _wo_repair_itemsRepository.Add(itemsmodel);
        }

        /// <summary>
        /// 发送微信模板消息
        /// </summary>
        /// <param name="wocode">工单编号</param>
        /// <param name="title">标题</param>
        /// <param name="content">内容</param>
        /// <param name="url">链接地址</param>
        /// <param name="openid">接收方</param>
        /// <param name="templateid">模板id</param>
        private async Task SendWechatMsg(string wocode, string title, string content, string url, string openid, string templateid = "")
        {
            url = "menucode=CKXQ&id=" + url;
            var obj = new
            {
                first = new { value = title, color = "#173177" },
                keyword1 = new { value = wocode },
                keyword2 = new { value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
                remark = new { value = content }
            };
            await _wechatservice.SendTemplateMsg(openid, url, obj.ToJson(), templateid);
        }

        /// <summary>
        /// 发送微信消息
        /// </summary>
        /// <param name="wocode">工单编号</param>
        /// <param name="title">标题</param>
        /// <param name="content">内容</param>
        /// <param name="url">链接地址</param>
        /// <param name="openid">接收方</param>
        private async Task SendWechatMessage(string wocode, string title, string content, string url, string openid)
        {
            url = "menucode=CKXQ&id=" + url;
            content = content + "\n时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            await _wechatservice.SendNewsMsg(openid, title, content, url);
        }

        /// <summary>
        /// app推送
        /// </summary>
        /// <param name="Title"></param>
        /// <param name="count"></param>
        /// <param name="CLIENTID"></param>
        /// <returns></returns>
        private string PushMessageToList(string Title, string count, string CLIENTID)
        {
            //参数设置 <-----参数需要重新设置----->
            //http的域名
            string HOST = "http://sdk.open.api.igexin.com/apiex.htm";

            //https的域名
            //private static String HOST = "https://api.getui.com/apiex.htm";

            //定义常量, appId、appKey、masterSecret 采用本文档 "第二步 获取访问凭证 "中获得的应用配置
            string APPID = _configuration["PushMessageOptions:APPID"].ToString();// Configs.GetValue("APPID");
            string APPKEY = _configuration["PushMessageOptions:APPKEY"].ToString();
            string MASTERSECRET = _configuration["PushMessageOptions:MASTERSECRET"].ToString();
            // 推送主类（方式1，不可与方式2共存）
            IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET);
            // 推送主类（方式2，不可与方式1共存）此方式可通过获取服务端地址列表判断最快域名后进行消息推送，每10分钟检查一次最快域名
            //  IGtPush push = new IGtPush("",APPKEY,MASTERSECRET);
            ListMessage message = new ListMessage();
            string state = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string end = DateTime.Now.AddMinutes(7).ToString("yyyy-MM-dd HH:mm:ss");
            NotificationTemplate template = NotificationTemplateDemo(Title, count, state, end);
            // 用户当前不在线时，是否离线存储,可选
            message.IsOffline = true;
            // 离线有效时间，单位为毫秒，可选
            message.OfflineExpireTime = 1000 * 3600 * 12;
            message.Data = template;
            message.PushNetWorkType = 0;        //判断是否客户端是否wifi环境下推送，1为在WIFI环境下，0为不限制网络环境。
            //设置接收者
            List<com.igetui.api.openservice.igetui.Target> targetList = new List<com.igetui.api.openservice.igetui.Target>();
            com.igetui.api.openservice.igetui.Target target1 = new com.igetui.api.openservice.igetui.Target();
            target1.appId = APPID;
            target1.clientId = CLIENTID;


            // 如需要，可以设置多个接收者
            //com.igetui.api.openservice.igetui.Target target2 = new com.igetui.api.openservice.igetui.Target();
            //target2.AppId = APPID;
            //target2.ClientId = "ddf730f6cabfa02ebabf06e0c7fc8da0";

            targetList.Add(target1);
            //targetList.Add(target2);

            string contentId = push.getContentId(message);
            string pushResult = push.pushMessageToList(contentId, targetList);

            return pushResult; ;
        }

        public NotificationTemplate NotificationTemplateDemo(string Title, string count, string state, string endtime)
        {
            string APPID = _configuration["PushMessageOptions:APPID"].ToString();// Configs.GetValue("APPID");
            string APPKEY = _configuration["PushMessageOptions:APPKEY"].ToString();
            NotificationTemplate template = new NotificationTemplate();
            template.AppId = APPID;
            template.AppKey = APPKEY;
            //通知栏标题
            template.Title = Title;
            //通知栏内容     
            template.Text = count;
            //通知栏显示本地图片
            template.Logo = "";
            //通知栏显示网络图标
            template.LogoURL = "";
            //应用启动类型，1：强制应用启动  2：等待应用启动
            template.TransmissionType = "1";
            //透传内容  
            template.TransmissionContent = "";
            //接收到消息是否响铃，true：响铃 false：不响铃   
            template.IsRing = true;
            //接收到消息是否震动，true：震动 false：不震动   
            template.IsVibrate = true;
            //接收到消息是否可清除，true：可清除 false：不可清除    
            template.IsClearable = true;
            //设置通知定时展示时间，结束时间与开始时间相差需大于6分钟，消息推送后，客户端将在指定时间差内展示消息（误差6分钟）
            String begin = state;
            String end = endtime;
            template.setDuration(begin, end);

            return template;
        }
        /// <summary>
        /// 获取当天、本周、本月、本年、累计时间
        /// </summary>
        /// <param name="datetype"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        public void GetStartEndTime(int datetype, out string start, out string end)
        {
            start = string.Empty;
            end = string.Empty;

            var datenow = DateTime.Now;
            var datestart = datenow;
            var dateend = datenow;
            switch (datetype)
            {
                case 0://当天
                    break;
                case 1://本周
                    int n = (int)datenow.DayOfWeek == 0 ? 7 : (int)datenow.DayOfWeek;
                    datestart = datenow.AddDays(1 - n);//本周周一
                    dateend = datestart.AddDays(6);//本周周日
                    break;
                case 2://本月

                    datestart = datenow.AddDays(1 - datenow.Day);  //本月月初  
                    dateend = datestart.AddMonths(1).AddDays(-1);  //本月月末  
                    break;
                case 3://本年
                    datestart = new DateTime(datenow.Year, 1, 1);  //本年年初  
                    dateend = new DateTime(datenow.Year, 12, 31);  //本年年末  
                    break;
                case 4://全部
                    break;
                default:
                    break;
            }
            if (datetype != 4)
            {
                start = datestart.ToString("yyyy-MM-dd");
                end = dateend.ToString("yyyy-MM-dd");
            }
        }
        #endregion
        #region 维修工单时效模块
        /// <summary>
        /// 获取所有带班的角色班组信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("getallbzcount")]
        public async Task<IActionResult> Getgetallbzcount()
        {
            var model = await _ISys_Role_InfoRepository.Get(p => p.role_name.Contains("班")&&p.role_code=="YWYG" && p.state_flag == 1);
            return Success("成功", model);
        }

        /// <summary>
        /// 添加维修项目时效类型信息表
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("addwxwptimes")]
        public async Task<IActionResult> Addaddwxwptimes(Model.Input.wxwpstimeinput tps)
        {
            wxwpstime tp = new wxwpstime();
            tp.wxcenter = tps.wxcenter;//维修项目
            tp.timecount = tps.timecount;//时长
            tp.bzid = tps.bzid;//班组编号
            tp.isdelete = 0;// 是否删除
            var a = _wxwpstimeRepository.AddRetID(tp);
            if (a.Result != null || a.Result != "")
            {
                return Success("成功", a.Result);
            }
            else
            {
                return Error("失败");
            }
        }
        /// <summary>
        /// 根据id获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("getwxwptimesbyid")]
        public async Task<IActionResult> Getwxwptimesbyid(string id)
        {
            var model = await _wxwpstimeRepository.Get(p => p.id == id);
            return Success("成功", model);
        }

        /// <summary>
        /// 修改维修项目信息
        /// </summary>
        /// <param name="tps"></param>
        /// <returns></returns>
        [HttpGet("upwxwptimestabs")]
        public async Task<IActionResult> upwxwptimestabs(Model.Input.wxwpstimeinput tps)
        {
            wxwpstime tp = new wxwpstime();
            tp.id = tps.id;
            tp.wxcenter = tps.wxcenter;//维修项目
            tp.timecount = tps.timecount;//时长
            tp.bzid = tps.bzid;//班组编号
            tp.isdelete = 0;// 是否删除
            var resua = _wxwpstimeRepository.Update(tp);
            if (resua.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }
        /// <summary>
        /// 删除维修项目信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("delewxwptimestabs")]
        public async Task<IActionResult> delewxwptimestabs(string id)
        {
            var result = _wxwpstimeRepository.Remove(id);
            if (result.Result)
            {
                return Success("成功");
            }
            else
            {
                return Error("失败");
            }

        }

        /// <summary>
        /// 获取维修项目信息
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpGet("getwxwptimestabs")]
        public async Task<IActionResult> Getwxwptimestabs(int PageIndex, int PageSize,string bzid)
        {
            //排序字段
            //var sort = Builders<Fxdjtabs>.Sort.Ascending("Szzydjsj");
            //根据条件查询集合
            var listApp = new List<FilterDefinition<wxwpstime>>();
            listApp.Add(Builders<wxwpstime>.Filter.Eq("isdelete", 0));

            if (bzid != null && bzid != "")
            {
                listApp.Add(Builders<wxwpstime>.Filter.Eq("bzid", bzid));
            }

            var filter = Builders<wxwpstime>.Filter.And(listApp);

            var list = await _wxwpstimeRepository.GetByPage(filter, PageIndex, PageSize);
            var redCount = await _wxwpstimeRepository.CountAsync(filter);   //获取总数
            var listmodel = new List<wxwpstime>();
            foreach (var item in list)
            {
                Sys_Role_Info  roleinfo = await _ISys_Role_InfoRepository.GetSingle(p => p.id == item.bzid);//获取班组信息时长
                ///定义选项
                var model = new wxwpstime();
                model.id = item.id;
                model.wxcenter = item.wxcenter;//维修项目
                model.timecount = item.timecount;//时长
                model.bzid = item.bzid;//班组编号
                if (roleinfo != null)
                {
                    model.bzname = roleinfo.role_name;//班组名称
                }
                listmodel.Add(model);
            }
            var obj = new
            {
                rows = listmodel,
                total = redCount
            };
            return Success("成功", obj);
        }

        ///根据当前登陆人所属班组
        [HttpGet("getwxwptimesbybz")]
        public async Task<IActionResult> Getwxwptimesbybz(string id)
        {
            ///获取当前登录人信息
            string optuser =  User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            
            var userinfos = _sys_user_accountRepository.GetUserInfo(optuser);
            var userinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());

            var model = await _wxwpstimeRepository.Get(p => p.bzid == userinfo.role_id);
            return Success("成功", model);
        }
        #endregion

        public async  Task<IActionResult> assignbz(string wocode, string assignman, string assignremark, int channel = 1)
        {
            if (string.IsNullOrEmpty(assignman))
            {
                return Error("请选择维修班组");
            }
            string optuser = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            string optusername = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;
            var cls = await _wo_repair_baseRepository.Get(p => p.wocode == wocode && p.isdelete == 0);
            var clmodel = cls.FirstOrDefault();
            if (optuser == "8000" || clmodel.createuser == optuser || optuser == "8008")
            {
            }
            else
            {
                return Error("这不是你提交的报修工单，无法进行派单操作");
            }

            if (clmodel != null && (clmodel.wostate == (int)EnumWorkOrderState.create || clmodel.wostate == (int)EnumWorkOrderState.assign || clmodel.wostate == (int)EnumWorkOrderState.hangup))
            {
                clmodel.wostate = 0;
                clmodel.totype = 1;
                clmodel.touser = new List<string>();
                clmodel.tousername = new List<string>();
                clmodel.tobztimes = DateTime.Now;

                clmodel.dealuser = new List<string>();
                clmodel.dealusername = new List<string>();
                ///判断是否派过班组
                if (clmodel.Jwocode != null && clmodel.Jwocode != assignman && (clmodel.Pwcode == null || clmodel.Pwcode == ""))
                {
                    clmodel.Pwcode = clmodel.Jwocode;
                }
                clmodel.Jwocode = assignman;//接单单班组科室
                string zdusercode = "";
                if (assignman != "后勤部") //自动派单
                {
                    //获取当前工单的科室名称
                    var index = clmodel.repairreplace.LastIndexOf(' ');
                    string keshiname = clmodel.repairreplace.Substring(index + 1, clmodel.repairreplace.Length - index - 1);
                    string grouptype = "";
                    string pbshijian = DateTime.Now.ToString("yyyy-MM-dd");
                    #region 判断当前的时间是白班还是夜班
                    var stime = _configuration["watchtime:stime"].ToString();
                    var etime = _configuration["watchtime:etime"].ToString();

                    DateTime startdt = DateTime.Parse(stime);
                    DateTime enddt = DateTime.Parse(etime);
                    int startdtH = startdt.Hour;
                    int enddth = enddt.Hour;

                    int startdtm = startdt.Minute;
                    int enddtm = enddt.Minute;
                    int currenthour = DateTime.Now.Hour;
                    int currentmin = DateTime.Now.Minute;
                    //假设stime 08:30 etime 17:30
                    if (currenthour > startdtH && currenthour < enddth)
                    {
                        //当前时间大于开始的小时小于结束的小时
                        //baiban
                        grouptype = "白班";
                    }
                    else if (currenthour == startdtH && currentmin >= startdtm)
                    {
                        grouptype = "白班";
                    }
                    else if (currenthour == enddth && currentmin <= enddtm)
                    {
                        grouptype = "白班";
                    }
                    else
                    {
                        grouptype = "夜班";
                    }
                    #endregion



                    #region 白班的时候根据科室名找到对应的人
                    if (grouptype == "白班")
                    {
                        var list = _sys_work_datgroupRepository.GetPBUserList(grouptype, keshiname, "").ToList();
                        if (list.Count > 0)
                        {
                            var pbuser = list.First();
                            var pbusermodel = JsonHelper.JsonToEntity<Sys_Work_DayGroup>(pbuser.ToJson());
                            zdusercode = pbusermodel.usercode;
                        }
                        else
                        {
                            return Error("先添加值班信息");
                        }
                    }
                    //夜班的时候根据当前日期找到对应的人
                    if (grouptype == "夜班")
                    {
                        var list = _sys_work_datgroupRepository.GetPBUserList(grouptype, "", pbshijian).ToList();
                        if (list.Count > 0)
                        {
                            var pbuser = list.First();
                            var pbusermodel = JsonHelper.JsonToEntity<Sys_Work_DayGroup>(pbuser.ToJson());
                            zdusercode = pbusermodel.usercode;
                        }
                        else
                        {
                            return Error("先添加值班信息");
                        }

                    }
                    #endregion


                    
                    var userinfos = _sys_user_accountRepository.GetUserInfo(zdusercode);
                    var touserinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());
                    if (touserinfo.level != 1)
                    {
                        return Error(assignman + "不是维修工");
                    }

                    var userinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());

                    clmodel.touser = new List<string>() { zdusercode };
                    clmodel.tousername = new List<string>() { touserinfo.username };
                    clmodel.wostate = (int)EnumWorkOrderState.assign;
                    clmodel.totime = DateTime.Now;

                    Wo_Repair_Send wrs = new Wo_Repair_Send();
                    wrs.wocode = clmodel.wocode;
                    wrs.wostate = clmodel.wostate;
                    wrs.channel = channel;
                    wrs.totype = 1;
                    wrs.touser = new List<string>() { zdusercode };
                    wrs.tousername = new List<string>() { touserinfo.username };
                    wrs.dealuser = new List<string>();
                    wrs.dealusername = new List<string>();
                    wrs.detail = assignremark;
                    wrs.isdeal = 0;
                    wrs.files = new List<FileBaseModel>();
                    wrs.isdelete = 0;
                    wrs.createuser = optuser;
                    wrs.createusername = optusername;
                    wrs.createtime = DateTime.Now;
                    await _wo_repair_sendRepository.Add(wrs);
                }

                await _wo_repair_baseRepository.UpdateOne(clmodel);


                //定义一个表 添加一条数据进行提醒
                if (assignman == "后勤部") //自动派单
                {
                    infotxs infotx = await _infotxsRepository.GetSingle(p => p.wocode == wocode);
                    if (infotx != null)
                    {
                        infotx.istxinfo = 0;
                        await _infotxsRepository.UpdateOne(infotx);
                    }
                    else
                    {
                        infotxs intxinfo = new infotxs();
                        intxinfo.zpusern = optusername;
                        intxinfo.jsuser = assignman;
                        intxinfo.wocode = wocode;
                        intxinfo.istxinfo = 0;
                        intxinfo.isdelete = 0;
                        await _infotxsRepository.Add(intxinfo);
                    }
                }
                string msg = string.IsNullOrEmpty(assignremark) ? "" : " ，备注：" + assignremark;
                if (assignman != "后勤部") //自动派单
                {
                    
                    var userinfos = _sys_user_accountRepository.GetUserInfo(zdusercode);
                    var touserinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());
                    AddItems(clmodel.wocode, clmodel.wostate, channel, optuser + "-" + optusername + " 指派工单给 " + assignman + "-" + zdusercode + "-" + touserinfo.username + msg, new List<FileBaseModel>(), optuser, optusername, "");
                }
                else
                {
                    AddItems(clmodel.wocode, clmodel.wostate, channel, optuser + "-" + optusername + " 指派工单给 " + assignman + msg, new List<FileBaseModel>(), optuser, optusername, "");
                }
                if (assignman == "后勤部") //自动派单
                {
                    //根据接单班组名称获取班组账号的code
                    var usercoudemode = await _sys_user_accountRepository.GetSingle(p => p.username == assignman);
                    if (usercoudemode != null)
                    {
                        var clintidlist1 = await _IusercoldandclintidsRepository.GetSingle(p => p.usercode == usercoudemode.usercode);
                        if (clintidlist1 != null)
                        {
                            PushMessageToList("通知", "您有新的工单需要处理", clintidlist1.clintid);
                        }
                    }
                }
                return Success("派单成功");
            }
            return Error("派单失败");

        }
    }
}