123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019-06-10
- * Time: 17:07
- */
- namespace App\Repositories;
- use App\Models\TopicGroup;
- use App\Models\TopicGroupInfo;
- use Dingo\Api\Http\Response;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Database\QueryException;
- use Illuminate\Support\Facades\Log;
- class TopicGroupRepository {
- public function __construct(TopicGroup $topicGroup,TopicGroupInfo $topicGroupInfo) {
- $this->topicGroup = $topicGroup;
- $this->topicGroupInfo = $topicGroupInfo;
- }
- public function index($request){
- $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
- $where = [];
- if(isset($request['name'])){
- $where[] = ['name', 'like', "%{$request['name']}%"];
- }
- return $this->topicGroup->where($where)->paginate($perPage);
- }
- //添加
- public function create($request){
- $category = $this->topicGroup->where(['name'=>$request['name']])->first();
- if($category){
- return Response::create([
- 'message' => '该分组已存在',
- 'status_code' => 500
- ]);
- }
- $data = [
- 'name' => $request['name'],
- 'status' => 1,
- 'desc' => '',
- ];
- if(!empty($request['topic_ids'])){
- $topic_ids = explode(',', $request['topic_ids']);
- }
- DB::beginTransaction();
- try{
- $topicGroup = $this->topicGroup->create($data);
- if($topicGroup){
- if($topic_ids){
- $topic_group_inof_data = [];
- foreach($topic_ids as $value){
- $topic_group_inof_data[] = [
- 'topic_group_id' => $topicGroup->id,
- 'topic_id' => $value,
- ];
- }
- $this->topicGroupInfo->insert($topic_group_inof_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){
- $topicGroup = $this->topicGroup->where(['id'=>$request['id']])->first();
- if(!$topicGroup){
- return Response::create([
- 'message' => '该话题组不存在',
- 'status_code' => 500
- ]);
- }
- $topicGroup->name = $request['name'];
- if(!empty($request['topic_ids'])){
- $topic_ids = explode(',', $request['topic_ids']);
- }
- DB::beginTransaction();
- try{
- $topicGroupInfo = $topicGroup->save();
- if($topicGroupInfo){
- //删除
- $this->topicGroupInfo->where(['topic_group_id'=>$topicGroup->id])->delete();
- if($topic_ids){
- $category_topic_data = [];
- foreach($topic_ids as $value){
- $category_topic_data[] = [
- 'topic_group_id' => $topicGroup->id,
- 'topic_id' => $value,
- ];
- }
- $this->topicGroupInfo->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 view($request){
- return $this->topicGroup->where(['id'=>$request['id']])->first();
- }
- //删除
- public function delete($request){
- $topicGroup = $this->topicGroup->where(['id'=>$request['id']])->first();
- DB::beginTransaction();
- try{
- if($topicGroup){
- $res = $this->topicGroup->where(['id'=>$request['id']])->delete();
- if($res){
- $this->topicGroupInfo->where(['topic_group_id'=>$topicGroup->id])->delete();
- }
- }
- DB::commit();
- return Response::create();
- }catch (QueryException $exception){
- DB::rollBack();
- Log::debug('删除推荐话题组:'.$exception->getMessage());
- return Response::create([
- 'message' => '删除推荐话题组失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- }
|