2025-09-24 01:07:21 +08:00
|
|
|
|
package article
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"net/http"
|
2025-09-24 20:57:23 +08:00
|
|
|
|
"time"
|
2025-09-24 01:07:21 +08:00
|
|
|
|
"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 20:57:23 +08:00
|
|
|
|
// ArticleList 数据库文章记录结构体
|
|
|
|
|
|
type ArticleList struct {
|
2025-09-27 23:15:44 +08:00
|
|
|
|
ArticleID int64 `gorm:"column:articleId;primaryKey;autoIncrement"`
|
|
|
|
|
|
PublishUserID string `gorm:"column:publish_user_id"`
|
|
|
|
|
|
Title string `gorm:"column:title"`
|
|
|
|
|
|
VoteType string `gorm:"column:vote_type"`
|
|
|
|
|
|
EndTime time.Time `gorm:"column:end_time"`
|
|
|
|
|
|
IsEnded bool `gorm:"column:is_ended"`
|
|
|
|
|
|
TotalVotersNum int `gorm:"column:total_voters_num"`
|
|
|
|
|
|
CreateTime time.Time `gorm:"column:create_time"`
|
|
|
|
|
|
Options []ArticleOption `gorm:"foreignKey:VoteArticleID;references:ArticleID"`
|
2025-09-24 20:57:23 +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 20:57:23 +08:00
|
|
|
|
ID int64 `gorm:"column:id"`
|
2025-09-27 23:15:44 +08:00
|
|
|
|
VoteArticleID int64 `gorm:"column:vote_article_id"`
|
2025-09-24 20:57:23 +08:00
|
|
|
|
OptionContent string `gorm:"column:option_content"`
|
|
|
|
|
|
OptionVotesNum int `gorm:"column:option_votes_num"`
|
|
|
|
|
|
SortOrder int `gorm:"column:sort_order"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ArticleOptionResp 格式化后的选项响应结构
|
|
|
|
|
|
type ArticleOptionResp struct {
|
|
|
|
|
|
ID int64 `json:"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-25 23:12:18 +08:00
|
|
|
|
ID int64 `json:"article_id"`
|
|
|
|
|
|
Title string `json:"article_title"`
|
|
|
|
|
|
VoteType string `json:"vote_type"`
|
|
|
|
|
|
TotalVoters int `json:"total_voters"`
|
|
|
|
|
|
EndTime string `json:"end_time"`
|
|
|
|
|
|
IsEnded bool `json:"is_ended"`
|
|
|
|
|
|
PublisherID string `json:"publisher_id"`
|
2025-10-08 01:39:12 +08:00
|
|
|
|
PublisherName string `json:"publisher_name"`
|
2025-09-25 23:12:18 +08:00
|
|
|
|
CreateTime string `json:"create_time"`
|
|
|
|
|
|
Options []ArticleOptionResp `json:"options"`
|
|
|
|
|
|
UserHasVoted bool `json:"user_has_voted"`
|
|
|
|
|
|
VotedOptionIDs []int64 `json:"user_voted_option_ids"`
|
2025-09-24 01:07:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-27 23:15:44 +08:00
|
|
|
|
// UserReq 请求参数结构体,允许uid为空
|
2025-09-24 01:48:12 +08:00
|
|
|
|
type UserReq struct {
|
2025-09-27 23:15:44 +08:00
|
|
|
|
Uid string `json:"uid" binding:"omitempty"` // 允许uid为空,空字符串也合法
|
2025-09-24 01:48:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-27 23:15:44 +08:00
|
|
|
|
// ArticleListget 获取所有文评及选项信息(支持空uid场景)
|
2025-09-24 01:07:21 +08:00
|
|
|
|
func ArticleListget(c *gin.Context) {
|
2025-09-24 01:48:12 +08:00
|
|
|
|
var req UserReq
|
|
|
|
|
|
|
2025-10-08 01:39:12 +08:00
|
|
|
|
// 1. 解析请求参数
|
2025-09-24 01:48:12 +08:00
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
|
|
|
|
"error": "参数解析失败",
|
|
|
|
|
|
"detail": err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-27 23:15:44 +08:00
|
|
|
|
// 2. 查询所有文章及关联的选项
|
2025-09-24 20:57:23 +08:00
|
|
|
|
var articles []ArticleList
|
|
|
|
|
|
if err := databaseInit.UserDB.Model(&ArticleList{}).
|
|
|
|
|
|
Preload("Options", func(db *gorm.DB) *gorm.DB {
|
|
|
|
|
|
return db.Order("sort_order ASC")
|
|
|
|
|
|
}).
|
2025-09-24 01:07:21 +08:00
|
|
|
|
Find(&articles).Error; err != nil {
|
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
|
|
|
|
"error": "查询文评失败: " + err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-08 01:39:12 +08:00
|
|
|
|
// 3. 批量查询所有发布者的用户名(核心修改:获取uid对应的username)
|
|
|
|
|
|
// 3.1 收集所有发布者的uid(去重)
|
|
|
|
|
|
uniquePublishUids := make(map[string]struct{})
|
|
|
|
|
|
for _, article := range articles {
|
|
|
|
|
|
uniquePublishUids[article.PublishUserID] = struct{}{}
|
|
|
|
|
|
}
|
|
|
|
|
|
// 转换为切片便于查询
|
|
|
|
|
|
publishUidList := make([]string, 0, len(uniquePublishUids))
|
|
|
|
|
|
for uid := range uniquePublishUids {
|
|
|
|
|
|
publishUidList = append(publishUidList, uid)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 3.2 从用户表查询这些uid对应的用户名(假设用户表为`users`,字段为`user_id`和`username`)
|
|
|
|
|
|
type userInfo struct {
|
|
|
|
|
|
UserID string `gorm:"column:uid"` // 对应发布者的PublishUserID
|
|
|
|
|
|
Username string `gorm:"column:username"` // 发布者用户名
|
|
|
|
|
|
}
|
|
|
|
|
|
var publishUsers []userInfo
|
|
|
|
|
|
if len(publishUidList) > 0 {
|
|
|
|
|
|
if err := databaseInit.UserDB.Table("user_info").
|
|
|
|
|
|
Where("uid IN (?)", publishUidList).
|
|
|
|
|
|
Find(&publishUsers).Error; err != nil {
|
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
|
|
|
|
"error": "查询发布者信息失败: " + err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 3.3 构建uid到username的映射(方便快速查询)
|
|
|
|
|
|
uidToUsername := make(map[string]string)
|
|
|
|
|
|
for _, user := range publishUsers {
|
|
|
|
|
|
uidToUsername[user.UserID] = user.Username
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 4. 查询用户投票记录(原有逻辑,保持不变)
|
2025-09-24 20:57:23 +08:00
|
|
|
|
userVotesMap := make(map[int64][]int64)
|
2025-09-27 23:15:44 +08:00
|
|
|
|
uidValid := req.Uid != ""
|
|
|
|
|
|
if uidValid {
|
|
|
|
|
|
var userAllVotes []UserVote
|
|
|
|
|
|
if err := databaseInit.UserDB.Table("user_votes").
|
|
|
|
|
|
Where("user_id = ?", req.Uid).
|
|
|
|
|
|
Find(&userAllVotes).Error; err != nil && err != gorm.ErrRecordNotFound {
|
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
|
|
|
|
"error": "查询用户投票记录失败: " + err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, vote := range userAllVotes {
|
|
|
|
|
|
userVotesMap[vote.VoteArticleID] = append(userVotesMap[vote.VoteArticleID], vote.OptionID)
|
|
|
|
|
|
}
|
2025-09-24 20:57:23 +08:00
|
|
|
|
}
|
2025-09-24 01:07:21 +08:00
|
|
|
|
|
2025-10-08 01:39:12 +08:00
|
|
|
|
// 5. 组装响应数据(添加用户名)
|
2025-09-24 20:57:23 +08:00
|
|
|
|
var responseList []ArticleResponse
|
2025-09-24 01:07:21 +08:00
|
|
|
|
for _, article := range articles {
|
2025-10-08 01:39:12 +08:00
|
|
|
|
// 5.1 处理投票状态(原有逻辑)
|
2025-09-27 23:15:44 +08:00
|
|
|
|
var votedOptionIDs []int64
|
|
|
|
|
|
var userHasVoted bool
|
|
|
|
|
|
if uidValid {
|
|
|
|
|
|
votedOptionIDs, userHasVoted = userVotesMap[article.ArticleID]
|
|
|
|
|
|
} else {
|
|
|
|
|
|
votedOptionIDs = []int64{}
|
|
|
|
|
|
userHasVoted = false
|
|
|
|
|
|
}
|
2025-09-24 01:48:12 +08:00
|
|
|
|
|
2025-10-08 01:39:12 +08:00
|
|
|
|
// 5.2 处理选项列表(原有逻辑)
|
2025-09-24 20:57:23 +08:00
|
|
|
|
var optionList []ArticleOptionResp
|
|
|
|
|
|
for _, opt := range article.Options {
|
2025-09-24 15:34:09 +08:00
|
|
|
|
isVoted := false
|
2025-09-27 23:15:44 +08:00
|
|
|
|
if uidValid {
|
|
|
|
|
|
for _, votedID := range votedOptionIDs {
|
|
|
|
|
|
if opt.ID == votedID {
|
|
|
|
|
|
isVoted = true
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
2025-09-24 15:34:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-24 20:57:23 +08:00
|
|
|
|
optionList = append(optionList, ArticleOptionResp{
|
2025-09-24 01:48:12 +08:00
|
|
|
|
ID: opt.ID,
|
2025-09-24 20:57:23 +08:00
|
|
|
|
Name: opt.OptionContent,
|
|
|
|
|
|
Votes: opt.OptionVotesNum,
|
|
|
|
|
|
IsVotedByUser: isVoted,
|
2025-09-24 01:07:21 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-08 01:39:12 +08:00
|
|
|
|
// 5.3 获取发布者用户名(核心修改:从映射中查询)
|
|
|
|
|
|
publisherName := uidToUsername[article.PublishUserID]
|
|
|
|
|
|
if publisherName == "" {
|
|
|
|
|
|
publisherName = "未知用户" // 处理用户不存在的情况
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 5.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,
|
2025-09-24 20:57:23 +08:00
|
|
|
|
EndTime: article.EndTime.Format("2006-01-02 15:04:05"),
|
2025-09-24 15:34:09 +08:00
|
|
|
|
IsEnded: article.IsEnded,
|
|
|
|
|
|
PublisherID: article.PublishUserID,
|
2025-10-08 01:39:12 +08:00
|
|
|
|
PublisherName: publisherName, // 填充用户名
|
2025-09-24 20:57:23 +08:00
|
|
|
|
CreateTime: article.CreateTime.Format("2006-01-02 15:04:05"),
|
2025-09-24 15:34:09 +08:00
|
|
|
|
Options: optionList,
|
2025-09-24 20:57:23 +08:00
|
|
|
|
UserHasVoted: userHasVoted,
|
|
|
|
|
|
VotedOptionIDs: votedOptionIDs,
|
2025-09-24 01:07:21 +08:00
|
|
|
|
}
|
2025-09-24 20:57:23 +08:00
|
|
|
|
responseList = append(responseList, articleData)
|
2025-09-24 01:07:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-08 01:39:12 +08:00
|
|
|
|
// 6. 返回响应
|
2025-09-24 20:57:23 +08:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": true,
|
|
|
|
|
|
"message": fmt.Sprintf("查询成功,共 %d 篇文章", len(responseList)),
|
|
|
|
|
|
"data": responseList,
|
|
|
|
|
|
})
|
2025-09-24 01:07:21 +08:00
|
|
|
|
}
|