diff --git a/src/main/java/com/chint/domain/aggregates/base/BaseEvent.java b/src/main/java/com/chint/domain/aggregates/base/BaseEvent.java new file mode 100644 index 00000000..4e38f653 --- /dev/null +++ b/src/main/java/com/chint/domain/aggregates/base/BaseEvent.java @@ -0,0 +1,28 @@ +package com.chint.domain.aggregates.base; + +import lombok.Data; + +import java.time.LocalDateTime; +import java.util.Map; + +@Data +public abstract class BaseEvent { + + private Integer eventType; + + private transient String eventName; + + private LocalDateTime happenTime; + + // 新增抽象方法:获取状态码与状态名称的映射表 + protected abstract Map getStatusMappings(); + + // 实现reloadStatus方法,使用状态映射表更新状态名称 + public BaseEvent reloadStatus() { + Map mappings = getStatusMappings(); + if (mappings != null) { + this.eventName = mappings.getOrDefault(this.eventType, "未知状态"); + } + return this; + } +} \ No newline at end of file diff --git a/src/main/java/com/chint/domain/aggregates/base/EventManageable.java b/src/main/java/com/chint/domain/aggregates/base/EventManageable.java new file mode 100644 index 00000000..38c8454a --- /dev/null +++ b/src/main/java/com/chint/domain/aggregates/base/EventManageable.java @@ -0,0 +1,23 @@ +package com.chint.domain.aggregates.base; + +import java.util.Comparator; +import java.util.List; +import java.util.NoSuchElementException; + +public interface EventManageable { + + // 抽象方法:获取事件列表 + List getEvents(); + + // 默认方法:添加事件 + default void addEvent(BaseEvent event) { + getEvents().add(event); + } + + // 默认方法:获取最新的事件 + default BaseEvent getLastEvent() { + return getEvents().stream() + .max(Comparator.comparing(BaseEvent::getHappenTime)) + .orElseThrow(() -> new NoSuchElementException("No events found.")); + } +} \ No newline at end of file