123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/6/5
- * Time: 16:03
- */
- namespace App\Repositories\Circle;
- use App\Models\InterestCircleArticle;
- use App\Models\Post;
- use Illuminate\Database\QueryException;
- use Dingo\Api\Http\Response;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Redis;
- class CircleArticleRepository
- {
- public function __construct(InterestCircleArticle $interestCircleArticle, Post $post)
- {
- $this->interestCircleArticle = $interestCircleArticle;
- $this->post = $post;
- }
- /**
- * 内容列表
- */
- public function lists($request)
- {
- $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
- $where = [];
- if (isset($request['uid'])) {
- $where[] = ['uid', $request['uid']];
- }
- $articleModel = $this->post;
- return $articleModel
- ->join('post_data', 'post_data.post_id', '=', 'post.id')
- ->join('interest_circle_articles', 'interest_circle_articles.post_id', '=', 'post.id')
- ->select('post.*', 'interest_circle_articles.is_recommend', 'interest_circle_articles.circle_id')
- ->with('data')
- ->where($where)
- ->where(function ($query) use ($request) {
- if (isset($request['content'])) {
- $query->where('title', 'like', "%{$request['content']}%")
- ->orWhere('content', 'like', "%{$request['content']}%");
- }
- })
- ->where(function ($query) use ($request) {
- if (isset($request['created_at'])) {
- $time = explode('_', $request['created_at']);
- $query->whereBetween('post.created_at', $time);
- }
- })
- ->orderBy('is_recommend', 'desc')
- ->paginate($perPage);
- }
- /**
- * 推荐精品文章
- */
- public function articleRecommend($request)
- {
- $article = $this->interestCircleArticle
- ->where('post_id', $request['post_id'])
- ->where('circle_id', $request['circle_id'])
- ->first();
- if (!$article) {
- return Response::create([
- 'message' => '获取精品文章信息失败',
- 'status_code' => 500
- ]);
- }
- if ($article->is_recommend == 1) {
- $article->is_recommend = 0;
- } else {
- $article->is_recommend = 1;
- }
- DB::beginTransaction();
- try {
- $article->save();
- DB::commit();
- return Response::create();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('推荐精品文章:' . $request['post_id'] . '-' . $request['circle_id'] . $exception->getMessage());
- return Response::create([
- 'message' => '操作失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- /**
- * 精品文章从当前圈子移出
- */
- public function articleRemove($request)
- {
- $article = $this->interestCircleArticle
- ->where('post_id', $request['post_id'])
- ->where('circle_id', $request['circle_id'])
- ->first();
- if (!$article) {
- return Response::create([
- 'message' => '获取精品文章信息失败',
- 'status_code' => 500
- ]);
- }
- DB::beginTransaction();
- try {
- $article->delete();
- $isRef = $this->interestCircleArticle->where('post_id', $request['post_id'])->exists();
- if (!$isRef) {
- //todo 移出后需要检测该帖子在其他圈子有没有设为精品,如没有,则删除该帖子的精品标识
- //$this->post->where('id',$request['post_id'])->update();
- }
- DB::commit();
- return Response::create();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('移出精品文章:' . $request['post_id'] . '-' . $request['circle_id'] . $exception->getMessage());
- return Response::create([
- 'message' => '操作失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- }
|