Commit 402f6f6d8da04fe390884be836eff3fe1dc6e102
1 parent
37296d1b
fix(scheduler): 今日能耗查询跳过未到小时并修正状态时长统计
Showing
1 changed file
with
122 additions
and
11 deletions
| ... | ... | @@ -103,6 +103,12 @@ public class EnergySearchService { |
| 103 | 103 | |
| 104 | 104 | Object kwhRawData = Collections.emptyList(); |
| 105 | 105 | BigDecimal totalKwh = BigDecimal.ZERO; |
| 106 | + long totalDuration = 0; | |
| 107 | + Map<Integer, Long> statusDurationMap = new LinkedHashMap<>(); | |
| 108 | + statusDurationMap.put(0, 0L); | |
| 109 | + statusDurationMap.put(1, 0L); | |
| 110 | + statusDurationMap.put(2, 0L); | |
| 111 | + statusDurationMap.put(3, 0L); | |
| 106 | 112 | try { |
| 107 | 113 | String kwhSql = "SELECT description FROM " + eqKwhTableName + " WHERE corp_code = ? AND dtuSn = ? AND use_date = ?"; |
| 108 | 114 | Map<String, Object> kwhRow = jt.queryForMap(kwhSql, corpCode, dtuSn, date + " 00:00:00"); |
| ... | ... | @@ -110,10 +116,36 @@ public class EnergySearchService { |
| 110 | 116 | String description = String.valueOf(kwhRow.get("description")); |
| 111 | 117 | kwhRawData = JSON.parseArray(description); |
| 112 | 118 | JSONArray dataArray = JSON.parseArray(description); |
| 119 | + boolean isToday = date.equals(new SimpleDateFormat("yyyy-MM-dd").format(new Date())); | |
| 120 | + int currentHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); | |
| 113 | 121 | for (int i = 0; i < dataArray.size(); i++) { |
| 114 | 122 | JSONObject item = dataArray.getJSONObject(i); |
| 123 | + // 如果查询的是今天,跳过未到当前时间的小时数据 | |
| 124 | + if (isToday) { | |
| 125 | + String startTime = item.getString("startTime"); | |
| 126 | + if (startTime != null && startTime.length() >= 13) { | |
| 127 | + try { | |
| 128 | + int itemHour = Integer.parseInt(startTime.substring(11, 13)); | |
| 129 | + if (itemHour >= currentHour) continue; | |
| 130 | + } catch (NumberFormatException ignored) {} | |
| 131 | + } | |
| 132 | + } | |
| 115 | 133 | Double value = item.getDouble("value"); |
| 116 | 134 | if (value != null) totalKwh = totalKwh.add(BigDecimal.valueOf(value)); |
| 135 | + boolean allStatusZero = true; | |
| 136 | + for (int statusKey = 0; statusKey <= 3; statusKey++) { | |
| 137 | + Long dur = item.getLong(String.valueOf(statusKey)); | |
| 138 | + if (dur != null && dur > 0) { | |
| 139 | + allStatusZero = false; | |
| 140 | + totalDuration += dur; | |
| 141 | + statusDurationMap.merge(statusKey, dur, Long::sum); | |
| 142 | + } | |
| 143 | + } | |
| 144 | + // 该条小时数据的所有状态时间都为0时,默认设为待机(2)=3600秒 | |
| 145 | + if (allStatusZero) { | |
| 146 | + totalDuration += 3600L; | |
| 147 | + statusDurationMap.merge(2, 3600L, Long::sum); | |
| 148 | + } | |
| 117 | 149 | } |
| 118 | 150 | } |
| 119 | 151 | } catch (Exception e) { |
| ... | ... | @@ -121,12 +153,6 @@ public class EnergySearchService { |
| 121 | 153 | } |
| 122 | 154 | |
| 123 | 155 | Object oeeRawData = Collections.emptyList(); |
| 124 | - long totalDuration = 0; | |
| 125 | - Map<Integer, Long> statusDurationMap = new LinkedHashMap<>(); | |
| 126 | - statusDurationMap.put(0, 0L); | |
| 127 | - statusDurationMap.put(1, 0L); | |
| 128 | - statusDurationMap.put(2, 0L); | |
| 129 | - statusDurationMap.put(3, 0L); | |
| 130 | 156 | try { |
| 131 | 157 | String oeeSql = "SELECT runStatus1, runStatus2 FROM " + eRunDtlTableName + " WHERE corp_code = ? AND dtuSn = ? AND use_date = ?"; |
| 132 | 158 | Map<String, Object> oeeRow = jt.queryForMap(oeeSql, corpCode, dtuSn, date + " 00:00:00"); |
| ... | ... | @@ -214,19 +240,40 @@ public class EnergySearchService { |
| 214 | 240 | if (kwhRow != null && kwhRow.get("description") != null) { |
| 215 | 241 | String desc = String.valueOf(kwhRow.get("description")); |
| 216 | 242 | JSONArray dataArray = JSON.parseArray(desc); |
| 243 | + boolean isToday = dateStr.equals(new SimpleDateFormat("yyyy-MM-dd").format(new Date())); | |
| 244 | + int currentHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); | |
| 217 | 245 | for (int i = 0; i < dataArray.size(); i++) { |
| 218 | 246 | JSONObject item = dataArray.getJSONObject(i); |
| 247 | + // 如果查询的是今天,跳过未到当前时间的小时数据 | |
| 248 | + if (isToday) { | |
| 249 | + String startTime = item.getString("startTime"); | |
| 250 | + if (startTime != null && startTime.length() >= 13) { | |
| 251 | + try { | |
| 252 | + int itemHour = Integer.parseInt(startTime.substring(11, 13)); | |
| 253 | + if (itemHour >= currentHour) continue; | |
| 254 | + } catch (NumberFormatException ignored) {} | |
| 255 | + } | |
| 256 | + } | |
| 219 | 257 | Double value = item.getDouble("value"); |
| 220 | 258 | if (value != null) dayKwh = dayKwh.add(BigDecimal.valueOf(value)); |
| 259 | + boolean allStatusZero = true; | |
| 221 | 260 | for (int statusKey = 0; statusKey <= 3; statusKey++) { |
| 222 | 261 | Long dur = item.getLong(String.valueOf(statusKey)); |
| 223 | 262 | if (dur != null && dur > 0) { |
| 263 | + allStatusZero = false; | |
| 224 | 264 | dayTotalDuration += dur; |
| 225 | 265 | totalDurationAll += dur; |
| 226 | 266 | dayStatusDuration.merge(statusKey, dur, Long::sum); |
| 227 | 267 | statusDurationAllMap.merge(statusKey, dur, Long::sum); |
| 228 | 268 | } |
| 229 | 269 | } |
| 270 | + // 该条小时数据的所有状态时间都为0时,默认设为待机(2)=3600秒 | |
| 271 | + if (allStatusZero) { | |
| 272 | + dayTotalDuration += 3600L; | |
| 273 | + totalDurationAll += 3600L; | |
| 274 | + dayStatusDuration.merge(2, 3600L, Long::sum); | |
| 275 | + statusDurationAllMap.merge(2, 3600L, Long::sum); | |
| 276 | + } | |
| 230 | 277 | } |
| 231 | 278 | kwhRawData = dataArray; |
| 232 | 279 | } |
| ... | ... | @@ -515,20 +562,42 @@ public class EnergySearchService { |
| 515 | 562 | try { |
| 516 | 563 | JSONArray dataArray = JSON.parseArray(desc); |
| 517 | 564 | if (dataArray == null) continue; |
| 565 | + String rowDate = String.valueOf(row.get("use_date")); | |
| 566 | + String dateStr = rowDate.length() > 10 ? rowDate.substring(0, 10) : rowDate; | |
| 567 | + boolean isToday = dateStr.equals(new SimpleDateFormat("yyyy-MM-dd").format(new Date())); | |
| 568 | + int currentHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); | |
| 518 | 569 | for (int i = 0; i < dataArray.size(); i++) { |
| 519 | 570 | JSONObject item = dataArray.getJSONObject(i); |
| 520 | 571 | if (item == null) continue; |
| 572 | + // 如果查询的是今天,跳过未到当前时间的小时数据 | |
| 573 | + if (isToday) { | |
| 574 | + String startTime = item.getString("startTime"); | |
| 575 | + if (startTime != null && startTime.length() >= 13) { | |
| 576 | + try { | |
| 577 | + int itemHour = Integer.parseInt(startTime.substring(11, 13)); | |
| 578 | + if (itemHour >= currentHour) continue; | |
| 579 | + } catch (NumberFormatException ignored) {} | |
| 580 | + } | |
| 581 | + } | |
| 521 | 582 | Double value = item.getDouble("value"); |
| 522 | 583 | if (value != null) dsr.totalKwh += value; |
| 584 | + boolean allStatusZero = true; | |
| 523 | 585 | for (int sk = 0; sk <= 3; sk++) { |
| 524 | 586 | Long dur = item.getLong(String.valueOf(sk)); |
| 525 | - if (dur != null && dur > 0) switch (sk) { | |
| 526 | - case 0 -> dsr.status0 += dur; | |
| 527 | - case 1 -> dsr.status1 += dur; | |
| 528 | - case 2 -> dsr.status2 += dur; | |
| 529 | - case 3 -> dsr.status3 += dur; | |
| 587 | + if (dur != null && dur > 0) { | |
| 588 | + allStatusZero = false; | |
| 589 | + switch (sk) { | |
| 590 | + case 0 -> dsr.status0 += dur; | |
| 591 | + case 1 -> dsr.status1 += dur; | |
| 592 | + case 2 -> dsr.status2 += dur; | |
| 593 | + case 3 -> dsr.status3 += dur; | |
| 594 | + } | |
| 530 | 595 | } |
| 531 | 596 | } |
| 597 | + // 该条小时数据的所有状态时间都为0时,默认设为待机(2)=3600秒 | |
| 598 | + if (allStatusZero) { | |
| 599 | + dsr.status2 += 3600L; | |
| 600 | + } | |
| 532 | 601 | } |
| 533 | 602 | } catch (Exception e) { |
| 534 | 603 | log.warn("解析eq_kwh description异常 - dtuSn:{}", sn, e); |
| ... | ... | @@ -750,20 +819,41 @@ public class EnergySearchService { |
| 750 | 819 | BigDecimal dayKwh = BigDecimal.ZERO; |
| 751 | 820 | long dayDur = 0; |
| 752 | 821 | Map<Integer, Long> dayStatus = initStatusMap(); |
| 822 | + boolean isToday = d.equals(new SimpleDateFormat("yyyy-MM-dd").format(new Date())); | |
| 823 | + int currentHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); | |
| 753 | 824 | for (int i = 0; i < dataArray.size(); i++) { |
| 754 | 825 | JSONObject item = dataArray.getJSONObject(i); |
| 755 | 826 | if (item == null) continue; |
| 827 | + // 如果查询的是今天,跳过未到当前时间的小时数据 | |
| 828 | + if (isToday) { | |
| 829 | + String startTime = item.getString("startTime"); | |
| 830 | + if (startTime != null && startTime.length() >= 13) { | |
| 831 | + try { | |
| 832 | + int itemHour = Integer.parseInt(startTime.substring(11, 13)); | |
| 833 | + if (itemHour >= currentHour) continue; | |
| 834 | + } catch (NumberFormatException ignored) {} | |
| 835 | + } | |
| 836 | + } | |
| 756 | 837 | Double value = item.getDouble("value"); |
| 757 | 838 | if (value != null) dayKwh = dayKwh.add(BigDecimal.valueOf(value)); |
| 839 | + boolean allStatusZero = true; | |
| 758 | 840 | for (int sk = 0; sk <= 3; sk++) { |
| 759 | 841 | Long dur = item.getLong(String.valueOf(sk)); |
| 760 | 842 | if (dur != null && dur > 0) { |
| 843 | + allStatusZero = false; | |
| 761 | 844 | dayDur += dur; |
| 762 | 845 | devTotalDur += dur; |
| 763 | 846 | dayStatus.merge(sk, dur, Long::sum); |
| 764 | 847 | devStatusAll.merge(sk, dur, Long::sum); |
| 765 | 848 | } |
| 766 | 849 | } |
| 850 | + // 该条小时数据的所有状态时间都为0时,默认设为待机(2)=3600秒 | |
| 851 | + if (allStatusZero) { | |
| 852 | + dayDur += 3600L; | |
| 853 | + devTotalDur += 3600L; | |
| 854 | + dayStatus.merge(2, 3600L, Long::sum); | |
| 855 | + devStatusAll.merge(2, 3600L, Long::sum); | |
| 856 | + } | |
| 767 | 857 | } |
| 768 | 858 | devTotalKwh = devTotalKwh.add(dayKwh); |
| 769 | 859 | grandTotalKwh = grandTotalKwh.add(devTotalKwh); |
| ... | ... | @@ -811,24 +901,45 @@ public class EnergySearchService { |
| 811 | 901 | JSONArray dataArray = rawObj instanceof JSONArray ? (JSONArray) rawObj : new JSONArray(); |
| 812 | 902 | |
| 813 | 903 | Map<String, Object> mEntry = monthAgg.get(monthKey); |
| 904 | + boolean isToday = d.equals(new SimpleDateFormat("yyyy-MM-dd").format(new Date())); | |
| 905 | + int currentHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); | |
| 814 | 906 | for (int i = 0; i < dataArray.size(); i++) { |
| 815 | 907 | JSONObject item = dataArray.getJSONObject(i); |
| 816 | 908 | if (item == null) continue; |
| 909 | + // 如果查询的是今天,跳过未到当前时间的小时数据 | |
| 910 | + if (isToday) { | |
| 911 | + String startTime = item.getString("startTime"); | |
| 912 | + if (startTime != null && startTime.length() >= 13) { | |
| 913 | + try { | |
| 914 | + int itemHour = Integer.parseInt(startTime.substring(11, 13)); | |
| 915 | + if (itemHour >= currentHour) continue; | |
| 916 | + } catch (NumberFormatException ignored) {} | |
| 917 | + } | |
| 918 | + } | |
| 817 | 919 | Double value = item.getDouble("value"); |
| 818 | 920 | if (value != null) { |
| 819 | 921 | BigDecimal valBd = BigDecimal.valueOf(value); |
| 820 | 922 | mEntry.put("totalKwh", ((BigDecimal) mEntry.get("totalKwh")).add(valBd)); |
| 821 | 923 | } |
| 822 | 924 | |
| 925 | + boolean allStatusZero = true; | |
| 823 | 926 | for (int sk = 0; sk <= 3; sk++) { |
| 824 | 927 | Long dur = item.getLong(String.valueOf(sk)); |
| 825 | 928 | if (dur != null && dur > 0) { |
| 929 | + allStatusZero = false; | |
| 826 | 930 | mEntry.put("totalDurationSeconds", (Long) mEntry.get("totalDurationSeconds") + dur); |
| 827 | 931 | @SuppressWarnings("unchecked") |
| 828 | 932 | Map<Integer, Long> sMap = (Map<Integer, Long>) mEntry.get("statusDurationMap"); |
| 829 | 933 | sMap.merge(sk, dur, Long::sum); |
| 830 | 934 | } |
| 831 | 935 | } |
| 936 | + // 该条小时数据的所有状态时间都为0时,默认设为待机(2)=3600秒 | |
| 937 | + if (allStatusZero) { | |
| 938 | + mEntry.put("totalDurationSeconds", (Long) mEntry.get("totalDurationSeconds") + 3600L); | |
| 939 | + @SuppressWarnings("unchecked") | |
| 940 | + Map<Integer, Long> sMap = (Map<Integer, Long>) mEntry.get("statusDurationMap"); | |
| 941 | + sMap.merge(2, 3600L, Long::sum); | |
| 942 | + } | |
| 832 | 943 | } |
| 833 | 944 | } |
| 834 | 945 | ... | ... |