Commit 087edfee by 刘鑫

fix(时间片容量): 时间片交集运算 以及并集运算修改

1 parent fa12213f
...@@ -231,8 +231,13 @@ public class CapacityUtils { ...@@ -231,8 +231,13 @@ public class CapacityUtils {
//计算最大空闲时间 //计算最大空闲时间
List<Long> engineerMaxDurationList = new ArrayList<>(); List<Long> engineerMaxDurationList = new ArrayList<>();
engineerSliceMap.forEach((engineerCode, t) -> { engineerSliceMap.forEach((engineerCode, t) -> {
long engineerMaxDuration = t.stream().map(CapacityEngineerSliceUsedEntity::getDurationTime)
.flatMap(Collection::stream) List<OccupyInfoDetail> timeSlots = t.stream().map(CapacityEngineerSliceUsedEntity::getDurationTime)
.flatMap(Collection::stream).collect(Collectors.toList());
timeSlots = calculateUnion(timeSlots);
long engineerMaxDuration = Optional.ofNullable(timeSlots).orElse(Collections.emptyList())
.stream()
.mapToLong(OccupyInfoDetail::getDuration) .mapToLong(OccupyInfoDetail::getDuration)
.sum(); .sum();
engineerMaxDurationList.add(engineerMaxDuration); engineerMaxDurationList.add(engineerMaxDuration);
......
...@@ -6,8 +6,6 @@ import lombok.NoArgsConstructor; ...@@ -6,8 +6,6 @@ import lombok.NoArgsConstructor;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@Data @Data
...@@ -31,8 +29,11 @@ public class OccupyInfoDetail extends OccupyInfo implements Comparable<OccupyInf ...@@ -31,8 +29,11 @@ public class OccupyInfoDetail extends OccupyInfo implements Comparable<OccupyInf
// 判断两个时间片是否有交集 // 判断两个时间片是否有交集
public boolean overlaps(OccupyInfoDetail other) { public boolean overlaps(OccupyInfoDetail other) {
// 如果一个时间片的开始时间在另一个时间片的结束时间之后,或者一个时间片的结束时间在另一个时间片的开始时间之前,那么他们没有交集 // 如果一个时间片的开始时间在另一个时间片的结束时间之后,或者一个时间片的结束时间在另一个时间片的开始时间之前,那么他们没有交集
return !(this.beginTime.isAfter(other.endTime) || this.endTime.isBefore(other.beginTime)); LocalDateTime maxStart = this.beginTime.isAfter(other.beginTime) ? this.beginTime : other.beginTime;
LocalDateTime minEnd = this.endTime.isAfter(other.endTime) ? other.endTime : this.endTime;
return maxStart.isBefore(minEnd);
} }
// 计算两个时间片的交集 // 计算两个时间片的交集
public OccupyInfoDetail intersection(OccupyInfoDetail other) { public OccupyInfoDetail intersection(OccupyInfoDetail other) {
// 如果两个时间片没有交集,返回null // 如果两个时间片没有交集,返回null
...@@ -45,6 +46,6 @@ public class OccupyInfoDetail extends OccupyInfo implements Comparable<OccupyInf ...@@ -45,6 +46,6 @@ public class OccupyInfoDetail extends OccupyInfo implements Comparable<OccupyInf
@Override @Override
public int compareTo(OccupyInfoDetail o) { public int compareTo(OccupyInfoDetail o) {
return this.beginTime.compareTo(o.beginTime); return this.beginTime.compareTo(o.beginTime);
} }
} }
...@@ -81,5 +81,29 @@ public class CapacityUtilsTest { ...@@ -81,5 +81,29 @@ public class CapacityUtilsTest {
Assertions.assertEquals(2, occupyInfoDetails1.size()); Assertions.assertEquals(2, occupyInfoDetails1.size());
} }
@Test
public void testOverlaps() {
OccupyInfoDetail fixedTime = new OccupyInfoDetail(LocalDateTime.of(LocalDate.now(), LocalTime.of(9, 0)
), LocalDateTime.of(LocalDate.now(), LocalTime.of(11, 0)));
OccupyInfoDetail dynaTime = new OccupyInfoDetail(LocalDateTime.of(LocalDate.now(), LocalTime.of(9, 0)
), LocalDateTime.of(LocalDate.now(), LocalTime.of(11, 0)));
Assertions.assertTrue(fixedTime.overlaps(dynaTime));
dynaTime = new OccupyInfoDetail(LocalDateTime.of(LocalDate.now(), LocalTime.of(10, 0)
), LocalDateTime.of(LocalDate.now(), LocalTime.of(11, 0)));
Assertions.assertTrue(fixedTime.overlaps(dynaTime));
dynaTime = new OccupyInfoDetail(LocalDateTime.of(LocalDate.now(), LocalTime.of(11, 0)
), LocalDateTime.of(LocalDate.now(), LocalTime.of(12, 0)));
Assertions.assertFalse(fixedTime.overlaps(dynaTime));
dynaTime = new OccupyInfoDetail(LocalDateTime.of(LocalDate.now(), LocalTime.of(8, 0)
), LocalDateTime.of(LocalDate.now(), LocalTime.of(9, 0)));
Assertions.assertFalse(fixedTime.overlaps(dynaTime));
}
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!