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.
165 lines
3.8 KiB
165 lines
3.8 KiB
package topic
|
|
|
|
// 会员关注
|
|
import (
|
|
"live/app/lib"
|
|
"live/app/model/topic"
|
|
"live/app/model/user"
|
|
)
|
|
|
|
type Follow struct {
|
|
}
|
|
|
|
type ReplyFollowItem struct {
|
|
topic.Topic
|
|
Substance uint `json:"substance"`
|
|
Partake uint `json:"partake"`
|
|
IsFollow int `json:"isFollow"`
|
|
}
|
|
|
|
// @Title 获取会员关注列表
|
|
// @Param userId uint true "当前登录会员id,未登录传0"
|
|
// @Param findUserId uint true "需要查询关注列表的会员id"
|
|
func (f *Follow) GetList(userId, findUserId uint, page lib.Page) (result *[]ReplyFollowItem, count int64) {
|
|
topicFollow := &topic.TopicFollow{}
|
|
result = &[]ReplyFollowItem{}
|
|
|
|
// 查询会员关注列表
|
|
start := (page.GetPage() - 1) * page.GetLimit()
|
|
count = topicFollow.GetCountByUserId(findUserId)
|
|
if count <= int64(start) {
|
|
// 没有数据,不需要查库
|
|
return
|
|
}
|
|
findFollows := topicFollow.GetListByUserId(findUserId, start, page.GetLimit())
|
|
if len(*findFollows) == 0 {
|
|
// 未查询到数据
|
|
return
|
|
}
|
|
topicIds := []uint{}
|
|
for _, findFollow := range *findFollows {
|
|
topicIds = append(topicIds, findFollow.TopicId)
|
|
}
|
|
|
|
// 获取话题信息
|
|
topicLists := (&Topic{}).GetListByIds(topicIds)
|
|
topicMap := map[uint]topic.Topic{}
|
|
for _, topicList := range *topicLists {
|
|
topicMap[topicList.Id] = topicList
|
|
}
|
|
|
|
// 获取当前用户关注情况
|
|
topicFollowsMap := map[uint]topic.TopicFollow{}
|
|
hasFollow := 1
|
|
if userId != findUserId {
|
|
hasFollow = 0
|
|
userFollows := topicFollow.GetByUserIdAndTopicIds(userId, topicIds)
|
|
for _, userFollow := range userFollows {
|
|
topicFollowsMap[userFollow.TopicId] = userFollow
|
|
}
|
|
}
|
|
|
|
for _, findFollow := range *findFollows {
|
|
// 如果能查询到会员信息
|
|
if topicItem, ok := topicMap[findFollow.TopicId]; ok {
|
|
isFollow := hasFollow
|
|
if isFollow == 0 {
|
|
// 判断是否关注
|
|
if _, ok := topicFollowsMap[findFollow.TopicId]; ok {
|
|
isFollow = 1
|
|
|
|
}
|
|
}
|
|
// 获取统计数据
|
|
*result = append(*result, ReplyFollowItem{
|
|
Topic: topicItem,
|
|
Substance: topicItem.Substance,
|
|
Partake: topicItem.Partake,
|
|
IsFollow: isFollow,
|
|
})
|
|
}
|
|
}
|
|
return result, count
|
|
}
|
|
|
|
// @Title 添加关注
|
|
func (f *Follow) Add(userId, topicId uint) (result bool) {
|
|
topicFollowModel := &topic.TopicFollow{}
|
|
topicFollow := topicFollowModel.GetByUserIdAndTopicId(userId, topicId)
|
|
|
|
tx := topicFollowModel.Begin()
|
|
defer func() {
|
|
if result {
|
|
tx.Commit()
|
|
} else {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
// 已经关注过
|
|
if topicFollow.Id != 0 {
|
|
// 关注过后取消关注
|
|
if topicFollow.IsDel == 1 {
|
|
res := topicFollowModel.RenewFollow(userId, topicId)
|
|
if res == 0 {
|
|
return false
|
|
}
|
|
userDataModel := &user.UserData{}
|
|
userDataModel.SetDb(tx)
|
|
res = userDataModel.AddFollow(userId)
|
|
if res == 0 {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
} else {
|
|
// 未关注添加关注
|
|
fid := topicFollowModel.Create(&topic.TopicFollow{
|
|
UserId: userId,
|
|
TopicId: topicId,
|
|
})
|
|
if fid == 0 {
|
|
return false
|
|
} else {
|
|
userDataModel := &user.UserData{}
|
|
userDataModel.SetDb(tx)
|
|
res := userDataModel.AddFollow(userId)
|
|
if res == 0 {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
|
|
// @Title 取消关注
|
|
func (f *Follow) Cancel(userId, topicId uint) (result bool) {
|
|
topicFollowModel := &topic.TopicFollow{}
|
|
topicFollow := topicFollowModel.GetByUserIdAndTopicId(userId, topicId)
|
|
// 已经关注过
|
|
if topicFollow.Id != 0 {
|
|
// 关注过后取消关注
|
|
if topicFollow.IsDel == 0 {
|
|
tx := topicFollow.Begin()
|
|
defer func() {
|
|
if result {
|
|
tx.Commit()
|
|
} else {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
res := topicFollowModel.CancelFollow(userId, topicId)
|
|
if res == 0 {
|
|
return false
|
|
}
|
|
// 统计数据处理
|
|
userDataModel := &user.UserData{}
|
|
userDataModel.SetDb(tx)
|
|
res = userDataModel.CancelFollow(userId)
|
|
if res == 0 {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
}
|