Browse Source

大屏统计

chejianzheng 1 month ago
parent
commit
7958970417

+ 1 - 16
portal-service-common/src/main/java/com/hfln/portal/common/dto/data/stat/ScreenStatsDTO.java

@@ -27,7 +27,7 @@ public class ScreenStatsDTO {
     private Integer detectedCount;
     private Integer detectedCount;
 
 
     @Schema(description = "今日活跃度")
     @Schema(description = "今日活跃度")
-    private double activeRate;
+    private Double activeRate;
 
 
     @Schema(description = "年龄统计信息")
     @Schema(description = "年龄统计信息")
     private List<AgeInfo> ageList;
     private List<AgeInfo> ageList;
@@ -41,15 +41,6 @@ public class ScreenStatsDTO {
     @Data
     @Data
     public static class AgeInfo {
     public static class AgeInfo {
 
 
-//        @Schema(description = "50-60岁年龄统计")
-//        private Integer age50To60Count;
-//
-//        @Schema(description = "60-70岁年龄统计")
-//        private Integer age60To70Count;
-//
-//        @Schema(description = "70岁以上年龄统计")
-//        private Integer age70Count;
-
         @Schema(description = "年龄段")
         @Schema(description = "年龄段")
         private String ageRange;
         private String ageRange;
 
 
@@ -60,12 +51,6 @@ public class ScreenStatsDTO {
     @Data
     @Data
     public static class GuardInfo {
     public static class GuardInfo {
 
 
-//        @Schema(description = "一般监测对象")
-//        private Integer generalCount;
-//
-//        @Schema(description = "重点监测对象")
-//        private Integer importantCount;
-
         @Schema(description = "监测对象类型")
         @Schema(description = "监测对象类型")
         private String guardType;
         private String guardType;
 
 

+ 8 - 0
portal-service-common/src/main/java/com/hfln/portal/common/request/device/WebUpdateDeviceParams.java

@@ -86,4 +86,12 @@ public class WebUpdateDeviceParams extends BaseVO {
     @Schema(description = "设备跌倒确认时间,大于0")
     @Schema(description = "设备跌倒确认时间,大于0")
     @DecimalMin(value = "1", message = "跌倒确认时间必须大于等于1")
     @DecimalMin(value = "1", message = "跌倒确认时间必须大于等于1")
     private BigDecimal fallingConfirm;
     private BigDecimal fallingConfirm;
+
+    @Schema(description = "监护对象年龄")
+    @Min(value = 0, message = "年龄不能小于0")
+    @Max(value = 120, message = "年龄不能大于120")
+    private Integer age;
+
+    @Schema(description = "监护对象类型")
+    private String guardianshipType;
 }
 }

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

@@ -420,6 +420,7 @@ public class DeviceGatewayImpl implements DeviceGateway {
             throw new BizException(ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorCode(), ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorMessage());
             throw new BizException(ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorCode(), ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorMessage());
         }
         }
 
 
+        logService.saveLog(OprLogType.UPDATE_DEVICE.getCode(), devInfo);
 
 
         //2.更新数据
         //2.更新数据
         devInfo.setTenantId(params.getTenantId());
         devInfo.setTenantId(params.getTenantId());
@@ -438,6 +439,8 @@ public class DeviceGatewayImpl implements DeviceGateway {
         devInfo.setZzEnd(params.getZzEnd());
         devInfo.setZzEnd(params.getZzEnd());
         devInfo.setLength(params.getXxEnd().subtract(params.getXxStart()));
         devInfo.setLength(params.getXxEnd().subtract(params.getXxStart()));
         devInfo.setWidth(params.getYyEnd().subtract(params.getYyStart()));
         devInfo.setWidth(params.getYyEnd().subtract(params.getYyStart()));
+        devInfo.setAge(params.getAge());
+        devInfo.setGuardianshipType(params.getGuardianshipType());
 
 
         if (params.getFallingConfirm() != null) {
         if (params.getFallingConfirm() != null) {
             devInfo.setFallingConfirm(params.getFallingConfirm());
             devInfo.setFallingConfirm(params.getFallingConfirm());
@@ -453,8 +456,6 @@ public class DeviceGatewayImpl implements DeviceGateway {
         //4.通过MQTT把信息参数发送到设备
         //4.通过MQTT把信息参数发送到设备
         mqttSend.sendWebDeviceParamsToMqtt(devInfo.getClientId(), params);
         mqttSend.sendWebDeviceParamsToMqtt(devInfo.getClientId(), params);
 
 
-        logService.saveLog(OprLogType.UPDATE_DEVICE.getCode(), devInfo);
-
         return updated;
         return updated;
     }
     }
 
 

+ 71 - 23
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/gateway/impl/WebStatsGatewayImpl.java

@@ -27,7 +27,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
-import java.time.LocalDate;
+import java.math.BigDecimal;
 import java.time.LocalDateTime;
 import java.time.LocalDateTime;
 import java.time.temporal.ChronoUnit;
 import java.time.temporal.ChronoUnit;
 import java.util.*;
 import java.util.*;
@@ -208,13 +208,9 @@ public class WebStatsGatewayImpl implements WebStatsGateway {
         TblTenant tenant = tblTenantService.getById(params.getTenantId());
         TblTenant tenant = tblTenantService.getById(params.getTenantId());
         res.setSystemGuardDay((int) ChronoUnit.DAYS.between(tenant.getCreateTime(), LocalDateTime.now()));
         res.setSystemGuardDay((int) ChronoUnit.DAYS.between(tenant.getCreateTime(), LocalDateTime.now()));
 
 
-        res.setDeviceCount(Math.toIntExact(devInfoService.lambdaQuery()
-                .eq(DevInfo::getTenantId, params.getTenantId())
-                .count()));
-        res.setOnlineCount(Math.toIntExact(devInfoService.lambdaQuery()
-                .eq(DevInfo::getTenantId, params.getTenantId())
-                .eq(DevInfo::getOnline, DevInfo.Constants.OnlineStatus.ONLINE)
-                .count()));
+        Map<String, BigDecimal> statInfo = devInfoService.queryStatInfo(params.getTenantId());
+        res.setDeviceCount(statInfo.get("deviceCount") == null ? 0 :statInfo.get("deviceCount").intValue());
+        res.setOnlineCount(statInfo.get("onlineCount") == null ? 0 :statInfo.get("onlineCount").intValue());
 
 
         if (res.getOnlineCount() != 0) {
         if (res.getOnlineCount() != 0) {
 
 
@@ -222,7 +218,6 @@ public class WebStatsGatewayImpl implements WebStatsGateway {
             res.setActiveRate((double) activeCount / res.getOnlineCount());
             res.setActiveRate((double) activeCount / res.getOnlineCount());
         }
         }
 
 
-
         res.setFallingCount(Math.toIntExact(eventListService.lambdaQuery()
         res.setFallingCount(Math.toIntExact(eventListService.lambdaQuery()
                 .eq(EventList::getTenantId, params.getTenantId())
                 .eq(EventList::getTenantId, params.getTenantId())
                 .count()));
                 .count()));
@@ -231,20 +226,73 @@ public class WebStatsGatewayImpl implements WebStatsGateway {
                 .count()));
                 .count()));
 
 
 
 
-        res.setDetectedCount(Math.toIntExact(devInfoService.lambdaQuery()
-                .eq(DevInfo::getTenantId, params.getTenantId())
-                        .ge(DevInfo::getOnoffTime, LocalDate.now())
-                .count()));
-        //     @Schema(description = "年龄统计信息")
-        //    private AgeInfo ageInfo;
-        //
-        //    @Schema(description = "守护统计信息")
-        //    private GuardInfo guardInfo;
-        //
-        //    @Schema(description = "安装位置统计信息")
-        //    private InstallPositionInfo installPositionInfo;
-
-        res.setInstallPositionList(devInfoService.queryInstallCountByTenantId(params.getTenantId()));
+        res.setDetectedCount(statInfo.get("detectedCount") == null ? 0 :statInfo.get("detectedCount").intValue());
+
+        List<ScreenStatsDTO.InstallPositionInfo> installPositionList = new ArrayList<>();
+        ScreenStatsDTO.InstallPositionInfo toiletCount = new ScreenStatsDTO.InstallPositionInfo();
+        toiletCount.setInstallPosition("Toilet");
+        toiletCount.setCount(statInfo.get("toiletCount") == null ? 0 :statInfo.get("toiletCount").intValue());
+        installPositionList.add(toiletCount);
+
+        ScreenStatsDTO.InstallPositionInfo bedroomCount = new ScreenStatsDTO.InstallPositionInfo();
+        bedroomCount.setInstallPosition("Bedroom");
+        bedroomCount.setCount(statInfo.get("bedroomCount") == null ? 0 :statInfo.get("bedroomCount").intValue());
+        installPositionList.add(bedroomCount);
+
+        ScreenStatsDTO.InstallPositionInfo livingRoomCount = new ScreenStatsDTO.InstallPositionInfo();
+        livingRoomCount.setInstallPosition("LivingRoom");
+        livingRoomCount.setCount(statInfo.get("toiletCount") == null ? 0 :statInfo.get("toiletCount").intValue());
+        installPositionList.add(livingRoomCount);
+
+        ScreenStatsDTO.InstallPositionInfo restaurantCount = new ScreenStatsDTO.InstallPositionInfo();
+        restaurantCount.setInstallPosition("Restaurant");
+        restaurantCount.setCount(statInfo.get("restaurantCount") == null ? 0 :statInfo.get("restaurantCount").intValue());
+        installPositionList.add(restaurantCount);
+        res.setInstallPositionList(installPositionList);
+
+        List<ScreenStatsDTO.AgeInfo> ageList = new ArrayList<>();
+        ScreenStatsDTO.AgeInfo less60 = new ScreenStatsDTO.AgeInfo();
+        less60.setAgeRange("0-59");
+        less60.setCount(statInfo.get("less60") == null ? 0 :statInfo.get("less60").intValue());
+        ageList.add(less60);
+
+        ScreenStatsDTO.AgeInfo less70 = new ScreenStatsDTO.AgeInfo();
+        less70.setAgeRange("60-69");
+        less70.setCount(statInfo.get("less70") == null ? 0 :statInfo.get("less70").intValue());
+        ageList.add(less70);
+
+        ScreenStatsDTO.AgeInfo less80 = new ScreenStatsDTO.AgeInfo();
+        less80.setAgeRange("70-79");
+        less80.setCount(statInfo.get("less80") == null ? 0 :statInfo.get("less80").intValue());
+        ageList.add(less80);
+
+        ScreenStatsDTO.AgeInfo less90 = new ScreenStatsDTO.AgeInfo();
+        less90.setAgeRange("80-89");
+        less90.setCount(statInfo.get("less90") == null ? 0 :statInfo.get("less90").intValue());
+        ageList.add(less90);
+
+        ScreenStatsDTO.AgeInfo less100 = new ScreenStatsDTO.AgeInfo();
+        less100.setAgeRange("90-99");
+        less100.setCount(statInfo.get("less100") == null ? 0 :statInfo.get("less100").intValue());
+        ageList.add(less100);
+
+        ScreenStatsDTO.AgeInfo ge100 = new ScreenStatsDTO.AgeInfo();
+        ge100.setAgeRange("100+");
+        ge100.setCount(statInfo.get("ge100") == null ? 0 :statInfo.get("ge100").intValue());
+        ageList.add(ge100);
+        res.setAgeList(ageList);
+
+        List<ScreenStatsDTO.GuardInfo> guardList = new ArrayList<>();
+        ScreenStatsDTO.GuardInfo generalCount = new ScreenStatsDTO.GuardInfo();
+        generalCount.setGuardType("general");
+        generalCount.setCount(statInfo.get("generalCount") == null ? 0 :statInfo.get("generalCount").intValue());
+        guardList.add(generalCount);
+
+        ScreenStatsDTO.GuardInfo importantCount = new ScreenStatsDTO.GuardInfo();
+        importantCount.setGuardType("important");
+        importantCount.setCount(statInfo.get("importantCount") == null ? 0 :statInfo.get("importantCount").intValue());
+        guardList.add(importantCount);
+        res.setGuardList(guardList);
 
 
         return res;
         return res;
     }
     }

+ 25 - 10
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/mapper/DevInfoMapper.java

@@ -1,13 +1,13 @@
 package com.hfln.portal.infrastructure.mapper;
 package com.hfln.portal.infrastructure.mapper;
 
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
-import com.hfln.portal.common.dto.data.stat.ScreenStatsDTO;
 import com.hfln.portal.infrastructure.po.DevInfo;
 import com.hfln.portal.infrastructure.po.DevInfo;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Select;
 import org.apache.ibatis.annotations.Select;
 import org.apache.ibatis.annotations.Update;
 import org.apache.ibatis.annotations.Update;
 
 
-import java.util.List;
+import java.math.BigDecimal;
+import java.util.Map;
 
 
 /**
 /**
  * 设备信息表 Mapper 接口
  * 设备信息表 Mapper 接口
@@ -47,12 +47,27 @@ public interface DevInfoMapper extends BaseMapper<DevInfo> {
             "WHERE dev_id = #{devId}")
             "WHERE dev_id = #{devId}")
     void reset(Long devId);
     void reset(Long devId);
 
 
-    @Select("SELECT " +
-            "   t.install_position AS installPosition, " +
-            "   COUNT(*) AS count " +
-            "FROM dev_info t " +
-            "WHERE t.tenant_id = #{tenantId} and t.is_deleted = 0 " +
-            "GROUP BY t.install_position " +
-            "ORDER BY installPosition ASC ")
-    List<ScreenStatsDTO.InstallPositionInfo> queryInstallCountByTenantId(Long tenantId);
+    @Select({
+            "<script>",
+            "SELECT ",
+            "   SUM(CASE WHEN age &lt; 60 THEN 1 ELSE 0 END) AS 'less60',",
+            "   SUM(CASE WHEN age &gt;= 60 AND age &lt; 70 THEN 1 ELSE 0 END) AS 'less70',",
+            "   SUM(CASE WHEN age &gt;= 70 AND age &lt; 80 THEN 1 ELSE 0 END) AS 'less80',",
+            "   SUM(CASE WHEN age &gt;= 80 AND age &lt; 90 THEN 1 ELSE 0 END) AS 'less90',",
+            "   SUM(CASE WHEN age &gt;= 90 AND age &lt; 100 THEN 1 ELSE 0 END) AS 'less100',",
+            "   SUM(CASE WHEN age &gt;= 100 THEN 1 ELSE 0 END) AS 'ge100', ",
+            "   sum(1) as 'deviceCount', ",
+            "   sum(case when online = 1 then 1 else 0 end) as 'onlineCount', ",
+            "   sum(case when onoff_time &gt;= #{today} then 1 else 0 end) as 'detectedCount', ",
+            "   sum(case when install_position = 'Toilet' then 1 else 0 end) as 'toiletCount', ",
+            "   sum(case when install_position = 'Bedroom' then 1 else 0 end) as 'bedroomCount', ",
+            "   sum(case when install_position = 'LivingRoom' then 1 else 0 end) as 'livingRoomCount', ",
+            "   sum(case when install_position = 'Restaurant' then 1 else 0 end) as 'restaurantCount', ",
+            "   sum(case when guardianship_type = 'general' then 1 else 0 end) as 'generalCount', ",
+            "   sum(case when guardianship_type = 'important' then 1 else 0 end) as 'importantCount' ",
+            "FROM dev_info ",
+            "WHERE tenant_id = #{tenantId} AND is_deleted = 0",
+            "</script>"
+    })
+    Map<String, BigDecimal> queryStatInfo(Long tenantId);
 }
 }

+ 12 - 0
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/po/DevInfo.java

@@ -211,6 +211,18 @@ public class DevInfo extends BasePO {
     private LocalDateTime presenceChangeTime;
     private LocalDateTime presenceChangeTime;
 
 
     /**
     /**
+     * 监护对象年龄
+     */
+    @TableField("age")
+    private Integer age;
+
+    /**
+     * 监护对象类型
+     */
+    @TableField("guardianship_type")
+    private String guardianshipType;
+
+    /**
      * 设备信息常量类
      * 设备信息常量类
      */
      */
     public static class Constants {
     public static class Constants {

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

@@ -2,20 +2,20 @@ package com.hfln.portal.infrastructure.service;
 
 
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.baomidou.mybatisplus.extension.service.IService;
-import com.hfln.portal.common.dto.data.stat.ScreenStatsDTO;
 import com.hfln.portal.common.request.device.DeviceBandingParams;
 import com.hfln.portal.common.request.device.DeviceBandingParams;
 import com.hfln.portal.common.request.device.DeviceListQueryReq;
 import com.hfln.portal.common.request.device.DeviceListQueryReq;
 import com.hfln.portal.infrastructure.po.DevInfo;
 import com.hfln.portal.infrastructure.po.DevInfo;
 
 
+import java.math.BigDecimal;
 import java.util.Collection;
 import java.util.Collection;
 import java.util.List;
 import java.util.List;
+import java.util.Map;
 
 
 public interface DevInfoService extends IService<DevInfo> {
 public interface DevInfoService extends IService<DevInfo> {
 
 
     List<DevInfo> queryDeviceList(Long userId, String keyWord,Integer status,List<Long> sharedDevIds);
     List<DevInfo> queryDeviceList(Long userId, String keyWord,Integer status,List<Long> sharedDevIds);
 
 
 
 
-
     DevInfo queryDevices(Long userId,String deviceId);
     DevInfo queryDevices(Long userId,String deviceId);
 
 
     List<DevInfo> queryByIds (Collection<String> clientIds);
     List<DevInfo> queryByIds (Collection<String> clientIds);
@@ -38,5 +38,6 @@ public interface DevInfoService extends IService<DevInfo> {
 
 
     void resetDevInfo(Long devId);
     void resetDevInfo(Long devId);
 
 
-    List<ScreenStatsDTO.InstallPositionInfo> queryInstallCountByTenantId(Long tenantId);
+
+    Map<String, BigDecimal> queryStatInfo(Long tenantId);
 }
 }

+ 5 - 7
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.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.hfln.portal.common.dto.data.stat.ScreenStatsDTO;
 import com.hfln.portal.common.request.device.DeviceBandingParams;
 import com.hfln.portal.common.request.device.DeviceBandingParams;
 import com.hfln.portal.common.request.device.DeviceListQueryReq;
 import com.hfln.portal.common.request.device.DeviceListQueryReq;
 import com.hfln.portal.domain.customer.util.UpdateWrapperBuilder;
 import com.hfln.portal.domain.customer.util.UpdateWrapperBuilder;
@@ -20,11 +19,9 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 import org.springframework.util.StringUtils;
 import org.springframework.util.StringUtils;
 
 
+import java.math.BigDecimal;
 import java.time.LocalDateTime;
 import java.time.LocalDateTime;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.Objects;
+import java.util.*;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
 
 
 @Service
 @Service
@@ -272,7 +269,8 @@ public class DevInfoServiceImpl extends ServiceImpl<DevInfoMapper, DevInfo> impl
     }
     }
 
 
     @Override
     @Override
-    public List<ScreenStatsDTO.InstallPositionInfo> queryInstallCountByTenantId(Long tenantId) {
-        return this.baseMapper.queryInstallCountByTenantId(tenantId);
+    public Map<String, BigDecimal> queryStatInfo(Long tenantId) {
+
+        return this.baseMapper.queryStatInfo(tenantId);
     }
     }
 }
 }