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

hdl_comment.go 10 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. package friend_circle
  2. import (
  3. "applet/app/e"
  4. "applet/app/md/friend_circles"
  5. "applet/app/svc"
  6. svc2 "applet/app/svc/friend_circle"
  7. "applet/app/utils/cache"
  8. "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
  9. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  10. svc3 "code.fnuoos.com/EggPlanet/egg_system_rules.git/svc"
  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. "fmt"
  16. "github.com/gin-gonic/gin"
  17. "github.com/olivere/elastic/v7"
  18. "time"
  19. )
  20. // IsCanComment
  21. // @Summary 朋友圈-是否可以评论
  22. // @Tags 朋友圈
  23. // @Description 是否可以评论
  24. // @Accept json
  25. // @Produce json
  26. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  27. // @Success 200 {string} "success"
  28. // @Failure 400 {object} md.Response "具体错误"
  29. // @Router /api/v1/circleFriends/isCanComment [Get]
  30. func IsCanComment(c *gin.Context) {
  31. isCan, err := svc2.IsCanComment(c)
  32. if err != nil {
  33. fmt.Println("IsCanComment:::::", err.Error())
  34. }
  35. resp := friend_circles.IsCanCommentResp{IsCan: isCan}
  36. e.OutSuc(c, resp, nil)
  37. }
  38. // Comment
  39. // @Summary 朋友圈-评论
  40. // @Tags 朋友圈
  41. // @Description 是否可以评论
  42. // @Accept json
  43. // @Produce json
  44. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  45. // @Success 200 {string} "许可链接"
  46. // @Failure 400 {object} md.Response "具体错误"
  47. // @Router /api/v1/circleFriends/comment [POST]
  48. func Comment(c *gin.Context) {
  49. var req friend_circles.CommentReq
  50. if err1 := c.ShouldBindJSON(&req); err1 != nil {
  51. e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
  52. return
  53. }
  54. isCan, err := svc2.IsCanComment(c)
  55. if err != nil {
  56. e.OutErr(c, e.ERR, err.Error())
  57. return
  58. }
  59. if !isCan {
  60. e.OutErr(c, e.ERR, "当前不允许评论!")
  61. return
  62. }
  63. //查找朋友圈记录
  64. doc, err := es.FirstDoc(md.EggFriendCircleEsIndex, req.CircleIndexId)
  65. if err != nil {
  66. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  67. return
  68. }
  69. if !doc.Found { // 表示没找到数据
  70. e.OutErr(c, e.ERR_NOT_FAN, "朋友圈文档记录不存在")
  71. return
  72. }
  73. var circle md.EggFriendCircleEs
  74. err = json.Unmarshal(doc.Source, &circle)
  75. if err != nil {
  76. e.OutErr(c, e.ERR, err.Error())
  77. return
  78. }
  79. //查找评论记录
  80. var comment md.EggFriendCircleCommentEs
  81. commentIndex := svc3.GetEggFriendCircleCommentEsIndex(circle.Uid)
  82. if req.CommentIndexId != "" {
  83. doc1, err1 := es.FirstDoc(commentIndex, req.CommentIndexId)
  84. if err1 != nil {
  85. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  86. return
  87. }
  88. if !doc1.Found { // 表示没找到数据
  89. e.OutErr(c, e.ERR_NOT_FAN, "评论文档记录不存在")
  90. return
  91. }
  92. err = json.Unmarshal(doc1.Source, &comment)
  93. if err != nil {
  94. e.OutErr(c, e.ERR, err.Error())
  95. return
  96. }
  97. }
  98. //新增es记录
  99. user := svc.GetUser(c)
  100. now := time.Now()
  101. imUser, err1 := svc.GetImUser(0, user.Phone)
  102. if err1 != nil {
  103. e.OutErr(c, e.ERR, err1.Error())
  104. return
  105. }
  106. var commentId, replyCommentId string
  107. var commentImUid, replyCommentImUid int64
  108. if req.CommentIndexId != "" {
  109. if comment.CommentId != "" {
  110. // 为二级评论赋值
  111. commentId = comment.CommentId
  112. commentImUid = comment.CommentImUid
  113. replyCommentId = req.CommentIndexId
  114. replyCommentImUid = comment.ImUid
  115. } else {
  116. // 不存在二级评论 就将本条当做二级评论
  117. commentId = req.CommentIndexId
  118. commentImUid = comment.ImUid
  119. }
  120. }
  121. createDocRet, err := es.CreateDoc(svc3.GetEggFriendCircleCommentEsIndex(user.Id), svc3.GetEggFriendCircleCommentEsIndexId(user.Id, req.CircleIndexId), md.EggFriendCircleCommentEs{
  122. Uid: user.Id,
  123. ImUid: imUser.UserId,
  124. Kind: 1,
  125. CircleId: req.CircleIndexId,
  126. CommentId: commentId,
  127. CommentImUid: commentImUid,
  128. ReplyCommentId: replyCommentId,
  129. ReplyCommentImUid: replyCommentImUid,
  130. Content: req.Content,
  131. LikesNums: 0,
  132. CommentNums: 0,
  133. State: 1,
  134. IsPraise: 2,
  135. CreatedAt: now.Format("2006-01-02 15:04:05"),
  136. UpdatedAt: now.Format("2006-01-02 15:04:05"),
  137. })
  138. fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet)
  139. if err != nil {
  140. e.OutErr(c, e.ERR, err.Error())
  141. return
  142. }
  143. //更新朋友圈&&评论记录
  144. _, err = es.UpdateDoc(md.EggFriendCircleEsIndex, req.CircleIndexId, map[string]interface{}{
  145. "comment_nums": circle.CommentNums + 1,
  146. })
  147. if err != nil {
  148. e.OutErr(c, e.ERR, err.Error())
  149. return
  150. }
  151. if req.CommentIndexId != "" {
  152. _, err = es.UpdateDoc(commentIndex, req.CommentIndexId, map[string]interface{}{
  153. "comment_nums": comment.CommentNums + 1,
  154. })
  155. if err != nil {
  156. e.OutErr(c, e.ERR, err.Error())
  157. return
  158. }
  159. }
  160. // 推到 es 更新评论次数
  161. ch, err := rabbit.Cfg.Pool.GetChannel()
  162. if err != nil {
  163. return
  164. }
  165. defer ch.Release()
  166. ch.Publish(md2.EggAppExchange, md2.EggCommentData{Uid: user.Id}, md2.EggRoutKeyForComment)
  167. e.OutSuc(c, "success", nil)
  168. }
  169. // CommentDelete
  170. // @Summary 朋友圈-删除评论
  171. // @Tags 朋友圈
  172. // @Description 评论点赞
  173. // @Accept json
  174. // @Produce json
  175. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  176. // @Success 200 {string} "success"
  177. // @Failure 400 {object} md.Response "具体错误"
  178. // @Router /api/v1/circleFriends/CommentDelete/{$comment_index_id} [DELETE]
  179. func CommentDelete(c *gin.Context) {
  180. commentIndexId := c.Param("comment_index_id")
  181. //查找评论记录
  182. boolQuery := elastic.NewBoolQuery()
  183. boolQuery.Must(elastic.NewTermQuery("_id", commentIndexId))
  184. searchResult, err := es.EsClient.Search().
  185. Index(md.EggFriendCircleCommentEsAlias).
  186. Query(boolQuery).
  187. Size(1).
  188. Pretty(true).
  189. Do(context.Background())
  190. if err != nil {
  191. e.OutErr(c, e.ERR, err.Error())
  192. return
  193. }
  194. var doc1 md.EggFriendCircleCommentEs
  195. // 检查是否有结果
  196. var index string
  197. if searchResult.Hits.TotalHits.Value != 0 {
  198. index = searchResult.Hits.Hits[0].Index
  199. err = json.Unmarshal(searchResult.Hits.Hits[0].Source, &doc1)
  200. if err != nil {
  201. e.OutErr(c, e.ERR, err.Error())
  202. return
  203. }
  204. } else {
  205. e.OutErr(c, e.ERR_NOT_FAN, "评论文档记录不存在")
  206. return
  207. }
  208. //1、删除es数据
  209. _, err = es.DeleteDoc(index, commentIndexId)
  210. if err != nil {
  211. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  212. return
  213. }
  214. //2、删除redis数据
  215. key := fmt.Sprintf(md.CommentLikeCacheKey, commentIndexId)
  216. cache.Del(key)
  217. e.OutSuc(c, "success", nil)
  218. }
  219. // CommentLike
  220. // @Summary 朋友圈-评论点赞
  221. // @Tags 朋友圈
  222. // @Description 评论点赞
  223. // @Accept json
  224. // @Produce json
  225. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  226. // @Param comment_index_id query string true "评论文档记录"
  227. // @Success 200 {string} "success"
  228. // @Failure 400 {object} md.Response "具体错误"
  229. // @Router /api/v1/circleFriends/commentLike [Get]
  230. func CommentLike(c *gin.Context) {
  231. commentIndexId := c.DefaultQuery("comment_index_id", "")
  232. boolQuery := elastic.NewBoolQuery()
  233. boolQuery.Must(elastic.NewTermQuery("_id", commentIndexId))
  234. searchResult, err := es.EsClient.Search().
  235. Index(md.EggFriendCircleCommentEsAlias).
  236. Query(boolQuery).
  237. Size(1).
  238. Pretty(true).
  239. Do(context.Background())
  240. if err != nil {
  241. e.OutErr(c, e.ERR, err.Error())
  242. return
  243. }
  244. var comment md.EggFriendCircleCommentEs
  245. var index string
  246. // 检查是否有结果
  247. if searchResult.Hits.TotalHits.Value != 0 {
  248. index = searchResult.Hits.Hits[0].Index
  249. err = json.Unmarshal(searchResult.Hits.Hits[0].Source, &comment)
  250. if err != nil {
  251. e.OutErr(c, e.ERR, err.Error())
  252. return
  253. }
  254. } else {
  255. e.OutErr(c, e.ERR_NOT_FAN, "评论文档记录不存在")
  256. return
  257. }
  258. //1、判断是否点赞
  259. user := svc.GetUser(c)
  260. isLike, err := svc2.GetUserWithCommentLike(commentIndexId, user.Id)
  261. if err != nil {
  262. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  263. return
  264. }
  265. if isLike {
  266. e.OutErr(c, e.ERR, "重复点赞!")
  267. return
  268. }
  269. //2、修改es数据
  270. _, err = es.UpdateDoc(index, commentIndexId, map[string]interface{}{
  271. "likes_nums": comment.CommentNums + 1,
  272. })
  273. if err != nil {
  274. e.OutErr(c, e.ERR, err.Error())
  275. return
  276. }
  277. //2、进行点赞
  278. key := fmt.Sprintf(md.CommentLikeCacheKey, commentIndexId)
  279. _, err = cache.SetBit(key, user.Id, 1)
  280. if err != nil {
  281. e.OutErr(c, e.ERR, err.Error())
  282. return
  283. }
  284. //3、设置过期时间
  285. _, err = cache.Expire(key, md.CommentLikeCacheTime)
  286. if err != nil {
  287. e.OutErr(c, e.ERR, err.Error())
  288. return
  289. }
  290. e.OutSuc(c, "success", nil)
  291. }
  292. // CommentCancelLike
  293. // @Summary 朋友圈-评论取消点赞
  294. // @Tags 朋友圈
  295. // @Description 评论取消点赞
  296. // @Accept json
  297. // @Produce json
  298. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  299. // @Param comment_index_id query string true "评论文档记录"
  300. // @Success 200 {string} "success"
  301. // @Failure 400 {object} md.Response "具体错误"
  302. // @Router /api/v1/circleFriends/commentCancelLike [Get]
  303. func CommentCancelLike(c *gin.Context) {
  304. commentIndexId := c.DefaultQuery("comment_index_id", "")
  305. boolQuery := elastic.NewBoolQuery()
  306. boolQuery.Must(elastic.NewTermQuery("_id", commentIndexId))
  307. searchResult, err := es.EsClient.Search().
  308. Index(md.EggFriendCircleCommentEsAlias).
  309. Query(boolQuery).
  310. Size(1).
  311. Pretty(true).
  312. Do(context.Background())
  313. if err != nil {
  314. e.OutErr(c, e.ERR, err.Error())
  315. return
  316. }
  317. var comment md.EggFriendCircleCommentEs
  318. var index string
  319. // 检查是否有结果
  320. if searchResult.Hits.TotalHits.Value != 0 {
  321. index = searchResult.Hits.Hits[0].Index
  322. err = json.Unmarshal(searchResult.Hits.Hits[0].Source, &comment)
  323. if err != nil {
  324. e.OutErr(c, e.ERR, err.Error())
  325. return
  326. }
  327. } else {
  328. e.OutErr(c, e.ERR_NOT_FAN, "评论文档记录不存在")
  329. return
  330. }
  331. //1、修改es数据
  332. _, err = es.UpdateDoc(index, commentIndexId, map[string]interface{}{
  333. "likes_nums": comment.LikesNums - 1,
  334. })
  335. if err != nil {
  336. e.OutErr(c, e.ERR, err.Error())
  337. return
  338. }
  339. //2、修改redis数据
  340. key := fmt.Sprintf(md.CommentLikeCacheKey, commentIndexId)
  341. user := svc.GetUser(c)
  342. _, err = cache.SetBit(key, user.Id, 0)
  343. if err != nil {
  344. e.OutErr(c, e.ERR, err.Error())
  345. return
  346. }
  347. e.OutSuc(c, "success", nil)
  348. }