6 changed files with 125 additions and 10 deletions
-
24api/vote_record/v1/vote_record.go
-
1api/vote_record/vote_record.go
-
12internal/controller/vote_record/vote_record_v1_get_vote_detail.go
-
95internal/logic/vote_record/get_vote_detail.go
-
1internal/service/vote_record.go
-
2manifest/config/config.yaml
@ -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) |
|||
} |
@ -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 |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue