|
@@ -13,24 +13,29 @@ package com.ruoyi.ems.task;
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
import com.alibaba.fastjson2.JSONArray;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.huashe.common.domain.model.DateAttr;
|
|
|
import com.huashe.common.domain.model.WeatherForecast;
|
|
|
import com.huashe.common.domain.model.WeatherRt;
|
|
|
import com.huashe.common.exception.Assert;
|
|
|
import com.huashe.common.utils.HttpUtils;
|
|
|
+import com.ruoyi.ems.config.DateAttrConfig;
|
|
|
import com.ruoyi.ems.config.WeatherConfig;
|
|
|
+import com.ruoyi.ems.service.IDateService;
|
|
|
import com.ruoyi.ems.service.IWeatherService;
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.apache.http.client.utils.URIBuilder;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.Year;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
@@ -47,20 +52,23 @@ import java.util.List;
|
|
|
public class TaskService {
|
|
|
private static final Logger log = LoggerFactory.getLogger(TaskService.class);
|
|
|
|
|
|
- @Value("${weather.adcode}")
|
|
|
- private String weatherAdcode;
|
|
|
-
|
|
|
@Resource
|
|
|
private WeatherConfig weatherConfig;
|
|
|
|
|
|
@Resource
|
|
|
+ private DateAttrConfig holidayConfig;
|
|
|
+
|
|
|
+ @Resource
|
|
|
private IWeatherService weatherService;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private IDateService dateService;
|
|
|
+
|
|
|
/**
|
|
|
* 天气实况采集
|
|
|
*/
|
|
|
@Async
|
|
|
- @Scheduled(cron = "${weather.rt.cron}")
|
|
|
+ @Scheduled(cron = "${general-data.weather.rt.cron}")
|
|
|
public void collectRt() {
|
|
|
log.debug("start collect weather rt.");
|
|
|
|
|
@@ -92,7 +100,7 @@ public class TaskService {
|
|
|
* 天气预报采集
|
|
|
*/
|
|
|
@Async
|
|
|
- @Scheduled(cron = "${weather.forecast.cron}")
|
|
|
+ @Scheduled(cron = "${general-data.weather.forecast.cron}")
|
|
|
public void collectForecast() {
|
|
|
log.debug("start collect weather forecast.");
|
|
|
|
|
@@ -124,4 +132,53 @@ public class TaskService {
|
|
|
log.error("collectForecast fail!", e);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 节假日采集
|
|
|
+ */
|
|
|
+ @Async
|
|
|
+ @Scheduled(cron = "${general-data.date.cron}")
|
|
|
+ public void collectDateAttr() {
|
|
|
+ log.debug("start collect date attr.");
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 获取当前年份
|
|
|
+ int currentYear = Year.now().getValue();
|
|
|
+
|
|
|
+ // 本年第一天
|
|
|
+ LocalDate firstDay = LocalDate.of(currentYear, 1, 1);
|
|
|
+
|
|
|
+ // 本年最后一天(12月31日)
|
|
|
+ LocalDate lastDay = LocalDate.of(currentYear, 12, 31);
|
|
|
+
|
|
|
+ // 定义日期格式
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
+
|
|
|
+ URIBuilder uriBuilder = new URIBuilder(holidayConfig.getApiAddr());
|
|
|
+ uriBuilder.addParameter("startDate", formatter.format(firstDay)).addParameter("endDate", formatter.format(lastDay))
|
|
|
+ .addParameter("apiKey", weatherConfig.getApiKey());
|
|
|
+ String res = HttpUtils.doGet(uriBuilder);
|
|
|
+ log.debug("get res:\r\n{}", res);
|
|
|
+
|
|
|
+ JSONObject jsonObject = JSON.parseObject(res);
|
|
|
+
|
|
|
+ String code = jsonObject.getString("code");
|
|
|
+ Assert.isTrue(StringUtils.equals(code, "0"), -1, jsonObject.getString("message"));
|
|
|
+
|
|
|
+ JSONArray data = jsonObject.getJSONArray("data");
|
|
|
+ Assert.notNull(data, -1, "date attr is null.");
|
|
|
+
|
|
|
+ if (!data.isEmpty()) {
|
|
|
+ List<DateAttr> dateAttrs = JSON.parseArray(data.toString(), DateAttr.class);
|
|
|
+
|
|
|
+ if (CollectionUtils.isNotEmpty(dateAttrs)) {
|
|
|
+ dateService.deleteByYear(String.valueOf(currentYear));
|
|
|
+ dateService.insertBatch(dateAttrs);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception e) {
|
|
|
+ log.error("collect date attr fail!", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|