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) { public ResponseEntity<Object> queryList(@Validated @RequestBody ConfigAppEnv args) {
return new ResponseEntity<>(configAppEnvService.queryList(args), HttpStatus.OK); 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); void create(ConfigAppEnv args);
List<SelectOption> queryList(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); String getContentById(ConfigFile args);
void upload(Long appId, String envCode, MultipartFile file) throws Exception; 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; package cn.odboy.service.impl;
import cn.hutool.core.util.StrUtil;
import cn.odboy.base.model.SelectOption; import cn.odboy.base.model.SelectOption;
import cn.odboy.domain.ConfigAppEnv; import cn.odboy.domain.ConfigAppEnv;
import cn.odboy.domain.ConfigFile;
import cn.odboy.infra.exception.BadRequestException;
import cn.odboy.mapper.ConfigAppEnvMapper; import cn.odboy.mapper.ConfigAppEnvMapper;
import cn.odboy.service.ConfigAppEnvService; 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.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; 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 @Service
@RequiredArgsConstructor @RequiredArgsConstructor
public class ConfigAppEnvServiceImpl extends ServiceImpl<ConfigAppEnvMapper, ConfigAppEnv> public class ConfigAppEnvServiceImpl extends ServiceImpl<ConfigAppEnvMapper, ConfigAppEnv>
implements ConfigAppEnvService { implements ConfigAppEnvService {
@Override private final ConfigFileService configFileService;
@Transactional(rollbackFor = Exception.class) private final ConfigVersionService configVersionService;
public void create(ConfigAppEnv args) {
try {
save(args);
} catch (Exception e) {
log.error("新增环境失败", e);
throw new RuntimeException("环境已存在, 请确认后再试");
}
}
@Override @Override
public List<SelectOption> queryList(ConfigAppEnv args) { @Transactional(rollbackFor = Exception.class)
return list(new LambdaQueryWrapper<ConfigAppEnv>() public void create(ConfigAppEnv args) {
.eq(ConfigAppEnv::getAppId, args.getAppId()) try {
).stream() save(args);
.map(m -> SelectOption.builder() } catch (Exception e) {
.label(m.getEnvCode()) log.error("新增环境失败", e);
.value(m.getEnvCode()) throw new RuntimeException("环境已存在, 请确认后再试");
.build())
.collect(Collectors.toList());
} }
}
@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 cn.odboy.service.ConfigVersionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; 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 lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; 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 @Service
@RequiredArgsConstructor @RequiredArgsConstructor
public class ConfigFileServiceImpl extends ServiceImpl<ConfigFileMapper, ConfigFile> public class ConfigFileServiceImpl extends ServiceImpl<ConfigFileMapper, ConfigFile>
implements ConfigFileService { implements ConfigFileService {
private final ConfigVersionService configVersionService; private final ConfigVersionService configVersionService;
@Override @Override
public List<ConfigFileInfo> getFileList(String env, String dataId) { public List<ConfigFileInfo> getFileList(String env, String dataId) {
return getBaseMapper().selectInfoByEnvAndAppName(env, dataId); return getBaseMapper().selectInfoByEnvAndAppName(env, dataId);
} }
@Override @Override
public List<ConfigFile> queryInAppId(Long appId) { public List<ConfigFile> queryInAppId(Long appId) {
return list(new LambdaQueryWrapper<ConfigFile>().in(ConfigFile::getAppId, appId)); return list(new LambdaQueryWrapper<ConfigFile>().in(ConfigFile::getAppId, appId));
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void create(ConfigFile.CreateArgs args) throws Exception { public void create(ConfigFile.CreateArgs args) throws Exception {
MultipartFile file = args.getFile(); MultipartFile file = args.getFile();
Long version = 1L; Long version = 1L;
ConfigFile configFile = new ConfigFile(); ConfigFile configFile = new ConfigFile();
configFile.setEnvCode(args.getEnv()); configFile.setEnvCode(args.getEnv());
configFile.setAppId(args.getAppId()); configFile.setAppId(args.getAppId());
configFile.setFileName(file.getOriginalFilename()); configFile.setFileName(file.getOriginalFilename());
configFile.setVersion(version); configFile.setVersion(version);
save(configFile); save(configFile);
ConfigVersion configVersion = new ConfigVersion(); ConfigVersion configVersion = new ConfigVersion();
configVersion.setFileId(configFile.getId()); configVersion.setFileId(configFile.getId());
configVersion.setFileType(FileUtil.getSuffix(file.getOriginalFilename())); configVersion.setFileType(FileUtil.getSuffix(file.getOriginalFilename()));
configVersion.setFileContent(StrUtil.str(file.getBytes(), StandardCharsets.UTF_8)); configVersion.setFileContent(StrUtil.str(file.getBytes(), StandardCharsets.UTF_8));
configVersion.setVersion(version); configVersion.setVersion(version);
configVersionService.save(configVersion); configVersionService.save(configVersion);
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void remove(ConfigFile.RemoveArgs args) { public void remove(ConfigFile.RemoveArgs args) {
removeById(args.getId()); removeById(args.getId());
configVersionService.removeBatchByFileId(args.getId()); configVersionService.removeBatchByFileId(args.getId());
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void modifyFileContent(ConfigFile.ModifyFileContentArgs args) { public void modifyFileContent(ConfigFile.ModifyFileContentArgs args) {
String originConfigFileContent = getBaseMapper().selectContentByFileId(args.getId()); String originConfigFileContent = getBaseMapper().selectContentByFileId(args.getId());
if (StrUtil.isNotBlank(originConfigFileContent)) { if (StrUtil.isNotBlank(originConfigFileContent)) {
originConfigFileContent = originConfigFileContent originConfigFileContent = originConfigFileContent.replaceAll("\r", "").replaceAll("\n", "");
.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);
} }
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 @Override
public List<ConfigFile.QueryList> queryList(ConfigFile.QueryList args) { public List<ConfigFile.QueryList> queryList(ConfigFile.QueryList args) {
return getBaseMapper().selectDetailByEnvAndAppId(args.getEnvCode(), args.getAppId()); return getBaseMapper().selectDetailByEnvAndAppId(args.getEnvCode(), args.getAppId());
} }
@Override @Override
public String getContentById(ConfigFile args) { public String getContentById(ConfigFile args) {
if (args.getId() == null) { if (args.getId() == null) {
throw new BadRequestException("id必填"); throw new BadRequestException("id必填");
}
return getBaseMapper().selectContentByFileId(args.getId());
} }
return getBaseMapper().selectContentByFileId(args.getId());
}
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void upload(Long appId, String envCode, MultipartFile file) throws IOException { public void upload(Long appId, String envCode, MultipartFile file) throws IOException {
if (appId == null) { if (appId == null) {
throw new BadRequestException("appId必填"); 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);
} }
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) { @Override
return getOne(new LambdaQueryWrapper<ConfigFile>() public List<ConfigFile> getByAppIdEnvCode(Long appId, String envCode) {
.eq(ConfigFile::getAppId, appId) return list(
.eq(ConfigFile::getEnvCode, envCode) new LambdaQueryWrapper<ConfigFile>()
.eq(ConfigFile::getFileName, fileName) .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));
}
} }