Forráskód Böngészése

设备库里null带出为-

xuzhancheng 1 hónapja
szülő
commit
742b5bb0db

+ 26 - 7
tz-module-pressure2/tz-module-pressure2-biz/src/main/java/cn/start/tz/module/pressure2/service/dynamicOFData/util/AutoDataBoilerService.java

@@ -1,5 +1,6 @@
 package cn.start.tz.module.pressure2.service.dynamicOFData.util;
 
+import cn.hutool.json.JSONUtil;
 import cn.start.tz.module.pressure2.controller.admin.equipboiler.vo.EquipBoilerReportVO;
 import cn.start.tz.module.pressure2.dal.dataobject.boilertaskorder.BoilerTaskOrderDO;
 import cn.start.tz.module.pressure2.dal.dataobject.boilertaskorderitem.BoilerTaskOrderItemDO;
@@ -81,8 +82,8 @@ public class AutoDataBoilerService {
         BoilerTaskOrderItemDO itemDO = boilerTaskOrderItemMapper.selectById(itemReportDO.getOrderItemId());
         if (itemDO != null) {
             EquipBoilerDO boilerDO = equipBoilerMapper.selectById(itemDO.getEquipId());
-            String boilerJson = JSONObject.toJSONString(boilerDO, JSONWriter.Feature.WriteMapNullValue);
-            JSONObject boilerInfo = JSONObject.parseObject(boilerJson);
+//            JSONObject boilerInfo = JSONObject.from(boilerDO);
+            cn.hutool.json.JSONObject boilerInfo = JSONUtil.parseObj(boilerDO, false);
             // 自动转换所有字段为字符串格式
             JsonConvertUtil.convertAllFieldsToString(boilerInfo);
             // 合并 json
@@ -98,6 +99,8 @@ public class AutoDataBoilerService {
         List<BoilerTaskOrderItemReportUserDO> itemReportUserDOS = boilerTaskOrderItemReportUserMapper.selectList(BoilerTaskOrderItemReportUserDO::getReportId, boilerTaskOrderItemReportDOId);
         if (taskOrderDO != null) {
             jsonObject.put("checkDate", taskOrderDO.getCheckDate().format(dateFormat));
+        } else {
+            jsonObject.put("checkDate", "-");
         }
         if (!itemReportUserDOS.isEmpty()) {
             AdminUserRespDTO userInfo = adminUserApi.getUser(itemReportUserDOS.get(0).getUserId()).getData();
@@ -116,6 +119,8 @@ public class AutoDataBoilerService {
         }
         if (itemReportDO.getRecheckDate() != null) {
             jsonObject.put("recheckDate", itemReportDO.getRecheckDate().format(dateFormat));
+        } else {
+            jsonObject.put("recheckDate", "-");
         }
         return jsonObject;
     }
@@ -135,16 +140,24 @@ public class AutoDataBoilerService {
         BoilerTaskOrderItemDO itemDO = boilerTaskOrderItemMapper.selectById(itemReportDO.getOrderItemId());
         if (itemDO != null) {
             EquipBoilerDO boilerDO = equipBoilerMapper.selectById(itemDO.getEquipId());
-            String boilerJson = JSONObject.toJSONString(boilerDO, JSONWriter.Feature.WriteMapNullValue);
-            JSONObject boilerInfo = JSONObject.parseObject(boilerJson);
+            // 先将对象转为 JSON 字符串(包含null值),再解析为 JSONObject
+            String jsonStr = JSONObject.toJSONString(boilerDO, JSONWriter.Feature.WriteNulls);
+            JSONObject boilerInfo = JSONObject.parseObject(jsonStr);
             // 自动转换所有字段为字符串格式(包括日期、数字、布尔等)
             JsonConvertUtil.convertAllFieldsToString(boilerInfo);
-            // 合并 json
+            // 合并 json - 遍历所有字段,包括null值
             for (String key : boilerInfo.keySet()) {
                 // 没有这个key或者为null或者为空字符串时put
                 if (!jsonObject.containsKey(key) || jsonObject.get(key) == null || jsonObject.get(key).toString().isEmpty()) {
-                    String str = (String) boilerInfo.get(key);
-                    jsonObject.put(key, Objects.requireNonNullElse(str, "-"));
+                    Object value = boilerInfo.get(key);
+                    // 如果值为null或空字符串,则使用"-"作为默认值
+                    String str;
+                    if (value == null || "null".equals(value.toString()) || value.toString().isEmpty()) {
+                        str = "-";
+                    } else {
+                        str = value.toString();
+                    }
+                    jsonObject.put(key, str);
                 }
             }
         }
@@ -162,6 +175,8 @@ public class AutoDataBoilerService {
         }
         if (itemReportDO.getPrepareTime() != null) {
             jsonObject.put("prepareTime", itemReportDO.getPrepareTime().format(dateFormat));
+        } else {
+            jsonObject.put("prepareTime", "-");
         }
 
         //审核人员信息
@@ -175,6 +190,8 @@ public class AutoDataBoilerService {
         }
         if (itemReportDO.getApprovalTime() != null) {
             jsonObject.put("approvalTime", itemReportDO.getApprovalTime().format(dateFormat));
+        } else {
+            jsonObject.put("approvalTime", "-");
         }
 
         //批准人员信息
@@ -188,6 +205,8 @@ public class AutoDataBoilerService {
         }
         if (itemReportDO.getRatifyTime() != null) {
             jsonObject.put("ratifyTime", itemReportDO.getRatifyTime().format(dateFormat));
+        } else {
+            jsonObject.put("ratifyTime", "-");
         }
         return jsonObject;
     }

+ 10 - 0
tz-module-pressure2/tz-module-pressure2-biz/src/main/java/cn/start/tz/module/pressure2/service/dynamicOFData/util/AutoDataPipeService.java

@@ -49,6 +49,8 @@ public class AutoDataPipeService {
         List<PipeTaskOrderItemReportUserDO> itemReportUserDOS = pipeTaskOrderItemReportUserMapper.selectList(PipeTaskOrderItemReportUserDO::getReportId, pipeTaskOrderItemReportDOId);
         if (taskOrderDO != null) {
             jsonObject.put("checkDate", taskOrderDO.getCheckDate().format(dateFormat));
+        } else {
+            jsonObject.put("checkDate", "-");
         }
         if (!itemReportUserDOS.isEmpty()) {
             AdminUserRespDTO userInfo = adminUserApi.getUser(itemReportUserDOS.get(0).getUserId()).getData();
@@ -66,6 +68,8 @@ public class AutoDataPipeService {
         }
         if (itemReportDO.getRecheckDate() != null) {
             jsonObject.put("recheckDate", itemReportDO.getRecheckDate().format(dateFormat));
+        } else {
+            jsonObject.put("recheckDate", "-");
         }
         return jsonObject;
     }
@@ -85,6 +89,8 @@ public class AutoDataPipeService {
         }
         if (itemReportDO.getPrepareTime() != null) {
             jsonObject.put("prepareTime", itemReportDO.getPrepareTime().format(dateFormat));
+        } else {
+            jsonObject.put("prepareTime", "-");
         }
 
         //审核人员信息
@@ -98,6 +104,8 @@ public class AutoDataPipeService {
         }
         if (itemReportDO.getApprovalTime() != null) {
             jsonObject.put("approvalTime", itemReportDO.getApprovalTime().format(dateFormat));
+        } else {
+            jsonObject.put("approvalTime", "-");
         }
 
         //批准人员信息
@@ -111,6 +119,8 @@ public class AutoDataPipeService {
         }
         if (itemReportDO.getRatifyTime() != null) {
             jsonObject.put("ratifyTime", itemReportDO.getRatifyTime().format(dateFormat));
+        } else {
+            jsonObject.put("ratifyTime", "-");
         }
         return jsonObject;
     }

+ 34 - 1
tz-module-pressure2/tz-module-pressure2-biz/src/main/java/cn/start/tz/module/pressure2/service/dynamicOFData/util/JsonConvertUtil.java

@@ -1,5 +1,6 @@
 package cn.start.tz.module.pressure2.service.dynamicOFData.util;
 
+import cn.hutool.core.util.StrUtil;
 import com.alibaba.fastjson2.JSONObject;
 
 import java.math.BigDecimal;
@@ -42,12 +43,41 @@ public class JsonConvertUtil {
             return jsonObject;
         }
 
+        // 遍历 JSONObject 的所有字段
+        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
+            Object value = entry.getValue();
+            if (value == null) {
+                jsonObject.put(entry.getKey(), "-");
+                continue;
+            }
+            String formattedValue = convertValueToString(value);
+            jsonObject.put(entry.getKey(), formattedValue);
+        }
+
+        return jsonObject;
+    }
+
+    /**
+     * 将 JSONObject 中的所有字段转换为字符串格式
+     * 自动识别并转换以下类型:
+     * - LocalDate: 使用日期格式
+     * - LocalDateTime: 使用日期时间格式
+     * - Integer/Long/Double/Float/BigDecimal/Short/Byte: 转为字符串
+     * - Boolean: 转为 "true"/"false"
+     *
+     * @param jsonObject JSONObject 对象
+     * @return 转换后的 JSONObject
+     */
+    public static cn.hutool.json.JSONObject convertAllFieldsToString(cn.hutool.json.JSONObject jsonObject) {
+        if (jsonObject == null || jsonObject.isEmpty()) {
+            return jsonObject;
+        }
+
         // 遍历 JSONObject 的所有字段
         for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
             Object value = entry.getValue();
             String formattedValue = convertValueToString(value);
 
-            // 如果转换成功(非 null),则更新字段值
             jsonObject.put(entry.getKey(), formattedValue);
         }
 
@@ -86,6 +116,9 @@ public class JsonConvertUtil {
         }
         // 已经是字符串类型
         else if (value instanceof String) {
+            if (StrUtil.isBlank((String) value) || "null".equals(value)) {
+                return "-";
+            }
             return (String) value;
         }
         // 其他类型,尝试转为字符串