feat: 添加手机号在线历史归类功能,新增 UserPhoneDayLog 模型并优化数据处理逻辑
This commit is contained in:
parent
c5b308c1c4
commit
fdb351a901
|
@ -11,18 +11,21 @@ use App\model\Dictionary;
|
||||||
use App\model\Message;
|
use App\model\Message;
|
||||||
use App\model\ExchangeRate;
|
use App\model\ExchangeRate;
|
||||||
use app\model\User;
|
use app\model\User;
|
||||||
|
use app\model\UserPhoneDayLog;
|
||||||
|
use app\model\UserPhoneLog;
|
||||||
use app\model\UserReward;
|
use app\model\UserReward;
|
||||||
use hg\apidoc\annotation as Apidoc;
|
use hg\apidoc\annotation as Apidoc;
|
||||||
use support\Db;
|
use support\Db;
|
||||||
use support\Log;
|
use support\Log;
|
||||||
|
use Carbon\Carbon;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Apidoc\Title("?")
|
* @Apidoc\Title("自用测试?")
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class TextController
|
class TextController
|
||||||
{
|
{
|
||||||
protected $noNeedLogin = ['get_projectdetailed', 'get_projectdetailed', 'get_mechanism_list'];
|
protected $noNeedLogin = ['classifyPhoneOnlineHistory', 'get_projectdetailed', 'get_mechanism_list'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Apidoc\Title("1.0 手机号在线历史归类函数")
|
* @Apidoc\Title("1.0 手机号在线历史归类函数")
|
||||||
|
@ -31,15 +34,40 @@ class TextController
|
||||||
*/
|
*/
|
||||||
public function classifyPhoneOnlineHistory(Request $request)
|
public function classifyPhoneOnlineHistory(Request $request)
|
||||||
{
|
{
|
||||||
$result = Db::table('your_table') // 这里的 'your_table' 应该是你存储用户积分的表名
|
$threeDaysAgo = Carbon::now()->subDays(3)->startOfDay(); // 三天前的开始时间
|
||||||
->select('username', Db::raw('SUM(amount) as total_points')) // 计算每个用户名的积分总数
|
$date = Db::table('user_phone_log')
|
||||||
->groupBy('username') // 按用户名分组
|
->selectRaw('DATE(created_at) AS date')
|
||||||
|
->where('created_at', '<=', $threeDaysAgo)
|
||||||
|
->groupByRaw('DATE(created_at)')
|
||||||
|
->orderBy('date')
|
||||||
->get();
|
->get();
|
||||||
foreach ($result as $v) {
|
foreach ($date as $v) {
|
||||||
$user = User::where('username', $v->username)->first();
|
// var_dump($v->date);
|
||||||
$user->money += $v->total_points;
|
|
||||||
$user->save();
|
$date = $v->date;
|
||||||
break;
|
|
||||||
|
// 使用 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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace app\model;
|
||||||
|
|
||||||
|
use support\Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户WS信息表模型
|
||||||
|
*
|
||||||
|
* @property int $id 用户记录的唯一标识符
|
||||||
|
* @property string $phone 用户手机号(WS手机号)
|
||||||
|
* @property string $created_at 创建时间
|
||||||
|
* @property string $updated_at 更新时间
|
||||||
|
* @property string $time 在线时间
|
||||||
|
*/
|
||||||
|
class UserPhoneDayLog extends Model
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 表名
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $table = 'user_phone_day_log';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 主键
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $primaryKey = 'id';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否自动维护时间戳字段
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
public $timestamps = true;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
# 获取所有符合条件的测试文件
|
||||||
|
$testFiles = Get-ChildItem -Path "./tests" -Filter "Test*.php"
|
||||||
|
|
||||||
|
# 遍历每个测试文件并执行 PHPUnit
|
||||||
|
foreach ($testFile in $testFiles) {
|
||||||
|
$filePath = $testFile.FullName # 获取文件的完整路径
|
||||||
|
Write-Host "Running tests on: $filePath"
|
||||||
|
|
||||||
|
# 执行 PHPUnit 测试
|
||||||
|
& ./vendor/bin/phpunit --bootstrap support/bootstrap.php --testdox $filePath
|
||||||
|
}
|
Loading…
Reference in New Issue