Files
cycle_api/app/Models/Customer/CustomerUserExtendModel.php
2024-03-02 16:53:43 +08:00

155 lines
5.9 KiB
PHP

<?php
namespace App\Models\Customer;
use App\Exceptions\ModelException;
use App\Models\Base\CustomerBaseModel;
use App\Models\WebSocket\CustomerWsHistoryModel;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class CustomerUserExtendModel extends CustomerBaseModel
{
protected $table = 'customer_user_extend';
protected $primaryKey = 'uid';
protected $fillable = [
'uid',
'is_active',
'fans_num',
'follow_num',
'updated_at',
];
const COL_IS_ACTIVE = 'is_active';
//是否活跃用户
const IS_ACTIVE_YES = 1;
const IS_ACTIVE_NO = 2;
const IS_ACTIVE = [
self::IS_ACTIVE_YES => '活跃',
self::IS_ACTIVE_NO => '不活跃',
];
//增加用户扩展信息
function addExtend($aItem): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null
{
if(empty($aItem['uid'])) throw new ModelException('uid error');
if($this->findItem($aItem['uid'],['uid'])) return null; //已存在
$sDateTime = date('Y-m-d H:i:s');
$aItem['updated_at'] = $sDateTime;
return $this->addItem($aItem);
}
//增加当前粉丝总数
function incrFansNum($uid): int
{
$oExtend = $this->findItem($uid,'uid');
if(!$oExtend) throw new ModelException('user extend not found');
return $this->newQuery()->where('uid',$uid)->increment('fans_num');
}
//减去当前追随者总数
function decrFansNum($uid): int
{
$oExtend = $this->findItem($uid,'uid');
if(!$oExtend) throw new ModelException('user extend not found');
return $this->newQuery()->where('uid',$uid)->decrement('fans_num');
}
//增加当前订阅总数
function incrFollowNum($uid): int
{
$oExtend = $this->findItem($uid,'uid');
if(!$oExtend) throw new ModelException('user extend not found');
return $this->newQuery()->where('uid',$uid)->increment('follow_num');
}
//减去当前订阅总数
function decrFollowNum($uid): int
{
$oExtend = $this->findItem($uid,'uid');
if(!$oExtend) throw new ModelException('user extend not found');
return $this->newQuery()->where('uid',$uid)->decrement('follow_num');
}
//获取用户活跃信息
function getUserActiveListLimit($nowUid, $limit = 500): \Illuminate\Database\Eloquent\Collection|array
{
return $this->newQuery()
->where('id', '>=', $nowUid)
->limit($limit)
->orderBy('id')
->get(['uid', 'is_active']);
}
//检测所有用户活跃状态
function updateUserActiveStatus($date = null): void
{
if(empty($date)) $date = Carbon::yesterday()->toDateString();
$oCustomerWsHistoryModel = new CustomerWsHistoryModel();
$aActiveUserIdList = $oCustomerWsHistoryModel->getActiveUserIdList($date); //三日内活跃用户
if(empty($aActiveUserIdList)) return;
$oCustomerChangeInfoLogModel = new CustomerChangeInfoLogModel();
$nowUid = 0;
while (true){
try{
Db::beginTransaction();
$aUserList = $this->getUserActiveListLimit($nowUid, 500);
if(empty($aUserList)) break;
$nowUid = max($aUserList->pluck('uid')->toArray());
foreach ($aUserList as $oUser){
$isChanged = false;
$aLogInsert = [
'type' => CustomerChangeInfoLogModel::TYPE_CHANG_USER_ACTIVE_STATUS,
'uid' => $oUser->uid,
'column' => self::COL_IS_ACTIVE,
'before_value' => $oUser->is_active,
'pid' => CustomerChangeInfoLogModel::PID_SYSTEM,
];
if(in_array($oUser->uid, $aActiveUserIdList)){ //在活跃列表中
if($oUser->is_active == self::IS_ACTIVE_YES) continue; //已经是活跃用户
//变更用户状态
$res = $this->newQuery()->where('uid',$oUser->uid)->update(['is_active'=>self::IS_ACTIVE_YES]);
if($res) {
$isChanged = true;
$aLogInsert['value'] = self::IS_ACTIVE_YES;
$aLogInsert['after_value'] = self::IS_ACTIVE_YES;
$aLogInsert['remark_key'] = CustomerChangeInfoLogModel::REMARK_DAILY_CHECK_USER_ACTIVE_STATUS_YES;
$aLogInsert['remark_desc'] = CustomerChangeInfoLogModel::REMARK[CustomerChangeInfoLogModel::REMARK_DAILY_CHECK_USER_ACTIVE_STATUS_YES];
}
}else{ //三日内不活跃
if($oUser->is_active == self::IS_ACTIVE_NO) continue; //已经是不活跃用户
//变更用户状态
$res = $this->newQuery()->where('uid',$oUser->uid)->update(['is_active'=>self::IS_ACTIVE_NO]);
if($res) {
$isChanged = true;
$aLogInsert['value'] = self::IS_ACTIVE_NO;
$aLogInsert['after_value'] = self::IS_ACTIVE_NO;
$aLogInsert['remark_key'] = CustomerChangeInfoLogModel::REMARK_DAILY_CHECK_USER_ACTIVE_STATUS_NO;
$aLogInsert['remark_desc'] = CustomerChangeInfoLogModel::REMARK[CustomerChangeInfoLogModel::REMARK_DAILY_CHECK_USER_ACTIVE_STATUS_NO];
}
}
if($isChanged){ //有变化插入日志
$oCustomerChangeInfoLogModel->addLog($aLogInsert);
}
}
Db::commit();
}catch (\Exception $e){
Log::error('updateUserActiveStatus error:'.$e->getMessage());
Db::rollBack();
}
}
}
}