CircleArticleRepository.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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\InterestCircle;
  10. use App\Models\InterestCircleArticle;
  11. use App\Models\Post;
  12. use Illuminate\Database\QueryException;
  13. use Dingo\Api\Http\Response;
  14. use Illuminate\Support\Carbon;
  15. use Illuminate\Support\Facades\DB;
  16. use Illuminate\Support\Facades\Log;
  17. use Illuminate\Support\Facades\Redis;
  18. class CircleArticleRepository
  19. {
  20. public function __construct(InterestCircleArticle $interestCircleArticle, Post $post,InterestCircle $interestCircle)
  21. {
  22. $this->interestCircleArticle = $interestCircleArticle;
  23. $this->post = $post;
  24. $this->interestCircle = $interestCircle;
  25. }
  26. /**
  27. * 内容列表
  28. */
  29. public function lists($request)
  30. {
  31. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  32. $where = [];
  33. if (isset($request['uid'])) {
  34. $where[] = ['uid', $request['uid']];
  35. }
  36. if (isset($request['circle_id'])) {
  37. $where[] = ['interest_circle_articles.circle_id', $request['circle_id']];
  38. }
  39. $articleModel = $this->post;
  40. return $articleModel
  41. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  42. ->join('interest_circle_articles', 'interest_circle_articles.post_id', '=', 'post.id')
  43. ->select('post.*', 'interest_circle_articles.is_recommend', 'interest_circle_articles.circle_id')
  44. ->with('data')
  45. ->where($where)
  46. ->where(function ($query) use ($request) {
  47. if (isset($request['content'])) {
  48. $query->where('title', 'like', "%{$request['content']}%")
  49. ->orWhere('content', 'like', "%{$request['content']}%");
  50. }
  51. })
  52. ->where(function ($query) use ($request) {
  53. if (isset($request['created_at'])) {
  54. $time = explode('_', $request['created_at']);
  55. $query->whereBetween('post.created_at', $time);
  56. }
  57. })
  58. ->orderBy('is_recommend', 'desc')
  59. ->paginate($perPage);
  60. }
  61. /**
  62. * 推荐精品文章
  63. */
  64. public function articleRecommend($request)
  65. {
  66. $article = $this->interestCircleArticle
  67. ->where('post_id', $request['post_id'])
  68. ->where('circle_id', $request['circle_id'])
  69. ->first();
  70. if (!$article) {
  71. return Response::create([
  72. 'message' => '获取精品文章信息失败',
  73. 'status_code' => 500
  74. ]);
  75. }
  76. if ($article->is_recommend == 1) {
  77. $article->is_recommend = 0;
  78. } else {
  79. $article->is_recommend = 1;
  80. }
  81. DB::beginTransaction();
  82. try {
  83. $article->save();
  84. DB::commit();
  85. return Response::create();
  86. } catch (QueryException $exception) {
  87. DB::rollBack();
  88. Log::debug('推荐精品文章:' . $request['post_id'] . '-' . $request['circle_id'] . $exception->getMessage());
  89. return Response::create([
  90. 'message' => '操作失败,请重试',
  91. 'error' => $exception->getMessage(),
  92. 'status_code' => 500
  93. ]);
  94. }
  95. }
  96. /**
  97. * 精品文章从当前圈子移出
  98. */
  99. public function articleRemove($request)
  100. {
  101. $article = $this->interestCircleArticle
  102. ->where('post_id', $request['post_id'])
  103. ->where('circle_id', $request['circle_id'])
  104. ->first();
  105. if (!$article) {
  106. return Response::create([
  107. 'message' => '获取精品文章信息失败',
  108. 'status_code' => 500
  109. ]);
  110. }
  111. DB::beginTransaction();
  112. try {
  113. $article->delete();
  114. $this->interestCircle->decrement('article_count');
  115. $isRef = $this->interestCircleArticle->where('post_id', $request['post_id'])->exists();
  116. if (!$isRef) {
  117. //todo 移出后需要检测该帖子在其他圈子有没有设为精品,如没有,则删除该帖子的精品标识
  118. //$this->post->where('id',$request['post_id'])->update();
  119. }
  120. $key = 'circle_articles_'.$request['circle_id'];
  121. Redis::zrem($key,$request['post_id']);
  122. DB::commit();
  123. return Response::create();
  124. } catch (QueryException $exception) {
  125. DB::rollBack();
  126. Log::debug('移出精品文章:' . $request['post_id'] . '-' . $request['circle_id'] . $exception->getMessage());
  127. return Response::create([
  128. 'message' => '操作失败,请重试',
  129. 'error' => $exception->getMessage(),
  130. 'status_code' => 500
  131. ]);
  132. }
  133. }
  134. /**
  135. * 新增精品文章
  136. */
  137. public function articleInsert($request)
  138. {
  139. $article = $this->interestCircleArticle
  140. ->where('post_id', $request['post_id'])
  141. ->where('circle_id', $request['circle_id'])
  142. ->first();
  143. if ($article) {
  144. return Response::create([
  145. 'message' => '该文章已存在当前圈子',
  146. 'status_code' => 500
  147. ]);
  148. }
  149. DB::beginTransaction();
  150. try {
  151. $this->interestCircleArticle->insert(['post_id'=>$request['post_id'],'circle_id'=>$request['circle_id']]);
  152. $this->interestCircle->increment('article_count');
  153. //将精品文章存入该圈子的有序集合中
  154. $key = 'circle_articles_'.$request['circle_id'];
  155. Redis::zadd($key,time(),$request['post_id']);
  156. DB::commit();
  157. return Response::create();
  158. } catch (QueryException $exception) {
  159. DB::rollBack();
  160. Log::debug('设置精品文章:' . $request['post_id'] . '-' . $request['circle_id'] . $exception->getMessage());
  161. return Response::create([
  162. 'message' => '操作失败,请重试',
  163. 'error' => $exception->getMessage(),
  164. 'status_code' => 500
  165. ]);
  166. }
  167. }
  168. /**
  169. * 当前圈子精品文章帖子来源列表
  170. * @param $request
  171. * @return mixed
  172. */
  173. public function postList($request){
  174. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  175. $where = [];
  176. if (isset($request['type'])) {
  177. $where[] = ['type', $request['type']];
  178. }
  179. if (isset($request['uid'])) {
  180. $where[] = ['uid', $request['uid']];
  181. }
  182. if (isset($request['circle_id'])) {
  183. $circleInfo = $this->interestCircle->where('id',$request['circle_id'])->first();
  184. if($circleInfo && $circleInfo->limit_article_ids){
  185. $request['topic_ids'] = "{$circleInfo->limit_article_ids}";
  186. }
  187. }
  188. $sort = 'post.id';
  189. if (isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])) {
  190. $sort = $request['sort'];
  191. }
  192. $post = $this->post;
  193. return $post
  194. ->where($where)
  195. ->where(function ($query) use ($request) {
  196. if (isset($request['keyword'])) {
  197. $query->where('uid', '=', $request['keyword'])
  198. ->orWhere('username', 'like', "%{$request['keyword']}%")
  199. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  200. }
  201. })
  202. ->where(function ($query) use ($request) {
  203. if (isset($request['content'])) {
  204. $query->where('title', 'like', "%{$request['content']}%")
  205. ->orWhere('content', 'like', "%{$request['content']}%");
  206. }
  207. })
  208. ->where(function ($query) use ($request) {
  209. if (isset($request['created_at'])) {
  210. $time = explode('_', $request['created_at']);
  211. $query->whereBetween('post.created_at', $time);
  212. }
  213. })
  214. ->where(function ($query) use ($request) {
  215. if (isset($request['category_ids']) || isset($request['topic_ids'])) {
  216. $ids = [];
  217. if (isset($request['category_ids'])) {
  218. $categoryIds = explode(',', $request['category_ids']);
  219. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  220. }
  221. if (isset($request['topic_ids'])) {
  222. $ids = array_merge($ids, explode(',', $request['topic_ids']));
  223. }
  224. foreach ($ids as $key => $id) {
  225. if ($key == 0) {
  226. $query = $query->whereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  227. } else {
  228. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  229. }
  230. }
  231. }
  232. })
  233. ->orderBy($sort, 'desc')
  234. ->paginate($perPage);
  235. }
  236. }