123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- <?php
- namespace app\Repositories\Eloquent;
- use App\Models\SpecialistInfo;
- use App\Models\SpecialistInfoLeaveMessage;
- use App\Models\SpecialistInfosAssociation;
- use App\Repositories\Contracts\SpecialistInfoInterface;
- use App\Repositories\Eloquent\BaseRepository;
- use Illuminate\Support\Facades\Auth;
- /**
- *
- * @author lilin
- *
- */
- class SpecialistInfoFacadeRepository extends BaseRepository implements SpecialistInfoInterface
- {
- public function getList(array $conditions, array $fields, string $sort, int $page, int $limit)
- {
- $result = SpecialistInfo::with(['firstCloumn'=>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);
- }
- }
|