| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- 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<Map<String, String>> platformList = platformService.getAllData();
- List<Map<String,Object>> configList = ibpsService.getConfList();
- List<Map<String,Object>> undoList = ibpsService.getUndoList();
- for(Map<String,Object> undoMap : undoList){
- String areaName = undoMap.get("qu_yu_ming_cheng_").toString();
- String RoomNo = undoMap.get("fang_jian_hao_").toString();
- Map<String,Object> configMap = getConfMap(configList,areaName,RoomNo);
- if (configMap.size() != 0){
- String collectID = configMap.get("cai_ji_qi_id_").toString();
- List<Map<String, String>> 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<String,Object> getConfMap(List<Map<String, Object>> configList, String areaName, String RoomNo){
- Map<String,Object> configMap = new HashMap<>();
- for(Map<String, Object> 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<Map<String, String>> getCurPlatformList(String collectID, List<Map<String, String>> platformList){
- List<Map<String, String>> retList = new ArrayList<>();
- for(Map<String, String> 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<Map<String, String>> 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<String, String> 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;
- }
- }
|