refactor: 更新文章配置和状态管理以增强一致性和可读性

- 修改数据库表结构,将文章状态字段类型从enum更改为tinyint,并更新相关注释。
- 在ArticleServiceImpl中引入ArticleStatus枚举,替换字符串状态检查,提升代码可读性和类型安全性。
- 更新Article和ArticleConfig模型,调整prev和next字段为Map类型,以支持更灵活的数据结构。
- 添加jackson-annotations依赖以支持JSON序列化和反序列化功能。
This commit is contained in:
宇阳
2026-03-29 00:51:46 +08:00
parent f3f3f7ed44
commit 9149b5dd51
6 changed files with 62 additions and 13 deletions

View File

@@ -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;

View File

@@ -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<ArticleMapper, Article> 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<ArticleMapper, Article> 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<ArticleMapper, Article> 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<ArticleMapper, Article> 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<ArticleMapper, Article> 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<ArticleMapper, Article> impl
LambdaQueryWrapper<ArticleConfig> 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<Integer> ids = articleConfigMapper.selectList(articleConfigLambdaQueryWrapper)
@@ -749,7 +750,7 @@ public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> impl
// 设置默认文章配置
ArticleConfig config = new ArticleConfig();
config.setStatus("default");
config.setStatus(ArticleStatus.DEFAULT);
config.setPassword("");
config.setIsDraft(0);
config.setIsEncrypt(0);

View File

@@ -33,5 +33,10 @@
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -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);
}
}

View File

@@ -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<Tag> tagList = new ArrayList<>();
@TableField(exist = false)
private Article prev;
private Map<String, Object> prev;
@TableField(exist = false)
private Article next;
private Map<String, Object> next;
}

View File

@@ -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;