123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019-06-10
- * Time: 18:12
- */
- namespace App\Repositories;
- use App\Models\Category;
- use App\Models\CategoryTopic;
- use App\Models\Topic;
- use Dingo\Api\Http\Response;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Database\QueryException;
- class TopicRepository {
- public function __construct(Topic $topic,CategoryTopic $categoryTopic){
- $this->topic = $topic;
- $this->categoryTopic = $categoryTopic;
- }
- //列表
- public function index($request){
- $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
- $where = [];
- if(isset($request['name'])){
- $where[] = ['name', 'like', "%{$request['name']}%"];
- }
- if(isset($request['topic_status'])){
- if($request['topic_status'] == 'is_suggest'){
- $where[] = ['is_suggest', 1];
- }
- if( $request['topic_status'] == 'is_hot'){
- $where[] = ['is_hot', 1];
- }
- if($request['topic_status'] == 'is_open'){
- $where[] = ['is_open', 1];
- }
- if($request['topic_status'] == 'is_close'){
- $where[] = ['is_open', 0];
- }
- }
- return $this->topic->where($where)->paginate($perPage);
- }
- //新增
- public function create($request){
- $topic = $this->topic->where(['name'=>$request['name']])->first();
- if($topic){
- return Response::create([
- 'message' => '该话题已存在',
- 'status_code' => 500
- ]);
- }
- $data = [
- 'name' => $request['name'],
- 'img' => $request['img'],
- 'desc' => $request['desc'],
- ];
- if(isset($request['category_ids'])){
- $category_ids = explode(',', $request['category_ids']);
- }
- DB::beginTransaction();
- try{
- $topicInfo = $this->topic->create($data);
- if($topicInfo){
- if($category_ids){
- $category_topic_data = [];
- foreach($category_ids as $value){
- $category_topic_data[] = [
- 'category_id' => $value,
- 'topic_id' => $topicInfo->id,
- ];
- }
- $this->categoryTopic->insert($category_topic_data);
- }
- }
- DB::commit();
- return Response::create();
- }catch (QueryException $exception){
- DB::rollBack();
- Log::debug('新增话题:'.$exception->getMessage());
- return Response::create([
- 'message' => '新增话题,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- public function edit($request){
- $topic = $this->topic->where(['id'=>$request['id']])->first();
- if(!$topic){
- return Response::create([
- 'message' => '该话题不存在',
- 'status_code' => 500
- ]);
- }
- $_topic = $this->topic->where(['name'=>$request['name']])->where('id','<>',$topic->id)->first();
- if($_topic){
- return Response::create([
- 'message' => '该话题已存在',
- 'status_code' => 500
- ]);
- }
- $topic->name = $request['name'];
- $topic->img = $request['img'];
- $topic->desc = $request['desc'];
- $category_ids = [];
- if(!empty($request['category_ids'])){
- $category_ids = explode(',', $request['category_ids']);
- }
- DB::beginTransaction();
- try{
- //保存
- $topicInfo = $topic->save();
- //删除原来关联关系
- $this->categoryTopic->where(['topic_id'=>$topic->id])->delete();
- if($topicInfo){
- if($category_ids){
- $category_topic_data = [];
- foreach($category_ids as $value){
- $category_topic_data[] = [
- 'category_id' => $value,
- 'topic_id' => $topic->id,
- ];
- }
- $this->categoryTopic->insert($category_topic_data);
- }
- }
- DB::commit();
- return Response::create();
- }catch (QueryException $exception){
- DB::rollBack();
- Log::debug('编辑话题:'.$exception->getMessage());
- return Response::create([
- 'message' => '编辑话题,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- //修改
- public function update($request){
- $topic = $this->topic->where('id', $request['id'])->first();
- if(!$topic){
- return Response::create([
- 'message' => '该话题不存在',
- 'status_code' => 500
- ]);
- }
- if(isset($request['is_open']) && $request['is_open'] !== null){
- $topic->is_open = $request['is_open'];
- }
- if(!empty($request['type'])){
- if($topic->is_open == 0){
- return Response::create([
- 'message' => '该话题未开启',
- 'status_code' => 500
- ]);
- }
- if(isset($request['type']) && $request['type'] == 1){
- $topic->is_suggest = $request['status'];
- }
- if(isset($request['type']) && $request['type'] == 2){
- $topic->is_hot = $request['status'];
- }
- }
- $res = $topic->save();
- if($res){
- return Response::create();
- }
- }
- //详情
- public function view($request){
- return $this->topic->where('id', $request['id'])->first();
- }
- }
|