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

363 lines
9.9 KiB

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