| | |
| | | package com.aps.core.service.impl; |
| | | |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.alibaba.fastjson2.JSONArray; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.aps.common.core.utils.DateUtils; |
| | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.DayOfWeek; |
| | | import java.time.LocalDate; |
| | | import java.time.YearMonth; |
| | | import java.util.List; |
| | | import java.time.ZoneId; |
| | | import java.time.temporal.TemporalAdjusters; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * 日历管理Service业务层处理 |
| | |
| | | } |
| | | return res; |
| | | } |
| | | |
| | | @Override |
| | | public JSONArray selectCalendarView(ApsWorkCalendar apsWorkCalendar) { |
| | | JSONArray res = new JSONArray(); |
| | | String[] parts = new SimpleDateFormat("yyyy-MM-dd").format(apsWorkCalendar.getEffectiveDate()).split("-"); |
| | | int year = Integer.parseInt(parts[0]); |
| | | int month = Integer.parseInt(parts[1]); |
| | | List<LocalDate> dates = getCalendarData(year, month); |
| | | apsWorkCalendar.setEffectiveDate(Date.from(dates.get(0).atStartOfDay(ZoneId.systemDefault()).toInstant())); |
| | | apsWorkCalendar.setExpiringDate(Date.from(dates.get(dates.size()-1).atStartOfDay(ZoneId.systemDefault()).toInstant())); |
| | | //生成日历基础数据 |
| | | LinkedHashMap<String, String> baseCalendar = new LinkedHashMap<>(); |
| | | for (LocalDate date : dates) { |
| | | baseCalendar.put(date+"", ""); |
| | | } |
| | | try { |
| | | List<ApsWorkCalendar> list = apsWorkCalendarMapper.selectApsWorkCalendarList(apsWorkCalendar); |
| | | for (ApsWorkCalendar apsWorkCalendarTemp : list) { |
| | | //工作日 |
| | | org.postgresql.util.PGobject pgObjectWeekdays = (org.postgresql.util.PGobject) apsWorkCalendarTemp.getContent(); |
| | | JSONArray weekdays = (JSON.parseObject(pgObjectWeekdays.getValue())).getJSONArray("weekdays"); |
| | | HashMap<String, String> weekdaysMap = new HashMap<>(); |
| | | for (int i = 0; i < weekdays.size(); i++) { |
| | | weekdaysMap.put(weekdays.getJSONObject(i).getString("day"), weekdays.getJSONObject(i).getString("work")); |
| | | } |
| | | List<LocalDate> datesTempWeekdays = getDatesBetween(apsWorkCalendarTemp.getEffectiveDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate(), apsWorkCalendarTemp.getExpiringDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()); |
| | | for (LocalDate dateTemp: datesTempWeekdays) { |
| | | if(baseCalendar.containsKey(dateTemp+"")){ |
| | | if ("y".equals(weekdaysMap.get(dateTemp.getDayOfWeek().getDisplayName(java.time.format.TextStyle.FULL, Locale.ENGLISH)))) { |
| | | baseCalendar.put(dateTemp+"", "工作日"); |
| | | } else if("n".equals(weekdaysMap.get(dateTemp.getDayOfWeek().getDisplayName(java.time.format.TextStyle.FULL, Locale.ENGLISH)))) { |
| | | baseCalendar.put(dateTemp+"", "休息日"); |
| | | } |
| | | } |
| | | } |
| | | //休息日 |
| | | org.postgresql.util.PGobject pgObjectHolidays = (org.postgresql.util.PGobject) apsWorkCalendarTemp.getHolidays(); |
| | | if(pgObjectHolidays!=null){ |
| | | JSONArray holidays = JSONArray.parse(pgObjectHolidays.getValue()); |
| | | for (int i = 0; i < holidays.size(); i++){ |
| | | JSONObject h = holidays.getJSONObject(i); |
| | | List<LocalDate> datesTempHolidays = getDatesBetween(h.getObject("startdate", LocalDate.class), h.getObject("enddate", LocalDate.class)); |
| | | for (LocalDate dateTemp: datesTempHolidays) { |
| | | if(baseCalendar.containsKey(dateTemp+"")){ |
| | | baseCalendar.put(dateTemp+"", "节假日"); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | for (Map.Entry<String, String> entry : baseCalendar.entrySet()) { |
| | | JSONObject dayInfo = new JSONObject(); |
| | | dayInfo.put("date", entry.getKey()); |
| | | dayInfo.put("type", entry.getValue()); |
| | | res.add(dayInfo); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return res; |
| | | } |
| | | return res; |
| | | } |
| | | |
| | | @Override |
| | | public int deleteApsWorkCalendar(ApsWorkCalendar apsWorkCalendar){ |
| | | return apsWorkCalendarMapper.deleteApsWorkCalendar(apsWorkCalendar); |
| | | } |
| | | |
| | | private List<LocalDate> getCalendarData(int year, int month) { |
| | | // 获取4月1号的当周的周日 |
| | | LocalDate startDate = LocalDate.of(year, month, 1) |
| | | .with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY)); |
| | | |
| | | // 获取4月的最后一天 |
| | | LocalDate lastDayOfMonth = LocalDate.of(year, month, 1) |
| | | .with(TemporalAdjusters.lastDayOfMonth()); |
| | | |
| | | // 获取4月最后一天的当周的周六 |
| | | LocalDate endDate = lastDayOfMonth.with(TemporalAdjusters.nextOrSame(DayOfWeek.SATURDAY)); |
| | | |
| | | // 获取日期范围内的所有日期 |
| | | List<LocalDate> dates = getDatesBetween(startDate, endDate); |
| | | |
| | | // 打印结果 |
| | | // for (LocalDate date : dates) { |
| | | // String dayOfWeekChinese = getChineseDayOfWeek(date.getDayOfWeek()); |
| | | // String dayOfWeekEnglish = date.getDayOfWeek().getDisplayName(java.time.format.TextStyle.FULL, Locale.ENGLISH); |
| | | // System.out.println(date + " - " + dayOfWeekChinese + " (" + dayOfWeekEnglish + ")"); |
| | | // } |
| | | return dates; |
| | | } |
| | | |
| | | private static List<LocalDate> getDatesBetween(LocalDate startDate, LocalDate endDate) { |
| | | List<LocalDate> dates = new ArrayList<>(); |
| | | for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) { |
| | | dates.add(date); |
| | | } |
| | | return dates; |
| | | } |
| | | |
| | | private static String getChineseDayOfWeek(DayOfWeek dayOfWeek) { |
| | | switch (dayOfWeek) { |
| | | case MONDAY: |
| | | return "星期一"; |
| | | case TUESDAY: |
| | | return "星期二"; |
| | | case WEDNESDAY: |
| | | return "星期三"; |
| | | case THURSDAY: |
| | | return "星期四"; |
| | | case FRIDAY: |
| | | return "星期五"; |
| | | case SATURDAY: |
| | | return "星期六"; |
| | | case SUNDAY: |
| | | return "星期日"; |
| | | default: |
| | | return ""; |
| | | } |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | // 定义目标月份和年份 |
| | | int year = 2025; |
| | | int month = 4; |
| | | |
| | | // 获取4月1号的当周的周日 |
| | | LocalDate startDate = LocalDate.of(year, month, 1) |
| | | .with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY)); |
| | | |
| | | // 获取4月的最后一天 |
| | | LocalDate lastDayOfMonth = LocalDate.of(year, month, 1) |
| | | .with(TemporalAdjusters.lastDayOfMonth()); |
| | | |
| | | // 获取4月最后一天的当周的周六 |
| | | LocalDate endDate = lastDayOfMonth.with(TemporalAdjusters.nextOrSame(DayOfWeek.SATURDAY)); |
| | | |
| | | // 获取日期范围内的所有日期 |
| | | List<LocalDate> dates = getDatesBetween(startDate, endDate); |
| | | |
| | | // 打印结果 |
| | | for (LocalDate date : dates) { |
| | | String dayOfWeekChinese = getChineseDayOfWeek(date.getDayOfWeek()); |
| | | String dayOfWeekEnglish = date.getDayOfWeek().getDisplayName(java.time.format.TextStyle.FULL, Locale.ENGLISH); |
| | | System.out.println(date + " - " + dayOfWeekChinese + " (" + dayOfWeekEnglish + ")"); |
| | | } |
| | | } |
| | | } |