system_user_statistics_logic.go
2.4 KB
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
package user
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type SystemUserStatisticsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSystemUserStatisticsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemUserStatisticsLogic {
return &SystemUserStatisticsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SystemUserStatisticsLogic) SystemUserStatistics(req *types.UserStatisticsRequest) (resp *types.UserStatisticsResponse, err error) {
var (
userId = req.UserId
userToken = contextdata.GetUserTokenFromCtx(l.ctx)
)
s := StatisticsItems(l.ctx, l.svcCtx, userToken.CompanyId, userId, req.ItemFlag)
resp = &types.UserStatisticsResponse{
TotalArticle: s.TotalArticle,
TotalComment: s.TotalComment,
TotalLoved: s.TotalLoved,
}
return
}
func StatisticsItems(ctx context.Context, svcCtx *svc.ServiceContext, companyId, userId int64, itemFlag int) (s Statistics) {
var conn = svcCtx.DefaultDBConn()
s = Statistics{}
if itemFlag == 0 {
itemFlag = -1
}
if (itemFlag & TotalArticle) > 0 {
var total int64
total, _, _ = svcCtx.ArticleRepository.Find(ctx, conn, companyId, domain.NewQueryOptions().WithCountOnly().MustWithKV("authorId", userId))
s.TotalArticle = int(total)
}
if (itemFlag & TotalComment) > 0 {
var total int64
total, _, _ = svcCtx.ArticleCommentRepository.Find(ctx, conn, domain.IndexCompanyId(companyId)().WithCountOnly().MustWithKV("fromUserId", userId))
s.TotalComment = int(total)
}
if (itemFlag & TotalLoved) > 0 {
var total int64
total, _, _ = svcCtx.UserLoveFlagRepository.Find(ctx, conn, domain.IndexCompanyId(companyId)().WithCountOnly().MustWithKV("userId", userId))
s.TotalComment = int(total)
}
return
}
type Statistics struct {
TotalArticle int `json:"totalArticle"`
TotalComment int `json:"totalComment"`
TotalLoved int `json:"totalLoved"`
}
const (
TotalArticle = 1
TotalComment = 2
TotalLoved = 4
)
func NewStatisticsItem(itemFlag int, value float64) types.StatisticsItem {
return types.StatisticsItem{
ItemFlag: itemFlag,
Value: value,
}
}