9 changed files with 181 additions and 1 deletions
-
34api/vote/v1/vote.go
-
10api/vote/vote.go
-
8internal/cmd/cmd.go
-
11internal/controller/vote/vote_new.go
-
11internal/controller/vote/vote_v1_get_votelist.go
-
84internal/logic/vote/get_allvote.go
-
7internal/logic/vote/vote_logic.go
-
15internal/service/vote.go
-
2manifest/config/config.yaml
@ -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:"投票活动状态"` |
|||
} |
@ -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) |
|||
} |
@ -0,0 +1,11 @@ |
|||
package vote |
|||
|
|||
import ( |
|||
"practice_ArticleVote_Go/api/vote" |
|||
) |
|||
|
|||
type VoteControllerV1 struct{} |
|||
|
|||
func VoteNewV1() vote.IVoteV1 { |
|||
return &VoteControllerV1{} |
|||
} |
@ -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) |
|||
} |
@ -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 |
|||
} |
@ -0,0 +1,7 @@ |
|||
package vote |
|||
|
|||
type VoteLogic struct{} |
|||
|
|||
func NewVoteLogic() *VoteLogic { |
|||
return &VoteLogic{} |
|||
} |
@ -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() |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue