修改投票和get逻辑

This commit is contained in:
JACKYMYPERSON
2025-09-24 15:34:09 +08:00
parent 4a0e61cac5
commit f66aa3a715
3 changed files with 140 additions and 110 deletions

View File

@@ -7,7 +7,7 @@ database:
params: "charset=utf8mb4&parseTime=True&loc=Local" params: "charset=utf8mb4&parseTime=True&loc=Local"
redis: redis:
host: "localhost" host: "localhost"
port: 6379 port: 30079
username: "default" username: "default"
password: "" password: ""
jwtsecret: "clka1af83af15vhyt8s652avre" jwtsecret: "clka1af83af15vhyt8s652avre"

View File

@@ -29,7 +29,7 @@ type ArticleResponse struct {
CreateTime string `json:"创建时间"` // 创建时间 CreateTime string `json:"创建时间"` // 创建时间
Options []ArticleOption `json:"选项"` // 按顺序排列的选项列表 Options []ArticleOption `json:"选项"` // 按顺序排列的选项列表
UserHasVoted bool `json:"用户是否已投票"` // 当前用户是否投过票 UserHasVoted bool `json:"用户是否已投票"` // 当前用户是否投过票
VotedOptionID *int64 `json:"用户投票的选项ID"` // 若已投票记录选项ID未投票为null VotedOptionIDs []int64 `json:"用户投票的选项ID"` // 若已投票,记录所有选项ID未投票为空数组
} }
type UserReq struct { type UserReq struct {
@@ -96,21 +96,25 @@ func ArticleListget(c *gin.Context) {
return return
} }
// 4.2 查询当前用户对该文评的投票记录 // 4.2 查询当前用户对该文评的所有投票记录
var userVote struct { var userVotes []struct {
OptionID int64 `gorm:"column:option_id"` OptionID int64 `gorm:"column:option_id"`
} }
voteErr := databaseInit.UserDB.Table("user_votes"). voteErr := databaseInit.UserDB.Table("user_votes").
Where("user_id = ? AND vote_article_id = ?", req.Uid, article.ArticleID). Where("user_id = ? AND vote_article_id = ?", req.Uid, article.ArticleID).
First(&userVote).Error Find(&userVotes).Error
// 标记用户是否投票及投票的选项ID // 标记用户是否投票及所有投票的选项ID
userHasVoted := false userHasVoted := false
var votedOptionID *int64 = nil var votedOptionIDs []int64 = []int64{}
if voteErr == nil {
if voteErr == nil && len(userVotes) > 0 {
userHasVoted = true userHasVoted = true
votedOptionID = &userVote.OptionID // 收集所有投票的选项ID
} else if voteErr != gorm.ErrRecordNotFound { for _, vote := range userVotes {
votedOptionIDs = append(votedOptionIDs, vote.OptionID)
}
} else if voteErr != nil && voteErr != gorm.ErrRecordNotFound {
// 处理查询错误(非"未找到"的错误) // 处理查询错误(非"未找到"的错误)
c.JSON(http.StatusInternalServerError, gin.H{ c.JSON(http.StatusInternalServerError, gin.H{
"error": "查询用户投票记录失败: " + voteErr.Error(), "error": "查询用户投票记录失败: " + voteErr.Error(),
@@ -121,8 +125,14 @@ func ArticleListget(c *gin.Context) {
// 4.3 格式化选项数据(标记用户是否投了该选项) // 4.3 格式化选项数据(标记用户是否投了该选项)
var optionList []ArticleOption var optionList []ArticleOption
for _, opt := range options { for _, opt := range options {
// 检查当前选项是否是用户投票的选项 // 检查当前选项是否是用户投票的选项之一
isVoted := userHasVoted && (opt.ID == *votedOptionID) isVoted := false
for _, votedID := range votedOptionIDs {
if opt.ID == votedID {
isVoted = true
break
}
}
optionList = append(optionList, ArticleOption{ optionList = append(optionList, ArticleOption{
ID: opt.ID, ID: opt.ID,
@@ -144,7 +154,7 @@ func ArticleListget(c *gin.Context) {
CreateTime: article.CreateTime, CreateTime: article.CreateTime,
Options: optionList, Options: optionList,
UserHasVoted: userHasVoted, // 整体标记用户是否投过票 UserHasVoted: userHasVoted, // 整体标记用户是否投过票
VotedOptionID: votedOptionID, // 记录用户投票的选项ID未投票则为null VotedOptionIDs: votedOptionIDs, // 记录用户投票的所有选项ID未投票则为空数组
} }
// 4.5 加入结果集 // 4.5 加入结果集

View File

@@ -1,7 +1,6 @@
package article package article
import ( import (
"fmt"
"net/http" "net/http"
"time" "time"
"toutoukan/init/databaseInit" "toutoukan/init/databaseInit"
@@ -10,10 +9,20 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
// 投票请求结构体,同时支持单选(optionId)和多选(optionIds)
type VoteArticleRequest struct { type VoteArticleRequest struct {
Uid string `json:"uid" binding:"required,min=1,max=50"` // 用户ID,非空且长度限制 Uid string `json:"uid" binding:"required,min=1,max=50"` // 用户ID
ArticleID int64 `json:"articleId" binding:"required,min=1"` // 文ID,非空且为正整数 ArticleID int64 `json:"articleId" binding:"required,min=1"` // 文ID
OptionID int64 `json:"optionId" binding:"required,min=1"` // 选项ID,投票必需 OptionID int64 `json:"optionId" binding:"omitempty,min=1"` // 单个选项ID(单选时使用)
OptionIDs []int64 `json:"optionIds" binding:"omitempty,dive,min=1"` // 多个选项ID多选时使用
}
// 用户投票记录表结构
type UserVote struct {
UserID string `gorm:"column:user_id"`
VoteArticleID int64 `gorm:"column:vote_article_id"`
OptionID int64 `gorm:"column:option_id"`
VoteTime time.Time `gorm:"column:vote_time"`
} }
func VoteArticle(c *gin.Context) { func VoteArticle(c *gin.Context) {
@@ -42,31 +51,11 @@ func VoteArticle(c *gin.Context) {
} }
}() }()
// 3. 检查用户是否已投票(防重复投票) // 3. 检查文章状态和投票类型
var existingVote struct{ ID int64 }
voteCheckErr := tx.Table("user_votes").
Where("user_id = ? AND vote_article_id = ?", req.Uid, req.ArticleID).
First(&existingVote).Error
if voteCheckErr == nil {
// 已存在投票记录
tx.Rollback()
c.JSON(http.StatusForbidden, gin.H{
"error": "您已对该文评投过票,无法重复投票",
})
return
} else if voteCheckErr != gorm.ErrRecordNotFound {
// 其他查询错误
tx.Rollback()
c.JSON(http.StatusInternalServerError, gin.H{
"error": "检查投票记录失败: " + voteCheckErr.Error(),
})
return
}
var articleStatus struct { var articleStatus struct {
IsEnded bool `gorm:"column:is_ended"` IsEnded bool `gorm:"column:is_ended"`
EndTime time.Time `gorm:"column:end_time"` // 新增:获取截止时间 EndTime time.Time `gorm:"column:end_time"`
VoteType string `gorm:"column:vote_type"` // 投票类型single-单选multiple-多选
} }
if err := tx.Table("article_list"). if err := tx.Table("article_list").
Where("articleId = ?", req.ArticleID). Where("articleId = ?", req.ArticleID).
@@ -80,70 +69,95 @@ func VoteArticle(c *gin.Context) {
return return
} }
// 双重检查:已标记结束 或 已过截止时间,均禁止投票 // 3.1 检查投票是否已结束
now := time.Now() now := time.Now()
if articleStatus.IsEnded || articleStatus.EndTime.Before(now) { if articleStatus.IsEnded || articleStatus.EndTime.Before(now) {
tx.Rollback() tx.Rollback()
c.JSON(http.StatusForbidden, gin.H{"error": "该文评投票已结束或已过截止时间,无法投票"})
// 区分提示信息(可选,增强用户体验)
var errMsg string
if articleStatus.IsEnded {
errMsg = "该文评投票已结束,无法投票"
} else {
errMsg = "该文评投票已过截止时间,无法投票"
}
c.JSON(http.StatusForbidden, gin.H{"error": errMsg})
return return
} }
// 额外优化如果已过截止时间但is_ended未更新主动更新状态可选 // 4. 处理投票选项统一转换为选项ID列表
if !articleStatus.IsEnded && articleStatus.EndTime.Before(now) { var selectedOptions []int64
if err := tx.Table("article_list"). if articleStatus.VoteType == "single" {
Where("articleId = ?", req.ArticleID). if req.OptionID == 0 || len(req.OptionIDs) > 0 {
Update("is_ended", true).Error; err != nil { tx.Rollback()
// 此处不回滚,仅记录警告,不影响主要逻辑 c.JSON(http.StatusForbidden, gin.H{
fmt.Printf("警告更新过期文评状态失败articleId=%d, err=%v\n", req.ArticleID, err) "error": "该文评为单选投票请使用optionId提交单个选项",
})
return
} }
selectedOptions = []int64{req.OptionID}
} else {
if len(req.OptionIDs) == 0 || req.OptionID != 0 {
tx.Rollback()
c.JSON(http.StatusForbidden, gin.H{
"error": "该文评为多选投票请使用optionIds提交多个选项",
})
return
}
selectedOptions = req.OptionIDs
} }
// 5. 检查选项是否属于该文评 // 5. 检查用户是否已投票
var optionCheck struct{ ID int64 } var voteCount int64
if err := tx.Table("article_options"). if err := tx.Table("user_votes").
Where("id = ? AND vote_article_id = ?", req.OptionID, req.ArticleID). Where("user_id = ? AND vote_article_id = ?", req.Uid, req.ArticleID).
First(&optionCheck).Error; err != nil { Count(&voteCount).Error; err != nil {
tx.Rollback()
c.JSON(http.StatusInternalServerError, gin.H{"error": "检查投票记录失败: " + err.Error()})
return
}
if voteCount > 0 {
tx.Rollback()
c.JSON(http.StatusForbidden, gin.H{"error": "您已对该文评投过票,无法重复投票"})
return
}
// 6. 批量检查所有选项是否属于该文评
var existingOptionsCount int64
if err := tx.Table("article_options").
Where("vote_article_id = ? AND id IN ?", req.ArticleID, selectedOptions).
Count(&existingOptionsCount).Error; err != nil {
tx.Rollback() tx.Rollback()
if err == gorm.ErrRecordNotFound {
c.JSON(http.StatusNotFound, gin.H{"error": "选项不存在或不属于该文评"})
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": "查询选项失败: " + err.Error()}) c.JSON(http.StatusInternalServerError, gin.H{"error": "查询选项失败: " + err.Error()})
}
return return
} }
if existingOptionsCount != int64(len(selectedOptions)) {
// 6. 执行投票事务(三表操作)
// 6.1 记录用户投票记录
if err := tx.Table("user_votes").Create(map[string]interface{}{
"user_id": req.Uid,
"vote_article_id": req.ArticleID,
"option_id": req.OptionID,
"vote_time": time.Now(),
}).Error; err != nil {
tx.Rollback() tx.Rollback()
c.JSON(http.StatusInternalServerError, gin.H{"error": "记录投票失败: " + err.Error()}) c.JSON(http.StatusNotFound, gin.H{"error": "部分选项ID不存在或不属于该文评"})
return return
} }
// 6.2 更新选项票数 // 7. 执行批量投票事务
voteTime := time.Now()
// 7.1 批量插入用户投票记录
userVotes := make([]UserVote, len(selectedOptions))
for i, optionID := range selectedOptions {
userVotes[i] = UserVote{
UserID: req.Uid,
VoteArticleID: req.ArticleID,
OptionID: optionID,
VoteTime: voteTime,
}
}
if err := tx.Table("user_votes").Create(&userVotes).Error; err != nil {
tx.Rollback()
c.JSON(http.StatusInternalServerError, gin.H{"error": "批量记录投票失败: " + err.Error()})
return
}
// 7.2 批量更新选项票数
if err := tx.Table("article_options"). if err := tx.Table("article_options").
Where("id = ?", req.OptionID). Where("id IN ?", selectedOptions).
Update("option_votes_num", gorm.Expr("option_votes_num + 1")).Error; err != nil { Update("option_votes_num", gorm.Expr("option_votes_num + 1")).Error; err != nil {
tx.Rollback() tx.Rollback()
c.JSON(http.StatusInternalServerError, gin.H{"error": "更新选项票数失败: " + err.Error()}) c.JSON(http.StatusInternalServerError, gin.H{"error": "批量更新选项票数失败: " + err.Error()})
return return
} }
// 6.3 更新文评总投票人数 // 7.3 更新文评总投票人数
if err := tx.Table("article_list"). if err := tx.Table("article_list").
Where("articleId = ?", req.ArticleID). Where("articleId = ?", req.ArticleID).
Update("total_voters_num", gorm.Expr("total_voters_num + 1")).Error; err != nil { Update("total_voters_num", gorm.Expr("total_voters_num + 1")).Error; err != nil {
@@ -152,21 +166,27 @@ func VoteArticle(c *gin.Context) {
return return
} }
// 7. 提交事务 // 8. 提交事务
if err := tx.Commit().Error; err != nil { if err := tx.Commit().Error; err != nil {
tx.Rollback() tx.Rollback()
c.JSON(http.StatusInternalServerError, gin.H{"error": "提交投票失败: " + err.Error()}) c.JSON(http.StatusInternalServerError, gin.H{"error": "提交投票失败: " + err.Error()})
return return
} }
// 8. 返回成功响应 // 9. 返回成功响应
responseData := gin.H{
"articleId": req.ArticleID,
"voteTime": voteTime.Format("2006-01-02 15:04:05"),
}
if articleStatus.VoteType == "single" {
responseData["optionId"] = req.OptionID
} else {
responseData["optionIds"] = req.OptionIDs
}
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"success": true, "success": true,
"message": "投票成功", "message": "投票成功",
"data": gin.H{ "data": responseData,
"articleId": req.ArticleID,
"optionId": req.OptionID,
"voteTime": time.Now().Format("2006-01-02 15:04:05"),
},
}) })
} }