From bbed99bb433aad9a551c40faae82c4a23e1a0eaa Mon Sep 17 00:00:00 2001 From: liruiqiang <3151805288@qq.com> Date: Tue, 24 Jun 2025 13:51:09 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[lrq]go=E8=AF=AD=E8=A8=80=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E7=94=9F=E6=88=90=E6=97=B6=E9=97=B4=E6=94=B9=E4=B8=BA=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manifest/config/config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifest/config/config.yaml b/manifest/config/config.yaml index 881daa8..f2fc73d 100644 --- a/manifest/config/config.yaml +++ b/manifest/config/config.yaml @@ -12,6 +12,6 @@ logger: # https://goframe.org/docs/core/gdb-config-file database: default: - link: "mysql:root:123456@tcp(127.0.0.1:3306)/link_test" + link: "mysql:root:root@tcp(127.0.0.1:3306)/link_test?loc=Local&parseTime=true" createdAt: "create_time" updatedAt: "update_time" \ No newline at end of file From 31b2aba444975752a02893e1777ec619a1ec9a05 Mon Sep 17 00:00:00 2001 From: liruiqiang <3151805288@qq.com> Date: Tue, 24 Jun 2025 13:52:09 +0800 Subject: [PATCH 2/2] =?UTF-8?q?[lrq]=E6=96=87=E7=AB=A0=E6=8A=95=E7=A5=A8?= =?UTF-8?q?=E6=B4=BB=E5=8A=A8=E6=89=80=E6=9C=89=E5=88=97=E8=A1=A8=EF=BC=88?= =?UTF-8?q?=E5=8C=85=E6=8B=AC=E7=AD=9B=E9=80=89=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/vote/v1/vote.go | 34 ++++++++++ api/vote/vote.go | 10 +++ internal/cmd/cmd.go | 10 ++- internal/controller/vote/vote_new.go | 11 ++++ internal/controller/vote/vote_v1_get_votelist.go | 11 ++++ internal/logic/vote/get_allvote.go | 84 ++++++++++++++++++++++++ internal/logic/vote/vote_logic.go | 7 ++ internal/service/vote.go | 15 +++++ 8 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 api/vote/v1/vote.go create mode 100644 api/vote/vote.go create mode 100644 internal/controller/vote/vote_new.go create mode 100644 internal/controller/vote/vote_v1_get_votelist.go create mode 100644 internal/logic/vote/get_allvote.go create mode 100644 internal/logic/vote/vote_logic.go create mode 100644 internal/service/vote.go diff --git a/api/vote/v1/vote.go b/api/vote/v1/vote.go new file mode 100644 index 0000000..c6b1644 --- /dev/null +++ b/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:"投票活动状态"` +} diff --git a/api/vote/vote.go b/api/vote/vote.go new file mode 100644 index 0000000..37f8542 --- /dev/null +++ b/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) +} diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index 7d4c26e..a34902f 100644 --- a/internal/cmd/cmd.go +++ b/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 }, diff --git a/internal/controller/vote/vote_new.go b/internal/controller/vote/vote_new.go new file mode 100644 index 0000000..70f9a6f --- /dev/null +++ b/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{} +} diff --git a/internal/controller/vote/vote_v1_get_votelist.go b/internal/controller/vote/vote_v1_get_votelist.go new file mode 100644 index 0000000..72c56f5 --- /dev/null +++ b/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) +} diff --git a/internal/logic/vote/get_allvote.go b/internal/logic/vote/get_allvote.go new file mode 100644 index 0000000..b6102cf --- /dev/null +++ b/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 +} diff --git a/internal/logic/vote/vote_logic.go b/internal/logic/vote/vote_logic.go new file mode 100644 index 0000000..ebed253 --- /dev/null +++ b/internal/logic/vote/vote_logic.go @@ -0,0 +1,7 @@ +package vote + +type VoteLogic struct{} + +func NewVoteLogic() *VoteLogic { + return &VoteLogic{} +} diff --git a/internal/service/vote.go b/internal/service/vote.go new file mode 100644 index 0000000..838dc68 --- /dev/null +++ b/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() +}