package user
import (
"github.com/golangkit/formatime"
"gorm.io/gorm"
"live/app/lib/db"
)
type UserTrendComment struct {
db . BaseModel
Id uint ` gorm:"column:id" json:"id" `
UserId uint ` gorm:"column:user_id" json:"userId" `
ParentId uint ` gorm:"column:parent_id" json:"parentId" `
CreatedAt formatime . Second ` gorm:"column:created_at" json:"createdAt" `
UpdatedAt formatime . Second ` gorm:"column:updated_at" json:"updatedAt" `
Content string ` gorm:"column:content" json:"content" `
TrendId uint ` gorm:"column:trend_id" json:"trendId" `
IsDel int ` gorm:"column:is_del" json:"isDel" `
CommentCount uint ` gorm:"comment_count" json:"commentCount" `
Praise uint ` gorm:"praise" json:"praise" `
}
// 插入
func ( c * UserTrendComment ) Create ( userTrendComment * UserTrendComment ) uint {
c . GetDb ( ) . Create ( userTrendComment )
if userTrendComment . Id > 0 {
return userTrendComment . Id
}
return 0
}
// @Title 获取动态评论列表
// @Param userId uint true "会员id"
func ( u * UserTrendComment ) GetListByTrendId ( trendId , parentId uint , start , limit int ) * [ ] UserTrendComment {
if trendId <= 0 {
return & [ ] UserTrendComment { }
}
userTrendComments := [ ] UserTrendComment { }
u . GetDb ( ) . Model ( u ) . Where ( "trend_id = ? and parent_id = ? and is_del = ?" , trendId , parentId , 0 ) . Limit ( limit ) . Offset ( start ) . Order ( "praise desc, id desc" ) . Find ( & userTrendComments )
return & userTrendComments
}
// 获取动态评论数量
func ( u * UserTrendComment ) GetCountByUserId ( trendId , parentId uint ) ( result int64 ) {
if trendId <= 0 {
return
}
u . GetDb ( ) . Model ( u ) . Where ( "trend_id = ? and parent_id = ? and is_del = ?" , trendId , parentId , 0 ) . Count ( & result )
return
}
// @Title 获取动态评论列表
// @Param userId uint true "会员id"
func ( u * UserTrendComment ) GetFirstByParentIds ( parentIds [ ] uint ) * [ ] UserTrendComment {
if len ( parentIds ) == 0 {
return & [ ] UserTrendComment { }
}
userTrendComments := [ ] UserTrendComment { }
u . GetDb ( ) . Model ( u ) . Select ( "id, user_id, parent_id, created_at, updated_at,content,praise" ) . Where ( "parent_id in (?) and is_del = ?" , parentIds , 0 ) . Group ( "parent_id" ) . Find ( & userTrendComments )
return & userTrendComments
}
// @Title 新增了评论
func ( u * UserTrendComment ) AddComment ( id uint ) int64 {
if id == 0 {
return 0
}
return u . GetDb ( ) . Model ( u ) . Where ( & UserTrendComment { Id : id } ) .
Where ( "is_del = ?" , 0 ) . Update ( "comment_count" , gorm . Expr ( "comment_count + ?" , 1 ) ) . RowsAffected
}
// @Title 根据id获取评论信息
func ( u * UserTrendComment ) GetById ( commitId uint ) ( result UserTrendComment ) {
if commitId <= 0 {
return
}
u . GetDb ( ) . Model ( u ) . First ( & result , "id = ?" , commitId )
return
}
// @Title 会员新增赞
func ( u * UserTrendComment ) AddPraise ( commentId uint ) int64 {
if commentId <= 0 {
return 0
}
return u . GetDb ( ) . Model ( u ) . Where ( & UserTrendComment { Id : commentId } ) . Update ( "praise" , gorm . Expr ( "praise + ?" , 1 ) ) . RowsAffected
}
// @Title 会员取消赞
func ( u * UserTrendComment ) CancelPraise ( commentId uint ) int64 {
if commentId == 0 {
return 0
}
return u . GetDb ( ) . Model ( u ) . Where ( & UserTrendComment { Id : commentId } ) . Update ( "praise" , gorm . Expr ( "praise - ?" , 1 ) ) . RowsAffected
}