|
- package svc
-
- import (
- "applet/app/db"
- "applet/app/db/im/model"
- "applet/app/md/friend_circles"
- "applet/app/svc"
- "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"
- "github.com/gin-gonic/gin"
- "github.com/olivere/elastic/v7"
- "time"
- )
-
- func IsCanPublish(c *gin.Context) (isCan bool, err error) {
- user := svc.GetUser(c)
- //1、检测是否已实名
- eggFriendCircleBasicDb := implement.NewEggFriendCircleBasicDb(db.Db)
- eggFriendCircleBasic, err := eggFriendCircleBasicDb.EggFriendCircleBasicGet()
- if err != nil {
- return
- }
- if eggFriendCircleBasic.PublishIsRealName == 1 && user.IsRealName == 0 {
- err = errors.New("当前未实名~")
- return
- }
-
- //2、检测是否在黑名单中
- eggFriendCircleUserBlackListDb := implement.NewEggFriendCircleUserBlackListDb(db.Db)
- eggFriendCircleUserBlackList, err := eggFriendCircleUserBlackListDb.EggFriendCircleUserBlackListGet(user.Id)
- if err != nil {
- return
- }
- if eggFriendCircleUserBlackList != nil {
- err = errors.New("已被拉入黑名单~")
- return
- }
-
- //3、检测是否已达次数
- now := time.Now()
- startOfDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Format("2006-01-02 15:04:05")
- endOfDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Add(24 * time.Hour).Format("2006-01-02 15:04:05")
- query := elastic.NewBoolQuery().
- Must(
- elastic.NewTermQuery("uid", user.Id),
- elastic.NewRangeQuery("created_at").Gte(startOfDay).Lt(endOfDay), // 根据你的索引结构调整字段名
- )
- searchResult, err := es.EsClient.Search().Index(md.EggFriendCircleEsIndex).
- Query(query).
- Size(0). // 只关心总数,不实际返回文档
- Do(context.Background())
- if err != nil {
- return
- }
- total := searchResult.TotalHits()
- if int(total) >= eggFriendCircleBasic.PublishNumsEveryDay {
- err = errors.New("今日朋友圈发布数量已达上限~")
- return
- }
- return
- }
-
- func MySelfList(c *gin.Context, req friend_circles.MySelfListReq) (resp friend_circles.MySelfListResp, err error) {
- user := svc.GetUser(c)
-
- //获取当前用户在im中的user_id
- var friends []*model.Friend
- err = db.DbIm.Where("`user_id`=?", user.Id).Limit(5000).Find(&friends)
- if err != nil {
- return
- }
- var friendIDs []interface{}
- for _, v := range friends {
- friendIDs = append(friendIDs, v.FriendId)
- }
- // 分页参数
- from := (req.Page - 1) * req.PageSize
-
- // 构建查询
- query := elastic.NewBoolQuery()
- query.Must(elastic.NewTermsQuery("im_uid", friendIDs...))
- query.Must(elastic.NewTermQuery("state", "1"))
-
- searchResult, err := es.EsClient.Search().
- Index(md.EggFriendCircleEsIndex). // 替换为你的索引名称
- Query(query).
- Sort("create_at", false). // 按时间倒排
- From(from).
- Size(req.PageSize).
- Pretty(true).
- Do(context.Background())
- if err != nil {
- logx.Fatalf("Error searching for documents: %v", err)
- return
- }
-
- // 检查是否有结果
- if searchResult.Hits.TotalHits.Value == 0 {
- return
- }
-
- // 解析结果
- for _, hit := range searchResult.Hits.Hits {
- var doc friend_circles.EggFriendCircleEsStruct
- err = json.Unmarshal(hit.Source, &doc)
- if err != nil {
- return
- }
- doc.CircleIndexId = hit.Id
- resp.List = append(resp.List, doc)
- }
-
- resp.Total = searchResult.TotalHits()
- return
- }
-
- func GetRecommendList(c *gin.Context, req friend_circles.RecommendListReq) (resp friend_circles.RecommendListResp, err error) {
- // 分页参数
- from := (req.Page - 1) * req.PageSize
-
- // 构建查询
- query := elastic.NewBoolQuery()
- query.Must(elastic.NewTermQuery("state", 1))
- query.Should(elastic.NewTermQuery("is_top_up", 1))
- searchResult, err := es.EsClient.Search().
- Index(md.EggFriendCircleEsAlias). // 替换为你的索引名称
- Query(query).
- //Sort("create_at", false). // 按时间倒排
- From(from).
- Size(req.PageSize).
- Pretty(true).
- Do(context.Background())
- if err != nil {
- return
- }
-
- // 检查是否有结果
- if searchResult.Hits.TotalHits.Value == 0 {
- return
- }
-
- // 解析结果
- for _, hit := range searchResult.Hits.Hits {
- var doc friend_circles.EggFriendCircleEsStruct
- err = json.Unmarshal(hit.Source, &doc)
- if err != nil {
- return
- }
- doc.CircleIndexId = hit.Id
- resp.List = append(resp.List, doc)
- }
-
- resp.Total = searchResult.TotalHits()
- return
- }
-
- func CommentList(req friend_circles.CommentListReq) (resp friend_circles.CommentListResp, err error) {
- // 分页参数
- from := (req.Page - 1) * req.PageSize
-
- // 查询所有评论
- query := elastic.NewBoolQuery()
- query.Must(elastic.NewTermQuery("circle_id", req.CircleIndexId))
- query.Must(elastic.NewTermQuery("state", "1"))
-
- searchResult, err := es.EsClient.Search().
- Index(md.EggFriendCircleCommentEsAlias). // 替换为你的索引名称
- Query(query).
- Sort("create_at", false). // 按时间倒排
- From(from).
- Size(req.PageSize).
- Pretty(true).
- Do(context.Background())
- if err != nil {
- logx.Fatalf("Error searching for documents: %v", err)
- return
- }
-
- // 检查是否有结果
- if searchResult.Hits.TotalHits.Value == 0 {
- return
- }
-
- // 解析结果
- for _, hit := range searchResult.Hits.Hits {
- var doc friend_circles.EggFriendCircleCommentEsStruct
- err = json.Unmarshal(hit.Source, &doc)
- if err != nil {
- return
- }
- user, err1 := svc.GetImUser(doc.ImUid, "")
- if err1 != nil {
- return friend_circles.CommentListResp{}, err1
- }
- doc.NickName = user.Nickname
- doc.AvatarUrl = user.AvatarUrl
- doc.CommentIndexId = hit.Id
- resp.List = append(resp.List, doc)
- }
-
- resp.Total = searchResult.TotalHits()
- return
- }
-
- func CommentDetail(req friend_circles.CommentDetailReq) (resp friend_circles.CommentDetailResp, err error) {
- // 分页参数
- from := (req.Page - 1) * req.PageSize
-
- // 查询所有评论
- query := elastic.NewBoolQuery()
- query.Must(elastic.NewTermQuery("comment_id", req.CommentIndexId))
- query.Must(elastic.NewTermQuery("state", "1"))
-
- searchResult, err := es.EsClient.Search().
- Index(md.EggFriendCircleCommentEsAlias). // 替换为你的索引名称
- Query(query).
- Sort("create_at", false). // 按时间倒排
- From(from).
- Size(req.PageSize).
- Pretty(true).
- Do(context.Background())
- if err != nil {
- logx.Fatalf("Error searching for documents: %v", err)
- return
- }
-
- // 检查是否有结果
- if searchResult.Hits.TotalHits.Value == 0 {
- return
- }
-
- // 解析结果
- for _, hit := range searchResult.Hits.Hits {
- var doc friend_circles.EggFriendCircleCommentEsStruct
- err = json.Unmarshal(hit.Source, &doc)
- if err != nil {
- return
- }
- user, err1 := svc.GetImUser(doc.ImUid, "")
- if err1 != nil {
- return friend_circles.CommentDetailResp{}, err1
- }
-
- doc.NickName = user.Nickname
- doc.AvatarUrl = user.AvatarUrl
-
- if doc.ReplyCommentId != "" {
- replyUser, err2 := svc.GetImUser(doc.ReplyCommentImUid, "")
- if err2 != nil {
- return friend_circles.CommentDetailResp{}, err2
- }
- doc.ReplyCommentUserNickname = replyUser.Nickname
- }
- doc.CommentIndexId = hit.Id
- resp.List = append(resp.List, doc)
- }
-
- 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
- }
|