package cp_m import ( "fmt" "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/resource/mysql" "hilo-user/_const/enum/cp_e" "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 Reminded mysql.YesNo Sort int } // 初始化6个cp纪念日 // 1)我们在一起;2)XXX的生日;3)XXX的生日;4)第一次说我爱你;5)第一次亲吻;6)结婚纪念日 // todo 翻译 func InitCpAnniversary(model *domain.Model, cp CpRelationTmp) error { users, err := user_m.GetUserMapByIds(model, []uint64{cp.UserId1, cp.UserId2}) if err != nil { return err } if err := AddCpAnniversary(model, cp_e.AnniversaryItemTypeNormal, cp, "我们在一起", time.Now().Unix(), true, 100); err != nil { return err } if err := AddCpAnniversary(model, cp_e.AnniversaryItemTypeAnniversary, cp, fmt.Sprintf("%s的生日", users[cp.UserId1].Nick), int64(users[cp.UserId1].Birthday), true, 0); err != nil { return err } if err := AddCpAnniversary(model, cp_e.AnniversaryItemTypeAnniversary, cp, fmt.Sprintf("%s的生日", users[cp.UserId2].Nick), int64(users[cp.UserId2].Birthday), true, 0); err != nil { return err } if err := AddCpAnniversary(model, cp_e.AnniversaryItemTypeNormal, cp, "第一次说我爱你", 0, false, 0); err != nil { return err } if err := AddCpAnniversary(model, cp_e.AnniversaryItemTypeNormal, cp, "第一次亲吻", 0, false, 0); err != nil { return err } if err := AddCpAnniversary(model, cp_e.AnniversaryItemTypeAnniversary, cp, "结婚纪念日", 0, false, 0); err != nil { return err } return nil } // 添加cp纪念日 func AddCpAnniversary(model *domain.Model, Type cp_e.AnniversaryItemType, cp CpRelationTmp, content string, ts int64, isRemind bool, sort int) error { return model.DB().Model(CpAnniversary{}).Create(&CpAnniversary{ CpId: cp.ID, Type: Type, UserId1: cp.UserId1, UserId2: cp.UserId2, Content: content, Timestamp: ts, IsRemind: isRemind, Reminded: mysql.NO, Sort: sort, }).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获取所有纪念日 func GetAllCpAnniversary(model *domain.Model, userId mysql.ID) []CpAnniversary { 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) } return res } // 获取所有需要提醒的纪念日 func GetNeedRemindCpAnniversary(model *domain.Model) []CpAnniversary { var res []CpAnniversary if err := model.DB().Model(CpAnniversary{}). Where("`timestamp` > ?", time.Now().Unix()). 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 }