From f66aa3a715e240e031ae26e2d5863f81201db20e Mon Sep 17 00:00:00 2001 From: JACKYMYPERSON Date: Wed, 24 Sep 2025 15:34:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8A=95=E7=A5=A8=E5=92=8Cge?= =?UTF-8?q?t=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yaml | 2 +- controllers/article/getarticle.go | 74 ++++++------ controllers/article/votearticle.go | 174 ++++++++++++++++------------- 3 files changed, 140 insertions(+), 110 deletions(-) diff --git a/config.yaml b/config.yaml index 9475799..603b842 100644 --- a/config.yaml +++ b/config.yaml @@ -7,7 +7,7 @@ database: params: "charset=utf8mb4&parseTime=True&loc=Local" redis: host: "localhost" - port: 6379 + port: 30079 username: "default" password: "" jwtsecret: "clka1af83af15vhyt8s652avre" diff --git a/controllers/article/getarticle.go b/controllers/article/getarticle.go index cc444fb..6a09609 100644 --- a/controllers/article/getarticle.go +++ b/controllers/article/getarticle.go @@ -19,17 +19,17 @@ type ArticleOption struct { // ArticleResponse 单个文评的响应结构 type ArticleResponse struct { - ID int64 `json:"文评ID"` // 文评唯一ID - Title string `json:"文评标题"` // 文评标题 - VoteType string `json:"投票类型"` // 投票类型 - TotalVoters int `json:"总投票人数"` // 总投票人数 - EndTime string `json:"结束时间"` // 结束时间 - IsEnded bool `json:"是否结束"` // 是否结束 - PublisherID string `json:"发布者ID"` // 发布者ID - CreateTime string `json:"创建时间"` // 创建时间 - Options []ArticleOption `json:"选项"` // 按顺序排列的选项列表 - UserHasVoted bool `json:"用户是否已投票"` // 当前用户是否投过票 - VotedOptionID *int64 `json:"用户投票的选项ID"` // 若已投票,记录选项ID(未投票为null) + ID int64 `json:"文评ID"` // 文评唯一ID + Title string `json:"文评标题"` // 文评标题 + VoteType string `json:"投票类型"` // 投票类型 + TotalVoters int `json:"总投票人数"` // 总投票人数 + EndTime string `json:"结束时间"` // 结束时间 + IsEnded bool `json:"是否结束"` // 是否结束 + PublisherID string `json:"发布者ID"` // 发布者ID + CreateTime string `json:"创建时间"` // 创建时间 + Options []ArticleOption `json:"选项"` // 按顺序排列的选项列表 + UserHasVoted bool `json:"用户是否已投票"` // 当前用户是否投过票 + VotedOptionIDs []int64 `json:"用户投票的选项ID"` // 若已投票,记录所有选项ID(未投票为空数组) } type UserReq struct { @@ -96,21 +96,25 @@ func ArticleListget(c *gin.Context) { return } - // 4.2 查询当前用户对该文评的投票记录 - var userVote struct { + // 4.2 查询当前用户对该文评的所有投票记录 + var userVotes []struct { OptionID int64 `gorm:"column:option_id"` } voteErr := databaseInit.UserDB.Table("user_votes"). Where("user_id = ? AND vote_article_id = ?", req.Uid, article.ArticleID). - First(&userVote).Error + Find(&userVotes).Error - // 标记用户是否投票及投票的选项ID + // 标记用户是否投票及所有投票的选项ID userHasVoted := false - var votedOptionID *int64 = nil - if voteErr == nil { + var votedOptionIDs []int64 = []int64{} + + if voteErr == nil && len(userVotes) > 0 { userHasVoted = true - votedOptionID = &userVote.OptionID - } else if voteErr != gorm.ErrRecordNotFound { + // 收集所有投票的选项ID + for _, vote := range userVotes { + votedOptionIDs = append(votedOptionIDs, vote.OptionID) + } + } else if voteErr != nil && voteErr != gorm.ErrRecordNotFound { // 处理查询错误(非"未找到"的错误) c.JSON(http.StatusInternalServerError, gin.H{ "error": "查询用户投票记录失败: " + voteErr.Error(), @@ -121,8 +125,14 @@ func ArticleListget(c *gin.Context) { // 4.3 格式化选项数据(标记用户是否投了该选项) var optionList []ArticleOption 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{ ID: opt.ID, @@ -134,17 +144,17 @@ func ArticleListget(c *gin.Context) { // 4.4 组装单个文评的响应数据 articleData := ArticleResponse{ - ID: article.ArticleID, - Title: article.Title, - VoteType: article.VoteType, - TotalVoters: article.TotalVotersNum, - EndTime: article.EndTime, - IsEnded: article.IsEnded, - PublisherID: article.PublishUserID, - CreateTime: article.CreateTime, - Options: optionList, - UserHasVoted: userHasVoted, // 整体标记用户是否投过票 - VotedOptionID: votedOptionID, // 记录用户投票的选项ID(未投票则为null) + ID: article.ArticleID, + Title: article.Title, + VoteType: article.VoteType, + TotalVoters: article.TotalVotersNum, + EndTime: article.EndTime, + IsEnded: article.IsEnded, + PublisherID: article.PublishUserID, + CreateTime: article.CreateTime, + Options: optionList, + UserHasVoted: userHasVoted, // 整体标记用户是否投过票 + VotedOptionIDs: votedOptionIDs, // 记录用户投票的所有选项ID(未投票则为空数组) } // 4.5 加入结果集 diff --git a/controllers/article/votearticle.go b/controllers/article/votearticle.go index 7117754..fe3402f 100644 --- a/controllers/article/votearticle.go +++ b/controllers/article/votearticle.go @@ -1,7 +1,6 @@ package article import ( - "fmt" "net/http" "time" "toutoukan/init/databaseInit" @@ -10,10 +9,20 @@ import ( "gorm.io/gorm" ) +// 投票请求结构体,同时支持单选(optionId)和多选(optionIds) type VoteArticleRequest struct { - Uid string `json:"uid" binding:"required,min=1,max=50"` // 用户ID,非空且长度限制 - ArticleID int64 `json:"articleId" binding:"required,min=1"` // 文评ID,非空且为正整数 - OptionID int64 `json:"optionId" binding:"required,min=1"` // 选项ID,投票必需 + Uid string `json:"uid" binding:"required,min=1,max=50"` // 用户ID + ArticleID int64 `json:"articleId" 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) { @@ -42,31 +51,11 @@ func VoteArticle(c *gin.Context) { } }() - // 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 - } - + // 3. 检查文章状态和投票类型 var articleStatus struct { - IsEnded bool `gorm:"column:is_ended"` - EndTime time.Time `gorm:"column:end_time"` // 新增:获取截止时间 + IsEnded bool `gorm:"column:is_ended"` + EndTime time.Time `gorm:"column:end_time"` + VoteType string `gorm:"column:vote_type"` // 投票类型:single-单选,multiple-多选 } if err := tx.Table("article_list"). Where("articleId = ?", req.ArticleID). @@ -80,70 +69,95 @@ func VoteArticle(c *gin.Context) { return } - // 双重检查:已标记结束 或 已过截止时间,均禁止投票 + // 3.1 检查投票是否已结束 now := time.Now() if articleStatus.IsEnded || articleStatus.EndTime.Before(now) { tx.Rollback() - - // 区分提示信息(可选,增强用户体验) - var errMsg string - if articleStatus.IsEnded { - errMsg = "该文评投票已结束,无法投票" - } else { - errMsg = "该文评投票已过截止时间,无法投票" - } - - c.JSON(http.StatusForbidden, gin.H{"error": errMsg}) + c.JSON(http.StatusForbidden, gin.H{"error": "该文评投票已结束或已过截止时间,无法投票"}) return } - // 额外优化:如果已过截止时间但is_ended未更新,主动更新状态(可选) - if !articleStatus.IsEnded && articleStatus.EndTime.Before(now) { - if err := tx.Table("article_list"). - Where("articleId = ?", req.ArticleID). - Update("is_ended", true).Error; err != nil { - // 此处不回滚,仅记录警告,不影响主要逻辑 - fmt.Printf("警告:更新过期文评状态失败,articleId=%d, err=%v\n", req.ArticleID, err) + // 4. 处理投票选项,统一转换为选项ID列表 + var selectedOptions []int64 + if articleStatus.VoteType == "single" { + if req.OptionID == 0 || len(req.OptionIDs) > 0 { + tx.Rollback() + c.JSON(http.StatusForbidden, gin.H{ + "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. 检查选项是否属于该文评 - var optionCheck struct{ ID int64 } - if err := tx.Table("article_options"). - Where("id = ? AND vote_article_id = ?", req.OptionID, req.ArticleID). - First(&optionCheck).Error; err != nil { + // 5. 检查用户是否已投票 + var voteCount int64 + if err := tx.Table("user_votes"). + Where("user_id = ? AND vote_article_id = ?", req.Uid, req.ArticleID). + Count(&voteCount).Error; err != nil { 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 } - - // 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 { + if voteCount > 0 { tx.Rollback() - c.JSON(http.StatusInternalServerError, gin.H{"error": "记录投票失败: " + err.Error()}) + c.JSON(http.StatusForbidden, gin.H{"error": "您已对该文评投过票,无法重复投票"}) return } - // 6.2 更新选项票数 + // 6. 批量检查所有选项是否属于该文评 + var existingOptionsCount int64 if err := tx.Table("article_options"). - Where("id = ?", req.OptionID). + Where("vote_article_id = ? AND id IN ?", req.ArticleID, selectedOptions). + Count(&existingOptionsCount).Error; err != nil { + tx.Rollback() + c.JSON(http.StatusInternalServerError, gin.H{"error": "查询选项失败: " + err.Error()}) + return + } + if existingOptionsCount != int64(len(selectedOptions)) { + tx.Rollback() + c.JSON(http.StatusNotFound, gin.H{"error": "部分选项ID不存在或不属于该文评"}) + return + } + + // 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"). + Where("id IN ?", selectedOptions). Update("option_votes_num", gorm.Expr("option_votes_num + 1")).Error; err != nil { tx.Rollback() - c.JSON(http.StatusInternalServerError, gin.H{"error": "更新选项票数失败: " + err.Error()}) + c.JSON(http.StatusInternalServerError, gin.H{"error": "批量更新选项票数失败: " + err.Error()}) return } - // 6.3 更新文评总投票人数 + // 7.3 更新文评总投票人数 if err := tx.Table("article_list"). Where("articleId = ?", req.ArticleID). Update("total_voters_num", gorm.Expr("total_voters_num + 1")).Error; err != nil { @@ -152,21 +166,27 @@ func VoteArticle(c *gin.Context) { return } - // 7. 提交事务 + // 8. 提交事务 if err := tx.Commit().Error; err != nil { tx.Rollback() c.JSON(http.StatusInternalServerError, gin.H{"error": "提交投票失败: " + err.Error()}) 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{ "success": true, "message": "投票成功", - "data": gin.H{ - "articleId": req.ArticleID, - "optionId": req.OptionID, - "voteTime": time.Now().Format("2006-01-02 15:04:05"), - }, + "data": responseData, }) }