Browse Source

[lrq]文章投票活动所有列表(包括筛选)

master
liruiqiang 1 week ago
parent
commit
31b2aba444
  1. 34
      api/vote/v1/vote.go
  2. 10
      api/vote/vote.go
  3. 10
      internal/cmd/cmd.go
  4. 11
      internal/controller/vote/vote_new.go
  5. 11
      internal/controller/vote/vote_v1_get_votelist.go
  6. 84
      internal/logic/vote/get_allvote.go
  7. 7
      internal/logic/vote/vote_logic.go
  8. 15
      internal/service/vote.go

34
api/vote/v1/vote.go

@ -0,0 +1,34 @@
package v1
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
)
type VoteReq struct {
g.Meta `path:"/getAllVote" method:"get" tags:"获取用户投票活动"`
UserId int64 `json:"userId" v:"required" dc:"用户ID"`
ArticleTitle string `json:"articleTitle" dc:"文章/视频标题"`
VoteTitle string `json:"voteTitle" dc:"投票活动标题"`
CreateTime *gtime.Time `json:"createTime" dc:"活动创建时间"`
DeadlineTime *gtime.Time `json:"deadlineTime" dc:"活动截止时间"`
Status int `json:"status" dc:"投票活动状态"`
Page int `p:"page" v:"required|integer|min:1#数据页不能为空|数据页只能是整数|数据页不能小于1" dc:"页数"`
PageSize int `p:"page_size" v:"integer|min:1#数据大小只能为整数|数据大小不能小于1" d:"20" dc:"每页数据量"`
}
type VoteRes struct {
Page int `json:"page" dc:"页数"`
PageSize int `json:"page_size" dc:"每页数据量"`
Total int `json:"total" dc:"总记录数"`
Rows []Vote `json:"rows" dc:"投票活动列表"`
}
type Vote struct {
ArticleTitle string `json:"articleTitle" dc:"文章/视频标题"`
VoteId int `json:"voteId" dc:"投票活动ID"`
VoteTitle string `json:"voteTitle" dc:"投票活动标题"`
CreateTime *gtime.Time `json:"createTime" dc:"活动创建时间"`
DeadlineTime *gtime.Time `json:"deadlineTime" dc:"活动截止时间"`
Status int `json:"status" dc:"投票活动状态"`
}

10
api/vote/vote.go

@ -0,0 +1,10 @@
package vote
import (
"context"
v1 "practice_ArticleVote_Go/api/vote/v1"
)
type IVoteV1 interface {
GetAllVote(ctx context.Context, req *v1.VoteReq) (res *v1.VoteRes, err error)
}

10
internal/cmd/cmd.go

@ -4,6 +4,7 @@ import (
"context"
"net/http"
"practice_ArticleVote_Go/internal/controller/article"
vo "practice_ArticleVote_Go/internal/controller/vote"
vr "practice_ArticleVote_Go/internal/controller/vote_record"
"github.com/gogf/gf/v2/frame/g"
@ -29,7 +30,7 @@ var (
s.Group("/", func(group *ghttp.RouterGroup) {
group.Middleware(ghttp.MiddlewareHandlerResponse)
group.Bind(article.ArticlecController)
group.Bind(article.ArticleController)
})
s.Group("/vote", func(group *ghttp.RouterGroup) {
@ -38,6 +39,13 @@ var (
vr.NewV1(),
)
})
s.Group("/user", func(group *ghttp.RouterGroup) {
group.Middleware(ghttp.MiddlewareHandlerResponse)
group.Bind(
vo.VoteNewV1(),
)
})
s.Run()
return nil
},

11
internal/controller/vote/vote_new.go

@ -0,0 +1,11 @@
package vote
import (
"practice_ArticleVote_Go/api/vote"
)
type VoteControllerV1 struct{}
func VoteNewV1() vote.IVoteV1 {
return &VoteControllerV1{}
}

11
internal/controller/vote/vote_v1_get_votelist.go

@ -0,0 +1,11 @@
package vote
import (
"context"
v1 "practice_ArticleVote_Go/api/vote/v1"
"practice_ArticleVote_Go/internal/service"
)
func (c *VoteControllerV1) GetAllVote(ctx context.Context, req *v1.VoteReq) (res *v1.VoteRes, err error) {
return service.NewVoteService().GetAllVote(ctx, req)
}

84
internal/logic/vote/get_allvote.go

@ -0,0 +1,84 @@
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
}

7
internal/logic/vote/vote_logic.go

@ -0,0 +1,7 @@
package vote
type VoteLogic struct{}
func NewVoteLogic() *VoteLogic {
return &VoteLogic{}
}

15
internal/service/vote.go

@ -0,0 +1,15 @@
package service
import (
"context"
v1 "practice_ArticleVote_Go/api/vote/v1"
"practice_ArticleVote_Go/internal/logic/vote"
)
type IVote interface {
GetAllVote(ctx context.Context, req *v1.VoteReq) (res *v1.VoteRes, err error)
}
func NewVoteService() IVote {
return vote.NewVoteLogic()
}
Loading…
Cancel
Save