|
- 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"
- "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"
- svc3 "code.fnuoos.com/EggPlanet/egg_system_rules.git/svc"
- "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
- }
- isCan = true
- return
- }
-
- func GetPersonalSendingList(c *gin.Context, req friend_circles.PersonalSendingListReq) (resp friend_circles.PersonalSendingListResp, err error) {
- user := svc.GetUser(c)
- // 分页参数
- from := (req.Page - 1) * req.PageSize
-
- // 构建查询
- query := elastic.NewBoolQuery()
- query.Must(elastic.NewTermQuery("uid", user.Id))
-
- searchResult, err := es.EsClient.Search().
- Index(md.EggFriendCircleEsIndex). // 替换为你的索引名称
- Query(query).
- Sort("created_at", false). // 按时间倒排
- From(from).
- Size(req.PageSize).
- Pretty(true).
- Do(context.Background())
- if err != nil {
- return
- }
-
- // 检查是否有结果
- if searchResult.Hits.TotalHits.Value == 0 {
- return
- }
- // 解析结果
- scheme, domain := svc.ImageBucket(db.Db)
- 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
- 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
- }
- temp := friend_circles.EggFriendCircleEsStructToRespStruct(doc)
- if doc.Image != "" {
- imageStr := svc.ImageFormatWithBucketForDataInfo(scheme, domain, doc.Image)
- var images []string
- utils.Unserialize([]byte(imageStr), &images)
- temp.Image = images
- }
- if doc.Video != "" {
- video := svc.ImageFormatWithBucketForDataInfo(scheme, domain, doc.Video)
- temp.Video = video
- }
- resp.List = append(resp.List, &temp)
- }
-
- resp.Total = searchResult.TotalHits()
- return
- }
-
- func MySelfList(c *gin.Context, req friend_circles.MySelfListReq) (resp friend_circles.MySelfListResp, err error) {
- 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
- }
- //获取当前用户在im中的user_id
- var friends []*model.Friend
- err = db.DbIm.Where("`user_id`=?", imUser.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)
- }
- // 加入用户自身
- friendIDs = append(friendIDs, imUser.Id)
- // 分页参数
- 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("created_at", false). // 按时间倒排
- From(from).
- Size(req.PageSize).
- Pretty(true).
- Do(context.Background())
- if err != nil {
- return
- }
-
- // 检查是否有结果
- if searchResult.Hits.TotalHits.Value == 0 {
- return
- }
-
- // 解析结果
- scheme, domain := svc.ImageBucket(db.Db)
- 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
- 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
- }
- temp := friend_circles.EggFriendCircleEsStructToRespStruct(doc)
- if doc.Image != "" {
- imageStr := svc.ImageFormatWithBucketForDataInfo(scheme, domain, doc.Image)
- var images []string
- utils.Unserialize([]byte(imageStr), &images)
- temp.Image = images
- }
- if doc.Video != "" {
- video := svc.ImageFormatWithBucketForDataInfo(scheme, domain, doc.Video)
- temp.Video = video
- }
- resp.List = append(resp.List, &temp)
- }
-
- resp.Total = searchResult.TotalHits()
- return
- }
-
- func GetRecommendList(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("created_at", false). // 按时间倒排
- From(from).
- Size(req.PageSize).
- Pretty(true).
- Do(context.Background())
- if err != nil {
- return
- }
-
- // 检查是否有结果
- if searchResult.Hits.TotalHits.Value == 0 {
- return
- }
-
- // 解析结果
- scheme, domain := svc.ImageBucket(db.Db)
- 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
- temp := friend_circles.EggFriendCircleEsStructToRespStruct(doc)
- if doc.Image != "" {
- imageStr := svc.ImageFormatWithBucketForDataInfo(scheme, domain, doc.Image)
- var images []string
- utils.Unserialize([]byte(imageStr), &images)
- temp.Image = images
- }
- if doc.Video != "" {
- video := svc.ImageFormatWithBucketForDataInfo(scheme, domain, doc.Video)
- temp.Video = video
- }
- resp.List = append(resp.List, &temp)
- }
-
- 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).
- From(from).
- Size(req.PageSize).
- Sort("created_at", false). // 按时间倒排
- 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
- isLike, _ := GetUserWithCommentLike(hit.Index, doc.Uid)
- doc.IsLike = isLike
- 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("created_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("created_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
- }
-
- func GetImUserNicknameAndAvatar(imUserIds []int64) (map[int64]friend_circles.ImUserNicknameAndAvatar, error) {
- var imUsers []model.User
- err := db.DbIm.In("id", imUserIds).Find(&imUsers)
- if err != nil {
- return nil, err
- }
- scheme, domain := svc.ImageBucket(db.Db)
- info := make(map[int64]friend_circles.ImUserNicknameAndAvatar, len(imUsers))
- for _, user := range imUsers {
- tempUrl := svc.ImageFormatWithBucket(scheme, domain, user.AvatarUrl)
- temp := friend_circles.ImUserNicknameAndAvatar{
- NickName: user.Nickname,
- Avatar: tempUrl,
- }
- info[user.Id] = temp
- }
- return info, nil
- }
|