蛋蛋星球-客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

278 lines
7.3 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/im/model"
  5. "applet/app/md/friend_circles"
  6. "applet/app/svc"
  7. "applet/app/utils/logx"
  8. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  9. "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
  10. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  11. "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
  12. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  13. "context"
  14. "encoding/json"
  15. "errors"
  16. "github.com/gin-gonic/gin"
  17. "github.com/olivere/elastic/v7"
  18. "time"
  19. )
  20. func IsCanPublish(c *gin.Context) (isCan bool, err error) {
  21. user := svc.GetUser(c)
  22. //1、检测是否已实名
  23. eggFriendCircleBasicDb := implement.NewEggFriendCircleBasicDb(db.Db)
  24. eggFriendCircleBasic, err := eggFriendCircleBasicDb.EggFriendCircleBasicGet()
  25. if err != nil {
  26. return
  27. }
  28. if eggFriendCircleBasic.PublishIsRealName == 1 && user.IsRealName == 0 {
  29. err = errors.New("当前未实名~")
  30. return
  31. }
  32. //2、检测是否在黑名单中
  33. eggFriendCircleUserBlackListDb := implement.NewEggFriendCircleUserBlackListDb(db.Db)
  34. eggFriendCircleUserBlackList, err := eggFriendCircleUserBlackListDb.EggFriendCircleUserBlackListGet(user.Id)
  35. if err != nil {
  36. return
  37. }
  38. if eggFriendCircleUserBlackList != nil {
  39. err = errors.New("已被拉入黑名单~")
  40. return
  41. }
  42. //3、检测是否已达次数
  43. now := time.Now()
  44. startOfDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Format("2006-01-02 15:04:05")
  45. 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")
  46. query := elastic.NewBoolQuery().
  47. Must(
  48. elastic.NewTermQuery("uid", user.Id),
  49. elastic.NewRangeQuery("created_at").Gte(startOfDay).Lt(endOfDay), // 根据你的索引结构调整字段名
  50. )
  51. searchResult, err := es.EsClient.Search().Index(md.EggFriendCircleEsIndex).
  52. Query(query).
  53. Size(0). // 只关心总数,不实际返回文档
  54. Do(context.Background())
  55. if err != nil {
  56. return
  57. }
  58. total := searchResult.TotalHits()
  59. if int(total) >= eggFriendCircleBasic.PublishNumsEveryDay {
  60. err = errors.New("今日朋友圈发布数量已达上限~")
  61. return
  62. }
  63. return
  64. }
  65. func MySelfList(c *gin.Context, req friend_circles.MySelfListReq) (resp friend_circles.MySelfListResp, err error) {
  66. user := svc.GetUser(c)
  67. //获取当前用户在im中的user_id
  68. var friends []*model.Friend
  69. err = db.DbIm.Where("`phone_number`=?", user.Phone).Limit(5000).Find(&friends)
  70. if err != nil {
  71. return
  72. }
  73. var friendIDs []interface{}
  74. for _, v := range friends {
  75. friendIDs = append(friendIDs, v.FriendId)
  76. }
  77. // 分页参数
  78. from := (req.Page - 1) * req.PageSize
  79. // 构建查询
  80. query := elastic.NewBoolQuery()
  81. query.Must(elastic.NewTermsQuery("im_uid", friendIDs...))
  82. query.Must(elastic.NewTermQuery("state", "1"))
  83. searchResult, err := es.EsClient.Search().
  84. Index(md.EggFriendCircleEsIndex). // 替换为你的索引名称
  85. Query(query).
  86. Sort("create_at", false). // 按时间倒排
  87. From(from).
  88. Size(req.PageSize).
  89. Pretty(true).
  90. Do(context.Background())
  91. if err != nil {
  92. logx.Fatalf("Error searching for documents: %v", err)
  93. return
  94. }
  95. // 检查是否有结果
  96. if searchResult.Hits.TotalHits.Value == 0 {
  97. return
  98. }
  99. // 解析结果
  100. for _, hit := range searchResult.Hits.Hits {
  101. var doc friend_circles.EggFriendCircleEsStruct
  102. err = json.Unmarshal(hit.Source, &doc)
  103. if err != nil {
  104. return
  105. }
  106. doc.CircleIndexId = hit.Id
  107. resp.List = append(resp.List, doc)
  108. }
  109. resp.Total = searchResult.TotalHits()
  110. return
  111. }
  112. func CommentList(req friend_circles.CommentListReq) (resp friend_circles.CommentListResp, err error) {
  113. // 分页参数
  114. from := (req.Page - 1) * req.PageSize
  115. // 查询所有评论
  116. query := elastic.NewBoolQuery()
  117. query.Must(elastic.NewTermQuery("circle_id", req.CircleIndexId))
  118. query.Must(elastic.NewTermQuery("state", "1"))
  119. searchResult, err := es.EsClient.Search().
  120. Index(md.EggFriendCircleCommentEsAlias). // 替换为你的索引名称
  121. Query(query).
  122. Sort("create_at", false). // 按时间倒排
  123. From(from).
  124. Size(req.PageSize).
  125. Pretty(true).
  126. Do(context.Background())
  127. if err != nil {
  128. logx.Fatalf("Error searching for documents: %v", err)
  129. return
  130. }
  131. // 检查是否有结果
  132. if searchResult.Hits.TotalHits.Value == 0 {
  133. return
  134. }
  135. // 解析结果
  136. for _, hit := range searchResult.Hits.Hits {
  137. var doc friend_circles.EggFriendCircleCommentEsStruct
  138. err = json.Unmarshal(hit.Source, &doc)
  139. if err != nil {
  140. return
  141. }
  142. user, err1 := svc.GetImUser(doc.ImUid, "")
  143. if err1 != nil {
  144. return friend_circles.CommentListResp{}, err1
  145. }
  146. doc.NickName = user.Nickname
  147. doc.AvatarUrl = user.AvatarUrl
  148. doc.CommentIndexId = hit.Id
  149. resp.List = append(resp.List, doc)
  150. }
  151. resp.Total = searchResult.TotalHits()
  152. return
  153. }
  154. func CommentDetail(req friend_circles.CommentDetailReq) (resp friend_circles.CommentDetailResp, err error) {
  155. // 分页参数
  156. from := (req.Page - 1) * req.PageSize
  157. // 查询所有评论
  158. query := elastic.NewBoolQuery()
  159. query.Must(elastic.NewTermQuery("comment_id", req.CommentIndexId))
  160. query.Must(elastic.NewTermQuery("state", "1"))
  161. searchResult, err := es.EsClient.Search().
  162. Index(md.EggFriendCircleCommentEsAlias). // 替换为你的索引名称
  163. Query(query).
  164. Sort("create_at", false). // 按时间倒排
  165. From(from).
  166. Size(req.PageSize).
  167. Pretty(true).
  168. Do(context.Background())
  169. if err != nil {
  170. logx.Fatalf("Error searching for documents: %v", err)
  171. return
  172. }
  173. // 检查是否有结果
  174. if searchResult.Hits.TotalHits.Value == 0 {
  175. return
  176. }
  177. // 解析结果
  178. for _, hit := range searchResult.Hits.Hits {
  179. var doc friend_circles.EggFriendCircleCommentEsStruct
  180. err = json.Unmarshal(hit.Source, &doc)
  181. if err != nil {
  182. return
  183. }
  184. user, err1 := svc.GetImUser(doc.ImUid, "")
  185. if err1 != nil {
  186. return friend_circles.CommentDetailResp{}, err1
  187. }
  188. doc.NickName = user.Nickname
  189. doc.AvatarUrl = user.AvatarUrl
  190. if doc.ReplyCommentId != "" {
  191. replyUser, err2 := svc.GetImUser(doc.ReplyCommentImUid, "")
  192. if err2 != nil {
  193. return friend_circles.CommentDetailResp{}, err2
  194. }
  195. doc.ReplyCommentUserNickname = replyUser.Nickname
  196. }
  197. doc.CommentIndexId = hit.Id
  198. resp.List = append(resp.List, doc)
  199. }
  200. resp.Total = searchResult.TotalHits()
  201. return
  202. }
  203. func DelFriendCircleComment(circleId string) (err error) {
  204. ch, err := rabbit.Cfg.Pool.GetChannel()
  205. if err != nil {
  206. return
  207. }
  208. defer ch.Release()
  209. page := 1
  210. limit := 100
  211. for {
  212. from := (page - 1) * limit
  213. query := elastic.NewBoolQuery()
  214. query.Must(elastic.NewTermQuery("circle_id", circleId))
  215. query.Must(elastic.NewTermQuery("state", "1"))
  216. var searchResult *elastic.SearchResult
  217. searchResult, err = es.EsClient.Search().
  218. Index(md.EggFriendCircleCommentEsAlias). // 替换为你的索引名称
  219. Query(query).
  220. Sort("create_at", true).
  221. From(from).
  222. Size(limit).
  223. Pretty(true).
  224. Do(context.Background())
  225. if err != nil {
  226. logx.Fatalf("Error searching for documents: %v", err)
  227. return
  228. }
  229. // 检查是否有结果
  230. if searchResult.Hits.TotalHits.Value == 0 {
  231. return
  232. }
  233. // 推入mq
  234. for _, hit := range searchResult.Hits.Hits {
  235. data := md2.IMEggEnergyStructForDelFriendCircleCommentData{
  236. CommentIndexId: hit.Id,
  237. }
  238. ch.Publish(md2.IMEggEnergyExchange, data, md2.IMEggEnergyRoutKeyForDelFriendCircleCommentData)
  239. }
  240. if searchResult.Hits.TotalHits.Value < int64(limit) {
  241. break
  242. }
  243. page++
  244. }
  245. return
  246. }