From 837f348d42fdd75dd9ad5ea20b35406a5b65af6b Mon Sep 17 00:00:00 2001 From: maziyang Date: Tue, 24 Jun 2025 10:09:03 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=8A=95=E7=A5=A8=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E5=92=8C=E8=8E=B7=E5=8F=96=E6=8A=95=E7=A5=A8=E5=90=8D?= =?UTF-8?q?=E5=8D=95=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/vote_record/v1/vote_record.go | 24 ++++-- api/vote_record/vote_record.go | 1 + .../vote_record/vote_record_v1_get_vote_detail.go | 12 +++ internal/logic/vote_record/get_vote_detail.go | 95 ++++++++++++++++++++++ internal/service/vote_record.go | 1 + manifest/config/config.yaml | 2 +- 6 files changed, 125 insertions(+), 10 deletions(-) create mode 100644 internal/controller/vote_record/vote_record_v1_get_vote_detail.go create mode 100644 internal/logic/vote_record/get_vote_detail.go diff --git a/api/vote_record/v1/vote_record.go b/api/vote_record/v1/vote_record.go index 01bbfb1..602fcb1 100644 --- a/api/vote_record/v1/vote_record.go +++ b/api/vote_record/v1/vote_record.go @@ -6,21 +6,27 @@ import ( ) type GetVoteDetailReq struct { - g.Meta `path:"/getAllVoteDetail" method:"GET" tags:"获取投票名单列表"` - VoteId int `json:"voteId" v:"required" dc:"投票活动ID"` + g.Meta `path:"/getAllVoteDetail" method:"get" tags:"获取投票名单列表"` + VoteId int `p:"voteId" v:"required" dc:"投票活动ID"` + Page int `p:"page" dc:"页码" default:"1"` + Size int `p:"size" dc:"每页数量" default:"50"` + Username string `p:"username" dc:"<用户名>"` + Account string `p:"account" dc:"<精网号>"` + OptionContent string `p:"optionContent" dc:"<选项>"` } type GetVoteDetailRes struct { - Username string `json:"username" dc:"名字"` - Account string `json:"account" dc:"精网号"` - VoteTitle string `json:"voteTitle" dc:"投票标题"` - ArticleTitle int `json:"articleTitle" dc:"文章/视频标题"` - OptionContent []string `json:"optionContent" dc:"选项名称"` - CreateTime *gtime.Time `json:"createTime" dc:"投票时间"` + Username string `json:"username" dc:"名字"` + Account string `json:"account" dc:"精网号"` + //VoteTitle string `json:"voteTitle" dc:"投票标题"` + //ArticleTitle string `json:"articleTitle" dc:"文章/视频标题"` + OptionContents string `json:"optionContents" dc:"选项名称"` + CreateTime *gtime.Time `json:"createTime" dc:"投票时间"` + TotalCount int `json:"totalCount" dc:""` } type GetVoteReq struct { g.Meta `path:"/getVote" method:"get" tags:"<获取文章及投票活动>"` - ArticleId int `json:"articleId" v:"required" dc:"<文章>ID"` + ArticleId int `p:"articleId" v:"required" dc:"<文章>ID"` } type OptionRes struct { Id int `json:"id" dc:"<选项>ID"` diff --git a/api/vote_record/vote_record.go b/api/vote_record/vote_record.go index c807448..e9ade0a 100644 --- a/api/vote_record/vote_record.go +++ b/api/vote_record/vote_record.go @@ -11,6 +11,7 @@ import ( ) type IVoteRecordV1 interface { + GetVoteDetail(ctx context.Context, req *v1.GetVoteDetailReq) (res []*v1.GetVoteDetailRes, err error) GetVote(ctx context.Context, req *v1.GetVoteReq) (res *v1.GetVoteRes, err error) GetIndex(ctx context.Context, req *v1.GetIndexReq) (res *v1.GetIndexRes, err error) AddRecord(ctx context.Context, req *v1.AddRecordReq) (res *v1.AddRecordRes, err error) diff --git a/internal/controller/vote_record/vote_record_v1_get_vote_detail.go b/internal/controller/vote_record/vote_record_v1_get_vote_detail.go new file mode 100644 index 0000000..66a8177 --- /dev/null +++ b/internal/controller/vote_record/vote_record_v1_get_vote_detail.go @@ -0,0 +1,12 @@ +package vote_record + +import ( + "context" + "practice_ArticleVote_Go/internal/service" + + "practice_ArticleVote_Go/api/vote_record/v1" +) + +func (c *ControllerV1) GetVoteDetail(ctx context.Context, req *v1.GetVoteDetailReq) (res []*v1.GetVoteDetailRes, err error) { + return service.NewVoteRecordService().GetVoteDetail(ctx, req) +} diff --git a/internal/logic/vote_record/get_vote_detail.go b/internal/logic/vote_record/get_vote_detail.go new file mode 100644 index 0000000..3769447 --- /dev/null +++ b/internal/logic/vote_record/get_vote_detail.go @@ -0,0 +1,95 @@ +package vote_record + +import ( + "context" + "github.com/gogf/gf/v2/frame/g" + v1 "practice_ArticleVote_Go/api/vote_record/v1" +) + +func (l *VoteRecordLogic) GetVoteDetail(ctx context.Context, req *v1.GetVoteDetailReq) (res []*v1.GetVoteDetailRes, err error) { + //query := ` + // SELECT + // u.username, + // u.account, + // GROUP_CONCAT(o.option_content ORDER BY o.id) AS option_contents, + // MAX(vr.create_time) AS create_time + // FROM vote_record vr + // JOIN user u ON vr.user_id = u.id + // JOIN vote_option o ON vr.option_id = o.id + // WHERE vr.vote_id = ? + //` + //conditions := []interface{}{req.VoteId} + //if req.Username != "" { + // query += " AND u.username LIKE ? " + // conditions = append(conditions, "%"+req.Username+"%") + //} + //if req.Account != "" { + // query += " AND u.account LIKE ? " + // conditions = append(conditions, "%"+req.Account+"%") + //} + //query += " GROUP BY u.id, DATE(vr.create_time), vr.vote_index " + //if req.OptionContent != "" { + // query += " HAVING SUM(o.option_content LIKE ?) > 0 " + // conditions = append(conditions, "%"+req.OptionContent+"%") + //} + //query += " ORDER BY create_time DESC LIMIT ?,?" + //offset := (req.Page - 1) * req.Size + //conditions = append(conditions, offset, req.Size) + //err = g.DB().Ctx(ctx).Raw(query, conditions...).Scan(&res) + // + //if err != nil { + // return nil, err + //} + //return res, nil + query := ` + SELECT + u.username, + u.account, + uv.option_contents, + uv.create_time + FROM ( + SELECT + vr.user_id, + vr.vote_index, + DATE(vr.create_time) AS vote_date, + MAX(vr.create_time) AS create_time, + GROUP_CONCAT(o.option_content ORDER BY o.id SEPARATOR ',') AS option_contents + FROM vote_record vr + JOIN vote_option o + ON vr.option_id = o.id + WHERE vr.vote_id = ? + GROUP BY vr.user_id, vr.vote_index, vote_date + ) AS uv + JOIN ` + "`user`" + ` u + ON u.id = uv.user_id + WHERE 1=1 + ` + + conditions := []interface{}{req.VoteId} + + if req.OptionContent != "" { + query += " AND uv.option_contents LIKE ?" + conditions = append(conditions, "%"+req.OptionContent+"%") + } + if req.Username != "" { + query += " AND u.username LIKE ?" + conditions = append(conditions, "%"+req.Username+"%") + } + if req.Account != "" { + query += " AND u.account LIKE ?" + conditions = append(conditions, "%"+req.Account+"%") + } + + query += " ORDER BY uv.create_time DESC " + query += " LIMIT ?, ? " + offset := (req.Page - 1) * req.Size + conditions = append(conditions, offset, req.Size) + + err = g.DB().Ctx(ctx). + Raw(query, conditions...). + Scan(&res) + if err != nil { + return nil, err + } + return res, nil +} diff --git a/internal/service/vote_record.go b/internal/service/vote_record.go index ea7ada9..ce59462 100644 --- a/internal/service/vote_record.go +++ b/internal/service/vote_record.go @@ -10,6 +10,7 @@ type IVoteRecord interface { GetIndex(ctx context.Context, req *v1.GetIndexReq) (res *v1.GetIndexRes, err error) AddRecord(ctx context.Context, req *v1.AddRecordReq) (res *v1.AddRecordRes, err error) GetVote(ctx context.Context, req *v1.GetVoteReq) (res *v1.GetVoteRes, err error) + GetVoteDetail(ctx context.Context, req *v1.GetVoteDetailReq) (res []*v1.GetVoteDetailRes, err error) } func NewVoteRecordService() IVoteRecord { diff --git a/manifest/config/config.yaml b/manifest/config/config.yaml index b6cd1b9..881daa8 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:root@tcp(127.0.0.1:3306)/link_test" + link: "mysql:root:123456@tcp(127.0.0.1:3306)/link_test" createdAt: "create_time" updatedAt: "update_time" \ No newline at end of file