package com.jyxt.getdatabyrestful; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.jyxt.getdatabyrestful.service.IBPSService; import com.jyxt.getdatabyrestful.service.PlatformService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.util.*; @Component public class HandleData { private static final Logger log = LoggerFactory.getLogger(GetDataByRESTfulApplication.class); private static final ObjectMapper mapper = new ObjectMapper(); @Autowired PlatformService platformService; @Autowired IBPSService ibpsService; public void startHandleData() { log.info("---------------------------------------------------------------"); List> platformList = platformService.getAllData(); List> configList = ibpsService.getConfList(); List> undoList = ibpsService.getUndoList(); for(Map undoMap : undoList){ String areaName = undoMap.get("qu_yu_ming_cheng_").toString(); String RoomNo = undoMap.get("fang_jian_hao_").toString(); Map configMap = getConfMap(configList,areaName,RoomNo); if (configMap.size() != 0){ String collectID = configMap.get("cai_ji_qi_id_").toString(); List> curPlatformList = getCurPlatformList(collectID,platformList); if (curPlatformList.size() != 0){ String id = undoMap.get("id_").toString(); String JsonStr = undoMap.get("lie_biao_shu_ju_").toString(); String timeSlot = getTimePeriod("1"); String retJsonStr = handJsonStr(JsonStr,curPlatformList,timeSlot); String JsonFlag = checkJson(retJsonStr); String timeStr = getTimePeriod("0"); String retStr = ibpsService.UpdateData("t_snwsdjkjlb",id,retJsonStr,JsonFlag, timeSlot, timeStr); if (retStr.equals("1")){ log.info("update success"); } else { log.info("update fail:"+retStr); } } } } } public Map getConfMap(List> configList, String areaName, String RoomNo){ Map configMap = new HashMap<>(); for(Map config : configList){ String curAreaName = config.get("qu_yu_").toString(); String curRoomNo = config.get("fang_jian_").toString(); if(areaName.equals(curAreaName) && RoomNo.equals(curRoomNo)){ configMap = config; } } return configMap; } public List> getCurPlatformList(String collectID, List> platformList){ List> retList = new ArrayList<>(); for(Map platformMap : platformList){ String curCollectID = platformMap.get("deviceAddr").toString(); if(curCollectID.equals(collectID)){ retList.add(platformMap); } } return retList; } /** * @ClassName HandleData * @Author huangws * @Description 模式0:返回当前时间(HH:mm格式) * 模式1:返回时间段 * @Date 2025/8/11 16:46 **/ public static String getTimePeriod(String timeType) { LocalTime now = LocalTime.now(); // 模式0:返回当前时间(HH:mm格式) if ("0".equals(timeType)) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm"); return now.format(formatter); } // 模式1:返回时间段 else if ("1".equals(timeType)) { LocalTime morningStart = LocalTime.of(6, 0); // 6:00 LocalTime afternoonStart = LocalTime.of(12, 0); // 12:00 LocalTime eveningStart = LocalTime.of(18, 0); // 18:00 if (now.isAfter(morningStart) && now.isBefore(afternoonStart)) { return "上午"; } else if (now.equals(afternoonStart) || (now.isAfter(afternoonStart) && now.isBefore(eveningStart))) { return "下午"; } else { return "其他时段"; } } // 处理无效参数 else { throw new IllegalArgumentException("无效参数: 只支持\"0\"或\"1\""); } } /** * 20250619 huangws * 处理json串,将获得的实际结果填入json串再返回 * @param JsonStr * @param curPlatformList * @param timeSlot * @return throws JsonProcessingException * @throws JsonProcessingException */ public String handJsonStr(String JsonStr, List> curPlatformList, String timeSlot) { String retVal = JsonStr; log.info("org json string:" + JsonStr); log.info("curPlatformList size: " + (curPlatformList != null ? curPlatformList.size() : 0)); if (curPlatformList == null || curPlatformList.isEmpty()) { log.warn("curPlatformList is empty, return original JSON"); return retVal; } try { ObjectMapper mapper = new ObjectMapper(); JsonNode rootNode = mapper.readTree(JsonStr); if (rootNode.isArray()) { ArrayNode arrayNode = (ArrayNode) rootNode; // 遍历所有平台数据 for (Map platformMap : curPlatformList) { String registerName = platformMap.get("registerName"); String dataValue = platformMap.get("data"); if (registerName == null || dataValue == null) { log.warn("Missing registerName or data in platformMap: " + platformMap); continue; } String targetLabel = timeSlot + registerName; // 如"上午温度" boolean found = false; // 在JSON数组中查找匹配项 for (int i = 0; i < arrayNode.size(); i++) { JsonNode item = arrayNode.get(i); if (!item.isObject()) continue; ObjectNode objNode = (ObjectNode) item; String label = objNode.path("label").asText(""); if (targetLabel.equals(label)) { found = true; // 更新value字段 objNode.put("value", dataValue); // 处理fixValue和result String fixValueStr = objNode.path("fixValue").asText(""); double dataVal = parseDoubleSafe(dataValue, 0.0); double resultVal = dataVal; if (!fixValueStr.isEmpty()) { double fixVal = parseDoubleSafe(fixValueStr, 0.0); resultVal = dataVal + fixVal; } // 更新result字段 objNode.put("result", String.valueOf(resultVal)); // 处理范围检查 JsonNode rangeNode = objNode.path("range"); if (rangeNode.isArray() && rangeNode.size() >= 2) { double min = parseDoubleSafe(rangeNode.get(0).asText(), Double.MIN_VALUE); double max = parseDoubleSafe(rangeNode.get(1).asText(), Double.MAX_VALUE); if (min != Double.MIN_VALUE && max != Double.MAX_VALUE) { String status = (resultVal >= min && resultVal <= max) ? "正常" : "失控"; objNode.put("status", status); } else { objNode.put("status", "范围数据错误"); log.warn("Invalid range data for label: " + label); } } else { objNode.put("status", "范围数据缺失"); log.warn("Missing range data for label: " + label); } break; // 找到匹配项后跳出内层循环 } } if (!found) { log.warn("No matching label found for: " + targetLabel); } } retVal = mapper.writeValueAsString(rootNode); } } catch (Exception e) { log.error("处理JSON时发生错误: " + e.getMessage(), e); } return retVal; } private double parseDoubleSafe(String value, double defaultValue) { if (value == null || value.isEmpty()) { return defaultValue; } try { return Double.parseDouble(value); } catch (NumberFormatException e) { log.warn("数值格式错误: " + value + ", 使用默认值: " + defaultValue); return defaultValue; } } /** * @ClassName HandleData * @Author huangws * @Description * @Date 2025/8/11 14:57 **/ public String checkJson(String retJsonStr) { boolean allResultsValid = true; boolean hasOutOfControl = false; try { ObjectMapper mapper = new ObjectMapper(); JsonNode rootNode = mapper.readTree(retJsonStr); if (rootNode.isArray()) { for (JsonNode item : rootNode) { // 检查result字段是否有效 JsonNode resultNode = item.get("result"); if (resultNode == null || !resultNode.isTextual() || resultNode.asText().isEmpty()) { allResultsValid = false; } // 检查status字段是否为"失控" JsonNode statusNode = item.get("status"); if (statusNode != null && statusNode.isTextual()) { String status = statusNode.asText(); if ("失控".equals(status)) { hasOutOfControl = true; } } } } } catch (Exception e) { log.error("校验JSON完成状态时发生错误: " + e.getMessage(), e); allResultsValid = false; } // 构建返回结果 String completionStatus = allResultsValid ? "1" : "0"; String controlStatus = hasOutOfControl ? "1" : "0"; return completionStatus + "^" + controlStatus; } }