枚举问题

This commit is contained in:
2025-12-18 20:33:08 +08:00
parent 0b9ebaaa96
commit e4f51f2dec
7 changed files with 88 additions and 35 deletions

View File

@@ -4,11 +4,11 @@ import cn.hutool.core.exceptions.ValidateException;
import com.czg.account.dto.PageDTO;
import com.czg.account.dto.shopinfo.*;
import com.czg.account.entity.ShopInfo;
import com.czg.constants.ShopSwitchTypeEnum;
import com.czg.exception.CzgException;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.service.IService;
import com.mybatisflex.core.util.LambdaGetter;
import java.util.function.Function;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
@@ -24,10 +24,10 @@ public interface ShopInfoService extends IService<ShopInfo> {
/**
* 检测开关
* @param shopId 店铺id
* @param switchGetter ShopInfo的某列 开关
* @param switchType ShopInfo的某列 开关
* @return true:开启 false:关闭
*/
<T> boolean checkSwitch(Long shopId, Function<ShopInfo, T> switchGetter) throws ValidateException;
boolean checkSwitch(Long shopId, ShopSwitchTypeEnum switchType) throws ValidateException;
Page<ShopInfo> get(PageDTO pageDTO, String shopName, Integer status, Integer isHeadShop);

View File

@@ -0,0 +1,20 @@
package com.czg.constants;
import java.io.Serial;
import java.io.Serializable;
import java.util.function.Function;
/**
* 可序列化的函数式接口
* 用于在Dubbo等需要序列化的场景中传递Lambda表达式
*
* @param <T> 输入类型
* @param <R> 输出类型
* @author ww
*/
@FunctionalInterface
public interface SerializableFunction<T, R> extends Function<T, R>, Serializable {
// serialVersionUID 不能是private
@Serial
long serialVersionUID = 1L;
}

View File

@@ -0,0 +1,57 @@
package com.czg.constants;
import com.czg.account.entity.ShopInfo;
/**
* 店铺开关枚举
*
* @author ww
*/
public enum ShopSwitchTypeEnum {
GROUP_BUY("groupBuy", "拼团开关", ShopInfo::getIsGroupBuy),
;
private final String code;
private final String description;
private final SerializableFunction<ShopInfo, Integer> getter;
ShopSwitchTypeEnum(String code, String description,
SerializableFunction<ShopInfo, Integer> getter) {
this.code = code;
this.description = description;
this.getter = getter;
}
public String getCode() {
return code;
}
public String getDescription() {
return description;
}
public boolean getValue(ShopInfo shopInfo) {
if (shopInfo == null) {
return false;
}
try {
return getter.apply(shopInfo) == 1;
} catch (Exception e) {
return false;
}
}
public SerializableFunction<ShopInfo, Integer> getGetter() {
return getter;
}
// 根据code获取枚举
public static ShopSwitchTypeEnum getByCode(String code) {
for (ShopSwitchTypeEnum type : values()) {
if (type.code.equals(code)) {
return type;
}
}
throw new IllegalArgumentException("未知的开关类型: " + code);
}
}