From fdb351a90168370e425a446ac2468536edba877a Mon Sep 17 00:00:00 2001 From: lingling <1077478963@qq.com> Date: Mon, 17 Mar 2025 19:22:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E6=89=8B=E6=9C=BA?= =?UTF-8?q?=E5=8F=B7=E5=9C=A8=E7=BA=BF=E5=8E=86=E5=8F=B2=E5=BD=92=E7=B1=BB?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=96=B0=E5=A2=9E=20UserPhoneDayLog?= =?UTF-8?q?=20=E6=A8=A1=E5=9E=8B=E5=B9=B6=E4=BC=98=E5=8C=96=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/api/TextController.php | 48 +++++++++++++++++++++------ app/model/UserPhoneDayLog.php | 40 ++++++++++++++++++++++ testall.ps1 | 11 ++++++ 3 files changed, 89 insertions(+), 10 deletions(-) create mode 100644 app/model/UserPhoneDayLog.php create mode 100644 testall.ps1 diff --git a/app/controller/api/TextController.php b/app/controller/api/TextController.php index 6cc1828..7559e31 100644 --- a/app/controller/api/TextController.php +++ b/app/controller/api/TextController.php @@ -11,18 +11,21 @@ use App\model\Dictionary; use App\model\Message; use App\model\ExchangeRate; use app\model\User; +use app\model\UserPhoneDayLog; +use app\model\UserPhoneLog; use app\model\UserReward; use hg\apidoc\annotation as Apidoc; use support\Db; use support\Log; +use Carbon\Carbon; /** - * @Apidoc\Title("?") + * @Apidoc\Title("自用测试?") */ class TextController { - protected $noNeedLogin = ['get_projectdetailed', 'get_projectdetailed', 'get_mechanism_list']; + protected $noNeedLogin = ['classifyPhoneOnlineHistory', 'get_projectdetailed', 'get_mechanism_list']; /** * @Apidoc\Title("1.0 手机号在线历史归类函数") @@ -31,15 +34,40 @@ class TextController */ public function classifyPhoneOnlineHistory(Request $request) { - $result = Db::table('your_table') // 这里的 'your_table' 应该是你存储用户积分的表名 - ->select('username', Db::raw('SUM(amount) as total_points')) // 计算每个用户名的积分总数 - ->groupBy('username') // 按用户名分组 + $threeDaysAgo = Carbon::now()->subDays(3)->startOfDay(); // 三天前的开始时间 + $date = Db::table('user_phone_log') + ->selectRaw('DATE(created_at) AS date') + ->where('created_at', '<=', $threeDaysAgo) + ->groupByRaw('DATE(created_at)') + ->orderBy('date') ->get(); - foreach ($result as $v) { - $user = User::where('username', $v->username)->first(); - $user->money += $v->total_points; - $user->save(); - break; + foreach ($date as $v) { + // var_dump($v->date); + + $date = $v->date; + + // 使用 Carbon 格式化日期为一天的开始和结束 + $startOfDay = Carbon::parse($date)->startOfDay(); + $endOfDay = Carbon::parse($date)->endOfDay(); + + // 查询指定日期的记录 + $logs = UserPhoneLog::whereBetween('created_at', [$startOfDay, $endOfDay]) + ->selectRaw('phone, SUM(time) AS total_time') // 按照手机号分组,统计总时长 + ->groupBy('phone') + ->get(); + var_dump($date); + // 处理每个手机的在线时长 + foreach ($logs as $log) { + // 在这里可以处理每个手机的在线时长,如记录日志、更新数据库等 + echo "Phone: {$log->phone}, Total Time: {$log->total_time}" . PHP_EOL; + $UserPhoneDayLog=new UserPhoneDayLog(); + $UserPhoneDayLog->phone=$log->phone; + $UserPhoneDayLog->created_at=$startOfDay; + $UserPhoneDayLog->updated_at=$startOfDay; + $UserPhoneDayLog->time=$log->total_time; + $UserPhoneDayLog->save(); + } + UserPhoneLog::whereBetween('created_at', [$startOfDay, $endOfDay])->delete(); } } } diff --git a/app/model/UserPhoneDayLog.php b/app/model/UserPhoneDayLog.php new file mode 100644 index 0000000..3b89094 --- /dev/null +++ b/app/model/UserPhoneDayLog.php @@ -0,0 +1,40 @@ +