From 35e4a71cfb57ba2c7fb21f64601889661e0f32e5 Mon Sep 17 00:00:00 2001 From: Superdandan <1033719135@qq.com> Date: Sun, 7 Apr 2024 21:55:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=87=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/aggregates/base/BaseEvent.java | 28 +++++++++++++++++++ .../aggregates/base/EventManageable.java | 23 +++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/main/java/com/chint/domain/aggregates/base/BaseEvent.java create mode 100644 src/main/java/com/chint/domain/aggregates/base/EventManageable.java 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