|
|
|
package com.zc.business.controller;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.ruoyi.system.service.ISysConfigService;
|
|
|
|
import com.zc.business.domain.DcHolidays;
|
|
|
|
import com.zc.business.enums.UniversalEnum;
|
|
|
|
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;
|
|
|
|
@Autowired
|
|
|
|
private ISysConfigService configService;
|
|
|
|
|
|
|
|
@Scheduled(cron = "0 0 0 1 * ?")
|
|
|
|
public void yearPriorToAcquisition() {
|
|
|
|
|
|
|
|
try {
|
|
|
|
String apiKey= configService.selectConfigByKey("holidayKey");//密钥
|
|
|
|
// 获取当前日期
|
|
|
|
LocalDate currentDate = LocalDate.now();
|
|
|
|
// 获取前一年 年份
|
|
|
|
int currentYear = currentDate.getYear();
|
|
|
|
String urlString = UniversalEnum.GET_HOLIDAYS.getValue() + apiKey + "&date=" + currentYear + "&type=1";
|
|
|
|
URL url = new URL(urlString);
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
|
|
|
|
|
connection.setRequestMethod(UniversalEnum.GET.getValue());
|
|
|
|
connection.setRequestProperty("Accept", "application/json");
|
|
|
|
|
|
|
|
if (connection.getResponseCode() != UniversalEnum.TWO_HUNDRED.getNumber()) {
|
|
|
|
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()) {
|
|
|
|
// 获取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()){
|
|
|
|
// 遍历节假日列表 添加数据
|
|
|
|
for (int i = UniversalEnum.ZERO.getNumber(); 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 = UniversalEnum.ZERO.getNumber(); 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|