hasOne(User::class,'uid','left_id'); } public function focus() { return $this->hasOne(User::class,'uid','right_id'); } public function community() { return $this->hasOne(Community::class,'community_id','right_id') ->bind(['community_id','title','image','start','uid','create_time','count_start','author','is_type']); } public function getIsStartAttr() { return self::where('left_id', $this->right_id) ->where('right_id',$this->left_id) ->where('type', 'fans') ->count() > 0; } public function spu() { return $this->hasOne(Spu::class, 'spu_id','right_id'); } public function merchant() { return $this->hasOne(Merchant::class, 'mer_id','right_id'); } public function category() { return $this->hasOne(StoreCategory::class, 'store_category_id','right_id'); } public function auth() { return $this->hasOne(Menu::class, 'menu_id','right_id'); } public function searchLeftIdAttr($query, $value) { $query->where('left_id', $value); } public function searchRightIdAttr($query, $value) { $query->where('right_id', $value); } public function searchTypeAttr($query, $value) { $query->where('type', $value); } /** * @return static */ public static function getInstance(): self { return new static(); } /** * TODO 搜索 * @param $where * @return BaseModel * @author Qinii * @day 2020-10-16 */ protected function getSearch(array $where) { foreach ($where as $key => $item) { if ($item !== '') { $keyArray[] = $key; $whereArr[$key] = $item; } } if(empty($keyArray)){ return self::getInstance()->db(); }else{ return self::withSearch($keyArray, $whereArr); } } /** * TODO 添加 * @param int $leftId * @param int $rightId * @param string $type * @param $check * @return bool * @author Qinii * @day 10/28/21 */ public function insert(int $leftId, int $rightId, string $type, bool $check = false) { if ($check && $this->checkHas($leftId, $rightId, $type)) { return false; } $data = [ 'left_id' => $leftId, 'right_id'=> $rightId, 'type' => $type, ]; try{ Relevance::create($data); return true; } catch (\Exception $exception) { throw new ValidateException('创建失败'); } } /** * TODO 删除 * @param int $leftId * @param string $type * @param int $rightId * @return bool * @author Qinii * @day 10/28/21 */ public function destory(int $leftId, int $rightId, string $type) { return $this->getSearch([ 'left_id' => $leftId, 'right_id'=> $rightId, 'type' => $type, ])->delete(); } /** * TODO 检测是否存在 * @param int $leftId * @param int $rightId * @param string $type * @return int * @author Qinii * @day 10/28/21 */ public function checkHas(int $leftId, int $rightId, string $type) { return $this->getSearch([ 'left_id' => $leftId, 'right_id'=> $rightId, 'type' => $type, ])->count(); } /** * TODO 根据左键批量删除 * @param int $leftId * @param $type * @return bool * @author Qinii * @day 10/28/21 */ public function batchDelete(int $leftId, $type) { return $this->getSearch([ 'left_id' => $leftId, 'type' => $type, ])->delete(); } /** * TODO 关注我的人 * @param int $uid * @return \think\Collection * @author Qinii * @day 10/28/21 */ public function getUserFans(int $uid, int $page, int $limit) { $query = $this->getSearch([ 'right_id' => $uid, 'type' => self::TYPE_COMMUNITY_FANS, ])->with([ 'fans' => function($query) { $query->field('uid,avatar,nickname,count_fans'); } ]); $count = $query->count(); $list = $query->page($page, $limit)->select()->append(['is_start']); return compact('count','list'); } /** * TODO 我关注的人 * @param $uid * @return \think\Collection * @author Qinii * @day 10/28/21 */ public function getUserFocus(int $uid, int $page, int $limit) { $query = $this->getSearch([ 'left_id' => $uid, 'type' => self::TYPE_COMMUNITY_FANS, ])->with([ 'focus' => function($query) { $query->field('uid,avatar,nickname,count_fans'); } ]); $count = $query->count(); $list = $query->page($page, $limit)->select()->append(['is_fans']); return compact('count','list'); } /** * TODO 我点赞过的文章 * @param int $uid * @return \think\Collection * @author Qinii * @day 10/28/21 */ public function getUserStartCommunity(array $where, int $page, int $limit) { $query = $this->joinUser($where)->with([ 'community'=> function($query) use($where){ $query->with([ 'author' => function($query){ $query->field('uid,real_name,status,avatar,nickname,count_start'); }, 'is_start' => function($query) use ($where) { $query->where('left_id',$where['uid']); }, 'topic' => function($query) { $query->where('status', 1)->where('is_del',0); $query->field('topic_id,topic_name,status,category_id,pic,is_del'); }, 'relevance' => [ 'spu' => function($query) { $query->field('spu_id,store_name,image,price,product_type,activity_id,product_id'); } ], 'is_fans' => function($query) use($where){ $query->where('left_id',$where['uid']); }]); }, ]); $count = $query->count(); $list = $query->page($page, $limit)->select()->each(function ($item){ $item['time'] = date('m月d日', strtotime($item['create_time'])); return $item; }); return compact('count','list'); } /** * TODO 我点赞过的文章 * @param int $uid * @return \think\Collection * @author Qinii * @day 10/28/21 */ public function getUserStartCommunityByVideos(array $where, int $page, int $limit) { $query = $this->joinUser($where)->with([ 'community'=> function($query) { $query->with(['author'=> function($query) { $query->field('uid,avatar,nickname'); }]); }, ]); $count = $query->count(); $list = $query->page($page, $limit)->select()->each(function ($item){ $item['time'] = date('m月d日', strtotime($item['create_time'])); return $item; }); return compact('count','list'); } public function getFieldCount(string $field, int $value, string $type) { return $this->getSearch([$field => $value, 'type' => $type,])->count(); } /** * */ public function createMany(int $leftId, array $rightId, string $type) { if (!empty($rightId)) { foreach ($rightId as $value) { $res[] = [ 'left_id' => $leftId, 'right_id' => $value, 'type' => $type, ]; } self::batchInsert($res); } } /** * 批量插入 * * @param array $data * @return int * @author xaboy * @day 2020/6/8 */ public function batchInsert(array $data) { $rows = Relevance::insertAll($data); return $rows; } // ------- 2 --------------- public function clear(int $id, $type, string $field) { if (is_string($type)) $type = [$type]; return Relevance::where($field, $id)->whereIn('type', $type)->delete(); } public function joinUser($where) { $query = Relevance::hasWhere('community',function($query) use($where){ $query->where('status',1)->where('is_show',1)->where('is_del',0); $query->when(isset($where['is_type']) && $where['is_type'] !== '',function($query) use($where){ $query->where('is_type',$where['is_type']); }); }); $query->where('left_id',$where['uid'])->where('type',self::TYPE_COMMUNITY_START); return $query; } //-------------------------- }