123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/6/5
- * Time: 16:03
- */
- namespace App\Repositories\Circle;
- use App\Models\InterestCircle;
- use App\Models\InterestCirclePicture;
- 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 CircleRepository
- {
- public function __construct(InterestCircle $interestCircle, InterestCirclePicture $interestCirclePicture)
- {
- $this->interestCircle = $interestCircle;
- $this->interestCirclePicture = $interestCirclePicture;
- }
- /**
- * 创建圈子
- */
- public function create($request)
- {
- $data = [
- 'name' => $request['name'],
- 'notice' => $request['notice'],
- 'image' => $request['image'] ?? '',
- 'join_limit' => $request['join_limit'],
- 'limit_condition' => $request['limit_condition'],
- 'join_question' => json_encode($request['join_question']) ?? '',
- 'limit_article_ids' => $request['limit_article_ids'] ?? '',
- 'contains_function' => json_encode($request['contains_function']) ?? '',
- ];
- DB::beginTransaction();
- try {
- $circle = $this->interestCircle->create($data);
- DB::commit();
- return Response::create();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('创建圈子exception:' . $exception->getMessage());
- return Response::create([
- 'message' => '创建失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- /**
- * 修改圈子
- */
- public function update($request)
- {
- $data = [
- 'name' => $request['name'],
- 'notice' => $request['notice'],
- 'image' => $request['image'] ?? '',
- 'join_limit' => $request['join_limit'],
- 'limit_condition' => $request['limit_condition'],
- 'join_question' => json_encode($request['join_question']),
- 'limit_article_ids' => $request['limit_article_ids'] ?? '',
- 'contains_function' => json_encode($request['contains_function']),
- ];
- DB::beginTransaction();
- try {
- $circle = $this->interestCircle->where('id', $request['id'])->update($data);
- DB::commit();
- return Response::create();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('修改圈子exception:' . $exception->getMessage());
- return Response::create([
- 'message' => '修改失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- /**
- * 圈子列表
- */
- public function circleLists($request)
- {
- $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
- $where = [];
- if (isset($request['is_open'])) {
- $where[] = ['is_open', $request['is_open']];
- }
- return $this->interestCircle
- ->where($where)
- ->where(function ($query) use ($request) {
- if (isset($request['keyword'])) {
- $query->Where('name', 'like', "%{$request['keyword']}%");
- }
- })
- ->orderBy('is_recommend', 'desc')
- ->orderBy('created_at', 'desc')
- ->paginate($perPage);
- }
- /**
- * 圈子详情
- */
- public function detail($request, $isTrashed = false)
- {
- $model = $this->interestCircle;
- if ($isTrashed) {
- $model->withTrashed();
- }
- return $model->find($request['id']);
- }
- /**
- * 推荐圈子
- */
- public function circleRecommend($request)
- {
- $circle = $this->interestCircle->where('id', $request['id'])->first();
- if (!$circle) {
- return Response::create([
- 'message' => '获取圈子信息失败',
- 'status_code' => 500
- ]);
- }
- if ($circle->is_recommend == 1) {
- $circle->is_recommend = 0;
- } else {
- $circle->is_recommend = 1;
- }
- DB::beginTransaction();
- try {
- $circle->save();
- DB::commit();
- return Response::create();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('推荐圈子:' . $request['id'] . $exception->getMessage());
- return Response::create([
- 'message' => '操作失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- /**
- * 开启/关闭圈子
- */
- public function circleStatus($request)
- {
- $circle = $this->interestCircle->where('id', $request['id'])->first();
- if (!$circle) {
- return Response::create([
- 'message' => '获取圈子信息失败',
- 'status_code' => 500
- ]);
- }
- if ($circle->is_open == 1) {
- $circle->is_open = 0;
- } else {
- $circle->is_open = 1;
- }
- DB::beginTransaction();
- try {
- $circle->save();
- DB::commit();
- return Response::create();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('操作圈子:' . $request['id'] . $exception->getMessage());
- return Response::create([
- 'message' => '操作失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- /**
- * 相册列表
- */
- public function pictureLists($request)
- {
- $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
- $where = [];
- if (isset($request['circle_id'])) {
- $where[] = ['circle_id', $request['circle_id']];
- }
- if (isset($request['uid'])) {
- $where[] = ['uid', $request['uid']];
- }
- return $this->interestCirclePicture
- ->where($where)
- ->orderBy('created_at', 'desc')
- ->paginate($perPage);
- }
- /**
- * 删除相册图片
- */
- public function deletePicture($request)
- {
- $circle = $this->interestCirclePicture->where('id', $request['id'])->first();
- if (!$circle) {
- return Response::create([
- 'message' => '获取图片信息失败',
- 'status_code' => 500
- ]);
- }
- DB::beginTransaction();
- try {
- $circle->delete();
- DB::commit();
- return Response::create();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('删除相册图片:' . $request['id'] . $exception->getMessage());
- return Response::create([
- 'message' => '操作失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- }
|