123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- package yb_community_question
- import (
- "errors"
- "fmt"
- "hongze/hongze_yb/global"
- "hongze/hongze_yb/models/response"
- "hongze/hongze_yb/models/tables/yb_community_question_like_tease"
- "time"
- )
- // SetLikeOrTease 用户对问答进行(取消)点赞或(取消)吐槽
- func SetLikeOrTease(userId uint64, communityQuestionId uint32, opType, enable, sourceAgent, source int8) (ybCommunityQuestionLikeTease *yb_community_question_like_tease.YbCommunityQuestionLikeTease, likeNum, teaseNum int64, err error, errMsg string) {
- //user.UserID
- defer func() {
- if err != nil {
- global.LOG.Critical(fmt.Sprintf("SetLike: userId=%d, err:%s, errMsg:%s", userId, err.Error(), errMsg))
- }
- }()
- if sourceAgent == 0 {
- err = errors.New("请输入来源")
- return
- }
- if communityQuestionId <= 0 {
- err = errors.New("请输入问答ID")
- return
- }
- // 判断是否有点赞权限(不需要权限校验,先保留,有需要后续再说)
- //err, errMsg = services.CheckSimpleCompanyProduct(user)
- //if err != nil {
- // return
- //}
- //查询用户对问答的点赞/吐槽状态
- ybCommunityQuestionLikeTease, err = yb_community_question_like_tease.GetByUserIdAndCommunityQuestionId(userId, communityQuestionId, source)
- if err != nil {
- errMsg = "查询点赞/吐槽记录出错"
- return
- }
- //如果未点赞则新增点赞记录
- now := time.Now()
- if ybCommunityQuestionLikeTease.CommunityQuestionLikeTeaseID <= 0 {
- ybCommunityQuestionLikeTease = &yb_community_question_like_tease.YbCommunityQuestionLikeTease{
- //CommunityQuestionLikeTeaseID: 0,
- UserID: userId,
- OpType: opType,
- Enabled: 1,
- CreateTime: now,
- ModifyTime: now,
- CommunityQuestionID: communityQuestionId,
- SourceAgent: sourceAgent,
- Source: source,
- }
- err = ybCommunityQuestionLikeTease.Create()
- if err != nil {
- errMsg = err.Error()
- err = errors.New("新增点赞/吐槽记录出错")
- return
- }
- } else {
- ybCommunityQuestionLikeTease.OpType = opType
- ybCommunityQuestionLikeTease.Enabled = enable
- ybCommunityQuestionLikeTease.SourceAgent = sourceAgent
- ybCommunityQuestionLikeTease.ModifyTime = now
- updates := []string{"op_type", "enabled", "source_agent", "modify_time"}
- err = ybCommunityQuestionLikeTease.Update(updates)
- if err != nil {
- errMsg = "更新点赞记录出错"
- return
- }
- }
- //查询总的点赞数
- likeNum, err = yb_community_question_like_tease.GetLikeNumByCommunityQuestionId(communityQuestionId, int(source))
- if err != nil {
- errMsg = err.Error()
- err = errors.New("查询点赞数出错")
- }
- teaseNum, err = yb_community_question_like_tease.GetTeaseNumByCommunityQuestionId(communityQuestionId, int(source))
- if err != nil {
- errMsg = err.Error()
- err = errors.New("查询吐槽数出错")
- }
- return
- }
- // HandleLikeOrTeaseByCommunityQuestionItemList 问答 点赞/吐槽 数据
- func HandleLikeOrTeaseByCommunityQuestionItemList(userId uint64, questionList []*response.CommunityQuestionItem) (err error) {
- listLen := len(questionList)
- if listLen == 0 {
- return
- }
- idArr := make([]uint32, 0)
- for i := 0; i < listLen; i++ {
- idArr = append(idArr, uint32(questionList[i].CommunityQuestionID))
- }
- // 获取点赞和吐槽数据
- ybCommunityQuestionLikeTeaseMap := make(map[uint32]*yb_community_question_like_tease.YbCommunityQuestionLikeTease)
- ybCommunityQuestionLikeTeaseList, err := yb_community_question_like_tease.GetByUserIdAndCommunityQuestionIds(userId, idArr, yb_community_question_like_tease.SourceQuestion)
- if err != nil {
- return
- }
- for _, v := range ybCommunityQuestionLikeTeaseList {
- ybCommunityQuestionLikeTeaseMap[v.CommunityQuestionID] = v
- }
- // 获取点赞和吐槽汇总数
- likeMap := make(map[uint32]int)
- teaseMap := make(map[uint32]int)
- likeList, err := yb_community_question_like_tease.GetLikeNumCommentByCommunityQuestionIds(idArr, yb_community_question_like_tease.SourceQuestion)
- if err != nil {
- return
- }
- for _, v := range likeList {
- likeMap[v.CommunityQuestionID] = v.Total
- }
- teaseList, err := yb_community_question_like_tease.GetTeaseNumCommentByCommunityQuestionIds(idArr, yb_community_question_like_tease.SourceQuestion)
- if err != nil {
- return
- }
- for _, v := range teaseList {
- teaseMap[v.CommunityQuestionID] = v.Total
- }
- for _, v := range questionList {
- if tmpTotal, ok := likeMap[uint32(v.CommunityQuestionID)]; ok {
- v.LikeTotal = tmpTotal
- }
- if tmpTotal, ok := teaseMap[uint32(v.CommunityQuestionID)]; ok {
- v.TeaseTotal = tmpTotal
- }
- if ybCommunityQuestionLikeTease, ok := ybCommunityQuestionLikeTeaseMap[uint32(v.CommunityQuestionID)]; ok {
- //类型. 1-点赞 2-吐槽
- v.OpType = ybCommunityQuestionLikeTease.OpType
- }
- }
- return
- }
|