Commit 9b2e3211 authored by hujiebin's avatar hujiebin

初始化cpLevel

parent b9d44c5f
......@@ -87,14 +87,14 @@ type Cp struct {
CreatedTime time.Time `json:"createdTime"`
}
func CreateCp(model *domain.Model, userId1, userId2 uint64) error {
func CreateCp(model *domain.Model, userId1, userId2 uint64) (int64, error) {
userIds := []uint64{userId1, userId2}
oldCp := make([]*Cp, 0)
// 这两个人以前是否有旧的cp关系
err := model.DB().Model(Cp{}).Where("status = 1 and user_id1 in (?) and user_id2 in (?)", userIds, userIds).Find(&oldCp).Error
if err != nil {
model.Log.Errorf("CreateCp user1:%d, user2:%d, err:%v", userId1, userId2, err)
return err
return 0, err
}
createdTime := time.Now()
if len(oldCp) > 0 {
......@@ -105,16 +105,18 @@ func CreateCp(model *domain.Model, userId1, userId2 uint64) error {
}
}
result := model.DB().Exec("insert into cp_relation(user_id1, user_id2, created_time) select ?,?,? where not exists (select user_id1 from cp_relation where user_id1 in (?) or user_id2 in (?));", userId1, userId2, createdTime.Format(utils.DATETIME_FORMAT), userIds, userIds)
if result.Error != nil {
model.Log.Errorf("CreateCp user1:%d, user2:%d, err:%v", userId1, userId2, result.Error)
return result.Error
result, err := model.DB().Config.ConnPool.ExecContext(model, "insert into cp_relation(user_id1, user_id2, created_time) select ?,?,? where not exists (select user_id1 from cp_relation where user_id1 in (?) or user_id2 in (?));", userId1, userId2, createdTime.Format(utils.DATETIME_FORMAT), userIds, userIds)
if err != nil {
model.Log.Errorf("CreateCp user1:%d, user2:%d, err:%v", userId1, userId2, err)
return 0, err
}
if result.RowsAffected <= 0 {
rowAffected, _ := result.RowsAffected()
if rowAffected <= 0 {
model.Log.Errorf("CreateCp user1:%d, user2:%d, err:%v", userId1, userId2, bizerr.TransactionFailed)
return bizerr.TransactionFailed
return 0, bizerr.TransactionFailed
}
return nil
id, err := result.LastInsertId()
return id, err
}
func GetCp(model *domain.Model, userId uint64) (*CpRelation, error) {
......
......@@ -5,6 +5,7 @@ import (
"git.hilo.cn/hilo-common/domain"
"git.hilo.cn/hilo-common/resource/mysql"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"hilo-user/_const/enum/cp_e"
"hilo-user/myerr"
"time"
......@@ -127,6 +128,16 @@ func GetApplyToUnbind(model *domain.Model, userId, cpUserId mysql.ID) bool {
return total > 0
}
// 初始化cpLevel
func InitCpLevel(model *domain.Model, cpId, userId1, userId2 mysql.ID) error {
return model.DB().Model(CpLevel{}).Clauses(clause.OnConflict{DoNothing: true}).Create(&CpLevel{
CpId: cpId,
UserId1: userId1,
UserId2: userId2,
ExpireAt: time.Now().AddDate(0, 1, 0),
}).Error
}
// 增加cp等级积分
// 送礼1钻石=1点数
// condition
......
......@@ -7,6 +7,7 @@ import (
"git.hilo.cn/hilo-common/domain"
"git.hilo.cn/hilo-common/mycontext"
"git.hilo.cn/hilo-common/myerr/comerr"
"git.hilo.cn/hilo-common/resource/mysql"
"git.hilo.cn/hilo-common/sdk/tencentyun"
"git.hilo.cn/hilo-common/txop/msg"
"git.hilo.cn/hilo-common/utils"
......@@ -184,11 +185,16 @@ func ReplyCpInvite(c *gin.Context) (*mycontext.MyContext, error) {
var msgData []byte
if optType == 1 { // 接受
// 写入cp关系表
err = cp_m.CreateCp(model, userSender.ID, user.ID)
cpId, err := cp_m.CreateCp(model, userSender.ID, user.ID)
if err != nil {
model.Log.Errorf("ReplyCpInvite userSender:%d, user:%d, status:%d, err:%v", userSender.ID, user.ID, updateStatus, err)
return err
}
// 初始化cp等级
if err := cp_m.InitCpLevel(model, mysql.ID(cpId), userSender.ID, user.ID); err != nil {
model.Log.Errorf("ReplyCpInvite InitCpLevel fail:%v-%v-%v", userSender.ID, user.ID, err)
return err
}
// 发放告白礼物 cwj----
//if _, err = bag_tx.SendUserBag(model, userSender.ID, 1, award.GiftId, award.GiftN, 3, "告白礼物"); err != nil {
// model.Log.Errorf("ReplyCpInvite userSender:%d, user:%d, status:%d, err:%v", userSender.ID, user.ID, updateStatus, err)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment