通用校验
This commit is contained in:
@@ -2,6 +2,8 @@ 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;
|
||||
@@ -30,6 +32,10 @@ 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;
|
||||
@@ -41,6 +47,7 @@ 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;
|
||||
@@ -84,7 +91,7 @@ public class ShopInfoServiceImpl extends ServiceImpl<ShopInfoMapper, ShopInfo> i
|
||||
@Cacheable(key = "#id")
|
||||
public ShopInfo getById(Serializable id) throws CzgException {
|
||||
ShopInfo shopInfo = super.getById(id);
|
||||
if(shopInfo == null){
|
||||
if (shopInfo == null) {
|
||||
throw new CzgException("店铺不存在");
|
||||
}
|
||||
ShopConfig shopConfig = shopConfigService.getById(shopInfo.getId());
|
||||
@@ -92,6 +99,20 @@ public class ShopInfoServiceImpl extends ServiceImpl<ShopInfoMapper, ShopInfo> i
|
||||
return shopInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> boolean checkSwitch(Long shopId, LambdaGetter<T> column) throws ValidateException {
|
||||
AssertUtil.isNull(shopId, "店铺ID不能为空");
|
||||
ShopInfo shopInfo = getById(shopId);
|
||||
AssertUtil.isNull(shopInfo, "店铺不存在");
|
||||
|
||||
String fieldName = LambdaUtil.getFieldName(column);
|
||||
Object fieldValue = ReflectUtil.getFieldValue(shopInfo, fieldName);
|
||||
if (fieldValue == null) {
|
||||
return false;
|
||||
}
|
||||
return convertToBoolean(fieldValue, fieldName);
|
||||
}
|
||||
|
||||
private ShopInfo getShopInfo(Long shopId) {
|
||||
ShopInfo shopInfo = getById(shopId);
|
||||
if (shopInfo == null) {
|
||||
@@ -446,7 +467,7 @@ public class ShopInfoServiceImpl extends ServiceImpl<ShopInfoMapper, ShopInfo> i
|
||||
q1.eq(ShopInfo::getId, mainShopId);
|
||||
});
|
||||
});
|
||||
}else {
|
||||
} else {
|
||||
shopIdList.add(mainShopId);
|
||||
queryWrapper.in(ShopInfo::getId, shopIdList);
|
||||
}
|
||||
@@ -473,4 +494,24 @@ public class ShopInfoServiceImpl extends ServiceImpl<ShopInfoMapper, ShopInfo> i
|
||||
public List<Long> getShopIdList() {
|
||||
return mapper.getShopIdList();
|
||||
}
|
||||
|
||||
|
||||
private boolean convertToBoolean(Object fieldValue, String fieldName) throws ValidateException {
|
||||
if (fieldValue instanceof Boolean) {
|
||||
// Boolean 类型直接返回
|
||||
return (Boolean) fieldValue;
|
||||
} else if (fieldValue instanceof Integer intValue) {
|
||||
// Integer 类型按 1/0 规则转换
|
||||
if (intValue == 1) {
|
||||
return true;
|
||||
} else if (intValue == 0) {
|
||||
return false;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// 不支持的类型抛异常
|
||||
throw new ValidateException("字段[" + fieldName + "]类型不支持,仅支持Boolean/Integer,当前类型:" + fieldValue.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user