package com.gunshi.project.ss.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.gunshi.project.ss.common.mapper.StPptnRMapper; import com.gunshi.project.ss.common.model.StPptnR; import com.gunshi.project.ss.entity.so.RealRainBaseSo; import com.gunshi.project.ss.entity.vo.RealRainListVo; import com.gunshi.project.ss.entity.vo.RealRainStatListVo; import com.gunshi.project.ss.mapper.RealRainMapper; import com.gunshi.project.ss.model.AttBasBase; import com.gunshi.project.ss.model.AttBasBaseAutoDao; import com.gunshi.project.ss.util.DateUtil; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.compress.utils.Lists; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; /** * Description: * Created by wanyan on 2024/7/8 * * @author wanyan * @version 1.0 */ @Service @Data @Slf4j public class RealRainService { @Autowired private RealRainMapper realRainMapper; private final AttBasBaseAutoDao attBasBaseAutoDao; @Autowired private StPptnRMapper stPptnRMapper; /** * 实时雨情-降雨信息-查询接口 * * @param realRainBaseSo * @return */ public List getRealRainList(RealRainBaseSo realRainBaseSo) { List result = realRainMapper.getRealRainList(realRainBaseSo.getStm(), realRainBaseSo.getEtm()); for (RealRainListVo realRainListVo : result) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(StPptnR::getStcd,realRainListVo.getStcd()) .orderByDesc(StPptnR::getTm) .last("limit 1"); StPptnR stPptnR = stPptnRMapper.selectOne(queryWrapper); if(stPptnR == null){ realRainListVo.setStatus(0); }else{ Date tm = stPptnR.getTm(); if(DateUtil.hoursBetweenDate(tm,new Date()) > 24){ realRainListVo.setStatus(0); }else{ realRainListVo.setStatus(1); } } } // result.sort(Comparator.comparing(RealRainListVo::getDrp, Comparator.nullsFirst(Double::compareTo)).reversed().thenComparing(RealRainListVo::getStcd)); return result; } /** * 频率统计 */ public Map> getRealRainStatLevel(RealRainBaseSo realRainBaseSo) { Map> map = new HashMap<>(); for (int i = 1; i <= 5; i++) { if (map.get(i) == null) { map.put(i, Lists.newArrayList()); } } if (realRainBaseSo.getSource().size() == 0) { return map; } List result = getRealRainStat(realRainBaseSo); //判断result是否为空 if (CollectionUtils.isEmpty(result)) { return map; } //按照降雨量级进行分组 if (realRainBaseSo.getTimeType() == null) { result = result.stream().filter(res -> res.getFreqH1() != 0).collect(Collectors.toList()); map = result.stream().collect( Collectors.groupingBy(RealRainStatListVo::getFreqH1, Collectors.toList()) ); } if (realRainBaseSo.getTimeType() == 3) { result = result.stream().filter(res -> res.getFreqH3() != 0).collect(Collectors.toList()); map = result.stream().collect( Collectors.groupingBy(RealRainStatListVo::getFreqH3, Collectors.toList()) ); } else if (realRainBaseSo.getTimeType() == 6) { result = result.stream().filter(res -> res.getFreqH6() != 0).collect(Collectors.toList()); map = result.stream().collect( Collectors.groupingBy(RealRainStatListVo::getFreqH6, Collectors.toList()) ); } else if (realRainBaseSo.getTimeType() == 12) { result = result.stream().filter(res -> res.getFreqH12() != 0).collect(Collectors.toList()); map = result.stream().collect( Collectors.groupingBy(RealRainStatListVo::getFreqH12, Collectors.toList()) ); } else if (realRainBaseSo.getTimeType() == 24) { result = result.stream().filter(res -> res.getFreqH24() != 0).collect(Collectors.toList()); map = result.stream().collect( Collectors.groupingBy(RealRainStatListVo::getFreqH24, Collectors.toList()) ); } else { result = result.stream().filter(res -> res.getFreqH1() != 0).collect(Collectors.toList()); map = result.stream().collect( Collectors.groupingBy(RealRainStatListVo::getFreqH1, Collectors.toList()) ); } for (int i = 1; i <= 5; i++) { if (!map.containsKey(i)) { map.put(i, Lists.newArrayList()); } } for (int i = 1; i <= 5; i++) { if (map.get(i) == null) { map.put(i, Lists.newArrayList()); } } return map; } /** * 频率分析 * * @param realRainBaseSo */ public List getRealRainStat(RealRainBaseSo realRainBaseSo) { List result = Lists.newArrayList(); if (realRainBaseSo.getSource().size() == 0) { return result; } checkParam(realRainBaseSo); List source = realRainBaseSo.getSource(); ExecutorService executorService = Executors.newFixedThreadPool(4); for (String sourceStr : source) { executorService.execute(() -> { List realRainList = realRainMapper.getRealRainStatList( realRainBaseSo.getAdcd(), realRainBaseSo.getBasCode(), sourceStr, realRainBaseSo.getStm(), realRainBaseSo.getEtm() ); result.addAll(realRainList); }); } executorService.shutdown(); try { executorService.awaitTermination(2, TimeUnit.MINUTES); } catch (InterruptedException e) { Thread.currentThread().interrupt(); log.error(e.getMessage(), e); } return result; } /** * 流域选择列表 * * @param */ public List queryBasNameList() { return attBasBaseAutoDao.list(); } private void checkParam(RealRainBaseSo realRainBaseSo) { if (Objects.nonNull(realRainBaseSo.getQueryType()) && realRainBaseSo.getQueryType() == 2 && realRainBaseSo.getTimeType() == null) { throw new IllegalArgumentException("频率统计中时间段范围类型不能为空"); } //检查source是否为空 if (realRainBaseSo.getSource() == null || realRainBaseSo.getSource().isEmpty()) { throw new IllegalArgumentException("来源不能为空"); } //检查source字段是否合法 for (String source : realRainBaseSo.getSource()) { if (!"SH".equals(source) && !"SW".equals(source) && !"SK".equals(source) && !"QX".equals(source)) { throw new IllegalArgumentException("来源只能是SH SW SK QX这4种"); } } } }