feat: 优化手机号在线时长统计,增加批量插入和事务处理
This commit is contained in:
parent
ff983a42f5
commit
71dc5f4ffd
|
@ -3,6 +3,7 @@
|
||||||
namespace app\controller\admin\api\v1;
|
namespace app\controller\admin\api\v1;
|
||||||
|
|
||||||
use app\dao\UserDao;
|
use app\dao\UserDao;
|
||||||
|
use app\model\UserPhoneDayLog;
|
||||||
use app\dao\UserRewardDao;
|
use app\dao\UserRewardDao;
|
||||||
use support\Request;
|
use support\Request;
|
||||||
use App\Utils\ApiResponse;
|
use App\Utils\ApiResponse;
|
||||||
|
@ -210,10 +211,11 @@ class JobuserController
|
||||||
// 遍历每个手机号,查询该手机号的在线时间总和
|
// 遍历每个手机号,查询该手机号的在线时间总和
|
||||||
foreach ($UserPhone as $v) {
|
foreach ($UserPhone as $v) {
|
||||||
$time = UserPhoneLog::where('phone', $v->phone)->sum('time');
|
$time = UserPhoneLog::where('phone', $v->phone)->sum('time');
|
||||||
|
$time2 = UserPhoneDayLog::where('phone', $v->phone)->sum('time');
|
||||||
// 将手机号和总时间添加到结果数组中
|
// 将手机号和总时间添加到结果数组中
|
||||||
$userPhonesWithTime[] = [
|
$userPhonesWithTime[] = [
|
||||||
'phone' => $v->phone,
|
'phone' => $v->phone,
|
||||||
'total_time' => floor($time / 60)
|
'total_time' => floor(($time+$time2) / 60)
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
$res['user'] = $user;
|
$res['user'] = $user;
|
||||||
|
|
|
@ -3,26 +3,18 @@
|
||||||
namespace app\controller\api;
|
namespace app\controller\api;
|
||||||
|
|
||||||
use support\Request;
|
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\UserPhoneDayLog;
|
||||||
use app\model\UserPhoneLog;
|
use app\model\UserPhoneLog;
|
||||||
use app\model\UserReward;
|
use App\Utils\ApiResponseApp;
|
||||||
use hg\apidoc\annotation as Apidoc;
|
use Carbon\Carbon;
|
||||||
use support\Db;
|
use support\Db;
|
||||||
use support\Log;
|
use support\Log;
|
||||||
use Carbon\Carbon;
|
use Exception;
|
||||||
|
use hg\apidoc\annotation as Apidoc;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Apidoc\Title("自用测试?")
|
* @Apidoc\Title("自用测试")
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class TextController
|
class TextController
|
||||||
{
|
{
|
||||||
protected $noNeedLogin = ['classifyPhoneOnlineHistory', 'get_projectdetailed', 'get_mechanism_list'];
|
protected $noNeedLogin = ['classifyPhoneOnlineHistory', 'get_projectdetailed', 'get_mechanism_list'];
|
||||||
|
@ -34,40 +26,62 @@ class TextController
|
||||||
*/
|
*/
|
||||||
public function classifyPhoneOnlineHistory(Request $request)
|
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')
|
->selectRaw('DATE(created_at) AS date')
|
||||||
->where('created_at', '<=', $threeDaysAgo)
|
->where('created_at', '<', $threeDaysAgo)
|
||||||
->groupByRaw('DATE(created_at)')
|
->groupByRaw('DATE(created_at)')
|
||||||
->orderBy('date')
|
->orderBy('date')
|
||||||
->get();
|
->get();
|
||||||
foreach ($date as $v) {
|
|
||||||
// var_dump($v->date);
|
|
||||||
|
|
||||||
|
// 遍历每个日期
|
||||||
|
foreach ($dates as $v) {
|
||||||
$date = $v->date;
|
$date = $v->date;
|
||||||
|
|
||||||
// 使用 Carbon 格式化日期为一天的开始和结束
|
// 使用 Carbon 获取当天的开始和结束时间
|
||||||
$startOfDay = Carbon::parse($date)->startOfDay();
|
$startOfDay = Carbon::parse($date)->startOfDay();
|
||||||
$endOfDay = Carbon::parse($date)->endOfDay();
|
$endOfDay = Carbon::parse($date)->endOfDay();
|
||||||
|
|
||||||
// 查询指定日期的记录
|
// 查询当天的日志记录,并按手机号汇总在线时间
|
||||||
$logs = UserPhoneLog::whereBetween('created_at', [$startOfDay, $endOfDay])
|
$logs = UserPhoneLog::whereBetween('created_at', [$startOfDay, $endOfDay])
|
||||||
->selectRaw('phone, SUM(time) AS total_time') // 按照手机号分组,统计总时长
|
->selectRaw('phone, SUM(time) AS total_time')
|
||||||
->groupBy('phone')
|
->groupBy('phone')
|
||||||
->get();
|
->get();
|
||||||
var_dump($date);
|
|
||||||
// 处理每个手机的在线时长
|
// 准备批量插入的数据
|
||||||
|
$batchData = [];
|
||||||
foreach ($logs as $log) {
|
foreach ($logs as $log) {
|
||||||
// 在这里可以处理每个手机的在线时长,如记录日志、更新数据库等
|
$batchData[] = [
|
||||||
echo "Phone: {$log->phone}, Total Time: {$log->total_time}" . PHP_EOL;
|
'phone' => $log->phone,
|
||||||
$UserPhoneDayLog=new UserPhoneDayLog();
|
'created_at' => $startOfDay,
|
||||||
$UserPhoneDayLog->phone=$log->phone;
|
'updated_at' => $startOfDay,
|
||||||
$UserPhoneDayLog->created_at=$startOfDay;
|
'time' => $log->total_time,
|
||||||
$UserPhoneDayLog->updated_at=$startOfDay;
|
];
|
||||||
$UserPhoneDayLog->time=$log->total_time;
|
|
||||||
$UserPhoneDayLog->save();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 开始数据库事务
|
||||||
|
Db::beginTransaction();
|
||||||
|
try {
|
||||||
|
// 批量插入数据到 user_phone_day_log 表
|
||||||
|
if (!empty($batchData)) {
|
||||||
|
UserPhoneDayLog::insert($batchData);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 删除已处理的日志记录
|
||||||
UserPhoneLog::whereBetween('created_at', [$startOfDay, $endOfDay])->delete();
|
UserPhoneLog::whereBetween('created_at', [$startOfDay, $endOfDay])->delete();
|
||||||
|
|
||||||
|
// 提交事务
|
||||||
|
Db::commit();
|
||||||
|
} catch (Exception $e) {
|
||||||
|
// 发生异常,回滚事务
|
||||||
|
Db::rollBack();
|
||||||
|
// 记录错误日志
|
||||||
|
Log::error("Error processing date {$date}: " . $e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return ApiResponseApp::success([]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue