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

336 lines
8.9 KiB

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