From 71dc5f4ffd17155c7cff018cb10e6866073277c6 Mon Sep 17 00:00:00 2001 From: lingling <1077478963@qq.com> Date: Fri, 21 Mar 2025 20:17:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E6=89=8B=E6=9C=BA?= =?UTF-8?q?=E5=8F=B7=E5=9C=A8=E7=BA=BF=E6=97=B6=E9=95=BF=E7=BB=9F=E8=AE=A1?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0=E6=89=B9=E9=87=8F=E6=8F=92=E5=85=A5?= =?UTF-8?q?=E5=92=8C=E4=BA=8B=E5=8A=A1=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/api/v1/JobuserController.php | 4 +- app/controller/api/TextController.php | 78 +++++++++++-------- 2 files changed, 49 insertions(+), 33 deletions(-) diff --git a/app/controller/admin/api/v1/JobuserController.php b/app/controller/admin/api/v1/JobuserController.php index a9ac07b..f238f80 100644 --- a/app/controller/admin/api/v1/JobuserController.php +++ b/app/controller/admin/api/v1/JobuserController.php @@ -3,6 +3,7 @@ namespace app\controller\admin\api\v1; use app\dao\UserDao; +use app\model\UserPhoneDayLog; use app\dao\UserRewardDao; use support\Request; use App\Utils\ApiResponse; @@ -210,10 +211,11 @@ class JobuserController // 遍历每个手机号,查询该手机号的在线时间总和 foreach ($UserPhone as $v) { $time = UserPhoneLog::where('phone', $v->phone)->sum('time'); + $time2 = UserPhoneDayLog::where('phone', $v->phone)->sum('time'); // 将手机号和总时间添加到结果数组中 $userPhonesWithTime[] = [ 'phone' => $v->phone, - 'total_time' => floor($time / 60) + 'total_time' => floor(($time+$time2) / 60) ]; } $res['user'] = $user; diff --git a/app/controller/api/TextController.php b/app/controller/api/TextController.php index 7559e31..e485160 100644 --- a/app/controller/api/TextController.php +++ b/app/controller/api/TextController.php @@ -3,26 +3,18 @@ namespace app\controller\api; use support\Request; - -use App\Utils\ApiResponseApp; - - -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 App\Utils\ApiResponseApp; +use Carbon\Carbon; use support\Db; use support\Log; -use Carbon\Carbon; +use Exception; +use hg\apidoc\annotation as Apidoc; /** - * @Apidoc\Title("自用测试?") + * @Apidoc\Title("自用测试") */ - class TextController { protected $noNeedLogin = ['classifyPhoneOnlineHistory', 'get_projectdetailed', 'get_mechanism_list']; @@ -34,40 +26,62 @@ class TextController */ public function classifyPhoneOnlineHistory(Request $request) { - $threeDaysAgo = Carbon::now()->subDays(3)->startOfDay(); // 三天前的开始时间 - $date = Db::table('user_phone_log') + // 计算三天前的日期 + $threeDaysAgo = Carbon::now()->subDays(3)->startOfDay(); + + // 查询三天前的日期列表 + $dates = Db::table('user_phone_log') ->selectRaw('DATE(created_at) AS date') - ->where('created_at', '<=', $threeDaysAgo) + ->where('created_at', '<', $threeDaysAgo) ->groupByRaw('DATE(created_at)') ->orderBy('date') ->get(); - foreach ($date as $v) { - // var_dump($v->date); + // 遍历每个日期 + foreach ($dates as $v) { $date = $v->date; - // 使用 Carbon 格式化日期为一天的开始和结束 + // 使用 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') // 按照手机号分组,统计总时长 + ->selectRaw('phone, SUM(time) AS total_time') ->groupBy('phone') ->get(); - var_dump($date); - // 处理每个手机的在线时长 + + // 准备批量插入的数据 + $batchData = []; 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(); + $batchData[] = [ + 'phone' => $log->phone, + 'created_at' => $startOfDay, + 'updated_at' => $startOfDay, + 'time' => $log->total_time, + ]; + } + + // 开始数据库事务 + Db::beginTransaction(); + try { + // 批量插入数据到 user_phone_day_log 表 + if (!empty($batchData)) { + UserPhoneDayLog::insert($batchData); + } + + // 删除已处理的日志记录 + UserPhoneLog::whereBetween('created_at', [$startOfDay, $endOfDay])->delete(); + + // 提交事务 + Db::commit(); + } catch (Exception $e) { + // 发生异常,回滚事务 + Db::rollBack(); + // 记录错误日志 + Log::error("Error processing date {$date}: " . $e->getMessage()); } - UserPhoneLog::whereBetween('created_at', [$startOfDay, $endOfDay])->delete(); } + return ApiResponseApp::success([]); } }