CircleArticleRepository.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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->where('id',$request['circle_id'])->decrement('article_count');
  115. $isRef = $this->interestCircleArticle->where('post_id', $request['post_id'])->exists();
  116. if (!$isRef) {
  117. //移出后需要检测该帖子在其他圈子有没有设为精品,如没有,则删除该帖子的精品标识
  118. $this->post->where('id', $request['post_id'])->update(['is_fine' => 0]);
  119. Redis::HSET('post_info_' . $request['post_id'],'is_fine',0);
  120. }
  121. $key = 'circle_articles_' . $request['circle_id'];
  122. Redis::zrem($key, $request['post_id']);
  123. DB::commit();
  124. return Response::create();
  125. } catch (QueryException $exception) {
  126. DB::rollBack();
  127. Log::debug('移出精品文章:' . $request['post_id'] . '-' . $request['circle_id'] . $exception->getMessage());
  128. return Response::create([
  129. 'message' => '操作失败,请重试',
  130. 'error' => $exception->getMessage(),
  131. 'status_code' => 500
  132. ]);
  133. }
  134. }
  135. /**
  136. * 新增精品文章
  137. */
  138. public function articleInsert($request)
  139. {
  140. $article = $this->interestCircleArticle
  141. ->where('post_id', $request['post_id'])
  142. ->where('circle_id', $request['circle_id'])
  143. ->first();
  144. if ($article) {
  145. return Response::create([
  146. 'message' => '该文章已存在当前圈子',
  147. 'status_code' => 500
  148. ]);
  149. }
  150. DB::beginTransaction();
  151. try {
  152. $this->interestCircleArticle->insert(['post_id' => $request['post_id'], 'circle_id' => $request['circle_id']]);
  153. $this->interestCircle->where('id',$request['circle_id'])->increment('article_count');
  154. //修改帖子为精品贴
  155. $this->post->where('id', $request['post_id'])->update(['is_fine' => 1]);
  156. Redis::HSET('post_info_' . $request['post_id'],'is_fine',1);
  157. //将精品文章存入该圈子的有序集合中
  158. $key = 'circle_articles_' . $request['circle_id'];
  159. Redis::zadd($key, time(), $request['post_id']);
  160. DB::commit();
  161. return Response::create();
  162. } catch (QueryException $exception) {
  163. DB::rollBack();
  164. Log::debug('设置精品文章:' . $request['post_id'] . '-' . $request['circle_id'] . $exception->getMessage());
  165. return Response::create([
  166. 'message' => '操作失败,请重试',
  167. 'error' => $exception->getMessage(),
  168. 'status_code' => 500
  169. ]);
  170. }
  171. }
  172. /**
  173. * 当前圈子精品文章帖子来源列表
  174. * @param $request
  175. * @return mixed
  176. */
  177. public function postList($request)
  178. {
  179. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  180. $where = [];
  181. if (isset($request['type'])) {
  182. $where[] = ['type', $request['type']];
  183. }
  184. if (isset($request['uid'])) {
  185. $where[] = ['uid', $request['uid']];
  186. }
  187. if (isset($request['circle_id'])) {
  188. $circleInfo = $this->interestCircle->where('id', $request['circle_id'])->first();
  189. if ($circleInfo && $circleInfo->limit_article_ids) {
  190. $request['topic_ids'] = "{$circleInfo->limit_article_ids}";
  191. }
  192. }
  193. $sort = 'post.id';
  194. if (isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])) {
  195. $sort = $request['sort'];
  196. }
  197. $post = $this->post;
  198. return $post
  199. ->where($where)
  200. ->where(function ($query) use ($request) {
  201. if (isset($request['keyword'])) {
  202. $query->where('uid', '=', $request['keyword'])
  203. ->orWhere('username', 'like', "%{$request['keyword']}%")
  204. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  205. }
  206. })
  207. ->where(function ($query) use ($request) {
  208. if (isset($request['content'])) {
  209. $query->where('title', 'like', "%{$request['content']}%")
  210. ->orWhere('content', 'like', "%{$request['content']}%");
  211. }
  212. })
  213. ->where(function ($query) use ($request) {
  214. if (isset($request['created_at'])) {
  215. $time = explode('_', $request['created_at']);
  216. $query->whereBetween('post.created_at', $time);
  217. }
  218. })
  219. ->where(function ($query) use ($request) {
  220. if (isset($request['category_ids']) || isset($request['topic_ids'])) {
  221. $ids = [];
  222. if (isset($request['category_ids'])) {
  223. $categoryIds = explode(',', $request['category_ids']);
  224. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  225. }
  226. if (isset($request['topic_ids'])) {
  227. $ids = array_merge($ids, explode(',', $request['topic_ids']));
  228. }
  229. foreach ($ids as $key => $id) {
  230. if ($key == 0) {
  231. $query = $query->whereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  232. } else {
  233. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  234. }
  235. }
  236. }
  237. })
  238. ->orderBy($sort, 'desc')
  239. ->paginate($perPage);
  240. }
  241. }