where('category_id', $conditions['category_id']); } if (isset($conditions['search'])){ $query->where('title', 'like' , '%'.$conditions['search'].'%'); } if (isset($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); return $this->response($result); } public function findBy(array $conditions, array $fields){ $result = MentalityDrill::with([ 'category'=>function($with){$with->select(['id','pic','name']);}, 'extend'=>function($with){$with->select(['id','drill_id','name','time','src']);}, 'subject'=>function($with){$with->select(['id','mentality_drills_id','name','time','src','type','pic']);}, 'leaveMessage'=>function($with){$with->select(['id','user_id','mentality_drills_id','content','created_at']);}, ])->where(function($query) use($conditions){ if (isset($conditions['id'])){ $query->where('id', $conditions['id']); } })->first($fields); MentalityDrill::where('id', $conditions['id'])->increment('number_of_studies'); 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_WORKOUT ))->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_WORKOUT ))->count(); $history = BrowseRecord::where('object_id', $result['id'])->where('column', BrowseRecord::COLUMN_4)->orderBy('id','desc')->first(); $result['current_subject_id'] = $history?$history->subject_id: 0; } return $result ? $this->response($result) : $this->error()->dataDoesNotExist(); } public function create(array $data) { $result = MentalityDrill::create($data); return $result ? $this->response($result) : $this->error()->fail(); } public function updateBy(array $conditions, array $data) { $result = MentalityDrill::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 = MentalityDrill::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 getDrillExtend(array $conditions, array $fields) { $result = MentalityDrillExtend::where(function($query) use($conditions){ if (isset($conditions['id'])){ $query->where('id', $conditions['id']); } })->first($fields); return $result ? $this->response($result) : $this->error()->dataDoesNotExist(); } public function thumbsUpNum(int $id, int $userId) { $operateType = request('operateType', 0); $result = MentalityDrill::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_WORKOUT ); $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 = MentalityDrill::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_WORKOUT ); $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 = MentalityDrill::query()->find($id); if (empty($result)){ return $this->error()->fail("数据信息不存在"); } $data=array( "user_id"=>$userId, "mentality_drills_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); } }