Files
wticreatorstudio/Modules/StudioPlus/Imports/StudioPlusVideoOttImport.php
T
2024-03-18 01:22:25 -04:00

89 lines
2.9 KiB
PHP

<?php
namespace Modules\StudioPlus\Imports;
use Carbon\Carbon;
use Illuminate\Support\Str;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Concerns\ToCollection;
use Modules\StudioPlus\Entities\StudioPlusCategory;
use Modules\StudioPlus\Entities\StudioPlusVideo;
use Modules\Language\Entities\Language;
class StudioPlusVideoOttImport implements ToCollection
{
public function collection(Collection $rows)
{
try {
DB::beginTransaction();
$languages = Language::where('status',1)->get();
$columns = [];
$newRows = [];
foreach ($rows as $key => $row) {
if ($key === 0) {
$columns = $row->toArray();
continue;
}
foreach ($columns as $key => $column) {
$newRows[$column] = $row[$key];
}
if (!isset($newRows['ASIN'])) continue;
$video = StudioPlusVideo::where('asin', $newRows['ASIN'])->first();
if ($video && $video->in_ott == 0) {
foreach ($languages as $language) {
$ott = $video->otts()->create([
'locale' => $language->code,
'title' => $language->code == 'en' ? $newRows['Title Name EN'] : $newRows['Title Name ES'],
'description' => $language->code == 'en' ? $newRows['Description EN'] : $newRows['Description ES']
]);
$ott->links()->create([
'shop_name' => 'Amazon',
'link' => $newRows['AMAZON COMISSION LINK']
]);
}
$videoCategory = $this->firstOrCreateCategoryByName($newRows['Category Name'] ?? '');
$video->studio_plus_category_id = $videoCategory->id;
$video->in_ott = 1;
$video->update();
} else if ($video && $video->in_ott == 1) {
Log::info('Video ott already processed: '.$newRows['ASIN']);
Log::info($newRows);
} else {
Log::info('Video not found: '.$newRows['ASIN']);
Log::info($newRows);
}
}
DB::commit();
} catch (\Exception $ex) {
DB::rollBack();
logger($ex->getMessage());
}
}
private function firstOrCreateCategoryByName(string|null $name = ''): StudioPlusCategory
{
return StudioPlusCategory::query()
->firstOrCreate(
[
'name' => $name ?: 'N/A'
],
[
'user_id' => 1
]
);
}
}