|
- package friend_circle
-
- import (
- "applet/app/e"
- "applet/app/md/friend_circles"
- "applet/app/svc"
- svc2 "applet/app/svc/friend_circle"
- "applet/app/utils/cache"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
- svc3 "code.fnuoos.com/EggPlanet/egg_system_rules.git/svc"
- "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
- "context"
- "encoding/json"
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/olivere/elastic/v7"
- "time"
- )
-
- // IsCanComment
- // @Summary 朋友圈-是否可以评论
- // @Tags 朋友圈
- // @Description 是否可以评论
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Success 200 {string} "success"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/circleFriends/isCanComment [Get]
- func IsCanComment(c *gin.Context) {
- isCan, err := svc2.IsCanComment(c)
- if err != nil {
- fmt.Println("IsCanComment:::::", err.Error())
- }
- resp := friend_circles.IsCanCommentResp{IsCan: isCan}
- e.OutSuc(c, resp, nil)
- }
-
- // Comment
- // @Summary 朋友圈-评论
- // @Tags 朋友圈
- // @Description 是否可以评论
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Success 200 {string} "许可链接"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/circleFriends/comment [POST]
- func Comment(c *gin.Context) {
- var req friend_circles.CommentReq
- if err1 := c.ShouldBindJSON(&req); err1 != nil {
- e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
- return
- }
- isCan, err := svc2.IsCanComment(c)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- if !isCan {
- e.OutErr(c, e.ERR, "当前不允许评论!")
- return
- }
-
- //查找朋友圈记录
- doc, err := es.FirstDoc(md.EggFriendCircleEsIndex, req.CircleIndexId)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if !doc.Found { // 表示没找到数据
- e.OutErr(c, e.ERR_NOT_FAN, "朋友圈文档记录不存在")
- return
- }
- var circle md.EggFriendCircleEs
- err = json.Unmarshal(doc.Source, &circle)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
-
- //查找评论记录
- var comment md.EggFriendCircleCommentEs
- commentIndex := svc3.GetEggFriendCircleCommentEsIndex(circle.Uid)
- if req.CommentIndexId != "" {
- doc1, err1 := es.FirstDoc(commentIndex, req.CommentIndexId)
- if err1 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err1.Error())
- return
- }
- if !doc1.Found { // 表示没找到数据
- e.OutErr(c, e.ERR_NOT_FAN, "评论文档记录不存在")
- return
- }
- err = json.Unmarshal(doc1.Source, &comment)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- }
-
- //新增es记录
- user := svc.GetUser(c)
- now := time.Now()
- imUser, err1 := svc.GetImUser(0, user.Phone)
- if err1 != nil {
- e.OutErr(c, e.ERR, err1.Error())
- return
- }
- var commentId, replyCommentId string
- var commentImUid, replyCommentImUid int64
- if req.CommentIndexId != "" {
- if comment.CommentId != "" {
- // 为二级评论赋值
- commentId = comment.CommentId
- commentImUid = comment.CommentImUid
- replyCommentId = req.CommentIndexId
- replyCommentImUid = comment.ImUid
- } else {
- // 不存在二级评论 就将本条当做二级评论
- commentId = req.CommentIndexId
- commentImUid = comment.ImUid
- }
- }
- createDocRet, err := es.CreateDoc(svc3.GetEggFriendCircleCommentEsIndex(user.Id), svc3.GetEggFriendCircleCommentEsIndexId(user.Id, req.CircleIndexId), md.EggFriendCircleCommentEs{
- Uid: user.Id,
- ImUid: imUser.UserId,
- Kind: 1,
- CircleId: req.CircleIndexId,
- CommentId: commentId,
- CommentImUid: commentImUid,
- ReplyCommentId: replyCommentId,
- ReplyCommentImUid: replyCommentImUid,
- Content: req.Content,
- LikesNums: 0,
- CommentNums: 0,
- State: 1,
- IsPraise: 2,
- CreatedAt: now.Format("2006-01-02 15:04:05"),
- UpdatedAt: now.Format("2006-01-02 15:04:05"),
- })
-
- fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
-
- //更新朋友圈&&评论记录
- _, err = es.UpdateDoc(md.EggFriendCircleEsIndex, req.CircleIndexId, map[string]interface{}{
- "comment_nums": circle.CommentNums + 1,
- })
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- if req.CommentIndexId != "" {
- _, err = es.UpdateDoc(commentIndex, req.CommentIndexId, map[string]interface{}{
- "comment_nums": comment.CommentNums + 1,
- })
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- }
-
- e.OutSuc(c, "success", nil)
- }
-
- // CommentDelete
- // @Summary 朋友圈-删除评论
- // @Tags 朋友圈
- // @Description 评论点赞
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Success 200 {string} "success"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/circleFriends/CommentDelete/{$comment_index_id} [DELETE]
- func CommentDelete(c *gin.Context) {
- commentIndexId := c.Param("comment_index_id")
- //查找评论记录
- boolQuery := elastic.NewBoolQuery()
- boolQuery.Must(elastic.NewTermQuery("_id", commentIndexId))
- searchResult, err := es.EsClient.Search().
- Index(md.EggFriendCircleCommentEsAlias).
- Query(boolQuery).
- Size(1).
- Pretty(true).
- Do(context.Background())
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- var doc1 md.EggFriendCircleCommentEs
- // 检查是否有结果
- var index string
- if searchResult.Hits.TotalHits.Value != 0 {
- index = searchResult.Hits.Hits[0].Index
- err = json.Unmarshal(searchResult.Hits.Hits[0].Source, &doc1)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- } else {
- e.OutErr(c, e.ERR_NOT_FAN, "评论文档记录不存在")
- return
- }
-
- //1、删除es数据
- _, err = es.DeleteDoc(index, commentIndexId)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- //2、删除redis数据
- key := fmt.Sprintf(md.CommentLikeCacheKey, commentIndexId)
- cache.Del(key)
-
- e.OutSuc(c, "success", nil)
- }
-
- // CommentLike
- // @Summary 朋友圈-评论点赞
- // @Tags 朋友圈
- // @Description 评论点赞
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Param comment_index_id query string true "评论文档记录"
- // @Success 200 {string} "success"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/circleFriends/commentLike [Get]
- func CommentLike(c *gin.Context) {
- commentIndexId := c.DefaultQuery("comment_index_id", "")
- boolQuery := elastic.NewBoolQuery()
- boolQuery.Must(elastic.NewTermQuery("_id", commentIndexId))
- searchResult, err := es.EsClient.Search().
- Index(md.EggFriendCircleCommentEsAlias).
- Query(boolQuery).
- Size(1).
- Pretty(true).
- Do(context.Background())
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- var comment md.EggFriendCircleCommentEs
- var index string
- // 检查是否有结果
- if searchResult.Hits.TotalHits.Value != 0 {
- index = searchResult.Hits.Hits[0].Index
- err = json.Unmarshal(searchResult.Hits.Hits[0].Source, &comment)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- } else {
- e.OutErr(c, e.ERR_NOT_FAN, "评论文档记录不存在")
- return
- }
-
- //1、判断是否点赞
- user := svc.GetUser(c)
- isLike, err := svc2.GetUserWithCommentLike(commentIndexId, user.Id)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if isLike {
- e.OutErr(c, e.ERR, "重复点赞!")
- return
- }
-
- //2、修改es数据
- _, err = es.UpdateDoc(index, commentIndexId, map[string]interface{}{
- "likes_nums": comment.CommentNums + 1,
- })
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
-
- //2、进行点赞
- key := fmt.Sprintf(md.CommentLikeCacheKey, commentIndexId)
- _, err = cache.SetBit(key, user.Id, 1)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
-
- //3、设置过期时间
- _, err = cache.Expire(key, md.CommentLikeCacheTime)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
-
- e.OutSuc(c, "success", nil)
- }
-
- // CommentCancelLike
- // @Summary 朋友圈-评论取消点赞
- // @Tags 朋友圈
- // @Description 评论取消点赞
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Param comment_index_id query string true "评论文档记录"
- // @Success 200 {string} "success"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/circleFriends/commentCancelLike [Get]
- func CommentCancelLike(c *gin.Context) {
- commentIndexId := c.DefaultQuery("comment_index_id", "")
-
- boolQuery := elastic.NewBoolQuery()
- boolQuery.Must(elastic.NewTermQuery("_id", commentIndexId))
- searchResult, err := es.EsClient.Search().
- Index(md.EggFriendCircleCommentEsAlias).
- Query(boolQuery).
- Size(1).
- Pretty(true).
- Do(context.Background())
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- var comment md.EggFriendCircleCommentEs
- var index string
- // 检查是否有结果
- if searchResult.Hits.TotalHits.Value != 0 {
- index = searchResult.Hits.Hits[0].Index
- err = json.Unmarshal(searchResult.Hits.Hits[0].Source, &comment)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- } else {
- e.OutErr(c, e.ERR_NOT_FAN, "评论文档记录不存在")
- return
- }
-
- //1、修改es数据
- _, err = es.UpdateDoc(index, commentIndexId, map[string]interface{}{
- "likes_nums": comment.LikesNums - 1,
- })
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
-
- //2、修改redis数据
- key := fmt.Sprintf(md.CommentLikeCacheKey, commentIndexId)
- user := svc.GetUser(c)
- _, err = cache.SetBit(key, user.Id, 0)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- }
|