12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- package yb_community_question
- import (
- "errors"
- "fmt"
- "hongze/hongze_yb/global"
- "hongze/hongze_yb/models/tables/yb_community_question_like_roast"
- "time"
- )
- // SetLikeOrRoast 用户对问答进行(取消)点赞或(取消)吐槽
- func SetLikeOrRoast(userId uint64, communityQuestionId uint32, opType, enable, sourceAgent int8) (ybCommunityQuestionLikeRoast *yb_community_question_like_roast.YbCommunityQuestionLikeRoast, likeNum, roastNum 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
- //}
- //查询用户对问答的点赞/吐槽状态
- ybCommunityQuestionLikeRoast, err = yb_community_question_like_roast.GetByUserIdAndCommunityQuestionId(userId, communityQuestionId)
- if err != nil {
- errMsg = "查询点赞/吐槽记录出错"
- return
- }
- //如果未点赞则新增点赞记录
- now := time.Now()
- if ybCommunityQuestionLikeRoast.CommunityQuestionLikeRoastID <= 0 {
- ybCommunityQuestionLikeRoast = &yb_community_question_like_roast.YbCommunityQuestionLikeRoast{
- //CommunityQuestionLikeRoastID: 0,
- UserID: userId,
- OpType: opType,
- Enabled: 1,
- CreateTime: now,
- ModifyTime: now,
- CommunityQuestionID: communityQuestionId,
- SourceAgent: sourceAgent,
- }
- err = ybCommunityQuestionLikeRoast.Create()
- if err != nil {
- errMsg = err.Error()
- err = errors.New("新增点赞/吐槽记录出错")
- return
- }
- } else {
- ybCommunityQuestionLikeRoast.OpType = opType
- ybCommunityQuestionLikeRoast.Enabled = enable
- ybCommunityQuestionLikeRoast.ModifyTime = now
- updates := []string{"op_type", "enabled", "modify_time"}
- err = ybCommunityQuestionLikeRoast.Update(updates)
- if err != nil {
- errMsg = "更新点赞记录出错"
- return
- }
- }
- //查询总的点赞数
- likeNum, err = yb_community_question_like_roast.GetLikeNumByCommunityQuestionId(communityQuestionId)
- if err != nil {
- errMsg = err.Error()
- err = errors.New("查询点赞数出错")
- }
- roastNum, err = yb_community_question_like_roast.GetRoastNumByCommunityQuestionId(communityQuestionId)
- if err != nil {
- errMsg = err.Error()
- err = errors.New("查询吐槽数出错")
- }
- return
- }
|