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;
|
}
|
}
|