BAOLH
2025-08-06 a561153115514a8ac76037c1348f6d4ddb171f5e
外出时长计算减去所有休息时段时间
2 files modified
121 ■■■■■ changed files
eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/attendance/domain/Calculate.java 59 ●●●●● patch | view | raw | blame | history
eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/attendance/service/impl/AttendanceGoOutServiceImpl.java 62 ●●●●● patch | view | raw | blame | history
eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/attendance/domain/Calculate.java
@@ -4,8 +4,10 @@
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.ArrayList;
import java.util.List;
@Data
@AllArgsConstructor
@@ -20,4 +22,59 @@
    public int compareTo(Calculate o) {
        return start.compareTo(o.start);
    }
    public Duration getDuration() {
        return Duration.between(start, end);
    }
    public static Duration calculateRemainingDuration(Calculate mainPeriod, Calculate... excludedPeriods) {
        List<Calculate> remainPeriods = new ArrayList<>();
        remainPeriods.add(mainPeriod);
        for (Calculate excluded : excludedPeriods) {
            List<Calculate> newRemaining = new ArrayList<>();
            for (Calculate remain : remainPeriods) {
                newRemaining.addAll(subtractPeriod(remain, excluded));
            }
            remainPeriods = newRemaining;
        }
        // 计算所有剩余时间段的总时长
        Duration total = Duration.ZERO;
        for (Calculate period : remainPeriods) {
            total = total.plus(period.getDuration());
        }
        return total;
    }
    private static List<Calculate> subtractPeriod(Calculate main, Calculate toSubtract) {
        List<Calculate> result = new ArrayList<>();
        // 没有重叠的情况
        if (main.getEnd().compareTo(toSubtract.getStart()) <= 0 ||
                main.getStart().compareTo(toSubtract.getEnd()) >= 0) {
            result.add(main);
            return result;
        }
        // 时间段完全包含在要减去的时间段中
        if (main.getStart().compareTo(toSubtract.getStart()) >= 0 &&
                main.getEnd().compareTo(toSubtract.getEnd()) <= 0) {
            return result; // 返回空列表
        }
        // 时间段部分重叠
        if (main.getStart().compareTo(toSubtract.getStart()) < 0) {
            // 左边有剩余
            result.add(new Calculate(main.getStart(), toSubtract.getStart()));
        }
        if (main.getEnd().compareTo(toSubtract.getEnd()) > 0) {
            // 右边有剩余
            result.add(new Calculate(toSubtract.getEnd(), main.getEnd()));
        }
        return result;
    }
}
eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/attendance/service/impl/AttendanceGoOutServiceImpl.java
@@ -74,13 +74,9 @@
public class AttendanceGoOutServiceImpl implements AttendanceGoOutService {
    private final AttendanceGoOutRepository attendanceGoOutRepository;
    private final AttendanceOvertimeService attendanceOvertimeService;
    private final AttendanceGoOutMapper attendanceGoOutMapper;
    private final UserRepository userRepository;
    private final AttendanceService attendanceService;
    private final SysConfigHolidayRepository sysConfigHolidayRepository;
    private final AttendanceLeaveServiceImpl attendanceLeaveService;
    private final AttendanceRepository attendanceRepository;
    private final AttendanceLeaveRepository attendanceLeaveRepository;
    private final AttendanceBusinessTravelRepository attendanceBusinessTravelRepository;
    private final ApproveEventRepository approveEventRepository;
@@ -526,31 +522,41 @@
        LocalTime endTime = calculate.getEnd().toLocalTime();
        long distance = ChronoUnit.DAYS.between(startDate, endDate);
        return Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).map(localDate -> {
            SysConfigHolidayDto sysConfigHolidayDto = sysConfigHolidayService.queryByHolidayDate(localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            Boolean isWeekend = localDate.getDayOfWeek() == DayOfWeek.SATURDAY || localDate.getDayOfWeek() == DayOfWeek.SUNDAY;
            if ((sysConfigHolidayDto == null && !isWeekend) ||
                    sysConfigHolidayDto != null && Constant.DATE_TYPE_WEEKDAY.equals(sysConfigHolidayDto.getDateType())) {
//            SysConfigHolidayDto sysConfigHolidayDto = sysConfigHolidayService.queryByHolidayDate(localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
//            Boolean isWeekend = localDate.getDayOfWeek() == DayOfWeek.SATURDAY || localDate.getDayOfWeek() == DayOfWeek.SUNDAY;
//            if ((sysConfigHolidayDto == null && !isWeekend) ||
//                    sysConfigHolidayDto != null && Constant.DATE_TYPE_WEEKDAY.equals(sysConfigHolidayDto.getDateType())) {
//
//                LocalDateTime goOutStartTime = localDate.atTime(startTime);
//                LocalDateTime goOutEndTime = localDate.atTime(endTime);
//                LocalDateTime lunchStartTime = localDate.atTime(sysRostering.getLunchStartTime());
//                LocalDateTime lunchEndTime = localDate.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;
//                }
//            } else {
//                LocalDateTime goOutStartTime = localDate.atTime(startTime);
//                LocalDateTime goOutEndTime = localDate.atTime(endTime);
//                return ChronoUnit.SECONDS.between(goOutStartTime, goOutEndTime);
//            }
                LocalDateTime goOutStartTime = localDate.atTime(startTime);
                LocalDateTime goOutEndTime = localDate.atTime(endTime);
                LocalDateTime lunchStartTime = localDate.atTime(sysRostering.getLunchStartTime());
                LocalDateTime lunchEndTime = localDate.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;
                }
            } else {
                LocalDateTime goOutStartTime = localDate.atTime(startTime);
                LocalDateTime goOutEndTime = localDate.atTime(endTime);
                return ChronoUnit.SECONDS.between(goOutStartTime, goOutEndTime);
            }
            // TODO:20250806修改 减去下班后休息时段
            LocalDateTime goOutStartTime = localDate.atTime(startTime);
            LocalDateTime goOutEndTime = localDate.atTime(endTime);
            LocalDateTime lunchStartTime = localDate.atTime(sysRostering.getLunchStartTime());
            LocalDateTime lunchEndTime = localDate.atTime(sysRostering.getLunchEndTime());
            LocalDateTime offStartTime = localDate.atTime(sysRostering.getWorkEndTime());
            LocalDateTime offEndTime = offStartTime.plusMinutes(30L);
            Duration duration = Calculate.calculateRemainingDuration(new Calculate(goOutStartTime, goOutEndTime), new Calculate(lunchStartTime, lunchEndTime), new Calculate(offStartTime, offEndTime));
            return duration.get(ChronoUnit.SECONDS);
        }).reduce(0L, (result, current) -> result + current);
    }