option('count'); $this->info("开始生成{$count}条测试数据..."); // 确保存在基础设备和员工声纹 $this->createBaseData(); // 生成音频记录 $devices = IntercomDevice::all(); $voiceprints = EmployeeVoiceprint::all()->pluck('emp_id')->toArray(); $voiceprints[] = null; // 加入空值,模拟未匹配声纹的情况 for ($i = 0; $i < $count; $i++) { $device = $devices->random(); $date = Carbon::now()->subDays(rand(0, 7))->subMinutes(rand(1, 1440)); $duration = rand(3, 15); // 音频时长3-15秒 $dialog = $this->dialogs[rand(0, count($this->dialogs) - 1)]; $voiceprintId = $voiceprints[rand(0, count($voiceprints) - 1)]; // 生成模拟音频文件路径 $fileName = $device->store_id . '_' . $device->device_sn . '_' . $date->timestamp . '_' . str_random(6) . '.mp3'; $filePath = 'uploads/audio/' . $fileName; AudioRecord::create([ 'device_id' => $device->id, 'audio_url' => $filePath, 'audio_text' => $dialog, 'duration' => $duration, 'voiceprint_id' => $voiceprintId, 'created_at' => $date, 'updated_at' => $date ]); // 更新设备最后上传时间 if (rand(0, 1)) { // 随机更新在线设备的最后上传时间 $device->update([ 'last_upload_time' => $date, 'status' => rand(0, 1) // 随机在线/离线状态 ]); } } $this->info("测试数据生成完成!共生成{$count}条音频记录。"); return Command::SUCCESS; } // 创建基础设备和员工声纹数据 private function createBaseData() { // 生成3个门店的5台设备 if (IntercomDevice::count() == 0) { $devices = [ ['device_sn' => 'Z418_001', 'store_id' => 1, 'status' => 1], ['device_sn' => 'Z418_002', 'store_id' => 1, 'status' => 1], ['device_sn' => 'Z418_003', 'store_id' => 2, 'status' => 0], ['device_sn' => 'Z418_004', 'store_id' => 3, 'status' => 1], ['device_sn' => 'Z418_005', 'store_id' => 3, 'status' => 1] ]; foreach ($devices as $dev) { IntercomDevice::create($dev + [ 'last_upload_time' => Carbon::now()->subMinutes(rand(10, 120)), 'created_at' => Carbon::now() ]); } } // 生成4个员工声纹 if (EmployeeVoiceprint::count() == 0) { $voiceprints = [ ['emp_id' => 1001, 'voiceprint_feature' => 'vp_' . rand(1000000000, 9999999999), 'store_id' => 1], ['emp_id' => 1002, 'voiceprint_feature' => 'vp_' . rand(1000000000, 9999999999), 'store_id' => 1], ['emp_id' => 1003, 'voiceprint_feature' => 'vp_' . rand(1000000000, 9999999999), 'store_id' => 2], ['emp_id' => 1004, 'voiceprint_feature' => 'vp_' . rand(1000000000, 9999999999), 'store_id' => 3] ]; foreach ($voiceprints as $vp) { EmployeeVoiceprint::create($vp); } } } }