/** * Process file import data into application data * * @return void */ private function processFileImport() { // Disable query log to conserve on memory DB::connection()->disableQueryLog(); // Find first pending file import and eager load the csvRows $this->csv_import = CsvImport::where('status', 'pending')->with('csvRows')->first(); if (!$this->csv_import) { printf("%s \n", "Nothing to process."); exit; } // Change the status of the import to avoid it being processed again. $this->csv_import->status = 'processing'; $this->csv_import->save(); if (count($this->csv_import->fileImportContent)) { // Get the header from the import $header = $this->csv_import->fileImportContent->first()->header; // Flip the values to keys and explode into array $header = array_flip(explode(',', $header)); foreach ($this->csv_import->fileImportContent as $row) { // Convert the raw row data into array $row_array = explode(',', $row->content); // Build array of import properties $import_array = $header; // Match data array to the header array foreach ($import_array as $index => $key) { $import_array[$index] = $row_array[$key]; } /** * Here we can perform additional validation * for each data field and save it to the database */ // Build array of parameters for our user import $user = User::firstOrCreate(['first_name' => $import_array['first_name'], 'last_name' => $import_array['last_name'], 'email' => $import_array['email']]); $movies = Movies::firstOrCreate(['user_id' => $user->id, 'movie_list' => $import_array['movies']]); $music = Music::firstOrCreate(['user_id' => $user->id, 'music_list' => $import_array['music']]); } $this->csv_import->status = 'processed'; $this->csv_import->save(); } else { $this->csv_import->status = 'error'; $this->csv_import->save(); //Log error exit; } }
/** * Execute the console command. * * @return mixed */ public function handle() { $list = []; $n = 0; DB::table('tmp_musics')->chunk(10000, function ($data) use(&$list, &$n) { foreach ($data as $record) { ++$n; // 合并整段记录 $key = implode('_', [$record->audio_part, $record->acrid]); if (empty($list) or isset($list[$key])) { $list[$key][] = $record; if (!empty($record->acrid)) { continue; } // 插入数据 } else { // 插入歌手记录 $list = current($list); $first = current($list); $end = end($list); $artistIds = []; foreach (explode('|', $first->artists) as $name) { $artist = Artist::firstOrCreate(['name' => $name]); $artist->increment('counts', 1); $artistIds[] = $artist->id; } // 插入音乐记录 $music = Music::firstOrCreate(['title' => $first->title, 'album' => $first->album, 'genres' => $first->genres, 'label' => $first->label, 'release_date' => $first->release_date, 'acrid' => $first->acrid, 'isrc' => $first->isrc, 'upc' => $first->upc, 'external_metadata' => $first->external_metadata]); // 插入音乐歌手记录 $music->artists()->sync($artistIds); // 插入节目音乐记录 Program::where('date', $first->program_date)->first()->musics()->attach($music->id, ['program_part' => $first->audio_part, 'start_sec' => $first->audio_start_sec, 'end_sec' => $end->audio_start_sec, 'url' => self::getQiniuUrl($first->program_date, $first->audio_start_sec, $end->audio_start_sec)]); // 插入节目歌手记录 foreach ($music->artists as $artist) { Program::where('date', $first->program_date)->first()->artists()->attach($artist->id); } // 输出日志 $this->info(implode("\t", [$n, $music->id, $first->artists, $first->title])); } // 清空列表 $list = []; } }); }