瀏覽代碼

北向代码提交

jingyuanchao 1 年之前
父節點
當前提交
2552861354

+ 168 - 0
soc-common/soc-common-core/src/main/java/com/xunmei/common/core/utils/JacksonUtils.java

@@ -0,0 +1,168 @@
+package com.xunmei.common.core.utils;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import lombok.extern.slf4j.Slf4j;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import java.io.File;
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+
+/**
+ * @author jingyuanchao
+ * @date 2024/2/27 14:27
+ */
+@Slf4j
+public class JacksonUtils {
+
+
+    private static ObjectMapper objectMapper = new ObjectMapper();
+
+    // 时间日期格式
+    private static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";
+
+    //以静态代码块初始化
+    static {
+        //对象的所有字段全部列入序列化
+        objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
+        //取消默认转换timestamps形式
+        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
+        //忽略空Bean转json的错误
+        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
+        //所有的日期格式都统一为以下的格式,即yyyy-MM-dd HH:mm:ss
+        objectMapper.setDateFormat(new SimpleDateFormat(STANDARD_FORMAT));
+        //忽略 在json字符串中存在,但在java对象中不存在对应属性的情况。防止错误
+        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+    }
+
+
+    /**===========================以下是从JSON中获取对象====================================*/
+    public static <T> T parseObject(String jsonString, Class<T> object) {
+        T t = null;
+        try {
+            t = objectMapper.readValue(jsonString, object);
+        } catch (JsonProcessingException e) {
+            log.error("JsonString转为自定义对象失败:{}", e.getMessage());
+        }
+        return t;
+    }
+
+    public static <T> T parseObject(File file, Class<T> object) {
+        T t = null;
+        try {
+            t = objectMapper.readValue(file, object);
+        } catch (IOException e) {
+            log.error("从文件中读取json字符串转为自定义对象失败:{}", e.getMessage());
+        }
+        return t;
+    }
+
+    //将json数组字符串转为指定对象List列表或者Map集合
+    public static <T> T parseJSONArray(String jsonArray, TypeReference<T> reference) {
+        T t = null;
+        try {
+            t = objectMapper.readValue(jsonArray, reference);
+        } catch (JsonProcessingException e) {
+            log.error("JSONArray转为List列表或者Map集合失败:{}", e.getMessage());
+        }
+        return t;
+    }
+
+
+    /**=================================以下是将对象转为JSON=====================================*/
+    public static String toJSONString(Object object) {
+        String jsonString = null;
+        try {
+            jsonString = objectMapper.writeValueAsString(object);
+        } catch (JsonProcessingException e) {
+            log.error("Object转JSONString失败:{}", e.getMessage());
+        }
+        return jsonString;
+    }
+
+    public static byte[] toByteArray(Object object) {
+        byte[] bytes = null;
+        try {
+            bytes = objectMapper.writeValueAsBytes(object);
+        } catch (JsonProcessingException e) {
+            log.error("Object转ByteArray失败:{}", e.getMessage());
+        }
+        return bytes;
+    }
+
+    public static void objectToFile(File file, Object object) {
+        try {
+            objectMapper.writeValue(file, object);
+        } catch (JsonProcessingException e) {
+            log.error("Object写入文件失败:{}", e.getMessage());
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+
+    /**=============================以下是与JsonNode相关的=======================================*/
+    //JsonNode和JSONObject一样,都是JSON树形模型,只不过在jackson中,存在的是JsonNode
+    public static JsonNode parseJSONObject(String jsonString) {
+        JsonNode jsonNode = null;
+        try {
+            jsonNode = objectMapper.readTree(jsonString);
+        } catch (JsonProcessingException e) {
+            log.error("JSONString转为JsonNode失败:{}", e.getMessage());
+        }
+        return jsonNode;
+    }
+
+    public static JsonNode parseJSONObject(Object object) {
+        JsonNode jsonNode = objectMapper.valueToTree(object);
+        return jsonNode;
+    }
+
+    public static String toJSONString(JsonNode jsonNode) {
+        String jsonString = null;
+        try {
+            jsonString = objectMapper.writeValueAsString(jsonNode);
+        } catch (JsonProcessingException e) {
+            log.error("JsonNode转JSONString失败:{}", e.getMessage());
+        }
+        return jsonString;
+    }
+
+    //JsonNode是一个抽象类,不能实例化,创建JSON树形模型,得用JsonNode的子类ObjectNode,用法和JSONObject大同小异
+    public static ObjectNode newJSONObject() {
+       return  objectMapper.createObjectNode();
+    }
+
+    //创建JSON数组对象,就像JSONArray一样用
+    public static ArrayNode newJSONArray() {
+        return objectMapper.createArrayNode();
+    }
+
+
+    /**===========以下是从JsonNode对象中获取key值的方法,个人觉得有点多余,直接用JsonNode自带的取值方法会好点,出于纠结症,还是补充进来了*/
+    public static String getString(JsonNode jsonObject, String key) {
+        String s = jsonObject.get(key).asText();
+        return s;
+    }
+
+    public static Integer getInteger(JsonNode jsonObject, String key) {
+        Integer i = jsonObject.get(key).asInt();
+        return i;
+    }
+
+    public static Boolean getBoolean(JsonNode jsonObject, String key) {
+        Boolean bool = jsonObject.get(key).asBoolean();
+        return bool;
+    }
+
+    public static JsonNode getJSONObject(JsonNode jsonObject, String key) {
+        JsonNode json = jsonObject.get(key);
+        return json;
+    }
+}

+ 2 - 0
soc-modules/soc-modules-mediator/src/main/java/com/xunmei/mediator/api/service/impl/IotAlarmSubSystemServiceImpl.java

@@ -197,6 +197,8 @@ public class IotAlarmSubSystemServiceImpl extends ServiceImpl<IotAlarmSubsystemM
         protection.setOrgName(org.getShortName());
         protection.setOrgPath(org.getPath());
         protection.setAlarmHostCode(protectionEditDto.getAlarmHostCode());
+        protection.setStatusUpdatorId(null);
+        protection.setStatusUpdatorName(null);
         this.saveOrUpdate(protection);
         this.northStatisticsSyncService.saveOrUpdateBusinessCountByDateAndDataType(DateUtil.today(), DataType.protectionStatusCount.getIndex(), 1L);
         this.saveProtectionLog(protection);

+ 6 - 1
soc-modules/soc-modules-mediator/src/main/java/com/xunmei/mediator/api/service/impl/IotDeviceServiceImpl.java

@@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.xunmei.common.core.constant.Constants;
 import com.xunmei.common.core.domain.iot.domain.IotDevice;
+import com.xunmei.common.core.enums.CategoryDataEnum;
 import com.xunmei.mediator.api.mapper.IotDeviceMapper;
 import com.xunmei.mediator.api.service.EquipmentSyncService;
 import com.xunmei.mediator.api.service.IIotDeviceService;
@@ -64,7 +65,11 @@ public class IotDeviceServiceImpl extends ServiceImpl<IotDeviceMapper, IotDevice
     public void updateDeviceDeleted() {
         final LambdaUpdateWrapper<IotDevice> wrapper = new LambdaUpdateWrapper<>();
         wrapper.eq(IotDevice::getSource, 1);
-        wrapper.in(IotDevice::getCategoryId, 3713671510851584L, 3713671920926720L, 3713672073773056L, 3713672266842112L);
+        wrapper.in(IotDevice::getCategoryId,
+                CategoryDataEnum.MONITOR_HOST_DEVICE.getId(),
+                 CategoryDataEnum.ALARM_HOST_DEVICE.getId(),
+                CategoryDataEnum.TALK_HOST_DEVICE.getId(),
+                CategoryDataEnum.DOOR_HOST_DEVICE.getId());
         wrapper.set(IotDevice::getDeleted, 1);
         update(wrapper);
     }