where('path_id', $mapping['album_path_id'])
->get();
foreach ($albumContents as $albumContent) {
// Insert or update base_product
$baseProduct = DB::table('base_product')
->where('title', $albumContent->model)
->first();
$detail = json_decode($albumContent->detail);
$detail_cn = json_decode($albumContent->detail_cn);
$content = "";
if ($detail) {
foreach ($detail as $key => $value) {
$content .= '
';
}
}
// if ($detail_cn) {
// foreach ($detail_cn as $key => $value) {
// $content .= '
';
// }
// }
$baseProductId = 0;
$sku = '';
if (strpos($albumContent->model, "MTB-") === 0) {
$sku = $albumContent->model;
}
if (!$baseProduct) {
$baseProduct = DB::table('base_product')
->insertGetId([
'title' => $albumContent->model,
'category_id' => $mapping['base_product_category'],
'sku' => $sku,
'content' => $content,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
'seo_title' => $albumContent->model,
]);
$baseProductId = $baseProduct;
} else {
DB::table('base_product')
->where('id', $baseProduct->id)
->update([
'title' => $albumContent->model,
'category_id' => $mapping['base_product_category'],
'sku' => $sku,
'content' => $content,
'updated_at' => Carbon::now(),
'seo_title' => $albumContent->model,
]);
$baseProductId = $baseProduct->id;
}
// var_dump($baseProduct->id);
// exit;
// Insert base_product_image
$photos = json_decode($albumContent->photo, true);
if (is_array($photos)) {
foreach ($photos as $photo) {
DB::table('base_product_image')
->insert([
'product_id' => $baseProductId,
'image_url' => $photo,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
]);
}
}
// Insert base_video
$videos = json_decode($albumContent->video, true);
if (is_array($videos)) {
foreach ($videos as $video) {
if (isset($video['video_src']) && $video['video_src'] !== '0' && !empty($video['video_src'])) {
DB::table('base_video')
->insert([
'title' => $video['video_title'] ?? null,
'category_id' => 1,
'video_url' => $video['video_src'],
'cover_image' => $video['cover'] ?? null,
'enabled' => 1,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
]);
}
}
}
}
}
$this->info('Sync completed successfully.');
return 0;
}
}