From e972899fbb26df4d677dfc796ddcb1cff8d3df0e Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Thu, 29 Jun 2023 01:41:55 +0800 Subject: [PATCH] =?UTF-8?q?IWithCode=20=E7=9B=B8=E5=85=B3=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E7=9A=84=E4=BD=BF=E7=94=A8=E8=B0=83=E6=95=B4=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/jdbc/BeanPropertyParamSource.java | 33 ++++++++++++------- .../system/domain/model/menu/Menu.java | 25 +++++++++++--- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/jdbc/BeanPropertyParamSource.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/jdbc/BeanPropertyParamSource.java index bcb72b0..4af7f99 100644 --- a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/jdbc/BeanPropertyParamSource.java +++ b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/jdbc/BeanPropertyParamSource.java @@ -3,6 +3,9 @@ package xyz.zhouxy.plusone.jdbc; import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource; import org.springframework.lang.Nullable; +import xyz.zhouxy.plusone.commons.exception.IWithCode; +import xyz.zhouxy.plusone.commons.exception.IWithIntCode; + /** * 扩展了 {@link BeanPropertySqlParameterSource},在将 POJO 转换为 * {@link org.springframework.jdbc.core.namedparam.SqlParameterSource} 时, @@ -17,17 +20,23 @@ import org.springframework.lang.Nullable; */ public class BeanPropertyParamSource extends BeanPropertySqlParameterSource { - public BeanPropertyParamSource(Object object) { - super(object); - } + public BeanPropertyParamSource(Object object) { + super(object); + } - @Override - @Nullable - public Object getValue(String paramName) throws IllegalArgumentException { - Object value = super.getValue(paramName); - if (value instanceof Enum) { - return ((Enum) value).ordinal(); - } - return value; - } + @Override + @Nullable + public Object getValue(String paramName) throws IllegalArgumentException { + Object value = super.getValue(paramName); + if (value instanceof Enum e) { + if (value instanceof IWithCode c) { + return c.getCode(); + } + if (value instanceof IWithIntCode c) { + return c.getCode(); + } + return e.ordinal(); + } + return value; + } } diff --git a/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/menu/Menu.java b/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/menu/Menu.java index e415593..3eabc13 100644 --- a/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/menu/Menu.java +++ b/plusone-system/plusone-system-domain/src/main/java/xyz/zhouxy/plusone/system/domain/model/menu/Menu.java @@ -9,6 +9,7 @@ import com.fasterxml.jackson.annotation.JsonValue; import lombok.Getter; import lombok.ToString; +import xyz.zhouxy.plusone.commons.exception.IWithIntCode; import xyz.zhouxy.plusone.constant.EntityStatus; import xyz.zhouxy.plusone.domain.AggregateRoot; import xyz.zhouxy.plusone.domain.IWithOrderNumber; @@ -129,12 +130,28 @@ public class Menu extends AggregateRoot implements IWithOrderNumber, IWith this.version = version; } - public enum MenuType { - MENU_LIST, MENU_ITEM; + public enum MenuType implements IWithIntCode { + MENU_LIST(0), MENU_ITEM(1); + + private final int code; + + private MenuType(int code) { + this.code = code; + } @JsonValue - public int value() { - return ordinal(); + @Override + public int getCode() { + return code; + } + + public static MenuType valueOf(int code) { + for (MenuType value : values()) { + if (value.code == code) { + return value; + } + } + throw new EnumConstantNotPresentException(MenuType.class, Integer.toString(code)); } } }