package me.zhengjie; import me.zhengjie.modules.attendance.domain.AttendanceGoOut; import me.zhengjie.modules.attendance.domain.AttendanceOvertime; import me.zhengjie.modules.attendance.domain.Calculate; import me.zhengjie.modules.attendance.service.AttendanceGoOutService; import me.zhengjie.modules.constant.Constant; import me.zhengjie.modules.quartz.task.AttendanceMonthlyMethod; import me.zhengjie.modules.system.domain.SysRostering; import me.zhengjie.modules.system.domain.User; import me.zhengjie.modules.system.repository.SysRosteringRepository; import me.zhengjie.modules.system.repository.UserRepository; import me.zhengjie.modules.system.service.SysConfigHolidayService; import me.zhengjie.modules.system.service.dto.SysConfigHolidayDto; import me.zhengjie.utils.SecurityUtils; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.math.BigDecimal; import java.sql.Timestamp; import java.text.ParseException; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.stream.Stream; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class AttendanceTest { @Autowired private AttendanceGoOutService attendanceGoOutService; @Autowired private UserRepository userRepository; @Autowired private SysConfigHolidayService sysConfigHolidayService; @Autowired private SysRosteringRepository sysRosteringRepository; @Test public void test() { AttendanceGoOut resources = new AttendanceGoOut(); resources.setUserId(128L); resources.setOutStartTime(Timestamp.valueOf(LocalDateTime.of(2024, 3, 25, 9, 0, 0))); resources.setOutEndTime(Timestamp.valueOf(LocalDateTime.of(2024, 3, 25, 18, 0, 0))); // 获取当前登陆人信息,设置为请假发起人 User user = userRepository.getOne(resources.getUserId()); LocalDate startDate = resources.getOutStartTime().toLocalDateTime().toLocalDate(); LocalDate endDate = resources.getOutEndTime().toLocalDateTime().toLocalDate(); LocalTime startTime = resources.getOutStartTime().toLocalDateTime().toLocalTime(); LocalTime endTime = resources.getOutEndTime().toLocalDateTime().toLocalTime(); long distance = ChronoUnit.DAYS.between(startDate, endDate); Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).forEach(localDate -> { //resUserId为null说明是添加,反之 if (resources.getUserId() == null) { resources.setUserId(user.getId()); } resources.setOutStartTime(Timestamp.valueOf(localDate.atTime(startTime))); resources.setOutEndTime(Timestamp.valueOf(localDate.atTime(endTime))); Calculate calculate = new Calculate(); calculate.setStart(resources.getOutStartTime().toLocalDateTime()); calculate.setEnd(resources.getOutEndTime().toLocalDateTime()); SysRostering sysRostering = sysRosteringRepository.findByRosId(2L); LocalDate startDate1 = calculate.getStart().toLocalDate(); LocalDate endDate2 = calculate.getEnd().toLocalDate(); LocalTime startTime2 = calculate.getStart().toLocalTime(); LocalTime endTime2 = calculate.getEnd().toLocalTime(); long distance2 = ChronoUnit.DAYS.between(startDate1, endDate2); long timeSeconds = Stream.iterate(startDate1, d -> d.plusDays(1)).limit(distance2 + 1).map(localDate2 -> { SysConfigHolidayDto sysConfigHolidayDto = sysConfigHolidayService.queryByHolidayDate(localDate2.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); Boolean isWeekend = localDate2.getDayOfWeek() == DayOfWeek.SATURDAY || localDate2.getDayOfWeek() == DayOfWeek.SUNDAY; if ((sysConfigHolidayDto == null && !isWeekend) || sysConfigHolidayDto != null && Constant.DATE_TYPE_WEEKDAY.equals(sysConfigHolidayDto.getDateType())) { LocalDateTime goOutStartTime = localDate2.atTime(startTime2); LocalDateTime goOutEndTime = localDate2.atTime(endTime2); LocalDateTime lunchStartTime = localDate2.atTime(sysRostering.getLunchStartTime()); LocalDateTime lunchEndTime = localDate2.atTime(sysRostering.getLunchEndTime()); if (goOutEndTime.isBefore(lunchStartTime) || goOutStartTime.isAfter(lunchEndTime)) { return ChronoUnit.SECONDS.between(goOutStartTime, goOutEndTime); } else if (!goOutEndTime.isBefore(lunchStartTime) && !goOutEndTime.isAfter(lunchEndTime)) { return ChronoUnit.SECONDS.between(goOutStartTime, lunchStartTime); } else if (!goOutStartTime.isBefore(lunchStartTime) && !goOutStartTime.isAfter(lunchEndTime)) { return ChronoUnit.SECONDS.between(lunchEndTime, goOutEndTime); } else { return ChronoUnit.SECONDS.between(goOutStartTime, goOutEndTime) - 3600; } } return 0L; }).reduce(0L, (result, current) -> result + current); Double result = Double.valueOf(new BigDecimal((float) timeSeconds / 3600).setScale(2, BigDecimal.ROUND_HALF_UP).toString()); System.out.println("result : " + result); }); } @Autowired private AttendanceMonthlyMethod attendanceMonthlyMethod; @Test public void exec100() throws ParseException { attendanceMonthlyMethod.getMonthlyData(); } }