HoliDayUtils.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package cn.cslg.pas.common.utils.commonUtils;
  2. import cn.cslg.pas.common.utils.RedisUtil;
  3. import cn.cslg.pas.domain.report.OralNotice;
  4. import cn.cslg.pas.exception.XiaoShiException;
  5. import com.alibaba.fastjson2.JSON;
  6. import com.alibaba.fastjson2.JSONObject;
  7. import okhttp3.OkHttpClient;
  8. import okhttp3.Request;
  9. import okhttp3.Response;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Component;
  12. import java.io.BufferedReader;
  13. import java.io.IOException;
  14. import java.io.InputStreamReader;
  15. import java.net.URL;
  16. import java.net.URLConnection;
  17. import java.text.ParseException;
  18. import java.text.SimpleDateFormat;
  19. import java.util.*;
  20. import java.util.concurrent.TimeUnit;
  21. @Component
  22. public class HoliDayUtils {
  23. @Autowired
  24. private RedisUtil redisUtil;
  25. public String isWorkingDay(Date parse) throws ParseException {
  26. SimpleDateFormat getYearFormat = new SimpleDateFormat("yyyy-MM-dd");
  27. String newDate = new SimpleDateFormat("yyyy").format(parse);
  28. String time = getYearFormat.format(parse);
  29. Date date = new SimpleDateFormat("yyyy-MM-dd").parse(time);
  30. Calendar calendar = Calendar.getInstance();
  31. calendar.setTime(date);
  32. int weekday = calendar.get(Calendar.DAY_OF_WEEK);
  33. List<JSONObject> jsonObjects = this.getYearHoliday(newDate);
  34. JSONObject temJsonObject = null;
  35. for (JSONObject jsonObject : jsonObjects) {
  36. String temDate = jsonObject.getString("date");
  37. if (time.equals(temDate)) {
  38. temJsonObject = jsonObject;
  39. break;
  40. }
  41. }
  42. if (temJsonObject != null) {
  43. if (temJsonObject.getBoolean("holiday")) {
  44. return "2"; // 节假日
  45. } else if (!temJsonObject.getBoolean("holiday")) {
  46. return "3"; // 调休日
  47. }
  48. } else {
  49. if (weekday == Calendar.SATURDAY || weekday == Calendar.SUNDAY) {
  50. return "1"; // 周末
  51. } else {
  52. return "0";
  53. }
  54. }
  55. return "0";
  56. }
  57. public List<JSONObject> getYearHoliday(String year) {
  58. List<JSONObject> jsonObjects = new ArrayList<>();
  59. String key = "holiday_" + year;
  60. String json = redisUtil.get(key);
  61. if (json == null || json.trim().equals("")) {
  62. String re = this.getYearHolidayHttp(year);
  63. if (re == null || re.trim().equals("")) {
  64. throw new XiaoShiException("获取节假日信息异常");
  65. }
  66. jsonObjects = this.formatHoliday(re);
  67. json = JSON.toJSONString(jsonObjects);
  68. redisUtil.set(key, json);
  69. } else {
  70. jsonObjects = JSON.parseArray(json, JSONObject.class);
  71. }
  72. return jsonObjects;
  73. }
  74. public String getYearHolidayHttp(String year) {
  75. String urlPath = "https://timor.tech/api/holiday/year/" + year;
  76. OkHttpClient okHttpClient = new OkHttpClient.Builder()
  77. .connectTimeout(60000, TimeUnit.SECONDS)
  78. .writeTimeout(60000, TimeUnit.SECONDS)
  79. .readTimeout(60000, TimeUnit.SECONDS)
  80. .build();
  81. Request request = new Request.Builder()
  82. .url(urlPath)
  83. .get()
  84. .header("User-Agent", "Mozilla/4.76")
  85. .build();
  86. Integer tryTime = 0;
  87. String re = null;
  88. while (tryTime < 3) {
  89. try {
  90. Response response = okHttpClient.newCall(request).execute();
  91. // 判断请求是否成功
  92. if (response.isSuccessful()) {
  93. // 打印服务端返回结果
  94. re = Objects.requireNonNull(response.body()).string();
  95. return re;
  96. }
  97. tryTime++;
  98. } catch (IOException e) {
  99. e.printStackTrace();
  100. }
  101. }
  102. // StringBuilder re = new StringBuilder();
  103. // try {
  104. //
  105. //
  106. // URL url = new URL(urlPath);
  107. // URLConnection connection = url.openConnection();
  108. // connection.setRequestProperty("User-Agent", "Mozilla/4.76");
  109. // BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
  110. // String strRead;
  111. // while ((strRead = reader.readLine()) != null) {
  112. // re.append(strRead).append("\r\n");
  113. // }
  114. // reader.close();
  115. // }
  116. // catch (Exception e){
  117. //
  118. // }
  119. return re;
  120. }
  121. public List<JSONObject> formatHoliday(String json) {
  122. List<JSONObject> jsonObjects = new ArrayList<>();
  123. JSONObject jsonObject = JSONObject.parseObject(json);
  124. String holidayStr = jsonObject.getString("holiday");
  125. JSONObject bodyJsonObject = JSONObject.parseObject(holidayStr);
  126. for (String key : bodyJsonObject.keySet()) {
  127. JSONObject mainObject = bodyJsonObject.getJSONObject(key);
  128. jsonObjects.add(mainObject);
  129. }
  130. return jsonObjects;
  131. }
  132. public Date getWorkingDay(Date date) throws ParseException {
  133. Boolean ifWorkingDate = false;
  134. while (!ifWorkingDate) {
  135. String re = this.isWorkingDay(date);
  136. if (re.equals("1") || re.equals("2")) {
  137. // 使用Calendar类加一天
  138. Calendar calendar = Calendar.getInstance();
  139. calendar.setTime(date); // 将Date对象设置到Calendar中
  140. calendar.add(Calendar.DATE, 1); // 增加一天。使用 Calendar.DAY_OF_MONTH 效果相同
  141. date= calendar.getTime(); // 转换回Date对象
  142. } else {
  143. ifWorkingDate = true;
  144. break;
  145. }
  146. }
  147. return date;
  148. }
  149. }