feat: 删除环境

This commit is contained in:
骑着蜗牛追导弹 2024-12-09 21:03:03 +08:00
parent f65f995cc5
commit 70c0ae9896
5 changed files with 186 additions and 140 deletions

View File

@ -37,4 +37,10 @@ public class ConfigEnvController {
public ResponseEntity<Object> queryList(@Validated @RequestBody ConfigAppEnv args) {
return new ResponseEntity<>(configAppEnvService.queryList(args), HttpStatus.OK);
}
@PostMapping("/remove")
public ResponseEntity<Object> remove(@Validated @RequestBody ConfigAppEnv args) {
configAppEnvService.remove(args);
return new ResponseEntity<>(HttpStatus.OK);
}
}

View File

@ -18,4 +18,6 @@ public interface ConfigAppEnvService extends IService<ConfigAppEnv> {
void create(ConfigAppEnv args);
List<SelectOption> queryList(ConfigAppEnv args);
void remove(ConfigAppEnv args);
}

View File

@ -35,4 +35,6 @@ public interface ConfigFileService extends IService<ConfigFile> {
String getContentById(ConfigFile args);
void upload(Long appId, String envCode, MultipartFile file) throws Exception;
List<ConfigFile> getByAppIdEnvCode(Long appId, String envCode);
}

View File

@ -1,18 +1,23 @@
package cn.odboy.service.impl;
import cn.hutool.core.util.StrUtil;
import cn.odboy.base.model.SelectOption;
import cn.odboy.domain.ConfigAppEnv;
import cn.odboy.domain.ConfigFile;
import cn.odboy.infra.exception.BadRequestException;
import cn.odboy.mapper.ConfigAppEnvMapper;
import cn.odboy.service.ConfigAppEnvService;
import cn.odboy.service.ConfigFileService;
import cn.odboy.service.ConfigVersionService;
import cn.odboy.util.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.stream.Collectors;
/**
* 配置环境 服务实现类
*
@ -22,27 +27,55 @@ import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
public class ConfigAppEnvServiceImpl extends ServiceImpl<ConfigAppEnvMapper, ConfigAppEnv>
implements ConfigAppEnvService {
@Override
@Transactional(rollbackFor = Exception.class)
public void create(ConfigAppEnv args) {
try {
save(args);
} catch (Exception e) {
log.error("新增环境失败", e);
throw new RuntimeException("环境已存在, 请确认后再试");
}
}
implements ConfigAppEnvService {
private final ConfigFileService configFileService;
private final ConfigVersionService configVersionService;
@Override
public List<SelectOption> queryList(ConfigAppEnv args) {
return list(new LambdaQueryWrapper<ConfigAppEnv>()
.eq(ConfigAppEnv::getAppId, args.getAppId())
).stream()
.map(m -> SelectOption.builder()
.label(m.getEnvCode())
.value(m.getEnvCode())
.build())
.collect(Collectors.toList());
@Override
@Transactional(rollbackFor = Exception.class)
public void create(ConfigAppEnv args) {
try {
save(args);
} catch (Exception e) {
log.error("新增环境失败", e);
throw new RuntimeException("环境已存在, 请确认后再试");
}
}
@Override
public List<SelectOption> queryList(ConfigAppEnv args) {
return list(new LambdaQueryWrapper<ConfigAppEnv>().eq(ConfigAppEnv::getAppId, args.getAppId()))
.stream()
.map(m -> SelectOption.builder().label(m.getEnvCode()).value(m.getEnvCode()).build())
.collect(Collectors.toList());
}
@Override
@Transactional(rollbackFor = Exception.class)
public void remove(ConfigAppEnv args) {
if (args.getAppId() == null) {
throw new BadRequestException("appId必填");
}
if (StrUtil.isBlank(args.getEnvCode())) {
throw new BadRequestException("envCode必填");
}
// delete from config_app_env
remove(
new LambdaQueryWrapper<ConfigAppEnv>()
.eq(ConfigAppEnv::getAppId, args.getAppId())
.eq(ConfigAppEnv::getEnvCode, args.getEnvCode()));
List<ConfigFile> configFiles =
configFileService.getByAppIdEnvCode(args.getAppId(), args.getEnvCode());
if (CollUtil.isNotEmpty(configFiles)) {
// delete from config_file
List<Long> configFileId =
configFiles.stream().map(ConfigFile::getId).collect(Collectors.toList());
configFileService.removeBatchByIds(configFileId);
Long fileId = configFileId.stream().findFirst().orElse(null);
if (fileId != null) {
// delete from config_version
configVersionService.removeBatchByFileId(fileId);
}
}
}
}

View File

@ -11,15 +11,14 @@ import cn.odboy.service.ConfigFileService;
import cn.odboy.service.ConfigVersionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
/**
* 配置文件 服务实现类
*
@ -29,128 +28,132 @@ import java.util.List;
@Service
@RequiredArgsConstructor
public class ConfigFileServiceImpl extends ServiceImpl<ConfigFileMapper, ConfigFile>
implements ConfigFileService {
private final ConfigVersionService configVersionService;
implements ConfigFileService {
private final ConfigVersionService configVersionService;
@Override
public List<ConfigFileInfo> getFileList(String env, String dataId) {
return getBaseMapper().selectInfoByEnvAndAppName(env, dataId);
}
@Override
public List<ConfigFileInfo> getFileList(String env, String dataId) {
return getBaseMapper().selectInfoByEnvAndAppName(env, dataId);
}
@Override
public List<ConfigFile> queryInAppId(Long appId) {
return list(new LambdaQueryWrapper<ConfigFile>().in(ConfigFile::getAppId, appId));
}
@Override
public List<ConfigFile> queryInAppId(Long appId) {
return list(new LambdaQueryWrapper<ConfigFile>().in(ConfigFile::getAppId, appId));
}
@Override
@Transactional(rollbackFor = Exception.class)
public void create(ConfigFile.CreateArgs args) throws Exception {
MultipartFile file = args.getFile();
Long version = 1L;
ConfigFile configFile = new ConfigFile();
configFile.setEnvCode(args.getEnv());
configFile.setAppId(args.getAppId());
configFile.setFileName(file.getOriginalFilename());
configFile.setVersion(version);
save(configFile);
ConfigVersion configVersion = new ConfigVersion();
configVersion.setFileId(configFile.getId());
configVersion.setFileType(FileUtil.getSuffix(file.getOriginalFilename()));
configVersion.setFileContent(StrUtil.str(file.getBytes(), StandardCharsets.UTF_8));
configVersion.setVersion(version);
configVersionService.save(configVersion);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void create(ConfigFile.CreateArgs args) throws Exception {
MultipartFile file = args.getFile();
Long version = 1L;
ConfigFile configFile = new ConfigFile();
configFile.setEnvCode(args.getEnv());
configFile.setAppId(args.getAppId());
configFile.setFileName(file.getOriginalFilename());
configFile.setVersion(version);
save(configFile);
ConfigVersion configVersion = new ConfigVersion();
configVersion.setFileId(configFile.getId());
configVersion.setFileType(FileUtil.getSuffix(file.getOriginalFilename()));
configVersion.setFileContent(StrUtil.str(file.getBytes(), StandardCharsets.UTF_8));
configVersion.setVersion(version);
configVersionService.save(configVersion);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void remove(ConfigFile.RemoveArgs args) {
removeById(args.getId());
configVersionService.removeBatchByFileId(args.getId());
}
@Override
@Transactional(rollbackFor = Exception.class)
public void remove(ConfigFile.RemoveArgs args) {
removeById(args.getId());
configVersionService.removeBatchByFileId(args.getId());
}
@Override
@Transactional(rollbackFor = Exception.class)
public void modifyFileContent(ConfigFile.ModifyFileContentArgs args) {
String originConfigFileContent = getBaseMapper().selectContentByFileId(args.getId());
if (StrUtil.isNotBlank(originConfigFileContent)) {
originConfigFileContent = originConfigFileContent
.replaceAll("\r", "")
.replaceAll("\n", "");
}
String commitContent = args.getFileContent()
.replaceAll("\r", "")
.replaceAll("\n", "");
if (originConfigFileContent.equals(commitContent)) {
throw new BadRequestException("未变更配置,无需提交");
}
ConfigFile originConfigFile = getById(args.getId());
long newVersion = originConfigFile.getVersion() + 1;
ConfigFile updVersion = new ConfigFile();
updVersion.setId(originConfigFile.getId());
updVersion.setVersion(newVersion);
updateById(updVersion);
ConfigVersion lastConfigVersion = new ConfigVersion();
lastConfigVersion.setFileId(originConfigFile.getId());
lastConfigVersion.setFileContent(args.getFileContent());
lastConfigVersion.setFileType(FileUtil.getSuffix(originConfigFile.getFileName()));
lastConfigVersion.setVersion(newVersion);
configVersionService.save(lastConfigVersion);
@Override
@Transactional(rollbackFor = Exception.class)
public void modifyFileContent(ConfigFile.ModifyFileContentArgs args) {
String originConfigFileContent = getBaseMapper().selectContentByFileId(args.getId());
if (StrUtil.isNotBlank(originConfigFileContent)) {
originConfigFileContent = originConfigFileContent.replaceAll("\r", "").replaceAll("\n", "");
}
String commitContent = args.getFileContent().replaceAll("\r", "").replaceAll("\n", "");
if (originConfigFileContent.equals(commitContent)) {
throw new BadRequestException("未变更配置,无需提交");
}
ConfigFile originConfigFile = getById(args.getId());
long newVersion = originConfigFile.getVersion() + 1;
ConfigFile updVersion = new ConfigFile();
updVersion.setId(originConfigFile.getId());
updVersion.setVersion(newVersion);
updateById(updVersion);
ConfigVersion lastConfigVersion = new ConfigVersion();
lastConfigVersion.setFileId(originConfigFile.getId());
lastConfigVersion.setFileContent(args.getFileContent());
lastConfigVersion.setFileType(FileUtil.getSuffix(originConfigFile.getFileName()));
lastConfigVersion.setVersion(newVersion);
configVersionService.save(lastConfigVersion);
}
@Override
public List<ConfigFile.QueryList> queryList(ConfigFile.QueryList args) {
return getBaseMapper().selectDetailByEnvAndAppId(args.getEnvCode(), args.getAppId());
}
@Override
public List<ConfigFile.QueryList> queryList(ConfigFile.QueryList args) {
return getBaseMapper().selectDetailByEnvAndAppId(args.getEnvCode(), args.getAppId());
}
@Override
public String getContentById(ConfigFile args) {
if (args.getId() == null) {
throw new BadRequestException("id必填");
}
return getBaseMapper().selectContentByFileId(args.getId());
@Override
public String getContentById(ConfigFile args) {
if (args.getId() == null) {
throw new BadRequestException("id必填");
}
return getBaseMapper().selectContentByFileId(args.getId());
}
@Override
@Transactional(rollbackFor = Exception.class)
public void upload(Long appId, String envCode, MultipartFile file) throws IOException {
if (appId == null) {
throw new BadRequestException("appId必填");
}
if (StrUtil.isBlank(envCode)) {
throw new BadRequestException("envCode必填");
}
if (file == null) {
throw new BadRequestException("file必填");
}
ConfigFile oldConfigFile = getVersionBy(appId, envCode, file.getOriginalFilename());
ConfigFile newConfigFile = new ConfigFile();
newConfigFile.setAppId(appId);
newConfigFile.setEnvCode(envCode);
newConfigFile.setFileName(file.getOriginalFilename());
long version;
if (oldConfigFile == null) {
version = 1L;
} else if (oldConfigFile.getVersion() == null) {
version = 1L;
} else {
version = oldConfigFile.getVersion();
}
long newVersion = version + 1;
newConfigFile.setVersion(newVersion);
save(newConfigFile);
ConfigVersion newConfigVersion = new ConfigVersion();
newConfigVersion.setFileId(newConfigFile.getId());
newConfigVersion.setFileContent(StrUtil.str(file.getBytes(), StandardCharsets.UTF_8));
newConfigVersion.setFileType(FileUtil.getSuffix(file.getOriginalFilename()));
newConfigVersion.setVersion(newVersion);
configVersionService.save(newConfigVersion);
@Override
@Transactional(rollbackFor = Exception.class)
public void upload(Long appId, String envCode, MultipartFile file) throws IOException {
if (appId == null) {
throw new BadRequestException("appId必填");
}
if (StrUtil.isBlank(envCode)) {
throw new BadRequestException("envCode必填");
}
if (file == null) {
throw new BadRequestException("file必填");
}
ConfigFile oldConfigFile = getVersionBy(appId, envCode, file.getOriginalFilename());
ConfigFile newConfigFile = new ConfigFile();
newConfigFile.setAppId(appId);
newConfigFile.setEnvCode(envCode);
newConfigFile.setFileName(file.getOriginalFilename());
long version;
if (oldConfigFile == null) {
version = 1L;
} else if (oldConfigFile.getVersion() == null) {
version = 1L;
} else {
version = oldConfigFile.getVersion();
}
long newVersion = version + 1;
newConfigFile.setVersion(newVersion);
save(newConfigFile);
ConfigVersion newConfigVersion = new ConfigVersion();
newConfigVersion.setFileId(newConfigFile.getId());
newConfigVersion.setFileContent(StrUtil.str(file.getBytes(), StandardCharsets.UTF_8));
newConfigVersion.setFileType(FileUtil.getSuffix(file.getOriginalFilename()));
newConfigVersion.setVersion(newVersion);
configVersionService.save(newConfigVersion);
}
private ConfigFile getVersionBy(Long appId, String envCode, String fileName) {
return getOne(new LambdaQueryWrapper<ConfigFile>()
.eq(ConfigFile::getAppId, appId)
.eq(ConfigFile::getEnvCode, envCode)
.eq(ConfigFile::getFileName, fileName)
);
}
@Override
public List<ConfigFile> getByAppIdEnvCode(Long appId, String envCode) {
return list(
new LambdaQueryWrapper<ConfigFile>()
.eq(ConfigFile::getAppId, appId)
.eq(ConfigFile::getEnvCode, envCode));
}
private ConfigFile getVersionBy(Long appId, String envCode, String fileName) {
return getOne(
new LambdaQueryWrapper<ConfigFile>()
.eq(ConfigFile::getAppId, appId)
.eq(ConfigFile::getEnvCode, envCode)
.eq(ConfigFile::getFileName, fileName));
}
}