| | |
| | | package com.gkhy.fourierSpecialGasMonitor.service.impl; |
| | | |
| | | import com.gkhy.fourierSpecialGasMonitor.commons.domain.Result; |
| | | 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.*; |
| | | 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.req.GasConcentrationExportBO; |
| | | import com.gkhy.fourierSpecialGasMonitor.entity.req.SummaryStatsReqDTO; |
| | | import com.gkhy.fourierSpecialGasMonitor.entity.resp.FindGasWarnUserPageRespDTO; |
| | | import com.gkhy.fourierSpecialGasMonitor.entity.resp.SummaryStatsPageRespDTO; |
| | | import com.gkhy.fourierSpecialGasMonitor.enums.DeleteStatusEnum; |
| | | import com.gkhy.fourierSpecialGasMonitor.repository.GasConcentrationRepository; |
| | | import com.gkhy.fourierSpecialGasMonitor.service.GasConcentrationService; |
| | | import com.gkhy.fourierSpecialGasMonitor.utils.SummaryUtils; |
| | | import io.micrometer.core.instrument.util.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.domain.Page; |
| | |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.persistence.criteria.*; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.Year; |
| | | import java.time.YearMonth; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.ArrayList; |
| | | import java.util.HashSet; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | |
| | | } |
| | | |
| | | @Override |
| | | public Page<GasConcentration> listDatabyTimeSlotAndPage(PageQuery<GasPageQuery> pageQuery) { |
| | | 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())); |
| | | } |
| | |
| | | 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; |
| | | } |
| | | |
| | | @Override |
| | | public List<GasConcentration> gasConcentrationExport(GasConcentrationExportBO gasConcentrationExportBO) { |
| | | |
| | | if(gasConcentrationExportBO.getPosition1() == null && gasConcentrationExportBO.getPosition2() == null |
| | | && gasConcentrationExportBO.getPosition3() == null){ |
| | | gasConcentrationExportBO.setPosition1(1); |
| | | gasConcentrationExportBO.setPosition2(2); |
| | | gasConcentrationExportBO.setPosition3(3); |
| | | } |
| | | |
| | | //封装查询参数 |
| | | Specification<GasConcentration> specification = new Specification<GasConcentration>() { |
| | | @Override |
| | | public Predicate toPredicate(Root<GasConcentration> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) { |
| | | List<Predicate> predicateList = new ArrayList<>(); |
| | | if (gasConcentrationExportBO.getStartTime() != null && !gasConcentrationExportBO.getStartTime().equals("")){ |
| | | predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("time"), gasConcentrationExportBO.getStartTime())); |
| | | } |
| | | if (gasConcentrationExportBO.getEndTime() != null && !gasConcentrationExportBO.getEndTime().equals("")){ |
| | | predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("time"), gasConcentrationExportBO.getEndTime())); |
| | | } |
| | | predicateList.add(criteriaBuilder.or( |
| | | criteriaBuilder.equal(root.get("position"), gasConcentrationExportBO.getPosition1()), |
| | | criteriaBuilder.equal(root.get("position"), gasConcentrationExportBO.getPosition2()), |
| | | criteriaBuilder.equal(root.get("position"), gasConcentrationExportBO.getPosition3()) |
| | | ) |
| | | ); |
| | | query.orderBy(criteriaBuilder.asc(root.get("time")),criteriaBuilder.asc(root.get("position"))); |
| | | return criteriaBuilder.and(predicateList.toArray(new Predicate[0])); |
| | | } |
| | | }; |
| | | List<GasConcentration> result = gasConcentrationRepository.findAll(specification); |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | public SummaryStats findStats(LocalDateTime startTime,LocalDateTime endTime) { |
| | | return gasConcentrationRepository.findStats(startTime,endTime); |
| | | } |
| | | |
| | | @Override |
| | | public Result gasAtmosphereExtremum(SummaryStatsReqDTO reqDTO) { |
| | | Result success = Result.success(); |
| | | |
| | | // 1. 参数校验 |
| | | SummaryUtils summaryUtils = new SummaryUtils(); |
| | | summaryUtils.validateParams(reqDTO); |
| | | |
| | | String type = reqDTO.getType(); // 气体类型(如 "temp"、"humidity") |
| | | Integer dateType = reqDTO.getDateType(); // 时间粒度(1-日、2-月、3-年) |
| | | String date = reqDTO.getDate(); // 日期(如 "2025-11-10"、"2025-11"、"2025") |
| | | |
| | | // 2. 根据 dateType 解析时间范围 |
| | | LocalDateTime startTime = null; |
| | | LocalDateTime endTime = null; |
| | | String formatDate = null; // 格式化后的日期(用于返回) |
| | | |
| | | switch (dateType) { |
| | | case 1: // 日查询(date格式:yyyy-MM-dd) |
| | | LocalDate day = LocalDate.parse(date); |
| | | startTime = day.atStartOfDay(); // 当天00:00:00 |
| | | endTime = day.atTime(23, 59, 59); // 当天23:59:59 |
| | | formatDate = day.format(DateTimeFormatter.ISO_LOCAL_DATE); |
| | | break; |
| | | case 2: // 月查询(date格式:yyyy-MM) |
| | | YearMonth month = YearMonth.parse(date); |
| | | startTime = month.atDay(1).atStartOfDay(); // 当月1日00:00:00 |
| | | endTime = month.atEndOfMonth().atTime(23, 59, 59); // 当月最后一天23:59:59 |
| | | formatDate = month.format(DateTimeFormatter.ofPattern("yyyy-MM")); |
| | | break; |
| | | case 3: // 年查询(date格式:yyyy) |
| | | Year year = Year.parse(date); |
| | | startTime = year.atDay(1).atStartOfDay(); // 当年1月1日00:00:00 |
| | | endTime = year.atMonth(12).atEndOfMonth().atTime(23, 59, 59); // 当年12月最后一天23:59:59 |
| | | formatDate = year.toString(); |
| | | break; |
| | | default: |
| | | throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_ILLEGAL.getCode(),"无效的dateType,必须为1(日)、2(月)、3(年)"); |
| | | } |
| | | |
| | | // 3. 动态查询指定类型和时间范围的统计值 |
| | | SummaryStatsPageRespDTO summaryStatsByTypeAndTimeRange = gasConcentrationRepository.findSummaryStatsByTypeAndTimeRange(type, startTime, endTime); |
| | | |
| | | success.setData(summaryStatsByTypeAndTimeRange); |
| | | |
| | | // 5. 返回结果 |
| | | return success; |
| | | } |
| | | } |