Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
hilo-user
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
chenweijian
hilo-user
Commits
48d2038a
Commit
48d2038a
authored
May 29, 2023
by
hujiebin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:统计成就
parent
6bd3c2cc
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
203 additions
and
0 deletions
+203
-0
achievement.go
_const/enum/cp_e/achievement.go
+11
-0
space.go
domain/event/cp_ev/space.go
+30
-0
achievement.go
domain/model/cp_m/achievement.go
+42
-0
visit.go
domain/model/cp_m/visit.go
+46
-0
cp_level.go
domain/service/event_s/cp_level.go
+34
-0
cp_visit.go
domain/service/event_s/cp_visit.go
+29
-0
event_init.go
domain/service/event_s/event_init.go
+1
-0
space.go
route/cp_r/space.go
+10
-0
No files found.
_const/enum/cp_e/achievement.go
0 → 100644
View file @
48d2038a
package
cp_e
type
CpAchievement
int
const
(
CpAchievementLevel
CpAchievement
=
1
// 等级
CpAchievementVisitors
CpAchievement
=
2
// 空间访问人数
CpAchievementMonthRank
CpAchievement
=
3
// 月榜最高
CpAchievementWeekRank
CpAchievement
=
4
// 周榜最高
CpAchievementDayRank
CpAchievement
=
5
// 日榜最高
)
domain/event/cp_ev/space.go
0 → 100644
View file @
48d2038a
package
cp_ev
import
(
"git.hilo.cn/hilo-common/domain"
"git.hilo.cn/hilo-common/resource/mysql"
)
//注册监听
var
spaceVisitListen
=
new
(
domain
.
EventBase
)
type
SpaceVisitEvent
struct
{
UserId
mysql
.
ID
CpId
mysql
.
ID
UserId1
,
UserId2
mysql
.
ID
}
//添加领域事件,在每个领域模型中init中添加,因为这是静态业务,非动态的。
func
AddCpSpaceVisitSync
(
callback
func
(
model
*
domain
.
Model
,
event
interface
{})
error
)
{
domain
.
AddEventSync
(
spaceVisitListen
,
callback
)
}
//加入到异步操作中
func
AddCpSpaceVisitAsync
(
callback
func
(
model
*
domain
.
Model
,
event
interface
{})
error
)
{
domain
.
AddEventAsync
(
spaceVisitListen
,
callback
)
}
//领域事件发布
func
PublishCpSpaceVisit
(
model
*
domain
.
Model
,
event
interface
{})
error
{
return
domain
.
PublishEvent
(
spaceVisitListen
,
model
,
event
)
}
domain/model/cp_m/achievement.go
0 → 100644
View file @
48d2038a
package
cp_m
import
(
"git.hilo.cn/hilo-common/domain"
"git.hilo.cn/hilo-common/resource/mysql"
"gorm.io/gorm"
"hilo-user/_const/enum/cp_e"
"time"
)
type
CpAchievement
struct
{
CpId
mysql
.
ID
UserId1
mysql
.
ID
UserId2
mysql
.
ID
Type
cp_e
.
CpAchievement
Score
mysql
.
Num
CreatedTime
time
.
Time
`gorm:"->"`
UpdatedTime
time
.
Time
`gorm:"->"`
}
// 更新cp成就
// 单进程操作,先create,后update
func
UpdateCpAchievement
(
model
*
domain
.
Model
,
cpId
,
userId1
,
userId2
mysql
.
ID
,
Type
cp_e
.
CpAchievement
,
score
mysql
.
Num
)
error
{
var
cpAchievement
CpAchievement
if
err
:=
model
.
DB
()
.
Model
(
CpAchievement
{})
.
Where
(
"cp_id = ? AND `type` = ?"
,
cpId
,
Type
)
.
First
(
&
cpAchievement
)
.
Error
;
err
!=
nil
{
if
err
!=
gorm
.
ErrRecordNotFound
{
model
.
Log
.
Errorf
(
"UpdateCpAchievement fail:%v"
,
err
)
return
err
}
// gorm.ErrRecordNotFound
cpAchievement
=
CpAchievement
{
CpId
:
cpId
,
UserId1
:
userId1
,
UserId2
:
userId2
,
Type
:
Type
,
Score
:
score
,
}
return
model
.
DB
()
.
Model
(
CpAchievement
{})
.
Create
(
&
cpAchievement
)
.
Error
}
// update if less than
return
model
.
DB
()
.
Model
(
CpAchievement
{})
.
Where
(
"cp_id = ? AND `type` = ?"
,
cpId
,
Type
)
.
Where
(
"score < ?"
,
score
)
.
UpdateColumn
(
"score"
,
score
)
.
Error
}
domain/model/cp_m/visit.go
0 → 100644
View file @
48d2038a
package
cp_m
import
(
"git.hilo.cn/hilo-common/domain"
"git.hilo.cn/hilo-common/resource/mysql"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"time"
)
type
CpVisitor
struct
{
CpId
mysql
.
ID
UserId1
mysql
.
ID
UserId2
mysql
.
ID
Visitor
mysql
.
ID
Times
mysql
.
Num
CreatedTime
time
.
Time
`gorm:"->"`
UpdatedTime
time
.
Time
`gorm:"->"`
}
// 添加cp空间访问量
func
AddCpSpaceVisitor
(
model
*
domain
.
Model
,
cpId
,
userId1
,
userId2
,
visitor
mysql
.
ID
)
error
{
vis
:=
&
CpVisitor
{
CpId
:
cpId
,
UserId1
:
userId1
,
UserId2
:
userId2
,
Visitor
:
visitor
,
Times
:
1
,
}
if
err
:=
model
.
DB
()
.
Model
(
CpVisitor
{})
.
Clauses
(
clause
.
OnConflict
{
Columns
:
[]
clause
.
Column
{{
Name
:
"cp_id"
},
{
Name
:
"visitor"
}},
DoUpdates
:
clause
.
Assignments
(
map
[
string
]
interface
{}{
"times"
:
gorm
.
Expr
(
"times + ?"
,
1
)})})
.
Create
(
vis
)
.
Error
;
err
!=
nil
{
model
.
Log
.
Errorf
(
"AddCpSpaceVisitor fail:%v"
,
err
)
return
err
}
return
nil
}
// 获取cp空间访问人数
func
CountCpSpaceVisitors
(
model
*
domain
.
Model
,
cpId
mysql
.
ID
)
int64
{
var
cnt
int64
if
err
:=
model
.
DB
()
.
Model
(
CpVisitor
{})
.
Where
(
"cp_id = ?"
,
cpId
)
.
Count
(
&
cnt
)
.
Error
;
err
!=
nil
{
model
.
Log
.
Errorf
(
"CountCpSpaceVisitors fail:%v"
,
err
)
}
return
cnt
}
domain/service/event_s/cp_level.go
View file @
48d2038a
...
...
@@ -2,8 +2,12 @@ package event_s
import
(
"git.hilo.cn/hilo-common/domain"
"git.hilo.cn/hilo-common/resource/mysql"
"github.com/jinzhu/now"
"hilo-user/_const/enum/cp_e"
"hilo-user/domain/event/gift_ev"
"hilo-user/domain/model/cp_m"
"time"
)
// 送礼增加cp等级
...
...
@@ -25,6 +29,36 @@ func CpGiftEvent() {
if
err
:=
cp_m
.
AddCpDayRank
(
model
,
cpRelation
,
diamonds
);
err
!=
nil
{
model
.
Log
.
Errorf
(
"AddCpDayRank fail:%v"
,
err
)
}
// 检查最新的等级
if
cpLevel
:=
cp_m
.
GetCpLevel
(
model
,
cpRelation
.
ID
);
cpLevel
.
CpId
>=
0
{
if
err
:=
cp_m
.
UpdateCpAchievement
(
model
,
cpLevel
.
CpId
,
cpRelation
.
UserId1
,
cpRelation
.
UserId2
,
cp_e
.
CpAchievementLevel
,
mysql
.
Num
(
cpLevel
.
Level
));
err
!=
nil
{
model
.
Log
.
Errorf
(
"UpdateCpAchievement fail:%v"
,
err
)
}
}
// 检查最高的分数
for
_
,
queryType
:=
range
[]
string
{
"day"
,
"week"
,
"month"
}
{
var
beginDate
,
endDate
string
var
cpAchievementType
cp_e
.
CpAchievement
switch
queryType
{
case
"day"
:
beginDate
,
endDate
=
time
.
Now
()
.
Format
(
"2006-01-02"
),
time
.
Now
()
.
Format
(
"2006-01-02"
)
cpAchievementType
=
cp_e
.
CpAchievementDayRank
case
"week"
:
beginDate
=
now
.
BeginningOfWeek
()
.
Format
(
"2006-01-02"
)
endDate
=
now
.
EndOfWeek
()
.
Format
(
"2006-01-02"
)
cpAchievementType
=
cp_e
.
CpAchievementWeekRank
case
"month"
:
beginDate
=
now
.
BeginningOfMonth
()
.
Format
(
"2006-01-02"
)
endDate
=
now
.
EndOfMonth
()
.
Format
(
"2006-01-02"
)
cpAchievementType
=
cp_e
.
CpAchievementMonthRank
}
if
data
:=
cp_m
.
GetCpDayRank
(
model
,
beginDate
,
endDate
,
cpRelation
.
ID
);
data
.
Score
>
0
{
if
err
:=
cp_m
.
UpdateCpAchievement
(
model
,
cpRelation
.
ID
,
cpRelation
.
UserId1
,
cpRelation
.
UserId2
,
cpAchievementType
,
data
.
Score
);
err
!=
nil
{
model
.
Log
.
Errorf
(
"UpdateCpAchievement fail:%v"
,
err
)
}
}
}
// 检查最新日周月榜单
return
nil
// 业务场景允许提前break(cp是唯一的)
}
}
...
...
domain/service/event_s/cp_visit.go
0 → 100644
View file @
48d2038a
package
event_s
import
(
"git.hilo.cn/hilo-common/domain"
"git.hilo.cn/hilo-common/resource/mysql"
"hilo-user/_const/enum/cp_e"
"hilo-user/domain/event/cp_ev"
"hilo-user/domain/model/cp_m"
"hilo-user/myerr/bizerr"
)
// cp空间访问
func
CpSpaceVisitEvent
()
{
cp_ev
.
AddCpSpaceVisitAsync
(
func
(
model
*
domain
.
Model
,
event
interface
{})
error
{
e
,
ok
:=
event
.
(
*
cp_ev
.
SpaceVisitEvent
)
if
!
ok
{
return
bizerr
.
InvalidParameter
}
if
err
:=
cp_m
.
AddCpSpaceVisitor
(
model
,
e
.
CpId
,
e
.
UserId1
,
e
.
UserId2
,
e
.
UserId
);
err
!=
nil
{
model
.
Log
.
Errorf
(
"AddCpSpaceVisitor fail:%v"
,
err
)
}
if
cnt
:=
cp_m
.
CountCpSpaceVisitors
(
model
,
e
.
CpId
);
cnt
>
0
{
if
err
:=
cp_m
.
UpdateCpAchievement
(
model
,
e
.
CpId
,
e
.
UserId1
,
e
.
UserId2
,
cp_e
.
CpAchievementVisitors
,
mysql
.
Num
(
cnt
));
err
!=
nil
{
model
.
Log
.
Errorf
(
"UpdateCpAchievement fail:%v"
,
err
)
}
}
return
nil
})
}
domain/service/event_s/event_init.go
View file @
48d2038a
...
...
@@ -15,6 +15,7 @@ import (
func
EventInit
()
{
UserBagSendEvent
()
CpGiftEvent
()
CpSpaceVisitEvent
()
}
func
UserBagSendEvent
()
{
...
...
route/cp_r/space.go
View file @
48d2038a
...
...
@@ -8,6 +8,7 @@ import (
"hilo-user/_const/enum/cp_e"
"hilo-user/cv/cp_cv"
"hilo-user/cv/user_cv"
"hilo-user/domain/event/cp_ev"
"hilo-user/domain/model/cp_m"
"hilo-user/domain/model/user_m"
"hilo-user/myerr/bizerr"
...
...
@@ -48,6 +49,15 @@ func CpSpace(c *gin.Context) (*mycontext.MyContext, error) {
level
:=
cp_m
.
GetCpLevel
(
model
,
cpRelation
.
ID
)
cpLevel
,
curPoints
=
level
.
Level
,
level
.
Points
expireAtUnix
=
level
.
ExpireAt
.
Unix
()
// cp 存在的时候,发布访问cp空间事件
if
err
:=
cp_ev
.
PublishCpSpaceVisit
(
model
,
&
cp_ev
.
SpaceVisitEvent
{
UserId
:
userId
,
CpId
:
cpRelation
.
ID
,
UserId1
:
cpRelation
.
UserId1
,
UserId2
:
cpRelation
.
UserId2
,
});
err
!=
nil
{
return
myContext
,
err
}
}
if
cpLevel
!=
cp_e
.
CpLevelMax
{
nextPoints
=
cp_e
.
CpLevelPoints
[
cpLevel
+
1
]
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment