From 70c0ae989628e00fffd47e543acf90cf858878ba Mon Sep 17 00:00:00 2001 From: odboy Date: Mon, 9 Dec 2024 21:03:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=88=A0=E9=99=A4=E7=8E=AF=E5=A2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/odboy/rest/ConfigEnvController.java | 6 + .../cn/odboy/service/ConfigAppEnvService.java | 2 + .../cn/odboy/service/ConfigFileService.java | 2 + .../service/impl/ConfigAppEnvServiceImpl.java | 81 ++++-- .../service/impl/ConfigFileServiceImpl.java | 235 +++++++++--------- 5 files changed, 186 insertions(+), 140 deletions(-) diff --git a/kenaito-config-service/src/main/java/cn/odboy/rest/ConfigEnvController.java b/kenaito-config-service/src/main/java/cn/odboy/rest/ConfigEnvController.java index ee7d3f7..c7f83d7 100644 --- a/kenaito-config-service/src/main/java/cn/odboy/rest/ConfigEnvController.java +++ b/kenaito-config-service/src/main/java/cn/odboy/rest/ConfigEnvController.java @@ -37,4 +37,10 @@ public class ConfigEnvController { public ResponseEntity queryList(@Validated @RequestBody ConfigAppEnv args) { return new ResponseEntity<>(configAppEnvService.queryList(args), HttpStatus.OK); } + + @PostMapping("/remove") + public ResponseEntity remove(@Validated @RequestBody ConfigAppEnv args) { + configAppEnvService.remove(args); + return new ResponseEntity<>(HttpStatus.OK); + } } diff --git a/kenaito-config-service/src/main/java/cn/odboy/service/ConfigAppEnvService.java b/kenaito-config-service/src/main/java/cn/odboy/service/ConfigAppEnvService.java index b0b03a7..d319732 100644 --- a/kenaito-config-service/src/main/java/cn/odboy/service/ConfigAppEnvService.java +++ b/kenaito-config-service/src/main/java/cn/odboy/service/ConfigAppEnvService.java @@ -18,4 +18,6 @@ public interface ConfigAppEnvService extends IService { void create(ConfigAppEnv args); List queryList(ConfigAppEnv args); + + void remove(ConfigAppEnv args); } diff --git a/kenaito-config-service/src/main/java/cn/odboy/service/ConfigFileService.java b/kenaito-config-service/src/main/java/cn/odboy/service/ConfigFileService.java index d6b96bd..2d70df5 100644 --- a/kenaito-config-service/src/main/java/cn/odboy/service/ConfigFileService.java +++ b/kenaito-config-service/src/main/java/cn/odboy/service/ConfigFileService.java @@ -35,4 +35,6 @@ public interface ConfigFileService extends IService { String getContentById(ConfigFile args); void upload(Long appId, String envCode, MultipartFile file) throws Exception; + + List getByAppIdEnvCode(Long appId, String envCode); } diff --git a/kenaito-config-service/src/main/java/cn/odboy/service/impl/ConfigAppEnvServiceImpl.java b/kenaito-config-service/src/main/java/cn/odboy/service/impl/ConfigAppEnvServiceImpl.java index bc93f74..2a196dc 100644 --- a/kenaito-config-service/src/main/java/cn/odboy/service/impl/ConfigAppEnvServiceImpl.java +++ b/kenaito-config-service/src/main/java/cn/odboy/service/impl/ConfigAppEnvServiceImpl.java @@ -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 - 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 queryList(ConfigAppEnv args) { - return list(new LambdaQueryWrapper() - .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 queryList(ConfigAppEnv args) { + return list(new LambdaQueryWrapper().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() + .eq(ConfigAppEnv::getAppId, args.getAppId()) + .eq(ConfigAppEnv::getEnvCode, args.getEnvCode())); + List configFiles = + configFileService.getByAppIdEnvCode(args.getAppId(), args.getEnvCode()); + if (CollUtil.isNotEmpty(configFiles)) { + // delete from config_file + List 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); + } + } + } } diff --git a/kenaito-config-service/src/main/java/cn/odboy/service/impl/ConfigFileServiceImpl.java b/kenaito-config-service/src/main/java/cn/odboy/service/impl/ConfigFileServiceImpl.java index 8385089..ebcfff1 100644 --- a/kenaito-config-service/src/main/java/cn/odboy/service/impl/ConfigFileServiceImpl.java +++ b/kenaito-config-service/src/main/java/cn/odboy/service/impl/ConfigFileServiceImpl.java @@ -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 - implements ConfigFileService { - private final ConfigVersionService configVersionService; + implements ConfigFileService { + private final ConfigVersionService configVersionService; - @Override - public List getFileList(String env, String dataId) { - return getBaseMapper().selectInfoByEnvAndAppName(env, dataId); - } + @Override + public List getFileList(String env, String dataId) { + return getBaseMapper().selectInfoByEnvAndAppName(env, dataId); + } - @Override - public List queryInAppId(Long appId) { - return list(new LambdaQueryWrapper().in(ConfigFile::getAppId, appId)); - } + @Override + public List queryInAppId(Long appId) { + return list(new LambdaQueryWrapper().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 queryList(ConfigFile.QueryList args) { - return getBaseMapper().selectDetailByEnvAndAppId(args.getEnvCode(), args.getAppId()); - } + @Override + public List 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() - .eq(ConfigFile::getAppId, appId) - .eq(ConfigFile::getEnvCode, envCode) - .eq(ConfigFile::getFileName, fileName) - ); - } + @Override + public List getByAppIdEnvCode(Long appId, String envCode) { + return list( + new LambdaQueryWrapper() + .eq(ConfigFile::getAppId, appId) + .eq(ConfigFile::getEnvCode, envCode)); + } + + private ConfigFile getVersionBy(Long appId, String envCode, String fileName) { + return getOne( + new LambdaQueryWrapper() + .eq(ConfigFile::getAppId, appId) + .eq(ConfigFile::getEnvCode, envCode) + .eq(ConfigFile::getFileName, fileName)); + } }