Commit fe8c02a6 authored by hujiebin's avatar hujiebin

fix:计算下一个纪念日

parent aa93073a
...@@ -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 rows, res []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(&res).Error; err != nil { Find(&rows).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 rows, res []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(&res).Error; err != nil { Find(&rows).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
}
...@@ -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 = cp_m.CalcNextAnniversary(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"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment