cp_relation.go 1.92 KB
Newer Older
chenweijian's avatar
chenweijian committed
1 2 3 4
package cp_m

import (
	"git.hilo.cn/hilo-common/domain"
chenweijian's avatar
chenweijian committed
5
	"git.hilo.cn/hilo-common/resource/mysql"
chenweijian's avatar
chenweijian committed
6
	"gorm.io/gorm"
chenweijian's avatar
chenweijian committed
7
	"hilo-user/_const/enum/cp_e"
chenweijian's avatar
chenweijian committed
8 9 10 11 12 13 14 15 16 17 18
	"hilo-user/myerr/bizerr"
	"time"
)

type CpRelation struct {
	Id          uint64    `json:"id"`
	UserId1     uint64    `json:"userId1"`
	UserId2     uint64    `json:"userId2"`
	CreatedTime time.Time `json:"createdTime"`
}

chenweijian's avatar
chenweijian committed
19 20 21 22 23 24 25 26
type CpInvite struct {
	Id           uint64     `json:"id"`
	UserId       uint64     `json:"userId"`
	InviteUserId uint64     `json:"inviteUserId"`
	DiamondNum   uint32     `json:"diamondNum"`
	Status       mysql.Type `json:"status"`
}

chenweijian's avatar
chenweijian committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
func CreateCp(model *domain.Model, userId1, userId2 uint64) error {
	userIds := []uint64{userId1, userId2}
	result := model.DB().Exec("insert into cp_relation(user_id1, user_id2) values(?,?) where not exists (select user_id1 from cp_relation where user_id1 in (?) or user_id2 in (?));", userId1, userId2, userIds, userIds)
	if result.Error != nil {
		model.Log.Errorf("CreateCp user1:%d, user2:%d, err:%v", userId1, userId2, result.Error)
		return result.Error
	}
	if result.RowsAffected <= 0 {
		return bizerr.TransactionFailed
	}
	return nil
}

func GetCp(model *domain.Model, userId uint64) (*CpRelation, error) {
	res := new(CpRelation)
	err := model.DB().Model(CpRelation{}).Where("user_id1 = ? or user_id2 = ?", userId, userId).First(&res).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return res, nil
		}
		model.Log.Errorf("CreateCp userId:%d, err:%v", userId, err)
		return nil, err
	}
	return res, nil
}
chenweijian's avatar
chenweijian committed
52 53 54 55 56 57 58 59 60

func CreateCpInvite(model *domain.Model, userId, userIdInvite uint64, diamondNum uint32) error {
	err := model.DB().Model(CpInvite{}).Create(CpInvite{UserId: userId, InviteUserId: userIdInvite, DiamondNum: diamondNum, Status: mysql.Type(cp_e.CpInvite)}).Error
	if err != nil {
		model.Log.Errorf("CreateCpInvite user1:%d, user2:%d, diamondNum:%d, err:%v", userId, userIdInvite, diamondNum, err)
		return err
	}
	return nil
}