浏览代码

1.封装 设备转移服务号推送方法
2.增添 设备转移 操作记录数据库日志

hxd 1 月之前
父节点
当前提交
87bc3fc107

+ 1 - 0
portal-service-domain/src/main/java/com/hfln/portal/domain/customer/OprLogType.java

@@ -25,6 +25,7 @@ public enum OprLogType {
 
     UPDATE_DEVICE("update_device","编辑设备"),
     UNBLIND_DEVICE("unbind_device", "解绑设备"),
+    DEVICE_TRANSFER("transfer_device", "设备转移"),
 
     USER_LOGOFF("logoff_user", "用户注销"),
     ;

+ 25 - 18
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/gateway/impl/DeviceGatewayImpl.java

@@ -38,6 +38,7 @@ import com.hfln.portal.domain.exception.ErrorEnum;
 import com.hfln.portal.domain.gateway.DeviceGateway;
 import com.hfln.portal.infrastructure.mqtt.MqttClient;
 import com.hfln.portal.infrastructure.mqtt.MqttSend;
+import com.hfln.portal.infrastructure.mqtt.MqttSubHandle;
 import com.hfln.portal.infrastructure.oss.OssClient;
 import com.hfln.portal.infrastructure.oss.OssUtils;
 import com.hfln.portal.infrastructure.po.*;
@@ -126,6 +127,9 @@ public class DeviceGatewayImpl implements DeviceGateway {
     @Autowired
     private org.springframework.data.redis.core.RedisTemplate<String, Object> redisTemplate;
 
+    @Autowired
+    private MqttSubHandle mqttSubHandle;
+
 
     @Override
     public HomeInfoDTO queryHomeInfo(Long userId) {
@@ -385,6 +389,7 @@ public class DeviceGatewayImpl implements DeviceGateway {
 
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public Boolean handleEvent(Long eventListId) {
 
         //1.查询事件信息
@@ -1134,10 +1139,10 @@ public class DeviceGatewayImpl implements DeviceGateway {
     }
 
     @Override
-    @Transactional
+    @Transactional(rollbackFor = Exception.class)
     public Boolean deviceTransfer(DeviceTransferParams param){
 
-        //1. 查询设备信息并校验是否存在
+        // 1.查询设备信息并校验是否存在
         DevInfo devInfo = devInfoService.getOne(
                 Wrappers.<DevInfo>lambdaQuery()
                         .eq(DevInfo::getDevId, param.getDevId())
@@ -1147,28 +1152,28 @@ public class DeviceGatewayImpl implements DeviceGateway {
             throw new BizException(ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorCode(), ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorMessage());
         }
 
-        //2.校验用户权限
+        // 2.校验用户权限
         if(!Objects.equals(param.getUserId(), devInfo.getUserId())){
             throw new BizException(ErrorEnum.USER_NO_PERMISSION.getErrorCode(), ErrorEnum.USER_NO_PERMISSION.getErrorMessage());
         }
 
-        //3.根据手机号查询转移对象是否注册
+        // 3.根据手机号查询转移对象是否注册
         UserInfo userInfo = userService.queryByPhone(param.getPhone());
         if (userInfo == null){
             throw new BizException(ErrorEnum.USER_IS_NOT_EXIST.getErrorCode(), ErrorEnum.USER_IS_NOT_EXIST.getErrorMessage());
         }
 
-        //4.转移当前设备归属
+        // 4.转移当前设备归属
         devInfo.setUserId(userInfo.getUserId());
 
-        //5.设备分享表修改
+        // 5.设备分享表修改
         List<DevShare> devShareList = devShareService.list(
                 Wrappers.<DevShare>lambdaQuery()
                         .eq(DevShare::getDevId, param.getDevId())
                         .eq(DevShare::getIsDeleted, BasePO.DeleteFlag.NOT_DELETED)
         );
 
-        //6. 遍历设备分享表
+        // 6.遍历设备分享表
         if (!devShareList.isEmpty()) {
             devShareList.forEach(devShare -> {
                 if (Objects.equals(devShare.getSharerUserId(), userInfo.getUserId())) {
@@ -1180,20 +1185,22 @@ public class DeviceGatewayImpl implements DeviceGateway {
             });
             devShareService.updateBatchById(devShareList);  // 批量更新设备分享记录
         }
-        //7. 更新设备信息(设备归属转移)
+        // 7.更新设备信息(设备归属转移)
         devInfo.setUpdateTime(LocalDateTime.now());  // 更新修改时间
         devInfoService.updateById(devInfo);  // 更新设备信息
 
-        //8.服务号推送告知转移对象
-        List<WxRelation> wxRelations = wxRelationService.queryByUnionId(userInfo.getUnionId());
-        String fwhOpenId = "";
-        if (wxRelations != null && !wxRelations.isEmpty()) {
-            fwhOpenId = wxRelations.get(0).getFwhOpenId();
-        }
-        log.info("mqttutil--当前useropenid=" + userInfo.getOpenid() + ", fwhopenId=" + fwhOpenId);
-        log.info("发送微信公众号信息:devName=" + devInfo.getDevName() + ", phoneNumber=" + userInfo.getPhone() + ", fwhOpenId=" + fwhOpenId);
-        wxOfficeAccountClient.sendMsg(devInfo.getClientId(), devInfo.getDevName(),userInfo.getPhone(), fwhOpenId, "设备转移成功,请前往小程序查看");
-        log.info("发送微信公众号信息完毕");
+        // 8.服务号推送告知转移对象
+        mqttSubHandle.pushDeviceTransferMsg(devInfo, userInfo);
+
+        // 9.记录操作日志
+        String logContent = String.format(
+                "用户 %d 将设备 %s(ID=%s)转移给用户 %d",
+                param.getUserId(),              // 转移前用户id
+                devInfo.getDevName(),           // 转移的设备名称
+                devInfo.getDevId(),             // 转移的设备id
+                userInfo.getUserId()            // 转移后的用户id
+        );
+        logService.saveLog(OprLogType.DEVICE_TRANSFER.getCode(), logContent );
         return true;
     }
 

+ 22 - 0
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/mqtt/MqttSubHandle.java

@@ -320,6 +320,28 @@ public class MqttSubHandle {
 
 
     /**
+     * 推送设备转移消息给转移对象
+     */
+    public void pushDeviceTransferMsg(DevInfo devInfo, UserInfo targetUser){
+
+        // 根据 unionId 查询服务号关系
+        List<WxRelation> wxRelations = wxRelationService.queryByUnionId(targetUser.getUnionId());
+        String fwhOpenId = "";
+        if (wxRelations != null && !wxRelations.isEmpty()) {
+            fwhOpenId = wxRelations.get(0).getFwhOpenId();
+        }
+
+        // 日志打印
+        log.info("mqttutil--当前useropenid={}, fwhopenId={}", targetUser.getOpenid(), fwhOpenId);
+        log.info("发送微信公众号信息:devName={}, phoneNumber={}, fwhOpenId={}", devInfo.getDevName(), targetUser.getPhone(), fwhOpenId);
+
+        // 服务号推送消息
+        wxOfficeAccountClient.sendMsg(devInfo.getClientId(), devInfo.getDevName(),targetUser.getPhone(), fwhOpenId, "设备转移成功,请前往小程序查看");
+
+        log.info("发送微信公众号信息完毕");
+    }
+
+    /**
      * 发送短信和微信公众号通知
      * @param sendList 需要发送通知的用户列表
      * @param devName 设备名称