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

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
hujiebin's avatar
hujiebin committed
6
	"hilo-user/_const/enum/cp_e"
hujiebin's avatar
hujiebin committed
7
	"time"
hujiebin's avatar
hujiebin committed
8 9 10 11 12
)

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

// 添加cp纪念日
hujiebin's avatar
hujiebin committed
25
func AddCpAnniversary(model *domain.Model, cp CpRelationTmp, content string, ts int64, isRemind bool) error {
hujiebin's avatar
hujiebin committed
26
	return model.DB().Model(CpAnniversary{}).Create(&CpAnniversary{
hujiebin's avatar
hujiebin committed
27 28 29 30 31 32 33
		CpId:      cp.ID,
		UserId1:   cp.UserId1,
		UserId2:   cp.UserId2,
		Content:   content,
		Timestamp: ts,
		IsRemind:  isRemind,
		Reminded:  mysql.NO,
hujiebin's avatar
hujiebin committed
34 35 36 37
	}).Error
}

// 更新cp纪念日
hujiebin's avatar
hujiebin committed
38
func UpdateCpAnniversary(model *domain.Model, id mysql.ID, content string, ts int64, isRemind bool) error {
hujiebin's avatar
hujiebin committed
39 40
	updates := map[string]interface{}{
		"content":   content,
hujiebin's avatar
hujiebin committed
41
		"timestamp": ts,
hujiebin's avatar
hujiebin committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
		"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
58
	if err := model.DB().Model(CpAnniversary{}).Where("cp_id = ?", relation.ID).Order("`sort` DESC,updated_time DESC").Find(&res).Error; err != nil {
hujiebin's avatar
hujiebin committed
59 60 61 62
		model.Log.Errorf("GetAllCpAnniversary fail:%v", err)
	}
	return res
}
hujiebin's avatar
hujiebin committed
63 64 65 66 67

// 获取所有需要提醒的纪念日
func GetNeedRemindCpAnniversary(model *domain.Model) []CpAnniversary {
	var res []CpAnniversary
	if err := model.DB().Model(CpAnniversary{}).
hujiebin's avatar
hujiebin committed
68
		Where("`timestamp` > ?", time.Now().Unix()).
hujiebin's avatar
hujiebin committed
69 70 71 72 73 74 75 76 77 78 79
		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
}