Bläddra i källkod

feat(device): 添加设备今日人员进出次数统计功能

- 在 DeviceDTO 中新增 inOutCount 字段用于记录今日人员进出次数- 优化 DevOnOffInfoServiceImpl 中的数据类型转换逻辑- 新增 PersonInOutService 接口及其实现类中的批量统计方法
- 在 WebGatewayImpl 中集成并回填设备今日进出次数数据
hxd 3 veckor sedan
förälder
incheckning
153c38bd4a

+ 6 - 0
portal-service-common/src/main/java/com/hfln/portal/common/dto/data/device/DeviceDTO.java

@@ -190,6 +190,12 @@ public class DeviceDTO extends BaseVO {
     private Integer offlineCount;
 
     /**
+     * 今日人员进出次数
+     */
+    @Schema(description = "今日人员进出次数")
+    private Integer inOutCount;
+
+    /**
      * 人物存在标志:0-无人,1-有人
      */
     @Schema(description = "人物存在标志:0-无人,1-有人")

+ 10 - 4
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/gateway/impl/WebGatewayImpl.java

@@ -109,6 +109,9 @@ public class WebGatewayImpl implements WebGateway {
     @Autowired
     private DevOnOffInfoService devOnOffInfoService;
 
+    @Autowired
+    private PersonInOutService personInOutService;
+
     @Override
     public void upsertParameter(ParameterUpsertParam param) {
 
@@ -260,20 +263,23 @@ public class WebGatewayImpl implements WebGateway {
                 .collect(Collectors.toSet());
 
         // 批量查今日掉线次数
-        Map<Long, Integer> offlineCountMap = Collections.emptyMap();
-        if (!CollectionUtils.isEmpty(devIds)) {
-            offlineCountMap = devOnOffInfoService.batchDevOfflineCount(devIds);
-        }
+        Map<Long, Integer> offlineCountMap = devOnOffInfoService.batchDevOfflineCount(devIds);
+
+
+        // 批量查今日进出次数
+        Map<Long, Integer> inOutCountMap = personInOutService.batchPersonInOutCount(devIds);
 
         // 一次性回填 tenantName & offlineCount
         Map<Long, String> finalTenantIdNameMap = tenantIdNameMap;
         Map<Long, Integer> finalOfflineCountMap = offlineCountMap;
+        Map<Long, Integer> finalInOutCountMap = inOutCountMap;
 
         deviceDTOList.forEach(dto -> {
             if (dto.getTenantId() != null) {
                 dto.setTenantName(finalTenantIdNameMap.get(dto.getTenantId()));
             }
             dto.setOfflineCount(finalOfflineCountMap.getOrDefault(dto.getDevId(), 0));
+            dto.setInOutCount(finalInOutCountMap.getOrDefault(dto.getDevId(), 0));
         });
 
 

+ 5 - 0
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/service/PersonInOutService.java

@@ -5,6 +5,9 @@ import com.baomidou.mybatisplus.extension.service.IService;
 import com.hfln.portal.common.request.event.WebInOutParams;
 import com.hfln.portal.infrastructure.po.PersonInOutInfo;
 
+import java.util.Map;
+import java.util.Set;
+
 public interface PersonInOutService extends IService<PersonInOutInfo> {
 
     PersonInOutInfo queryLatestOneByDevId(Long devId);
@@ -12,4 +15,6 @@ public interface PersonInOutService extends IService<PersonInOutInfo> {
     Long countActive(Long tenantId);
 
     Page<PersonInOutInfo> selectPage(WebInOutParams params);
+
+    Map<Long, Integer> batchPersonInOutCount(Set<Long> devIds);
 }

+ 2 - 2
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/service/impl/DevOnOffInfoServiceImpl.java

@@ -77,8 +77,8 @@ public class DevOnOffInfoServiceImpl extends ServiceImpl<DevOnOffInfoMapper, Dev
         // 转成 Map<devId, count>
         return countList.stream()
                 .collect(Collectors.toMap(
-                        m -> (Long) m.get("dev_id"),
-                        m -> ((Long) m.get("cnt")).intValue()
+                        m -> ((Number) m.get("dev_id")).longValue(),
+                        m -> ((Number) m.get("cnt")).intValue()
                 ));
     }
 }

+ 32 - 2
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/service/impl/PersonInOutServiceImpl.java

@@ -1,7 +1,9 @@
 package com.hfln.portal.infrastructure.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.hfln.portal.common.request.event.WebInOutParams;
@@ -11,8 +13,10 @@ import com.hfln.portal.infrastructure.service.PersonInOutService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 
+import java.time.LocalDate;
 import java.time.LocalDateTime;
-import java.util.Objects;
+import java.util.*;
+import java.util.stream.Collectors;
 
 @Service
 @Slf4j
@@ -58,4 +62,30 @@ public class PersonInOutServiceImpl extends ServiceImpl<PersonInOutMapper, Perso
         queryWrapper.orderByDesc(PersonInOutInfo::getCreateTime);
         return this.baseMapper.selectPage(page, queryWrapper);
     }
-}
+
+    @Override
+    public Map<Long, Integer> batchPersonInOutCount(Set<Long> devIds) {
+        if (CollectionUtils.isEmpty(devIds)){
+            return Collections.emptyMap();
+        }
+
+        LocalDateTime start = LocalDate.now().atStartOfDay();
+        LocalDateTime end = start.plusDays(1);
+
+        // 批量统计 sql
+        List<Map<String, Object>> countList = this.baseMapper.selectMaps(
+                new QueryWrapper<PersonInOutInfo>()
+                        .select("dev_id, COUNT(*) AS cnt")
+                        .in("dev_id", devIds)
+                        .between("create_time", start, end)
+                        .groupBy("dev_id")
+        );
+
+        // 转成 Map<devId, count>
+        return countList.stream()
+                .collect(Collectors.toMap(
+                        m -> ((Number) m.get("dev_id")).longValue(),
+                        m -> ((Number) m.get("cnt")).intValue()
+                ));
+    }
+}