From a1685adcadba3a1af24d40abd884c4d49f26b669 Mon Sep 17 00:00:00 2001 From: zpc Date: Fri, 14 Mar 2025 14:33:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/command/UserStatisticsHour.php | 59 +++++++++++++++++++++++++++++ app/common/model/UserStatistics.php | 55 +++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 app/command/UserStatisticsHour.php create mode 100644 app/common/model/UserStatistics.php diff --git a/app/command/UserStatisticsHour.php b/app/command/UserStatisticsHour.php new file mode 100644 index 0000000..63b3dc4 --- /dev/null +++ b/app/command/UserStatisticsHour.php @@ -0,0 +1,59 @@ +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_account,last_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') + ]); + } + + } +} diff --git a/app/common/model/UserStatistics.php b/app/common/model/UserStatistics.php new file mode 100644 index 0000000..7a4fb04 --- /dev/null +++ b/app/common/model/UserStatistics.php @@ -0,0 +1,55 @@ +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; + } + +} \ No newline at end of file