| | |
| | | package com.yuanchu.mom.backup; |
| | | |
| | | import com.yuanchu.mom.dto.PerformanceShiftAddDto; |
| | | import com.yuanchu.mom.mapper.EnumMapper; |
| | | import com.yuanchu.mom.pojo.Enums; |
| | | import com.yuanchu.mom.pojo.PerformanceShift; |
| | | import com.yuanchu.mom.pojo.User; |
| | | import com.yuanchu.mom.service.EnumService; |
| | | import com.yuanchu.mom.service.PerformanceShiftService; |
| | | import com.yuanchu.mom.service.UserService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.scheduling.annotation.EnableScheduling; |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.*; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.LocalTime; |
| | | import java.time.temporal.TemporalAdjusters; |
| | | import java.time.temporal.WeekFields; |
| | | import java.util.Date; |
| | | import java.util.Dictionary; |
| | | import java.util.List; |
| | | import java.util.Locale; |
| | | import java.util.stream.Collectors; |
| | | |
| | | //@Component |
| | | @Component |
| | | @EnableScheduling |
| | | @Slf4j |
| | | public class MysqlDataBackup { |
| | |
| | | @Value("${backup.mysqldump}") |
| | | private String mysqldump; |
| | | |
| | | @Resource |
| | | private PerformanceShiftService performanceShiftService; |
| | | |
| | | @Resource |
| | | private EnumService enumService; |
| | | |
| | | @Resource |
| | | private UserService userService; |
| | | |
| | | /** |
| | | * 每天晚上23点05秒执行 【 0 0 4 1/1 * ? 】 |
| | | * 测试 20 秒一次【 0/20 * * * * ? 】@Scheduled(cron = "5 * 23 * * ?") |
| | | */ |
| | | // @Scheduled(cron = "5 0 23 * * ?") |
| | | //@Scheduled(cron = "0/20 * * * * ?") |
| | | private void configureTasks() { |
| | | log.info("【备份数据库】--START"); |
| | | String dbUrl2 = dbUrl.replace("jdbc:mysql://", ""); |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 定时任务,每个月1号的00:00:00 |
| | | * 给每个人都进行排班(默认早班) |
| | | */ |
| | | @Scheduled(cron = "0 0 0 1 * ?") |
| | | //@Scheduled(cron = "0/20 * * * * ?") |
| | | private void timerCreateSchedule(){ |
| | | System.out.println("开始给每个人进行排班,默认早班======start"); |
| | | // TODO 给每个人都进行排班(默认早班) |
| | | PerformanceShiftAddDto performanceShiftAddDto = new PerformanceShiftAddDto(); |
| | | //班次--早(查询字典) |
| | | List<Enums> shiftType = enumService.selectEnumByCategory("班次类型"); |
| | | List<String> collect = shiftType.stream().filter(enums -> enums.getLabel().equals("早")).map(enums -> enums.getValue()).collect(Collectors.toList()); |
| | | performanceShiftAddDto.setShift(collect.get(0)); |
| | | //人员--所有人 |
| | | String userIds = userService.getDeviceManager().stream().map(user -> user.getId().toString()).distinct().collect(Collectors.joining(",")); |
| | | performanceShiftAddDto.setUserId(userIds); |
| | | //周次--当月所有 |
| | | // 获取当前日期 |
| | | LocalDate today = LocalDate.now(); |
| | | // 获取本月的第一天和最后一天 |
| | | LocalDate firstDayOfMonth = today.with(TemporalAdjusters.firstDayOfMonth()); |
| | | LocalDate lastDayOfMonth = today.with(TemporalAdjusters.lastDayOfMonth()); |
| | | // 获取周字段信息(根据区域设置) |
| | | WeekFields weekFields = WeekFields.of(Locale.getDefault()); |
| | | // 获取本月第一天的周一 |
| | | LocalDate startOfWeek = firstDayOfMonth.with(TemporalAdjusters.previousOrSame(weekFields.getFirstDayOfWeek())); |
| | | // 遍历本月所有天数,找出每周的第一天和最后一天 |
| | | LocalDate endOfWeek; |
| | | while (startOfWeek.isBefore(firstDayOfMonth.plusMonths(1))) { |
| | | endOfWeek = startOfWeek.plusDays(6); |
| | | LocalDateTime startDateTime = LocalDateTime.of(startOfWeek, LocalTime.MIDNIGHT); |
| | | LocalDateTime endDateTime = LocalDateTime.of(endOfWeek, LocalTime.MIDNIGHT); |
| | | System.out.println("Week starts on " + startDateTime + " and ends on " + endDateTime); |
| | | performanceShiftAddDto.setStartWeek(startDateTime); |
| | | performanceShiftAddDto.setEndWeek(endDateTime); |
| | | performanceShiftService.performanceShiftAdd(performanceShiftAddDto); |
| | | startOfWeek = startOfWeek.plusWeeks(1); |
| | | } |
| | | |
| | | System.out.println("排班结束======end"); |
| | | } |
| | | |
| | | /** |
| | | * 判断文件是否存在,不存在创建 |
| | | */ |