“djh”
2025-11-11 63486c527b01c459110a88930d9cda1ded633aee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.gkhy.fourierSpecialGasMonitor.repository;
 
 
import com.gkhy.fourierSpecialGasMonitor.entity.GasCategory;
import com.gkhy.fourierSpecialGasMonitor.entity.GasConcentration;
import com.gkhy.fourierSpecialGasMonitor.entity.SummaryStats;
import com.gkhy.fourierSpecialGasMonitor.entity.req.SummaryStatsReqDTO;
import com.gkhy.fourierSpecialGasMonitor.entity.resp.SummaryStatsPageRespDTO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Repository;
 
import javax.persistence.OrderBy;
import java.time.LocalDateTime;
import java.util.List;
 
@Repository
public interface GasConcentrationRepository extends JpaRepository<GasConcentration,Long>, JpaSpecificationExecutor<GasConcentration> {
 
    @OrderBy("dataReceivingTime desc")
    GasConcentration findTopByOrderByDataReceivingTimeDesc();
 
    List<GasConcentration> findAllByDataReceivingTimeBetweenOrderByDataReceivingTimeDesc(LocalDateTime startTime, LocalDateTime endTime);
 
    //分别查询temp,humidity,windSpeed,windDirection,pressure的平均值,最小值,最大值
    @Query("SELECT new com.gkhy.fourierSpecialGasMonitor.entity.SummaryStats(MIN(g.temp), MAX(g.temp), FUNCTION('ROUND', AVG(g.temp), 2)," +
            "MIN(g.humidity), MAX(g.humidity), FUNCTION('ROUND', AVG(g.humidity), 2), " +
            "MIN(g.windSpeed), MAX(g.windSpeed), FUNCTION('ROUND', AVG(g.windSpeed), 2), " +
            "MIN(g.windDirection), MAX(g.windDirection), FUNCTION('ROUND', AVG(g.windDirection), 2), " +
            "MIN(g.pressure), MAX(g.pressure), FUNCTION('ROUND', AVG(g.pressure), 2) " +
            ") FROM GasConcentration g WHERE g.time BETWEEN :startTime AND :endTime")
    SummaryStats findStats(@Param("startTime") LocalDateTime startTime,@Param("endTime") LocalDateTime endTime);
 
 
    @Query("SELECT new com.gkhy.fourierSpecialGasMonitor.entity.resp.SummaryStatsPageRespDTO( " +
            "MAX(CASE WHEN :type = 'temp' THEN g.temp " +
            "WHEN :type = 'humidity' THEN g.humidity " +
            "WHEN :type = 'windSpeed' THEN g.windSpeed " +
            "WHEN :type = 'windDirection' THEN g.windDirection " +
            "WHEN :type = 'pressure' THEN g.pressure END), " +
            "MIN(CASE WHEN :type = 'temp' THEN g.temp " +
            "WHEN :type = 'humidity' THEN g.humidity " +
            "WHEN :type = 'windSpeed' THEN g.windSpeed " +
            "WHEN :type = 'windDirection' THEN g.windDirection " +
            "WHEN :type = 'pressure' THEN g.pressure END), " +
            "FUNCTION('ROUND', AVG(CASE WHEN :type = 'temp' THEN g.temp " +
            "WHEN :type = 'humidity' THEN g.humidity " +
            "WHEN :type = 'windSpeed' THEN g.windSpeed " +
            "WHEN :type = 'windDirection' THEN g.windDirection " +
            "WHEN :type = 'pressure' THEN g.pressure END), 2) " +
            ") FROM GasConcentration g " +
            "WHERE g.time BETWEEN :startTime AND :endTime")
    SummaryStatsPageRespDTO findSummaryStatsByTypeAndTimeRange(@Param("type")String type,@Param("startTime") LocalDateTime startTime,@Param("endTime") LocalDateTime endTime);
}