webman/app/controller/api/TextController.php

74 lines
2.4 KiB
PHP

<?php
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 support\Db;
use support\Log;
use Carbon\Carbon;
/**
* @Apidoc\Title("自用测试?")
*/
class TextController
{
protected $noNeedLogin = ['classifyPhoneOnlineHistory', 'get_projectdetailed', 'get_mechanism_list'];
/**
* @Apidoc\Title("1.0 手机号在线历史归类函数")
* @Apidoc\Url("api/text/classifyPhoneOnlineHistory")
* @Apidoc\Method("POST")
*/
public function classifyPhoneOnlineHistory(Request $request)
{
$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 ($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();
}
}
}