123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- namespace app\Repositories\Eloquent;
- use App\Models\BrowseRecord;
- use App\Models\SpecialistInfoLeaveMessage;
- use App\Models\SpecialistInfosAssociation;
- use App\Repositories\Eloquent\BaseRepository;
- use App\Repositories\Contracts\MentalityDrillInterface;
- use App\Models\MentalityDrill;
- use App\Models\MentalityDrillExtend;
- use Illuminate\Support\Facades\Auth;
- /**
- *
- * @author lilin
- *
- */
- class MentalityDrillFacadeRepository extends BaseRepository implements MentalityDrillInterface
- {
- public function getList(array $conditions, array $fields, string $sort, int $page, int $limit)
- {
- $result = MentalityDrill::where(function($query) use($conditions){
- if (isset($conditions['category_id'])){
- $query->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);
- }
- }
|