SyncAlbumContent.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. * 先在log中复制$tableMapping到这里,然后运行命令
  10. * php artisan sync:album-content
  11. */
  12. class SyncAlbumContent extends Command
  13. {
  14. /**
  15. * The name and signature of the console command.
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'sync:album-content';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = 'Sync album content to product tables';
  26. /**
  27. * Execute the console command.
  28. *
  29. * @return int
  30. */
  31. public function handle()
  32. {
  33. // 任务1: 同步 cover 字段
  34. $albumPaths = DB::table('album_path')->get();
  35. foreach ($albumPaths as $albumPath) {
  36. DB::table('site_album_folder')
  37. ->where('title', $albumPath->name)
  38. ->update(['cover' => $albumPath->cover]);
  39. }
  40. $this->info('Cover data synced successfully.');
  41. // 任务2: 同步 album_content 数据到 site_album
  42. $siteAlbumFolders = DB::table('site_album_folder')->get();
  43. foreach ($siteAlbumFolders as $siteAlbumFolder) {
  44. $albumPath = DB::table('album_path')
  45. ->where('name', $siteAlbumFolder->title)
  46. ->first();
  47. if ($albumPath) {
  48. $albumContents = DB::table('album_content')
  49. ->where('path_id', $albumPath->id)
  50. ->get();
  51. foreach ($albumContents as $albumContent) {
  52. $cover = empty($albumContent->cover) ? [] : [$albumContent->cover];
  53. $cover = json_encode($cover);
  54. if (empty($albumContent->photo) == false) {
  55. $cover = $albumContent->photo;
  56. }
  57. $detail = empty($albumContent->detail) ? '[]' : $albumContent->detail;
  58. $detail_cn = empty($albumContent->detail_cn) ? '[]' : $albumContent->detail_cn;
  59. $poster = empty($albumContent->poster) ? '[]' : $albumContent->poster;
  60. $cert = empty($albumContent->cert) ? '[]' : $albumContent->cert;
  61. $pdf = empty($albumContent->pdf) ? '[]' : $albumContent->pdf;
  62. $video = empty($albumContent->video) ? '[]' : $albumContent->video;
  63. DB::table('site_album')->insert([
  64. 'folder_id' => $siteAlbumFolder->id,
  65. 'order' => 0, // 你可以根据需要设置排序
  66. 'title' => $albumContent->model,
  67. 'title_en' => $albumContent->model,
  68. 'model' => $albumContent->model,
  69. 'parameters' => null, // 你可以根据需要设置参数
  70. 'cover' => $cover,
  71. 'en_detail' => $detail,
  72. 'cn_detail' => $detail_cn,
  73. 'poster' => $poster,
  74. 'cert' => $cert,
  75. 'pdf' => $pdf,
  76. 'video' => $video,
  77. 'created_at' => now(),
  78. 'updated_at' => now(),
  79. 'enabled' => 1,
  80. ]);
  81. }
  82. }
  83. }
  84. $this->info('Sync completed successfully.');
  85. return 0;
  86. }
  87. }