refactor(iot): 重构设备属性和扩展参数的存储方式- 将设备属性和扩展参数从 Map<String, Object> 改为 List<Map<String, Object>> 类型
- 更新了相关的 DTO、Entity 和 Mapper 文件 - 重命名并重构了 JsonTypeHandler 类,使其支持多种 JSON 格式的处理 - 更新了 Menu 类中的 Type 枚举,使其实现 IEnum 接口
This commit is contained in:
parent
616d640228
commit
275b7fbe50
@ -0,0 +1,78 @@
|
|||||||
|
package com.zsc.edu.gateway.framework.json;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.apache.ibatis.type.BaseTypeHandler;
|
||||||
|
import org.apache.ibatis.type.JdbcType;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.sql.CallableStatement;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhuang
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class JsonTypeHandler extends BaseTypeHandler<Object> {
|
||||||
|
|
||||||
|
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
|
||||||
|
try {
|
||||||
|
String jsonString;
|
||||||
|
if (parameter instanceof Map) {
|
||||||
|
jsonString = objectMapper.writeValueAsString(parameter);
|
||||||
|
} else if (parameter instanceof String) {
|
||||||
|
jsonString = (String) parameter;
|
||||||
|
} else if (parameter instanceof List) {
|
||||||
|
jsonString = objectMapper.writeValueAsString(parameter);
|
||||||
|
} else {
|
||||||
|
throw new SQLException("Unsupported parameter type: " + parameter.getClass().getName());
|
||||||
|
}
|
||||||
|
ps.setString(i, jsonString);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
throw new SQLException("Error converting object to JSON", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
||||||
|
String json = rs.getString(columnName);
|
||||||
|
return parseJson(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
||||||
|
String json = rs.getString(columnIndex);
|
||||||
|
return parseJson(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
||||||
|
String json = cs.getString(columnIndex);
|
||||||
|
return parseJson(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object parseJson(String json) {
|
||||||
|
if (json == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (json.startsWith("{")) {
|
||||||
|
return objectMapper.readValue(json, HashMap.class);
|
||||||
|
} else if (json.startsWith("[")) {
|
||||||
|
return objectMapper.readValue(json, List.class);
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("Invalid JSON format");
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("Error parsing JSON", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,61 +0,0 @@
|
|||||||
package com.zsc.edu.gateway.framework.json;
|
|
||||||
|
|
||||||
import org.apache.ibatis.type.BaseTypeHandler;
|
|
||||||
import org.apache.ibatis.type.JdbcType;
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import java.sql.CallableStatement;
|
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author zhuang
|
|
||||||
*/
|
|
||||||
@Component
|
|
||||||
public class MapJsonTypeHandler extends BaseTypeHandler<Map<String, Object>> {
|
|
||||||
|
|
||||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setNonNullParameter(PreparedStatement ps, int i, Map<String, Object> parameter, JdbcType jdbcType) throws SQLException {
|
|
||||||
try {
|
|
||||||
ps.setString(i, objectMapper.writeValueAsString(parameter));
|
|
||||||
} catch (JsonProcessingException e) {
|
|
||||||
throw new SQLException("Error converting map to JSON", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Map<String, Object> getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
|
||||||
String json = rs.getString(columnName);
|
|
||||||
return parseJson(json);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Map<String, Object> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
|
||||||
String json = rs.getString(columnIndex);
|
|
||||||
return parseJson(json);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Map<String, Object> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
|
||||||
String json = cs.getString(columnIndex);
|
|
||||||
return parseJson(json);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String, Object> parseJson(String json) {
|
|
||||||
if (json == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
return objectMapper.readValue(json, HashMap.class);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException("Error parsing JSON to map", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -7,7 +7,9 @@ import lombok.NoArgsConstructor;
|
|||||||
|
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import org.springframework.boot.configurationprocessor.json.JSONObject;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -33,11 +35,11 @@ public class BatchDeviceDto {
|
|||||||
/**
|
/**
|
||||||
* 扩展属性
|
* 扩展属性
|
||||||
*/
|
*/
|
||||||
private Map<String, Object> extendParams;
|
private List<Map<String, Object>> extendParams;
|
||||||
/**
|
/**
|
||||||
* 设备物模型属性
|
* 设备物模型属性
|
||||||
*/
|
*/
|
||||||
private Map<String, Object> properties;
|
private List<Map<String, Object>> properties;
|
||||||
/**
|
/**
|
||||||
* 固件版本
|
* 固件版本
|
||||||
*/
|
*/
|
||||||
|
@ -6,7 +6,9 @@ import lombok.EqualsAndHashCode;
|
|||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
import org.springframework.boot.configurationprocessor.json.JSONObject;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -36,12 +38,13 @@ public class DeviceDto {
|
|||||||
/**
|
/**
|
||||||
* 扩展属性
|
* 扩展属性
|
||||||
*/
|
*/
|
||||||
private Map<String, Object> extendParams;
|
|
||||||
|
private List<Map<String, Object>> extendParams;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设备物模型属性
|
* 设备物模型属性
|
||||||
*/
|
*/
|
||||||
private Map<String, Object> properties;
|
private List<Map<String, Object>> properties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 所属产品
|
* 所属产品
|
||||||
|
@ -4,15 +4,16 @@ import com.baomidou.mybatisplus.annotation.IEnum;
|
|||||||
import com.baomidou.mybatisplus.annotation.TableField;
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
import com.baomidou.mybatisplus.annotation.TableName;
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
import com.zsc.edu.gateway.common.enums.IState;
|
import com.zsc.edu.gateway.common.enums.IState;
|
||||||
import com.zsc.edu.gateway.framework.json.MapJsonTypeHandler;
|
import com.zsc.edu.gateway.framework.json.JsonTypeHandler;
|
||||||
import com.zsc.edu.gateway.modules.iot.product.entity.Product;
|
import com.zsc.edu.gateway.modules.iot.product.entity.Product;
|
||||||
import com.zsc.edu.gateway.modules.system.entity.BaseEntity;
|
import com.zsc.edu.gateway.modules.system.entity.BaseEntity;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
import org.springframework.boot.configurationprocessor.json.JSONObject;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author 15864
|
* @author 15864
|
||||||
@ -64,14 +65,15 @@ public class Device extends BaseEntity {
|
|||||||
/**
|
/**
|
||||||
* 扩展配置
|
* 扩展配置
|
||||||
*/
|
*/
|
||||||
@TableField(typeHandler = MapJsonTypeHandler.class)
|
//TODO String
|
||||||
private Map<String, Object> extendParams;
|
@TableField(typeHandler = JsonTypeHandler.class)
|
||||||
|
private List<JSONObject> extendParams;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设备属性
|
* 设备属性
|
||||||
*/
|
*/
|
||||||
@TableField(typeHandler = MapJsonTypeHandler.class)
|
@TableField(typeHandler = JsonTypeHandler.class)
|
||||||
private Map<String, Object> properties;
|
private List<JSONObject> properties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 所属产品ID
|
* 所属产品ID
|
||||||
|
@ -6,8 +6,10 @@ import com.zsc.edu.gateway.modules.iot.device.entity.Device;
|
|||||||
import com.zsc.edu.gateway.modules.iot.product.entity.Product;
|
import com.zsc.edu.gateway.modules.iot.product.entity.Product;
|
||||||
import com.zsc.edu.gateway.modules.iot.tsl.entity.Param;
|
import com.zsc.edu.gateway.modules.iot.tsl.entity.Param;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.configurationprocessor.json.JSONObject;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@ -61,12 +63,12 @@ public class DeviceVo {
|
|||||||
/**
|
/**
|
||||||
* 扩展配置
|
* 扩展配置
|
||||||
*/
|
*/
|
||||||
private Map<String, Object> extendParams;
|
private List<Map<String, Object>> extendParams;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设备属性
|
* 设备属性
|
||||||
*/
|
*/
|
||||||
private Map<String, Object> properties;
|
private List<Map<String, Object>> properties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 备注说明
|
* 备注说明
|
||||||
|
@ -3,7 +3,7 @@ package com.zsc.edu.gateway.modules.iot.record.entity;
|
|||||||
import com.baomidou.mybatisplus.annotation.TableField;
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
import com.baomidou.mybatisplus.annotation.TableName;
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
import com.zsc.edu.gateway.framework.json.MapJsonTypeHandler;
|
import com.zsc.edu.gateway.framework.json.JsonTypeHandler;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
import org.springframework.format.annotation.DateTimeFormat;
|
import org.springframework.format.annotation.DateTimeFormat;
|
||||||
|
|
||||||
@ -26,7 +26,7 @@ public class RecordData {
|
|||||||
|
|
||||||
private String attachmentId;
|
private String attachmentId;
|
||||||
|
|
||||||
@TableField(typeHandler = MapJsonTypeHandler.class)
|
@TableField(typeHandler = JsonTypeHandler.class)
|
||||||
private Map<String, Object> content;
|
private Map<String, Object> content;
|
||||||
|
|
||||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||||
|
@ -38,7 +38,6 @@ public class RecordDataServiceImpl extends ServiceImpl<RecordDataRepository, Rec
|
|||||||
return baseMapper.selectList(page, queryWrapper);
|
return baseMapper.selectList(page, queryWrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO 上报数据
|
|
||||||
@Override
|
@Override
|
||||||
public List<RecordData> recordData(String clientId) {
|
public List<RecordData> recordData(String clientId) {
|
||||||
LocalDateTime recordTime = LocalDateTime.parse(redisUtils.get("serve:sendTime:photograph:" + clientId));
|
LocalDateTime recordTime = LocalDateTime.parse(redisUtils.get("serve:sendTime:photograph:" + clientId));
|
||||||
|
@ -191,7 +191,7 @@ public class UserController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据ID查询用户
|
* 获取MENU
|
||||||
* */
|
* */
|
||||||
@GetMapping("/menu")
|
@GetMapping("/menu")
|
||||||
public List<MenuVo> menu(@AuthenticationPrincipal UserDetailsImpl userDetails) {
|
public List<MenuVo> menu(@AuthenticationPrincipal UserDetailsImpl userDetails) {
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package com.zsc.edu.gateway.modules.system.entity;
|
package com.zsc.edu.gateway.modules.system.entity;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.EnumValue;
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.IEnum;
|
||||||
import com.baomidou.mybatisplus.annotation.TableName;
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import com.zsc.edu.gateway.common.enums.IState;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -29,14 +31,33 @@ public class Menu extends BaseEntity {
|
|||||||
private String permissions;
|
private String permissions;
|
||||||
private String authority;
|
private String authority;
|
||||||
|
|
||||||
@Getter
|
public enum Type implements IEnum<Integer>, IState<Type> {
|
||||||
@AllArgsConstructor
|
/**
|
||||||
public enum Type {
|
* 页面
|
||||||
|
*/
|
||||||
PAGE(1, "页面"),
|
PAGE(1, "页面"),
|
||||||
|
/**
|
||||||
|
* 操作
|
||||||
|
*/
|
||||||
OPERATION(2, "操作");
|
OPERATION(2, "操作");
|
||||||
@EnumValue
|
|
||||||
private final Integer code;
|
private final Integer value;
|
||||||
private final String desc;
|
private final String description;
|
||||||
|
|
||||||
|
Type(int value, String description) {
|
||||||
|
this.value = value;
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer getValue() {
|
||||||
|
return this.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.description;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -14,9 +14,9 @@
|
|||||||
<result column="client_id" property="clientId"/>
|
<result column="client_id" property="clientId"/>
|
||||||
<result column="product_id" property="productId"/>
|
<result column="product_id" property="productId"/>
|
||||||
<result column="extend_params" property="extendParams"
|
<result column="extend_params" property="extendParams"
|
||||||
typeHandler="com.zsc.edu.gateway.framework.json.MapJsonTypeHandler"/>
|
typeHandler="com.zsc.edu.gateway.framework.json.JsonTypeHandler"/>
|
||||||
<result column="properties" property="properties"
|
<result column="properties" property="properties"
|
||||||
typeHandler="com.zsc.edu.gateway.framework.json.MapJsonTypeHandler"/>
|
typeHandler="com.zsc.edu.gateway.framework.json.JsonTypeHandler"/>
|
||||||
<result column="create_by" jdbcType="VARCHAR" property="deviceCreateBy"/>
|
<result column="create_by" jdbcType="VARCHAR" property="deviceCreateBy"/>
|
||||||
<result column="update_by" jdbcType="VARCHAR" property="deviceUpdateBy"/>
|
<result column="update_by" jdbcType="VARCHAR" property="deviceUpdateBy"/>
|
||||||
<result column="create_time" jdbcType="DATE" property="deviceCreateTime"/>
|
<result column="create_time" jdbcType="DATE" property="deviceCreateTime"/>
|
||||||
|
Loading…
Reference in New Issue
Block a user