From e4f51f2dec8dc9f9c4fdd14acb3f39498cc6a828 Mon Sep 17 00:00:00 2001 From: wangw <1594593906@qq.com> Date: Thu, 18 Dec 2025 20:33:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9E=9A=E4=B8=BE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application-dev.yml | 5 -- .../src/main/resources/application-dev.yml | 5 -- .../czg/account/service/ShopInfoService.java | 8 +-- .../czg/constants/SerializableFunction.java | 20 +++++++ .../com/czg/constants/ShopSwitchTypeEnum.java | 57 +++++++++++++++++++ .../service/impl/ShopInfoServiceImpl.java | 20 ++----- .../service/impl/GbWareServiceImpl.java | 8 +-- 7 files changed, 88 insertions(+), 35 deletions(-) create mode 100644 cash-common/cash-common-service/src/main/java/com/czg/constants/SerializableFunction.java create mode 100644 cash-common/cash-common-service/src/main/java/com/czg/constants/ShopSwitchTypeEnum.java diff --git a/cash-api/account-server/src/main/resources/application-dev.yml b/cash-api/account-server/src/main/resources/application-dev.yml index 9a37e2b4a..a07b7f122 100644 --- a/cash-api/account-server/src/main/resources/application-dev.yml +++ b/cash-api/account-server/src/main/resources/application-dev.yml @@ -29,11 +29,6 @@ dubbo: name: account-server qos-port: 22221 qos-enable: true - serialize-check-status: WARN - # 或者使用以下配置添加白名单 - serialize-allow-list: - - com.czg.service.market.service.impl.GbWareServiceImpl - - com.czg.service.account.service.impl.ShopInfoServiceImpl registry: address: nacos://121.40.109.122:8848 # Nacos 服务地址 group: server-dev diff --git a/cash-api/order-server/src/main/resources/application-dev.yml b/cash-api/order-server/src/main/resources/application-dev.yml index 62d99b846..a2d9fc027 100644 --- a/cash-api/order-server/src/main/resources/application-dev.yml +++ b/cash-api/order-server/src/main/resources/application-dev.yml @@ -29,11 +29,6 @@ spring: dubbo: application: name: order-server - serialize-check-status: WARN - # 或者使用以下配置添加白名单 - serialize-allow-list: - - com.czg.service.market.service.impl.GbWareServiceImpl - - com.czg.service.account.service.impl.ShopInfoServiceImpl qos-port: 22231 qos-enable: true registry: diff --git a/cash-common/cash-common-service/src/main/java/com/czg/account/service/ShopInfoService.java b/cash-common/cash-common-service/src/main/java/com/czg/account/service/ShopInfoService.java index ea1ea171b..468c552a1 100644 --- a/cash-common/cash-common-service/src/main/java/com/czg/account/service/ShopInfoService.java +++ b/cash-common/cash-common-service/src/main/java/com/czg/account/service/ShopInfoService.java @@ -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 { /** * 检测开关 * @param shopId 店铺id - * @param switchGetter ShopInfo的某列 开关 + * @param switchType ShopInfo的某列 开关 * @return true:开启 false:关闭 */ - boolean checkSwitch(Long shopId, Function switchGetter) throws ValidateException; + boolean checkSwitch(Long shopId, ShopSwitchTypeEnum switchType) throws ValidateException; Page get(PageDTO pageDTO, String shopName, Integer status, Integer isHeadShop); diff --git a/cash-common/cash-common-service/src/main/java/com/czg/constants/SerializableFunction.java b/cash-common/cash-common-service/src/main/java/com/czg/constants/SerializableFunction.java new file mode 100644 index 000000000..6c6f4642a --- /dev/null +++ b/cash-common/cash-common-service/src/main/java/com/czg/constants/SerializableFunction.java @@ -0,0 +1,20 @@ +package com.czg.constants; + +import java.io.Serial; +import java.io.Serializable; +import java.util.function.Function; + +/** + * 可序列化的函数式接口 + * 用于在Dubbo等需要序列化的场景中传递Lambda表达式 + * + * @param 输入类型 + * @param 输出类型 + * @author ww + */ +@FunctionalInterface +public interface SerializableFunction extends Function, Serializable { + // serialVersionUID 不能是private + @Serial + long serialVersionUID = 1L; +} \ No newline at end of file diff --git a/cash-common/cash-common-service/src/main/java/com/czg/constants/ShopSwitchTypeEnum.java b/cash-common/cash-common-service/src/main/java/com/czg/constants/ShopSwitchTypeEnum.java new file mode 100644 index 000000000..2cc498a8e --- /dev/null +++ b/cash-common/cash-common-service/src/main/java/com/czg/constants/ShopSwitchTypeEnum.java @@ -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 getter; + + ShopSwitchTypeEnum(String code, String description, + SerializableFunction 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 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); + } +} diff --git a/cash-service/account-service/src/main/java/com/czg/service/account/service/impl/ShopInfoServiceImpl.java b/cash-service/account-service/src/main/java/com/czg/service/account/service/impl/ShopInfoServiceImpl.java index 10214d758..aa2e0aeae 100644 --- a/cash-service/account-service/src/main/java/com/czg/service/account/service/impl/ShopInfoServiceImpl.java +++ b/cash-service/account-service/src/main/java/com/czg/service/account/service/impl/ShopInfoServiceImpl.java @@ -3,7 +3,6 @@ package com.czg.service.account.service.impl; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.core.exceptions.ValidateException; -import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.crypto.SecureUtil; import com.czg.account.dto.PageDTO; @@ -14,6 +13,7 @@ import com.czg.account.enums.ShopTypeEnum; import com.czg.account.service.*; import com.czg.config.RabbitPublisher; import com.czg.config.RedisCst; +import com.czg.constants.ShopSwitchTypeEnum; import com.czg.enums.StatusEnum; import com.czg.enums.YesNoEnum; import com.czg.exception.CzgException; @@ -32,10 +32,6 @@ import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.mybatisflex.core.paginate.Page; import com.mybatisflex.core.query.QueryWrapper; -import com.mybatisflex.core.table.TableInfo; -import com.mybatisflex.core.table.TableInfoFactory; -import com.mybatisflex.core.util.LambdaGetter; -import com.mybatisflex.core.util.LambdaUtil; import com.mybatisflex.spring.service.impl.ServiceImpl; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; @@ -47,14 +43,12 @@ import org.springframework.cache.annotation.Cacheable; import org.springframework.transaction.annotation.Transactional; import java.io.Serializable; -import java.lang.invoke.SerializedLambda; import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.function.Function; import java.util.stream.Collectors; /** @@ -63,7 +57,7 @@ import java.util.stream.Collectors; @Slf4j @DubboService @CacheConfig(cacheNames = "shopInfo") -public class ShopInfoServiceImpl extends ServiceImpl implements ShopInfoService, Serializable { +public class ShopInfoServiceImpl extends ServiceImpl implements ShopInfoService{ @Resource private RabbitPublisher rabbitPublisher; @Resource @@ -101,17 +95,11 @@ public class ShopInfoServiceImpl extends ServiceImpl i } @Override - public boolean checkSwitch(Long shopId, Function switchGetter) throws ValidateException { + public boolean checkSwitch(Long shopId, ShopSwitchTypeEnum switchType) throws ValidateException { AssertUtil.isNull(shopId, "店铺ID不能为空"); ShopInfo shopInfo = getById(shopId); AssertUtil.isNull(shopInfo, "店铺不存在"); - T switchValue = switchGetter.apply(shopInfo); - - if (switchValue == null) { - return false; - } - - return convertToBoolean(switchValue); + return switchType.getValue(shopInfo); } private ShopInfo getShopInfo(Long shopId) { diff --git a/cash-service/market-service/src/main/java/com/czg/service/market/service/impl/GbWareServiceImpl.java b/cash-service/market-service/src/main/java/com/czg/service/market/service/impl/GbWareServiceImpl.java index f73c5f7df..33f9b8b4b 100644 --- a/cash-service/market-service/src/main/java/com/czg/service/market/service/impl/GbWareServiceImpl.java +++ b/cash-service/market-service/src/main/java/com/czg/service/market/service/impl/GbWareServiceImpl.java @@ -2,8 +2,8 @@ package com.czg.service.market.service.impl; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.util.StrUtil; -import com.czg.account.entity.ShopInfo; import com.czg.account.service.ShopInfoService; +import com.czg.constants.ShopSwitchTypeEnum; import com.czg.market.dto.GbWareDTO; import com.czg.market.dto.GbWareQueryParamDTO; import com.czg.market.entity.GbWare; @@ -17,8 +17,6 @@ import com.mybatisflex.spring.service.impl.ServiceImpl; import org.apache.dubbo.config.annotation.DubboReference; import org.springframework.stereotype.Service; -import java.io.Serializable; - /** * 拼团商品 服务层实现。 * @@ -26,14 +24,14 @@ import java.io.Serializable; * @since 2025-12-15 */ @Service -public class GbWareServiceImpl extends ServiceImpl implements GbWareService, Serializable { +public class GbWareServiceImpl extends ServiceImpl implements GbWareService { @DubboReference private ShopInfoService shopInfoService; @Override public Page getGbWarePage(GbWareQueryParamDTO param, Long shopId) { - if (!shopInfoService.checkSwitch(shopId, ShopInfo::getIsGroupBuy)) { + if (!shopInfoService.checkSwitch(shopId, ShopSwitchTypeEnum.GROUP_BUY)) { return new Page<>(); } Long mainShopId = shopInfoService.getMainIdByShopId(shopId);