Browse Source

获取投票名单个数

master
maziyang 1 week ago
parent
commit
96df6afc0b
  1. 5
      api/vote_record/v1/vote_record.go
  2. 2
      api/vote_record/vote_record.go
  3. 2
      internal/controller/vote_record/vote_record_v1_get_vote_detail.go
  4. 19
      internal/logic/vote_record/get_vote_detail.go
  5. 2
      internal/service/vote_record.go

5
api/vote_record/v1/vote_record.go

@ -5,6 +5,10 @@ import (
"github.com/gogf/gf/v2/os/gtime" "github.com/gogf/gf/v2/os/gtime"
) )
type GetVoteDetailListRes struct {
VoteList []*GetVoteDetailRes `json:"voteList"`
TotalCount int `json:"totalCount"`
}
type GetVoteDetailReq struct { type GetVoteDetailReq struct {
g.Meta `path:"/getAllVoteDetail" method:"get" tags:"获取投票名单列表"` g.Meta `path:"/getAllVoteDetail" method:"get" tags:"获取投票名单列表"`
VoteId int `p:"voteId" v:"required" dc:"投票活动ID"` VoteId int `p:"voteId" v:"required" dc:"投票活动ID"`
@ -22,7 +26,6 @@ type GetVoteDetailRes struct {
//ArticleTitle string `json:"articleTitle" dc:"文章/视频标题"` //ArticleTitle string `json:"articleTitle" dc:"文章/视频标题"`
OptionContents string `json:"optionContents" dc:"选项名称"` OptionContents string `json:"optionContents" dc:"选项名称"`
CreateTime *gtime.Time `json:"createTime" dc:"投票时间"` CreateTime *gtime.Time `json:"createTime" dc:"投票时间"`
TotalCount int `json:"totalCount" dc:"<UNK>"`
} }
type GetVoteReq struct { type GetVoteReq struct {
g.Meta `path:"/getVote" method:"get" tags:"<获取文章及投票活动>"` g.Meta `path:"/getVote" method:"get" tags:"<获取文章及投票活动>"`

2
api/vote_record/vote_record.go

@ -11,7 +11,7 @@ import (
) )
type IVoteRecordV1 interface { type IVoteRecordV1 interface {
GetVoteDetail(ctx context.Context, req *v1.GetVoteDetailReq) (res []*v1.GetVoteDetailRes, err error)
GetVoteDetail(ctx context.Context, req *v1.GetVoteDetailReq) (res *v1.GetVoteDetailListRes, err error)
GetVote(ctx context.Context, req *v1.GetVoteReq) (res *v1.GetVoteRes, 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) GetIndex(ctx context.Context, req *v1.GetIndexReq) (res *v1.GetIndexRes, err error)
AddRecord(ctx context.Context, req *v1.AddRecordReq) (res *v1.AddRecordRes, err error) AddRecord(ctx context.Context, req *v1.AddRecordReq) (res *v1.AddRecordRes, err error)

2
internal/controller/vote_record/vote_record_v1_get_vote_detail.go

@ -7,6 +7,6 @@ import (
"practice_ArticleVote_Go/api/vote_record/v1" "practice_ArticleVote_Go/api/vote_record/v1"
) )
func (c *ControllerV1) GetVoteDetail(ctx context.Context, req *v1.GetVoteDetailReq) (res []*v1.GetVoteDetailRes, err error) {
func (c *ControllerV1) GetVoteDetail(ctx context.Context, req *v1.GetVoteDetailReq) (res *v1.GetVoteDetailListRes, err error) {
return service.NewVoteRecordService().GetVoteDetail(ctx, req) return service.NewVoteRecordService().GetVoteDetail(ctx, req)
} }

19
internal/logic/vote_record/get_vote_detail.go

@ -2,11 +2,12 @@ package vote_record
import ( import (
"context" "context"
"fmt"
"github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/frame/g"
v1 "practice_ArticleVote_Go/api/vote_record/v1" v1 "practice_ArticleVote_Go/api/vote_record/v1"
) )
func (l *VoteRecordLogic) GetVoteDetail(ctx context.Context, req *v1.GetVoteDetailReq) (res []*v1.GetVoteDetailRes, err error) {
func (l *VoteRecordLogic) GetVoteDetail(ctx context.Context, req *v1.GetVoteDetailReq) (res *v1.GetVoteDetailListRes, err error) {
//query := ` //query := `
// SELECT // SELECT
// u.username, // u.username,
@ -79,17 +80,23 @@ func (l *VoteRecordLogic) GetVoteDetail(ctx context.Context, req *v1.GetVoteDeta
query += " AND u.account LIKE ?" query += " AND u.account LIKE ?"
conditions = append(conditions, "%"+req.Account+"%") conditions = append(conditions, "%"+req.Account+"%")
} }
query += " ORDER BY uv.create_time DESC "
query += " LIMIT ?, ? "
countQuery := "SELECT COUNT(*) AS cnt FROM ( " + query + " ) AS subquery"
var cntResult struct{ Cnt int }
err = g.DB().Ctx(ctx).Raw(countQuery, conditions...).Scan(&cntResult)
fmt.Println(cntResult.Cnt)
res = &v1.GetVoteDetailListRes{
TotalCount: cntResult.Cnt,
}
query += " ORDER BY uv.create_time DESC LIMIT ?, ?"
offset := (req.Page - 1) * req.Size offset := (req.Page - 1) * req.Size
conditions = append(conditions, offset, req.Size) conditions = append(conditions, offset, req.Size)
var voteList []*v1.GetVoteDetailRes
err = g.DB().Ctx(ctx). err = g.DB().Ctx(ctx).
Raw(query, conditions...). Raw(query, conditions...).
Scan(&res)
Scan(&voteList)
if err != nil { if err != nil {
return nil, err return nil, err
} }
res.VoteList = voteList
return res, nil return res, nil
} }

2
internal/service/vote_record.go

@ -10,7 +10,7 @@ type IVoteRecord interface {
GetIndex(ctx context.Context, req *v1.GetIndexReq) (res *v1.GetIndexRes, 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) AddRecord(ctx context.Context, req *v1.AddRecordReq) (res *v1.AddRecordRes, err error)
GetVote(ctx context.Context, req *v1.GetVoteReq) (res *v1.GetVoteRes, 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)
GetVoteDetail(ctx context.Context, req *v1.GetVoteDetailReq) (res *v1.GetVoteDetailListRes, err error)
} }
func NewVoteRecordService() IVoteRecord { func NewVoteRecordService() IVoteRecord {

Loading…
Cancel
Save