2025-09-24 01:07:21 +08:00
|
|
|
|
package article
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"toutoukan/init/databaseInit"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2025-09-24 01:48:12 +08:00
|
|
|
|
"gorm.io/gorm"
|
2025-09-24 01:07:21 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-09-24 01:48:12 +08:00
|
|
|
|
// ArticleOption 文评选项结构
|
2025-09-24 01:07:21 +08:00
|
|
|
|
type ArticleOption struct {
|
2025-09-24 01:48:12 +08:00
|
|
|
|
ID int64 `json:"id"` // 选项ID
|
|
|
|
|
|
Name string `json:"name"` // 选项名称
|
|
|
|
|
|
Votes int `json:"votes"` // 该选项的投票数
|
|
|
|
|
|
IsVotedByUser bool `json:"is_voted"` // 当前用户是否投了该选项
|
2025-09-24 01:07:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-24 01:48:12 +08:00
|
|
|
|
// ArticleResponse 单个文评的响应结构
|
2025-09-24 01:07:21 +08:00
|
|
|
|
type ArticleResponse struct {
|
2025-09-24 15:34:09 +08:00
|
|
|
|
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(未投票为空数组)
|
2025-09-24 01:07:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-24 01:48:12 +08:00
|
|
|
|
type UserReq struct {
|
|
|
|
|
|
Uid string `json:"uid"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ArticleListget 获取所有文评及选项信息(包含用户投票状态)
|
2025-09-24 01:07:21 +08:00
|
|
|
|
func ArticleListget(c *gin.Context) {
|
2025-09-24 01:48:12 +08:00
|
|
|
|
var req UserReq
|
|
|
|
|
|
|
|
|
|
|
|
// 1. 解析并验证请求参数
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
|
|
|
|
"error": "参数解析失败",
|
|
|
|
|
|
"detail": err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 查询所有文评
|
2025-09-24 01:07:21 +08:00
|
|
|
|
var articles []struct {
|
|
|
|
|
|
ArticleID int64 `gorm:"column:articleId"`
|
|
|
|
|
|
Title string `gorm:"column:title"`
|
|
|
|
|
|
VoteType string `gorm:"column:vote_type"`
|
|
|
|
|
|
TotalVotersNum int `gorm:"column:total_voters_num"`
|
|
|
|
|
|
EndTime string `gorm:"column:end_time"`
|
|
|
|
|
|
IsEnded bool `gorm:"column:is_ended"`
|
|
|
|
|
|
PublishUserID string `gorm:"column:publish_user_id"`
|
|
|
|
|
|
CreateTime string `gorm:"column:create_time"`
|
|
|
|
|
|
}
|
2025-09-24 01:48:12 +08:00
|
|
|
|
|
2025-09-24 01:07:21 +08:00
|
|
|
|
if err := databaseInit.UserDB.Table("article_list").
|
|
|
|
|
|
Select("articleId, title, vote_type, total_voters_num, end_time, is_ended, publish_user_id, create_time").
|
|
|
|
|
|
Find(&articles).Error; err != nil {
|
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
|
|
|
|
"error": "查询文评失败: " + err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fmt.Println("所有文评列表:", articles)
|
|
|
|
|
|
|
2025-09-24 01:48:12 +08:00
|
|
|
|
// 3. 构建结果映射
|
2025-09-24 01:07:21 +08:00
|
|
|
|
result := make(map[string]interface{})
|
|
|
|
|
|
|
2025-09-24 01:48:12 +08:00
|
|
|
|
// 4. 逐个处理文评
|
2025-09-24 01:07:21 +08:00
|
|
|
|
for _, article := range articles {
|
2025-09-24 01:48:12 +08:00
|
|
|
|
// 4.1 查询当前文评的选项
|
2025-09-24 01:07:21 +08:00
|
|
|
|
var options []struct {
|
2025-09-24 01:48:12 +08:00
|
|
|
|
ID int64 `gorm:"column:id"`
|
2025-09-24 01:07:21 +08:00
|
|
|
|
OptionName string `gorm:"column:option_content"`
|
|
|
|
|
|
VoteCount int `gorm:"column:option_votes_num"`
|
2025-09-24 01:48:12 +08:00
|
|
|
|
SortOrder int `gorm:"column:sort_order"`
|
2025-09-24 01:07:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := databaseInit.UserDB.Table("article_options").
|
|
|
|
|
|
Where("vote_article_id = ?", article.ArticleID).
|
2025-09-24 01:48:12 +08:00
|
|
|
|
Select("id, option_content, option_votes_num, sort_order").
|
2025-09-24 01:07:21 +08:00
|
|
|
|
Order("sort_order ASC").
|
|
|
|
|
|
Find(&options).Error; err != nil {
|
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
|
|
|
|
"error": "查询文评选项失败: " + err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-24 15:34:09 +08:00
|
|
|
|
// 4.2 查询当前用户对该文评的所有投票记录
|
|
|
|
|
|
var userVotes []struct {
|
2025-09-24 01:48:12 +08:00
|
|
|
|
OptionID int64 `gorm:"column:option_id"`
|
|
|
|
|
|
}
|
|
|
|
|
|
voteErr := databaseInit.UserDB.Table("user_votes").
|
|
|
|
|
|
Where("user_id = ? AND vote_article_id = ?", req.Uid, article.ArticleID).
|
2025-09-24 15:34:09 +08:00
|
|
|
|
Find(&userVotes).Error
|
2025-09-24 01:48:12 +08:00
|
|
|
|
|
2025-09-24 15:34:09 +08:00
|
|
|
|
// 标记用户是否投票及所有投票的选项ID
|
2025-09-24 01:48:12 +08:00
|
|
|
|
userHasVoted := false
|
2025-09-24 15:34:09 +08:00
|
|
|
|
var votedOptionIDs []int64 = []int64{}
|
|
|
|
|
|
|
|
|
|
|
|
if voteErr == nil && len(userVotes) > 0 {
|
2025-09-24 01:48:12 +08:00
|
|
|
|
userHasVoted = true
|
2025-09-24 15:34:09 +08:00
|
|
|
|
// 收集所有投票的选项ID
|
|
|
|
|
|
for _, vote := range userVotes {
|
|
|
|
|
|
votedOptionIDs = append(votedOptionIDs, vote.OptionID)
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if voteErr != nil && voteErr != gorm.ErrRecordNotFound {
|
2025-09-24 01:48:12 +08:00
|
|
|
|
// 处理查询错误(非"未找到"的错误)
|
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
|
|
|
|
"error": "查询用户投票记录失败: " + voteErr.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 4.3 格式化选项数据(标记用户是否投了该选项)
|
2025-09-24 01:07:21 +08:00
|
|
|
|
var optionList []ArticleOption
|
|
|
|
|
|
for _, opt := range options {
|
2025-09-24 15:34:09 +08:00
|
|
|
|
// 检查当前选项是否是用户投票的选项之一
|
|
|
|
|
|
isVoted := false
|
|
|
|
|
|
for _, votedID := range votedOptionIDs {
|
|
|
|
|
|
if opt.ID == votedID {
|
|
|
|
|
|
isVoted = true
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-24 01:48:12 +08:00
|
|
|
|
|
2025-09-24 01:07:21 +08:00
|
|
|
|
optionList = append(optionList, ArticleOption{
|
2025-09-24 01:48:12 +08:00
|
|
|
|
ID: opt.ID,
|
|
|
|
|
|
Name: opt.OptionName,
|
|
|
|
|
|
Votes: opt.VoteCount,
|
|
|
|
|
|
IsVotedByUser: isVoted, // 标记当前用户是否投了这个选项
|
2025-09-24 01:07:21 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-24 01:48:12 +08:00
|
|
|
|
// 4.4 组装单个文评的响应数据
|
2025-09-24 01:07:21 +08:00
|
|
|
|
articleData := ArticleResponse{
|
2025-09-24 15:34:09 +08:00
|
|
|
|
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(未投票则为空数组)
|
2025-09-24 01:07:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-24 01:48:12 +08:00
|
|
|
|
// 4.5 加入结果集
|
2025-09-24 01:07:21 +08:00
|
|
|
|
result[article.Title] = articleData
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
|
|
|
|
}
|