huangzhen
2023-12-28 d6ae4bd543971312b51ba0e40b2565f63ed682e0
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.gkhy.fourierSpecialGasMonitor.service.impl;
 
import com.gkhy.fourierSpecialGasMonitor.commons.domain.SearchResult;
import com.gkhy.fourierSpecialGasMonitor.commons.enums.ResultCode;
import com.gkhy.fourierSpecialGasMonitor.commons.exception.BusinessException;
import com.gkhy.fourierSpecialGasMonitor.commons.model.PageQuery;
import com.gkhy.fourierSpecialGasMonitor.entity.GasConcentration;
import com.gkhy.fourierSpecialGasMonitor.entity.GasWarnLog;
import com.gkhy.fourierSpecialGasMonitor.entity.GasWarnUser;
import com.gkhy.fourierSpecialGasMonitor.entity.query.FindGasWarnLogPageQuery;
import com.gkhy.fourierSpecialGasMonitor.entity.query.FindGasWarnUserPageQuery;
import com.gkhy.fourierSpecialGasMonitor.entity.query.GasAtmospherePageQuery;
import com.gkhy.fourierSpecialGasMonitor.entity.query.GasPageQuery;
import com.gkhy.fourierSpecialGasMonitor.entity.resp.FindGasWarnUserPageRespDTO;
import com.gkhy.fourierSpecialGasMonitor.enums.DeleteStatusEnum;
import com.gkhy.fourierSpecialGasMonitor.repository.GasConcentrationRepository;
import com.gkhy.fourierSpecialGasMonitor.service.GasConcentrationService;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
 
import javax.persistence.criteria.*;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
@Service
public class GasConcentrationServiceImpl implements GasConcentrationService {
 
    @Autowired
    private GasConcentrationRepository gasConcentrationRepository;
 
    @Override
    public GasConcentration save(GasConcentration gasConcentration) {
        return gasConcentrationRepository.save(gasConcentration);
    }
 
    @Override
    public GasConcentration getLastData() {
        GasConcentration gasConcentration = gasConcentrationRepository.findTopByOrderByDataReceivingTimeDesc();
        return gasConcentration;
    }
 
    @Override
    public List<GasConcentration> listDatabyTimeSlot(LocalDateTime startTime, LocalDateTime endTime) {
        if (startTime == null || endTime == null)
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"时间区段值不能为空");
        Specification<GasConcentration> specification = new Specification<GasConcentration>() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) {
                Set<Predicate> predicateList = new HashSet<>();
                predicateList.add(criteriaBuilder.between(root.get("time").as(LocalDateTime.class),startTime,endTime));
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        List<GasConcentration> gasConcentrations = gasConcentrationRepository.findAll(specification);
        return gasConcentrations;
    }
 
    @Override
    public Page<GasConcentration> listDatabyTimeSlotAndPositionAndPage(PageQuery<GasPageQuery> pageQuery) {
        Pageable pageable = PageRequest.of(pageQuery.getPageIndex()-1, pageQuery.getPageSize(), Sort.Direction.DESC, "time");
        Specification<GasConcentration> specification = new Specification<GasConcentration>() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) {
                Set<Predicate> predicateList = new HashSet<>();
                GasPageQuery searchParams = pageQuery.getSearchParams();
                if (searchParams != null && searchParams.getPosition() != null){
                    predicateList.add(criteriaBuilder.equal(root.get("position").as(Integer.class),searchParams.getPosition()));
                }
                if (searchParams != null && searchParams.getStartTime() != null && searchParams.getEndTime() != null){
                    predicateList.add(criteriaBuilder.between(root.get("time").as(LocalDateTime.class),searchParams.getStartTime(),searchParams.getEndTime()));
                }
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        Page<GasConcentration> pageResult = gasConcentrationRepository.findAll(specification,pageable);
        return pageResult;
    }
 
    @Override
    public Page<GasConcentration> gasAtmospherePage(PageQuery<GasAtmospherePageQuery> pageQuery) {
        Pageable pageable = PageRequest.of(pageQuery.getPageIndex()-1, pageQuery.getPageSize(), Sort.Direction.DESC, "time");
        Specification<GasConcentration> specification = new Specification<GasConcentration>() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) {
                Set<Predicate> predicateList = new HashSet<>();
                GasAtmospherePageQuery searchParams = pageQuery.getSearchParams();
                if (searchParams != null && searchParams.getStartTime() != null && searchParams.getEndTime() != null){
                    predicateList.add(criteriaBuilder.between(root.get("time").as(LocalDateTime.class),searchParams.getStartTime(),searchParams.getEndTime()));
                }
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        Page<GasConcentration> pageResult = gasConcentrationRepository.findAll(specification,pageable);
        return pageResult;
    }
 
    @Override
    public List<GasConcentration> listDatabyTimeSlotAndPosition(LocalDateTime startTime, LocalDateTime endTime, Integer position) {
        if (startTime == null || endTime == null)
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"时间区段值不能为空");
        Specification<GasConcentration> specification = new Specification<GasConcentration>() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) {
                Set<Predicate> predicateList = new HashSet<>();
                if (position != null){
                    predicateList.add(criteriaBuilder.equal(root.get("position").as(Integer.class),position));
                }
                predicateList.add(criteriaBuilder.between(root.get("time").as(LocalDateTime.class),startTime,endTime));
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        List<GasConcentration> gasConcentrations = gasConcentrationRepository.findAll(specification);
        return gasConcentrations;
    }
}