From d7e6e1adddb9f86802f25d3942bba04b02bae13e Mon Sep 17 00:00:00 2001 From: hujiebin Date: Wed, 7 Jun 2023 10:30:11 +0800 Subject: [PATCH] refact: timestamp --- cron/cp_cron/anniversary.go | 4 ++-- cv/cp_cv/rank.go | 8 +++---- domain/model/cp_m/anniversary.go | 40 ++++++++++++++------------------ route/cp_r/anniversary.go | 26 +++++++++------------ 4 files changed, 35 insertions(+), 43 deletions(-) diff --git a/cron/cp_cron/anniversary.go b/cron/cp_cron/anniversary.go index 037ab10..1bbda1c 100644 --- a/cron/cp_cron/anniversary.go +++ b/cron/cp_cron/anniversary.go @@ -14,7 +14,7 @@ import ( type CpAnniversaryNoticeMsg struct { Identifier string `json:"identifier"` Content string `json:"content"` - Date string `json:"date"` + Timestamp int64 `json:"timestamp"` } func CpAnniversaryNotice() { @@ -39,7 +39,7 @@ func CpAnniversaryNotice() { data, _ := json.Marshal(CpAnniversaryNoticeMsg{ Identifier: "CpAnniversaryNotice", Content: v.Content, - Date: v.Date, + Timestamp: v.Timestamp, }) if err := tencentyun.BatchSendCustomMsg(model, 1, users[0].ExternalId, []string{users[1].ExternalId}, string(data), "cp纪念日"); err != nil { model.Log.Errorf("BatchSendCustomMsg fail:%v", err) diff --git a/cv/cp_cv/rank.go b/cv/cp_cv/rank.go index 2f441b1..0a5949e 100644 --- a/cv/cp_cv/rank.go +++ b/cv/cp_cv/rank.go @@ -25,10 +25,10 @@ type CvCpAchievement struct { } type CvCpAnniversary struct { - Id uint64 `json:"id"` // 记录id - Content string `json:"content"` // 纪念日内容 - Date string `json:"date"` // 纪念日日期 - IsRemind bool `json:"isRemind"` // 是否提醒 + Id uint64 `json:"id"` // 记录id + Content string `json:"content"` // 纪念日内容 + Timestamp int64 `json:"timestamp"` // 纪念日时间戳 + IsRemind bool `json:"isRemind"` // 是否提醒 } type CpTops struct { diff --git a/domain/model/cp_m/anniversary.go b/domain/model/cp_m/anniversary.go index bea8b30..2c8e91e 100644 --- a/domain/model/cp_m/anniversary.go +++ b/domain/model/cp_m/anniversary.go @@ -9,36 +9,33 @@ import ( // CpAnniversary cp纪念日 type CpAnniversary struct { mysql.Entity - CpId mysql.ID - UserId1 mysql.ID - UserId2 mysql.ID - Content string - Date string - Timezone string - IsRemind bool - Reminded mysql.YesNo + CpId mysql.ID + UserId1 mysql.ID + UserId2 mysql.ID + Content string + Timestamp int64 + IsRemind bool + Reminded mysql.YesNo } // 添加cp纪念日 -func AddCpAnniversary(model *domain.Model, cp CpRelationTmp, content, date, tz string, isRemind bool) error { +func AddCpAnniversary(model *domain.Model, cp CpRelationTmp, content string, ts int64, isRemind bool) error { return model.DB().Model(CpAnniversary{}).Create(&CpAnniversary{ - CpId: cp.ID, - UserId1: cp.UserId1, - UserId2: cp.UserId2, - Content: content, - Date: date, - Timezone: tz, - IsRemind: isRemind, - Reminded: mysql.NO, + CpId: cp.ID, + UserId1: cp.UserId1, + UserId2: cp.UserId2, + Content: content, + Timestamp: ts, + IsRemind: isRemind, + Reminded: mysql.NO, }).Error } // 更新cp纪念日 -func UpdateCpAnniversary(model *domain.Model, id mysql.ID, content, date, tz string, isRemind bool) error { +func UpdateCpAnniversary(model *domain.Model, id mysql.ID, content string, ts int64, isRemind bool) error { updates := map[string]interface{}{ "content": content, - "date": date, - "timezone": tz, + "timestamp": ts, "is_remind": isRemind, } return model.DB().Model(CpAnniversary{}).Where("id = ?", id).Updates(updates).Error @@ -64,9 +61,8 @@ func GetAllCpAnniversary(model *domain.Model, userId mysql.ID) []CpAnniversary { // 获取所有需要提醒的纪念日 func GetNeedRemindCpAnniversary(model *domain.Model) []CpAnniversary { var res []CpAnniversary - date := time.Now().Format("2006-01-02") if err := model.DB().Model(CpAnniversary{}). - Where("`date` = ?", date). + Where("`timestamp` > ?", time.Now().Unix()). Where("is_remind = 1"). Where("reminded = ?", mysql.NO). Find(&res).Error; err != nil { diff --git a/route/cp_r/anniversary.go b/route/cp_r/anniversary.go index 6b52edc..fa270fc 100644 --- a/route/cp_r/anniversary.go +++ b/route/cp_r/anniversary.go @@ -13,18 +13,17 @@ import ( ) type PostPutAnniversaryReq struct { - Content string `form:"content" binding:"required"` - Date string `form:"date" binding:"required"` - IsRemind bool `form:"isRemind"` + Content string `form:"content" binding:"required"` + Timestamp int64 `form:"timestamp" binding:"required"` + IsRemind bool `form:"isRemind"` } // @Tags CP v2 // @Summary 发布纪念日 // @Param token header string true "token" // @Param nonce header string true "随机数字" -// @Param timezone header string true "时区" // @Param content formData string true "纪念日名称" -// @Param date formData string true "纪念日时间(年月日)" +// @Param timestamp formData int true "时间戳" // @Param isRemind formData bool false "是否提醒" // @Success 200 // @Router /v2/cp/anniversary [post] @@ -43,8 +42,7 @@ func PostAnniversary(c *gin.Context) (*mycontext.MyContext, error) { if !exits { return myCtx, bizerr.CpNotRelation } else { - tz := c.GetHeader(mycontext.TIMEZONE) - if err := cp_m.AddCpAnniversary(model, relation, param.Content, param.Date, tz, param.IsRemind); err != nil { + if err := cp_m.AddCpAnniversary(model, relation, param.Content, param.Timestamp, param.IsRemind); err != nil { return myCtx, err } } @@ -56,9 +54,8 @@ func PostAnniversary(c *gin.Context) (*mycontext.MyContext, error) { // @Summary 修改纪念日 // @Param token header string true "token" // @Param nonce header string true "随机数字" -// @Param timezone header string true "时区" // @Param content formData string true "纪念日名称" -// @Param date formData string true "纪念日时间(年月日)" +// @Param timestamp formData int true "时间戳" // @Param isRemind formData bool false "是否提醒" // @Param id path int true "更新的记录id" // @Success 200 @@ -71,8 +68,7 @@ func PutAnniversary(c *gin.Context) (*mycontext.MyContext, error) { } id, _ := strconv.ParseUint(c.Param("id"), 10, 64) model := domain.CreateModelContext(myCtx) - tz := c.GetHeader(mycontext.TIMEZONE) - if err := cp_m.UpdateCpAnniversary(model, id, param.Content, param.Date, tz, param.IsRemind); err != nil { + if err := cp_m.UpdateCpAnniversary(model, id, param.Content, param.Timestamp, param.IsRemind); err != nil { return myCtx, err } resp.ResponseOk(c, "") @@ -96,10 +92,10 @@ func PageAnniversary(c *gin.Context) (*mycontext.MyContext, error) { anniversary := cp_m.GetAllCpAnniversary(model, userId) for _, v := range anniversary { response = append(response, cp_cv.CvCpAnniversary{ - Id: v.ID, - Content: v.Content, - Date: v.Date, - IsRemind: v.IsRemind, + Id: v.ID, + Content: v.Content, + Timestamp: v.Timestamp, + IsRemind: v.IsRemind, }) } resp.ResponsePageBaseOk(c, response, 0, false) -- 2.22.0