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

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