From 9149b5dd513eca814d89dd7b505c4e46d3beb01c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E9=98=B3?= Date: Sun, 29 Mar 2026 00:51:46 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=96=B0=E6=96=87?= =?UTF-8?q?=E7=AB=A0=E9=85=8D=E7=BD=AE=E5=92=8C=E7=8A=B6=E6=80=81=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E4=BB=A5=E5=A2=9E=E5=BC=BA=E4=B8=80=E8=87=B4=E6=80=A7?= =?UTF-8?q?=E5=92=8C=E5=8F=AF=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改数据库表结构,将文章状态字段类型从enum更改为tinyint,并更新相关注释。 - 在ArticleServiceImpl中引入ArticleStatus枚举,替换字符串状态检查,提升代码可读性和类型安全性。 - 更新Article和ArticleConfig模型,调整prev和next字段为Map类型,以支持更灵活的数据结构。 - 添加jackson-annotations依赖以支持JSON序列化和反序列化功能。 --- ThriveX.sql | 4 +- .../web/service/impl/ArticleServiceImpl.java | 15 +++---- model/pom.xml | 5 +++ .../net/core/enums/ArticleStatus.java | 41 +++++++++++++++++++ .../java/liuyuyang/net/model/Article.java | 5 ++- .../liuyuyang/net/model/ArticleConfig.java | 5 ++- 6 files changed, 62 insertions(+), 13 deletions(-) create mode 100644 model/src/main/java/liuyuyang/net/core/enums/ArticleStatus.java diff --git a/ThriveX.sql b/ThriveX.sql index 7a68d44..e162a10 100644 --- a/ThriveX.sql +++ b/ThriveX.sql @@ -80,7 +80,7 @@ DROP TABLE IF EXISTS `article_config`; /*!50503 SET character_set_client = utf8mb4 */; CREATE TABLE `article_config` ( `id` int NOT NULL AUTO_INCREMENT, - `status` enum('default','no_home','hide') DEFAULT 'default' COMMENT '文章状态', + `status` tinyint NOT NULL DEFAULT 1 COMMENT '文章状态:1 正常,2 首页隐藏,3 全站隐藏', `password` varchar(100) DEFAULT '' COMMENT '是否文章加密', `is_encrypt` tinyint DEFAULT '0' COMMENT '是否加密', `is_draft` tinyint DEFAULT '0' COMMENT '是否为草稿', @@ -97,7 +97,7 @@ CREATE TABLE `article_config` ( LOCK TABLES `article_config` WRITE; /*!40000 ALTER TABLE `article_config` DISABLE KEYS */; -INSERT INTO `article_config` VALUES (1,'default','',0,0,0,1),(2,'default','',0,0,0,2),(18,'default','',0,0,0,5); +INSERT INTO `article_config` VALUES (1,1,'',0,0,0,1),(2,1,'',0,0,0,2),(18,1,'',0,0,0,5); /*!40000 ALTER TABLE `article_config` ENABLE KEYS */; UNLOCK TABLES; diff --git a/blog/src/main/java/liuyuyang/net/web/service/impl/ArticleServiceImpl.java b/blog/src/main/java/liuyuyang/net/web/service/impl/ArticleServiceImpl.java index e3e059f..58bbf51 100755 --- a/blog/src/main/java/liuyuyang/net/web/service/impl/ArticleServiceImpl.java +++ b/blog/src/main/java/liuyuyang/net/web/service/impl/ArticleServiceImpl.java @@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import liuyuyang.net.core.enums.ArticleStatus; import liuyuyang.net.core.execption.CustomException; import liuyuyang.net.core.utils.CommonUtils; import liuyuyang.net.dto.article.ArticleFormDTO; @@ -118,7 +119,7 @@ public class ArticleServiceImpl extends ServiceImpl impl ArticleConfig config = article.getConfig(); ArticleConfig articleConfig = new ArticleConfig(); articleConfig.setArticleId(article.getId()); - articleConfig.setStatus(config.getStatus()); + articleConfig.setStatus(config.getStatus() != null ? config.getStatus() : ArticleStatus.DEFAULT); articleConfig.setPassword(config.getPassword()); articleConfig.setIsDraft(article.getConfig().getIsDraft()); articleConfig.setIsEncrypt(article.getConfig().getIsEncrypt()); @@ -206,7 +207,7 @@ public class ArticleServiceImpl extends ServiceImpl impl ArticleConfig config = articleFormDTO.getConfig(); ArticleConfig articleConfig = new ArticleConfig(); articleConfig.setArticleId(articleFormDTO.getId()); - articleConfig.setStatus(config.getStatus()); + articleConfig.setStatus(config.getStatus() != null ? config.getStatus() : ArticleStatus.DEFAULT); articleConfig.setPassword(config.getPassword()); articleConfig.setIsDraft(config.getIsDraft()); articleConfig.setIsEncrypt(config.getIsEncrypt()); @@ -240,7 +241,7 @@ public class ArticleServiceImpl extends ServiceImpl impl throw new CustomException(404, "该文章已被删除"); } - if ("hide".equals(config.getStatus())) { + if (ArticleStatus.HIDE.equals(config.getStatus())) { throw new CustomException(611, "该文章已被隐藏"); } @@ -493,7 +494,7 @@ public class ArticleServiceImpl extends ServiceImpl impl if (isAdmin) return true; // 非管理员不能看到隐藏文章 - return !Objects.equals(config.getStatus(), "hide"); + return !Objects.equals(config.getStatus(), ArticleStatus.HIDE); } /** @@ -504,7 +505,7 @@ public class ArticleServiceImpl extends ServiceImpl impl if (config == null) { return true; } - return !Objects.equals(config.getStatus(), "no_home"); + return !Objects.equals(config.getStatus(), ArticleStatus.NO_HOME); } /** @@ -525,7 +526,7 @@ public class ArticleServiceImpl extends ServiceImpl impl LambdaQueryWrapper articleConfigLambdaQueryWrapper = new LambdaQueryWrapper<>(); articleConfigLambdaQueryWrapper.eq(ArticleConfig::getIsDraft, 0); articleConfigLambdaQueryWrapper.eq(ArticleConfig::getIsDel, 0); - articleConfigLambdaQueryWrapper.eq(ArticleConfig::getStatus, "default"); + articleConfigLambdaQueryWrapper.eq(ArticleConfig::getStatus, ArticleStatus.DEFAULT); articleConfigLambdaQueryWrapper.eq(ArticleConfig::getPassword, ""); List ids = articleConfigMapper.selectList(articleConfigLambdaQueryWrapper) @@ -749,7 +750,7 @@ public class ArticleServiceImpl extends ServiceImpl impl // 设置默认文章配置 ArticleConfig config = new ArticleConfig(); - config.setStatus("default"); + config.setStatus(ArticleStatus.DEFAULT); config.setPassword(""); config.setIsDraft(0); config.setIsEncrypt(0); diff --git a/model/pom.xml b/model/pom.xml index 6423270..40e263f 100755 --- a/model/pom.xml +++ b/model/pom.xml @@ -33,5 +33,10 @@ com.github.xiaoymin knife4j-openapi2-spring-boot-starter + + + com.fasterxml.jackson.core + jackson-annotations + \ No newline at end of file diff --git a/model/src/main/java/liuyuyang/net/core/enums/ArticleStatus.java b/model/src/main/java/liuyuyang/net/core/enums/ArticleStatus.java new file mode 100644 index 0000000..739f9ab --- /dev/null +++ b/model/src/main/java/liuyuyang/net/core/enums/ArticleStatus.java @@ -0,0 +1,41 @@ +package liuyuyang.net.core.enums; + +import com.baomidou.mybatisplus.annotation.EnumValue; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; +import lombok.Getter; + +public enum ArticleStatus { + DEFAULT(1, "正常"), + NO_HOME(2, "首页隐藏"), + HIDE(3, "全站隐藏"); + + @EnumValue + private final int value; + + @Getter + private final String desc; + + ArticleStatus(int value, String desc) { + this.value = value; + this.desc = desc; + } + + @JsonValue + public int getValue() { + return value; + } + + @JsonCreator + public static ArticleStatus fromJson(Integer value) { + if (value == null) return null; + + for (ArticleStatus s : values()) { + if (s.value == value) { + return s; + } + } + + throw new IllegalArgumentException("未知文章状态: " + value); + } +} diff --git a/model/src/main/java/liuyuyang/net/model/Article.java b/model/src/main/java/liuyuyang/net/model/Article.java index 8fedfcd..7ee1f6d 100755 --- a/model/src/main/java/liuyuyang/net/model/Article.java +++ b/model/src/main/java/liuyuyang/net/model/Article.java @@ -9,6 +9,7 @@ import lombok.EqualsAndHashCode; import java.util.ArrayList; import java.util.List; +import java.util.Map; @Data @EqualsAndHashCode(callSuper = true) @@ -29,7 +30,7 @@ public class Article extends ArticleFormDTO { private List tagList = new ArrayList<>(); @TableField(exist = false) - private Article prev; + private Map prev; @TableField(exist = false) - private Article next; + private Map next; } diff --git a/model/src/main/java/liuyuyang/net/model/ArticleConfig.java b/model/src/main/java/liuyuyang/net/model/ArticleConfig.java index a6d96cc..a11ac86 100755 --- a/model/src/main/java/liuyuyang/net/model/ArticleConfig.java +++ b/model/src/main/java/liuyuyang/net/model/ArticleConfig.java @@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import io.swagger.annotations.ApiModelProperty; +import liuyuyang.net.core.enums.ArticleStatus; import lombok.Data; @Data @@ -13,8 +14,8 @@ public class ArticleConfig { @ApiModelProperty(value = "ID") private Integer id; - @ApiModelProperty(value = "文章状态", example = "默认(default) 不在首页显示(no_home) 全站隐藏(hide)") - private String status; + @ApiModelProperty(value = "文章状态:1 正常,2 首页隐藏,3 全站隐藏", example = "1", allowableValues = "1, 2, 3") + private ArticleStatus status; @ApiModelProperty(value = "文章密码", example = "默认为空表示不加密") private String password;