anniversary.go 6.31 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
package cp_m

import (
	"fmt"
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
	"github.com/bluele/gcache"
	"hilo-user/_const/enum/cp_e"
	"hilo-user/domain/model/res_m"
	"hilo-user/domain/model/user_m"
	"time"
)

// CpAnniversary  cp纪念日
type CpAnniversary struct {
	mysql.Entity
	Type           cp_e.AnniversaryItemType
	CpId           mysql.ID
	UserId1        mysql.ID
	UserId2        mysql.ID
	Content        string
	Timestamp      int64
	IsRemind       bool
	LastRemindTime int64
	Sort           int
hujiebin's avatar
hujiebin committed
26
	MsgId          uint
hujiebin's avatar
hujiebin committed
27 28 29 30 31 32 33 34 35
}

// 初始化6个cp纪念日
// 1)我们在一起;2)XXX的生日;3)XXX的生日;4)第一次说我爱你;5)第一次亲吻;6)结婚纪念日
func InitCpAnniversary(model *domain.Model, cp CpRelation, lang string) error {
	users, err := user_m.GetUserMapByIds(model, []uint64{cp.UserId1, cp.UserId2})
	if err != nil {
		return err
	}
hujiebin's avatar
hujiebin committed
36
	if err := AddCpAnniversary(model, cp_e.AnniversaryItemTypeNormal, cp, GetTranslate(259, lang), time.Now().Unix(), true, 100, 259); err != nil {
hujiebin's avatar
hujiebin committed
37 38
		return err
	}
hujiebin's avatar
hujiebin committed
39
	if err := AddCpAnniversary(model, cp_e.AnniversaryItemTypeAnniversary, cp, fmt.Sprintf(GetTranslate(260, lang), users[cp.UserId1].Nick), 0, true, 0, 260); err != nil {
hujiebin's avatar
hujiebin committed
40 41
		return err
	}
hujiebin's avatar
hujiebin committed
42
	if err := AddCpAnniversary(model, cp_e.AnniversaryItemTypeAnniversary, cp, fmt.Sprintf(GetTranslate(260, lang), users[cp.UserId2].Nick), 0, true, 0, 260); err != nil {
hujiebin's avatar
hujiebin committed
43 44
		return err
	}
hujiebin's avatar
hujiebin committed
45
	if err := AddCpAnniversary(model, cp_e.AnniversaryItemTypeNormal, cp, GetTranslate(261, lang), 0, true, 0, 261); err != nil {
hujiebin's avatar
hujiebin committed
46 47
		return err
	}
hujiebin's avatar
hujiebin committed
48
	if err := AddCpAnniversary(model, cp_e.AnniversaryItemTypeNormal, cp, GetTranslate(262, lang), 0, true, 0, 262); err != nil {
hujiebin's avatar
hujiebin committed
49 50
		return err
	}
hujiebin's avatar
hujiebin committed
51
	if err := AddCpAnniversary(model, cp_e.AnniversaryItemTypeAnniversary, cp, GetTranslate(263, lang), 0, true, 0, 263); err != nil {
hujiebin's avatar
hujiebin committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
		return err
	}
	return nil
}

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"
}

// 添加cp纪念日
hujiebin's avatar
hujiebin committed
72
func AddCpAnniversary(model *domain.Model, Type cp_e.AnniversaryItemType, cp CpRelation, content string, ts int64, isRemind bool, sort int, msgId uint) error {
hujiebin's avatar
hujiebin committed
73 74 75 76 77 78 79 80 81 82
	return model.DB().Model(CpAnniversary{}).Create(&CpAnniversary{
		CpId:           cp.Id,
		Type:           Type,
		UserId1:        cp.UserId1,
		UserId2:        cp.UserId2,
		Content:        content,
		Timestamp:      ts,
		IsRemind:       isRemind,
		LastRemindTime: 0,
		Sort:           sort,
hujiebin's avatar
hujiebin committed
83
		MsgId:          msgId,
hujiebin's avatar
hujiebin committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	}).Error
}

// 更新cp纪念日
func UpdateCpAnniversary(model *domain.Model, id mysql.ID, content string, ts int64, isRemind bool) error {
	updates := map[string]interface{}{
		"content":   content,
		"timestamp": ts,
		"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获取所有纪念日
hujiebin's avatar
hujiebin committed
102
func GetAllCpAnniversary(model *domain.Model, userId mysql.ID, lang string) []CpAnniversary {
hujiebin's avatar
hujiebin committed
103 104 105 106 107 108 109 110
	var res []CpAnniversary
	relation, exists := GetCpRelation(model, userId)
	if !exists {
		return res
	}
	if err := model.DB().Model(CpAnniversary{}).Where("cp_id = ?", relation.Id).Order("`sort` DESC,updated_time DESC,id ASC").Find(&res).Error; err != nil {
		model.Log.Errorf("GetAllCpAnniversary fail:%v", err)
	}
hujiebin's avatar
hujiebin committed
111 112 113
	var userIds = []mysql.ID{relation.UserId1, relation.UserId2}
	users, _ := user_m.GetUserMapByIds(model, userIds)
	n := 0
hujiebin's avatar
hujiebin committed
114 115 116
	for i, v := range res {
		if v.CreatedTime.Equal(v.UpdatedTime) && v.MsgId > 0 {
			res[i].Content = GetTranslate(v.MsgId, lang)
hujiebin's avatar
hujiebin committed
117 118 119 120 121 122 123 124
			if v.MsgId == 260 {
				n++
				if n == 1 {
					res[i].Content = fmt.Sprintf(GetTranslate(v.MsgId, lang), users[relation.UserId1].Nick)
				} else {
					res[i].Content = fmt.Sprintf(GetTranslate(v.MsgId, lang), users[relation.UserId2].Nick)
				}
			}
hujiebin's avatar
hujiebin committed
125 126
		}
	}
hujiebin's avatar
hujiebin committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
	return res
}

// 获取所有需要提醒的纪念日
func GetNeedRemindCpAnniversary(model *domain.Model) []CpAnniversary {
	var rows, res []CpAnniversary
	if err := model.DB().Model(CpAnniversary{}).
		Where("`timestamp` > 0").
		Where("`timestamp` < ?", time.Now().Unix()).
		Where("is_remind = 1").
		Where("last_remind_time < ?", time.Now().AddDate(-1, 0, 0).Unix()). // 一年前提醒过
		Find(&rows).Error; err != nil {
		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
}

// 获取cp当天需要提醒的纪念日
func GetUserTodayCpAnniversary(model *domain.Model, cpId mysql.ID) []CpAnniversary {
	var rows, res []CpAnniversary
	if err := model.DB().Model(CpAnniversary{}).
		Where("`timestamp` > 0").
		Where("`timestamp` < ?", time.Now().Unix()).
		Where("is_remind = 1").
		Where("cp_id = ?", cpId).
		Find(&rows).Error; err != nil {
		model.Log.Errorf("GetUserTodayCpAnniversary fail:%v", err)
	}
	now := time.Now().Unix()
	for i, v := range rows {
		ts := CalcNextAnniversary(v.Timestamp)
		if now > ts {
			res = append(res, rows[i])
		}
	}
hujiebin's avatar
hujiebin committed
169
	return res
hujiebin's avatar
hujiebin committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
}

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
}

// 计算下一个纪念日
func CalcNextAnniversary(timestamp int64) int64 {
	now := time.Now()
	// 还没超过一天,不用计算明年的
	if now.Unix()-timestamp < 86400 {
		return timestamp
	}
	birthday := time.Unix(timestamp, 0)
	// 计算今年的生日日期
hujiebin's avatar
hujiebin committed
185
	thisYearBirthday := time.Date(now.Year(), birthday.Month(), birthday.Day(), 0, 0, 0, 0, time.Local)
hujiebin's avatar
hujiebin committed
186 187 188 189 190 191 192 193 194 195 196 197 198

	// 如果今年的生日还未到,则生日日期为今年的生日日期;否则为明年的生日日期
	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
}