like.go 2.68 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 26 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
package user_m

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
	"gorm.io/gorm"
	"hilo-user/_const/enum/user_e"
	"hilo-user/myerr"
)

//用户喜欢
type UserLike struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	UserId        mysql.ID
	LikeUserId    mysql.ID
	SceneType     user_e.UserLikeSceneType
}

type UserLikeOperate struct {
	mysql.Entity
	UserId     mysql.ID
	LikeUserId mysql.ID
	Type       user_e.UserLikeOperateType
	SceneType  user_e.UserLikeSceneType
}

func AddUserLikeOperate(model *domain.Model, userId mysql.ID, likeUserId mysql.ID, t user_e.UserLikeOperateType, sceneType user_e.UserLikeSceneType) (mysql.ID, error) {
	userLikeOperate := UserLikeOperate{
		UserId:     userId,
		LikeUserId: likeUserId,
		Type:       t,
		SceneType:  sceneType,
	}
	if err := model.Db.Create(&userLikeOperate).Error; err != nil {
		return 0, myerr.WrapErr(err)
	} else {
		return userLikeOperate.ID, nil
	}
}

func initUserLike(model *domain.Model, userId mysql.ID) *UserLike {
	return &UserLike{
		Model:  model,
		UserId: userId,
	}
}

/*func GetUserLike(model *domain.Model, userId mysql.ID, likeUserId mysql.ID) (*UserLike, error) {
	var userLike UserLike
	err := model.Db.Where(&UserLike{
		UserId:     userId,
		LikeUserId: likeUserId,
	}).First(&userLike).Error
	if err == nil {
		return &userLike, nil
	} else if err == gorm.ErrRecordNotFound {
		return nil, nil
	} else {
		return nil, myerr.WrapErr(err)
	}
}*/

//喜欢
func (userLike *UserLike) like(likeUserId mysql.ID, sceneType user_e.UserLikeSceneType) (*UserLike, mysql.ID, error) {
	err := userLike.Db.Where(&UserLike{
		UserId:     userLike.UserId,
		LikeUserId: likeUserId,
	}).First(userLike).Error
	//已经喜欢
	if err == nil {
		return nil, 0, myerr.NewWaring("已经标记喜欢")
	} else if err == gorm.ErrRecordNotFound {
		userLikeOperateId, err := AddUserLikeOperate(userLike.Model, userLike.UserId, likeUserId, user_e.LikeAdd, sceneType)
		if err != nil {
			return nil, 0, err
		}
		userLike.LikeUserId = likeUserId
		userLike.SceneType = sceneType
		return userLike, userLikeOperateId, nil
	} else {
		return nil, 0, myerr.WrapErr(err)
	}
}

//取消喜欢
func (userLike *UserLike) likeCancel(likeUserId mysql.ID) (*UserLike, error) {
	err := userLike.Db.Where(&UserLike{
		UserId:     userLike.UserId,
		LikeUserId: likeUserId,
	}).First(userLike).Error
	//
	if err == nil {
		if _, err := AddUserLikeOperate(userLike.Model, userLike.UserId, likeUserId, user_e.LikeCancel, userLike.SceneType); err != nil {
			return nil, err
		}
		userLike.SetDel()
		return userLike, nil
	} else if err == gorm.ErrRecordNotFound {
		return nil, myerr.NewWaring("没有喜欢的记录")
	} else {
		return nil, myerr.WrapErr(err)
	}
}