function($with){ $with->select(['id','pid','name']); },'secondCloumn'=>function($with){ $with->select(['id','pid','name']); }])->where(function($query) use($conditions){ if (isset($conditions['first_id'])){ $query->where('first_id', $conditions['first_id']); } if (isset($conditions['second_id'])){ $second_id = (int)$conditions['second_id']; $query->where('second_id', $second_id); // $excludeData = array(12,13); // if (in_array($second_id,$excludeData)){ // $query->where('second_id',$second_id); // }elseif ($second_id == 0){ // $query->whereNotIn('second_id',$excludeData); // }else{ // $query->where('second_id', $second_id); // } } if (isset($conditions['type'])){ $query->where('type', $conditions['type']); } if (isset($conditions['tag_id']) && $conditions['tag_id']){ $query->where('tag_id', $conditions['tag_id']); } if (isset($conditions['name']) && !empty($conditions['name'])){ $query->where('name', 'like' , '%'.$conditions['name'].'%'); } if (isset($conditions['comment_tag']) && $conditions['comment_tag']){ $query->where(function($query) use($conditions){ foreach ($conditions['comment_tag'] as $tag){ $tag = str_replace(array("[","]","\""),"",$tag); $query->orWhereRaw("JSON_CONTAINS(comment_tag, '\"{$tag}\"', '$')"); } }); } })->orderByRaw($sort)->paginate($limit, $fields); $result->each(function($item){ if ($item->cover){ $newCover = []; foreach ($item->cover as $cover){ $newCover[] = config('console.pic_path').$cover; } $item->cover = $newCover; } }); return $this->response($result); } public function findBy(array $conditions, array $fields){ $result = SpecialistInfo::with( [ 'firstCloumn'=>function($with){$with->select(['id','pid','name']);}, 'secondCloumn'=>function($with){$with->select(['id','pid','name']);}, 'leaveMessage'=>function($with){$with->select(['id','user_id','specialist_infos_id','content','created_at']);}, ] )->where(function($query) use($conditions){ if (isset($conditions['id'])){ $query->where('id', $conditions['id']); } })->first($fields); if ($result->cover){ $newCover = []; foreach ($result->cover as $cover){ $newCover[] = config('console.pic_path').$cover; } $result->cover = $newCover; } if ($result->video){ $result->video = config('console.pic_path').$result->video; } if ($result){ $result = $result->toArray(); $user_id = Auth::id(); $result["is_thumbs_up"] = SpecialistInfosAssociation::query() ->where(array( "user_id"=>$user_id, "association_id"=>$result["id"], "association_type"=>SpecialistInfosAssociation::ASSOCIATION_TYPE_THUMBS_UP, "operation_type"=>SpecialistInfosAssociation::OPERATION_TYPE_CLASS_ROOM ))->count(); $result["is_love"] = SpecialistInfosAssociation::query() ->where(array( "user_id"=>$user_id, "association_id"=>$result["id"], "association_type"=>SpecialistInfosAssociation::ASSOCIATION_TYPE_LOVE, "operation_type"=>SpecialistInfosAssociation::OPERATION_TYPE_CLASS_ROOM ))->count(); } return $result ? $this->response($result) : $this->error()->dataDoesNotExist(); } public function create(array $data) { $result = SpecialistInfo::create($data); return $result ? $this->response($result) : $this->error()->fail(); } public function updateBy(array $conditions, array $data) { $result = SpecialistInfo::where(function($query) use($conditions){ if (isset($conditions['id'])){ $query->where('id', $conditions['id']); } })->update($data); return $result ? $this->response($result) : $this->error()->fail(); } public function deleteBy(array $conditions) { $result = SpecialistInfo::where(function($query) use($conditions){ if (isset($conditions['id'])){ $query->where('id', $conditions['id']); } })->delete(); return $result ? $this->response($result) : $this->error()->fail(); } public function thumbsUpNum(int $id, int $userId) { $operateType = request('operateType', 0); $result = SpecialistInfo::query()->find($id); if (empty($result)){ return $this->error()->fail("数据信息不存在"); } if (empty($userId) || $userId <= 0){ return $this->error()->fail("用户信息错误"); } $data=array( "user_id"=>$userId, "association_id"=>$id, "association_type"=>SpecialistInfosAssociation::ASSOCIATION_TYPE_THUMBS_UP, "operation_type"=>SpecialistInfosAssociation::OPERATION_TYPE_CLASS_ROOM ); $checkCount = SpecialistInfosAssociation::query()->where($data)->count(); if ($operateType){ if ($checkCount <= 0){ $addResult = SpecialistInfosAssociation::query()->create($data); if (empty($addResult)){ return $this->error()->fail("新增点赞信息失败"); } $result->increment('thumbs_up_num'); } }else{ if ($checkCount){ $result->decrement('thumbs_up_num'); SpecialistInfosAssociation::query()->where($data)->delete(); } } return $this->response($result); } public function loveNum(int $id, int $userId) { $operateType = request('operateType', 0); $result = SpecialistInfo::query()->find($id); if (empty($result)){ return $this->error()->fail("数据信息不存在"); } if (empty($userId) || $userId <= 0){ return $this->error()->fail("用户信息错误"); } $data=array( "user_id"=>$userId, "association_id"=>$id, "association_type"=>SpecialistInfosAssociation::ASSOCIATION_TYPE_LOVE, "operation_type"=>SpecialistInfosAssociation::OPERATION_TYPE_CLASS_ROOM ); $checkCount = SpecialistInfosAssociation::query()->where($data)->count(); if ($operateType){ if ($checkCount <= 0){ $addResult = SpecialistInfosAssociation::query()->create($data); if (empty($addResult)){ return $this->error()->fail("新增喜欢信息失败"); } $result->increment('love_num'); } }else{ if ($checkCount){ $result->decrement('love_num'); SpecialistInfosAssociation::query()->where($data)->delete(); } } return $this->response($result); } public function leaveMessage(int $id, int $userId, string $message) { if (empty($userId) || $userId <= 0){ return $this->error()->fail("用户信息错误"); } if (empty($message)){ return $this->error()->fail("留言信息不能为空"); } $result = SpecialistInfo::query()->find($id); if (empty($result)){ return $this->error()->fail("数据信息不存在"); } $data=array( "user_id"=>$userId, "specialist_infos_id"=>$id, ); $checkCount = SpecialistInfoLeaveMessage::query()->where($data)->count(); if ($checkCount <= 0){ $addResult = SpecialistInfoLeaveMessage::query()->create( array_merge( $data, array("content"=>$message) ) ); if (empty($addResult)){ return $this->error()->fail("新增留言信息失败"); } $result->increment('leave_num'); } return $this->response($result); } public function specialistInfosAssociationList(int $userId, int $associationType,int $page, int $limit) { $operateType = request('operateType', 0); $base = array( "user_id"=>$userId, "association_type"=>$associationType, "operation_type"=>SpecialistInfosAssociation::OPERATION_TYPE_CLASS_ROOM ); $result = SpecialistInfosAssociation::with( [ "specialist"=>function($with){$with->select([ 'id','first_id','second_id','name','subtitle','content', 'cover','video','type','speaker', 'source','author_id','number_of_studies','thumbs_up_num', 'love_num','leave_num' ]);} ] )->where( function($query) use($base,$operateType){ $query->where($base); // if ($operateType==1){ // $query->whereRaw(" // association_id in ( select id from specialist_infos where second_id in (12,13) ) //"); // }else { // $query->whereRaw(" // association_id not in ( select id from specialist_infos where second_id in (12,13) ) //"); // } } ) ->orderByRaw("id desc")->paginate($limit, ['*'],"page",$page); $list = $result->items(); foreach ($list as &$val){ $val = $val->toArray(); if ($val["specialist"]){ $specialist = $val["specialist"]; $specialist["video"] = config('console.pic_path').$specialist["video"]; if ($specialist["cover"]){ foreach ($specialist["cover"] as &$va){ $va = config('console.pic_path').$va; } } $val["specialist"] = $specialist; } } $result->setCollection(collect($list)); return $this->response($result); } }