-
Notifications
You must be signed in to change notification settings - Fork 14
/
talk_records_vote.go
118 lines (89 loc) · 2.97 KB
/
talk_records_vote.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package dao
import (
"context"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/iimeta/iim-client/internal/model"
"github.com/iimeta/iim-client/internal/model/do"
"github.com/iimeta/iim-client/internal/model/entity"
"github.com/iimeta/iim-client/utility/cache"
"github.com/iimeta/iim-client/utility/db"
"github.com/iimeta/iim-client/utility/redis"
"go.mongodb.org/mongo-driver/bson"
)
var TalkRecordsVote = NewTalkRecordsVoteDao()
type TalkRecordsVoteDao struct {
*MongoDB[entity.TalkRecordsVote]
cache *cache.Vote
}
func NewTalkRecordsVoteDao(database ...string) *TalkRecordsVoteDao {
if len(database) == 0 {
database = append(database, db.DefaultDatabase)
}
return &TalkRecordsVoteDao{
MongoDB: NewMongoDB[entity.TalkRecordsVote](database[0], do.TALK_RECORDS_VOTE_COLLECTION),
cache: cache.NewVote(redis.Client),
}
}
func (d *TalkRecordsVoteDao) GetVoteAnswerUser(ctx context.Context, vid string) ([]int, error) {
// 读取缓存
if uids, err := d.cache.GetVoteAnswerUser(ctx, vid); err == nil {
return uids, nil
}
uids, err := d.SetVoteAnswerUser(ctx, vid)
if err != nil {
return nil, err
}
return uids, nil
}
func (d *TalkRecordsVoteDao) SetVoteAnswerUser(ctx context.Context, vid string) ([]int, error) {
talkRecordsVoteAnswerList := make([]*entity.TalkRecordsVoteAnswer, 0)
if err := Find(ctx, d.Database, do.TALK_RECORDS_VOTE_ANSWER_COLLECTION, bson.M{"vote_id": vid}, &talkRecordsVoteAnswerList); err != nil {
return nil, err
}
uids := make([]int, 0)
for _, answer := range talkRecordsVoteAnswerList {
uids = append(uids, answer.UserId)
}
_ = d.cache.SetVoteAnswerUser(ctx, vid, uids)
return uids, nil
}
func (d *TalkRecordsVoteDao) GetVoteStatistics(ctx context.Context, vid string) (*model.VoteStatistics, error) {
value, err := d.cache.GetVoteStatistics(ctx, vid)
if err != nil {
return d.SetVoteStatistics(ctx, vid)
}
statistic := &model.VoteStatistics{}
_ = gjson.Unmarshal([]byte(value), statistic)
return statistic, nil
}
func (d *TalkRecordsVoteDao) SetVoteStatistics(ctx context.Context, vid string) (*model.VoteStatistics, error) {
vote, err := d.FindById(ctx, vid)
if err != nil {
return nil, err
}
answerOption := make(map[string]any)
if err := gjson.Unmarshal([]byte(vote.AnswerOption), &answerOption); err != nil {
return nil, err
}
talkRecordsVoteAnswerList := make([]*entity.TalkRecordsVoteAnswer, 0)
if err := Find(ctx, d.Database, do.TALK_RECORDS_VOTE_ANSWER_COLLECTION, bson.M{"vote_id": vid}, &talkRecordsVoteAnswerList); err != nil {
return nil, err
}
options := make([]string, 0)
for _, answer := range talkRecordsVoteAnswerList {
options = append(options, answer.Option)
}
opts := make(map[string]int)
for option := range answerOption {
opts[option] = 0
}
for _, option := range options {
opts[option] += 1
}
statistic := &model.VoteStatistics{
Options: opts,
Count: len(options),
}
_ = d.cache.SetVoteStatistics(ctx, vid, gjson.MustEncodeString(statistic))
return statistic, nil
}