Pārlūkot izejas kodu

refactor(devinfo):重构设备名称填充逻辑以支持泛型- 修改 fillDevNames 方法签名,使用泛型参数替代具体类型- 引入 Function 和 BiConsumer 参数,提高方法通用性- 更新调用点,适配新的泛型方法签名
- 添加事务回滚异常类型指定
-优化代码格式和空格处理

hxd 1 dienu atpakaļ
vecāks
revīzija
6f4c8b7188

+ 1 - 1
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/gateway/impl/StatsGatewayImpl.java

@@ -92,7 +92,7 @@ public class StatsGatewayImpl implements StatsGateway {
         List<EventsDTO> dtoList = CopyUtils.copyList(page.getRecords(), EventsDTO.class);
 
         //新增 填充devName
-        devInfoService.fillDevNames(dtoList);
+        devInfoService.fillDevNames(dtoList,EventsDTO::getClientId, EventsDTO::setDevName);
 
         // 封装成 PageRecord 返回
         return CopyUtils.copyPage(page, dtoList);

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

@@ -54,7 +54,7 @@ public class WebAdminDealGatewayImpl implements WebAdminDealGateway {
 
 
     @Override
-    @Transactional
+    @Transactional(rollbackFor = Exception.class)
     public void unbind(AdminDealUnbindParam param) {
 
         // 设备解绑
@@ -75,7 +75,7 @@ public class WebAdminDealGatewayImpl implements WebAdminDealGateway {
             throw new BizException(ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorCode(), ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorMessage());
         }
 
-        tblOprLogService.saveLog(param.getDevId(),OprLogType.ADMIN_DEAL_UNBIND.getCode(), devInfo);
+        tblOprLogService.saveLog(param.getDevId(), OprLogType.ADMIN_DEAL_UNBIND.getCode(), devInfo);
         log.info("设备信息:{}", JSON.toJSONString(devInfo));
 
         // 查询设备分享者 逻辑删除
@@ -121,7 +121,7 @@ public class WebAdminDealGatewayImpl implements WebAdminDealGateway {
                         .set(DevRoom::getIsDeleted, 1)
         );
         // 设备下跌倒事件 逻辑删除
-        List<Events> events = eventsService.list(new  LambdaQueryWrapper<Events>().eq(Events::getClientId, devInfo.getClientId()));
+        List<Events> events = eventsService.list(new LambdaQueryWrapper<Events>().eq(Events::getClientId, devInfo.getClientId()));
         log.info("设备下跌倒事件:{}", JSON.toJSONString(events));
         eventsService.update(
                 Wrappers.<Events>lambdaUpdate()

+ 2 - 1
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/gateway/impl/WebStatsGatewayImpl.java

@@ -103,7 +103,8 @@ public class WebStatsGatewayImpl implements WebStatsGateway {
         List<EventsDTO> dtoList = CopyUtils.copyList(eventsPage.getRecords(), EventsDTO.class);
 
         // 4.填充devName
-        devInfoService.fillDevNames(dtoList);
+//        devInfoService.fillDevNames(dtoList);
+        devInfoService.fillDevNames(dtoList, EventsDTO::getClientId, EventsDTO::setDevName);
 
         // 5.封装成 PageRecord 返回
         return CopyUtils.copyPage(eventsPage, dtoList);

+ 6 - 3
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/service/DevInfoService.java

@@ -2,15 +2,17 @@ package com.hfln.portal.infrastructure.service;
 
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.IService;
-import com.hfln.portal.common.dto.data.event.EventsDTO;
 import com.hfln.portal.common.request.device.DeviceListQueryReq;
 import com.hfln.portal.common.request.device.UpdateDeviceParams;
 import com.hfln.portal.infrastructure.po.DevInfo;
 
+
 import java.math.BigDecimal;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
+import java.util.function.BiConsumer;
+import java.util.function.Function;
 
 public interface DevInfoService extends IService<DevInfo> {
 
@@ -47,5 +49,6 @@ public interface DevInfoService extends IService<DevInfo> {
     /**
      * 批量填充设备名称
      */
-    void fillDevNames(List<EventsDTO> dtoList);
-}
+//    void fillDevNames(List<EventsDTO> dtoList);
+
+    <T> void fillDevNames(List<T> list, Function<T, String> clientIdGetter, BiConsumer<T, String> devNameSetter);}

+ 47 - 14
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/service/impl/DevInfoServiceImpl.java

@@ -5,7 +5,6 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.hfln.portal.common.dto.data.event.EventsDTO;
 import com.hfln.portal.common.request.device.DeviceListQueryReq;
 import com.hfln.portal.common.request.device.UpdateDeviceParams;
 import com.hfln.portal.domain.customer.util.UpdateWrapperBuilder;
@@ -23,6 +22,8 @@ import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.temporal.TemporalAdjusters;
 import java.util.*;
+import java.util.function.BiConsumer;
+import java.util.function.Function;
 import java.util.stream.Collectors;
 
 @Service
@@ -269,25 +270,57 @@ public class DevInfoServiceImpl extends ServiceImpl<DevInfoMapper, DevInfo> impl
         return this.baseMapper.queryStatInfoV2(LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()));
     }
 
+//    @Override
+//    public void fillDevNames(List<EventsDTO> dtoList) {
+//        if (CollectionUtils.isEmpty(dtoList)){
+//            return;
+//        }
+//
+//        Set<String> allClientIds = dtoList.stream()
+//                .map(EventsDTO::getClientId)
+//                .collect(Collectors.toSet());
+//
+//        if (!allClientIds.isEmpty()) {
+//            Map<String, String> devIdNameMap = this.queryByClientIds(allClientIds)
+//                    .stream()
+//                    .collect(Collectors.toMap(
+//                            DevInfo::getClientId,
+//                            dev -> Optional.ofNullable(dev.getDevName()).orElse("未命名设备")
+//                    ));
+//
+//            dtoList.forEach(dto -> dto.setDevName(devIdNameMap.getOrDefault(dto.getClientId(),"未知设备")));
+//        }
+//    }
+
     @Override
-    public void fillDevNames(List<EventsDTO> dtoList) {
-        if (CollectionUtils.isEmpty(dtoList)){
+    public <T> void fillDevNames(
+            List<T> list,
+            Function<T, String> clientIdGetter,
+            BiConsumer<T, String> devNameSetter) {
+
+        if (CollectionUtils.isEmpty(list)) {
             return;
         }
 
-        Set<String> allClientIds = dtoList.stream()
-                .map(EventsDTO::getClientId)
+        Set<String> allClientIds = list.stream()
+                .map(clientIdGetter)
+                .filter(Objects::nonNull)
                 .collect(Collectors.toSet());
 
-        if (!allClientIds.isEmpty()) {
-            Map<String, String> devIdNameMap = this.queryByClientIds(allClientIds)
-                    .stream()
-                    .collect(Collectors.toMap(
-                            DevInfo::getClientId,
-                            dev -> Optional.ofNullable(dev.getDevName()).orElse("未命名设备")
-                    ));
-
-            dtoList.forEach(dto -> dto.setDevName(devIdNameMap.getOrDefault(dto.getClientId(),"未知设备")));
+        if (allClientIds.isEmpty()) {
+            return;
         }
+
+        Map<String, String> devIdNameMap = this.queryByClientIds(allClientIds)
+                .stream()
+                .collect(Collectors.toMap(
+                        DevInfo::getClientId,
+                        dev -> Optional.ofNullable(dev.getDevName()).orElse("未命名设备")
+                ));
+
+        list.forEach(item -> {
+            String clientId = clientIdGetter.apply(item);
+            devNameSetter.accept(item, devIdNameMap.getOrDefault(clientId, "未知设备"));
+        });
     }
 }