From f5f716800988b576f203b8314bab49e13dd5cb7f Mon Sep 17 00:00:00 2001 From: cano Date: Sat, 2 Mar 2024 16:53:43 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=AF=8F=E6=97=A5=E6=B4=BB?= =?UTF-8?q?=E8=B7=83=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/DailyCheckUserActiveStatus.php | 35 ++++++++ app/Console/Kernel.php | 1 + .../Customer/CustomerChangeInfoLogModel.php | 50 +++++++++++ .../Customer/CustomerLoginHistoryModel.php | 23 ++++++ .../Customer/CustomerUserExtendModel.php | 82 +++++++++++++++++++ app/Models/Customer/CustomerUserModel.php | 2 + app/Models/Follow/FollowModel.php | 2 +- app/Models/Post/PostPushBoxModel.php | 1 + .../WebSocket/CustomerWsHistoryModel.php | 49 +++++++++++ 9 files changed, 244 insertions(+), 1 deletion(-) create mode 100644 app/Console/Commands/DailyCheckUserActiveStatus.php create mode 100644 app/Models/Customer/CustomerChangeInfoLogModel.php create mode 100644 app/Models/Customer/CustomerLoginHistoryModel.php create mode 100644 app/Models/WebSocket/CustomerWsHistoryModel.php diff --git a/app/Console/Commands/DailyCheckUserActiveStatus.php b/app/Console/Commands/DailyCheckUserActiveStatus.php new file mode 100644 index 0000000..497552a --- /dev/null +++ b/app/Console/Commands/DailyCheckUserActiveStatus.php @@ -0,0 +1,35 @@ +info('每日更新用户活跃状态'); + $this->info('开始...'); + $oCustomerUserExtendModelExtend = new CustomerUserExtendModel(); + $oCustomerUserExtendModelExtend->updateUserActiveStatus(); + $this->info('结束...'); + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index e6b9960..1c275dd 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -13,6 +13,7 @@ class Kernel extends ConsoleKernel protected function schedule(Schedule $schedule): void { // $schedule->command('inspire')->hourly(); + $schedule->command('app:daily-check-user-active-status')->dailyAt('00:30')->onOneServer(); } /** diff --git a/app/Models/Customer/CustomerChangeInfoLogModel.php b/app/Models/Customer/CustomerChangeInfoLogModel.php new file mode 100644 index 0000000..55655fb --- /dev/null +++ b/app/Models/Customer/CustomerChangeInfoLogModel.php @@ -0,0 +1,50 @@ + '修改用户活跃状态', + ]; + + const REMARK_DAILY_CHECK_USER_ACTIVE_STATUS_YES = 'dailyCheckUserActiveStatusYes'; + const REMARK_DAILY_CHECK_USER_ACTIVE_STATUS_NO = 'dailyCheckUserActiveStatusNo'; + const REMARK = [ + self::REMARK_DAILY_CHECK_USER_ACTIVE_STATUS_YES => '每日检查用户活跃状态-活跃', + self::REMARK_DAILY_CHECK_USER_ACTIVE_STATUS_NO => '每日检查用户活跃状态-不活跃', + ]; + + function addLog($aItem): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null + { + $sDateTime = Carbon::now()->toDateTimeString(); + $aItem['created_at'] = $sDateTime; + return $this->addItem($aItem); + } + +} diff --git a/app/Models/Customer/CustomerLoginHistoryModel.php b/app/Models/Customer/CustomerLoginHistoryModel.php new file mode 100644 index 0000000..05825c2 --- /dev/null +++ b/app/Models/Customer/CustomerLoginHistoryModel.php @@ -0,0 +1,23 @@ +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(); + } + + } + } + } diff --git a/app/Models/Customer/CustomerUserModel.php b/app/Models/Customer/CustomerUserModel.php index d44574d..f4edd45 100644 --- a/app/Models/Customer/CustomerUserModel.php +++ b/app/Models/Customer/CustomerUserModel.php @@ -138,4 +138,6 @@ class CustomerUserModel extends CustomerBaseModel // } + + } diff --git a/app/Models/Follow/FollowModel.php b/app/Models/Follow/FollowModel.php index 9dd3027..08e9868 100644 --- a/app/Models/Follow/FollowModel.php +++ b/app/Models/Follow/FollowModel.php @@ -96,7 +96,7 @@ class FollowModel extends BaseModel $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) + ->where('b.fans_num','>=', $iFansLimit) ->get($col); } diff --git a/app/Models/Post/PostPushBoxModel.php b/app/Models/Post/PostPushBoxModel.php index a069e0b..064b180 100644 --- a/app/Models/Post/PostPushBoxModel.php +++ b/app/Models/Post/PostPushBoxModel.php @@ -149,6 +149,7 @@ class PostPushBoxModel extends BaseModel //放在用户状态更新时调用 function pullBigFanMasterPost($uid) { + //获取大v定义粉丝数,获取大于该粉丝数的大v文章 $iConfigFansPushLimit = intval(env('CONFIG_FANS_PUSH_LIMIT', 2000)); //@此处需针对大量数据进行分批获取 $oFollowModel = new FollowModel(); diff --git a/app/Models/WebSocket/CustomerWsHistoryModel.php b/app/Models/WebSocket/CustomerWsHistoryModel.php new file mode 100644 index 0000000..fb39390 --- /dev/null +++ b/app/Models/WebSocket/CustomerWsHistoryModel.php @@ -0,0 +1,49 @@ + 'onConnect', + ]; + + const STATUS_SUCCESS = 1; + const STATUS_FAIL = 2; + const STATUS = [ + self::STATUS_SUCCESS => '成功', + self::STATUS_FAIL => '失败', + ]; + + function getActiveUserIdList($date, $days = 3): array + { + return $this->newQuery() + ->where('event', self::EVENT_ON_CONNECT) + ->where('status', self::STATUS_SUCCESS) + ->whereBetween(DB::raw("DATE_FORMAT(created_at,'%Y-%m-%d')"), [Carbon::parse($date)->subDays($days - 1)->toDateString(), Carbon::parse($date)->toDateString()]) + ->distinct(['uid']) + ->get() + ->pluck('uid') + ->toArray(); + } + +}