|
- 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"
- "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
- "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("`phone_number`=?", user.Phone).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 md.EggFriendCircleEs
- err = json.Unmarshal(hit.Source, &doc)
- if err != nil {
- return
- }
- 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
- 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 != "" {
-
- }
- resp.List = append(resp.List, doc)
- }
-
- resp.Total = searchResult.TotalHits()
- return
- }
|