diff --git a/hzims-service-api/weather-api/src/main/java/com/hnac/hzims/hzimsweather/feign/IRainfallClient.java b/hzims-service-api/weather-api/src/main/java/com/hnac/hzims/hzimsweather/feign/IRainfallClient.java new file mode 100644 index 0000000..1bd3fed --- /dev/null +++ b/hzims-service-api/weather-api/src/main/java/com/hnac/hzims/hzimsweather/feign/IRainfallClient.java @@ -0,0 +1,21 @@ +package com.hnac.hzims.hzimsweather.feign; + +import com.hnac.hzims.hzimsweather.constants.WeatherConstant; +import org.springblade.core.tool.api.R; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; + +import java.util.List; +import java.util.Map; + +@FeignClient(WeatherConstant.APP_NAME) +public interface IRainfallClient { + + String API_PREFIX = "/weather/rainfall"; + String GET_DURATION_RAIN_FALL = API_PREFIX + "/getDurationRainFall"; + + @GetMapping(GET_DURATION_RAIN_FALL) + R>> getDurationRainFall(@RequestParam String stationCode, @RequestParam String startDate, @RequestParam String endDate); + +} diff --git a/hzims-service/weather/src/main/java/com/hnac/hzims/weather/feign/RainfallClient.java b/hzims-service/weather/src/main/java/com/hnac/hzims/weather/feign/RainfallClient.java new file mode 100644 index 0000000..d4ccfa2 --- /dev/null +++ b/hzims-service/weather/src/main/java/com/hnac/hzims/weather/feign/RainfallClient.java @@ -0,0 +1,63 @@ +package com.hnac.hzims.weather.feign; + + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.google.common.collect.Lists; +import com.hnac.hzims.hzimsweather.entity.DailyHeWeatherEntity; +import com.hnac.hzims.hzimsweather.feign.IRainfallClient; +import com.hnac.hzims.weather.service.IDailyHeWeatherService; +import lombok.AllArgsConstructor; +import org.springblade.core.boot.ctrl.BladeController; +import org.springblade.core.log.exception.ServiceException; +import org.springblade.core.tool.api.R; +import org.springblade.core.tool.utils.DateUtil; +import org.springframework.util.Assert; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.time.Duration; +import java.time.LocalDate; +import java.util.*; +import java.util.stream.Collectors; + +@RestController +@AllArgsConstructor +public class RainfallClient extends BladeController implements IRainfallClient { + + private final IDailyHeWeatherService dailyHeWeatherService; + + @Override + @GetMapping(GET_DURATION_RAIN_FALL) + public R>> getDurationRainFall(@RequestParam String stationCode, @RequestParam String startDate, @RequestParam String endDate) { + LocalDate start = LocalDate.parse(startDate, DateUtil.DATE_FORMATTER); + LocalDate end = LocalDate.parse(endDate, DateUtil.DATE_FORMATTER); + Assert.isTrue(start.isBefore(end) || start.isEqual(end),()-> { + throw new ServiceException("降雨量查询传参错误,开始时间必须小于等于结束时间"); + }); + LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery().select(DailyHeWeatherEntity::getFxDate, DailyHeWeatherEntity::getPrecip) + .eq(DailyHeWeatherEntity::getStationCode, stationCode) + .ge(DailyHeWeatherEntity::getFxDate, startDate) + .le(DailyHeWeatherEntity::getFxDate, endDate); + List> precipList = dailyHeWeatherService.listMaps(queryWrapper); + List days = Lists.newArrayList(); + while(start.isBefore(end) || start.isEqual(end)) { + days.add(start); + start = start.plusDays(1); + } + List> result = days.stream().map(date -> { + String dateStr = DateUtil.format(date, DateUtil.PATTERN_DATE); + Optional> exit = precipList.stream().filter(m -> DateUtil.format((Date) m.get("fx_date"),DateUtil.PATTERN_DATE).equals(dateStr)).findFirst(); + if (exit.isPresent()) { + return exit.get(); + } else { + return new HashMap() {{ + put("fx_date", dateStr); + put("precip", null); + }}; + } + }).collect(Collectors.toList()); + return R.data(result); + } +}