济菏高速数据中心代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
4.3 KiB

10 months ago
package com.zc.business.controller;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.system.service.ISysConfigService;
10 months ago
import com.zc.business.domain.DcHolidays;
import com.zc.business.enums.UniversalEnum;
10 months ago
import com.zc.business.service.IDcHolidaysService;
import org.springframework.beans.factory.annotation.Autowired;
9 months ago
import org.springframework.scheduling.annotation.Scheduled;
10 months ago
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.LocalDate;
import java.util.List;
/**
*
*/
@Component
public class DcTrafficFlowWarningDate {
@Autowired
private IDcHolidaysService dcHolidaysService;
@Autowired
private ISysConfigService configService;
10 months ago
9 months ago
@Scheduled(cron = "0 0 0 1 * ?")
10 months ago
public void yearPriorToAcquisition() {
try {
String apiKey= configService.selectConfigByKey("holidayKey");//密钥
10 months ago
// 获取当前日期
LocalDate currentDate = LocalDate.now();
// 获取前一年 年份
int currentYear = currentDate.getYear();
String urlString = UniversalEnum.GET_HOLIDAYS.getValue() + apiKey + "&date=" + currentYear + "&type=1";
10 months ago
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(UniversalEnum.GET.getValue());
10 months ago
connection.setRequestProperty("Accept", "application/json");
if (connection.getResponseCode() != UniversalEnum.TWO_HUNDRED.getNumber()) {
10 months ago
throw new RuntimeException("HTTP错误码:" + connection.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
String output;
StringBuilder response = new StringBuilder();
while ((output = br.readLine()) != null) {
response.append(output);
}
connection.disconnect();
JSONObject jsonResponse = JSONObject.parseObject(response.toString());
// 检查响应状态是否成功
if (jsonResponse.getInteger("code") == UniversalEnum.TWO_HUNDRED.getNumber()) {
10 months ago
// 获取result对象
JSONObject resultObj = jsonResponse.getJSONObject("result");
// 获取节假日列表JSONArray
JSONArray listArray = resultObj.getJSONArray("list");
DcHolidays dcHolidays = new DcHolidays();
dcHolidays.setDate(currentYear);
List<DcHolidays> dcHolidays1 = dcHolidaysService.selectDcHolidaysList(dcHolidays);
if (dcHolidays1.size() <=UniversalEnum.ZERO.getNumber()){
10 months ago
// 遍历节假日列表 添加数据
for (int i = UniversalEnum.ZERO.getNumber(); i < listArray.size(); i++) {
10 months ago
JSONObject holidayObj = listArray.getJSONObject(i);
String vacation = holidayObj.getString("vacation");
String name = holidayObj.getString("name");
dcHolidays.setName(name);
dcHolidays.setVacation(vacation);
// 年份
dcHolidays.setDate(currentYear);
int i1 = dcHolidaysService.insertDcHolidays(dcHolidays);
}
}else {
// 遍历节假日列表 修改数据
for (int i = UniversalEnum.ZERO.getNumber(); i < listArray.size(); i++) {
10 months ago
JSONObject holidayObj = listArray.getJSONObject(i);
String vacation = holidayObj.getString("vacation");
String name = holidayObj.getString("name");
DcHolidays dcHoliday = new DcHolidays();
dcHoliday.setName(name);
dcHoliday.setVacation(vacation);
// 年份
dcHoliday.setDate(currentYear);
dcHolidaysService.updateDcHoliday(dcHoliday);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}