You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
1.9 KiB
84 lines
1.9 KiB
package vote
|
|
|
|
import (
|
|
"context"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
v1 "practice_ArticleVote_Go/api/vote/v1"
|
|
)
|
|
|
|
func (l *VoteLogic) GetAllVote(ctx context.Context, req *v1.VoteReq) (res *v1.VoteRes, err error) {
|
|
|
|
res = &v1.VoteRes{
|
|
Total: 0,
|
|
Rows: []v1.Vote{},
|
|
}
|
|
|
|
query := `
|
|
SELECT
|
|
a.article_title AS articleTitle,
|
|
v.id AS voteId,
|
|
v.vote_title AS voteTitle,
|
|
v.create_time AS createTime,
|
|
v.deadline_time AS deadlineTime,
|
|
v.status
|
|
FROM article a
|
|
INNER JOIN vote_poll v ON a.id = v.article_id
|
|
WHERE a.user_id = ?
|
|
AND a.vote_status = 1
|
|
`
|
|
|
|
conditions := []interface{}{req.UserId}
|
|
|
|
if req.ArticleTitle != "" {
|
|
query += " AND a.article_title LIKE ?"
|
|
conditions = append(conditions, "%"+req.ArticleTitle+"%")
|
|
}
|
|
if req.VoteTitle != "" {
|
|
query += " AND v.vote_title LIKE ?"
|
|
conditions = append(conditions, "%"+req.VoteTitle+"%")
|
|
}
|
|
if req.CreateTime != nil {
|
|
query += " AND DATE(v.create_time) = ?"
|
|
conditions = append(conditions, req.CreateTime)
|
|
}
|
|
if req.DeadlineTime != nil {
|
|
query += " AND DATE(v.deadline_time) = ?"
|
|
conditions = append(conditions, req.DeadlineTime)
|
|
}
|
|
if req.Status != 0 {
|
|
query += " AND v.status = ?"
|
|
conditions = append(conditions, req.Status)
|
|
}
|
|
|
|
// 构造 countQuery:使用相同的 baseQuery 和 WHERE 条件,但只统计行数
|
|
var cntResult struct{ Cnt int }
|
|
countQuery := "SELECT COUNT(*) AS cnt FROM (" + query + ") AS subquery"
|
|
err = g.DB().Ctx(ctx).
|
|
Raw(countQuery, conditions...).
|
|
Scan(&cntResult)
|
|
res = &v1.VoteRes{
|
|
Total: cntResult.Cnt,
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
query += " ORDER BY v.create_time DESC "
|
|
query += " LIMIT ?, ? "
|
|
offset := (req.Page - 1) * req.PageSize
|
|
conditions = append(conditions, offset, req.PageSize)
|
|
|
|
err = g.DB().Ctx(ctx).
|
|
Raw(query, conditions...).
|
|
Scan(&res.Rows)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 设置分页信息
|
|
res.Page = req.Page
|
|
res.PageSize = req.PageSize
|
|
|
|
return res, nil
|
|
}
|