baji/app/command/UserStatisticsHour.php
2025-03-14 14:33:18 +08:00

60 lines
2.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\command;
use app\common\model\UserAccount;
use app\common\model\UserStatistics;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
class UserStatisticsHour extends Command
{
//*/30 * * * * /www/server/cron/ZDFH
protected function configure()
{
$this->setName('UserStatisticsHour')->setDescription('用户登录数据统计');
}
protected function execute(Input $input, Output $output)
{
//# 统计表 user_statistics
// ELECT `id`, `login_count`, `register_count`, `created_at`, `updated_at`, `record_date` FROM `user_statistics`
//# 用户登录记录表 user_accountlast_login_time 最后登录时间时间戳int(11) 。user_id 用户id
//SELECT `id`, `user_id`, `account_token`, `token_num`, `token_time`, `last_login_time`, `last_login_ip`, `last_login_ip1`, `ip_adcode`, `ip_province`, `ip_city` FROM `user_account`
// 获取当天日期
$today = date('Y-m-d');
// 查询当天的登录人数
$loginCount = UserAccount::where('last_login_time', '>=', strtotime($today))
->where('last_login_time', '<', strtotime($today . ' +1 day'))
->count('DISTINCT user_id');
// 检查当天的统计记录是否存在
$existingRecord = UserStatistics::where('record_date', $today)
->find();
if ($existingRecord) {
// 更新记录
UserStatistics::where('record_date', $today)
->update(['login_count' => $loginCount, 'updated_at' => date('Y-m-d H:i:s')]);
} else {
// 插入新记录
UserStatistics::insert([
'login_count' => $loginCount,
'register_count' => 0, // 假设注册人数统计在其他地方处理
'record_date' => $today,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
]);
}
}
}