SyncAlbumContent.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Console\Commands;
  3. use Carbon\Carbon;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Str;
  7. /**
  8. * 导入相册内容到产品表
  9. * php artisan sync:album-content
  10. */
  11. class SyncAlbumContent extends Command
  12. {
  13. /**
  14. * The name and signature of the console command.
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'sync:album-content';
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = 'Sync album content to product tables';
  25. /**
  26. * Execute the console command.
  27. *
  28. * @return int
  29. */
  30. public function handle()
  31. {
  32. $tableMapping = json_decode('[{"base_product_category":11,"album_path_id":21},{"base_product_category":12,"album_path_id":22},{"base_product_category":13,"album_path_id":31},{"base_product_category":14,"album_path_id":35},{"base_product_category":15,"album_path_id":51},{"base_product_category":16,"album_path_id":64},{"base_product_category":17,"album_path_id":74},{"base_product_category":18,"album_path_id":79},{"base_product_category":19,"album_path_id":83},{"base_product_category":20,"album_path_id":114},{"base_product_category":21,"album_path_id":123},{"base_product_category":22,"album_path_id":141},{"base_product_category":23,"album_path_id":142},{"base_product_category":24,"album_path_id":143},{"base_product_category":25,"album_path_id":145},{"base_product_category":26,"album_path_id":151},{"base_product_category":27,"album_path_id":153},{"base_product_category":28,"album_path_id":154}] ');
  33. foreach ($tableMapping as $mapping) {
  34. $mapping = (array)$mapping;
  35. $albumContents = DB::table('album_content')
  36. ->where('path_id', $mapping['album_path_id'])
  37. ->get();
  38. foreach ($albumContents as $albumContent) {
  39. // Insert or update base_product
  40. $baseProduct = DB::table('base_product')
  41. ->where('title', $albumContent->model)
  42. ->first();
  43. $detail = json_decode($albumContent->detail);
  44. $detail_cn = json_decode($albumContent->detail_cn);
  45. $content = "";
  46. if ($detail) {
  47. foreach ($detail as $key => $value) {
  48. $content .= '<img src="'. $value. '"">';
  49. }
  50. }
  51. if ($detail_cn) {
  52. foreach ($detail_cn as $key => $value) {
  53. $content .= '<img src="'. $value. '"">';
  54. }
  55. }
  56. $baseProductId = 0;
  57. if (!$baseProduct) {
  58. $baseProduct = DB::table('base_product')
  59. ->insertGetId([
  60. 'title' => $albumContent->model,
  61. 'category_id' => $mapping['base_product_category'],
  62. 'content' => $content,
  63. 'created_at' => Carbon::now(),
  64. 'updated_at' => Carbon::now(),
  65. ]);
  66. $baseProductId = $baseProduct;
  67. } else {
  68. DB::table('base_product')
  69. ->where('id', $baseProduct->id)
  70. ->update([
  71. 'title' => $albumContent->model,
  72. 'category_id' => $mapping['base_product_category'],
  73. 'content' => $content,
  74. 'updated_at' => Carbon::now(),
  75. ]);
  76. $baseProductId = $baseProduct->id;
  77. }
  78. // var_dump($baseProduct->id);
  79. // exit;
  80. // Insert base_product_image
  81. $photos = json_decode($albumContent->photo, true);
  82. if (is_array($photos)) {
  83. foreach ($photos as $photo) {
  84. DB::table('base_product_image')
  85. ->insert([
  86. 'product_id' => $baseProductId,
  87. 'image_url' => $photo,
  88. 'created_at' => Carbon::now(),
  89. 'updated_at' => Carbon::now(),
  90. ]);
  91. }
  92. }
  93. // Insert base_video
  94. $videos = json_decode($albumContent->video, true);
  95. if (is_array($videos)) {
  96. foreach ($videos as $video) {
  97. if (isset($video['video_src']) && $video['video_src'] !== '0' && !empty($video['video_src'])) {
  98. DB::table('base_video')
  99. ->insert([
  100. 'title' => $video['video_title'] ?? null,
  101. 'category_id' => 1,
  102. 'video_url' => $video['video_src'],
  103. 'cover_image' => $video['cover'] ?? null,
  104. 'enabled' => 1,
  105. 'created_at' => Carbon::now(),
  106. 'updated_at' => Carbon::now(),
  107. ]);
  108. }
  109. }
  110. }
  111. }
  112. }
  113. $this->info('Sync completed successfully.');
  114. return 0;
  115. }
  116. }