EveryAvgAlarmJob.java 27.0 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
package com.skua.modules.alarm.quartz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.skua.core.util.chuanglan.HttpSenderMsg;
import com.skua.modules.alarm.entity.AlarmMessageHistory;
import com.skua.modules.alarm.entity.AlarmRecordHistory;
import com.skua.modules.alarm.service.AlarmMessageHistoryService;
import com.skua.modules.alarm.service.AlarmRecordHistoryService;
import com.skua.modules.alarm.service.AlarmRuleConfigService;
import com.skua.modules.alarm.vo.AlarmRuleAndAlarmRuleLevelVO;
import com.skua.modules.message.websocket.WebSocket;
import com.skua.modules.system.entity.SysFactoryInfo;
import com.skua.modules.system.entity.SysUser;
import com.skua.modules.system.service.ISysAnnouncementSendService;
import com.skua.modules.system.service.ISysAnnouncementService;
import com.skua.modules.system.service.ISysFactoryInfoService;
import com.skua.modules.system.service.ISysUserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: fmPlatform
 * @description:
 * @author: xie chao
 * @create: 2020-11-24 15:37
 */
@Slf4j
public class EveryAvgAlarmJob implements Job {

    @Autowired
    private AlarmRuleConfigService alarmRuleConfigService;

    @Autowired
    private ISysFactoryInfoService sysFactoryInfoService;

    @Autowired
    private AlarmRecordHistoryService alarmRecordHistoryService;

    @Autowired
    private AlarmMessageHistoryService alarmMessageHistoryService;

    @Autowired
    private ISysUserService sysUserService;

    @Resource
    private WebSocket webSocket;

    private static String excessMsgContent = "【金控数矿】${date},${factoryName}采集数据,${alarmParamName}:${actualAvgValue}${alarmParamUnit}${compare}标准${limitValue},报警级别${alarmRuleLevelName}。";


    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        //从数据库中获取日均值报警的信息
        QueryWrapper<AlarmRuleAndAlarmRuleLevelVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("arc.alarm_rule_type","2");
        queryWrapper.eq("alarm_rule_status","1");
        queryWrapper.eq("arc.del_flag","1");
        List<AlarmRuleAndAlarmRuleLevelVO> alarmRuleAndAlarmRuleLevelVOList = alarmRuleConfigService.getAlarmRuleAndAlarmRuleLeveList(queryWrapper);

        //查询需要从mysql中查询实时数据的厂
        QueryWrapper<SysFactoryInfo> sysFactoryInfoQueryWrapper = new QueryWrapper<>();
        sysFactoryInfoQueryWrapper.eq("real_time_database","mysql");
        sysFactoryInfoQueryWrapper.select("factory_id","real_time_database");
        List<Map<String, Object>> mysqlFactoryMapList = sysFactoryInfoService.listMaps(sysFactoryInfoQueryWrapper);

        Map<String,String> mysqlFactoryMap = new HashMap<>();

        mysqlFactoryMapList.forEach(map ->{

            mysqlFactoryMap.put(map.get("factory_id").toString(),map.get("real_time_database").toString());

        });

        //查询需要从pg中查询实时数据的厂
        sysFactoryInfoQueryWrapper = new QueryWrapper<>();
        sysFactoryInfoQueryWrapper.eq("real_time_database","pg");
        sysFactoryInfoQueryWrapper.select("factory_id");
        List<Map<String, Object>> pgFactoryMapList = sysFactoryInfoService.listMaps(sysFactoryInfoQueryWrapper);

        Map<String,String> pgFactoryMap = new HashMap<>();

        pgFactoryMapList.forEach(map->{

            pgFactoryMap.put(map.get("factory_id").toString(),map.get("real_time_database").toString());
        });


        //存放数据超标报警,并且实时数据源是mysql
        List<AlarmRuleAndAlarmRuleLevelVO> mysqlExcessAlarmList = new ArrayList<>();

        //存放数据超标报警,并且实时数据源是pg
        List<AlarmRuleAndAlarmRuleLevelVO> pgExcessAlarmList = new ArrayList<>();

        alarmRuleAndAlarmRuleLevelVOList.forEach(alarmRuleAndAlarmRuleLevelVO->{

            if(mysqlFactoryMap.containsKey(alarmRuleAndAlarmRuleLevelVO.getFactoryId())){
                mysqlExcessAlarmList.add(alarmRuleAndAlarmRuleLevelVO);
            }else if (pgFactoryMap.containsKey(alarmRuleAndAlarmRuleLevelVO.getFactoryId())) {
                pgExcessAlarmList.add(alarmRuleAndAlarmRuleLevelVO);
            }


        });

        List<Map<String, Object>> checkMysqlEveryAvgAlarmList = checkMysqlEveryAvgAlarm(mysqlExcessAlarmList);


        sendMessage(checkMysqlEveryAvgAlarmList,mysqlExcessAlarmList);

    }

    private List<Map<String,Object>> checkMysqlEveryAvgAlarm(List<AlarmRuleAndAlarmRuleLevelVO> mysqlExcessAlarmList) {

        //存放厂ID和指标的List集合
        Map<String,String> stringMap = new HashMap<>();

        mysqlExcessAlarmList.forEach(alarmRuleAndAlarmRuleLevelVO -> {


            String alarmProgramCode = stringMap.get(alarmRuleAndAlarmRuleLevelVO.getFactoryId());

            if(alarmProgramCode == null){
                stringMap.put(alarmRuleAndAlarmRuleLevelVO.getFactoryId(),alarmRuleAndAlarmRuleLevelVO.getAlarmProgramCode());
            }else{
                stringMap.put(alarmRuleAndAlarmRuleLevelVO.getFactoryId(),alarmProgramCode +","+alarmRuleAndAlarmRuleLevelVO.getAlarmProgramCode());
            }

        });

        Map<String,List<Map<String,Object>>> resultListMap = new HashMap<>();

        //循环将计算数据取出
        stringMap.forEach((factoryId,alarmProgramCodeStr)->{

            //根据factoryId查询数据库的数据,查询的时候注意将超标的字段设置为查询字段
            //List<Map<String, Object>> maps = factoryInfoForMysqlService.queryFactoryInfoCount(factoryId, alarmProgramCodeStr, (new SimpleDateFormat("yyyy-MM-dd").format(new Date())+" 00:00:00"), (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())), "1");
            List<Map<String, Object>> maps = null;

            resultListMap.put(factoryId,maps);

        });


        //存放报警规则级别ID和超标数据
        Map<String, List<Double>> dataMap = new HashMap<>();

        //TODO 后期需要优化
        mysqlExcessAlarmList.forEach(alarmRuleAndAlarmRuleLevelVO->{

            //存放超标数据
            List<Double> dataMapList = new ArrayList<>();

            String alarmRuleLevelId = alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelId();

            //通过厂ID得到厂的实际所有报警指标的实际数值
            List<Map<String, Object>> mapList = resultListMap.get(alarmRuleAndAlarmRuleLevelVO.getFactoryId());

            if(mapList.size() > 0) {
                mapList.forEach(listMap -> {

                    listMap.forEach((alarmProgramCode, value) -> {

                        //判断指标是否相等
                        if (alarmRuleAndAlarmRuleLevelVO.getAlarmProgramCode().equals(alarmProgramCode)) {

                            if(Double.parseDouble(value.toString()) > Double.parseDouble(alarmRuleAndAlarmRuleLevelVO.getEffectiveUpperLimit())
                                    && Double.parseDouble(value.toString()) < Double.parseDouble(alarmRuleAndAlarmRuleLevelVO.getEffectiveLowerLimit())) {
                                if (value != null) {
                                    dataMapList.add(Double.parseDouble(value.toString()));
                                }
                            }

                        }

                    });
                });
            }

            if(dataMapList.size() > 0){
                dataMap.put(alarmRuleLevelId,dataMapList);
            }

        });


        //存储各指标的计算
        List<Map<String,Object>>  resultList = new ArrayList<>();

        dataMap.forEach((alarmRuleLevelId,list) ->{

            Map<String,Object> resultMap = new HashMap<>();

            DoubleSummaryStatistics statistics = list.stream().mapToDouble(Number::doubleValue).summaryStatistics();

            double avgValue = statistics.getAverage();

            BigDecimal bg = new BigDecimal(avgValue).setScale(2, RoundingMode.UP);

            resultMap.put("avgValue",bg.doubleValue());
            resultMap.put("alarmRuleLevelId",alarmRuleLevelId);

            resultList.add(resultMap);

        });

        return resultList;
    }

    private void sendMessage(List<Map<String, Object>> resultList, List<AlarmRuleAndAlarmRuleLevelVO> alarmRuleAndAlarmRuleLevelVOList) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");

        List<AlarmRecordHistory> alarmRecordHistoryList = new ArrayList<>();
        List<AlarmMessageHistory> alarmMessageHistoryList = new ArrayList<>();

        List<String> userList = new ArrayList<>();

        //获取需要发送短信的手机号
        alarmRuleAndAlarmRuleLevelVOList.forEach(alarmRuleAndAlarmRuleLevelVO -> {

            userList.add(alarmRuleAndAlarmRuleLevelVO.getAlarmSmsRecipient());

        });

        QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
        if(userList.size() >0){
            sysUserQueryWrapper.in("id",userList);
        }
        List<SysUser> sysUserList = sysUserService.list(sysUserQueryWrapper);

        //用map存放用户信息和手机号信息
        Map<String,String> userPhoneMap = new HashMap<>();

        sysUserList.forEach(sysUser -> {
            userPhoneMap.put(sysUser.getId(),sysUser.getPhone());
        });

        resultList.forEach((resultMap)->{

            alarmRuleAndAlarmRuleLevelVOList.forEach( alarmRuleAndAlarmRuleLevelVO -> {

                //判断报警规则级别ID是否相等
                if(resultMap.get("alarmRuleLevelId").equals(alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelId())){

                    //判断是否超过容错率,如果有,则表示需要生成警报记录
                    if(Double.parseDouble(resultMap.get("avgValue").toString()) < Double.parseDouble(alarmRuleAndAlarmRuleLevelVO.getAlarmParamUpperLimit())
                            || Double.parseDouble(resultMap.get("avgValue").toString()) > Double.parseDouble(alarmRuleAndAlarmRuleLevelVO.getAlarmParamLowerLimit())){

                        //查询短信上一条报警是否处理,未处理就不新增报警记录
                        //TODO 先从数据库中获取,后期优化放redis中
                        QueryWrapper<AlarmRecordHistory> queryWrapper = new QueryWrapper<>();
                        queryWrapper.eq("alarm_rule_level_id",alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelId());
                        queryWrapper.orderByDesc("record_time").last("limit 1");
                        AlarmRecordHistory alarmRecordHistoryOne = alarmRecordHistoryService.getOne(queryWrapper);

                        //获取上次信息发送的时间,如果满足则发送
                        //TODO 先从数据库中获取,后期优化方redis中
                        QueryWrapper<AlarmMessageHistory> alarmMessageHistoryQueryWrapper = new QueryWrapper<>();
                        alarmMessageHistoryQueryWrapper.eq("alarm_rule_level_id",alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelId());
                        alarmMessageHistoryQueryWrapper.eq("alarm_send_type","1");
                        alarmMessageHistoryQueryWrapper.orderByDesc("alarm_send_time").last("limit 1");
                        AlarmMessageHistory messageHistoryOne = alarmMessageHistoryService.getOne(alarmMessageHistoryQueryWrapper);

                        String msgContent = "";
                        //替换报警的内容
                        String compareCon = "";
                        String limitValue = "";
                        if(Double.parseDouble(resultMap.get("avgValue").toString()) > Double.parseDouble(alarmRuleAndAlarmRuleLevelVO.getAlarmParamUpperLimit())){
                            compareCon = "高于";
                            limitValue = "最大值";
                        }else if(Double.parseDouble(resultMap.get("avgValue").toString()) <Double.parseDouble(alarmRuleAndAlarmRuleLevelVO.getAlarmParamLowerLimit())){
                            compareCon = "低于";
                            limitValue = "最小值";
                        }

                        msgContent = excessMsgContent.replace("${date}",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())).replace("${factoryName}",
                                alarmRuleAndAlarmRuleLevelVO.getFactoryName()).replace("${alarmParamName}",alarmRuleAndAlarmRuleLevelVO.getAlarmParamName()).
                                replace("${actualAvgValue}",resultMap.get("avgValue").toString()).replace("${alarmParamUnit}",alarmRuleAndAlarmRuleLevelVO.getAlarmParamUnit())
                                .replace("${compare}",compareCon).replace("${alarmRuleLevelName}",alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelName()).replace("${limitValue}",limitValue);

                        Date lastTime = null;
                        //如果数据库没有上次时间,则设置一个默认时间
                        if(messageHistoryOne != null){
                            lastTime = messageHistoryOne.getAlarmSendTime();
                        }else{
                            try {
                                lastTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2020-01-01 00:00:00");
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                        }


                        //如果没有上条记录或者处理状态(0:未处理,1:已处理)为已处理
                        if(alarmRecordHistoryOne == null || alarmRecordHistoryOne.getHandleStatus() == 1){

                            AlarmRecordHistory alarmRecordHistory = new AlarmRecordHistory();

                            alarmRecordHistory.setAlarmParamCode(alarmRuleAndAlarmRuleLevelVO.getAlarmParamCode());
                            alarmRecordHistory.setAlarmParamName(alarmRuleAndAlarmRuleLevelVO.getAlarmParamName());
                            alarmRecordHistory.setAlarmRuleLevelId(alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelId());

                            alarmRecordHistory.setAlarmRuleType(alarmRuleAndAlarmRuleLevelVO.getAlarmRuleType());
                            alarmRecordHistory.setAlarmParamType(alarmRuleAndAlarmRuleLevelVO.getAlarmParamType());

                            alarmRecordHistory.setRecordTime(new Date());
                            alarmRecordHistory.setAlarmParamUpperLimit(alarmRuleAndAlarmRuleLevelVO.getAlarmParamUpperLimit());
                            alarmRecordHistory.setAlarmParamLowerLimit(alarmRuleAndAlarmRuleLevelVO.getAlarmParamLowerLimit());
                            alarmRecordHistory.setAlarmParamUnit(alarmRuleAndAlarmRuleLevelVO.getAlarmParamUnit());
                            alarmRecordHistory.setFactoryId(alarmRuleAndAlarmRuleLevelVO.getFactoryId());
                            alarmRecordHistory.setActualAvgValue(resultMap.get("avgValue").toString());
                            alarmRecordHistory.setHandleStatus(0);


                            alarmRecordHistory.setAlarmContent(msgContent);

                            alarmRecordHistoryList.add(alarmRecordHistory);

                        }

                        //判断是否需要发送短信,1表示发送
                        if(alarmRuleAndAlarmRuleLevelVO.getIsSendSms() == 1){

                            //判读是否在发送时间内
                            Date allowStartDate = null;
                            Date allowEndDate = null;
                            Date nowDate = null;
                            try {
                                allowEndDate = simpleDateFormat.parse(alarmRuleAndAlarmRuleLevelVO.getSmsAllowSendTime().split("-")[1]);
                                allowStartDate = simpleDateFormat.parse(alarmRuleAndAlarmRuleLevelVO.getSmsAllowSendTime().split("-")[0]);
                                nowDate = simpleDateFormat.parse(simpleDateFormat.format(new Date()));
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }

                            //如果在发送时间内,则发送
                            if(isEffectiveDate(nowDate,allowStartDate,allowEndDate )){

                                //判断是否满足短信发送频率,将两个时间转为毫秒
                                Long lastTimeLong = lastTime.getTime();

                                Long nowDateLong = new Date().getTime();

                                if(nowDateLong - lastTimeLong > Double.parseDouble(alarmRuleAndAlarmRuleLevelVO.getSmsSendFrequency())*60*60*1000){

                                    //获取报警接收人的手机号
                                    StringBuilder phoneStringBuilder = new StringBuilder();

                                    String[] userArr = alarmRuleAndAlarmRuleLevelVO.getAlarmSmsRecipient().split(",");

                                    for (int i=0; i < userArr.length;i++){

                                        phoneStringBuilder.append(",");
                                        phoneStringBuilder.append(userPhoneMap.get(userArr[i]));

                                        //发送短信
                                        HttpSenderMsg.sendMsg(msgContent, userPhoneMap.get(userArr[i]));
                                    }

                                    String phone = phoneStringBuilder.toString().replaceFirst(",","");

                                    //发送短信
//                                    HttpSenderMsg.sendMsg(msgContent, phone);

                                    //将发送的短信保存到数据库
                                    AlarmMessageHistory alarmMessageHistory = new AlarmMessageHistory();

                                    alarmMessageHistory.setAlarmRuleLevelId(alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelId());
                                    alarmMessageHistory.setAlarmSendUser(alarmRuleAndAlarmRuleLevelVO.getAlarmSmsRecipient());
                                    alarmMessageHistory.setAlarmSendPhone(phone);
                                    alarmMessageHistory.setAlarmSendTime(new Date());
                                    alarmMessageHistory.setAlarmSendContent(msgContent);
                                    alarmMessageHistory.setAlarmSendType(1);
                                    alarmMessageHistory.setAlarmParamCode(alarmRuleAndAlarmRuleLevelVO.getAlarmParamCode());
                                    alarmMessageHistory.setAlarmParamName(alarmRuleAndAlarmRuleLevelVO.getAlarmParamName());
                                    alarmMessageHistory.setAlarmRuleLevelId(alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelId());

                                    alarmMessageHistory.setAlarmRuleType(alarmRuleAndAlarmRuleLevelVO.getAlarmRuleType());
                                    alarmMessageHistory.setAlarmParamType(alarmRuleAndAlarmRuleLevelVO.getAlarmParamType());

                                    alarmMessageHistory.setAlarmParamUpperLimit(alarmRuleAndAlarmRuleLevelVO.getAlarmParamUpperLimit());
                                    alarmMessageHistory.setAlarmParamLowerLimit(alarmRuleAndAlarmRuleLevelVO.getAlarmParamLowerLimit());
                                    alarmMessageHistory.setAlarmParamUnit(alarmRuleAndAlarmRuleLevelVO.getAlarmParamUnit());
                                    alarmMessageHistory.setFactoryId(alarmRuleAndAlarmRuleLevelVO.getFactoryId());
                                    alarmMessageHistory.setActualAvgValue(resultMap.get("avgValue").toString());

                                    alarmMessageHistoryList.add(alarmMessageHistory);


                                }
                            }

                        }

                        //判断是否需要发送通知,1表示发送
                       /* if(alarmRuleAndAlarmRuleLevelVO.getIsSendNotice() == 1){

                            //判断上次的通知记录是否已读
                            //获取上次信息发送的时间,如果满足则发送
                            //TODO 先从数据库中获取,后期优化方redis中
                            QueryWrapper<AlarmMessageHistory> noticeMessageHistoryQueryWrapper = new QueryWrapper<>();
                            noticeMessageHistoryQueryWrapper.eq("alarm_rule_level_id",alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelId());
                            noticeMessageHistoryQueryWrapper.eq("alarm_send_type","2");
                            noticeMessageHistoryQueryWrapper.orderByDesc("alarm_send_time").last("limit 1");
                            AlarmMessageHistory noticeHistoryOne = alarmMessageHistoryService.getOne(alarmMessageHistoryQueryWrapper);

                            SysAnnouncementSend sysAnnouncementSendOne = null;
                            if(noticeHistoryOne != null) {

                                String noticeId = noticeHistoryOne.getAlarmSendPhone();
                                QueryWrapper<SysAnnouncementSend> sysAnnouncementSendQueryWrapper = new QueryWrapper<>();
                                sysAnnouncementSendQueryWrapper.eq("annt_id", noticeId);

                                sysAnnouncementSendOne = sysAnnouncementSendService.getOne(sysAnnouncementSendQueryWrapper);
                            }

                            if(noticeHistoryOne == null || (sysAnnouncementSendOne != null && sysAnnouncementSendOne.getReadFlag().equals("1"))) {

                                String[] userArr = alarmRuleAndAlarmRuleLevelVO.getAlarmSmsRecipient().split(",");
                                for (int i=0; i < userArr.length;i++){
                                    //发送站内通知
                                    SysAnnouncement sysAnnouncement = new SysAnnouncement();
                                    //生成uuid
                                    String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                                    sysAnnouncement.setId(uuid);
                                    sysAnnouncement.setTitile("报警通知");
                                    sysAnnouncement.setMsgContent(msgContent);
                                    sysAnnouncement.setPriority(CommonConstant.PRIORITY_M);
                                    sysAnnouncement.setMsgType(CommonConstant.MSG_TYPE_UESR);
                                    sysAnnouncement.setSendStatus(CommonConstant.HAS_SEND);
                                    sysAnnouncement.setSendTime(new Date());
                                    sysAnnouncement.setMsgCategory(CommonConstant.MSG_CATEGORY_2);
                                    sysAnnouncement.setDelFlag(String.valueOf(CommonConstant.DEL_FLAG_0));
                                    sysAnnouncement.setSender(userArr[i]);
                                    sysAnnouncementService.save(sysAnnouncement);

                                    SysAnnouncementSend announcementSend = new SysAnnouncementSend();
                                    announcementSend.setAnntId(sysAnnouncement.getId());
                                    announcementSend.setUserId(userArr[i]);
                                    announcementSend.setReadFlag(CommonConstant.NO_READ_FLAG);
                                    sysAnnouncementSendService.save(announcementSend);
                                    JSONObject obj = new JSONObject();
                                    obj.put("cmd", "user");
                                    obj.put("userId", userArr[i]);
                                    obj.put("msgId", sysAnnouncement.getId());
                                    obj.put("msgTxt", sysAnnouncement.getTitile());
                                    webSocket.sendOneMessage(userArr[i], obj.toJSONString());

                                }

                                //将发送的站内通知保存到数据库
                                AlarmMessageHistory alarmMessageHistory = new AlarmMessageHistory();
                                alarmMessageHistory.setAlarmRuleLevelId(alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelId());
                                alarmMessageHistory.setAlarmSendUser(alarmRuleAndAlarmRuleLevelVO.getAlarmSmsRecipient());
                                alarmMessageHistory.setAlarmSendTime(new Date());
                                alarmMessageHistory.setAlarmSendContent(msgContent);
                                alarmMessageHistory.setAlarmSendType(2);
                                alarmMessageHistory.setAlarmParamCode(alarmRuleAndAlarmRuleLevelVO.getAlarmParamCode());
                                alarmMessageHistory.setAlarmParamName(alarmRuleAndAlarmRuleLevelVO.getAlarmParamName());
                                alarmMessageHistory.setAlarmRuleLevelId(alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelId());

                                alarmMessageHistory.setAlarmRuleType(alarmRuleAndAlarmRuleLevelVO.getAlarmRuleType());
                                alarmMessageHistory.setAlarmParamType(alarmRuleAndAlarmRuleLevelVO.getAlarmParamType());

                                alarmMessageHistory.setAlarmParamUpperLimit(alarmRuleAndAlarmRuleLevelVO.getAlarmParamUpperLimit());
                                alarmMessageHistory.setAlarmParamLowerLimit(alarmRuleAndAlarmRuleLevelVO.getAlarmParamLowerLimit());
                                alarmMessageHistory.setAlarmParamUnit(alarmRuleAndAlarmRuleLevelVO.getAlarmParamUnit());
                                alarmMessageHistory.setFactoryId(alarmRuleAndAlarmRuleLevelVO.getFactoryId());
                                alarmMessageHistory.setActualAvgValue(resultMap.get("avgValue").toString());
                                //用phone存放接收通知的UUID
//                                alarmMessageHistory.setAlarmSendPhone(uuid);

                                alarmMessageHistoryList.add(alarmMessageHistory);
                            }

                        }*/

                    }


                }


            });


        });

        alarmMessageHistoryService.saveBatch(alarmMessageHistoryList);
        alarmRecordHistoryService.saveBatch(alarmRecordHistoryList);

    }


    /**
     * 判断当前时间是否在[startTime, endTime]区间,注意时间格式要一致
     *
     * @param nowTime 当前时间
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     * @author xiechao
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }

    }

}