diff --git a/common/src/main/java/com/github/dynamic/threadpool/common/toolkit/ConditionUtil.java b/common/src/main/java/com/github/dynamic/threadpool/common/toolkit/ConditionUtil.java new file mode 100644 index 00000000..b016647b --- /dev/null +++ b/common/src/main/java/com/github/dynamic/threadpool/common/toolkit/ConditionUtil.java @@ -0,0 +1,29 @@ +package com.github.dynamic.threadpool.common.toolkit; + +/** + * Condition util. + * + * @author chen.ma + * @date 2021/11/6 22:40 + */ +public class ConditionUtil { + + public static void condition(boolean condition, NoArgsConsumer trueConsumer, NoArgsConsumer falseConsumer) { + if (condition) { + trueConsumer.accept(); + } else { + falseConsumer.accept(); + } + } + + + public interface NoArgsConsumer { + + /** + * 执行方法 + */ + void accept(); + + } + +} diff --git a/config/src/main/java/com/github/dynamic/threadpool/config/service/biz/impl/ConfigServiceImpl.java b/config/src/main/java/com/github/dynamic/threadpool/config/service/biz/impl/ConfigServiceImpl.java index 55e0aa79..372c6b24 100644 --- a/config/src/main/java/com/github/dynamic/threadpool/config/service/biz/impl/ConfigServiceImpl.java +++ b/config/src/main/java/com/github/dynamic/threadpool/config/service/biz/impl/ConfigServiceImpl.java @@ -1,21 +1,26 @@ package com.github.dynamic.threadpool.config.service.biz.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.toolkit.SqlHelper; +import com.github.dynamic.threadpool.common.toolkit.ConditionUtil; import com.github.dynamic.threadpool.common.toolkit.ContentUtil; import com.github.dynamic.threadpool.common.toolkit.Md5Util; import com.github.dynamic.threadpool.config.event.LocalDataChangeEvent; import com.github.dynamic.threadpool.config.mapper.ConfigInfoMapper; import com.github.dynamic.threadpool.config.model.ConfigAllInfo; +import com.github.dynamic.threadpool.config.model.ConfigInfoBase; import com.github.dynamic.threadpool.config.service.ConfigChangePublisher; import com.github.dynamic.threadpool.config.service.biz.ConfigService; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import java.util.Optional; + /** * Config service impl. * @@ -35,19 +40,31 @@ public class ConfigServiceImpl implements ConfigService { .eq(!StringUtils.isBlank(tpId), ConfigAllInfo::getTpId, tpId) .eq(!StringUtils.isBlank(itemId), ConfigAllInfo::getItemId, itemId) .eq(!StringUtils.isBlank(tenantId), ConfigAllInfo::getTenantId, tenantId); + ConfigAllInfo configAllInfo = configInfoMapper.selectOne(wrapper); return configAllInfo; } @Override - public void insertOrUpdate(String identify, ConfigAllInfo configAllInfo) { + public void insertOrUpdate(String identify, ConfigAllInfo configInfo) { + LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(ConfigAllInfo.class) + .eq(ConfigAllInfo::getTenantId, configInfo.getTenantId()) + .eq(ConfigInfoBase::getItemId, configInfo.getItemId()) + .eq(ConfigInfoBase::getTpId, configInfo.getTpId()); + ConfigAllInfo existConfig = configInfoMapper.selectOne(queryWrapper); + try { - addConfigInfo(configAllInfo); + ConditionUtil + .condition( + existConfig == null, + () -> addConfigInfo(configInfo), + () -> updateConfigInfo(configInfo) + ); } catch (Exception ex) { - updateConfigInfo(configAllInfo); + updateConfigInfo(configInfo); } - ConfigChangePublisher.notifyConfigChange(new LocalDataChangeEvent(identify, ContentUtil.getGroupKey(configAllInfo))); + ConfigChangePublisher.notifyConfigChange(new LocalDataChangeEvent(identify, ContentUtil.getGroupKey(configInfo))); } private Integer addConfigInfo(ConfigAllInfo config) { @@ -61,6 +78,7 @@ public class ConfigServiceImpl implements ConfigService { log.error("[db-error] message :: {}", ex.getMessage(), ex); throw ex; } + return null; }