| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use Illuminate\Support\Str;
- use App\Module\Pet\Models\PetActiveSkill;
- use App\Module\Pet\Models\PetSkillProcessLog;
- use App\Module\Pet\Jobs\ProcessActiveSkillsJob;
- /**
- * 测试宠物技能处理日志功能
- */
- class TestPetSkillProcessLog extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'pet:test-skill-process-log {--user-id= : 指定用户ID进行测试}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '测试宠物技能处理日志功能';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle()
- {
- $userId = $this->option('user-id');
-
- $this->info('开始测试宠物技能处理日志功能...');
- // 1. 查看当前激活的技能
- $this->info('1. 查看当前激活的技能');
- $activeSkills = PetActiveSkill::active();
-
- if ($userId) {
- $activeSkills = $activeSkills->whereHas('pet', function ($query) use ($userId) {
- $query->where('user_id', $userId);
- });
- }
-
- $activeSkills = $activeSkills->get();
-
- if ($activeSkills->isEmpty()) {
- $this->warn('没有找到激活的技能');
- return 0;
- }
- $this->table(
- ['ID', '宠物ID', '用户ID', '技能名称', '状态', '开始时间', '结束时间'],
- $activeSkills->map(function ($skill) {
- return [
- $skill->id,
- $skill->pet_id,
- $skill->pet->user_id,
- $skill->skill_name,
- $skill->status,
- $skill->start_time->format('Y-m-d H:i:s'),
- $skill->end_time->format('Y-m-d H:i:s'),
- ];
- })->toArray()
- );
- // 2. 执行技能处理任务
- $this->info('2. 执行技能处理任务');
-
- $job = new ProcessActiveSkillsJob();
- $result = $job->run();
-
- if ($result) {
- $this->info('技能处理任务执行成功');
- } else {
- $this->error('技能处理任务执行失败');
- return 1;
- }
- // 3. 查看生成的处理日志
- $this->info('3. 查看生成的处理日志(最近10条)');
-
- $logs = PetSkillProcessLog::with(['activeSkill', 'pet'])
- ->orderBy('processed_at', 'desc')
- ->limit(10);
-
- if ($userId) {
- $logs = $logs->where('user_id', $userId);
- }
-
- $logs = $logs->get();
- if ($logs->isEmpty()) {
- $this->warn('没有找到处理日志');
- } else {
- $this->table(
- ['ID', '技能ID', '用户ID', '技能名称', '状态', '原因', '执行时间(秒)', '处理时间'],
- $logs->map(function ($log) {
- return [
- $log->id,
- $log->active_skill_id,
- $log->user_id,
- $log->skill_name,
- $log->process_status,
- Str::limit($log->process_reason, 30),
- $log->execution_time,
- $log->processed_at->format('Y-m-d H:i:s'),
- ];
- })->toArray()
- );
- }
- // 4. 显示处理数据详情
- if ($logs->isNotEmpty()) {
- $this->info('4. 显示最新日志的处理数据详情');
- $latestLog = $logs->first();
-
- $this->line("日志ID: {$latestLog->id}");
- $this->line("技能名称: {$latestLog->skill_name}");
- $this->line("处理状态: {$latestLog->process_status}");
- $this->line("处理原因: {$latestLog->process_reason}");
- $this->line("执行时间: {$latestLog->execution_time}秒");
-
- if ($latestLog->process_data) {
- $this->line("处理数据:");
- $this->line(json_encode($latestLog->process_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
- }
- }
- // 5. 统计信息
- $this->info('5. 处理日志统计信息');
-
- $stats = PetSkillProcessLog::selectRaw('
- process_status,
- skill_name,
- COUNT(*) as count,
- AVG(execution_time) as avg_execution_time,
- MAX(execution_time) as max_execution_time
- ')
- ->where('processed_at', '>=', now()->subHour())
- ->groupBy('process_status', 'skill_name')
- ->get();
- if ($stats->isNotEmpty()) {
- $this->table(
- ['状态', '技能名称', '次数', '平均执行时间(秒)', '最大执行时间(秒)'],
- $stats->map(function ($stat) {
- return [
- $stat->process_status,
- $stat->skill_name,
- $stat->count,
- round($stat->avg_execution_time, 3),
- round($stat->max_execution_time, 3),
- ];
- })->toArray()
- );
- } else {
- $this->warn('最近1小时内没有处理日志');
- }
- $this->info('测试完成!');
- return 0;
- }
- }
|