Browse Source

获取投票信息和获取投票名单列表

master
maziyang 1 week ago
parent
commit
837f348d42
  1. 24
      api/vote_record/v1/vote_record.go
  2. 1
      api/vote_record/vote_record.go
  3. 12
      internal/controller/vote_record/vote_record_v1_get_vote_detail.go
  4. 95
      internal/logic/vote_record/get_vote_detail.go
  5. 1
      internal/service/vote_record.go
  6. 2
      manifest/config/config.yaml

24
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:"<UNK>"`
}
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"`

1
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)

12
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)
}

95
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
}

1
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 {

2
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"
Loading…
Cancel
Save