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

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