通用校验

This commit is contained in:
2025-12-18 19:53:36 +08:00
parent 3e40634e9e
commit f7c28d93ed
5 changed files with 11 additions and 23 deletions

View File

@@ -11,14 +11,9 @@ import com.czg.service.account.mapper.ShopConfigMapper;
import com.czg.service.account.mapper.ShopInfoMapper;
import com.czg.utils.PageUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.LambdaGetter;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

View File

@@ -54,6 +54,7 @@ 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;
/**
@@ -100,17 +101,17 @@ public class ShopInfoServiceImpl extends ServiceImpl<ShopInfoMapper, ShopInfo> i
}
@Override
public <T> boolean checkSwitch(Long shopId, LambdaGetter<T> column) throws ValidateException {
public <T> boolean checkSwitch(Long shopId, Function<ShopInfo, T> switchGetter) throws ValidateException {
AssertUtil.isNull(shopId, "店铺ID不能为空");
ShopInfo shopInfo = getById(shopId);
AssertUtil.isNull(shopInfo, "店铺不存在");
T switchValue = switchGetter.apply(shopInfo);
String fieldName = LambdaUtil.getFieldName(column);
Object fieldValue = ReflectUtil.getFieldValue(shopInfo, fieldName);
if (fieldValue == null) {
if (switchValue == null) {
return false;
}
return convertToBoolean(fieldValue, fieldName);
return convertToBoolean(switchValue);
}
private ShopInfo getShopInfo(Long shopId) {
@@ -496,7 +497,7 @@ public class ShopInfoServiceImpl extends ServiceImpl<ShopInfoMapper, ShopInfo> i
}
private boolean convertToBoolean(Object fieldValue, String fieldName) throws ValidateException {
private boolean convertToBoolean(Object fieldValue) throws ValidateException {
if (fieldValue instanceof Boolean) {
// Boolean 类型直接返回
return (Boolean) fieldValue;
@@ -511,7 +512,7 @@ public class ShopInfoServiceImpl extends ServiceImpl<ShopInfoMapper, ShopInfo> i
}
} else {
// 不支持的类型抛异常
throw new ValidateException("字段[" + fieldName + "]类型不支持仅支持Boolean/Integer当前类型" + fieldValue.getClass().getSimpleName());
throw new ValidateException("字段类型不支持仅支持Boolean/Integer当前类型" + fieldValue.getClass().getSimpleName());
}
}
}