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
fe8c02a6
Commit
fe8c02a6
authored
Jun 16, 2023
by
hujiebin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix:计算下一个纪念日
parent
aa93073a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
33 deletions
+44
-33
anniversary.go
domain/model/cp_m/anniversary.go
+43
-7
anniversary.go
route/cp_r/anniversary.go
+1
-26
No files found.
domain/model/cp_m/anniversary.go
View file @
fe8c02a6
...
@@ -111,32 +111,68 @@ func GetAllCpAnniversary(model *domain.Model, userId mysql.ID) []CpAnniversary {
...
@@ -111,32 +111,68 @@ func GetAllCpAnniversary(model *domain.Model, userId mysql.ID) []CpAnniversary {
// 获取所有需要提醒的纪念日
// 获取所有需要提醒的纪念日
func
GetNeedRemindCpAnniversary
(
model
*
domain
.
Model
)
[]
CpAnniversary
{
func
GetNeedRemindCpAnniversary
(
model
*
domain
.
Model
)
[]
CpAnniversary
{
var
res
[]
CpAnniversary
var
r
ows
,
r
es
[]
CpAnniversary
if
err
:=
model
.
DB
()
.
Model
(
CpAnniversary
{})
.
if
err
:=
model
.
DB
()
.
Model
(
CpAnniversary
{})
.
Where
(
"`timestamp` >= ?"
,
time
.
Now
()
.
Unix
()
-
86400
)
.
Where
(
"`timestamp` < ?"
,
time
.
Now
()
.
Unix
())
.
Where
(
"`timestamp` < ?"
,
time
.
Now
()
.
Unix
())
.
Where
(
"is_remind = 1"
)
.
Where
(
"is_remind = 1"
)
.
Where
(
"last_remind_time < ?"
,
time
.
Now
()
.
AddDate
(
-
1
,
0
,
0
)
.
Unix
())
.
// 一年前提醒过
Where
(
"last_remind_time < ?"
,
time
.
Now
()
.
AddDate
(
-
1
,
0
,
0
)
.
Unix
())
.
// 一年前提醒过
Find
(
&
r
e
s
)
.
Error
;
err
!=
nil
{
Find
(
&
r
ow
s
)
.
Error
;
err
!=
nil
{
model
.
Log
.
Errorf
(
"GetNeedRemindCpAnniversary fail:%v"
,
err
)
model
.
Log
.
Errorf
(
"GetNeedRemindCpAnniversary fail:%v"
,
err
)
}
}
now
:=
time
.
Now
()
.
Unix
()
for
i
,
v
:=
range
rows
{
ts
:=
CalcNextAnniversary
(
v
.
Timestamp
)
if
now
>
ts
{
res
=
append
(
res
,
rows
[
i
])
}
}
return
res
return
res
}
}
// 获取cp当天需要提醒的纪念日
// 获取cp当天需要提醒的纪念日
func
GetUserTodayCpAnniversary
(
model
*
domain
.
Model
,
cpId
mysql
.
ID
)
[]
CpAnniversary
{
func
GetUserTodayCpAnniversary
(
model
*
domain
.
Model
,
cpId
mysql
.
ID
)
[]
CpAnniversary
{
var
res
[]
CpAnniversary
var
r
ows
,
r
es
[]
CpAnniversary
if
err
:=
model
.
DB
()
.
Model
(
CpAnniversary
{})
.
if
err
:=
model
.
DB
()
.
Model
(
CpAnniversary
{})
.
Where
(
"`timestamp` >= ?"
,
time
.
Now
()
.
Unix
()
-
86400
)
.
Where
(
"`timestamp` < ?"
,
time
.
Now
()
.
Unix
())
.
Where
(
"`timestamp` < ?"
,
time
.
Now
()
.
Unix
())
.
Where
(
"is_remind = 1"
)
.
Where
(
"is_remind = 1"
)
.
Where
(
"cp_id = ?"
,
cpId
)
.
Where
(
"cp_id = ?"
,
cpId
)
.
Find
(
&
r
e
s
)
.
Error
;
err
!=
nil
{
Find
(
&
r
ow
s
)
.
Error
;
err
!=
nil
{
model
.
Log
.
Errorf
(
"GetUserTodayCpAnniversary fail:%v"
,
err
)
model
.
Log
.
Errorf
(
"GetUserTodayCpAnniversary fail:%v"
,
err
)
}
}
return
res
now
:=
time
.
Now
()
.
Unix
()
for
i
,
v
:=
range
rows
{
ts
:=
CalcNextAnniversary
(
v
.
Timestamp
)
if
now
>
ts
{
res
=
append
(
res
,
rows
[
i
])
}
}
return
rows
}
}
func
UpdateCpAnniversaryReminded
(
model
*
domain
.
Model
,
id
mysql
.
ID
)
error
{
func
UpdateCpAnniversaryReminded
(
model
*
domain
.
Model
,
id
mysql
.
ID
)
error
{
return
model
.
DB
()
.
Model
(
CpAnniversary
{})
.
Where
(
"id = ?"
,
id
)
.
Update
(
"last_remind_time"
,
time
.
Now
()
.
Unix
())
.
Error
return
model
.
DB
()
.
Model
(
CpAnniversary
{})
.
Where
(
"id = ?"
,
id
)
.
Update
(
"last_remind_time"
,
time
.
Now
()
.
Unix
())
.
Error
}
}
// 计算下一个纪念日
func
CalcNextAnniversary
(
timestamp
int64
)
int64
{
now
:=
time
.
Now
()
// 还没超过一天,不用计算明年的
if
now
.
Unix
()
-
timestamp
<
86400
{
return
timestamp
}
birthday
:=
time
.
Unix
(
timestamp
,
0
)
// 计算今年的生日日期
thisYearBirthday
:=
time
.
Date
(
now
.
Year
(),
birthday
.
Month
(),
birthday
.
Day
(),
birthday
.
Hour
(),
birthday
.
Minute
(),
birthday
.
Second
(),
0
,
time
.
Local
)
// 如果今年的生日还未到,则生日日期为今年的生日日期;否则为明年的生日日期
var
next
time
.
Time
if
thisYearBirthday
.
After
(
now
)
||
now
.
Sub
(
thisYearBirthday
)
.
Seconds
()
<
86400
{
next
=
thisYearBirthday
}
else
{
next
=
thisYearBirthday
.
AddDate
(
1
,
0
,
0
)
}
// 计算时间戳
nextTimestamp
:=
next
.
Unix
()
return
nextTimestamp
}
route/cp_r/anniversary.go
View file @
fe8c02a6
...
@@ -13,7 +13,6 @@ import (
...
@@ -13,7 +13,6 @@ import (
"hilo-user/req"
"hilo-user/req"
"hilo-user/resp"
"hilo-user/resp"
"strconv"
"strconv"
"time"
)
)
type
PostPutAnniversaryReq
struct
{
type
PostPutAnniversaryReq
struct
{
...
@@ -123,7 +122,7 @@ func PageAnniversary(c *gin.Context) (*mycontext.MyContext, error) {
...
@@ -123,7 +122,7 @@ func PageAnniversary(c *gin.Context) (*mycontext.MyContext, error) {
for
_
,
v
:=
range
anniversary
{
for
_
,
v
:=
range
anniversary
{
timestamp
:=
v
.
Timestamp
timestamp
:=
v
.
Timestamp
if
v
.
Type
==
cp_e
.
AnniversaryItemTypeAnniversary
&&
timestamp
>
0
{
if
v
.
Type
==
cp_e
.
AnniversaryItemTypeAnniversary
&&
timestamp
>
0
{
timestamp
=
calcNextAnniversary
(
timestamp
)
timestamp
=
c
p_m
.
C
alcNextAnniversary
(
timestamp
)
}
}
// 客户端只认识0 1
// 客户端只认识0 1
Type
:=
v
.
Type
Type
:=
v
.
Type
...
@@ -145,30 +144,6 @@ func PageAnniversary(c *gin.Context) (*mycontext.MyContext, error) {
...
@@ -145,30 +144,6 @@ func PageAnniversary(c *gin.Context) (*mycontext.MyContext, error) {
return
myCtx
,
nil
return
myCtx
,
nil
}
}
// 计算下一个纪念日
func
calcNextAnniversary
(
timestamp
int64
)
int64
{
now
:=
time
.
Now
()
// 还没超过一天,不用计算明年的
if
now
.
Unix
()
-
timestamp
<
86400
{
return
timestamp
}
birthday
:=
time
.
Unix
(
timestamp
,
0
)
// 计算今年的生日日期
thisYearBirthday
:=
time
.
Date
(
now
.
Year
(),
birthday
.
Month
(),
birthday
.
Day
(),
birthday
.
Hour
(),
birthday
.
Minute
(),
birthday
.
Second
(),
0
,
time
.
Local
)
// 如果今年的生日还未到,则生日日期为今年的生日日期;否则为明年的生日日期
var
next
time
.
Time
if
thisYearBirthday
.
After
(
now
)
{
next
=
thisYearBirthday
}
else
{
next
=
thisYearBirthday
.
AddDate
(
1
,
0
,
0
)
}
// 计算时间戳
nextTimestamp
:=
next
.
Unix
()
return
nextTimestamp
}
// @Tags CP v2
// @Tags CP v2
// @Summary 获取纪念日
// @Summary 获取纪念日
// @Param token header string true "token"
// @Param token header string true "token"
...
...
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