package com.ruoyi.http.util; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.http.pojo.TqdianbiaoEleRecord; import com.ruoyi.http.vo.StatisticEleRecordVo; import java.math.BigDecimal; import java.math.RoundingMode; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Comparator; import java.util.List; public final class StatisticEleParseUtil { private StatisticEleParseUtil() { } public static List parseToEntities(String raw, String dimension) { JSONObject root = JSON.parseObject(raw); if (root == null) { throw new ServiceException("电表接口返回为空"); } if (root.getIntValue("status") != 1) { String msg = root.getString("msg"); throw new ServiceException("电表接口返回异常: " + (msg != null ? msg : raw)); } JSONObject data = root.getJSONObject("data"); if (data == null || data.isEmpty()) { return new ArrayList<>(); } LocalDateTime now = LocalDateTime.now(); List list = new ArrayList<>(); for (String timeKey : data.keySet()) { JSONArray records = data.getJSONArray(timeKey); if (records == null) { continue; } for (int i = 0; i < records.size(); i++) { JSONObject rec = records.getJSONObject(i); TqdianbiaoEleRecord entity = new TqdianbiaoEleRecord(); entity.setMeterId(rec.getLong("mid")); entity.setDimension(dimension); entity.setTimeKey(timeKey); entity.setReadingMethod("sync"); Integer ratio = rec.getInteger("r"); entity.setRatio(ratio); entity.setStartTime(rec.getString("st")); entity.setEndTime(rec.getString("et")); JSONArray sArr = rec.getJSONArray("s"); JSONArray eArr = rec.getJSONArray("e"); JSONArray dArr = rec.getJSONArray("d"); entity.setStartReading(StatisticEleReadingUtil.formatReadingArray(sArr)); entity.setEndReading(StatisticEleReadingUtil.formatReadingArray(eArr)); entity.setPrevReading(StatisticEleReadingUtil.firstReading(sArr)); entity.setCurrReading(StatisticEleReadingUtil.firstReading(eArr)); fillConsumption(entity, dArr, ratio, sArr, eArr); entity.setSyncTime(now); list.add(entity); } } list.sort(Comparator.comparing(TqdianbiaoEleRecord::getTimeKey)); return list; } public static List toVoList(List entities) { List list = new ArrayList<>(); for (TqdianbiaoEleRecord entity : entities) { list.add(toVo(entity)); } return list; } public static StatisticEleRecordVo toVo(TqdianbiaoEleRecord entity) { StatisticEleRecordVo vo = new StatisticEleRecordVo(); vo.setId(entity.getId()); vo.setTimeKey(entity.getTimeKey()); vo.setMeterId(entity.getMeterId()); vo.setMeterName(entity.getMeterName()); vo.setRatio(entity.getRatio()); vo.setReadingMethod(entity.getReadingMethod()); vo.setPrevReading(toDouble(entity.getPrevReading())); vo.setCurrReading(toDouble(entity.getCurrReading())); vo.setStartTime(entity.getStartTime()); vo.setEndTime(entity.getEndTime()); vo.setTotalConsumption(toDouble(entity.getTotalConsumption())); vo.setSharpConsumption(toDouble(entity.getSharpConsumption())); vo.setPeakConsumption(toDouble(entity.getPeakConsumption())); vo.setFlatConsumption(toDouble(entity.getFlatConsumption())); vo.setValleyConsumption(toDouble(entity.getValleyConsumption())); vo.setStartReading(entity.getStartReading()); vo.setEndReading(entity.getEndReading()); return vo; } private static void fillConsumption(TqdianbiaoEleRecord entity, JSONArray d, Integer ratio, JSONArray sArr, JSONArray eArr) { if (d != null && !d.isEmpty()) { entity.setTotalConsumption(StatisticEleReadingUtil.calcConsumptionFromRaw(d.getBigDecimal(0), ratio)); entity.setSharpConsumption(consumptionAt(d, 1, ratio)); entity.setPeakConsumption(consumptionAt(d, 2, ratio)); entity.setFlatConsumption(consumptionAt(d, 3, ratio)); entity.setValleyConsumption(consumptionAt(d, 4, ratio)); entity.setDeepValleyConsumption(consumptionAt(d, 5, ratio)); } else { BigDecimal rawTotal = StatisticEleReadingUtil.calcConsumption( StatisticEleReadingUtil.firstReading(sArr), StatisticEleReadingUtil.firstReading(eArr), 1); entity.setTotalConsumption(StatisticEleReadingUtil.calcConsumptionFromRaw(rawTotal, ratio)); } } private static BigDecimal consumptionAt(JSONArray d, int index, Integer ratio) { if (d.size() <= index) { return null; } return StatisticEleReadingUtil.calcConsumptionFromRaw(d.getBigDecimal(index), ratio); } private static Double toDouble(BigDecimal value) { if (value == null) { return null; } return value.setScale(StatisticEleReadingUtil.CONSUMPTION_SCALE, RoundingMode.HALF_UP).doubleValue(); } }