/
poll_log.go
224 lines (210 loc) · 6.06 KB
/
poll_log.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package user
import (
"context"
"fmt"
"time"
"golang.org/x/sync/errgroup"
"github.com/hitokoto-osc/reviewer/internal/consts"
vtime "github.com/hitokoto-osc/reviewer/utility/time"
"github.com/hitokoto-osc/reviewer/internal/model"
"github.com/gogf/gf/v2/database/gdb"
"github.com/hitokoto-osc/reviewer/internal/dao"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/hitokoto-osc/reviewer/internal/model/entity"
"github.com/hitokoto-osc/reviewer/internal/service"
)
func (s *sUser) GetUserPollLogs(ctx context.Context, in model.GetUserPollLogsInput) (*model.GetUserPollLogsOutput, error) {
if in.UserID <= 0 {
user := service.BizCtx().GetUser(ctx)
if user == nil {
return nil, gerror.New("user not found")
}
in.UserID = user.Id
}
query := dao.PollLog.Ctx(ctx)
out := &model.GetUserPollLogsOutput{}
if in.WithCache {
query = query.Cache(gdb.CacheOption{
Duration: time.Second * 3,
Name: fmt.Sprintf("poll_logs:uid:%d:page:%d:pageSize:%d:order:%s", in.UserID, in.Page, in.PageSize, in.Order),
Force: false,
})
}
query = query.Where(dao.PollLog.Columns().UserId, in.UserID)
if in.Page > 0 && in.PageSize > 0 {
query = query.Page(in.Page, in.PageSize)
out.Page, out.PageSize = in.Page, in.PageSize
}
if in.Order != "" {
query = query.Order(in.Order)
}
// fetch data from query
var pollLogs []entity.PollLog
eg, egCtx := errgroup.WithContext(ctx)
eg.Go(func() error {
var e error
out.Total, e = s.GetUserPollLogsCount(egCtx, in.UserID)
return e
})
eg.Go(func() error {
return query.Scan(&pollLogs)
})
if err := eg.Wait(); err != nil {
return nil, err
}
userPollLogs := make([]model.UserPollLog, len(pollLogs))
for i, v := range pollLogs {
userPollLogs[i] = model.UserPollLog{
PollID: uint(v.PollId),
Point: v.Point,
SentenceUUID: v.SentenceUuid,
Method: consts.PollMethod(v.Type),
Comment: v.Comment,
CreatedAt: (*vtime.Time)(v.CreatedAt),
UpdatedAt: (*vtime.Time)(v.UpdatedAt),
}
}
out.Collection = userPollLogs
return out, nil
}
func (s *sUser) GetUserPollLogsWithSentence(
ctx context.Context,
in model.GetUserPollLogsInput,
) (*model.GetUserPollLogsWithSentenceOutput, error) {
if in.UserID <= 0 {
user := service.BizCtx().GetUser(ctx)
if user == nil {
return nil, gerror.New("user not found")
}
in.UserID = user.Id
}
pollLogs, err := s.GetUserPollLogs(ctx, in)
if err != nil {
return nil, err
}
out := &model.GetUserPollLogsWithSentenceOutput{
Page: in.Page,
PageSize: in.PageSize,
Total: pollLogs.Total,
}
userPollLogs := make([]model.UserPollLogWithSentenceAndUserMarks, len(pollLogs.Collection))
// 并发获取句子
eg, egCtx := errgroup.WithContext(ctx)
for i, v := range pollLogs.Collection {
index, value := i, v // 解决并发问题
eg.Go(func() error {
var e error
userPollLogs[index].UserMarks, e = service.Poll().GetPollMarksByPollIDAndUserID(egCtx, int(value.PollID), int(in.UserID))
return e
})
eg.Go(func() error {
sentence, e := service.Hitokoto().GetHitokotoV1SchemaByUUID(egCtx, value.SentenceUUID)
if e != nil {
return e
}
userPollLogs[index].UserPollLog = value
userPollLogs[index].Sentence = sentence
return nil
})
}
err = eg.Wait()
if err != nil {
return nil, err
}
out.Collection = userPollLogs
return out, nil
}
// GetUserPollLogsCount 获取用户投票记录数量
func (s *sUser) GetUserPollLogsCount(ctx context.Context, userID uint) (int, error) {
if userID <= 0 {
user := service.BizCtx().GetUser(ctx)
if user == nil {
return 0, gerror.New("user not found")
}
userID = user.Id
}
return dao.PollLog.Ctx(ctx).Where(dao.PollLog.Columns().UserId, userID).Count()
}
func (s *sUser) GetUserPollLogsWithPollResult(
ctx context.Context,
in model.GetUserPollLogsInput,
) (*model.GetUserPollLogsWithPollResultOutput, error) {
pollLogs, err := s.GetUserPollLogsWithSentence(ctx, in)
if err != nil {
return nil, err
}
collections := make([]model.UserPollElement, len(pollLogs.Collection))
// Copy Properties and fetch poll result
eg, egCtx := errgroup.WithContext(ctx)
for i, v := range pollLogs.Collection {
index, value := i, v
collections[index] = model.UserPollElement{
UserPollLogWithSentenceAndUserMarks: value,
}
// 并发获取投票结果
eg.Go(func() error {
poll, e := service.Poll().GetPollByID(egCtx, int(value.PollID))
if e != nil {
return e
}
collections[index].PollInfo = &model.PollElement{
SentenceUUID: poll.SentenceUuid,
Status: consts.PollStatus(poll.Status),
Approve: poll.Accept,
Reject: poll.Reject,
NeedModify: poll.NeedEdited,
NeedCommonUserPoll: poll.NeedUserPoll,
CreatedAt: (*vtime.Time)(poll.CreatedAt),
UpdatedAt: (*vtime.Time)(poll.UpdatedAt),
}
return nil
})
// 并发获取标记
eg.Go(func() error {
marks, e := service.Poll().GetPollMarksByPollID(egCtx, value.PollID)
if e != nil {
return e
}
collections[index].PollMarks = marks
return nil
})
}
err = eg.Wait()
if err != nil {
return nil, err
}
out := &model.GetUserPollLogsWithPollResultOutput{
Collection: collections,
Page: in.Page,
PageSize: in.PageSize,
Total: pollLogs.Total,
}
return out, nil
}
func (s *sUser) GetUserPolledDataWithPollID(ctx context.Context, userID, pid uint) (*model.PolledData, error) {
if userID <= 0 {
user := service.BizCtx().GetUser(ctx)
if user == nil {
return nil, gerror.New("user not found")
}
userID = user.Id
}
var pollLog *entity.PollLog
err := dao.PollLog.Ctx(ctx).
Where(dao.PollLog.Columns().PollId, pid).
Where(dao.PollLog.Columns().UserId, userID).
WhereNot(dao.PollLog.Columns().Type, consts.PollMethodNeedCommonUserPoll). // 排除需要普通用户投票的记录
Scan(&pollLog)
if err != nil {
return nil, err
} else if pollLog == nil {
return nil, nil
}
res := &model.PolledData{
Point: pollLog.Point,
Method: consts.PollMethod(pollLog.Type),
CreatedAt: (*vtime.Time)(pollLog.CreatedAt),
UpdatedAt: (*vtime.Time)(pollLog.UpdatedAt),
}
return res, nil
}