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.
99 lines
4.0 KiB
99 lines
4.0 KiB
10 months ago
|
package com.zc.business.controller;
|
||
|
|
||
|
import com.alibaba.fastjson.JSONArray;
|
||
|
import com.alibaba.fastjson.JSONObject;
|
||
|
import com.zc.business.domain.DcHolidays;
|
||
|
import com.zc.business.service.IDcHolidaysService;
|
||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||
|
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;
|
||
|
|
||
|
//@Scheduled(cron = "0/10 * * * * ?")
|
||
|
public void yearPriorToAcquisition() {
|
||
|
|
||
|
try {
|
||
|
String apiKey = "00b26cbc2cb242283452ac5c842e81d1"; // 替换为你的API密钥
|
||
|
// 获取当前日期
|
||
|
LocalDate currentDate = LocalDate.now();
|
||
|
// 获取前一年 年份
|
||
|
int currentYear = currentDate.getYear();
|
||
|
String urlString = "https://apis.tianapi.com/jiejiari/index?key=" + apiKey + "&date=" + currentYear + "&type=1";
|
||
|
URL url = new URL(urlString);
|
||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||
|
|
||
|
connection.setRequestMethod("GET");
|
||
|
connection.setRequestProperty("Accept", "application/json");
|
||
|
|
||
|
if (connection.getResponseCode() != 200) {
|
||
|
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") == 200) {
|
||
|
// 获取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() <=0){
|
||
|
// 遍历节假日列表 添加数据
|
||
|
for (int i = 0; i < listArray.size(); i++) {
|
||
|
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 = 0; i < listArray.size(); i++) {
|
||
|
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();
|
||
|
}
|
||
|
}
|
||
|
}
|