haungxing
10 months ago
2 changed files with 84 additions and 0 deletions
@ -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<List<Map<String, Object>>> getDurationRainFall(@RequestParam String stationCode, @RequestParam String startDate, @RequestParam String endDate); |
||||||
|
|
||||||
|
} |
@ -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<List<Map<String, Object>>> 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<DailyHeWeatherEntity> queryWrapper = Wrappers.<DailyHeWeatherEntity>lambdaQuery().select(DailyHeWeatherEntity::getFxDate, DailyHeWeatherEntity::getPrecip) |
||||||
|
.eq(DailyHeWeatherEntity::getStationCode, stationCode) |
||||||
|
.ge(DailyHeWeatherEntity::getFxDate, startDate) |
||||||
|
.le(DailyHeWeatherEntity::getFxDate, endDate); |
||||||
|
List<Map<String, Object>> precipList = dailyHeWeatherService.listMaps(queryWrapper); |
||||||
|
List<LocalDate> days = Lists.newArrayList(); |
||||||
|
while(start.isBefore(end) || start.isEqual(end)) { |
||||||
|
days.add(start); |
||||||
|
start = start.plusDays(1); |
||||||
|
} |
||||||
|
List<Map<String, Object>> result = days.stream().map(date -> { |
||||||
|
String dateStr = DateUtil.format(date, DateUtil.PATTERN_DATE); |
||||||
|
Optional<Map<String, Object>> 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<String, Object>() {{ |
||||||
|
put("fx_date", dateStr); |
||||||
|
put("precip", null); |
||||||
|
}}; |
||||||
|
} |
||||||
|
}).collect(Collectors.toList()); |
||||||
|
return R.data(result); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue