refactor(transaction): 在服务层方法中添加事务注解

- 在多个服务类的创建、更新和删除方法中添加 @Transactional 注解
- 优化数据库操作,确保数据一致性
- 主要涉及设备、产品、参数、属性、事件和服务等模块
This commit is contained in:
zhuangtianxiang 2025-01-08 19:34:05 +08:00
parent b431bf1c08
commit 616d640228
15 changed files with 43 additions and 24 deletions

View File

@ -22,6 +22,7 @@ import com.zsc.edu.gateway.modules.iot.product.repo.ProductRepository;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.*;
@ -46,6 +47,7 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceRepository, Device> imp
* 新建设备
*/
@Override
@Transactional
public Device create(DeviceDto dto) {
if (baseMapper.findByName(dto.getName()) != null) {
throw new ConstraintException("该设备已存在!");
@ -60,6 +62,7 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceRepository, Device> imp
* 批量新增
*/
@Override
@Transactional
public List<Device> batchCreate(BatchDeviceDto dto) {
if (dto.getNum() == null || dto.getNum() <= 0) {
throw new ConstraintException("设备数量必须大于0");
@ -118,6 +121,7 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceRepository, Device> imp
* 更新设备
*/
@Override
@Transactional
public Device update(DeviceDto dto, Long id) {
Device device = baseMapper.selectById(id);
if (Objects.equals(device.getName(), dto.getName())) {

View File

@ -11,6 +11,7 @@ import com.zsc.edu.gateway.modules.iot.tsl.entity.Param;
import com.zsc.edu.gateway.modules.iot.tsl.service.ParamService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
@ -26,6 +27,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductRepository, Product>
* 新建产品
*/
@Override
@Transactional
public Product create(ProductDto dto) {
if (baseMapper.findByName(dto.getName()) != null) {
throw new ConstraintException("该设备已存在!");
@ -34,7 +36,6 @@ public class ProductServiceImpl extends ServiceImpl<ProductRepository, Product>
throw new ConstraintException("设备名称不能为空!");
}
Product product = mapper.toEntity(dto);
product.setId(null);
save(product);
if (dto.getParams() != null) {
paramService.create(dto.getParams(), product.getId(), Param.ForeignType.PRODUCT);
@ -45,6 +46,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductRepository, Product>
* 更新产品
*/
@Override
@Transactional
public Product update(ProductDto dto, Long id) {
if (dto.getName() == null) {
throw new ConstraintException("设备名称不能为空!");
@ -69,6 +71,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductRepository, Product>
* 删除
*/
@Override
@Transactional
public boolean delete(Long id) {
removeById(id);
paramService.delete(id);

View File

@ -11,6 +11,7 @@ import com.zsc.edu.gateway.modules.iot.tsl.service.EventService;
import com.zsc.edu.gateway.modules.iot.tsl.service.ParamService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* @author Yao
@ -25,7 +26,7 @@ public class EventServiceImpl extends ServiceImpl<EventRepository, Event> implem
* 新建物模型事件
*/
@Override
@Transactional
public Event create(EventDto dto) {
if (baseMapper.findByName(dto.getName()) != null) {
throw new ConstraintException("该事件已存在!");
@ -42,6 +43,7 @@ public class EventServiceImpl extends ServiceImpl<EventRepository, Event> implem
* 更新物模型事件
*/
@Override
@Transactional
public Event update(EventDto dto, Long id) {
if (dto.getName() == null) {
throw new ConstraintException("事件名称不能为空!");
@ -71,6 +73,7 @@ public class EventServiceImpl extends ServiceImpl<EventRepository, Event> implem
*删除
*/
@Override
@Transactional
public boolean delete(Long id) {
removeById(id);
return paramService.delete(id);

View File

@ -9,6 +9,7 @@ import com.zsc.edu.gateway.modules.iot.tsl.repo.ParamRepository;
import com.zsc.edu.gateway.modules.iot.tsl.service.ParamService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Objects;
@ -24,6 +25,7 @@ public class ParamServiceImpl extends ServiceImpl<ParamRepository, Param> implem
private final ParamMapper mapper;
private final ParamMapper paramMapper;
@Override
@Transactional
public Boolean create(List<ParamDto> params, Long id, Param.ForeignType foreignType) {
List<Param> paramsToInsert = params.stream()
.map(dto -> {
@ -38,6 +40,7 @@ public class ParamServiceImpl extends ServiceImpl<ParamRepository, Param> implem
}
@Override
@Transactional
public Boolean update(List<ParamDto> paramDto, Long id) {
List<Param> params = baseMapper.selectList(new LambdaQueryWrapper<Param>()
.eq(Objects.nonNull(id), Param::getForeignId, id));
@ -57,6 +60,7 @@ public class ParamServiceImpl extends ServiceImpl<ParamRepository, Param> implem
}
@Override
@Transactional
public Boolean delete(Long id) {
return baseMapper.delete(new LambdaQueryWrapper<Param>()
.eq(Objects.nonNull(id), Param::getForeignId, id)) > 0;

View File

@ -24,6 +24,7 @@ public class PropertyServiceImpl extends ServiceImpl<PropertyRepository, Propert
* 新建物模型属性
*/
@Override
@Transactional
public Property create(PropertyDto dto) {
if (baseMapper.findByName(dto.getName()) != null) {
throw new ApiException("该属性已存在!");

View File

@ -11,6 +11,7 @@ import com.zsc.edu.gateway.modules.iot.tsl.service.ParamService;
import com.zsc.edu.gateway.modules.iot.tsl.service.ServeService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
@ -29,6 +30,7 @@ public class ServeServiceImpl extends ServiceImpl<ServeRepository, Serve> implem
* 新建功能
*/
@Override
@Transactional
public Serve create(ServeDto dto) {
if (baseMapper.findByName(dto.getName()) != null) {
throw new ConstraintException("该服务已存在!");
@ -47,6 +49,7 @@ public class ServeServiceImpl extends ServiceImpl<ServeRepository, Serve> implem
* 更新功能
*/
@Override
@Transactional
public Serve update(ServeDto dto, Long id) {
if (dto.getName() == null) {
throw new ConstraintException("服务名称不能为空!");
@ -81,6 +84,7 @@ public class ServeServiceImpl extends ServiceImpl<ServeRepository, Serve> implem
* @return true
*/
@Override
@Transactional
public Boolean delete(Long id) {
removeById(id);
return paramService.delete(id);

View File

@ -32,12 +32,12 @@ public class UserNoticeController {
* 普通用户查看消息详情
*
* @param userDetails 操作用户
* @param messageId 消息ID
* @param noticeId 消息ID
* @return 用户消息详情
*/
@GetMapping("/self/{noticeId}")
public UserNoticeVo selfDetail(@AuthenticationPrincipal UserDetailsImpl userDetails, @PathVariable("noticeId") Long messageId) {
return service.detail(messageId, userDetails.getId());
public UserNoticeVo selfDetail(@AuthenticationPrincipal UserDetailsImpl userDetails, @PathVariable("noticeId") Long noticeId) {
return service.detail(noticeId, userDetails.getId());
}
/**

View File

@ -17,7 +17,7 @@ import org.apache.ibatis.annotations.Param;
*/
public interface UserNoticeRepository extends BaseMapper<UserNotice> {
UserNoticeVo selectByNoticeIdAndUserId(@Param("noticeId") Long messageId, @Param("userId") Long userId);
UserNoticeVo selectByNoticeIdAndUserId(@Param("noticeId") Long noticeId, @Param("userId") Long userId);
IPage<UserNoticeVo> page(Page<UserNoticeVo> page, @Param("query") UserNoticeQuery query);

View File

@ -22,13 +22,13 @@ public interface UserNoticeService extends IService<UserNotice> {
Boolean createByAdmin(UserNoticeDto dto);
UserNoticeVo detail(Long messageId, Long userId);
UserNoticeVo detail(Long noticeId, Long userId);
IPage<UserNoticeVo> page(Page<UserNoticeVo> page, UserNoticeQuery query);
Integer countUnread(UserDetailsImpl userDetails);
boolean markAsRead(UserDetailsImpl userDetails, List<Long> messageIds);
boolean markAsRead(UserDetailsImpl userDetails, List<Long> noticeIds);
boolean markAllAsRead(UserDetailsImpl userDetails);

View File

@ -13,7 +13,7 @@ public class UserNoticeVo {
/**
* 用户消息id
*/
private Long messageId;
private Long noticeId;
/**
* 是否已读
*/

View File

@ -497,16 +497,16 @@ VALUES (1, 'Device1', TRUE, 1, 'HW1.0', 'FW1.0', 'FactoryA', 'Client1', 1, '{"pa
INSERT INTO iot_product (id, name, product_type, model, link, create_by, create_time, update_by, update_time, remark,
dept_id)
VALUES (1, 'Product1', 'TypeA', 'ModelX', 1, 'Admin', CURRENT_TIMESTAMP, 'Admin', CURRENT_TIMESTAMP, 'Remark1', 101),
(2, 'Product2', 'TypeB', 'ModelY', 2, 'Admin', CURRENT_TIMESTAMP, 'Admin', CURRENT_TIMESTAMP, 'Remark2', 102),
(3, 'Product3', 'TypeA', 'ModelZ', 1, 'Admin', CURRENT_TIMESTAMP, 'Admin', CURRENT_TIMESTAMP, 'Remark3', 103),
(4, 'Product4', 'TypeC', 'ModelW', 3, 'Admin', CURRENT_TIMESTAMP, 'Admin', CURRENT_TIMESTAMP, 'Remark4', 104),
(5, 'Product5', 'TypeB', 'ModelV', 2, 'Admin', CURRENT_TIMESTAMP, 'Admin', CURRENT_TIMESTAMP, 'Remark5', 105),
(6, 'Product6', 'TypeD', 'ModelU', 4, 'Admin', CURRENT_TIMESTAMP, 'Admin', CURRENT_TIMESTAMP, 'Remark6', 106),
(7, 'Product7', 'TypeA', 'ModelT', 1, 'Admin', CURRENT_TIMESTAMP, 'Admin', CURRENT_TIMESTAMP, 'Remark7', 107),
(8, 'Product8', 'TypeC', 'ModelS', 3, 'Admin', CURRENT_TIMESTAMP, 'Admin', CURRENT_TIMESTAMP, 'Remark8', 108),
(9, 'Product9', 'TypeB', 'ModelR', 2, 'Admin', CURRENT_TIMESTAMP, 'Admin', CURRENT_TIMESTAMP, 'Remark9', 109),
(10, 'Product10', 'TypeD', 'ModelQ', 4, 'Admin', CURRENT_TIMESTAMP, 'Admin', CURRENT_TIMESTAMP, 'Remark10', 110);
VALUES ('Product1', 'TypeA', 'ModelX', 1, 'Admin', CURRENT_TIMESTAMP, 'Admin', CURRENT_TIMESTAMP, 'Remark1', 101),
('Product2', 'TypeB', 'ModelY', 2, 'Admin', CURRENT_TIMESTAMP, 'Admin', CURRENT_TIMESTAMP, 'Remark2', 102),
('Product3', 'TypeA', 'ModelZ', 1, 'Admin', CURRENT_TIMESTAMP, 'Admin', CURRENT_TIMESTAMP, 'Remark3', 103),
('Product4', 'TypeC', 'ModelW', 3, 'Admin', CURRENT_TIMESTAMP, 'Admin', CURRENT_TIMESTAMP, 'Remark4', 104),
('Product5', 'TypeB', 'ModelV', 2, 'Admin', CURRENT_TIMESTAMP, 'Admin', CURRENT_TIMESTAMP, 'Remark5', 105),
('Product6', 'TypeD', 'ModelU', 4, 'Admin', CURRENT_TIMESTAMP, 'Admin', CURRENT_TIMESTAMP, 'Remark6', 106),
('Product7', 'TypeA', 'ModelT', 1, 'Admin', CURRENT_TIMESTAMP, 'Admin', CURRENT_TIMESTAMP, 'Remark7', 107),
('Product8', 'TypeC', 'ModelS', 3, 'Admin', CURRENT_TIMESTAMP, 'Admin', CURRENT_TIMESTAMP, 'Remark8', 108),
('Product9', 'TypeB', 'ModelR', 2, 'Admin', CURRENT_TIMESTAMP, 'Admin', CURRENT_TIMESTAMP, 'Remark9', 109),
('Product10', 'TypeD', 'ModelQ', 4, 'Admin', CURRENT_TIMESTAMP, 'Admin', CURRENT_TIMESTAMP, 'Remark10', 110);
INSERT INTO iot_param (id, data_type, uint, type, identifier, name, remark)
VALUES (1, 1, 'm', 1, 'identifier1', 'Parameter1', 'Remark1'),
@ -544,7 +544,7 @@ VALUES (1, 1, 1, 'prop1', 'Property 1', 'This is property 1'),
(9, 1, 1, 'prop9', 'Property 9', 'This is property 9'),
(10, 2, 2, 'prop10', 'Property 10', 'This is property 10');
INSERT INTO iot_serve (id, product_id, identifier, name, remark)
INSERT INTO iot_serve (product_id, identifier, name, remark)
VALUES (1, 'serve1', 'Service 1', 'This is service 1'),
(2, 'serve2', 'Service 2', 'This is service 2'),
(3, 'serve3', 'Service 3', 'This is service 3'),

View File

@ -1,6 +1,6 @@
create table iot_event
(
id bigserial not null
id bigint not null
constraint iot_event_pk
primary key,
product_id bigint,

View File

@ -1,6 +1,6 @@
create table iot_product
(
id bigserial not null
id bigint not null
constraint iot_product_pk
primary key,
name varchar,

View File

@ -1,6 +1,6 @@
create table iot_serve
(
id bigserial not null
id bigint not null
constraint iot_serve_pk
primary key,
product_id bigint,

View File

@ -4,7 +4,7 @@
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zsc.edu.gateway.modules.message.repo.UserNoticeRepository">
<resultMap id="userNoticeMap" type="com.zsc.edu.gateway.modules.message.vo.UserNoticeVo">
<id column="message_id" jdbcType="BIGINT" property="messageId"/>
<id column="notice_id" jdbcType="BIGINT" property="noticeId"/>
<result column="is_read" jdbcType="BOOLEAN" property="isRead"/>
<result column="read_time" jdbcType="TIMESTAMP" property="readTime"/>
<result column="username" jdbcType="VARCHAR" property="username"/>