From 0366c0d8bc9272344a3e5b9b1aec2662a4280936 Mon Sep 17 00:00:00 2001 From: shenjiachi Date: Wed, 18 Dec 2024 16:42:39 +0800 Subject: [PATCH] update friend circle --- app/hdl/friend_circle/hdl_comment.go | 115 ++++++++++++------ .../friend_circle/hdl_friend_circle._test.go | 2 +- app/hdl/friend_circle/hdl_friend_circle.go | 90 +++++++++++--- app/md/md_home_page.go | 2 +- app/router/router.go | 2 +- app/svc/friend_circle/svc_comment.go | 3 +- app/svc/friend_circle/svc_firend_circle.go | 24 ++-- go.mod | 2 +- 8 files changed, 178 insertions(+), 62 deletions(-) diff --git a/app/hdl/friend_circle/hdl_comment.go b/app/hdl/friend_circle/hdl_comment.go index 6a615e3..87fed14 100644 --- a/app/hdl/friend_circle/hdl_comment.go +++ b/app/hdl/friend_circle/hdl_comment.go @@ -9,9 +9,11 @@ import ( "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" ) @@ -43,7 +45,7 @@ func IsCanComment(c *gin.Context) { // @param Authorization header string true "验证参数Bearer和token空格拼接" // @Success 200 {string} "许可链接" // @Failure 400 {object} md.Response "具体错误" -// @Router /api/v1/circleFriends/comment [Get] +// @Router /api/v1/circleFriends/comment [POST] func Comment(c *gin.Context) { var req friend_circles.CommentReq if err1 := c.ShouldBindJSON(&req); err1 != nil { @@ -79,8 +81,9 @@ func Comment(c *gin.Context) { //查找评论记录 var comment md.EggFriendCircleCommentEs + commentIndex := svc3.GetEggFriendCircleCommentEsIndex(circle.Uid) if req.CommentIndexId != "" { - doc1, err1 := es.FirstDoc(md.EggFriendCircleCommentEsAlias, req.CircleIndexId) + doc1, err1 := es.FirstDoc(commentIndex, req.CommentIndexId) if err1 != nil { e.OutErr(c, e.ERR_DB_ORM, err1.Error()) return @@ -106,11 +109,15 @@ func Comment(c *gin.Context) { } var commentId, replyCommentId string var commentImUid, replyCommentImUid int64 - if &comment != nil { + if req.CommentIndexId != "" { if comment.CommentId != "" { + // 为二级评论赋值 + commentId = comment.CommentId + commentImUid = comment.CommentImUid replyCommentId = req.CommentIndexId - commentImUid = comment.ImUid + replyCommentImUid = comment.ImUid } else { + // 不存在二级评论 就将本条当做二级评论 commentId = req.CommentIndexId commentImUid = comment.ImUid } @@ -147,8 +154,8 @@ func Comment(c *gin.Context) { e.OutErr(c, e.ERR, err.Error()) return } - if &comment != nil { - _, err = es.UpdateDoc(md.EggFriendCircleCommentEsAlias, req.CommentIndexId, map[string]interface{}{ + if req.CommentIndexId != "" { + _, err = es.UpdateDoc(commentIndex, req.CommentIndexId, map[string]interface{}{ "comment_nums": comment.CommentNums + 1, }) if err != nil { @@ -172,18 +179,36 @@ func Comment(c *gin.Context) { // @Router /api/v1/circleFriends/CommentDelete/{$comment_index_id} [DELETE] func CommentDelete(c *gin.Context) { commentIndexId := c.Param("comment_index_id") - doc1, err := es.FirstDoc(md.EggFriendCircleCommentEsAlias, commentIndexId) + //查找评论记录 + 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_DB_ORM, err.Error()) + e.OutErr(c, e.ERR, err.Error()) return } - if !doc1.Found { // 表示没找到数据 + 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(md.EggFriendCircleCommentEsAlias, commentIndexId) + _, err = es.DeleteDoc(index, commentIndexId) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return @@ -206,22 +231,33 @@ func CommentDelete(c *gin.Context) { // @Param comment_index_id query string true "评论文档记录" // @Success 200 {string} "success" // @Failure 400 {object} md.Response "具体错误" -// @Router /api/v1/circleFriends/CommentLike [Get] +// @Router /api/v1/circleFriends/commentLike [Get] func CommentLike(c *gin.Context) { commentIndexId := c.DefaultQuery("comment_index_id", "") - doc, err := es.FirstDoc(md.EggFriendCircleCommentEsAlias, commentIndexId) + 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_DB_ORM, err.Error()) - return - } - if !doc.Found { // 表示没找到数据 - e.OutErr(c, e.ERR_NOT_FAN, "评论文档记录不存在") + e.OutErr(c, e.ERR, err.Error()) return } var comment md.EggFriendCircleCommentEs - err = json.Unmarshal(doc.Source, &comment) - if err != nil { - e.OutErr(c, e.ERR, err.Error()) + 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 } @@ -238,8 +274,8 @@ func CommentLike(c *gin.Context) { } //2、修改es数据 - _, err = es.UpdateDoc(md.EggFriendCircleCommentEsAlias, commentIndexId, map[string]interface{}{ - "like_nums": comment.CommentNums + 1, + _, err = es.UpdateDoc(index, commentIndexId, map[string]interface{}{ + "likes_nums": comment.CommentNums + 1, }) if err != nil { e.OutErr(c, e.ERR, err.Error()) @@ -274,29 +310,40 @@ func CommentLike(c *gin.Context) { // @Param comment_index_id query string true "评论文档记录" // @Success 200 {string} "success" // @Failure 400 {object} md.Response "具体错误" -// @Router /api/v1/circleFriends/CommentLike [Get] +// @Router /api/v1/circleFriends/commentCancelLike [Get] func CommentCancelLike(c *gin.Context) { commentIndexId := c.DefaultQuery("comment_index_id", "") - doc, err := es.FirstDoc(md.EggFriendCircleCommentEsAlias, commentIndexId) + 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_DB_ORM, err.Error()) - return - } - if !doc.Found { // 表示没找到数据 - e.OutErr(c, e.ERR_NOT_FAN, "评论文档记录不存在") + e.OutErr(c, e.ERR, err.Error()) return } var comment md.EggFriendCircleCommentEs - err = json.Unmarshal(doc.Source, &comment) - if err != nil { - e.OutErr(c, e.ERR, err.Error()) + 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(md.EggFriendCircleCommentEsAlias, commentIndexId, map[string]interface{}{ - "like_nums": comment.CommentNums - 1, + _, err = es.UpdateDoc(index, commentIndexId, map[string]interface{}{ + "likes_nums": comment.LikesNums - 1, }) if err != nil { e.OutErr(c, e.ERR, err.Error()) diff --git a/app/hdl/friend_circle/hdl_friend_circle._test.go b/app/hdl/friend_circle/hdl_friend_circle._test.go index a05d907..483f939 100644 --- a/app/hdl/friend_circle/hdl_friend_circle._test.go +++ b/app/hdl/friend_circle/hdl_friend_circle._test.go @@ -45,7 +45,7 @@ func TestCreateFriendCircleCommentIndex(t *testing.T) { func TestDeleteIndex(t *testing.T) { es.Init("http://123.57.140.192:9200", "elastic", "fnuo123") for i := 0; i < 10; i++ { - index := fmt.Sprintf("%s%d", md.EggFriendCircleLikeEsIndex, i) + index := fmt.Sprintf("%s%d", md.EggFriendCircleCommentEsIndex, i) _, err := es.DeleteIndex(index) if err != nil { fmt.Println(err) diff --git a/app/hdl/friend_circle/hdl_friend_circle.go b/app/hdl/friend_circle/hdl_friend_circle.go index 9649c3e..20d42ab 100644 --- a/app/hdl/friend_circle/hdl_friend_circle.go +++ b/app/hdl/friend_circle/hdl_friend_circle.go @@ -1,6 +1,8 @@ package friend_circle import ( + "applet/app/db" + "applet/app/db/im/model" "applet/app/e" "applet/app/md/friend_circles" "applet/app/svc" @@ -11,6 +13,7 @@ import ( es2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/es" "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es" "context" + "encoding/json" "errors" "fmt" "github.com/gin-gonic/gin" @@ -95,11 +98,19 @@ func Publish(c *gin.Context) { return } - //插入es记录 user := svc.GetUser(c) + var imUser model.User + _, err = db.DbIm.Where("phone_number = ?", user.Phone).Get(&imUser) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + + //插入es记录 now := time.Now() createDocRet, err := es.CreateDoc(md.EggFriendCircleEsIndex, strconv.FormatInt(user.Id, 10)+"_"+utils.Int64ToStr(now.Unix()), md.EggFriendCircleEs{ Uid: user.Id, + ImUid: imUser.Id, Kind: 1, Content: req.Content, Image: utils.SerializeStr(req.ImageList), @@ -120,16 +131,35 @@ func Publish(c *gin.Context) { yearStr := utils.IntToStr(year) weekStr := utils.IntToStr(week) index := es2.GetAppointIndexFromAlias(yearStr, weekStr) - id := fmt.Sprintf("%d%d-%d", year, week, user.Id) - script := elastic.NewScript("ctx._source.send_circle_of_friend_nums += params.inc").Param("inc", 1) - _, err = es.EsClient.Update(). - Index(index). - Id(id). - Script(script). - Do(context.Background()) + id := fmt.Sprintf("%d%d_%d", year, week, user.Id) + _, err = es.FirstDoc(index, id) if err != nil { - e.OutErr(c, e.ERR, err.Error()) - return + if err.Error() != "elastic: Error 404 (Not Found)" { + e.OutErr(c, e.ERR, err.Error()) + return + } else { + m := md.EggEnergyUserEggScoreEs{ + Uid: user.Id, + SendCircleOfFriendNums: 1, + } + newCreateDoc, err := es.CreateDoc(index, id, &m) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + fmt.Printf("newCreateDoc ==> %+v \n\n", newCreateDoc) + } + } else { + script := elastic.NewScript("ctx._source.send_circle_of_friend_nums += params.inc").Param("inc", 1) + _, err = es.EsClient.Update(). + Index(index). + Id(id). + Script(script). + Do(context.Background()) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } } e.OutSuc(c, "success", nil) } @@ -218,12 +248,26 @@ func CommentDetail(c *gin.Context) { } //查找评论记录 - doc1, err1 := es.FirstDoc(md.EggFriendCircleCommentEsAlias, req.CommentIndexId) - if err1 != nil { - e.OutErr(c, e.ERR_DB_ORM, err1.Error()) + boolQuery := elastic.NewBoolQuery() + boolQuery.Must(elastic.NewTermQuery("_id", req.CommentIndexId)) + searchResult, err := es.EsClient.Search(). + Index(md.EggFriendCircleCommentEsAlias). + Query(boolQuery). + Pretty(true). + Do(context.Background()) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) return } - if !doc1.Found { // 表示没找到数据 + var doc md.EggFriendCircleCommentEs + // 检查是否有结果 + if searchResult.Hits.TotalHits.Value != 0 { + err = json.Unmarshal(searchResult.Hits.Hits[0].Source, &doc) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + } else { e.OutErr(c, e.ERR_NOT_FAN, "评论文档记录不存在") return } @@ -233,6 +277,7 @@ func CommentDetail(c *gin.Context) { e.OutErr(c, e.ERR, err.Error()) return } + resp.CircleIndexId = doc.CircleId e.OutSuc(c, resp, nil) } @@ -269,7 +314,9 @@ func Delete(c *gin.Context) { e.OutErr(c, e.ERR_NOT_FAN, "朋友圈文档记录不存在") return } - if doc.Uid != utils.Int64ToStr(user.Id) { + var docInfo md.EggFriendCircleEs + utils.Unserialize(doc.Source, &docInfo) + if docInfo.Uid != user.Id { e.OutErr(c, e.ERR, errors.New("不允许删除别人的朋友圈")) return } @@ -368,11 +415,18 @@ func Like(c *gin.Context) { return } + var imUser model.User + _, err = db.DbIm.Where("phone_number = ?", user.Phone).Get(&imUser) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + // 新增点赞信息 now := time.Now() m := md.EggFriendCircleLikeEs{ Uid: user.Id, - ImUid: 0, + ImUid: imUser.Id, CircleId: req.CircleIndexId, CreatedAt: now.Format("2006-01-02 15:04:05"), UpdatedAt: now.Format("2006-01-02 15:04:05"), @@ -427,6 +481,10 @@ func CancelLike(c *gin.Context) { likeId := svc3.GetEggFriendCircleLikeEsIndexId(user.Id, req.CircleIndexId) deleteDocRet, err := es.DeleteDoc(likeEsIndex, likeId) if err != nil { + if err.Error() == "elastic: Error 404 (Not Found)" { + e.OutErr(c, e.ERR_BAD_REQUEST, errors.New("点赞已取消,请勿重复请求").Error()) + return + } e.OutErr(c, e.ERR, err.Error()) return } diff --git a/app/md/md_home_page.go b/app/md/md_home_page.go index 4ebef31..af825ac 100644 --- a/app/md/md_home_page.go +++ b/app/md/md_home_page.go @@ -9,7 +9,7 @@ type HomePageResp struct { NowBasalRate string `json:"now_basal_rate"` // 当前基础速率/小时 NowTeamRate string `json:"now_team_rate"` // 当前团队速率/小时 NickName string `json:"nick_name"` // 用户名称 - LeaveTimer string `json:"leave_timer"` //可签到剩余时间 秒 + LeaveTimer string `json:"leave_timer"` // 可签到剩余时间 秒 } type HomePageWatchAdRuleResp struct { diff --git a/app/router/router.go b/app/router/router.go index c771fa7..473f49b 100644 --- a/app/router/router.go +++ b/app/router/router.go @@ -227,7 +227,7 @@ func rCircleFriends(r *gin.RouterGroup) { r.DELETE("/commentDelete/:comment_index_id", friend_circle.CommentDelete) // 删除评论 r.GET("/commentLike", friend_circle.CommentLike) // 点赞评论 - r.POST("/commentCancelLike", friend_circle.CommentCancelLike) // 取消点赞评论 + r.GET("/commentCancelLike", friend_circle.CommentCancelLike) // 取消点赞评论 } func rComm(r *gin.RouterGroup) { diff --git a/app/svc/friend_circle/svc_comment.go b/app/svc/friend_circle/svc_comment.go index 5a1244b..9c4596b 100644 --- a/app/svc/friend_circle/svc_comment.go +++ b/app/svc/friend_circle/svc_comment.go @@ -57,9 +57,10 @@ func IsCanComment(c *gin.Context) (isCan bool, err error) { } total := searchResult.TotalHits() if int(total) >= eggFriendCircleBasic.CommentNumsEveryDay { - err = errors.New("今日朋友圈发布数量已达上限~") + err = errors.New("今日评论发布数量已达上限~") return } + isCan = true return } diff --git a/app/svc/friend_circle/svc_firend_circle.go b/app/svc/friend_circle/svc_firend_circle.go index ae401ee..25acd7e 100644 --- a/app/svc/friend_circle/svc_firend_circle.go +++ b/app/svc/friend_circle/svc_firend_circle.go @@ -3,6 +3,7 @@ package svc import ( "applet/app/db" "applet/app/db/im/model" + "applet/app/e" "applet/app/md/friend_circles" "applet/app/svc" "applet/app/utils/logx" @@ -65,6 +66,7 @@ func IsCanPublish(c *gin.Context) (isCan bool, err error) { err = errors.New("今日朋友圈发布数量已达上限~") return } + isCan = true return } @@ -75,12 +77,20 @@ func MySelfList(c *gin.Context, req friend_circles.MySelfListReq) (resp friend_c var friends []*model.Friend err = db.DbIm.Where("`user_id`=?", user.Id).Limit(5000).Find(&friends) if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } var friendIDs []interface{} for _, v := range friends { friendIDs = append(friendIDs, v.FriendId) } + var imUser model.User + _, err = db.DbIm.Where("phone_number = ?", user.Phone).Get(&imUser) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + friendIDs = append(friendIDs, imUser.Id) // 分页参数 from := (req.Page - 1) * req.PageSize @@ -98,7 +108,6 @@ func MySelfList(c *gin.Context, req friend_circles.MySelfListReq) (resp friend_c Pretty(true). Do(context.Background()) if err != nil { - logx.Fatalf("Error searching for documents: %v", err) return } @@ -115,9 +124,10 @@ func MySelfList(c *gin.Context, req friend_circles.MySelfListReq) (resp friend_c return } doc.CircleIndexId = hit.Id - likeId := svc3.GetEggFriendCircleLikeEsIndexId(user.Id, hit.Index) - likeDoc, _ := es.FirstDoc(md.EggFriendCircleLikeEsAlias, likeId) - if likeDoc.Found { // 表示已点赞 + likeEsIndex := svc3.GetEggFriendCircleLikeEsIndex(user.Id) + likeId := svc3.GetEggFriendCircleLikeEsIndexId(user.Id, hit.Id) + likeDoc, _ := es.FirstDoc(likeEsIndex, likeId) + if likeDoc != nil && likeDoc.Found { // 表示已点赞 doc.IsLike = true } resp.List = append(resp.List, doc) @@ -179,9 +189,9 @@ func CommentList(req friend_circles.CommentListReq) (resp friend_circles.Comment searchResult, err := es.EsClient.Search(). Index(md.EggFriendCircleCommentEsAlias). // 替换为你的索引名称 Query(query). - Sort("create_at", false). // 按时间倒排 From(from). Size(req.PageSize). + Sort("created_at", false). // 按时间倒排 Pretty(true). Do(context.Background()) if err != nil { @@ -229,7 +239,7 @@ func CommentDetail(req friend_circles.CommentDetailReq) (resp friend_circles.Com searchResult, err := es.EsClient.Search(). Index(md.EggFriendCircleCommentEsAlias). // 替换为你的索引名称 Query(query). - Sort("create_at", false). // 按时间倒排 + Sort("created_at", false). // 按时间倒排 From(from). Size(req.PageSize). Pretty(true). @@ -293,7 +303,7 @@ func DelFriendCircleComment(circleId string) (err error) { searchResult, err = es.EsClient.Search(). Index(md.EggFriendCircleCommentEsAlias). // 替换为你的索引名称 Query(query). - Sort("create_at", true). + Sort("created_at", true). From(from). Size(limit). Pretty(true). diff --git a/go.mod b/go.mod index 1c599f3..6f1bf39 100644 --- a/go.mod +++ b/go.mod @@ -34,7 +34,7 @@ require ( require ( code.fnuoos.com/EggPlanet/egg_models.git v0.2.1-0.20241217110120-62a0f203ce90 - code.fnuoos.com/EggPlanet/egg_system_rules.git v0.0.4-0.20241217121546-d895a4983b84 + code.fnuoos.com/EggPlanet/egg_system_rules.git v0.0.4-0.20241218083435-34181bb6bcbc code.fnuoos.com/go_rely_warehouse/zyos_go_es.git v1.0.1-0.20241118083738-0f22da9ba0be code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git v0.0.5 github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible