jinpengyong
2024-04-22 4b4a35fc7cc56a8b304ca35f0a9ec0bdd2e0e036
screen-api/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java
@@ -350,7 +350,7 @@
            Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
            Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
            List<String> tableNames = MybatisPLUSUtils.getTableNamesByWrapper(startDate, endDate, SeparateTableType.MONTH);
            for (String mac:macs) {
/*            for (String mac:macs) {
                Map<String, Object> mapParams = new HashMap<>();
                mapParams.put("startDate",startDate);
                mapParams.put("endDate",endDate);
@@ -384,6 +384,56 @@
                    historyHourlyMap.put("time",timeStr);
                    list.add(historyHourlyMap);
                }
            }*/
            for (String mac:macs) {
                Map<String, Object> mapParams = new HashMap<>();
                mapParams.put("startDate",startDate);
                mapParams.put("endDate",endDate);
                mapParams.put("mac",mac);
                List<HistoryFiveMinutely> HistoryFiveMinutelys = FiveMinuteTableQuery(mapParams, tableNames);
                HistoryFiveMinutelys = HistoryFiveMinutelys.stream().distinct().collect(Collectors.toList());
                List<HistoryFiveMinutely> distinctHistoryHourlies = new ArrayList<>();
                Map<String, Object> disMap = new HashMap<>();
                for (HistoryFiveMinutely historyFiveMinutely:HistoryFiveMinutelys) {
                    Date time = historyFiveMinutely.getTime();
                    String timeStr = DateUtils.dateToDateString(time,DateUtils.yyyy_MM_dd_HH_mm_EN);
                    if (!disMap.containsKey(timeStr)){
                        distinctHistoryHourlies.add(historyFiveMinutely);
                        disMap.put(timeStr,true);
                    }
                }
                for (HistoryFiveMinutely historyFiveMinutely:distinctHistoryHourlies) {
                    Map<String,Object> historyHourlyMap = new HashMap<>();
                    historyHourlyMap.put("mac",mac);
                    JSONObject  value= JSONObject.parseObject(historyFiveMinutely.getValue());
                    Double sensorValue = Objects.nonNull(value)&&Objects.nonNull(value.get(sensorCode))?Double.parseDouble(value.get(sensorCode).toString()):0d;
                    historyHourlyMap.put(sensorCode,sensorValue);
                    Date time = historyFiveMinutely.getTime();
                    String timeStr = DateUtils.dateToDateString(time, DateUtils.yyyy_MM_dd_HH_mm_EN);
                    historyHourlyMap.put("time",timeStr);
                    list.add(historyHourlyMap);
                }
            }
            Date middleDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_mm_EN);
            while (DateUtils.compareDateStr(endTime,DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_mm_EN),DateUtils.yyyy_MM_dd_HH_mm_EN)<=0){
                for (String mac:macs) {
                    boolean flag = true;
                    for (Map map:list) {
                        if (map.get("time").equals(DateUtils.dateToDateString(middleDate, DateUtils.yyyy_MM_dd_HH_mm_EN)) && map.get("mac").toString().equals(mac.toString())){
                            flag = false;
                        }
                    }
                    if (flag){
                        Map<String,Object> historyHourlyMap = new HashMap<>();
                        historyHourlyMap.put("mac",mac);
                        historyHourlyMap.put(sensorCode,"0.0");
                        historyHourlyMap.put("time",DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_mm_EN));
                        list.add(historyHourlyMap);
                    }
                }
                middleDate = DateUtils.getDateOfMin(middleDate,5);
            }
        }
        for (Map map:list) {
@@ -558,9 +608,10 @@
        long hour = (diff / nh)+1; //获取间隔几个小时
        Map<String, List<OnlineRateVo>> collectList=new HashMap<>();
        List<OnlineRateVo> OnlineRateVoList = deviceMapper.getLists(onlineRatePageCond.getOrganizationId(),onlineRatePageCond.getState());
//        List<OnlineRateVo> OnlineRateVoList = deviceMapper.getLists(onlineRatePageCond.getOrganizationId(),onlineRatePageCond.getState());
        List<OnlineRateVo> OnlineRateVoList = deviceMapper.getLists(onlineRatePageCond.getMacs(),onlineRatePageCond.getState());
        if (!ObjectUtils.isEmpty(OnlineRateVoList)){
            collectList = OnlineRateVoList.stream().collect(Collectors.groupingBy(OnlineRateVo::getHourState));
            for (OnlineRateVo onlineRateVo : OnlineRateVoList) {
                List<HistoryHourly> valueByMacAndTime = historyHourlyService.getValueByMacAndTime(onlineRateVo.getMac(), start, end);
                ArrayList<Date> dates = new ArrayList<>();
@@ -685,7 +736,9 @@
                }
            });
        }*/
        if (!ObjectUtils.isEmpty(OnlineRateVoList)){
            collectList = OnlineRateVoList.stream().collect(Collectors.groupingBy(OnlineRateVo::getHourState));
        }
        return ObjectUtils.isEmpty(onlineRatePageCond.getHourState())?OnlineRateVoList:collectList.get(onlineRatePageCond.getHourState());
    }
@@ -744,11 +797,21 @@
                        }
                    }else {
                        if (flag){
                            EndDates.add(start);
                            flag=false;
                            Date date = new Date();
                            String dateString = DateUtils.dateToDateString(date, DateUtils.yyyy_MM_dd_HH_EN);
                            Date date1 = DateUtils.getDate(dateString, DateUtils.yyyy_MM_dd_HH_EN);
                            if (!date1.equals(start)){
                                EndDates.add(start);
                                flag=false;
                            }
                        }
                        map.put(DateUtils.dateToDateString(start),0);
                    }
                    start = DateUtils.getDateAddHour(start,1);
                }
            }else {
                while (DateUtils.isTimeBeforE(end,start)){
                    map.put(DateUtils.dateToDateString(start),0);
                    start = DateUtils.getDateAddHour(start,1);
                }
            }
@@ -791,11 +854,8 @@
            rsMap.put("tabulation",OrfList);
            //离线多少个小时
            int endNumber = map.size() - dates.size();
            double number = (double) dates.size() / map.size() * 100;
            String result = String.format("%.2f", number);
            rsMap.put("pieChart1",endNumber);
            rsMap.put("pieChart2",dates.size());
//            rsMap.put("code","共"+map.size()+"小时,"+"在线"+dates.size()+"小时,"+"在线率"+(result.equals("NaN")?"0":result));
            rsMap.put("sum",map.size());
        } else if (type.equals("day")){
@@ -826,7 +886,11 @@
                        }
                        map.put(DateUtils.dateToDateString(start),0);
                    }
//                    start = DateUtils.getDateAddHour(start,24);
                    start = DateUtils.getDateOfDay(start,1);
                }
            }else {
                while (DateUtils.isTimeBeforE(end,start)){
                    map.put(DateUtils.dateToDateString(start),0);
                    start = DateUtils.getDateOfDay(start,1);
                }
            }
@@ -850,12 +914,6 @@
                            onlineRateLogsForm.setMun(Long.toString(Math.abs(l/nd)));
                        }
                    }
//                    if (!ObjectUtils.isEmpty(StartDates)){
//                        long l = EndDates.get(i).getTime() - StartDates.get(i).getTime();
//                        onlineRateLogsForm.setMun(Long.toString(l/nd));
//                    }else {
//                        onlineRateLogsForm.setMun("-");
//                    }
                    onlineRateLogsForm.setDate("天");
                    OrfList.add(onlineRateLogsForm);
                }
@@ -869,21 +927,24 @@
                list2.add(Integer.parseInt(map.get(string).toString()));
            }
            rsMap.put("lineChart1",list1);
            rsMap.put("lineChart2",list2);
            rsMap.put("tabulation",OrfList);
            //离线多少天
            int endNumber = map.size() - dates.size();
            double number = (double) dates.size() / map.size() * 100;
            String result = String.format("%.2f", number);
            rsMap.put("pieChart1",endNumber);
            rsMap.put("pieChart2",dates.size());
//            rsMap.put("code","共"+map.size()+"天,"+"在线"+dates.size()+"天,"+"在线率"+(result.equals("NaN")?"0":result));
            rsMap.put("sum",map.size());
        }else {
            Date start = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_mm_EN);
            Date end = DateUtils.getDate(endTime, DateUtils.yyyy_MM_dd_HH_mm_EN);
            Date date = new Date();
            String dateString = DateUtils.dateToDateString(date, DateUtils.yyyy_MM_dd_HH_mm_EN);
            Date date1 = DateUtils.getDate(dateString, DateUtils.yyyy_MM_dd_HH_mm_EN);
            if (end.equals(date1)){
                String dateOfMin = DateUtils.getDateOfMin(dateString, -1, DateUtils.yyyy_MM_dd_HH_mm_EN);
                end = DateUtils.getDate(dateOfMin, DateUtils.yyyy_MM_dd_HH_mm_EN);
            }
            List<HistoryMinutely> historyMinutelys = historyHourlyService.getHistoryMinutely(mac, start, end);
            if (!ObjectUtils.isEmpty(historyMinutelys)){
                //数据去重
@@ -909,7 +970,11 @@
                        }
                        map.put(DateUtils.dateToDateString(start),0);
                    }
//                    start = DateUtils.getDateAddHour(start,24);
                    start = DateUtils.getDateOfMin(start,1);
                }
            }else {
                while (DateUtils.isTimeBeforE(end,start)){
                    map.put(DateUtils.dateToDateString(start),0);
                    start = DateUtils.getDateOfMin(start,1);
                }
            }
@@ -933,12 +998,6 @@
                            onlineRateLogsForm.setMun(Long.toString(Math.abs(l/nm)));
                        }
                    }
//                    if (!ObjectUtils.isEmpty(StartDates)){
//                        long l = EndDates.get(i).getTime() - StartDates.get(i).getTime();
//                        onlineRateLogsForm.setMun(Long.toString(l/nm));
//                    }else {
//                        onlineRateLogsForm.setMun("-");
//                    }
                    onlineRateLogsForm.setDate("分钟");
                    OrfList.add(onlineRateLogsForm);
                }
@@ -956,17 +1015,40 @@
            rsMap.put("tabulation",OrfList);
            //离线多少天
            int endNumber = map.size() - dates.size();
//            double number = (double) dates.size() / map.size() * 100;
//            String result = String.format("%.2f", number);
            rsMap.put("pieChart1",endNumber);
            rsMap.put("pieChart2",dates.size());
//            rsMap.put("code","共"+map.size()+"分钟,"+"在线"+dates.size()+"分钟,"+"在线率"+(result.equals("NaN")?"0":result));
            rsMap.put("sum",map.size());
        }
        return rsMap;
    }
    @Override
    public Map<String,Object> getStart(List<String> macs) {
        HashMap<String, Object> map = new HashMap<>();
        List<OnlineRateVo> lists = deviceMapper.getLists(macs, null);
        if (!ObjectUtils.isEmpty(lists)){
            Map<String, List<OnlineRateVo>> collect = lists.stream().collect(Collectors.groupingBy(OnlineRateVo::getState));
            int size1 = lists.size();
            List<OnlineRateVo> onlineRateVos = collect.get("0");
            int i ;
            if (!ObjectUtils.isEmpty(onlineRateVos)){
                int size = onlineRateVos.size();
                 i = size1 - size;
            }else {
                 i= size1;
            }
            //总数
            map.put("sum",lists.size());
            //在线
            map.put("online",i);
            double number = (double) i / size1 * 100;
            String result = String.format("%.2f", number);
            map.put("onlineRate",result);
        }
        return map;
    }
    @Override
    public Map<String, Object> detailV1(String mac, String startTime, String endTime, String type) {
        HashMap<String, Object> rsMap = new HashMap<>();
        Map<String, Object> map = new TreeMap<>(