feat: 优化手机号在线时长统计,增加批量插入和事务处理

This commit is contained in:
lingling 2025-03-21 20:17:17 +08:00
parent ff983a42f5
commit 71dc5f4ffd
2 changed files with 49 additions and 33 deletions

View File

@ -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;

View File

@ -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([]);
}
}