KQJob.java 27.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
package com.sincere.quartz.job;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sincere.common.dto.smartCampus.*;
import com.sincere.common.enums.PushTypeEnums;
import com.sincere.common.util.DateUtils;
import com.sincere.common.util.HttpClientUtils;
import com.sincere.quartz.enums.KqTypeEnums;
import com.sincere.quartz.enums.TypeEnums;
import com.sincere.quartz.feign.ScFeign;
import com.sincere.quartz.model.DingSms;
import com.sincere.quartz.model.ShortMsg;
import com.sincere.quartz.model.WeChatSms;
import com.sincere.quartz.service.ManagerService;
import com.sincere.quartz.service.SmsService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * 考勤任务推送调用
 * 主要推送盯盯以及企业号
 * @author chen
 * @version 1.0
 * @date 2019/11/27 0027 17:05
 */
@Service
public class KQJob {

    private Logger logger = LoggerFactory.getLogger(KQJob.class);

    @Autowired
    ScFeign scFeign ;

    @Autowired
    SmsService smsService;

    @Autowired
    ManagerService managerService ;

    private static String iotUrl = "http://60.190.202.57:8035";

    private static String date ;
    private static Map<String , String> map = new HashMap<>();   //学校 考勤推送map

    private static String alarmDate ;
    private static Map<String , String> alarmMap = new HashMap<>();   //考勤告警推送map
    private static Map<String , BigDecimal> templateMap = new HashMap<>(); // 考勤比例

    private static List<String> opexList ;
    private static String integration ;

    static{
        opexList = new ArrayList<>();
        opexList.add("15857566147");  //王汉栋
        opexList.add("13858485977");  //王楠彬

        integration = "18767117554" ;  //林炜
    }

    @Scheduled(cron = "0 0-59 * * * ? ")
    public void kaoQing() {
         String now = DateUtils.date2String(new Date(),DateUtils.format1) ;
        if(StringUtils.isBlank(date)){
            initMap();
            date = now ;
        }
        if(!DateUtils.date2String(new Date(),DateUtils.format1).equals(date)){
            initMap();
            date = DateUtils.date2String(new Date(),DateUtils.format1) ;
        }
        //开始过滤数据  推送
        List<String> keyList = new ArrayList<>();
        for(Map.Entry<String, String> entity : map.entrySet()){
            String endTime = entity.getValue().split("_")[1];
            if(DateUtils.getDateDifference(new Date(),DateUtils.string2Date(now+" "+endTime+":00",DateUtils.format2),"m")>=2
                    && DateUtils.getDateDifference(new Date(),DateUtils.string2Date(now+" "+endTime+":00",DateUtils.format2),"m")<5){
                String key = entity.getKey();
                String[] messageArray = key.split("_");
                if(messageArray[2].contains(PushTypeEnums.QIYEHAO.getType()+"")){
                    logger.info("企业号推送:" + messageArray[0] + "__" + messageArray[1]);
                    weChatBatchPush(messageArray[0],Integer.valueOf(messageArray[1]),entity.getValue());
                }
                if(messageArray[2].contains(PushTypeEnums.DING.getType()+"")){
                    logger.info(("盯盯推送:" + messageArray[0] + "__" + messageArray[1]));
                    dingBatchPush(messageArray[0],Integer.valueOf(messageArray[1]),entity.getValue());
                }
                keyList.add(key);
            }
        }
        for(String key : keyList){
            map.remove(key);
        }
    }

    @Scheduled(cron = "0 0-59 * * * ? ")
    public void alarmKaoQing() {
        String now = DateUtils.date2String(new Date(),DateUtils.format1) ;
        if(StringUtils.isBlank(alarmDate)){
            initAlarmMap();
            alarmDate = now ;
        }
        if(!DateUtils.date2String(new Date(),DateUtils.format1).equals(alarmDate)){
            initAlarmMap();
            alarmDate = DateUtils.date2String(new Date(),DateUtils.format1) ;
        }
        //开始过滤数据  推送
        List<String> keyList = new ArrayList<>();
        for(Map.Entry<String, String> entity : alarmMap.entrySet()){
            String endTime = entity.getValue().split("_")[1];
            if(DateUtils.getDateDifference(new Date(),DateUtils.string2Date(now+" "+endTime+":00",DateUtils.format2),"m")>=2
                    ){
                String key = entity.getKey();
                keyList.add(key);
                //alarm(entity);  //告警
                //insertIOT(entity); //iot
            }
        }
        for(String key : keyList){
            map.remove(key);
        }
    }

    private void initAlarmMap(){
        alarmMap = new HashMap<>();
        List<TemplateDto> list = scFeign.getAllTemplateAlarm();
        logger.info(("------考勤告警模板------"));
        for(TemplateDto templateDto : list){
            String config = templateDto.getConfig();
            String[] array = config.split("<Template");
            for(int i = 1 ; i<array.length ;i++){
                try{
                    String msg = array[i];
                    String beginTime = msg.substring(msg.indexOf("BeginTime")+11,msg.indexOf("BeginTime")+16);
                    String endTime = msg.substring(msg.indexOf("EndTime")+9,msg.indexOf("EndTime")+14);
                    String templateId = msg.substring(msg.indexOf("TemplateID")+12,msg.indexOf("TemplateID")+22);
                    String Week = msg.substring(msg.indexOf("Week")+6,msg.indexOf("Week")+19);
                    String type = msg.substring(msg.indexOf("Type")+6,msg.indexOf("Type")+8);
                    type = type.replace("\"","");
                    int nowWeek = DateUtils.getWeek() ;
                    if(Week.contains(nowWeek+"") && nowWeek != 6 && nowWeek != 0 ){  //周末不告警
                        logger.info((templateId+"_"+templateDto.getSchoolId()+"------"+beginTime+"_"+endTime));
                        alarmMap.put(templateId+"_"+templateDto.getSchoolId()+"_"+templateDto.getTitle() , beginTime+"_"+endTime+"_"+type+"_"+templateDto.getId());
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

    //告警推送
    private void alarm(Map.Entry<String,String> entry){
        String templateId = entry.getKey().split("_")[0] ;
        String schoolId = entry.getKey().split("_")[1] ;
        String id = entry.getValue().split("_")[3];
        String type = entry.getValue().split("_")[2] ;

        if(DateUtils.getWeek() == 2 || DateUtils.getWeek() == 5){
            //可以相差40%
            BigDecimal percent = getAlarmCensus(Integer.valueOf(id),templateId,Integer.valueOf(schoolId),Integer.valueOf(type));
            if(templateMap.get(templateId) != null){
                if(percent.subtract(templateMap.get(templateId)).compareTo(new BigDecimal(0.4)) > 0
                    || templateMap.get(templateId).subtract(percent).compareTo(new BigDecimal(0.4)) > 0) {
                    alarmPush(Integer.valueOf(schoolId));
                }
            }
            templateMap.put(templateId,percent);
        }else {
            //相差30%
            BigDecimal percent = getAlarmCensus(Integer.valueOf(id),templateId,Integer.valueOf(schoolId),Integer.valueOf(type));
            if(templateMap.get(templateId) != null){
                if(percent.subtract(templateMap.get(templateId)).compareTo(new BigDecimal(0.3)) > 0
                        || templateMap.get(templateId).subtract(percent).compareTo(new BigDecimal(0.3)) > 0) {
                    alarmPush(Integer.valueOf(schoolId));
                }
            }
            templateMap.put(templateId,percent);
        }
    }

    private void insertIOT(Map.Entry<String,String> entry){
        String templateId = entry.getKey().split("_")[0] ;
        String schoolId = entry.getKey().split("_")[1] ;
        String title = entry.getKey().split("_")[2] ;
        String id = entry.getValue().split("_")[3];
        String type = entry.getValue().split("_")[2] ;
        String beginTime = entry.getValue().split("_")[0] ;
        String endTime = entry.getValue().split("_")[1] ;

        List<Integer> list = getIOTCensus(Integer.valueOf(id),templateId,Integer.valueOf(schoolId),Integer.valueOf(type)) ;
        JSONObject object = new JSONObject();
        object.put("strTime",beginTime);
        object.put("endTime",endTime);
        object.put("inTime",DateUtils.date2String(new Date(),DateUtils.format1));
        object.put("noAttendanceCount",list.get(1));
        object.put("attendanceCount",list.get(0));
        object.put("leaveCount",list.get(2));
        object.put("templateName",title);
        object.put("qianDaoIdId",id);
        object.put("schoolId",schoolId);
        object.put("templateId",templateId);
        HttpClientUtils.httpPostJson(iotUrl+"/api/Association/addIntelligenceAttendance",object.toJSONString());
    }

    //初始化要推送的模板
    private void initMap(){
        map = new HashMap<>();
        List<TemplateDto> list = scFeign.getAllTemplate();
        logger.info(("------需要推送的考勤模板------"));
        for(TemplateDto templateDto : list){
            String config = templateDto.getConfig();
            String[] array = config.split("<Template");
            for(int i = 1 ; i<array.length ;i++){
                try{
                    String msg = array[i];
                    String beginTime = msg.substring(msg.indexOf("BeginTime")+11,msg.indexOf("BeginTime")+16);
                    String endTime = msg.substring(msg.indexOf("EndTime")+9,msg.indexOf("EndTime")+14);
                    String templateId = msg.substring(msg.indexOf("TemplateID")+12,msg.indexOf("TemplateID")+22);
                    String Week = msg.substring(msg.indexOf("Week")+6,msg.indexOf("Week")+19);
                    String type = msg.substring(msg.indexOf("Type")+6,msg.indexOf("Type")+8);
                    type = type.replace("\"","");
                    int nowWeek = DateUtils.getWeek() ;
                    if(Week.contains(nowWeek+"")){
                        logger.info((templateId+"_"+templateDto.getSchoolId()+"_"+templateDto.getType()+"------"+beginTime+"_"+endTime));
                        map.put(templateId+"_"+templateDto.getSchoolId()+"_"+templateDto.getType() , beginTime+"_"+endTime+"_"+type+"_"+templateDto.getId());
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

    //盯盯批量推送
    private void dingBatchPush(String templateId , int schoolId , String key){
        String[] times = key.split("_");
        AppDto appDto = scFeign.getApp(schoolId,1);
        if(Integer.valueOf(times[2])  < 7){
            //出入校   推班主任
            List<KqTeacherDto> list = scFeign.getTeacherList(schoolId);
            for(KqTeacherDto teacher : list){
                 dingSchoolPush(templateId,teacher,key,appDto,0);
            }
        }else {
            //出入寝  推班主任
            List<KqTeacherDto> list = scFeign.getTeacherList(schoolId);
            for(KqTeacherDto teacher : list){
                dingSchoolPush(templateId,teacher, key, appDto,1);
            }
            //推宿管
            List<KqTeacherDto> chamberList = scFeign.selectChamberTeacher(schoolId);
            for(KqTeacherDto teacher : chamberList){
                dingChamberPush(templateId,teacher, key, appDto);
            }
        }
    }

    //企业号批量推送
    private void weChatBatchPush(String templateId , int schoolId , String key){
        String[] times = key.split("_");
        AppDto appDto = scFeign.getApp(schoolId, 0);
        if(Integer.valueOf(times[2])  < 7) {
            //出入校   推班主任
            List<KqTeacherDto> list = scFeign.getTeacherList(schoolId);
            for (KqTeacherDto teacher : list) {
                weChatSchoolPush(templateId,teacher, key, appDto,0);
            }
        }else {
            //出入寝  推班主任
            List<KqTeacherDto> list = scFeign.getTeacherList(schoolId);
            for (KqTeacherDto teacher : list) {
                weChatSchoolPush(templateId,teacher, key, appDto,1);
            }
            //推宿管
            List<KqTeacherDto> chamberList = scFeign.selectChamberTeacher(schoolId);
            for(KqTeacherDto teacher : chamberList){
                weChatChamberPush(templateId,teacher, key, appDto);
            }
        }
    }

    //盯盯推送班主任
    private void dingSchoolPush(String templateId ,KqTeacherDto teacher , String key , AppDto appDto , int type){
        String[] times = key.split("_");
        String tableSuffix = DateUtils.date2String(new Date(), DateUtils.format);
        String thirdOpenId = scFeign.getThirdId(teacher.getUserId(),0);
        if(StringUtils.isNotBlank(thirdOpenId)){
            List<CensusKqDto> censusKqDtos = getCensus(Integer.valueOf(times[3]),templateId,teacher.getSchoolId(),teacher.getUserId(),type);
            if(censusKqDtos != null &&censusKqDtos.size() > 0){
                String message = "";
                for(CensusKqDto censusKqDto : censusKqDtos){
                    message += "[" + censusKqDto.getTargetName() + "]未考勤" + censusKqDto.getNotAttendCount() + "人,请假"+censusKqDto.getLeaveCount()+"人;";
                }
                DingSms dingSms = new DingSms();
                dingSms.setName(teacher.getName());
                dingSms.setTableName("DingSmsNew"+tableSuffix);
                dingSms.setMsg("报告类型:"+KqTypeEnums.getName(Integer.valueOf(times[2])) + "出勤报告 \n - "+
                        "签到日期:"+DateUtils.date2String(new Date(),DateUtils.format1)+ "("+DateUtils.getWeekName()+") \n - "+
                        "签到时间:"+times[0]+"-"+ times[1] +" \n - "+
                        "整体数据:" + message);
                String wapUrl = "http://campus.myjxt.com/studentRecord/znxwwebapp/index.html?" +
                        "schoolid="+teacher.getSchoolId()+"&userId="+teacher.getUserId()+"&name="+teacher.getName()+ "&TemplateId=" + templateId+
                        "&data="+thirdOpenId+"&type=5&stype=4&mobile="+teacher.getMobile()+"&pass="+teacher.getPass()+
                        "&QianDaoId="+times[3]+"&ClassId=0&face=&sourcetype=16&soutype=3&timestamp="+ DateUtils.getDate()+"&time="+DateUtils.date2String(new Date(),DateUtils.format1) ;
                dingSms.setSchoolId(teacher.getSchoolId());
                dingSms.setDingUserId(thirdOpenId);
                dingSms.setAgentId(appDto.getAgentId());
                dingSms.setWapUrl(wapUrl);
                dingSms.setReceiveUserId(teacher.getUserId());
                dingSms.setTdType(TypeEnums.kaoqing.getType());
                this.insertDing(dingSms);
                logger.info("盯盯推送班主任—————" + templateId + "--------" + teacher.getName());
            }
        }
    }

    //盯盯推送宿管
    private void dingChamberPush(String templateId, KqTeacherDto teacher , String key , AppDto appDto){
        String[] times = key.split("_");
        String tableSuffix = DateUtils.date2String(new Date(), DateUtils.format);
        String thirdOpenId = scFeign.getThirdId(teacher.getUserId(),0);
        if(StringUtils.isNotBlank(thirdOpenId)){
            List<CensusKqDto> censusKqDtos = getCensus(Integer.valueOf(times[3]),templateId,teacher.getSchoolId(),teacher.getUserId(),1);
            if(censusKqDtos != null &&censusKqDtos.size() > 0){
                String message = "";
                for(CensusKqDto censusKqDto : censusKqDtos){
                    message += "[" + censusKqDto.getTargetName() + "]未考勤" + censusKqDto.getNotAttendCount() + "人,请假"+censusKqDto.getLeaveCount()+"人;";
                }
                DingSms dingSms = new DingSms();
                dingSms.setName(teacher.getName());
                dingSms.setTableName("DingSmsNew"+tableSuffix);
                dingSms.setMsg("报告类型:"+KqTypeEnums.getName(Integer.valueOf(times[2])) + "出勤报告 \n - "+
                        "签到日期:"+DateUtils.date2String(new Date(),DateUtils.format1)+ "("+DateUtils.getWeekName()+") \n - "+
                        "签到时间:"+times[0]+"-"+ times[1] +" \n - "+
                        "整体数据:" + message);
                String msgUrl = "http://campus.myjxt.com/studentRecord/znxwwebapp/index.html?" +
                        "schoolid=" + teacher.getSchoolId() + "&userId=" + teacher.getUserId() + "&name=" + teacher.getName() +"&TemplateId=" + templateId+
                        "&pass=" + teacher.getPass() + "&type=4&soutype=3&time="+DateUtils.date2String(new Date(),DateUtils.format1);
                dingSms.setSchoolId(teacher.getSchoolId());
                dingSms.setDingUserId(thirdOpenId);
                dingSms.setAgentId(appDto.getAgentId());
                dingSms.setWapUrl(msgUrl);
                dingSms.setReceiveUserId(teacher.getUserId());
                dingSms.setTdType(TypeEnums.kaoqing.getType());
                this.insertDing(dingSms);
                logger.info("盯盯推送宿管—————" + templateId + "--------" + teacher.getName());
            }
        }
    }

    //企业号推送班主任
    private void weChatSchoolPush(String templateId, KqTeacherDto teacher , String key , AppDto appDto , int type){
        String[] times = key.split("_");
        String tableSuffix = DateUtils.date2String(new Date(), DateUtils.format);
        String thirdOpenId = scFeign.getThirdId(teacher.getUserId(),1);
        if (StringUtils.isNotBlank(thirdOpenId)) {
            List<CensusKqDto> censusKqDtos = getCensus(Integer.valueOf(times[3]),templateId,teacher.getSchoolId(),teacher.getUserId(),type);
            if (censusKqDtos != null && censusKqDtos.size() > 0) {
                String message = "";
                for (CensusKqDto censusKqDto : censusKqDtos) {
                    message += "[" + censusKqDto.getTargetName() + "]未考勤" + censusKqDto.getNotAttendCount() + "人,请假"+censusKqDto.getLeaveCount()+"人;";
                }
                WeChatSms weChatSms = new WeChatSms();
                weChatSms.setTableName("qyhSmsNew" + tableSuffix);
                weChatSms.setMsg("报告类型:" + KqTypeEnums.getName(Integer.valueOf(times[2])) + "出勤报告 \n - " +
                        "签到日期:" + DateUtils.date2String(new Date(), DateUtils.format1) + "(" + DateUtils.getWeekName() + ") \n - " +
                        "签到时间:" + times[0] + "-" + times[1] + " \n - " +
                        "整体数据:" + message);
                weChatSms.setSchoolId(teacher.getSchoolId());
                weChatSms.setName(teacher.getName());
                weChatSms.setQiYeHaoUserId(thirdOpenId);
                weChatSms.setReceiveUserId(teacher.getUserId());
                weChatSms.setAppId(appDto.getAgentId());
                weChatSms.setSecret(appDto.getAgentSecret());
                String msgUrl = "http://campus.myjxt.com/studentRecord/znxwwebapp/index.html?" +
                        "schoolid=" + teacher.getSchoolId() + "&userId=" + teacher.getUserId() + "&name=" + teacher.getName() +"&TemplateId=" + templateId+
                        "&data=" + thirdOpenId + "&type=1&stype=3&mobile=" + teacher.getMobile() + "&pass=" + teacher.getPass() +
                        "&face=" + teacher.getFace() + "&sourcetype=16&soutype=2&timestamp=" + DateUtils.getDate()+"&time="+DateUtils.date2String(new Date(),DateUtils.format1)+"&";
                weChatSms.setMsgUrl(msgUrl);
                weChatSms.setTdType(TypeEnums.kaoqing.getType());
                this.insertQYH(weChatSms);
                logger.info("企业号推送班主任—————" + templateId + "--------" + teacher.getName());
            }
        }
    }

    //企业号推送宿管
    private void weChatChamberPush(String templateId, KqTeacherDto teacher , String key , AppDto appDto){
        String[] times = key.split("_");
        String tableSuffix = DateUtils.date2String(new Date(), DateUtils.format);
        String thirdOpenId = scFeign.getThirdId(teacher.getUserId(),1);
        if (StringUtils.isNotBlank(thirdOpenId)) {
            List<CensusKqDto> censusKqDtos = getCensus(Integer.valueOf(times[3]),templateId,teacher.getSchoolId(),teacher.getUserId(),1);
            if (censusKqDtos != null && censusKqDtos.size() > 0) {
                String message = "";
                for (CensusKqDto censusKqDto : censusKqDtos) {
                    message += "[" + censusKqDto.getTargetName() + "]未考勤" + censusKqDto.getNotAttendCount() + "人,请假"+censusKqDto.getLeaveCount()+"人;";
                }
                WeChatSms weChatSms = new WeChatSms();
                weChatSms.setTableName("qyhSmsNew" + tableSuffix);
                weChatSms.setMsg("报告类型:" + KqTypeEnums.getName(Integer.valueOf(times[2])) + "出勤报告 \n - " +
                        "签到日期:" + DateUtils.date2String(new Date(), DateUtils.format1) + "(" + DateUtils.getWeekName() + ") \n - " +
                        "签到时间:" + times[0] + "-" + times[1] + " \n - " +
                        "整体数据:" + message);
                weChatSms.setSchoolId(teacher.getSchoolId());
                weChatSms.setName(teacher.getName());
                weChatSms.setQiYeHaoUserId(thirdOpenId);
                weChatSms.setReceiveUserId(teacher.getUserId());
                weChatSms.setAppId(appDto.getAgentId());
                weChatSms.setSecret(appDto.getAgentSecret());
                String msgUrl = "http://campus.myjxt.com/studentRecord/znxwwebapp/index.html?" +
                        "schoolid=" + teacher.getSchoolId() + "&userId=" + teacher.getUserId() + "&name=" + teacher.getName() +"&TemplateId=" + templateId+
                        "&pass=" + teacher.getPass() + "&type=4&soutype=2&time="+DateUtils.date2String(new Date(),DateUtils.format1)+"&";
                weChatSms.setMsgUrl(msgUrl);
                weChatSms.setTdType(TypeEnums.kaoqing.getType());
                this.insertQYH(weChatSms);
                logger.info("企业号推送宿管—————" + templateId + "--------" + teacher.getName());
            }
        }
    }

    //type  0 出入校 1 出入寝
    private List<CensusKqDto> getCensus(int id , String templateId ,int schoolId , String userId ,int type){
        List<CensusKqDto> list = new ArrayList<>();
        String url = "http://campus.myjxt.com/api/EasyN/GeAttendDetailNew?schoolId="+schoolId+
                "&id="+id+"&templateID="+templateId+"&type="+type+"&userId="+userId+"&time=" + DateUtils.date2String(new Date(),DateUtils.format1);
        JSONObject jsonObject = HttpClientUtils.httpGet(url);
        try{
            JSONArray data = (JSONArray)jsonObject.get("data");
            for(int i = 0 ; i < data.size() ; i++){
                CensusKqDto censusKqDto = new CensusKqDto();
                JSONObject object = data.getJSONObject(i);
                censusKqDto.setLeaveCount((Integer) object.get("leaveCount"));
                censusKqDto.setNotAttendCount((Integer) object.get("noAttendCount"));
                censusKqDto.setTargetName((String)object.get("name"));
                list.add(censusKqDto);
            }
        }catch (Exception e){

        }
        return list ;
    }

    private BigDecimal getAlarmCensus(int id , String templateId ,int schoolId , int type){
        int allNumber = 0 , attendNumber = 0 ;
        List<Integer> list = scFeign.selectClassBySchoolId(schoolId);
        for(Integer classId : list){
            String url = "http://campus.myjxt.com/api/EasyN/GeAttendDetail?classId="+classId+
                    "&id="+id+"&templateID="+templateId+"&type="+type+"&time=" + DateUtils.date2String(new Date(),DateUtils.format1);
            JSONObject jsonObject = HttpClientUtils.httpGet(url);
            try{
                JSONObject data = (JSONObject) jsonObject.get("data");
                attendNumber = attendNumber + (Integer) data.get("stuAttendCount");
                allNumber = allNumber + (Integer) data.get("stuCount");
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if(allNumber != 0){
            BigDecimal pecrent = new BigDecimal(attendNumber).divide(new BigDecimal(allNumber),10, BigDecimal.ROUND_HALF_DOWN) ;
            return  pecrent ;
        }else {
            return BigDecimal.ZERO ;
        }

    }

    /**
     *
     * @param id
     * @param templateId
     * @param schoolId
     * @param type
     * @return list(0)  出勤 list(1) 未出勤  List(2) 请假
     */
    private List<Integer> getIOTCensus(int id , String templateId ,int schoolId , int type){
        List<Integer> result = new ArrayList<>();
        int attend = 0 , unattend = 0 , leave = 0;
        List<Integer> list = scFeign.selectClassBySchoolId(schoolId);
        for(Integer classId : list){
            String url = "http://campus.myjxt.com/api/EasyN/GeAttendDetail?classId="+classId+
                    "&id="+id+"&templateID="+templateId+"&type="+type+"&time=" + DateUtils.date2String(new Date(),DateUtils.format1);
            JSONObject jsonObject = HttpClientUtils.httpGet(url);
            try{
                JSONObject data = (JSONObject) jsonObject.get("data");
                attend = attend + (Integer) data.get("stuAttendCount");
                unattend = unattend + (Integer) data.get("noAttendCount");
                leave = leave + (Integer) data.get("leaveCount");
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        result.add(attend);
        result.add(unattend);
        result.add(leave);
        return result ;
    }


    private void insertDing(DingSms dingSms){
        try{
            smsService.insertDing(dingSms);
        }catch (Exception e){
            logger.info(e.toString());
        }
    }

    private void insertQYH(WeChatSms weChatSms){
        try{
            smsService.insertWeChat(weChatSms);
        }catch (Exception e){
            logger.info(e.toString());
        }
    }

    private void alarmPush(int schoolId){
        String msg = "【考勤异动】" ;
        try{
            SchoolDto schoolDto = scFeign.selectSchoolBySchoolId(schoolId);
            msg +=  schoolDto.getSchoolName() + "考勤有异动,请关注!" ;
            sendMessage(schoolDto.getSchoolId(),opexList.get(0),msg);
            sendMessage(schoolDto.getSchoolId(),opexList.get(1),msg);
            sendMessage(schoolDto.getSchoolId(),integration,msg);
            String managerPhone = managerService.selectManagerById(schoolDto.getManagerUserId()) ;
            if(StringUtils.isNotBlank(managerPhone)){
                sendMessage(schoolDto.getSchoolId(),managerPhone,msg);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private void sendMessage(int schoolId , String mobile , String msg){
        String tableSuffix = DateUtils.date2String(new Date(), DateUtils.format);
        ShortMsg shortMsg = new ShortMsg();
        shortMsg.setTableName("smsNew"+tableSuffix);
        shortMsg.setSchoolId(schoolId);
        shortMsg.setMobile(mobile);
        shortMsg.setMsg(msg);
        smsService.insertSMS(shortMsg);
    }
}