From 086c43b7bf7c6b6166fca33c49016db816fda6f1 Mon Sep 17 00:00:00 2001 From: shenjiachi Date: Wed, 4 Dec 2024 17:28:39 +0800 Subject: [PATCH] update --- app/hdl/friend_circle/hdl_friend_circle.go | 201 +++++++++++++++++++++ app/md/friend_circles/md_friend_circle.go | 16 ++ app/svc/friend_circle/svc_firend_circle.go | 52 ++++++ go.mod | 2 +- 4 files changed, 270 insertions(+), 1 deletion(-) diff --git a/app/hdl/friend_circle/hdl_friend_circle.go b/app/hdl/friend_circle/hdl_friend_circle.go index 40225d7..6f481d0 100644 --- a/app/hdl/friend_circle/hdl_friend_circle.go +++ b/app/hdl/friend_circle/hdl_friend_circle.go @@ -7,9 +7,13 @@ import ( svc2 "applet/app/svc/friend_circle" "applet/app/utils" "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" + "errors" "fmt" "github.com/gin-gonic/gin" + "github.com/olivere/elastic/v7" "strconv" "time" ) @@ -185,16 +189,213 @@ func CommentDetail(c *gin.Context) { e.OutSuc(c, resp, nil) } +// Delete +// @Summary 朋友圈-删除朋友圈 +// @Tags 朋友圈 +// @Description 删除朋友圈 +// @Accept json +// @Produce json +// @param Authorization header string true "验证参数Bearer和token空格拼接" +// @Param req body friend_circles.EggFriendCircleDelReq true "请求参数" +// @Success 200 {string} "success" +// @Failure 400 {object} md.Response "具体错误" +// @Router /api/v1/circleFriends/delete [DELETE] func Delete(c *gin.Context) { + var req friend_circles.EggFriendCircleDelReq + err := c.ShouldBindJSON(&req) + if err != nil { + err = svc.HandleValidateErr(err) + err1 := err.(e.E) + e.OutErr(c, err1.Code, err1.Error()) + return + } + + user := svc.GetUser(c) + aliasName := md.EggFriendCircleEsAlias + // 1.判断是否是自己文章 + 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 + } + if doc.Uid != utils.Int64ToStr(user.Id) { + e.OutErr(c, e.ERR, errors.New("不允许删除别人的朋友圈")) + return + } + // 2.删除文章 + deleteDocRet, err := es.DeleteDoc(aliasName, req.CircleIndexId) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + fmt.Printf("CreateDoc ==> %+v \n\n", deleteDocRet) + + // 3.将评论id推入mq + err = svc2.DelFriendCircleComment(req.CircleIndexId) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } e.OutSuc(c, "success", nil) } + +// Share +// @Summary 朋友圈-分享后朋友圈分享数(增加) +// @Tags 朋友圈 +// @Description 分享后朋友圈分享数(增加) +// @Accept json +// @Produce json +// @param Authorization header string true "验证参数Bearer和token空格拼接" +// @Param req body friend_circles.EggFriendCircleShareReq true "请求参数" +// @Success 200 {string} "success" +// @Failure 400 {object} md.Response "具体错误" +// @Router /api/v1/circleFriends/share [POST] func Share(c *gin.Context) { + var req friend_circles.EggFriendCircleShareReq + err := c.ShouldBindJSON(&req) + if err != nil { + err = svc.HandleValidateErr(err) + err1 := err.(e.E) + e.OutErr(c, err1.Code, err1.Error()) + return + } + + script := elastic.NewScript("ctx._source.share_nums += params.inc").Param("inc", 1) + aliasName := md.EggFriendCircleEsAlias + _, err = es.EsClient.Update(). + Index(aliasName). + Id(req.CircleIndexId). + Script(script). + Do(context.Background()) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } e.OutSuc(c, "success", nil) } + +// Like +// @Summary 朋友圈-点赞朋友圈 +// @Tags 朋友圈 +// @Description 点赞朋友圈 +// @Accept json +// @Produce json +// @param Authorization header string true "验证参数Bearer和token空格拼接" +// @Param req body friend_circles.EggFriendCircleLikeReq true "请求参数" +// @Success 200 {string} "success" +// @Failure 400 {object} md.Response "具体错误" +// @Router /api/v1/circleFriends/like [POST] func Like(c *gin.Context) { + var req friend_circles.EggFriendCircleLikeReq + err := c.ShouldBindJSON(&req) + if err != nil { + err = svc.HandleValidateErr(err) + err1 := err.(e.E) + e.OutErr(c, err1.Code, err1.Error()) + return + } + user := svc.GetUser(c) + + likeEsIndex := svc3.GetEggFriendCircleLikeEsIndex(user.Id) + likeId := svc3.GetEggFriendCircleLikeEsIndexId(user.Id, req.CircleIndexId) + // 判断是否已经点赞过 + query := elastic.NewBoolQuery(). + Must( + elastic.NewTermQuery("_id", likeId), + ) + searchResult, err := es.EsClient.Search().Index(likeEsIndex). + Query(query). + Size(0). // 只关心总数,不实际返回文档 + Do(context.Background()) + if err != nil { + return + } + total := searchResult.TotalHits() + if int(total) > 0 { + e.OutSuc(c, "success", nil) + return + } + + // 新增点赞信息 + now := time.Now() + m := md.EggFriendCircleLikeEs{ + Uid: user.Id, + ImUid: 0, + CircleId: req.CircleIndexId, + CreatedAt: now.Format("2006-01-02 15:04:05"), + UpdatedAt: now.Format("2006-01-02 15:04:05"), + } + createDocRet, err := es.CreateDoc(likeEsIndex, likeId, &m) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet) + + // 增加文章点赞数 + script := elastic.NewScript("ctx._source.likes_nums += params.inc").Param("inc", 1) + aliasName := md.EggFriendCircleEsAlias + _, err = es.EsClient.Update(). + Index(aliasName). + Id(req.CircleIndexId). + Script(script). + Do(context.Background()) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } e.OutSuc(c, "success", nil) } + +// CancelLike +// @Summary 朋友圈-取消点赞朋友圈 +// @Tags 朋友圈 +// @Description 取消点赞朋友圈 +// @Accept json +// @Produce json +// @param Authorization header string true "验证参数Bearer和token空格拼接" +// @Param req body friend_circles.EggFriendCircleCancelLikeReq true "请求参数" +// @Success 200 {string} "success" +// @Failure 400 {object} md.Response "具体错误" +// @Router /api/v1/circleFriends/cancelLike [POST] func CancelLike(c *gin.Context) { + // 减少文章点赞数 + var req friend_circles.EggFriendCircleCancelLikeReq + err := c.ShouldBindJSON(&req) + if err != nil { + err = svc.HandleValidateErr(err) + err1 := err.(e.E) + e.OutErr(c, err1.Code, err1.Error()) + return + } + user := svc.GetUser(c) + + // 删除点赞信息 + likeEsIndex := svc3.GetEggFriendCircleLikeEsIndex(user.Id) + likeId := svc3.GetEggFriendCircleLikeEsIndexId(user.Id, req.CircleIndexId) + deleteDocRet, err := es.DeleteDoc(likeEsIndex, likeId) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + fmt.Printf("DeleteDoc ==> %+v \n\n", deleteDocRet) + + // 减少文章点赞数 + script := elastic.NewScript("ctx._source.likes_nums -= params.inc").Param("inc", 1) + aliasName := md.EggFriendCircleEsAlias + _, err = es.EsClient.Update(). + Index(aliasName). + Id(req.CircleIndexId). + Script(script). + Do(context.Background()) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } e.OutSuc(c, "success", nil) } diff --git a/app/md/friend_circles/md_friend_circle.go b/app/md/friend_circles/md_friend_circle.go index aa2cedd..5014364 100644 --- a/app/md/friend_circles/md_friend_circle.go +++ b/app/md/friend_circles/md_friend_circle.go @@ -79,3 +79,19 @@ type EggFriendCircleCommentEsStruct struct { CreatedAt string `json:"created_at"` UpdatedAt string `json:"updated_at"` } + +type EggFriendCircleDelReq struct { + CircleIndexId string `json:"circle_index_id"` // 文章索引 +} + +type EggFriendCircleShareReq struct { + CircleIndexId string `json:"circle_index_id"` // 文章索引 +} + +type EggFriendCircleLikeReq struct { + CircleIndexId string `json:"circle_index_id"` // 文章索引 +} + +type EggFriendCircleCancelLikeReq struct { + CircleIndexId string `json:"circle_index_id"` // 文章索引 +} diff --git a/app/svc/friend_circle/svc_firend_circle.go b/app/svc/friend_circle/svc_firend_circle.go index 3b9862e..1dd19cc 100644 --- a/app/svc/friend_circle/svc_firend_circle.go +++ b/app/svc/friend_circle/svc_firend_circle.go @@ -8,7 +8,9 @@ import ( "applet/app/utils/logx" "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_system_rules.git/md" + md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md" "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es" + "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" "context" "encoding/json" "errors" @@ -223,3 +225,53 @@ func CommentDetail(req friend_circles.CommentDetailReq) (resp friend_circles.Com resp.Total = searchResult.TotalHits() return } + +func DelFriendCircleComment(circleId string) (err error) { + ch, err := rabbit.Cfg.Pool.GetChannel() + if err != nil { + return + } + defer ch.Release() + + page := 1 + limit := 100 + + for { + from := (page - 1) * limit + query := elastic.NewBoolQuery() + query.Must(elastic.NewTermQuery("circle_id", circleId)) + query.Must(elastic.NewTermQuery("state", "1")) + var searchResult *elastic.SearchResult + searchResult, err = es.EsClient.Search(). + Index(md.EggFriendCircleCommentEsAlias). // 替换为你的索引名称 + Query(query). + Sort("create_at", true). + From(from). + Size(limit). + Pretty(true). + Do(context.Background()) + if err != nil { + logx.Fatalf("Error searching for documents: %v", err) + return + } + + // 检查是否有结果 + if searchResult.Hits.TotalHits.Value == 0 { + return + } + + // 推入mq + for _, hit := range searchResult.Hits.Hits { + data := md2.IMEggEnergyStructForDelFriendCircleCommentData{ + CommentIndexId: hit.Id, + } + ch.Publish(md2.IMEggEnergyExchange, data, md2.IMEggEnergyRoutKeyForDelFriendCircleCommentData) + } + + if searchResult.Hits.TotalHits.Value < int64(limit) { + break + } + page++ + } + return +} diff --git a/go.mod b/go.mod index 98922b3..bd859d1 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.19 //replace code.fnuoos.com/EggPlanet/egg_models.git => E:/company/Egg/egg_models -//replace code.fnuoos.com/EggPlanet/egg_system_rules.git => E:/company/Egg/egg_system_rules +replace code.fnuoos.com/EggPlanet/egg_system_rules.git => E:/company/Egg/egg_system_rules require ( github.com/boombuler/barcode v1.0.1