diff --git a/kenaito-config-service/src/main/java/cn/odboy/constant/RegexConst.java b/kenaito-config-service/src/main/java/cn/odboy/constant/RegexConst.java index 6646876..1daf560 100644 --- a/kenaito-config-service/src/main/java/cn/odboy/constant/RegexConst.java +++ b/kenaito-config-service/src/main/java/cn/odboy/constant/RegexConst.java @@ -7,8 +7,9 @@ package cn.odboy.constant; * @date 2024-09-13 */ public interface RegexConst { - // 正则表达式,用于匹配中国大陆手机号码 - String PHONE_NUMBER = "^1[3-9]\\d{9}$"; - // 小写字母和下划线 - String LOW_CHAR_LINE = "^[a-z_]+$"; + /** 正则表达式,用于匹配中国大陆手机号码 */ + String PHONE_NUMBER = "^1[3-9]\\d{9}$"; + + /** 小写字母和下划线 */ + String LOW_CHAR_LINE = "^[a-z_]+$"; } diff --git a/kenaito-config-service/src/main/java/cn/odboy/domain/ConfigApp.java b/kenaito-config-service/src/main/java/cn/odboy/domain/ConfigApp.java index bc3d251..6c05f46 100644 --- a/kenaito-config-service/src/main/java/cn/odboy/domain/ConfigApp.java +++ b/kenaito-config-service/src/main/java/cn/odboy/domain/ConfigApp.java @@ -1,17 +1,19 @@ package cn.odboy.domain; -import cn.odboy.base.MyObject; +import cn.odboy.base.MyLogicEntity; +import cn.odboy.base.MyNormalEntity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import lombok.Data; import lombok.Getter; import lombok.Setter; /** - *

* 配置应用 - *

* * @author odboy * @since 2024-12-05 @@ -19,18 +21,46 @@ import lombok.Setter; @Getter @Setter @TableName("config_app") -public class ConfigApp extends MyObject { +public class ConfigApp extends MyNormalEntity { - @TableId(value = "id", type = IdType.AUTO) - private Long id; - /** - * 应用名称 - */ - @TableField("app_name") + @TableId(value = "id", type = IdType.AUTO) + private Long id; + + /** 应用名称 */ + @TableField("app_name") + private String appName; + + /** 应用说明 */ + @TableField("description") + private String description; + + @Data + public static class CreateArgs { + @NotBlank(message = "必填") private String appName; - /** - * 应用说明 - */ - @TableField("description") + + @NotBlank(message = "必填") private String description; + } + + @Data + public static class ModifyDescriptionArgs { + @NotNull(message = "必填") + private Long id; + + @NotBlank(message = "必填") + private String description; + } + + @Data + public static class RemoveArgs{ + @NotNull(message = "必填") + private Long id; + } + + @Data + public static class ClientInfo{ + private String ip; + private Boolean isActive = false; + } } diff --git a/kenaito-config-service/src/main/java/cn/odboy/domain/ConfigAppUser.java b/kenaito-config-service/src/main/java/cn/odboy/domain/ConfigAppUser.java new file mode 100644 index 0000000..d3e6d05 --- /dev/null +++ b/kenaito-config-service/src/main/java/cn/odboy/domain/ConfigAppUser.java @@ -0,0 +1,25 @@ +package cn.odboy.domain; + +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Getter; +import lombok.Setter; + +/** + * 配置应用与用户关联关系 + * + * @author odboy + * @since 2024-12-06 + */ +@Getter +@Setter +@TableName("config_app_user") +public class ConfigAppUser { + /** 应用名称 */ + @TableField("app_id") + private Long appId; + + /** 应用名称 */ + @TableField("user_id") + private Long userId; +} diff --git a/kenaito-config-service/src/main/java/cn/odboy/domain/ConfigFile.java b/kenaito-config-service/src/main/java/cn/odboy/domain/ConfigFile.java index f0824dd..4ad962b 100644 --- a/kenaito-config-service/src/main/java/cn/odboy/domain/ConfigFile.java +++ b/kenaito-config-service/src/main/java/cn/odboy/domain/ConfigFile.java @@ -1,18 +1,21 @@ package cn.odboy.domain; -import cn.odboy.base.MyObject; +import cn.odboy.base.MyNormalEntity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; + +import javax.mail.Multipart; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; import lombok.Data; import lombok.Getter; import lombok.Setter; +import org.springframework.web.multipart.MultipartFile; /** - *

* 配置文件 - *

* * @author odboy * @since 2024-12-05 @@ -20,26 +23,51 @@ import lombok.Setter; @Getter @Setter @TableName("config_file") -public class ConfigFile extends MyObject { +public class ConfigFile extends MyNormalEntity { - @TableId(value = "id", type = IdType.AUTO) - private Long id; + @TableId(value = "id", type = IdType.AUTO) + private Long id; - @TableField("app_name") - private String appName; + /** configAppId */ + @TableField("app_id") + private Long appId; - @TableField("env") + /** 环境编码 */ + @TableField("env") + private String env; + + /** 例如: application-daily.properties */ + @TableField("file_name") + private String fileName; + + /** 当前配置内容版本 */ + @TableField("version") + private Long version; + + @Data + public static class CreateArgs { + @NotNull(message = "必填") + private Long appId; + + @NotBlank(message = "必填") private String env; - /** - * 例如: application-daily.properties - */ - @TableField("file_name") - private String fileName; + @NotNull(message = "必填") + private MultipartFile file; + } - /** - * 当前配置内容版本 - */ - @TableField("version") - private Long version; + @Data + public static class ModifyFileContentArgs { + @NotNull(message = "必填") + private Long id; + + @NotBlank(message = "必填") + private String fileContent; + } + + @Data + public class RemoveArgs { + @NotNull(message = "必填") + private Long id; + } } diff --git a/kenaito-config-service/src/main/java/cn/odboy/domain/ConfigVersion.java b/kenaito-config-service/src/main/java/cn/odboy/domain/ConfigVersion.java index bd4df12..c1434fa 100644 --- a/kenaito-config-service/src/main/java/cn/odboy/domain/ConfigVersion.java +++ b/kenaito-config-service/src/main/java/cn/odboy/domain/ConfigVersion.java @@ -1,17 +1,13 @@ package cn.odboy.domain; import cn.odboy.base.MyObject; -import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; -import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Getter; import lombok.Setter; /** - *

* 配置内容版本 - *

* * @author odboy * @since 2024-12-05 @@ -20,9 +16,15 @@ import lombok.Setter; @Setter @TableName("config_version") public class ConfigVersion extends MyObject { - @TableField("file_id") - private Long fileId; + /** 文件id */ + @TableField("file_id") + private Long fileId; - @TableField("file_content") - private byte[] fileContent; + /** 配置文件内容 */ + @TableField("file_content") + private String fileContent; + + /** 配置文件内容版本 */ + @TableField("version") + private Long version; } diff --git a/kenaito-config-service/src/main/java/cn/odboy/mapper/ConfigVersionMapper.java b/kenaito-config-service/src/main/java/cn/odboy/mapper/ConfigVersionMapper.java new file mode 100644 index 0000000..40625f9 --- /dev/null +++ b/kenaito-config-service/src/main/java/cn/odboy/mapper/ConfigVersionMapper.java @@ -0,0 +1,14 @@ +package cn.odboy.mapper; + +import cn.odboy.domain.ConfigVersion; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Mapper; + +/** + * 配置版本 Mapper 接口 + * + * @author odboy + * @since 2024-12-05 + */ +@Mapper +public interface ConfigVersionMapper extends BaseMapper {} diff --git a/kenaito-config-service/src/main/java/cn/odboy/rest/ConfigAppController.java b/kenaito-config-service/src/main/java/cn/odboy/rest/ConfigAppController.java new file mode 100644 index 0000000..26d6b84 --- /dev/null +++ b/kenaito-config-service/src/main/java/cn/odboy/rest/ConfigAppController.java @@ -0,0 +1,58 @@ +package cn.odboy.rest; + +import cn.odboy.base.model.SelectOption; +import cn.odboy.domain.ConfigApp; +import cn.odboy.infra.exception.BadRequestException; +import cn.odboy.infra.netty.ConfigClientManage; +import cn.odboy.infra.response.PageArgs; +import cn.odboy.infra.response.PageResult; +import cn.odboy.infra.rest.AnonymousAccess; +import cn.odboy.modules.system.domain.User; +import cn.odboy.modules.system.domain.vo.UserQueryCriteria; +import cn.odboy.service.ConfigAppService; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * 配置文件 前端控制器 + * + * @author odboy + * @since 2024-12-05 + */ +@RequiredArgsConstructor +@RestController +@RequestMapping("/api/config/app") +public class ConfigAppController { + private final ConfigAppService configAppService; + + @PostMapping("/create") + public ResponseEntity create(@Validated @RequestBody ConfigApp.CreateArgs args) { + configAppService.create(args); + return new ResponseEntity<>(HttpStatus.OK); + } + + @PostMapping("/remove") + public ResponseEntity remove(@Validated @RequestBody ConfigApp.RemoveArgs args) { + configAppService.remove(args); + return new ResponseEntity<>(HttpStatus.OK); + } + + @PostMapping("/modifyDescription") + public ResponseEntity modifyDescription(@Validated @RequestBody ConfigApp.ModifyDescriptionArgs args) { + configAppService.modifyDescription(args); + return new ResponseEntity<>(HttpStatus.OK); + } + + @AnonymousAccess + @GetMapping("/queryClientList") + public ResponseEntity queryClientList() { + return new ResponseEntity<>(ConfigClientManage.queryClientInfos("daily", "kenaito-config-demo"), HttpStatus.OK); + } +} diff --git a/kenaito-config-service/src/main/java/cn/odboy/rest/ConfigFileController.java b/kenaito-config-service/src/main/java/cn/odboy/rest/ConfigFileController.java index f419103..381d0d8 100644 --- a/kenaito-config-service/src/main/java/cn/odboy/rest/ConfigFileController.java +++ b/kenaito-config-service/src/main/java/cn/odboy/rest/ConfigFileController.java @@ -1,18 +1,44 @@ package cn.odboy.rest; +import cn.odboy.domain.ConfigApp; +import cn.odboy.domain.ConfigFile; +import cn.odboy.service.ConfigFileService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** - *

* 配置文件 前端控制器 - *

* * @author odboy * @since 2024-12-05 */ +@RequiredArgsConstructor @RestController -@RequestMapping("/api/configFile") +@RequestMapping("/api/config/file") public class ConfigFileController { + private final ConfigFileService configFileService; + @PostMapping("/create") + public ResponseEntity create(@Validated @RequestBody ConfigFile.CreateArgs args) throws Exception { + configFileService.create(args); + return new ResponseEntity<>(HttpStatus.OK); + } + + @PostMapping("/remove") + public ResponseEntity remove(@Validated @RequestBody ConfigFile.RemoveArgs args) { + configFileService.remove(args); + return new ResponseEntity<>(HttpStatus.OK); + } + + @PostMapping("/modifyFileContent") + public ResponseEntity modifyFileContent(@Validated @RequestBody ConfigFile.ModifyFileContentArgs args) { + configFileService.modifyFileContent(args); + return new ResponseEntity<>(HttpStatus.OK); + } } diff --git a/kenaito-config-service/src/main/java/cn/odboy/service/ConfigAppService.java b/kenaito-config-service/src/main/java/cn/odboy/service/ConfigAppService.java index 4a19d5d..a86e79a 100644 --- a/kenaito-config-service/src/main/java/cn/odboy/service/ConfigAppService.java +++ b/kenaito-config-service/src/main/java/cn/odboy/service/ConfigAppService.java @@ -14,4 +14,9 @@ import com.baomidou.mybatisplus.extension.service.IService; */ public interface ConfigAppService extends IService { + void create(ConfigApp.CreateArgs args); + + void remove(ConfigApp.RemoveArgs args); + + void modifyDescription(ConfigApp.ModifyDescriptionArgs 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 6654ce8..467f38d 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 @@ -1,22 +1,26 @@ package cn.odboy.service; import cn.odboy.config.model.msgtype.ConfigFileInfo; +import cn.odboy.domain.ConfigFile; +import com.baomidou.mybatisplus.extension.service.IService; import java.util.List; /** - *

* 配置文件 服务类 - *

* * @author odboy * @since 2024-12-05 */ -public interface ConfigFileService { - /** - * @param env 环境编码 - * @param dataId 数据ID,这里是应用名称 - * @return / - */ - List getFileList(String env, String dataId); +public interface ConfigFileService extends IService { + /** + * @param env 环境编码 + * @param dataId 数据ID,这里是应用名称 + * @return / + */ + List getFileList(String env, String dataId); + List queryInAppId(Long appId); + void create(ConfigFile.CreateArgs args) throws Exception; + void remove(ConfigFile.RemoveArgs args); + void modifyFileContent(ConfigFile.ModifyFileContentArgs args); } diff --git a/kenaito-config-service/src/main/java/cn/odboy/service/ConfigVersionService.java b/kenaito-config-service/src/main/java/cn/odboy/service/ConfigVersionService.java new file mode 100644 index 0000000..78883d9 --- /dev/null +++ b/kenaito-config-service/src/main/java/cn/odboy/service/ConfigVersionService.java @@ -0,0 +1,17 @@ +package cn.odboy.service; + +import cn.odboy.domain.ConfigVersion; +import com.baomidou.mybatisplus.extension.service.IService; + +import java.util.List; + +/** + * 配置版本 服务类 + * + * @author odboy + * @since 2024-12-05 + */ +public interface ConfigVersionService extends IService { + List queryInFileId(List fileIds); + void removeBatchByFileId(Long fileId); +} diff --git a/kenaito-config-service/src/main/java/cn/odboy/service/impl/ConfigAppServiceImpl.java b/kenaito-config-service/src/main/java/cn/odboy/service/impl/ConfigAppServiceImpl.java index 29480d8..6ef26ee 100644 --- a/kenaito-config-service/src/main/java/cn/odboy/service/impl/ConfigAppServiceImpl.java +++ b/kenaito-config-service/src/main/java/cn/odboy/service/impl/ConfigAppServiceImpl.java @@ -1,23 +1,66 @@ package cn.odboy.service.impl; +import cn.hutool.core.bean.BeanUtil; import cn.odboy.domain.ConfigApp; import cn.odboy.domain.ConfigFile; +import cn.odboy.domain.ConfigVersion; import cn.odboy.mapper.ConfigAppMapper; -import cn.odboy.mapper.ConfigFileMapper; import cn.odboy.service.ConfigAppService; import cn.odboy.service.ConfigFileService; +import cn.odboy.service.ConfigVersionService; 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; /** - *

* 配置应用 服务实现类 - *

* * @author odboy * @since 2024-12-05 */ @Service -public class ConfigAppServiceImpl extends ServiceImpl implements ConfigAppService { +@RequiredArgsConstructor +public class ConfigAppServiceImpl extends ServiceImpl + implements ConfigAppService { + private final ConfigFileService configFileService; + private final ConfigVersionService configVersionService; + @Override + @Transactional(rollbackFor = Exception.class) + public void create(ConfigApp.CreateArgs args) { + save(BeanUtil.copyProperties(args, ConfigApp.class)); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void remove(ConfigApp.RemoveArgs args) { + removeById(args.getId()); + List configFileList = configFileService.queryInAppId(args.getId()); + if (configFileList.isEmpty()) { + return; + } + List configFileIds = + configFileList.stream().map(ConfigFile::getId).distinct().collect(Collectors.toList()); + configFileService.removeBatchByIds(configFileIds); + List configVersionList = configVersionService.queryInFileId(configFileIds); + if (configVersionList.isEmpty()) { + return; + } + List configVersionIds = + configVersionList.stream() + .map(ConfigVersion::getFileId) + .distinct() + .collect(Collectors.toList()); + configVersionService.removeBatchByIds(configVersionIds); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void modifyDescription(ConfigApp.ModifyDescriptionArgs args) { + ConfigApp configApp = BeanUtil.copyProperties(args, ConfigApp.class); + updateById(configApp); + } } 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 35f901b..d035a50 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 @@ -1,28 +1,81 @@ package cn.odboy.service.impl; +import cn.hutool.core.util.StrUtil; import cn.odboy.config.model.msgtype.ConfigFileInfo; +import cn.odboy.domain.ConfigFile; +import cn.odboy.domain.ConfigVersion; import cn.odboy.mapper.ConfigFileMapper; 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.nio.charset.StandardCharsets; +import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; - -import java.util.List; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; /** - *

* 配置文件 服务实现类 - *

* * @author odboy * @since 2024-12-05 */ @Service @RequiredArgsConstructor -public class ConfigFileServiceImpl implements ConfigFileService { - private final ConfigFileMapper configFileMapper; +public class ConfigFileServiceImpl extends ServiceImpl + implements ConfigFileService { + private final ConfigVersionService configVersionService; - @Override - public List getFileList(String env, String dataId) { - return configFileMapper.selectByEnvAndAppName(env, dataId); - } + @Override + public List getFileList(String env, String dataId) { + return getBaseMapper().selectByEnvAndAppName(env, dataId); + } + + @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.setEnv(args.getEnv()); + configFile.setAppId(args.getAppId()); + configFile.setFileName(file.getOriginalFilename()); + configFile.setVersion(version); + save(configFile); + ConfigVersion configVersion = new ConfigVersion(); + configVersion.setFileId(configFile.getId()); + 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 modifyFileContent(ConfigFile.ModifyFileContentArgs args) { + 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.setVersion(newVersion); + configVersionService.save(lastConfigVersion); + } } diff --git a/kenaito-config-service/src/main/java/cn/odboy/service/impl/ConfigVersionServiceImpl.java b/kenaito-config-service/src/main/java/cn/odboy/service/impl/ConfigVersionServiceImpl.java new file mode 100644 index 0000000..0a4d522 --- /dev/null +++ b/kenaito-config-service/src/main/java/cn/odboy/service/impl/ConfigVersionServiceImpl.java @@ -0,0 +1,31 @@ +package cn.odboy.service.impl; + +import cn.odboy.domain.ConfigVersion; +import cn.odboy.mapper.ConfigVersionMapper; +import cn.odboy.service.ConfigVersionService; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import java.util.List; +import org.springframework.stereotype.Service; + +/** + * 配置版本 服务实现类 + * + * @author odboy + * @since 2024-12-05 + */ +@Service +public class ConfigVersionServiceImpl extends ServiceImpl + implements ConfigVersionService { + @Override + public List queryInFileId(List fileIds) { + return list(new LambdaQueryWrapper().in(ConfigVersion::getFileId, fileIds)); + } + + @Override + public void removeBatchByFileId(Long fileId) { + remove(new LambdaQueryWrapper() + .eq(ConfigVersion::getFileId, fileId) + ); + } +} diff --git a/kenaito-config-service/src/main/resources/mapper/ConfigFileMapper.xml b/kenaito-config-service/src/main/resources/mapper/ConfigFileMapper.xml index c1abedc..8787d96 100644 --- a/kenaito-config-service/src/main/resources/mapper/ConfigFileMapper.xml +++ b/kenaito-config-service/src/main/resources/mapper/ConfigFileMapper.xml @@ -2,12 +2,13 @@