refactor(iot): 重构参数处理逻辑

- 合并参数创建和更新方法,减少重复代码
- 优化参数查询和删除逻辑,提高效率
-调整服务和产品相关方法,适应新的参数处理方式
-移除未使用的 ParamType 枚举
This commit is contained in:
zhuangtianxiang 2024-12-19 15:26:54 +08:00
parent f6a12e2b35
commit be080ffb3a
14 changed files with 115 additions and 94 deletions

View File

@ -13,6 +13,7 @@ import com.zsc.edu.gateway.modules.iot.product.repo.ProductParamRepository;
import com.zsc.edu.gateway.modules.iot.product.repo.ProductRepository; import com.zsc.edu.gateway.modules.iot.product.repo.ProductRepository;
import com.zsc.edu.gateway.modules.iot.product.service.ProductService; import com.zsc.edu.gateway.modules.iot.product.service.ProductService;
import com.zsc.edu.gateway.modules.iot.tsl.dto.ParamDto; import com.zsc.edu.gateway.modules.iot.tsl.dto.ParamDto;
import com.zsc.edu.gateway.modules.iot.tsl.entity.EventParam;
import com.zsc.edu.gateway.modules.iot.tsl.entity.Param; import com.zsc.edu.gateway.modules.iot.tsl.entity.Param;
import com.zsc.edu.gateway.modules.iot.tsl.mapper.ParamMapper; import com.zsc.edu.gateway.modules.iot.tsl.mapper.ParamMapper;
import com.zsc.edu.gateway.modules.iot.tsl.repo.ParamRepository; import com.zsc.edu.gateway.modules.iot.tsl.repo.ParamRepository;
@ -21,6 +22,7 @@ import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
/** /**
@ -45,7 +47,10 @@ public class ProductServiceImpl extends ServiceImpl<ProductRepository, Product>
} }
Product product = mapper.toEntity(dto); Product product = mapper.toEntity(dto);
save(product); save(product);
productParamRepo.insert(paramService.productParamCreate(dto, product.getId())); List<ProductParam> productParams = paramService.paramCreate(dto.getParams()).stream()
.map(paramId -> new ProductParam(product.getId(), paramId))
.toList();
productParamRepo.insert(productParams);
return product; return product;
} }
/** /**
@ -56,7 +61,12 @@ public class ProductServiceImpl extends ServiceImpl<ProductRepository, Product>
Product product = baseMapper.selectById(id); Product product = baseMapper.selectById(id);
mapper.convert(dto, product); mapper.convert(dto, product);
updateById(product); updateById(product);
paramService.productParamUpdate(dto.getParams(), id); paramService.paramUpdate(dto.getParams(),
productParamRepo.selectList(new LambdaQueryWrapper<ProductParam>()
.eq(ProductParam::getProductId, product.getId()))
.stream()
.map(ProductParam::getParamId)
.collect(Collectors.toList()));
return product; return product;
} }

View File

@ -21,7 +21,9 @@ public class ServeDto {
public String identifier; public String identifier;
private List<ParamDto> inputs; private List<ParamDto> params;
private List<ParamDto> outputs; //TODO 将inputs和outputs整合成一个params集合操作一样params中有type区分
// private List<ParamDto> inputs;
// private List<ParamDto> outputs;
} }

View File

@ -32,6 +32,10 @@ public class Param extends BaseParam {
*/ */
private Type type; private Type type;
// /**
// * 事件/服务/产品的类型
// */
// private ParamType paramType;
public enum Type implements IEnum<Integer>, IState<Type> { public enum Type implements IEnum<Integer>, IState<Type> {
/** /**
@ -65,4 +69,37 @@ public class Param extends BaseParam {
return this.description; return this.description;
} }
} }
// public enum ParamType implements IEnum<Integer>, IState<ParamType> {
// /**
// * 事件
// */
// EVENT(1, "Event"),
// /**
// * 服务
// */
// SERVICE(2, "Service"),
// /**
// * 产品
// */
// PRODUCT(3, "Product");
//
// private final int value;
// private final String description;
//
// ParamType(int value, String description) {
// this.value = value;
// this.description = description;
// }
//
// @Override
// public Integer getValue() {
// return value;
// }
//
// @Override
// public String toString() {
// return this.description;
// }
// }
} }

View File

@ -24,16 +24,11 @@ public class Serve extends BaseParam {
private Long productId; private Long productId;
/** /**
* 服务所需参数 * 服务输入/输出参数根据param中的type区分
*/ */
@TableField(exist = false) @TableField(exist = false)
private List<Param> inputs; private List<Param> params;
/**
* 服务输出的参数
*/
@TableField(exist = false)
private List<Param> outputs;
} }

View File

@ -3,5 +3,8 @@ package com.zsc.edu.gateway.modules.iot.tsl.repo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zsc.edu.gateway.modules.iot.tsl.entity.EventParam; import com.zsc.edu.gateway.modules.iot.tsl.entity.EventParam;
/**
* @author zhuang
*/
public interface EventParamRepository extends BaseMapper<EventParam> { public interface EventParamRepository extends BaseMapper<EventParam> {
} }

View File

@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsc.edu.gateway.modules.iot.tsl.entity.Event; import com.zsc.edu.gateway.modules.iot.tsl.entity.Event;
import com.zsc.edu.gateway.modules.iot.tsl.query.EventQuery; import com.zsc.edu.gateway.modules.iot.tsl.query.EventQuery;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Select;

View File

@ -3,5 +3,8 @@ package com.zsc.edu.gateway.modules.iot.tsl.repo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zsc.edu.gateway.modules.iot.tsl.entity.Param; import com.zsc.edu.gateway.modules.iot.tsl.entity.Param;
/**
* @author lenovo
*/
public interface ParamRepository extends BaseMapper<Param> { public interface ParamRepository extends BaseMapper<Param> {
} }

View File

@ -11,6 +11,9 @@ import org.apache.ibatis.annotations.Param;
import java.util.List; import java.util.List;
/**
* @author zhuang
*/
public interface PropertyRepository extends BaseMapper<Property> { public interface PropertyRepository extends BaseMapper<Property> {

View File

@ -16,15 +16,7 @@ import java.util.List;
* @author zhuang * @author zhuang
*/ */
public interface ParamService extends IService<Param> { public interface ParamService extends IService<Param> {
List<EventParam> eventParamCreate(EventDto dto, Long eventId); List<Long> paramCreate(List<ParamDto> params);
List<ProductParam> productParamCreate(ProductDto dto, Long productId); boolean paramUpdate(List<ParamDto> paramDto, List<Long> paramIds);
List<ServeParam> serveParamCreate(ServeDto dto, Long serveId);
boolean eventParamUpdate(List<ParamDto> paramDto, Long eventId);
boolean productParamUpdate(List<ParamDto> paramDto, Long productId);
boolean serveParamUpdate(List<ParamDto> paramDto, Long serveId);
} }

View File

@ -23,6 +23,7 @@ import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
/** /**
* @author Yao * @author Yao
@ -46,7 +47,10 @@ public class EventServiceImpl extends ServiceImpl<EventRepository, Event> implem
} }
Event event = mapper.toEntity(dto); Event event = mapper.toEntity(dto);
save(event); save(event);
eventParamRepo.insert(paramService.eventParamCreate(dto, event.getId())); List<EventParam> eventParams = paramService.paramCreate(dto.getOutputs()).stream()
.map(paramId -> new EventParam(event.getId(), paramId))
.toList();
eventParamRepo.insert(eventParams);
return event; return event;
} }
/** /**
@ -57,7 +61,12 @@ public class EventServiceImpl extends ServiceImpl<EventRepository, Event> implem
Event event = baseMapper.selectById(id); Event event = baseMapper.selectById(id);
mapper.convert(dto, event); mapper.convert(dto, event);
updateById(event); updateById(event);
paramService.eventParamUpdate(dto.getOutputs(), id); paramService.paramUpdate(dto.getOutputs(),
eventParamRepo.selectList(new LambdaQueryWrapper<EventParam>()
.eq(EventParam::getEventId, event.getId()))
.stream()
.map(EventParam::getParamId)
.collect(Collectors.toList()));
return event; return event;
} }
/** /**

View File

@ -5,7 +5,6 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsc.edu.gateway.modules.iot.product.dto.ProductDto; import com.zsc.edu.gateway.modules.iot.product.dto.ProductDto;
import com.zsc.edu.gateway.modules.iot.product.entity.ProductParam; import com.zsc.edu.gateway.modules.iot.product.entity.ProductParam;
import com.zsc.edu.gateway.modules.iot.product.repo.ProductParamRepository; import com.zsc.edu.gateway.modules.iot.product.repo.ProductParamRepository;
import com.zsc.edu.gateway.modules.iot.tsl.dto.EventDto;
import com.zsc.edu.gateway.modules.iot.tsl.dto.ParamDto; import com.zsc.edu.gateway.modules.iot.tsl.dto.ParamDto;
import com.zsc.edu.gateway.modules.iot.tsl.dto.ServeDto; import com.zsc.edu.gateway.modules.iot.tsl.dto.ServeDto;
import com.zsc.edu.gateway.modules.iot.tsl.entity.EventParam; import com.zsc.edu.gateway.modules.iot.tsl.entity.EventParam;
@ -21,6 +20,8 @@ import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/** /**
* @author zhuang * @author zhuang
@ -34,79 +35,34 @@ public class ParamServiceImpl extends ServiceImpl<ParamRepository, Param> implem
private final ProductParamRepository productParamRepository; private final ProductParamRepository productParamRepository;
private final ServeParamRepository serveParamRepository; private final ServeParamRepository serveParamRepository;
@Override
public List<Long> paramCreate(List<ParamDto> params) { public List<Long> paramCreate(List<ParamDto> params) {
List<Long> paramIds = new ArrayList<>(); List<Long> paramIds = new ArrayList<>();
params.forEach(paramDto -> { List<Param> paramsToInsert = params.stream()
Param param = mapper.toEntity(paramDto); .map(mapper::toEntity)
baseMapper.insert(param); .collect(Collectors.toList());
paramIds.add(param.getId()); baseMapper.insert(paramsToInsert);
}); paramsToInsert.forEach(param -> paramIds.add(param.getId()));
return paramIds; return paramIds;
} }
@Override @Override
public List<EventParam> eventParamCreate(EventDto dto, Long eventId) {
return paramCreate(dto.getOutputs()).stream()
.map(paramId -> new EventParam(eventId, paramId))
.toList();
}
@Override
public List<ProductParam> productParamCreate(ProductDto dto, Long productId) {
return paramCreate(dto.getParams()).stream()
.map(paramId -> new ProductParam(productId, paramId))
.toList();
}
@Override
public List<ServeParam> serveParamCreate(ServeDto dto, Long serveId) {
List<ServeParam> serveParams = new ArrayList<>();
serveParams.addAll(paramCreate(dto.getInputs()).stream()
.map(paramId -> new ServeParam(serveId, paramId))
.toList());
serveParams.addAll(paramCreate(dto.getOutputs()).stream()
.map(paramId -> new ServeParam(serveId, paramId))
.toList());
return serveParams;
}
public boolean paramUpdate(List<ParamDto> paramDto, List<Long> paramIds) { public boolean paramUpdate(List<ParamDto> paramDto, List<Long> paramIds) {
List<Param> params = baseMapper.selectByIds(paramIds); List<Param> params = baseMapper.selectByIds(paramIds);
if (!params.isEmpty() && !paramDto.isEmpty()) { if (!params.isEmpty() && !paramDto.isEmpty()) {
for (int i = 0; i < Math.min(params.size(), paramDto.size()); i++) { List<Param> updatedParams = IntStream.range(0, Math.min(params.size(), paramDto.size()))
Param param = params.get(i); .parallel()
ParamDto dto = paramDto.get(i); .mapToObj(i -> {
paramMapper.convert(dto, param); Param param = params.get(i);
baseMapper.updateById(param); ParamDto dto = paramDto.get(i);
} paramMapper.convert(dto, param);
return param;
})
.collect(Collectors.toList());
baseMapper.updateById(updatedParams);
} }
return true; return true;
} }
@Override //TODO 整合方法方法一保持现状但是会有多个联表多个entity多个repo方法二建新联表其中values(product_id,serve_id,event_id,param_id)一表联三表
public boolean eventParamUpdate(List<ParamDto> paramDto, Long eventId) {
return paramUpdate(paramDto, eventParamRepository.selectList(new LambdaQueryWrapper<EventParam>()
.eq(EventParam::getEventId, eventId))
.stream()
.map(EventParam::getParamId)
.toList());
}
@Override
public boolean productParamUpdate(List<ParamDto> paramDto, Long productId) {
return paramUpdate(paramDto, productParamRepository.selectList(new LambdaQueryWrapper<ProductParam>()
.eq(ProductParam::getProductId, productId))
.stream()
.map(ProductParam::getParamId)
.toList());
}
@Override
public boolean serveParamUpdate(List<ParamDto> paramDto, Long serveId) {
return paramUpdate(paramDto, serveParamRepository.selectList(new LambdaQueryWrapper<ServeParam>()
.eq(ServeParam::getServeId, serveId))
.stream()
.map(ServeParam::getParamId)
.toList());
}
} }

View File

@ -21,6 +21,8 @@ import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/** /**
@ -31,7 +33,6 @@ import java.util.List;
public class ServeServiceImpl extends ServiceImpl<ServeRepository, Serve> implements ServeService { public class ServeServiceImpl extends ServiceImpl<ServeRepository, Serve> implements ServeService {
private final ServeMapper mapper; private final ServeMapper mapper;
private final ParamMapper paramMapper;
private final ParamRepository paramRepository; private final ParamRepository paramRepository;
private final ServeParamRepository serveParamRepository; private final ServeParamRepository serveParamRepository;
private final ParamService paramService; private final ParamService paramService;
@ -45,7 +46,10 @@ public class ServeServiceImpl extends ServiceImpl<ServeRepository, Serve> implem
} }
Serve serve = mapper.toEntity(dto); Serve serve = mapper.toEntity(dto);
save(serve); save(serve);
serveParamRepository.insert(paramService.serveParamCreate(dto, serve.getId())); List<ServeParam> serveParams = paramService.paramCreate(dto.getParams()).stream()
.map(paramId -> new ServeParam(serve.getId(), paramId))
.toList();
serveParamRepository.insert(serveParams);
return serve; return serve;
} }
/** /**
@ -56,8 +60,12 @@ public class ServeServiceImpl extends ServiceImpl<ServeRepository, Serve> implem
Serve serve = baseMapper.selectById(id); Serve serve = baseMapper.selectById(id);
mapper.convert(dto, serve); mapper.convert(dto, serve);
updateById(serve); updateById(serve);
paramService.serveParamUpdate(dto.getInputs(), id); paramService.paramUpdate(dto.getParams(),
paramService.serveParamUpdate(dto.getOutputs(), id); serveParamRepository.selectList(new LambdaQueryWrapper<ServeParam>()
.eq(ServeParam::getServeId, serve.getId()))
.stream()
.map(ServeParam::getParamId)
.collect(Collectors.toList()));
return serve; return serve;
} }

View File

@ -70,9 +70,8 @@ public class BulletinController {
*/ */
@GetMapping() @GetMapping()
@PreAuthorize("hasAuthority('BULLETIN_QUERY')") @PreAuthorize("hasAuthority('BULLETIN_QUERY')")
public IPage<BulletinVo> query(Page<BulletinVo> page, BulletinQuery query) { public Page<Bulletin> query(Page<Bulletin> page, BulletinQuery query) {
// Page<BulletinVo> page = new Page<>(query.getPageNum(), query.getPageSize()); return service.page(page, query.wrapper());
return service.selectPageByConditions(page, query);
} }
/** /**

View File

@ -101,7 +101,10 @@ public class UserMessageServiceImpl extends ServiceImpl<UserMessageRepository, U
@Override @Override
public boolean markAsRead(UserDetailsImpl userDetails, List<Long> messageIds) { public boolean markAsRead(UserDetailsImpl userDetails, List<Long> messageIds) {
if (CollectionUtils.isEmpty(messageIds)) { if (CollectionUtils.isEmpty(messageIds)) {
throw new RuntimeException("messageIds is NULL!"); return this.lambdaUpdate().eq(UserMessage::getUserId, userDetails.getId())
.set(UserMessage::getIsRead, true)
.set(UserMessage::getReadTime, LocalDateTime.now())
.update();
} }
return this.lambdaUpdate().eq(UserMessage::getUserId, userDetails.getId()) return this.lambdaUpdate().eq(UserMessage::getUserId, userDetails.getId())
.in(UserMessage::getMessageId, messageIds) .in(UserMessage::getMessageId, messageIds)