anniversary.go 4.6 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3
package cp_m

import (
hujiebin's avatar
hujiebin committed
4
	"fmt"
hujiebin's avatar
hujiebin committed
5 6
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
hujiebin's avatar
hujiebin committed
7
	"github.com/bluele/gcache"
hujiebin's avatar
hujiebin committed
8
	"hilo-user/_const/enum/cp_e"
hujiebin's avatar
hujiebin committed
9
	"hilo-user/domain/model/res_m"
hujiebin's avatar
hujiebin committed
10
	"hilo-user/domain/model/user_m"
hujiebin's avatar
hujiebin committed
11
	"time"
hujiebin's avatar
hujiebin committed
12 13 14 15 16
)

// CpAnniversary  cp纪念日
type CpAnniversary struct {
	mysql.Entity
hujiebin's avatar
hujiebin committed
17
	Type      cp_e.AnniversaryItemType
hujiebin's avatar
hujiebin committed
18
	CpId      mysql.ID
hujiebin's avatar
hujiebin committed
19 20 21 22 23 24
	UserId1   mysql.ID
	UserId2   mysql.ID
	Content   string
	Timestamp int64
	IsRemind  bool
	Reminded  mysql.YesNo
hujiebin's avatar
hujiebin committed
25
	Sort      int
hujiebin's avatar
hujiebin committed
26 27
}

hujiebin's avatar
hujiebin committed
28 29
// 初始化6个cp纪念日
// 1)我们在一起;2)XXX的生日;3)XXX的生日;4)第一次说我爱你;5)第一次亲吻;6)结婚纪念日
hujiebin's avatar
hujiebin committed
30
func InitCpAnniversary(model *domain.Model, cp CpRelationTmp, lang string) error {
hujiebin's avatar
hujiebin committed
31 32 33 34
	users, err := user_m.GetUserMapByIds(model, []uint64{cp.UserId1, cp.UserId2})
	if err != nil {
		return err
	}
hujiebin's avatar
hujiebin committed
35
	if err := AddCpAnniversary(model, cp_e.AnniversaryItemTypeNormal, cp, GetTranslate(259, lang), time.Now().Unix(), true, 100); err != nil {
hujiebin's avatar
hujiebin committed
36 37
		return err
	}
hujiebin's avatar
hujiebin committed
38
	if err := AddCpAnniversary(model, cp_e.AnniversaryItemTypeAnniversary, cp, fmt.Sprintf(GetTranslate(260, lang), users[cp.UserId1].Nick), 0, true, 0); err != nil {
hujiebin's avatar
hujiebin committed
39 40
		return err
	}
hujiebin's avatar
hujiebin committed
41
	if err := AddCpAnniversary(model, cp_e.AnniversaryItemTypeAnniversary, cp, fmt.Sprintf(GetTranslate(260, lang), users[cp.UserId2].Nick), 0, true, 0); err != nil {
hujiebin's avatar
hujiebin committed
42 43
		return err
	}
hujiebin's avatar
hujiebin committed
44
	if err := AddCpAnniversary(model, cp_e.AnniversaryItemTypeNormal, cp, GetTranslate(261, lang), 0, false, 0); err != nil {
hujiebin's avatar
hujiebin committed
45 46
		return err
	}
hujiebin's avatar
hujiebin committed
47
	if err := AddCpAnniversary(model, cp_e.AnniversaryItemTypeNormal, cp, GetTranslate(262, lang), 0, false, 0); err != nil {
hujiebin's avatar
hujiebin committed
48 49
		return err
	}
hujiebin's avatar
hujiebin committed
50
	if err := AddCpAnniversary(model, cp_e.AnniversaryItemTypeAnniversary, cp, GetTranslate(263, lang), 0, false, 0); err != nil {
hujiebin's avatar
hujiebin committed
51 52 53 54 55
		return err
	}
	return nil
}

hujiebin's avatar
hujiebin committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69
var translateCache = gcache.New(1000).LRU().Build()

func GetTranslate(msgId uint, lang string) string {
	key := fmt.Sprintf("%v-%v", msgId, lang)
	if data, err := translateCache.Get(key); err == nil {
		return data.(string)
	}
	if resMul, _ := res_m.GetResMultiTextBy(mysql.Db, msgId, lang); resMul != nil {
		_ = translateCache.SetWithExpire(key, resMul.Content, time.Hour)
		return resMul.Content
	}
	return "default"
}

hujiebin's avatar
hujiebin committed
70
// 添加cp纪念日
hujiebin's avatar
hujiebin committed
71
func AddCpAnniversary(model *domain.Model, Type cp_e.AnniversaryItemType, cp CpRelationTmp, content string, ts int64, isRemind bool, sort int) error {
hujiebin's avatar
hujiebin committed
72
	return model.DB().Model(CpAnniversary{}).Create(&CpAnniversary{
hujiebin's avatar
hujiebin committed
73
		CpId:      cp.ID,
hujiebin's avatar
hujiebin committed
74
		Type:      Type,
hujiebin's avatar
hujiebin committed
75 76 77 78 79 80
		UserId1:   cp.UserId1,
		UserId2:   cp.UserId2,
		Content:   content,
		Timestamp: ts,
		IsRemind:  isRemind,
		Reminded:  mysql.NO,
hujiebin's avatar
hujiebin committed
81
		Sort:      sort,
hujiebin's avatar
hujiebin committed
82 83 84 85
	}).Error
}

// 更新cp纪念日
hujiebin's avatar
hujiebin committed
86
func UpdateCpAnniversary(model *domain.Model, id mysql.ID, content string, ts int64, isRemind bool) error {
hujiebin's avatar
hujiebin committed
87 88
	updates := map[string]interface{}{
		"content":   content,
hujiebin's avatar
hujiebin committed
89
		"timestamp": ts,
hujiebin's avatar
hujiebin committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
		"is_remind": isRemind,
	}
	return model.DB().Model(CpAnniversary{}).Where("id = ?", id).Updates(updates).Error
}

func DelCpAnniversary(model *domain.Model, id mysql.ID) error {
	return model.DB().Model(CpAnniversary{}).Where("id = ? ", id).Delete(&CpAnniversary{}).Error
}

// 根据用户id获取所有纪念日
func GetAllCpAnniversary(model *domain.Model, userId mysql.ID) []CpAnniversary {
	var res []CpAnniversary
	relation, exists := GetCpRelation(model, userId)
	if !exists {
		return res
	}
hujiebin's avatar
hujiebin committed
106
	if err := model.DB().Model(CpAnniversary{}).Where("cp_id = ?", relation.ID).Order("`sort` DESC,updated_time DESC,id ASC").Find(&res).Error; err != nil {
hujiebin's avatar
hujiebin committed
107 108 109 110
		model.Log.Errorf("GetAllCpAnniversary fail:%v", err)
	}
	return res
}
hujiebin's avatar
hujiebin committed
111 112 113 114 115

// 获取所有需要提醒的纪念日
func GetNeedRemindCpAnniversary(model *domain.Model) []CpAnniversary {
	var res []CpAnniversary
	if err := model.DB().Model(CpAnniversary{}).
116 117
		Where("`timestamp` >= ?", time.Now().Unix()-86400).
		Where("`timestamp` < ?", time.Now().Unix()).
hujiebin's avatar
hujiebin committed
118 119 120 121 122 123 124 125
		Where("is_remind = 1").
		Where("reminded = ?", mysql.NO).
		Find(&res).Error; err != nil {
		model.Log.Errorf("GetNeedRemindCpAnniversary fail:%v", err)
	}
	return res
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139
// 获取cp当天需要提醒的纪念日
func GetUserTodayCpAnniversary(model *domain.Model, cpId mysql.ID) []CpAnniversary {
	var res []CpAnniversary
	if err := model.DB().Model(CpAnniversary{}).
		Where("`timestamp` >= ?", time.Now().Unix()-86400).
		Where("`timestamp` < ?", time.Now().Unix()).
		Where("is_remind = 1").
		Where("cp_id = ?", cpId).
		Find(&res).Error; err != nil {
		model.Log.Errorf("GetUserTodayCpAnniversary fail:%v", err)
	}
	return res
}

hujiebin's avatar
hujiebin committed
140 141 142
func UpdateCpAnniversaryReminded(model *domain.Model, id mysql.ID) error {
	return model.DB().Model(CpAnniversary{}).Where("id = ?", id).Update("reminded", mysql.YES).Error
}