174 lines
5.8 KiB
PHP
174 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Follow;
|
|
|
|
use App\Exceptions\ModelException;
|
|
use App\Models\Base\BaseModel;
|
|
use App\Models\Customer\CustomerUserExtendModel;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class FollowModel extends BaseModel
|
|
{
|
|
protected $table = 'customer_follow';
|
|
protected $primaryKey = 'id';
|
|
protected $fillable = [
|
|
'id',
|
|
'uid',
|
|
'follow_uid',
|
|
'created_at',
|
|
];
|
|
|
|
/**
|
|
* 添加订阅 订阅别人
|
|
* @throws ModelException
|
|
*/
|
|
function addFollow($uid, $follow_uid): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null
|
|
{
|
|
$aItem['uid'] = $uid;
|
|
$aItem['follow_uid'] = $follow_uid;
|
|
$sDateTime = date('Y-m-d H:i:s');
|
|
$aItem['created_at'] = $sDateTime;
|
|
$res = $this->addItem($aItem);
|
|
if ($res) {
|
|
//增加到历史记录里
|
|
$oFollowHistoryModel = new FollowHistoryModel();
|
|
$oFollowHistoryModel->addFollowHistory(FollowHistoryModel::METHOD_FOLLOW, $uid, $follow_uid);
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
//增加到user_extends中
|
|
$oCustomerUserExtendModel = new CustomerUserExtendModel();
|
|
$oCustomerUserExtendModel->incrFollowNum($uid);
|
|
$oCustomerUserExtendModel->incrFansNum($follow_uid);
|
|
|
|
DB::commit();
|
|
}catch (\Exception $e){
|
|
DB::rollBack();
|
|
throw new ModelException($e->getMessage());
|
|
}
|
|
}
|
|
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 取消订阅
|
|
* @throws ModelException
|
|
*/
|
|
function unFollow($uid, $follow_uid)
|
|
{
|
|
$res = $this->newQuery()->where('uid', $uid)->where('follow_uid', $follow_uid)->delete();
|
|
if ($res) {
|
|
//增加到历史记录里
|
|
$oFollowHistoryModel = new FollowHistoryModel();
|
|
$oFollowHistoryModel->addFollowHistory(FollowHistoryModel::METHOD_UNFOLLOW, $uid, $follow_uid);
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
//增加到user_extends中
|
|
$oCustomerUserExtendModel = new CustomerUserExtendModel();
|
|
$oCustomerUserExtendModel->decrFollowNum($uid);
|
|
$oCustomerUserExtendModel->decrFansNum($follow_uid);
|
|
|
|
DB::commit();
|
|
}catch (\Exception $e){
|
|
DB::rollBack();
|
|
throw new ModelException($e->getMessage());
|
|
}
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
//获取user following(订阅)列表
|
|
function getFollowList($uid): \Illuminate\Database\Eloquent\Collection|array
|
|
{
|
|
return $this->newQuery()->where('uid', $uid)->get();
|
|
}
|
|
|
|
//获取关注列表中大v用户
|
|
function getFollowListWithFansLimit($uid,$iFansLimit = 2000,$col = ['a.follow_uid']): \Illuminate\Support\Collection
|
|
{
|
|
$oCustomerUserExtendModel = new CustomerUserExtendModel();
|
|
$oFollowModel = new FollowModel();
|
|
|
|
$oModel = DB::table($oFollowModel->getTable() . ' as a');
|
|
return $oModel->where('a.uid', $uid)
|
|
->leftjoin($oCustomerUserExtendModel->getTable() . ' as b', 'a.follow_uid', '=', 'b.uid')
|
|
->where('b.fans_num', $iFansLimit)
|
|
->get($col);
|
|
}
|
|
|
|
//检测是否双向关注
|
|
function isEachOtherFollow($uid, $follow_uid): bool
|
|
{
|
|
$aFollow = $this->isFollow($uid, $follow_uid);
|
|
$aFollow2 = $this->isFollow($follow_uid, $uid);
|
|
if ($aFollow && $aFollow2) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function isFollow($uid, $follow_uid): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|null
|
|
{
|
|
return $this->newQuery()->where('uid', $uid)->where('follow_uid', $follow_uid)->first(['id']);
|
|
}
|
|
|
|
//获取关注数
|
|
function getFollowCount($uid): int
|
|
{
|
|
return $this->newQuery()->where('uid', $uid)->count();
|
|
}
|
|
|
|
//获取被关注列表
|
|
function getFansList($uid, $col = ['*'], $offset = null, $limit = null): \Illuminate\Database\Eloquent\Collection|array
|
|
{
|
|
$oModel = $this->newQuery();
|
|
if ($offset) $oModel->offset($offset);
|
|
if ($limit) $oModel->limit($offset);
|
|
return $oModel->where('follow_uid', $uid)->get($col);
|
|
}
|
|
|
|
//检测是否被关注
|
|
function isFollowed($uid, $follow_uid): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|null
|
|
{
|
|
return $this->newQuery()->where('uid', $follow_uid)->where('follow_uid', $uid)->first(['id']);
|
|
}
|
|
|
|
//获取粉丝数(被关注数)
|
|
function getFansCount($uid): int
|
|
{
|
|
return $this->newQuery()->where('follow_uid', $uid)->count(['id']);
|
|
}
|
|
|
|
//获取活跃粉丝列表
|
|
function getActiveFansUidList($uid, $col = ['a.*'], $offset = null, $limit = null): \Illuminate\Support\Collection
|
|
{
|
|
$oCustomerUserExtendModel = new CustomerUserExtendModel();
|
|
$oFollowModel = new FollowModel();
|
|
|
|
$oModel = DB::table($oFollowModel->getTable() . ' as a');
|
|
if ($offset) $oModel->offset($offset);
|
|
if ($limit) $oModel->limit($offset);
|
|
return $oModel->where('a.follow_uid', $uid)
|
|
->leftjoin($oCustomerUserExtendModel->getTable() . ' as b', 'a.uid', '=', 'b.uid')
|
|
->where('b.is_active', CustomerUserExtendModel::IS_ACTIVE_YES)
|
|
->get($col);
|
|
}
|
|
|
|
//获取活跃粉丝计数
|
|
function getActiveFansUidListCount($uid, $col = ['a.id']): int
|
|
{
|
|
$oCustomerUserExtendModel = new CustomerUserExtendModel();
|
|
$oFollowModel = new FollowModel();
|
|
return DB::table($oFollowModel->getTable() . ' as a')->where('a.follow_uid', $uid)
|
|
->leftjoin($oCustomerUserExtendModel->getTable() . ' as b', 'a.uid', '=', 'b.uid')
|
|
->where('b.is_active', CustomerUserExtendModel::IS_ACTIVE_YES)
|
|
->count($col);
|
|
}
|
|
|
|
|
|
}
|