瀏覽代碼

refactor(device):优化设备网关事务管理与代码格式- 为多个方法添加 @Transactional(rollbackFor = Exception.class) 注解以增强事务处理
- 调整 DevInfo 实体类中的字段注释和空行,提升代码可读性
- 移除 DeviceGatewayImpl 中不再使用的辅助方法 setIfNull
- 统一 logService.saveLog 方法调用的代码风格,增加空格提高可读性- 修复 UserGatewayImpl 和 WebQuestionGatewayImpl 中事务注解的遗漏问题

hxd 4 周之前
父節點
當前提交
8690fab169

+ 10 - 21
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/gateway/impl/DeviceGatewayImpl.java

@@ -269,7 +269,7 @@ public class DeviceGatewayImpl implements DeviceGateway {
         }
 
         // 记录操作日志
-        logService.saveLog(Long.valueOf(devId),OprLogType.UNBLIND_DEVICE.getCode(), logContent);
+        logService.saveLog(Long.valueOf(devId), OprLogType.UNBLIND_DEVICE.getCode(), logContent);
         return updateFlag;
     }
 
@@ -298,7 +298,7 @@ public class DeviceGatewayImpl implements DeviceGateway {
         devInfo.setActiveTime(LocalDateTime.now());
         devInfoService.updateById(devInfo);
 
-        logService.saveLog(devInfo.getDevId(),OprLogType.BIND_DEVICE.getCode(), String.format("用户 %d 绑定设备 %s", request.getUserId(), request.getClientId()));
+        logService.saveLog(devInfo.getDevId(), OprLogType.BIND_DEVICE.getCode(), String.format("用户 %d 绑定设备 %s", request.getUserId(), request.getClientId()));
 
         //5.返回绑定结果和设备主键id
         DeviceDTO result = new DeviceDTO();
@@ -326,9 +326,6 @@ public class DeviceGatewayImpl implements DeviceGateway {
     }
 
 
-
-
-
     @Override
     @Transactional(rollbackFor = Exception.class)
     public Boolean handleEvent(Long eventListId) {
@@ -387,7 +384,7 @@ public class DeviceGatewayImpl implements DeviceGateway {
             throw new BizException(ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorCode(), ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorMessage());
         }
 
-        logService.saveLog(devInfo.getDevId(),OprLogType.UPDATE_DEVICE.getCode(), devInfo);
+        logService.saveLog(devInfo.getDevId(), OprLogType.UPDATE_DEVICE.getCode(), devInfo);
 
         //2.更新数据
         devInfo.setTenantId(params.getTenantId());
@@ -468,7 +465,7 @@ public class DeviceGatewayImpl implements DeviceGateway {
             throw new BizException(ErrorEnum.DEVICE_UPDATE_FAIL.getErrorCode(), ErrorEnum.DEVICE_UPDATE_FAIL.getErrorMessage());
         }
 
-        logService.saveLog(devInfo.getDevId(),OprLogType.UPDATE_DEVICE.getCode(), params);
+        logService.saveLog(devInfo.getDevId(), OprLogType.UPDATE_DEVICE.getCode(), params);
 
         //5.通过MQTT把信息参数发送到设备
         mqttSend.sendDeviceParamsToMqtt(devInfo.getClientId(), params);
@@ -489,15 +486,6 @@ public class DeviceGatewayImpl implements DeviceGateway {
     }
 
 
-    // 辅助方法:如果值为null则设置默认值
-    private <T> void setIfNull(java.util.function.Supplier<T> getter,
-                               java.util.function.Supplier<T> defaultValueSupplier,
-                               java.util.function.Consumer<T> setter) {
-        if (getter.get() == null) {
-            setter.accept(defaultValueSupplier.get());
-        }
-    }
-
     @Override
     public Boolean updateDeviceLocation(DeviceLocationParams params) {
 
@@ -548,7 +536,7 @@ public class DeviceGatewayImpl implements DeviceGateway {
     }
 
     @Override
-    @Transactional
+    @Transactional(rollbackFor = Exception.class)
     public Boolean statusLight(StatusLightParams params) {
         //1.校验设备是否存在
         DevInfo devInfo = devInfoService.getById(params.getDevId());
@@ -611,6 +599,7 @@ public class DeviceGatewayImpl implements DeviceGateway {
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public Boolean saveRoom(RoomParam param) {
 
         //查询设备信息
@@ -645,7 +634,7 @@ public class DeviceGatewayImpl implements DeviceGateway {
         }
 
         // 记录操作日志
-        logService.saveLog(param.getDevId(),OprLogType.UPDATE_ROOM.getCode(), param);
+        logService.saveLog(param.getDevId(), OprLogType.UPDATE_ROOM.getCode(), param);
 
         return true;
     }
@@ -801,7 +790,7 @@ public class DeviceGatewayImpl implements DeviceGateway {
         devShareService.removeById(devShare);
 
         //3.取消分享记录到日志
-        logService.saveLog(devShare.getDevId(),OprLogType.CANCEL_SHARE.getCode(), shareId);
+        logService.saveLog(devShare.getDevId(), OprLogType.CANCEL_SHARE.getCode(), shareId);
     }
 
     @Override
@@ -820,7 +809,7 @@ public class DeviceGatewayImpl implements DeviceGateway {
         devShareService.updateById(devShare);
 
         //3.记录操作日志
-        logService.saveLog(devShare.getDevId(),OprLogType.UPDATE_DEVICE_SHARE_PERMISSION.getCode(), param);
+        logService.saveLog(devShare.getDevId(), OprLogType.UPDATE_DEVICE_SHARE_PERMISSION.getCode(), param);
     }
 
     @Override
@@ -1137,7 +1126,7 @@ public class DeviceGatewayImpl implements DeviceGateway {
                 devInfo.getDevId(),             // 转移的设备id
                 userInfo.getUserId()            // 转移后的用户id
         );
-        logService.saveLog(param.getDevId(),OprLogType.DEVICE_TRANSFER.getCode(), logContent);
+        logService.saveLog(param.getDevId(), OprLogType.DEVICE_TRANSFER.getCode(), logContent);
         return true;
     }
 

+ 6 - 0
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/gateway/impl/UserGatewayImpl.java

@@ -118,6 +118,7 @@ public class UserGatewayImpl implements UserGateway {
     private TblOprLogService logService;
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public UserInfoWxRes loginWx(String code) {
         if (StringUtils.isEmpty(code)) {
             throw new BizException(ErrorEnum.WECHAT_CODE_ISNULL.getErrorCode(), ErrorEnum.WECHAT_CODE_ISNULL.getErrorMessage());
@@ -143,6 +144,7 @@ public class UserGatewayImpl implements UserGateway {
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public UserTokenInfo loginByPhone(PhoneLoginParams params) {
 
         // 1 校验当前用户是否已注册
@@ -202,6 +204,7 @@ public class UserGatewayImpl implements UserGateway {
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public UserDto queryByOpenId(String openId) {
 
         UserInfo userInfo = userService.queryByOpenId(openId);
@@ -214,6 +217,7 @@ public class UserGatewayImpl implements UserGateway {
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public UserTokenInfo loginBySmsCode(LoginBySmsCodeParams request) {
         String phone = request.getUserName();
         UserInfo userInfo = userService.queryByPhone(phone);
@@ -265,6 +269,7 @@ public class UserGatewayImpl implements UserGateway {
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public String getUserPhone(String code, String accessToken) {
         if (StringUtils.isEmpty(code)) {
             throw new BizException(ErrorEnum.WECHAT_CODE_ISNULL.getErrorCode(), ErrorEnum.WECHAT_CODE_ISNULL.getErrorMessage());
@@ -462,6 +467,7 @@ public class UserGatewayImpl implements UserGateway {
 
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public void uploadCommonFile(MultipartFile file, String fileType) throws IOException {
         // 校验文件非空
         if (file.isEmpty()) {

+ 6 - 3
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/gateway/impl/WebQuestionGatewayImpl.java

@@ -46,6 +46,7 @@ public class WebQuestionGatewayImpl implements WebQuestionGateway {
     private UserService userService;
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public QuestionnaireDTO saveQuestionnaire(QuestionnaireSaveParam param) {
 
         Questionnaire questionnaire;
@@ -72,8 +73,8 @@ public class WebQuestionGatewayImpl implements WebQuestionGateway {
         return CopyUtils.copy(questionnaire, QuestionnaireDTO.class);
     }
 
-    @Transactional(rollbackFor = Exception.class)
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public void deleteQuestionnaire(Long questionnaireId) {
         // 1. 检查问卷是否存在
         Questionnaire questionnaire = questionnaireService.getById(questionnaireId);
@@ -113,6 +114,7 @@ public class WebQuestionGatewayImpl implements WebQuestionGateway {
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public PageRecord<QuestionnaireDTO> queryQuestionnaire(QuestionnaireQueryParam param) {
 
         // 1. 构建分页对象
@@ -161,6 +163,7 @@ public class WebQuestionGatewayImpl implements WebQuestionGateway {
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public QuestionnaireDetailsDTO queryQuestionnaireDetails(Long questionnaireId) {
         if (questionnaireId == null) {
             throw new BizException(
@@ -200,8 +203,8 @@ public class WebQuestionGatewayImpl implements WebQuestionGateway {
         return detailsDTO;
     }
 
-    @Transactional(rollbackFor = Exception.class)
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public List<QuestionDTO> saveQuestion(List<QuestionSaveParam> params) {
         if (params == null || params.isEmpty()) {
             throw new BizException(ErrorEnum.QUESTION_LIST_NOT_EXIST.getErrorCode(), ErrorEnum.QUESTION_LIST_NOT_EXIST.getErrorMessage());
@@ -276,8 +279,8 @@ public class WebQuestionGatewayImpl implements WebQuestionGateway {
                 .collect(Collectors.toList());
     }
 
-    @Transactional(rollbackFor = Exception.class)
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public void saveAnswer(List<AnswerSaveParam> params) {
 
         // 1. 校验入参是否为空

+ 5 - 7
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/po/DevInfo.java

@@ -3,6 +3,7 @@ package com.hfln.portal.infrastructure.po;
 import com.baomidou.mybatisplus.annotation.*;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
+
 import java.math.BigDecimal;
 import java.time.LocalDateTime;
 
@@ -63,7 +64,6 @@ public class DevInfo extends BasePO {
     private LocalDateTime onoffTime;
 
 
-
     /**
      * 软件版本号
      */
@@ -161,7 +161,6 @@ public class DevInfo extends BasePO {
     private BigDecimal width;
 
 
-
     /**
      * 北向夹角 //判断数值 0 90 180 270
      */
@@ -205,7 +204,7 @@ public class DevInfo extends BasePO {
     private Integer existFlag;
 
     /**
-     *  存在改变时间
+     * 存在改变时间
      */
     @TableField("presence_change_time")
     private LocalDateTime presenceChangeTime;
@@ -226,7 +225,7 @@ public class DevInfo extends BasePO {
      * 设备信息常量类
      */
     public static class Constants {
-        
+
         /**
          * 在线状态
          */
@@ -242,9 +241,8 @@ public class DevInfo extends BasePO {
             public static final int NOT_EXIST = 0;  // 不存在
             public static final int EXIST = 1;   // 存在
         }
-        
 
-        
+
         /**
          * 安装方式
          */
@@ -252,7 +250,7 @@ public class DevInfo extends BasePO {
             public static final String WALL = "Wall";     // 墙装
             public static final String CEILING = "Ceiling"; // 顶装
         }
-        
+
         /**
          * 指示灯状态
          */