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.

91 lines
2.7 KiB

package user
import (
"github.com/golangkit/formatime"
"live/app/lib/db"
)
type UserFollow struct {
db.BaseModel
Id uint `gorm:"column:id" json:"id"`
UserId uint `gorm:"column:user_id" json:"userId"`
FollowUserId uint `gorm:"column:follow_user_id" json:"followUserId"`
CreatedAt formatime.Second `gorm:"column:created_at" json:"createdAt"`
UpdatedAt formatime.Second `gorm:"column:updated_at" json:"updatedAt"`
IsDel int `gorm:"column:is_del" json:"isDel"`
}
// 插入
func (u *UserFollow) Create(userFollow *UserFollow) uint {
u.GetDb().Create(userFollow)
if userFollow.Id > 0 {
return userFollow.Id
}
return 0
}
// @Title 通过会员id获取关注列表
// @Param userId uint true "会员id"
func (u *UserFollow) GetListByUserId(userId uint, start, limit int) *[]UserFollow {
if userId <= 0 {
return &[]UserFollow{}
}
userFollow := []UserFollow{}
u.GetDb().Model(u).Where(UserFollow{}).Where("user_id = ? and is_del = ?", userId, 0).Limit(limit).Offset(start).Order("id desc").Find(&userFollow)
return &userFollow
}
// 获取会员关注数量
func (u *UserFollow) GetCountByUserId(userId uint) (result int64) {
if userId <= 0 {
return
}
u.GetDb().Model(u).Where(UserFollow{}).Where("user_id = ? and is_del = ?", userId, 0).Count(&result)
return
}
// 获取会员关注的会员信息
func (u *UserFollow) GetByUserIdAndFollowIds(userId uint, followUserIds []uint) (result []UserFollow) {
if userId == 0 || len(followUserIds) == 0 {
return
}
u.GetDb().Model(u).Find(&result, "user_id = ? and follow_user_id in (?) and is_del = 0", userId, followUserIds)
return
}
// 获取会员关注的单个会员信息
func (u *UserFollow) GetByUserIdAndFollowId(userId uint, followUserId uint) (result UserFollow) {
if userId == 0 || followUserId == 0 {
return
}
u.GetDb().Model(u).First(&result, "user_id = ? and follow_user_id = ?", userId, followUserId)
return
}
// @Title 取消关注
func (u *UserFollow) CancelFollow(userId, followUserId uint) int64 {
if userId == 0 || followUserId == 0 {
return 0
}
return u.GetDb().Model(u).Where(&UserFollow{UserId: userId, FollowUserId: followUserId}).
Where("is_del = ?", 0).Update("is_Del", 1).RowsAffected
}
// @Title 重新关注
func (u *UserFollow) RenewFollow(userId, followUserId uint) int64 {
if userId == 0 || followUserId == 0 {
return 0
}
return u.GetDb().Model(u).Where(&UserFollow{UserId: userId, FollowUserId: followUserId}).
Where("is_del = ?", 1).Update("is_Del", 0).RowsAffected
}
// @Title 获取会员被关注过数据
func (u *UserFollow) GetCountAll(userId uint) (result int64) {
if userId <= 0 {
return
}
u.GetDb().Model(u).Where(UserFollow{}).Where("follow_user_id = ?", userId).Count(&result)
return
}