diff --git a/cron/cp_cron/anniversary.go b/cron/cp_cron/anniversary.go new file mode 100644 index 0000000000000000000000000000000000000000..1f73b57c2ee2ec41758c11d025a68d91bb5361a2 --- /dev/null +++ b/cron/cp_cron/anniversary.go @@ -0,0 +1,56 @@ +package cp_cron + +import ( + "encoding/json" + "git.hilo.cn/hilo-common/domain" + "git.hilo.cn/hilo-common/resource/config" + "git.hilo.cn/hilo-common/sdk/tencentyun" + "github.com/robfig/cron" + "hilo-user/domain/model/cp_m" + "hilo-user/domain/model/user_m" +) + +// 纪念日 +type CpAnniversaryNoticeMsg struct { + Identifier string `json:"identifier"` + Content string `json:"content"` + Date string `json:"date"` +} + +func CpAnniversaryNotice() { + c := cron.New() + // 1小时操作一次 + spec := "0 0 */1 * * ?" + if !config.AppIsRelease() { + // 测服1分钟 + spec = "0 * * * * ?" + } + _ = c.AddFunc(spec, func() { + var model = domain.CreateModelNil() + anniversary := cp_m.GetNeedRemindCpAnniversary(model) + for _, v := range anniversary { + var userIds []uint64 + userIds = append(userIds, v.UserId1, v.UserId2) + users, err := user_m.GetUserMapByIds(model, userIds) + if err != nil || len(users) < 2 { + model.Log.Errorf("GetUserMapByIds fail:%v", err) + continue + } + data, _ := json.Marshal(CpAnniversaryNoticeMsg{ + Identifier: "CpAnniversaryNotice", + Content: v.Content, + Date: v.Date, + }) + 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) + } + if err := tencentyun.BatchSendCustomMsg(model, 1, users[1].ExternalId, []string{users[0].ExternalId}, string(data), "cp纪念日"); err != nil { + model.Log.Errorf("BatchSendCustomMsg fail:%v", err) + } + if err := cp_m.UpdateCpAnniversaryReminded(model, v.ID); err != nil { + model.Log.Errorf("UpdateCpAnniversaryReminded fail:%v", err) + } + } + }) + c.Start() +} diff --git a/cron/cron.go b/cron/cron.go index c63df46205bc7bbf5f0fcc29f26d36bcab6ae112..d034cf6ab9a43e653d6c5bc3dd35af8842516d45 100644 --- a/cron/cron.go +++ b/cron/cron.go @@ -13,4 +13,5 @@ func Init() { gift_cron.SendGiftEventInit() // 礼物消息 gift_cron.GiftRemark() // 礼物消息补偿 cp_cron.ClearCpExpire() // 清理过期cp + cp_cron.CpAnniversaryNotice() // cp纪念日 } diff --git a/domain/model/cp_m/anniversary.go b/domain/model/cp_m/anniversary.go index 08ca9a47bef6aba10541aeb1ab14a21d30d1a0c3..bea8b3090f9687d4e13cbc05c7496709f7545a7d 100644 --- a/domain/model/cp_m/anniversary.go +++ b/domain/model/cp_m/anniversary.go @@ -3,6 +3,7 @@ package cp_m import ( "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/resource/mysql" + "time" ) // CpAnniversary cp纪念日 @@ -15,6 +16,7 @@ type CpAnniversary struct { Date string Timezone string IsRemind bool + Reminded mysql.YesNo } // 添加cp纪念日 @@ -27,6 +29,7 @@ func AddCpAnniversary(model *domain.Model, cp CpRelationTmp, content, date, tz s Date: date, Timezone: tz, IsRemind: isRemind, + Reminded: mysql.NO, }).Error } @@ -35,7 +38,7 @@ func UpdateCpAnniversary(model *domain.Model, id mysql.ID, content, date, tz str updates := map[string]interface{}{ "content": content, "date": date, - "timezone": tz, + "timezone": tz, "is_remind": isRemind, } return model.DB().Model(CpAnniversary{}).Where("id = ?", id).Updates(updates).Error @@ -57,3 +60,21 @@ func GetAllCpAnniversary(model *domain.Model, userId mysql.ID) []CpAnniversary { } return res } + +// 获取所有需要提醒的纪念日 +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("is_remind = 1"). + Where("reminded = ?", mysql.NO). + Find(&res).Error; err != nil { + model.Log.Errorf("GetNeedRemindCpAnniversary fail:%v", err) + } + return res +} + +func UpdateCpAnniversaryReminded(model *domain.Model, id mysql.ID) error { + return model.DB().Model(CpAnniversary{}).Where("id = ?", id).Update("reminded", mysql.YES).Error +} diff --git a/go.mod b/go.mod index 09851b8fd5e9e6bd51d17450e344873b09238315..9492f43b55a2857a96efe9c4bc033358605ae32e 100644 --- a/go.mod +++ b/go.mod @@ -65,6 +65,9 @@ require ( github.com/modern-go/reflect2 v1.0.1 // indirect github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 // indirect github.com/robfig/cron v1.2.0 // indirect + github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.479 // indirect + github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ims v1.0.479 // indirect + github.com/tencentyun/tls-sig-api-v2-golang v1.0.0 // indirect github.com/ugorji/go/codec v1.1.7 // indirect golang.org/x/net v0.0.0-20210428140749-89ef3d95e781 // indirect golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect diff --git a/go.sum b/go.sum index 71c73f3d67ce39daf53522bab1bfc01805cde73f..43ebc57ad82cf6a6fa673f5e65703b9102544d32 100644 --- a/go.sum +++ b/go.sum @@ -267,6 +267,12 @@ github.com/swaggo/gin-swagger v1.2.0/go.mod h1:qlH2+W7zXGZkczuL+r2nEBR2JTT+/lX05 github.com/swaggo/swag v1.5.1/go.mod h1:1Bl9F/ZBpVWh22nY0zmYyASPO1lI/zIwRDrpZU+tv8Y= github.com/swaggo/swag v1.6.7 h1:e8GC2xDllJZr3omJkm9YfmK0Y56+rMO3cg0JBKNz09s= github.com/swaggo/swag v1.6.7/go.mod h1:xDhTyuFIujYiN3DKWC/H/83xcfHp+UE/IzWWampG7Zc= +github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.479 h1:3kwDb6p1J3LxmwnNgSSEheemPffo+vMewoDzKysYdig= +github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.479/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y= +github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ims v1.0.479 h1:xDmo1rBmSJ7Hw3iOa6DQ/++z1d7pgsEVKrZno35zR7w= +github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ims v1.0.479/go.mod h1:3YlJ1g/Ko/iFgyBJdok+dRPTH6zRB6BBopAcFkbujPc= +github.com/tencentyun/tls-sig-api-v2-golang v1.0.0 h1:NavMw9XO2iCLv8hTKaJW2kTaGR2SdNljMABbe39yu6Q= +github.com/tencentyun/tls-sig-api-v2-golang v1.0.0/go.mod h1:u7WiArmCTXTaQAHJwAOaLgpJ5e2xdY5/cgMEy3ubL60= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= github.com/ugorji/go v1.1.5-pre/go.mod h1:FwP/aQVg39TXzItUBMwnWp9T9gPQnXw4Poh4/oBQZ/0= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=