You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
2.8 KiB

package user
import (
"gorm.io/gorm"
"live/app/lib/db"
)
type UserData struct {
db.BaseModel
Id uint `gorm:"column:id" json:"id"`
UserId uint `gorm:"column:user_id" json:"userId"`
Follows uint `gorm:"column:follows" json:"follows"`
Fans uint `gorm:"column:fans" json:"fans"`
Praise uint `gorm:"column:praise" json:"praise"`
}
// 插入
func (u *UserData) Create(userData *UserData) uint {
u.GetDb().Model(u).Create(userData)
if userData.Id > 0 {
return userData.Id
}
return 0
}
// @Title 获取会员统计数据
// @Param userId uint true "会员userId"
func (u *UserData) GetByUserId(userId uint) (result UserData) {
if userId == 0 {
return
}
u.GetDb().Model(u).First(&result, &UserData{UserId: userId})
return
}
// @Title 根据userIds获取统计数据
// @Param userIds uint true "会员userIds"
func (u *UserData) GetByUserIds(userIds []uint) (result []UserData) {
if len(userIds) == 0 {
return
}
u.GetDb().Model(u).Find(&result, "user_id in (?)", userIds)
return
}
// @Title 添加会员关注
func (u *UserData) AddFollow(userId uint) int64 {
if userId == 0 {
return 0
}
rows := u.GetDb().Model(u).Where(UserData{UserId: userId}).Update("follows", gorm.Expr("follows + ?", 1)).RowsAffected
if rows == 0 {
id := u.GetDb().Create(&UserData{
UserId: userId,
Follows: 1,
}).RowsAffected
if id != 0 {
return 1
}
}
return rows
}
// @Title 取消会员关注
func (u *UserData) CancelFollow(userId uint) int64 {
if userId == 0 {
return 0
}
return u.GetDb().Model(u).Where(&UserData{UserId: userId}).Update("follows", gorm.Expr("follows - ?", 1)).RowsAffected
}
// @Title 会员新增粉丝
func (u *UserData) AddFans(userId uint) int64 {
if userId == 0 {
return 0
}
rows := u.GetDb().Model(u).Where(&UserData{UserId: userId}).Update("fans", gorm.Expr("fans + ?", 1)).RowsAffected
if rows == 0 {
id := u.GetDb().Create(&UserData{
UserId: userId,
Fans: 1,
}).RowsAffected
if id != 0 {
return 1
}
}
return rows
}
// @Title 会员取消粉丝
func (u *UserData) CancelFans(userId uint) int64 {
if userId == 0 {
return 0
}
return u.GetDb().Model(u).Where(&UserData{UserId: userId}).Update("fans", gorm.Expr("fans - ?", 1)).RowsAffected
}
// @Title 会员新增赞
func (u *UserData) AddPraise(userId uint) int64 {
if userId == 0 {
return 0
}
rows := u.GetDb().Model(u).Where(&UserData{UserId: userId}).Update("praise", gorm.Expr("praise + ?", 1)).RowsAffected
if rows == 0 {
id := u.GetDb().Create(&UserData{
UserId: userId,
Praise: 1,
}).RowsAffected
if id != 0 {
return 1
}
}
return rows
}
// @Title 会员取消赞
func (u *UserData) CancelPraise(userId uint) int64 {
if userId == 0 {
return 0
}
return u.GetDb().Model(u).Where(&UserData{UserId: userId}).Update("praise", gorm.Expr("praise - ?", 1)).RowsAffected
}