TestJobController.java 69.9 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 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
package com.skua.modules.alarm.controller;

import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;

import com.alibaba.fastjson.JSONObject;
import com.skua.core.constant.CommonConstant;
import com.skua.core.context.SpringContextUtils;
import com.skua.core.service.IPgQueryService;
import com.skua.core.util.ConvertUtils;
import com.skua.core.util.DateUtils;
import com.skua.core.util.DoubleOperaUtils;
import com.skua.modules.report.entity.ReportWaterData;
import com.skua.modules.report.service.IReportWaterDataService;
import com.skua.modules.system.entity.*;
import com.skua.modules.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.skua.core.service.IFactoryInfoService;
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 lombok.AllArgsConstructor;

/**
 * @program: fmboot
 * @description:
 * @author: xie chao
 * @create: 2020-11-06 15:10
 */
@RestController
@RequestMapping("/alarm/test")
public class TestJobController {

    @Autowired
    private ISysDepartService sysDepartService;
    @Autowired
    private ISysAnnouncementService sysAnnouncementService;
    @Autowired
    private ISysAnnouncementSendService sysAnnouncementSendService;
    @Autowired
    private ISysUserDepartService iSysUserDepartService;
    @Resource
    private WebSocket webSocket;

    private AlarmRuleConfigService alarmRuleConfigService;

    private ISysFactoryInfoService sysFactoryInfoService;

    private AlarmRecordHistoryService alarmRecordHistoryService;

    private AlarmMessageHistoryService alarmMessageHistoryService;

    private IFactoryInfoService factoryInfoService;

    private ISysUserService sysUserService;

    @Autowired
    private IReportWaterDataService reportWaterDataService;
    @Autowired
    private IPgQueryService pgQueryService;

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

    private static String breakdownMsgContent = "【金控数矿】尊敬的用户,数矿特别提示:${date},${factoryName}采集数据,${alarmParamName}反馈故障,报警级别${alarmRuleLevelName},请您及时采取处理措施。";

    @GetMapping("/test")
    public void test(){

        //查询需要实时数据报警厂的信息和报警规则字段的集合
        QueryWrapper<AlarmRuleAndAlarmRuleLevelVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("arc.alarm_rule_type","1");
        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> mysqlBreakdownAlarmList = new ArrayList<>();

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

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

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

        alarmRuleAndAlarmRuleLevelVOList.forEach(alarmRuleAndAlarmRuleLevelVO->{

                if(alarmRuleAndAlarmRuleLevelVO.getAlarmParamType().equals("1") && mysqlFactoryMap.containsKey(alarmRuleAndAlarmRuleLevelVO.getFactoryId())){
                    mysqlExcessAlarmList.add(alarmRuleAndAlarmRuleLevelVO);
                }else if(alarmRuleAndAlarmRuleLevelVO.getAlarmParamType().equals("2") && mysqlFactoryMap.containsKey(alarmRuleAndAlarmRuleLevelVO.getFactoryId())){
                    mysqlBreakdownAlarmList.add(alarmRuleAndAlarmRuleLevelVO);
                }else if (alarmRuleAndAlarmRuleLevelVO.getAlarmParamType().equals("1") && pgFactoryMap.containsKey(alarmRuleAndAlarmRuleLevelVO.getFactoryId())) {
                    pgExcessAlarmList.add(alarmRuleAndAlarmRuleLevelVO);
                } else if (alarmRuleAndAlarmRuleLevelVO.getAlarmParamType().equals("2") && pgFactoryMap.containsKey(alarmRuleAndAlarmRuleLevelVO.getFactoryId())) {
                    pgBreakdownAlarmList.add(alarmRuleAndAlarmRuleLevelVO);
                }


        });

        List<Map<String, Object>> breakdownAlarmList = checkMysqlBreakdownAlarm(mysqlBreakdownAlarmList);

        sendMessage(breakdownAlarmList,mysqlBreakdownAlarmList);


        //查询mysql数据超标警报
        List<Map<String, Object>> excessAlarmList = checkMysqlExcessAlarm(mysqlExcessAlarmList);

        sendMessage(excessAlarmList,mysqlExcessAlarmList);

    }


    /**
     * 判断mysql数据库中超标警报
     * @param mysqlExcessAlarmList
     */
    public List<Map<String,Object>> checkMysqlExcessAlarm(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<>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date nowDate = new Date();

        Date afterDate = new Date(nowDate.getTime() - 600000);
        //循环将实时数据取出
        stringMap.forEach((factoryId,alarmProgramCodeStr)->{

            //根据factoryId查询数据库的数据,查询的时候注意将超标的字段设置为查询字段
            //List<Map<String, Object>> maps = factoryInfoForMysqlService.queryFactoryInfos(factoryId, alarmProgramCodeStr, sdf.format(afterDate), sdf.format(nowDate), "0");
            List<Map<String, Object>> maps = null;

            resultListMap.put(factoryId,maps);

        });


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

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

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

            //存放超标数据
            List<Double> excessList = new ArrayList<>();
            List<Double> noExcessList = 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)) {

                            //判断是否超过数据上下限
                            String lowerLimitData = alarmRuleAndAlarmRuleLevelVO.getAlarmParamLowerLimit();
                            String upperLimitData = alarmRuleAndAlarmRuleLevelVO.getAlarmParamUpperLimit();
                            String actualValue = value.toString();

                            if(Double.parseDouble(actualValue) < Double.parseDouble(alarmRuleAndAlarmRuleLevelVO.getEffectiveUpperLimit())
                                    && Double.parseDouble(actualValue) > Double.parseDouble(alarmRuleAndAlarmRuleLevelVO.getEffectiveLowerLimit())) {

                                if (Double.parseDouble(actualValue) >= Double.parseDouble(upperLimitData)) {
                                    excessList.add(Double.parseDouble(actualValue));
                                } else if (Double.parseDouble(actualValue) <= Double.parseDouble(lowerLimitData)) {
                                    excessList.add(Double.parseDouble(actualValue));
                                } else {
                                    noExcessList.add(Double.parseDouble(actualValue));
                                }
                            }

                        }

                    });
                });
            }
            //excessList和noExcessList不为空才放入到map集合中
            if(excessList.size() > 0){
                excessMap.put(alarmRuleLevelId,excessList);
            }

            if(noExcessList.size() > 0) {
                noExcessMap.put(alarmRuleLevelId, noExcessList);
            }

        });


        List<Map<String,Object>> resultList = getRestulList(excessMap,noExcessMap);

        return resultList;

    }

    /**
     * 判断mysql数据库中故障警报
     * @param mysqlBreakdownAlarmList
     */
    public List<Map<String,Object>> checkMysqlBreakdownAlarm(List<AlarmRuleAndAlarmRuleLevelVO> mysqlBreakdownAlarmList){

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

        mysqlBreakdownAlarmList.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<>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date nowDate = new Date();

        Date afterDate = new Date(nowDate.getTime() - 600000);

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

            //根据factoryId查询数据库的数据,查询的时候注意将超标的字段设置为查询字段
            //List<Map<String, Object>> maps = factoryInfoForMysqlService.queryFactoryInfos(factoryId, alarmProgramCodeStr, sdf.format(afterDate), sdf.format(nowDate), "0");
            List<Map<String, Object>> maps = null;

            resultListMap.put(factoryId,maps);

        });


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

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

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

            //存放超标数据
            List<Double> breakdownList = new ArrayList<>();
            List<Double> noBreakdownMapList = new ArrayList<>();
            String alarmRuleLevelId = alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelId();

            //通过厂ID得到厂的实际所有报警指标的实际数值
            resultListMap.get(alarmRuleAndAlarmRuleLevelVO.getFactoryId()).forEach(listMap ->{

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

                    String actualValue = value.toString();

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

                        if (Double.parseDouble(actualValue) == 1) {
                            breakdownList.add(Double.parseDouble(actualValue));
                        } else if (Double.parseDouble(actualValue) == 0) {
                            noBreakdownMapList.add(Double.parseDouble(actualValue));
                        }

                    }

                });
            });

            //excessList和noExcessList不为空才放入到map集合中
            if(breakdownList.size() > 0){
                breakdownMap.put(alarmRuleLevelId,breakdownList);
            }

            if(noBreakdownMapList.size() > 0) {
                noBreakdownMap.put(alarmRuleLevelId,noBreakdownMapList);
            }

        });


        List<Map<String,Object>> resultList = getRestulList(breakdownMap,noBreakdownMap);

        return resultList;
    }

    /**
     * 判断pg数据库中超标的警报
     * @param pgExcessAlarmList
     */
    public List<Map<String,Object>>  checkPgExcessAlarm(List<AlarmRuleAndAlarmRuleLevelVO> pgExcessAlarmList){

        //用于拼接alarmProgramCode字段,去pg数据库查询
        StringBuilder stringBuilder = new StringBuilder();

        List<Map<String, Object>> result = new ArrayList();

        //判断拼接的字段
        int count = 0;

        //最大需要拼接的字段
        int maxCount = 20;

        for(AlarmRuleAndAlarmRuleLevelVO alarmRuleAndAlarmRuleLevelVO : pgExcessAlarmList){

            //当到达最大的的拼接字段时,去数据库查询一次
            if(count >= maxCount){
                //去数据库查询的操作
//                result.addAll(realTimeAlarmService.queryExcessAlarmDataForAlarmProgramCode(stringBuilder.toString().replaceFirst(",","")));
                count = 0;
            }

            //拼接pg实时表中的alarmProgramCode
            stringBuilder.append(",");
            stringBuilder.append("'"+alarmRuleAndAlarmRuleLevelVO.getAlarmProgramCode()+"'");

            count++;
        }

        //当count不等于0时,表示需要还需要在查询一次
        if(count > 0){
            //去数据库查询的操作
//            result.addAll(realTimeAlarmService.queryExcessAlarmDataForAlarmProgramCode(stringBuilder.toString().replaceFirst(",","")));
        }

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

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

        pgExcessAlarmList.forEach(alarmRuleAndAlarmRuleLevelVO ->{
            //存放超标数据
            List<Double> excessList = new ArrayList<>();
            List<Double> noExcessList = new ArrayList<>();

            String alarmRuleLevelId = alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelId();
            String lowerLimitData = alarmRuleAndAlarmRuleLevelVO.getAlarmParamLowerLimit();
            String upperLimitData = alarmRuleAndAlarmRuleLevelVO.getAlarmParamUpperLimit();

            result.forEach(obj ->{

                if(obj.get(alarmRuleAndAlarmRuleLevelVO.getAlarmProgramCode()) != null) {

                    String realData = obj.get(alarmRuleAndAlarmRuleLevelVO.getAlarmProgramCode()).toString();

                    if (Double.parseDouble(realData) >= Double.parseDouble(upperLimitData)) {
                        excessList.add(Double.parseDouble(realData));
                    } else if (Double.parseDouble(realData) <= Double.parseDouble(lowerLimitData)) {
                        excessList.add(Double.parseDouble(realData));
                    } else {
                        noExcessList.add(Double.parseDouble(realData));
                    }
                }
            });
            excessMap.put(alarmRuleLevelId,excessList);
            noExcessMap.put(alarmRuleLevelId,noExcessList);

        });

        List<Map<String,Object>> resultList = getRestulList(excessMap,noExcessMap);

        return resultList;

    }

    /**
     * 判断mysql数据库中超标的警报
     * @param pgBreakdownAlarmList
     */
    public List<Map<String,Object>> checkPgBreakdownAlarm(List<AlarmRuleAndAlarmRuleLevelVO> pgBreakdownAlarmList){


        //用于拼接alarmProgramCode字段,去pg数据库查询
        StringBuilder stringBuilder = new StringBuilder();

        List<Map<String, Object>> result = new ArrayList();

        //判断拼接的字段
        int count = 0;

        //最大需要拼接的字段
        int maxCount = 20;

        for(AlarmRuleAndAlarmRuleLevelVO alarmRuleAndAlarmRuleLevelVO : pgBreakdownAlarmList){

            //当到达最大的的拼接字段时,去数据库查询一次
            if(count >= maxCount){
                //去数据库查询的操作
//                result.addAll(realTimeAlarmService.queryExcessAlarmDataForAlarmProgramCode(stringBuilder.toString().replaceFirst(",","")));
                count = 0;
            }

            //拼接pg实时表中的alarmProgramCode
            stringBuilder.append(",");
            stringBuilder.append("'"+alarmRuleAndAlarmRuleLevelVO.getAlarmProgramCode()+"'");

            count++;
        }

        //当count不等于0时,表示需要还需要在查询一次
        if(count > 0){
            //去数据库查询的操作
//            result.addAll(realTimeAlarmService.queryExcessAlarmDataForAlarmProgramCode(stringBuilder.toString().replaceFirst(",","")));
        }

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

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

        pgBreakdownAlarmList.forEach(alarmRuleAndAlarmRuleLevelVO ->{
            //存放超标数据
            List<Double> breakdownList = new ArrayList<>();
            List<Double> noBreakdownList = new ArrayList<>();

            String alarmRuleLevelId = alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelId();

            result.forEach(obj ->{

                if(obj.get(alarmRuleAndAlarmRuleLevelVO.getAlarmProgramCode()) != null) {

                    String realData = obj.get(alarmRuleAndAlarmRuleLevelVO.getAlarmProgramCode()).toString();

                    if (Double.parseDouble(realData) == 1) {
                        breakdownList.add(Double.parseDouble(realData));
                    } else if (Double.parseDouble(realData)  == 0) {
                        noBreakdownList.add(Double.parseDouble(realData));
                    }
                }
            });
            breakdownMap.put(alarmRuleLevelId,breakdownList);
            noBreakdownMap.put(alarmRuleLevelId,noBreakdownList);

        });

        List<Map<String,Object>> resultList = getRestulList(breakdownMap,noBreakdownMap);

        return resultList;

    }

    /**
     * 判断是否需要发送短信和通知
     * @param resultList
     * @param alarmRuleAndAlarmRuleLevelVOList
     */
    public 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 -> {

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

            for (int i=0; i < userArr.length;i++){
                userList.add(userArr[i]);
            }

        });

        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("faultToleranceRate").toString()) > Double.parseDouble(alarmRuleAndAlarmRuleLevelVO.getAlarmDataOverStandardRate())){

                        //查询上一条报警是否处理,未处理就不新增报警记录
                        //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 = "";

                        if("1".equals(alarmRuleAndAlarmRuleLevelVO.getAlarmParamType())) {
                            //替换报警的内容
                            String compareCon = "";
                            String limitValue = "";
                            if (Double.parseDouble(resultMap.get("maxValue").toString()) > Double.parseDouble(alarmRuleAndAlarmRuleLevelVO.getAlarmParamUpperLimit())) {
                                compareCon = "高于";
                                limitValue = "最大值";
                            } else if (Double.parseDouble(resultMap.get("maxValue").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("${actualMinValue}", resultMap.get("minValue").toString())
                                    .replace("${actualMaxValue}", resultMap.get("maxValue").toString()).replace("${alarmParamUnit}", alarmRuleAndAlarmRuleLevelVO.getAlarmParamUnit())
                                    .replace("${compare}", compareCon).replace("${alarmRuleLevelName}", alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelName()).replace("${limitValue}", limitValue);
                        }else if("2".equals(alarmRuleAndAlarmRuleLevelVO.getAlarmParamType())){

                            //"【金控数矿】尊敬的用户,数矿特别提示:${date},${factoryName}采集数据,${alarmParamName}反馈故障,报警级别${alarmRuleLevelName},请您及时采取处理措施。";
                            msgContent = breakdownMsgContent.replace("${date}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())).replace("${factoryName}", alarmRuleAndAlarmRuleLevelVO.getFactoryName())
                                            .replace("${alarmParamName}", alarmRuleAndAlarmRuleLevelVO.getAlarmParamName()).replace("${alarmRuleLevelName}", alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelName());

                        }
                        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.setActualMinValue(resultMap.get("minValue").toString());
                            alarmRecordHistory.setActualMaxValue(resultMap.get("maxValue").toString());
                            alarmRecordHistory.setActualAvgValue(resultMap.get("avgValue").toString());
                            alarmRecordHistory.setHandleStatus(0);
                            alarmRecordHistory.setAlarmDataOverStandardRate(resultMap.get("faultToleranceRate").toString());

                            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(",","");

                                    //将发送的短信保存到数据库
                                    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.setActualMinValue(resultMap.get("minValue").toString());
                                    alarmMessageHistory.setActualMaxValue(resultMap.get("maxValue").toString());
                                    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.setActualMinValue(resultMap.get("minValue").toString());
                                alarmMessageHistory.setActualMaxValue(resultMap.get("maxValue").toString());
                                alarmMessageHistory.setActualAvgValue(resultMap.get("avgValue").toString());
                                //用phone存放接收通知的UUID
//                                alarmMessageHistory.setAlarmSendPhone(uuid);

                                alarmMessageHistoryList.add(alarmMessageHistory);
                            }


                        }*/

                    }


                }


            });


        });

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

    }

    /**
     * 通过指标超标集合和指标未超标集合,得到超标率和最大值,最小值,平均值
     * @param excessMap
     * @param noExcessMap
     * @return
     */
    public List<Map<String,Object>> getRestulList(Map<String, List<Double>> excessMap,Map<String, List<Double>> noExcessMap){

        List<Map<String,Object>>  resultList = new ArrayList<>();

        excessMap.forEach((alarmRuleLevelId,excessList)->{

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

            List<Double> noExcessList = noExcessMap.get(alarmRuleLevelId);

            int allCount = 0;
            if(noExcessList != null){
                allCount = excessList.size()+noExcessList.size();
            }else{
                allCount = excessList.size();
            }


            //计算容错率
            NumberFormat numberFormat = NumberFormat.getInstance();
            // 设置精确到小数点后2位
            numberFormat.setMaximumFractionDigits(2);
            String faultToleranceRate = numberFormat.format((double)excessList.size()/allCount*100);


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

            //取最大值
            double maxValue = statistics.getMax();
            //取最小值
            double minValue = statistics.getMin();
            //取平均值
            double avgValue = statistics.getAverage();


            resultMap.put("faultToleranceRate",faultToleranceRate);
            resultMap.put("maxValue",maxValue);
            resultMap.put("minValue",minValue);
            resultMap.put("avgValue",avgValue);
            resultMap.put("alarmRuleLevelId",alarmRuleLevelId);

            resultList.add(resultMap);

        });

        return resultList;
    }

    /**
     * 判断当前时间是否在[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;
        }


    }

    @GetMapping("/testSyncData")
    public void testSyncData(String reportDate,String hour){
        JdbcTemplate masterDB = (JdbcTemplate) SpringContextUtils.getBean("master");
        List<Map<String,Object>> dataList = new ArrayList<Map<String,Object>>();
        //获取厂站列表
        String factorySql= "select w.* " +
                " from water_quality_monitoring w " +
                " left join (select DISTINCT factory_id,'no' as status from report_water_data " +
                " where report_date = '"+reportDate+"' and report_time = '"+hour+"') d on w.factory_id = d.factory_id " +
                " where d.status is null ";
        List<Map<String, Object>> list = masterDB.queryForList(factorySql);
        String pgField = "";
        String factoryIds = "";
        //获取0点累计流量
        Map<String,Object> waterMap = new HashMap<>();
        waterMap = getLastWaterData(reportDate,0);
        for(Map<String, Object> vo : list) {
            //拼接指标
            if(ConvertUtils.isNotEmpty(vo.get("in_cod"))){
                pgField = pgField +","+vo.get("in_cod");
            }
            if(ConvertUtils.isNotEmpty(vo.get("in_ss"))){
                pgField = pgField +","+vo.get("in_ss");
            }
            if(ConvertUtils.isNotEmpty(vo.get("in_nh3"))){
                pgField = pgField +","+vo.get("in_nh3");
            }
            if(ConvertUtils.isNotEmpty(vo.get("in_ph"))){
                pgField = pgField +","+vo.get("in_ph");
            }
            if(ConvertUtils.isNotEmpty(vo.get("in_tp"))){
                pgField = pgField +","+vo.get("in_tp");
            }
            if(ConvertUtils.isNotEmpty(vo.get("in_tn"))){
                pgField = pgField +","+vo.get("in_tn");
            }
            if(ConvertUtils.isNotEmpty(vo.get("in_flow"))){
                pgField = pgField +","+vo.get("in_flow");
            }
            if(ConvertUtils.isNotEmpty(vo.get("out_flow"))){
                pgField = pgField +","+vo.get("out_flow");
            }
            if(ConvertUtils.isNotEmpty(vo.get("in_lj_flow"))){
                pgField = pgField +","+vo.get("in_lj_flow");
            }
            if(ConvertUtils.isNotEmpty(vo.get("out_lj_flow"))){
                pgField = pgField +","+vo.get("out_lj_flow");
            }
            if(ConvertUtils.isNotEmpty(vo.get("out_cod"))){
                pgField = pgField +","+vo.get("out_cod");
            }
            if(ConvertUtils.isNotEmpty(vo.get("out_ss"))){
                pgField = pgField +","+vo.get("out_ss");
            }
            if(ConvertUtils.isNotEmpty(vo.get("out_nh3"))){
                pgField = pgField +","+vo.get("out_nh3");
            }
            if(ConvertUtils.isNotEmpty(vo.get("out_tp"))){
                pgField = pgField +","+vo.get("out_tp");
            }
            if(ConvertUtils.isNotEmpty(vo.get("out_tn"))){
                pgField = pgField +","+vo.get("out_tn");
            }
            if(ConvertUtils.isNotEmpty(vo.get("out_ph"))){
                pgField = pgField +","+vo.get("out_ph");
            }
            if(ConvertUtils.isNotEmpty(vo.get("out_temperature"))){
                pgField = pgField +","+vo.get("out_temperature");
            }
            if(ConvertUtils.isNotEmpty(vo.get("do1"))){
                pgField = pgField +","+vo.get("do1");
            }
            if(ConvertUtils.isNotEmpty(vo.get("do2"))){
                pgField = pgField +","+vo.get("do2");
            }
            if(ConvertUtils.isNotEmpty(vo.get("do3"))){
                pgField = pgField +","+vo.get("do3");
            }
            if(ConvertUtils.isNotEmpty(vo.get("do4"))){
                pgField = pgField +","+vo.get("do4");
            }
            if(ConvertUtils.isNotEmpty(vo.get("orp1"))){
                pgField = pgField +","+vo.get("orp1");
            }
            if(ConvertUtils.isNotEmpty(vo.get("orp2"))){
                pgField = pgField +","+vo.get("orp2");
            }
            if(ConvertUtils.isNotEmpty(vo.get("orp3"))){
                pgField = pgField +","+vo.get("orp3");
            }
            if(ConvertUtils.isNotEmpty(vo.get("orp4"))){
                pgField = pgField +","+vo.get("orp4");
            }
            if(ConvertUtils.isNotEmpty(vo.get("orp5"))){
                pgField = pgField +","+vo.get("orp5");
            }
            if(ConvertUtils.isNotEmpty(vo.get("orp6"))){
                pgField = pgField +","+vo.get("orp6");
            }
            if(ConvertUtils.isNotEmpty(vo.get("orp7"))){
                pgField = pgField +","+vo.get("orp7");
            }
            if(ConvertUtils.isNotEmpty(vo.get("orp8"))){
                pgField = pgField +","+vo.get("orp8");
            }
            if(ConvertUtils.isNotEmpty(vo.get("mlss1"))){
                pgField = pgField +","+vo.get("mlss1");
            }
            if(ConvertUtils.isNotEmpty(vo.get("mlss2"))){
                pgField = pgField +","+vo.get("mlss2");
            }
            if(ConvertUtils.isNotEmpty(vo.get("mlss3"))){
                pgField = pgField +","+vo.get("mlss3");
            }
            if(ConvertUtils.isNotEmpty(vo.get("mlss4"))){
                pgField = pgField +","+vo.get("mlss4");
            }
            if(ConvertUtils.isNotEmpty(vo.get("tsl11"))){
                pgField = pgField +","+vo.get("tsl11");
            }
            if(ConvertUtils.isNotEmpty(vo.get("tsl12"))){
                pgField = pgField +","+vo.get("tsl12");
            }
            if(ConvertUtils.isNotEmpty(vo.get("tsl13"))){
                pgField = pgField +","+vo.get("tsl13");
            }
            if(ConvertUtils.isNotEmpty(vo.get("tsl14"))){
                pgField = pgField +","+vo.get("tsl14");
            }
            if(ConvertUtils.isNotEmpty(vo.get("tsl15"))){
                pgField = pgField +","+vo.get("tsl15");
            }
            if(ConvertUtils.isNotEmpty(vo.get("tsl16"))){
                pgField = pgField +","+vo.get("tsl16");
            }
            if(ConvertUtils.isNotEmpty(vo.get("tsl21"))){
                pgField = pgField +","+vo.get("tsl21");
            }
            if(ConvertUtils.isNotEmpty(vo.get("tsl22"))){
                pgField = pgField +","+vo.get("tsl22");
            }
            if(ConvertUtils.isNotEmpty(vo.get("tsl23"))){
                pgField = pgField +","+vo.get("tsl23");
            }
            if(ConvertUtils.isNotEmpty(vo.get("tsl24"))){
                pgField = pgField +","+vo.get("tsl24");
            }
            if(ConvertUtils.isNotEmpty(vo.get("tsl25"))){
                pgField = pgField +","+vo.get("tsl25");
            }
            if(ConvertUtils.isNotEmpty(vo.get("tsl26"))){
                pgField = pgField +","+vo.get("tsl26");
            }
            if(ConvertUtils.isNotEmpty(vo.get("kmyc11"))){
                pgField = pgField +","+vo.get("kmyc11");
            }
            if(ConvertUtils.isNotEmpty(vo.get("kmyc12"))){
                pgField = pgField +","+vo.get("kmyc12");
            }
            if(ConvertUtils.isNotEmpty(vo.get("kmyc13"))){
                pgField = pgField +","+vo.get("kmyc13");
            }
            if(ConvertUtils.isNotEmpty(vo.get("kmyc14"))){
                pgField = pgField +","+vo.get("kmyc14");
            }
            if(ConvertUtils.isNotEmpty(vo.get("kmyc15"))){
                pgField = pgField +","+vo.get("kmyc15");
            }
            if(ConvertUtils.isNotEmpty(vo.get("kmyc16"))){
                pgField = pgField +","+vo.get("kmyc16");
            }
            if(ConvertUtils.isNotEmpty(vo.get("kmyc21"))){
                pgField = pgField +","+vo.get("kmyc21");
            }
            if(ConvertUtils.isNotEmpty(vo.get("kmyc22"))){
                pgField = pgField +","+vo.get("kmyc22");
            }
            if(ConvertUtils.isNotEmpty(vo.get("kmyc23"))){
                pgField = pgField +","+vo.get("kmyc23");
            }
            if(ConvertUtils.isNotEmpty(vo.get("kmyc24"))){
                pgField = pgField +","+vo.get("kmyc24");
            }
            if(ConvertUtils.isNotEmpty(vo.get("kmyc25"))){
                pgField = pgField +","+vo.get("kmyc25");
            }
            if(ConvertUtils.isNotEmpty(vo.get("kmyc26"))){
                pgField = pgField +","+vo.get("kmyc26");
            }
            //拼接厂站
            factoryIds = factoryIds +","+vo.get("factory_id");
        }
        pgField = pgField.substring(1);
        factoryIds = factoryIds.substring(1);
        dataList = pgQueryService.queryFactoryMonitorFromRealTimeData(factoryIds,pgField);
        //将指标数据封装到map集合中
        Map<String,Object> monitorDataMap = new HashMap<String,Object>();
        for(Map<String,Object> map : dataList) {
            monitorDataMap.putAll(map);
        }
        for (Map<String, Object> factoryMap : list) {
            String factoryId = factoryMap.get("factory_id").toString();
            Map<String, Object> drljllMap = JSONObject.parseObject(JSONObject.toJSONString(waterMap.get(factoryId)), Map.class);
            String inFlow = null;String inLjFlow = null;String inCod = null;String inNh3 = null;
            String inTp = null;String inTn = null;String inPh = null;String inSs = null;String drjslj = null;
            String outFlow = null;String outLjFlow = null;String outCod = null;String outNh3 = null;
            String outTp = null;String outTn = null;String outPh = null;String outSs = null;String outWd = null;String drcslj = null;
            String do1 = null;String do2 = null;String do3 = null;String do4 = null;
            String orp1 = null;String orp2 = null;String orp3 = null;String orp4 = null;
            String orp5 = null;String orp6 = null;String orp7 = null;String orp8 = null;
            String mlss1 = null;String mlss2 = null;String mlss3 = null;String mlss4 = null;
            String tsl11 = null;String tsl12 = null;String tsl13 = null;String tsl14 = null;
            String tsl15 = null;String tsl16 = null;String tsl21 = null;String tsl22 = null;
            String tsl23 = null;String tsl24 = null;String tsl25 = null;String tsl26 = null;
            String kmyc11 = null;String kmyc12 = null;String kmyc13 = null;String kmyc14 = null;
            String kmyc15 = null;String kmyc16 = null;String kmyc21 = null;String kmyc22 = null;
            String kmyc23 = null;String kmyc24 = null;String kmyc25 = null;String kmyc26 = null;
            if(factoryMap.get("in_flow")!=null){
                inFlow = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("in_flow"))).toString();
            }
            if(factoryMap.get("in_ss")!=null){
                inSs = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("in_ss"))).toString();
            }
            if(factoryMap.get("in_lj_flow")!=null){
                inLjFlow = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("in_lj_flow"))).toString();
                if(Integer.parseInt(hour)!=0){
                    Object jsljll = drljllMap.get("jsljll");
                    if(!"".equals(jsljll)){
                        drjslj = String.valueOf(DoubleOperaUtils.bigDecimalRound(Double.valueOf(inLjFlow) - Double.valueOf(jsljll.toString()),4));
                    }
                }
            }
            if(factoryMap.get("in_cod")!=null){
                inCod = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("in_cod"))).toString();
            }
            if(factoryMap.get("in_nh3")!=null){
                inNh3 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("in_nh3"))).toString();
            }
            if(factoryMap.get("in_ph")!=null){
                inPh = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("in_ph"))).toString();
            }
            if(factoryMap.get("in_tp")!=null){
                inTp = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("in_tp"))).toString();
            }
            if(factoryMap.get("in_tn")!=null){
                inTn = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("in_tn"))).toString();
            }
            if(factoryMap.get("out_flow")!=null){
                outFlow = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("out_flow"))).toString();
            }
            if(factoryMap.get("out_ss")!=null){
                outSs = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("out_ss"))).toString();
            }
            if(factoryMap.get("out_lj_flow")!=null){
                outLjFlow = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("out_lj_flow"))).toString();
                if(Integer.parseInt(hour)!=0){
                    Object csljll = drljllMap.get("csljll");
                    if(!"".equals(csljll)){
                        drcslj = String.valueOf(DoubleOperaUtils.bigDecimalRound(Double.valueOf(outLjFlow) - Double.valueOf(csljll.toString()),4));
                    }
                }
            }
            if(factoryMap.get("out_cod")!=null){
                outCod = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("out_cod"))).toString();
            }
            if(factoryMap.get("out_nh3")!=null){
                outNh3 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("out_nh3"))).toString();
            }
            if(factoryMap.get("out_tp")!=null){
                outTp = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("out_tp"))).toString();
            }
            if(factoryMap.get("out_tn")!=null){
                outTn = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("out_tn"))).toString();
            }
            if(factoryMap.get("out_ph")!=null){
                outPh = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("out_ph"))).toString();
            }
            if(factoryMap.get("out_temperature")!=null){
                outWd = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("out_temperature"))).toString();
            }
            if(factoryMap.get("do1")!=null){
                do1 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("do1"))).toString();
            }
            if(factoryMap.get("do2")!=null){
                do2 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("do2"))).toString();
            }
            if(factoryMap.get("do3")!=null){
                do3 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("do3"))).toString();
            }
            if(factoryMap.get("do4")!=null){
                do4 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("do4"))).toString();
            }
            if(factoryMap.get("orp1")!=null){
                orp1 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("orp1"))).toString();
            }
            if(factoryMap.get("orp2")!=null){
                orp2 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("orp2"))).toString();
            }
            if(factoryMap.get("orp3")!=null){
                orp3 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("orp3"))).toString();
            }
            if(factoryMap.get("orp4")!=null){
                orp4 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("orp4"))).toString();
            }
            if(factoryMap.get("orp5")!=null){
                orp5 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("orp5"))).toString();
            }
            if(factoryMap.get("orp6")!=null){
                orp6 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("orp6"))).toString();
            }
            if(factoryMap.get("orp7")!=null){
                orp7 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("orp7"))).toString();
            }
            if(factoryMap.get("orp8")!=null){
                orp8 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("orp8"))).toString();
            }
            if(factoryMap.get("mlss1")!=null){
                mlss1 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("mlss1"))).toString();
            }
            if(factoryMap.get("mlss2")!=null){
                mlss2 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("mlss2"))).toString();
            }
            if(factoryMap.get("mlss3")!=null){
                mlss3 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("mlss3"))).toString();
            }
            if(factoryMap.get("mlss4")!=null){
                mlss4 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("mlss4"))).toString();
            }
            if(factoryMap.get("tsl11")!=null){
                tsl11 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("tsl11"))).toString();
            }
            if(factoryMap.get("tsl12")!=null){
                tsl12 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("tsl12"))).toString();
            }
            if(factoryMap.get("tsl13")!=null){
                tsl13 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("tsl13"))).toString();
            }
            if(factoryMap.get("tsl14")!=null){
                tsl14 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("tsl14"))).toString();
            }
            if(factoryMap.get("tsl15")!=null){
                tsl15 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("tsl15"))).toString();
            }
            if(factoryMap.get("tsl16")!=null){
                tsl16 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("tsl16"))).toString();
            }
            if(factoryMap.get("tsl21")!=null){
                tsl21 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("tsl21"))).toString();
            }
            if(factoryMap.get("tsl22")!=null){
                tsl22 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("tsl22"))).toString();
            }
            if(factoryMap.get("tsl23")!=null){
                tsl23 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("tsl23"))).toString();
            }
            if(factoryMap.get("tsl24")!=null){
                tsl24 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("tsl24"))).toString();
            }
            if(factoryMap.get("tsl25")!=null){
                tsl25 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("tsl25"))).toString();
            }
            if(factoryMap.get("tsl26")!=null){
                tsl26 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("tsl26"))).toString();
            }
            if(factoryMap.get("kmyc11")!=null){
                kmyc11 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("kmyc11"))).toString();
            }
            if(factoryMap.get("kmyc12")!=null){
                kmyc12 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("kmyc12"))).toString();
            }
            if(factoryMap.get("kmyc13")!=null){
                kmyc13 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("kmyc13"))).toString();
            }
            if(factoryMap.get("kmyc14")!=null){
                kmyc14 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("kmyc14"))).toString();
            }
            if(factoryMap.get("kmyc15")!=null){
                kmyc15 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("kmyc15"))).toString();
            }
            if(factoryMap.get("kmyc16")!=null){
                kmyc16 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("kmyc16"))).toString();
            }
            if(factoryMap.get("kmyc21")!=null){
                kmyc21 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("kmyc21"))).toString();
            }
            if(factoryMap.get("kmyc22")!=null){
                kmyc22 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("kmyc22"))).toString();
            }
            if(factoryMap.get("kmyc23")!=null){
                kmyc23 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("kmyc23"))).toString();
            }
            if(factoryMap.get("kmyc24")!=null){
                kmyc24 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("kmyc24"))).toString();
            }
            if(factoryMap.get("kmyc25")!=null){
                kmyc25 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("kmyc25"))).toString();
            }
            if(factoryMap.get("kmyc26")!=null){
                kmyc26 = monitorDataMap.get(ConvertUtils.getString(factoryMap.get("kmyc26"))).toString();
            }
            ReportWaterData reportWaterData = new ReportWaterData();
            if(Integer.parseInt(hour)!=0){
                reportWaterData.setReportDrjslj(drjslj);
                reportWaterData.setReportDrcslj(drcslj);
            }else{
                reportWaterData.setReportDrjslj("0");
                reportWaterData.setReportDrcslj("0");
            }
            reportWaterData.setFactoryId(factoryMap.get("factory_id").toString());
            reportWaterData.setReportDate(reportDate);
            reportWaterData.setReportTime(String.valueOf(hour));
            reportWaterData.setReportJscod(inCod);
            reportWaterData.setReportJstn(inTn);
            reportWaterData.setReportJstp(inTp);
            reportWaterData.setReportJsssll(inFlow);
            reportWaterData.setReportJsljll(inLjFlow);
            reportWaterData.setReportJsnh3n(inNh3);
            reportWaterData.setReportJsss(inSs);
            reportWaterData.setReportJsph(inPh);
            reportWaterData.setReportCsssll(outFlow);
            reportWaterData.setReportCsljll(outLjFlow);
            reportWaterData.setReportCscod(outCod);
            reportWaterData.setReportCsnh3n(outNh3);
            reportWaterData.setReportCstn(outTn);
            reportWaterData.setReportCstp(outTp);
            reportWaterData.setReportCsph(outPh);
            reportWaterData.setReportCsss(outSs);
            reportWaterData.setReportCswd(outWd);
            reportWaterData.setReportDo1(do1);
            reportWaterData.setReportDo2(do2);
            reportWaterData.setReportDo3(do3);
            reportWaterData.setReportDo4(do4);
            reportWaterData.setReportOrp1(orp1);
            reportWaterData.setReportOrp2(orp2);
            reportWaterData.setReportOrp3(orp3);
            reportWaterData.setReportOrp4(orp4);
            reportWaterData.setReportOrp5(orp5);
            reportWaterData.setReportOrp6(orp6);
            reportWaterData.setReportOrp7(orp7);
            reportWaterData.setReportOrp8(orp8);
            reportWaterData.setReportMlss1(mlss1);
            reportWaterData.setReportMlss2(mlss2);
            reportWaterData.setReportMlss3(mlss3);
            reportWaterData.setReportMlss4(mlss4);
            reportWaterData.setReportTsl11(tsl11);
            reportWaterData.setReportTsl12(tsl12);
            reportWaterData.setReportTsl13(tsl13);
            reportWaterData.setReportTsl14(tsl14);
            reportWaterData.setReportTsl15(tsl15);
            reportWaterData.setReportTsl16(tsl16);
            reportWaterData.setReportTsl21(tsl21);
            reportWaterData.setReportTsl22(tsl22);
            reportWaterData.setReportTsl23(tsl23);
            reportWaterData.setReportTsl24(tsl24);
            reportWaterData.setReportTsl25(tsl25);
            reportWaterData.setReportTsl26(tsl26);
            reportWaterData.setReportKmyc11(kmyc11);
            reportWaterData.setReportKmyc12(kmyc12);
            reportWaterData.setReportKmyc13(kmyc13);
            reportWaterData.setReportKmyc14(kmyc14);
            reportWaterData.setReportKmyc15(kmyc15);
            reportWaterData.setReportKmyc16(kmyc16);
            reportWaterData.setReportKmyc21(kmyc21);
            reportWaterData.setReportKmyc22(kmyc22);
            reportWaterData.setReportKmyc23(kmyc23);
            reportWaterData.setReportKmyc24(kmyc24);
            reportWaterData.setReportKmyc25(kmyc25);
            reportWaterData.setReportKmyc26(kmyc26);
            reportWaterDataService.save(reportWaterData);
        }
    }

    private Map<String,Object> getLastWaterData(String reportDate, int hour) {
        Map<String,Object> map = reportWaterDataService.getLastWaterData(reportDate, hour);
        return map;
    }

    @GetMapping("/testSMS")
    public void testSMS(String phones){
        HttpSenderMsg.sendMsg("测试短信", phones);
    }

    @GetMapping("/testSMSToWeb")
    public void testSMSToWeb(){
        Date date = new Date();
        int nowHour = date.getHours();
        String today = DateUtils.format(date,"yyyy-MM-dd");
        if(nowHour!=0){
            String parentId = "ee0f983f190f4da4a2e19cba70370ea7";
            List<SysDepart> list = sysDepartService.getFactoryListByParent(parentId);
            sendMsgToWeb(list,today,nowHour);
        }
    }

    private void sendMsgToWeb(List<SysDepart> list,String today,int hour) {
        Double d = new Double(Math.floor((hour/2)+1));
        int allCount = d.intValue();
        if(list.size()>0){
            for (SysDepart dept : list) {
                String factoryId = dept.getId();
                int haveCount = reportWaterDataService.getDataCount(factoryId,today);
                if(allCount-haveCount > 0){
                    List<String> userIds= iSysUserDepartService.queryUserIdByfactoryId(factoryId);
                    sendMsg(userIds,factoryId,dept.getDepartName(),allCount-haveCount);
                }
            }
        }

    }

    private void sendMsg(List<String> userIds,String factoryId,String factoryName,int count) {
        String sendMsg = "截止当前时间"+factoryName+"存在"+count+"条未填报情况,请及时填写工艺运行记录表!";
        String usersId = "";
        for (int i = 0; i < userIds.size(); i++) {
            String userId = userIds.get(i);
            usersId = usersId + "," + userId;
        }
        //发送站内通知
        SysAnnouncement sysAnnouncement = new SysAnnouncement();
        //生成uuid
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        sysAnnouncement.setId(uuid);
        sysAnnouncement.setTitile(factoryName+"工艺运行记录表填报通知");
        sysAnnouncement.setMsgContent(sendMsg);
        sysAnnouncement.setPriority(CommonConstant.PRIORITY_M);
        sysAnnouncement.setMsgType(CommonConstant.MSG_TYPE_UESR);
        sysAnnouncement.setSendStatus(CommonConstant.HAS_SEND);
        sysAnnouncement.setSendTime(new Date());
        sysAnnouncement.setStartTime(new Date());
        sysAnnouncement.setEndTime(getDateByAdd(new Date(), 2));
        sysAnnouncement.setUserIds(usersId);
        sysAnnouncement.setFactoryId(factoryId);
        sysAnnouncement.setMsgCategory(CommonConstant.MSG_CATEGORY_2);
        sysAnnouncement.setDelFlag(String.valueOf(CommonConstant.DEL_FLAG_0));
        sysAnnouncement.setSender("admin");
        sysAnnouncementService.save(sysAnnouncement);

        for (String userid : userIds) {
            SysAnnouncementSend announcementSend = new SysAnnouncementSend();
            announcementSend.setAnntId(sysAnnouncement.getId());
            announcementSend.setUserId(userid);
            announcementSend.setReadFlag(CommonConstant.NO_READ_FLAG);
            sysAnnouncementSendService.save(announcementSend);
        }
        JSONObject obj = new JSONObject();
        obj.put("cmd", "topic");
        obj.put("msgId", sysAnnouncement.getId());
        obj.put("msgTxt", sysAnnouncement.getMsgContent());
        webSocket.sendOneMessage("e9ca23d68d884d4ebb19d07889727dae",obj.toJSONString());
    }

    /**
     * @description   获取*单位后的时间
     * @param num 时间单位数(如果num数为负数,说明是此日期前的天数)
     */
    public static Date getDateByAdd(Date date, int num) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, num);
        return cal.getTime();
    }
}