Commit 5e6fbbab358d703805d27ec1a470ed6e0747445e

Authored by 戈灵虎
1 parent 06a7c311

feat(workorder): 实现事件信息中图片数组的JSON存储和映射

urbanops-module-workorder/src/main/java/com/zteits/urbanops/module/workorder/config/StringArrayTypeHandler.java 0 → 100644
  1 +package com.zteits.urbanops.module.workorder.config;
  2 +
  3 +/**
  4 + * @Classname StringArrayTypeHandler
  5 + * @Description
  6 + * @Date 2025/6/11 14:47
  7 + * @Created by wangqian
  8 + */
  9 +
  10 +import com.fasterxml.jackson.databind.ObjectMapper;
  11 +import org.apache.ibatis.type.BaseTypeHandler;
  12 +import org.apache.ibatis.type.JdbcType;
  13 +import org.apache.ibatis.type.MappedJdbcTypes;
  14 +import org.apache.ibatis.type.MappedTypes;
  15 +
  16 +import java.sql.CallableStatement;
  17 +import java.sql.PreparedStatement;
  18 +import java.sql.ResultSet;
  19 +import java.sql.SQLException;
  20 +
  21 +/**
  22 + * MyBatis 类型处理器,用于处理 JSON 格式的字符串数组与 Java String[] 之间的转换
  23 + */
  24 +@MappedTypes(String[].class)
  25 +@MappedJdbcTypes({JdbcType.VARCHAR, JdbcType.LONGVARCHAR, JdbcType.OTHER})
  26 +public class StringArrayTypeHandler extends BaseTypeHandler<String[]> {
  27 +
  28 + private static final ObjectMapper objectMapper = new ObjectMapper();
  29 +
  30 + @Override
  31 + public void setNonNullParameter(PreparedStatement ps, int i, String[] parameter, JdbcType jdbcType) throws SQLException {
  32 + try {
  33 + // 将 String[] 转换为 JSON 字符串
  34 + String json = objectMapper.writeValueAsString(parameter);
  35 + ps.setString(i, json);
  36 + } catch (Exception e) {
  37 + throw new SQLException("Error converting String[] to JSON: " + e.getMessage(), e);
  38 + }
  39 + }
  40 +
  41 + @Override
  42 + public String[] getNullableResult(ResultSet rs, String columnName) throws SQLException {
  43 + return parseJson(rs.getString(columnName));
  44 + }
  45 +
  46 + @Override
  47 + public String[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
  48 + return parseJson(rs.getString(columnIndex));
  49 + }
  50 +
  51 + @Override
  52 + public String[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
  53 + return parseJson(cs.getString(columnIndex));
  54 + }
  55 +
  56 + /**
  57 + * 将 JSON 字符串解析为 String[]
  58 + */
  59 + private String[] parseJson(String json) {
  60 + if (json == null || json.isEmpty()) {
  61 + return new String[0];
  62 + }
  63 + try {
  64 + // 将 JSON 字符串转换为 String[]
  65 + return objectMapper.readValue(json, String[].class);
  66 + } catch (Exception e) {
  67 + throw new RuntimeException("Error converting JSON to String[]: " + e.getMessage(), e);
  68 + }
  69 + }
  70 +}
urbanops-module-workorder/src/main/java/com/zteits/urbanops/module/workorder/controller/admin/eventinfo/vo/EventInfoPageReqVO.java
@@ -50,7 +50,7 @@ public class EventInfoPageReqVO extends PageParam { @@ -50,7 +50,7 @@ public class EventInfoPageReqVO extends PageParam {
50 private String lonLatAddress; 50 private String lonLatAddress;
51 51
52 @Schema(description = "用户提交照片") 52 @Schema(description = "用户提交照片")
53 - private String confrimImgs; 53 + private String[] confrimImgs;
54 54
55 @Schema(description = "事件状态", example = "1") 55 @Schema(description = "事件状态", example = "1")
56 private Integer eventStatus; 56 private Integer eventStatus;
urbanops-module-workorder/src/main/java/com/zteits/urbanops/module/workorder/controller/admin/eventinfo/vo/EventInfoRespVO.java
@@ -63,7 +63,7 @@ public class EventInfoRespVO { @@ -63,7 +63,7 @@ public class EventInfoRespVO {
63 63
64 @Schema(description = "用户提交照片", requiredMode = Schema.RequiredMode.REQUIRED) 64 @Schema(description = "用户提交照片", requiredMode = Schema.RequiredMode.REQUIRED)
65 @ExcelProperty("用户提交照片") 65 @ExcelProperty("用户提交照片")
66 - private String confrimImgs; 66 + private String[] confrimImgs;
67 67
68 @Schema(description = "事件状态", example = "1") 68 @Schema(description = "事件状态", example = "1")
69 @ExcelProperty("事件状态") 69 @ExcelProperty("事件状态")
urbanops-module-workorder/src/main/java/com/zteits/urbanops/module/workorder/dal/dataobject/eventinfo/EventInfoDO.java
1 package com.zteits.urbanops.module.workorder.dal.dataobject.eventinfo; 1 package com.zteits.urbanops.module.workorder.dal.dataobject.eventinfo;
2 2
  3 +import com.zteits.urbanops.module.workorder.config.StringArrayTypeHandler;
3 import lombok.*; 4 import lombok.*;
4 import java.util.*; 5 import java.util.*;
5 import java.time.LocalDateTime; 6 import java.time.LocalDateTime;
@@ -16,7 +17,7 @@ import com.zteits.urbanops.framework.mybatis.core.dataobject.BaseDO; @@ -16,7 +17,7 @@ import com.zteits.urbanops.framework.mybatis.core.dataobject.BaseDO;
16 * 17 *
17 * @author 超级管理员 18 * @author 超级管理员
18 */ 19 */
19 -@TableName("workorder_event_info") 20 +@TableName(value = "workorder_event_info", autoResultMap = true)
20 @KeySequence("workorder_event_info_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。 21 @KeySequence("workorder_event_info_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
21 @Data 22 @Data
22 @EqualsAndHashCode(callSuper = true) 23 @EqualsAndHashCode(callSuper = true)
@@ -80,6 +81,7 @@ public class EventInfoDO extends BaseDO { @@ -80,6 +81,7 @@ public class EventInfoDO extends BaseDO {
80 /** 81 /**
81 * 用户提交照片 82 * 用户提交照片
82 */ 83 */
  84 + @TableField(typeHandler = StringArrayTypeHandler.class)
83 private String[] confrimImgs; 85 private String[] confrimImgs;
84 /** 86 /**
85 * 事件状态 87 * 事件状态