refactor(iot): 重构 IoT 相关代码

- 优化了 ParamMapper 接口,删除了未使用的代码
- 改进了 ProductServiceImpl 中的代码格式和命名
- 调整了 PropertyQuery 和 PropertyServiceImpl 中的代码结构
- 删除了 GlobalResponseHandler 类,简化了响应处理逻辑
This commit is contained in:
zhuangtianxiang 2024-12-12 15:19:11 +08:00
parent 6f4a7a3345
commit 1c1c9586e6
11 changed files with 336 additions and 339 deletions

View File

@ -1,61 +1,61 @@
package com.zsc.edu.gateway.framework.response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
/**
* 响应统一封装
* <p>
* 将响应数据封装成统一的数据格式
* <p>
* 通过本处理器将接口方法返回的数据统一封装到 ResponseResult data 字段中如果接口方法返回为 void data 字段的值为 null
*
* @author zhuang
*/
@Slf4j
@RestControllerAdvice(basePackages = "com.zsc.edu.gateway")
public class GlobalResponseHandler implements ResponseBodyAdvice<Object> {
/**
* 此组件是否支持给定的控制器方法返回类型和选定的 {@code HttpMessageConverter} 类型
*
* @return 如果应该调用 {@link #beforeBodyWrite} 则为 {@code true}否则为false
*/
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
// 返回类型不为ResponseResult才需要封装
return returnType.getParameterType() != ResponseResult.class;
}
/**
* 统一封装返回响应数据
*/
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
ServerHttpResponse response) {
// 数据封装为ResponseResult将接口方法返回的数据封装到 ResponseResult.data 字段中
ResponseResult<Object> responseResult = ResponseResult.success(body);
// 返回类型不是 String直接返回
if (returnType.getParameterType() != String.class) {
return responseResult;
}
// 返回类型是 String不能直接返回需要进行额外处理
// 1. Content-Type 设为 application/json 返回类型是String时默认 Content-Type = text/plain
HttpHeaders headers = response.getHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
// 2. ResponseResult 转为 Json字符串 再返回
// 否则会报错 java.lang.ClassCastException: com.example.core.model.ResponseResult cannot be cast to java.lang.String
return JsonUtil.toJson(responseResult);
}
}
//package com.zsc.edu.gateway.framework.response;
//
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.core.MethodParameter;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.MediaType;
//import org.springframework.http.converter.HttpMessageConverter;
//import org.springframework.http.server.ServerHttpRequest;
//import org.springframework.http.server.ServerHttpResponse;
//import org.springframework.web.bind.annotation.RestControllerAdvice;
//import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
//
///**
// * 响应统一封装
// * <p>
// * 将响应数据封装成统一的数据格式
// * <p>
// * 通过本处理器将接口方法返回的数据统一封装到 ResponseResult data 字段中如果接口方法返回为 void data 字段的值为 null
// *
// * @author zhuang
// */
//@Slf4j
//@RestControllerAdvice(basePackages = "com.zsc.edu.gateway")
//public class GlobalResponseHandler implements ResponseBodyAdvice<Object> {
//
// /**
// * 此组件是否支持给定的控制器方法返回类型和选定的 {@code HttpMessageConverter} 类型
// *
// * @return 如果应该调用 {@link #beforeBodyWrite} 则为 {@code true}否则为false
// */
// @Override
// public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
// // 返回类型不为ResponseResult才需要封装
// return returnType.getParameterType() != ResponseResult.class;
// }
//
// /**
// * 统一封装返回响应数据
// */
// @Override
// public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
// Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
// ServerHttpResponse response) {
//
// // 数据封装为ResponseResult将接口方法返回的数据封装到 ResponseResult.data 字段中
// ResponseResult<Object> responseResult = ResponseResult.success(body);
//
// // 返回类型不是 String直接返回
// if (returnType.getParameterType() != String.class) {
// return responseResult;
// }
//
// // 返回类型是 String不能直接返回需要进行额外处理
// // 1. Content-Type 设为 application/json 返回类型是String时默认 Content-Type = text/plain
// HttpHeaders headers = response.getHeaders();
// headers.setContentType(MediaType.APPLICATION_JSON);
// // 2. ResponseResult 转为 Json字符串 再返回
// // 否则会报错 java.lang.ClassCastException: com.example.core.model.ResponseResult cannot be cast to java.lang.String
// return JsonUtil.toJson(responseResult);
// }
//}

View File

@ -1,265 +1,265 @@
package com.zsc.edu.gateway.framework.response;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 返回结果集
*
* @author javadog
**/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResponseResult<T> {
//TODO 返回封装处理
/**
* 状态码
*/
private Integer code;
/**
* 状态信息
*/
private Boolean status;
/**
* 返回信息
*/
private String message;
/**
* 数据
*/
private T data;
/**
* 全参数方法
*
* @param code 状态码
* @param status 状态
* @param message 返回信息
* @param data 返回数据
* @param <T> 泛型
* @return {@link ResponseResult<T>}
*/
private static <T> ResponseResult<T> response(Integer code, Boolean status, String message, T data) {
ResponseResult<T> responseResult = new ResponseResult<>();
responseResult.setCode(code);
responseResult.setStatus(status);
responseResult.setMessage(message);
responseResult.setData(data);
return responseResult;
}
/**
* 全参数方法
*
* @param code 状态码
* @param status 状态
* @param message 返回信息
* @param <T> 泛型
* @return {@link ResponseResult<T>}
*/
private static <T> ResponseResult<T> response(Integer code, Boolean status, String message) {
ResponseResult<T> responseResult = new ResponseResult<>();
responseResult.setCode(code);
responseResult.setStatus(status);
responseResult.setMessage(message);
return responseResult;
}
/**
* 成功返回无参
*
* @param <T> 泛型
* @return {@link ResponseResult<T>}
*/
public static <T> ResponseResult<T> success() {
// return response(HttpStatus.OK.value(), true, HttpStatus.OK.getReasonPhrase(), null);
return response(HttpStatusEnum.SUCCESS.getCode(), true, HttpStatusEnum.SUCCESS.getMessage(), null);
}
/**
* 成功返回枚举参数
*
* @param httpResponseEnum 枚举参数
* @param <T> 泛型
* @return {@link ResponseResult<T>}
*/
public static <T> ResponseResult<T> success(HttpStatusEnum httpResponseEnum) {
return response(httpResponseEnum.getCode(), true, httpResponseEnum.getMessage());
}
/**
* 成功返回状态码+返回信息
*
* @param code 状态码
* @param message 返回信息
* @param <T> 泛型
* @return {@link ResponseResult<T>}
*/
public static <T> ResponseResult<T> success(Integer code, String message) {
return response(code, true, message);
}
/**
* 成功返回返回信息 + 数据
*
* @param message 返回信息
* @param data 数据
* @param <T> 泛型
* @return {@link ResponseResult<T>}
*/
public static <T> ResponseResult<T> success(String message, T data) {
return response(HttpStatusEnum.SUCCESS.getCode(), true, message, data);
}
/**
* 成功返回状态码+返回信息+数据
*
* @param code 状态码
* @param message 返回信息
* @param data 数据
* @param <T> 泛型
* @return {@link ResponseResult<T>}
*/
public static <T> ResponseResult<T> success(Integer code, String message, T data) {
return response(code, true, message, data);
}
/**
* 成功返回数据
*
* @param data 数据
* @param <T> 泛型
* @return {@link ResponseResult<T>}
*/
public static <T> ResponseResult<T> success(T data) {
return response(HttpStatusEnum.SUCCESS.getCode(), true, HttpStatusEnum.SUCCESS.getMessage(), data);
}
/**
* 成功返回返回信息
*
* @param message 返回信息
* @param <T> 泛型
* @return {@link ResponseResult<T>}
*/
public static <T> ResponseResult<T> success(String message) {
return response(HttpStatusEnum.SUCCESS.getCode(), true, message, null);
}
/**
* 失败返回无参
*
* @param <T> 泛型
* @return {@link ResponseResult<T>}
*/
public static <T> ResponseResult<T> fail() {
return response(HttpStatusEnum.ERROR.getCode(), false, HttpStatusEnum.ERROR.getMessage(), null);
}
/**
* 失败返回枚举
*
* @param httpResponseEnum 枚举
* @param <T> 泛型
* @return {@link ResponseResult<T>}
*/
public static <T> ResponseResult<T> fail(HttpStatusEnum httpResponseEnum) {
return response(httpResponseEnum.getCode(), false, httpResponseEnum.getMessage());
}
/**
* 失败返回状态码+返回信息
*
* @param code 状态码
* @param message 返回信息
* @param <T> 泛型
* @return {@link ResponseResult<T>}
*/
public static <T> ResponseResult<T> fail(Integer code, String message) {
return response(code, false, message);
}
/**
* 失败返回返回信息+数据
*
* @param message 返回信息
* @param data 数据
* @param <T> 泛型
* @return {@link ResponseResult<T>}
*/
public static <T> ResponseResult<T> fail(String message, T data) {
return response(HttpStatusEnum.ERROR.getCode(), false, message, data);
}
/**
* 失败返回状态码+返回信息+数据
*
* @param code 状态码
* @param message 返回消息
* @param data 数据
* @param <T> 泛型
* @return {@link ResponseResult<T>}
*/
public static <T> ResponseResult<T> fail(Integer code, String message, T data) {
return response(code, false, message, data);
}
/**
* 失败返回数据
*
* @param data 数据
* @param <T> 泛型
* @return {@link ResponseResult<T>}
*/
public static <T> ResponseResult<T> fail(T data) {
return response(HttpStatusEnum.ERROR.getCode(), false, HttpStatusEnum.ERROR.getMessage(), data);
}
/**
* 失败返回返回信息
*
* @param message 返回信息
* @param <T> 泛型
* @return {@link ResponseResult<T>}
*/
public static <T> ResponseResult<T> fail(String message) {
return response(HttpStatusEnum.ERROR.getCode(), false, message, null);
}
}
//package com.zsc.edu.gateway.framework.response;
//
//import lombok.AllArgsConstructor;
//import lombok.Data;
//import lombok.NoArgsConstructor;
//
///**
// * 返回结果集
// *
// * @author javadog
// **/
//@Data
//@AllArgsConstructor
//@NoArgsConstructor
//public class ResponseResult<T> {
//
////TODO 返回封装处理
// /**
// * 状态码
// */
// private Integer code;
//
// /**
// * 状态信息
// */
// private Boolean status;
//
// /**
// * 返回信息
// */
// private String message;
//
// /**
// * 数据
// */
// private T data;
//
// /**
// * 全参数方法
// *
// * @param code 状态码
// * @param status 状态
// * @param message 返回信息
// * @param data 返回数据
// * @param <T> 泛型
// * @return {@link ResponseResult<T>}
// */
// private static <T> ResponseResult<T> response(Integer code, Boolean status, String message, T data) {
//
//
// ResponseResult<T> responseResult = new ResponseResult<>();
// responseResult.setCode(code);
// responseResult.setStatus(status);
// responseResult.setMessage(message);
// responseResult.setData(data);
// return responseResult;
// }
//
// /**
// * 全参数方法
// *
// * @param code 状态码
// * @param status 状态
// * @param message 返回信息
// * @param <T> 泛型
// * @return {@link ResponseResult<T>}
// */
// private static <T> ResponseResult<T> response(Integer code, Boolean status, String message) {
//
//
// ResponseResult<T> responseResult = new ResponseResult<>();
// responseResult.setCode(code);
// responseResult.setStatus(status);
// responseResult.setMessage(message);
// return responseResult;
// }
//
// /**
// * 成功返回无参
// *
// * @param <T> 泛型
// * @return {@link ResponseResult<T>}
// */
// public static <T> ResponseResult<T> success() {
//
//// return response(HttpStatus.OK.value(), true, HttpStatus.OK.getReasonPhrase(), null);
// return response(HttpStatusEnum.SUCCESS.getCode(), true, HttpStatusEnum.SUCCESS.getMessage(), null);
// }
//
// /**
// * 成功返回枚举参数
// *
// * @param httpResponseEnum 枚举参数
// * @param <T> 泛型
// * @return {@link ResponseResult<T>}
// */
// public static <T> ResponseResult<T> success(HttpStatusEnum httpResponseEnum) {
//
//
// return response(httpResponseEnum.getCode(), true, httpResponseEnum.getMessage());
// }
//
// /**
// * 成功返回状态码+返回信息
// *
// * @param code 状态码
// * @param message 返回信息
// * @param <T> 泛型
// * @return {@link ResponseResult<T>}
// */
// public static <T> ResponseResult<T> success(Integer code, String message) {
//
//
// return response(code, true, message);
// }
//
// /**
// * 成功返回返回信息 + 数据
// *
// * @param message 返回信息
// * @param data 数据
// * @param <T> 泛型
// * @return {@link ResponseResult<T>}
// */
// public static <T> ResponseResult<T> success(String message, T data) {
//
//
// return response(HttpStatusEnum.SUCCESS.getCode(), true, message, data);
// }
//
// /**
// * 成功返回状态码+返回信息+数据
// *
// * @param code 状态码
// * @param message 返回信息
// * @param data 数据
// * @param <T> 泛型
// * @return {@link ResponseResult<T>}
// */
// public static <T> ResponseResult<T> success(Integer code, String message, T data) {
//
//
// return response(code, true, message, data);
// }
//
// /**
// * 成功返回数据
// *
// * @param data 数据
// * @param <T> 泛型
// * @return {@link ResponseResult<T>}
// */
// public static <T> ResponseResult<T> success(T data) {
//
//
// return response(HttpStatusEnum.SUCCESS.getCode(), true, HttpStatusEnum.SUCCESS.getMessage(), data);
// }
//
// /**
// * 成功返回返回信息
// *
// * @param message 返回信息
// * @param <T> 泛型
// * @return {@link ResponseResult<T>}
// */
// public static <T> ResponseResult<T> success(String message) {
//
//
// return response(HttpStatusEnum.SUCCESS.getCode(), true, message, null);
// }
//
// /**
// * 失败返回无参
// *
// * @param <T> 泛型
// * @return {@link ResponseResult<T>}
// */
// public static <T> ResponseResult<T> fail() {
//
//
// return response(HttpStatusEnum.ERROR.getCode(), false, HttpStatusEnum.ERROR.getMessage(), null);
// }
//
// /**
// * 失败返回枚举
// *
// * @param httpResponseEnum 枚举
// * @param <T> 泛型
// * @return {@link ResponseResult<T>}
// */
// public static <T> ResponseResult<T> fail(HttpStatusEnum httpResponseEnum) {
//
//
// return response(httpResponseEnum.getCode(), false, httpResponseEnum.getMessage());
// }
//
// /**
// * 失败返回状态码+返回信息
// *
// * @param code 状态码
// * @param message 返回信息
// * @param <T> 泛型
// * @return {@link ResponseResult<T>}
// */
// public static <T> ResponseResult<T> fail(Integer code, String message) {
//
//
// return response(code, false, message);
// }
//
// /**
// * 失败返回返回信息+数据
// *
// * @param message 返回信息
// * @param data 数据
// * @param <T> 泛型
// * @return {@link ResponseResult<T>}
// */
// public static <T> ResponseResult<T> fail(String message, T data) {
//
//
// return response(HttpStatusEnum.ERROR.getCode(), false, message, data);
// }
//
// /**
// * 失败返回状态码+返回信息+数据
// *
// * @param code 状态码
// * @param message 返回消息
// * @param data 数据
// * @param <T> 泛型
// * @return {@link ResponseResult<T>}
// */
// public static <T> ResponseResult<T> fail(Integer code, String message, T data) {
//
//
// return response(code, false, message, data);
// }
//
// /**
// * 失败返回数据
// *
// * @param data 数据
// * @param <T> 泛型
// * @return {@link ResponseResult<T>}
// */
// public static <T> ResponseResult<T> fail(T data) {
//
//
// return response(HttpStatusEnum.ERROR.getCode(), false, HttpStatusEnum.ERROR.getMessage(), data);
// }
//
// /**
// * 失败返回返回信息
// *
// * @param message 返回信息
// * @param <T> 泛型
// * @return {@link ResponseResult<T>}
// */
// public static <T> ResponseResult<T> fail(String message) {
//
//
// return response(HttpStatusEnum.ERROR.getCode(), false, message, null);
// }
//}

View File

@ -100,6 +100,7 @@ public class AttachmentController {
/**
* 根据附件ID删除附件信息
*/
//TODO 上传时没有url
@DeleteMapping("delete/{id}")
public Boolean delete(@PathVariable("id") String id) {
return service.removeById(id);

View File

@ -61,7 +61,6 @@ public class ProductServiceImpl extends ServiceImpl<ProductRepository, Product>
productParamRepo.insert(productParams);
return product;
}
/**
* 更新产品
*/

View File

@ -23,13 +23,11 @@ import java.util.Optional;
@Service
public class RecordServiceImpl extends ServiceImpl<RecordRepository, Record> implements RecordService {
RedisUtils redisUtils;
@Override
public Record create(Record record) {
save(record);
return record;
}
@Override
public List<Record> page(IPage<Record> page, String clientId) {
LambdaQueryWrapper<Record> queryWrapper = new LambdaQueryWrapper<>();

View File

@ -3,9 +3,6 @@ package com.zsc.edu.gateway.modules.iot.tsl.mapper;
import com.zsc.edu.gateway.common.mapstruct.BaseMapper;
import com.zsc.edu.gateway.modules.iot.tsl.dto.ParamDto;
import com.zsc.edu.gateway.modules.iot.tsl.entity.Param;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface ParamMapper extends BaseMapper<ParamDto, Param> {
}

View File

@ -30,7 +30,6 @@ import java.util.List;
@AllArgsConstructor
public class EventServiceImpl extends ServiceImpl<EventRepository, Event> implements EventService {
private final EventMapper mapper;
private final ParamMapper paramMapper;
private final ParamRepository paramRepo;
private final EventParamRepository eventParamRepo;
@ -104,7 +103,6 @@ public class EventServiceImpl extends ServiceImpl<EventRepository, Event> implem
/**
* 查询详情
*
* @param id Id
* @return Event
*/

View File

@ -116,7 +116,6 @@ public class ServeServiceImpl extends ServiceImpl<ServeRepository, Serve> implem
/**
* 查询详情
*
* @param id 主键
* @return Serve
*/
@ -124,7 +123,6 @@ public class ServeServiceImpl extends ServiceImpl<ServeRepository, Serve> implem
public Serve detail(Long id) {
return baseMapper.selectById(id);
}
@Override
public Boolean delete(Long id) {
removeById(id);

View File

@ -93,6 +93,12 @@ public class BulletinServiceImpl extends ServiceImpl<BulletinRepository, Bulleti
bulletin.setCreateBy(userDetails.getName());
bulletin.setCreateTime(LocalDateTime.now());
bulletin.state = edit;
if (dto.getAttachmentIds() != null) {
LambdaQueryWrapper<BulletinAttachment> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(BulletinAttachment::getBulletinId, bulletin.getId());
bulletinAttachmentRepository.delete(queryWrapper);
insertInto(bulletin.getId(), dto.getAttachmentIds());
}
return updateById(bulletin);
}
/**

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB