提交代码

This commit is contained in:
zpc 2025-03-14 14:33:18 +08:00
parent 8b4292c8b4
commit a1685adcad
2 changed files with 114 additions and 0 deletions

View File

@ -0,0 +1,59 @@
<?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')
]);
}
}
}

View File

@ -0,0 +1,55 @@
<?php
namespace app\common\model;
use app\common\model\Base;
use think\Model;
/**
* 用户统计数据
*/
class UserStatistics extends Base{
// 设置当前模型对应的完整数据表名称
protected $table = 'user_statistics';
/**
* 获取列表
*/
public static function getList($where = [],$field='*',$order='',$pageSize = "15")
{
$list = self::where($where)
->field($field)
->order($order)
->paginate(['list_rows'=>$pageSize,'query' => request()->param()]);
$page = $list->render();
$data['list'] = $list->toArray()['data'];
$data['count']=$list->total();
$data['last_page']=$list->toArray()['last_page'];
$data['page']=$page;
return $data;
}
/**
* 获取列表 不分页
*/
public static function getAllList($where = [],$field='*',$order='',$limit = '0')
{
$data = self::where($where)
->field($field)
->order($order)
->limit($limit)
->select();
return $data;
}
/**
* 获取单条数据
*/
public static function getInfo($where = [],$field = '*'){
$data = self::where($where)
->field($field)
->find();
return $data;
}
}