CircleArticleRepository.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/5
  6. * Time: 16:03
  7. */
  8. namespace App\Repositories\Circle;
  9. use App\Models\InterestCircleArticle;
  10. use App\Models\Post;
  11. use Illuminate\Database\QueryException;
  12. use Dingo\Api\Http\Response;
  13. use Illuminate\Support\Carbon;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Log;
  16. use Illuminate\Support\Facades\Redis;
  17. class CircleArticleRepository
  18. {
  19. public function __construct(InterestCircleArticle $interestCircleArticle, Post $post)
  20. {
  21. $this->interestCircleArticle = $interestCircleArticle;
  22. $this->post = $post;
  23. }
  24. /**
  25. * 内容列表
  26. */
  27. public function lists($request)
  28. {
  29. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  30. $where = [];
  31. if (isset($request['uid'])) {
  32. $where[] = ['uid', $request['uid']];
  33. }
  34. $articleModel = $this->post;
  35. return $articleModel
  36. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  37. ->join('interest_circle_articles', 'interest_circle_articles.post_id', '=', 'post.id')
  38. ->select('post.*', 'interest_circle_articles.is_recommend', 'interest_circle_articles.circle_id')
  39. ->with('data')
  40. ->where($where)
  41. ->where(function ($query) use ($request) {
  42. if (isset($request['content'])) {
  43. $query->where('title', 'like', "%{$request['content']}%")
  44. ->orWhere('content', 'like', "%{$request['content']}%");
  45. }
  46. })
  47. ->where(function ($query) use ($request) {
  48. if (isset($request['created_at'])) {
  49. $time = explode('_', $request['created_at']);
  50. $query->whereBetween('post.created_at', $time);
  51. }
  52. })
  53. ->orderBy('is_recommend', 'desc')
  54. ->paginate($perPage);
  55. }
  56. /**
  57. * 推荐精品文章
  58. */
  59. public function articleRecommend($request)
  60. {
  61. $article = $this->interestCircleArticle
  62. ->where('post_id', $request['post_id'])
  63. ->where('circle_id', $request['circle_id'])
  64. ->first();
  65. if (!$article) {
  66. return Response::create([
  67. 'message' => '获取精品文章信息失败',
  68. 'status_code' => 500
  69. ]);
  70. }
  71. if ($article->is_recommend == 1) {
  72. $article->is_recommend = 0;
  73. } else {
  74. $article->is_recommend = 1;
  75. }
  76. DB::beginTransaction();
  77. try {
  78. $article->save();
  79. DB::commit();
  80. return Response::create();
  81. } catch (QueryException $exception) {
  82. DB::rollBack();
  83. Log::debug('推荐精品文章:' . $request['post_id'] . '-' . $request['circle_id'] . $exception->getMessage());
  84. return Response::create([
  85. 'message' => '操作失败,请重试',
  86. 'error' => $exception->getMessage(),
  87. 'status_code' => 500
  88. ]);
  89. }
  90. }
  91. /**
  92. * 精品文章从当前圈子移出
  93. */
  94. public function articleRemove($request)
  95. {
  96. $article = $this->interestCircleArticle
  97. ->where('post_id', $request['post_id'])
  98. ->where('circle_id', $request['circle_id'])
  99. ->first();
  100. if (!$article) {
  101. return Response::create([
  102. 'message' => '获取精品文章信息失败',
  103. 'status_code' => 500
  104. ]);
  105. }
  106. DB::beginTransaction();
  107. try {
  108. $article->delete();
  109. $isRef = $this->interestCircleArticle->where('post_id', $request['post_id'])->exists();
  110. if (!$isRef) {
  111. //todo 移出后需要检测该帖子在其他圈子有没有设为精品,如没有,则删除该帖子的精品标识
  112. //$this->post->where('id',$request['post_id'])->update();
  113. }
  114. DB::commit();
  115. return Response::create();
  116. } catch (QueryException $exception) {
  117. DB::rollBack();
  118. Log::debug('移出精品文章:' . $request['post_id'] . '-' . $request['circle_id'] . $exception->getMessage());
  119. return Response::create([
  120. 'message' => '操作失败,请重试',
  121. 'error' => $exception->getMessage(),
  122. 'status_code' => 500
  123. ]);
  124. }
  125. }
  126. }