BAOLH
2025-12-02 d8eddeba2456ad1c89df68979112ae98d82c7948
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package me.zhengjie.modules.attendance.domain;
 
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
 
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
 
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Calculate implements Comparable<Calculate> {
 
    private LocalDateTime start;
 
    private LocalDateTime end;
 
    @Override
    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;
    }
 
    /**
     * 判断与另一个时间区间是否有交集
     */
    public Boolean overlapsWith(Calculate otherCalculate) {
        Objects.requireNonNull(otherCalculate, "另一个时间区间不能为null");
        return !this.end.isBefore(otherCalculate.start) && !otherCalculate.end.isBefore(this.start);
    }
 
    /**
     * 获取交集区间(没有返回null)
     */
    public Calculate getOverlap(Calculate other) {
        if (!overlapsWith(other)) {
            return null;
        }
 
        LocalDateTime overlapStart = this.start.isAfter(other.start) ? this.start : other.start;
        LocalDateTime overlapEnd = this.end.isBefore(other.end) ? this.end : other.end;
 
        return new Calculate(overlapStart, overlapEnd);
    }
}