HolidayDateServiceImpl.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package com.xjrsoft.module.holiday.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  6. import com.github.yulichang.base.MPJBaseServiceImpl;
  7. import com.xjrsoft.common.exception.MyException;
  8. import com.xjrsoft.common.utils.DateUtils;
  9. import com.xjrsoft.common.utils.HolidayUtil;
  10. import com.xjrsoft.module.holiday.dto.AddHolidayDateDto;
  11. import com.xjrsoft.module.holiday.entity.HolidayDate;
  12. import com.xjrsoft.module.holiday.mapper.HolidayDateMapper;
  13. import com.xjrsoft.module.holiday.service.IHolidayDateService;
  14. import com.xjrsoft.module.holiday.vo.AlmanacVo;
  15. import lombok.AllArgsConstructor;
  16. import org.springframework.stereotype.Service;
  17. import org.springframework.transaction.annotation.Transactional;
  18. import java.text.ParseException;
  19. import java.text.SimpleDateFormat;
  20. import java.time.LocalDateTime;
  21. import java.time.format.DateTimeFormatter;
  22. import java.util.*;
  23. import java.util.stream.Collectors;
  24. /**
  25. * @title:
  26. * @Author fanxp
  27. * @Date: 2024-03-26
  28. * @Version 1.0
  29. */
  30. @Service
  31. @AllArgsConstructor
  32. public class HolidayDateServiceImpl extends MPJBaseServiceImpl<HolidayDateMapper, HolidayDate> implements IHolidayDateService {
  33. private final HolidayDateMapper holidayDateMapper;
  34. /**
  35. * 当前时间是否休息
  36. */
  37. public Boolean currentIsRest() {
  38. LocalDateTime now = LocalDateTime.now();
  39. String day = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
  40. Long count = holidayDateMapper.selectCount(Wrappers.<HolidayDate>query().lambda()
  41. .eq(HolidayDate::getDate, day)
  42. .eq(HolidayDate::getStatus,3)
  43. );
  44. return count > 0;
  45. }
  46. /**
  47. * 添加数据
  48. *
  49. * @param dto
  50. * @return
  51. */
  52. @Transactional(rollbackFor = Exception.class)
  53. public Boolean add(AddHolidayDateDto dto) throws ParseException {
  54. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  55. Date start = sdf.parse(dto.getStartDate());
  56. Date end = sdf.parse(dto.getEndDate());
  57. if (start.compareTo(end) > 0) {
  58. throw new MyException("开始时间不能大于结束时间!");
  59. }
  60. while (start.compareTo(end) <= 0) {
  61. Calendar calendar = Calendar.getInstance();
  62. calendar.setTime(start);
  63. HolidayDate param = HolidayDate.builder()
  64. .date(sdf.format(start))
  65. .way(2)
  66. .year(calendar.get(Calendar.YEAR))
  67. .month(calendar.get(Calendar.MONTH) + 1)
  68. .day(calendar.get(Calendar.DATE))
  69. .status(dto.getStatus())
  70. .build();
  71. holidayDateMapper.insert(param);
  72. start = DateUtils.addDateDays(start, 1);
  73. }
  74. return true;
  75. }
  76. /**
  77. * 删除数据
  78. *
  79. * @param ids
  80. * @return
  81. */
  82. @Transactional(rollbackFor = Exception.class)
  83. public Boolean delete(List<Long> ids) {
  84. List<HolidayDate> holidayDates = holidayDateMapper.selectBatchIds(ids);
  85. for (HolidayDate h : holidayDates) {
  86. // 用户数据直接删除,系统数据添加标识
  87. if (h.getWay() == 1) {
  88. h.setDeleteStatus(1);
  89. holidayDateMapper.updateById(h);
  90. } else {
  91. holidayDateMapper.deleteById(h.getId());
  92. }
  93. }
  94. return true;
  95. }
  96. /**
  97. * 初始化节假日
  98. *
  99. * @param year
  100. * @return
  101. */
  102. @Transactional(rollbackFor = Exception.class)
  103. public Boolean initHoliday(int year) {
  104. List<HolidayDate> allList = new ArrayList<>();
  105. for (int month = 1; month < 13; month++) {
  106. List<HolidayDate> monthList = getYearMonthHoliday(year, month);
  107. allList.addAll(monthList);
  108. }
  109. // List<HolidayDate> monthList = getYearMonthHoliday(year - 1, 12);
  110. // allList.addAll(monthList);
  111. Map<String, HolidayDate> mData = allList.stream().collect(Collectors.toMap(HolidayDate::getDate, item -> item));
  112. // 获取用户数据
  113. List<HolidayDate> userDates = holidayDateMapper.selectList(Wrappers.<HolidayDate>query().lambda().eq(HolidayDate::getYear, year).eq(HolidayDate::getWay, 2));
  114. for (HolidayDate uh : userDates) {
  115. mData.keySet().removeIf(key -> Objects.equals(key, uh.getDate()));
  116. }
  117. // 清除系统数据
  118. holidayDateMapper.delete(Wrappers.<HolidayDate>query().lambda().eq(HolidayDate::getYear, year).eq(HolidayDate::getWay, 1));
  119. // 保存系统数据
  120. for (Map.Entry<String, HolidayDate> entry : mData.entrySet()) {
  121. holidayDateMapper.insert(entry.getValue());
  122. }
  123. return true;
  124. }
  125. /**
  126. * Init month list.
  127. * 按月爬取节假日
  128. *
  129. * @param year the year
  130. * @param month the month
  131. * @return the list
  132. */
  133. public static List<HolidayDate> getYearMonthHoliday(int year, int month) {
  134. List<HolidayDate> holidayDateList = new ArrayList<>();
  135. try {
  136. String result = HolidayUtil.getMonth(year + "", month + "");
  137. JSONObject json = JSON.parseObject(result);
  138. System.out.println(result);
  139. JSONArray data = json.getJSONArray("data");
  140. JSONObject dataObj = JSON.parseObject(data.get(0).toString());
  141. List<AlmanacVo> almanacs = JSONArray.parseArray(dataObj.getString("almanac"), AlmanacVo.class);
  142. for (AlmanacVo almanac : almanacs) {
  143. if (almanac.getMonth().equals(String.valueOf(month)) && almanac.getStatus() != null) {
  144. HolidayDate date = HolidayDate.builder()
  145. .year(year).month(month).day(Integer.parseInt(almanac.getDay()))
  146. .date(DateUtils.format(new Date(year - 1900, month - 1, Integer.parseInt(almanac.getDay())), "yyyy-MM-dd"))
  147. .build();
  148. String status = almanac.getStatus();
  149. if (status.equals("1")) {
  150. date.setStatus(3);
  151. } else if (status.equals("2")) {
  152. date.setStatus(2);
  153. }
  154. holidayDateList.add(date);
  155. }
  156. }
  157. } catch (ClassCastException classCastException) {
  158. throw new MyException("可能是当前月份(" + month + "月)没有节日");
  159. } catch (Exception e) {
  160. throw new MyException(e.getMessage());
  161. }
  162. return holidayDateList;
  163. }
  164. }