蛋蛋星球-客户端
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.
 
 
 
 
 

318 lines
8.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("`user_id`=?", user.Id).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 GetRecommendList(c *gin.Context, req friend_circles.RecommendListReq) (resp friend_circles.RecommendListResp, err error) {
  113. // 分页参数
  114. from := (req.Page - 1) * req.PageSize
  115. // 构建查询
  116. query := elastic.NewBoolQuery()
  117. query.Must(elastic.NewTermQuery("state", 1))
  118. query.Should(elastic.NewTermQuery("is_top_up", 1))
  119. searchResult, err := es.EsClient.Search().
  120. Index(md.EggFriendCircleEsAlias). // 替换为你的索引名称
  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. return
  129. }
  130. // 检查是否有结果
  131. if searchResult.Hits.TotalHits.Value == 0 {
  132. return
  133. }
  134. // 解析结果
  135. for _, hit := range searchResult.Hits.Hits {
  136. var doc friend_circles.EggFriendCircleEsStruct
  137. err = json.Unmarshal(hit.Source, &doc)
  138. if err != nil {
  139. return
  140. }
  141. doc.CircleIndexId = hit.Id
  142. resp.List = append(resp.List, doc)
  143. }
  144. resp.Total = searchResult.TotalHits()
  145. return
  146. }
  147. func CommentList(req friend_circles.CommentListReq) (resp friend_circles.CommentListResp, err error) {
  148. // 分页参数
  149. from := (req.Page - 1) * req.PageSize
  150. // 查询所有评论
  151. query := elastic.NewBoolQuery()
  152. query.Must(elastic.NewTermQuery("circle_id", req.CircleIndexId))
  153. query.Must(elastic.NewTermQuery("state", "1"))
  154. searchResult, err := es.EsClient.Search().
  155. Index(md.EggFriendCircleCommentEsAlias). // 替换为你的索引名称
  156. Query(query).
  157. Sort("create_at", false). // 按时间倒排
  158. From(from).
  159. Size(req.PageSize).
  160. Pretty(true).
  161. Do(context.Background())
  162. if err != nil {
  163. logx.Fatalf("Error searching for documents: %v", err)
  164. return
  165. }
  166. // 检查是否有结果
  167. if searchResult.Hits.TotalHits.Value == 0 {
  168. return
  169. }
  170. // 解析结果
  171. for _, hit := range searchResult.Hits.Hits {
  172. var doc friend_circles.EggFriendCircleCommentEsStruct
  173. err = json.Unmarshal(hit.Source, &doc)
  174. if err != nil {
  175. return
  176. }
  177. user, err1 := svc.GetImUser(doc.ImUid, "")
  178. if err1 != nil {
  179. return friend_circles.CommentListResp{}, err1
  180. }
  181. doc.NickName = user.Nickname
  182. doc.AvatarUrl = user.AvatarUrl
  183. doc.CommentIndexId = hit.Id
  184. resp.List = append(resp.List, doc)
  185. }
  186. resp.Total = searchResult.TotalHits()
  187. return
  188. }
  189. func CommentDetail(req friend_circles.CommentDetailReq) (resp friend_circles.CommentDetailResp, err error) {
  190. // 分页参数
  191. from := (req.Page - 1) * req.PageSize
  192. // 查询所有评论
  193. query := elastic.NewBoolQuery()
  194. query.Must(elastic.NewTermQuery("comment_id", req.CommentIndexId))
  195. query.Must(elastic.NewTermQuery("state", "1"))
  196. searchResult, err := es.EsClient.Search().
  197. Index(md.EggFriendCircleCommentEsAlias). // 替换为你的索引名称
  198. Query(query).
  199. Sort("create_at", false). // 按时间倒排
  200. From(from).
  201. Size(req.PageSize).
  202. Pretty(true).
  203. Do(context.Background())
  204. if err != nil {
  205. logx.Fatalf("Error searching for documents: %v", err)
  206. return
  207. }
  208. // 检查是否有结果
  209. if searchResult.Hits.TotalHits.Value == 0 {
  210. return
  211. }
  212. // 解析结果
  213. for _, hit := range searchResult.Hits.Hits {
  214. var doc friend_circles.EggFriendCircleCommentEsStruct
  215. err = json.Unmarshal(hit.Source, &doc)
  216. if err != nil {
  217. return
  218. }
  219. user, err1 := svc.GetImUser(doc.ImUid, "")
  220. if err1 != nil {
  221. return friend_circles.CommentDetailResp{}, err1
  222. }
  223. doc.NickName = user.Nickname
  224. doc.AvatarUrl = user.AvatarUrl
  225. if doc.ReplyCommentId != "" {
  226. replyUser, err2 := svc.GetImUser(doc.ReplyCommentImUid, "")
  227. if err2 != nil {
  228. return friend_circles.CommentDetailResp{}, err2
  229. }
  230. doc.ReplyCommentUserNickname = replyUser.Nickname
  231. }
  232. doc.CommentIndexId = hit.Id
  233. resp.List = append(resp.List, doc)
  234. }
  235. resp.Total = searchResult.TotalHits()
  236. return
  237. }
  238. func DelFriendCircleComment(circleId string) (err error) {
  239. ch, err := rabbit.Cfg.Pool.GetChannel()
  240. if err != nil {
  241. return
  242. }
  243. defer ch.Release()
  244. page := 1
  245. limit := 100
  246. for {
  247. from := (page - 1) * limit
  248. query := elastic.NewBoolQuery()
  249. query.Must(elastic.NewTermQuery("circle_id", circleId))
  250. query.Must(elastic.NewTermQuery("state", "1"))
  251. var searchResult *elastic.SearchResult
  252. searchResult, err = es.EsClient.Search().
  253. Index(md.EggFriendCircleCommentEsAlias). // 替换为你的索引名称
  254. Query(query).
  255. Sort("create_at", true).
  256. From(from).
  257. Size(limit).
  258. Pretty(true).
  259. Do(context.Background())
  260. if err != nil {
  261. logx.Fatalf("Error searching for documents: %v", err)
  262. return
  263. }
  264. // 检查是否有结果
  265. if searchResult.Hits.TotalHits.Value == 0 {
  266. return
  267. }
  268. // 推入mq
  269. for _, hit := range searchResult.Hits.Hits {
  270. data := md2.IMEggEnergyStructForDelFriendCircleCommentData{
  271. CommentIndexId: hit.Id,
  272. }
  273. ch.Publish(md2.IMEggEnergyExchange, data, md2.IMEggEnergyRoutKeyForDelFriendCircleCommentData)
  274. }
  275. if searchResult.Hits.TotalHits.Value < int64(limit) {
  276. break
  277. }
  278. page++
  279. }
  280. return
  281. }