SyncAlbumContent.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. exit;
  34. $this->migrateAlbumLogToSiteAlbumLog();
  35. exit;
  36. // 任务1: 同步 cover 字段
  37. $albumPaths = DB::table('album_path')->get();
  38. foreach ($albumPaths as $albumPath) {
  39. DB::table('site_album_folder')
  40. ->where('title', $albumPath->name)
  41. ->update(['cover' => $albumPath->cover]);
  42. }
  43. $this->info('Cover data synced successfully.');
  44. // 任务2: 同步 album_content 数据到 site_album
  45. $siteAlbumFolders = DB::table('site_album_folder')->get();
  46. foreach ($siteAlbumFolders as $siteAlbumFolder) {
  47. $albumPath = DB::table('album_path')
  48. ->where('name', $siteAlbumFolder->title)
  49. ->first();
  50. if ($albumPath) {
  51. $albumContents = DB::table('album_content')
  52. ->where('path_id', $albumPath->id)
  53. ->get();
  54. foreach ($albumContents as $albumContent) {
  55. $cover = empty($albumContent->cover) ? [] : [$albumContent->cover];
  56. $cover = json_encode($cover);
  57. if (empty($albumContent->photo) == false) {
  58. $cover = $albumContent->photo;
  59. }
  60. $detail = empty($albumContent->detail) ? '[]' : $albumContent->detail;
  61. $detail_cn = empty($albumContent->detail_cn) ? '[]' : $albumContent->detail_cn;
  62. $poster = empty($albumContent->poster) ? '[]' : $albumContent->poster;
  63. $cert = empty($albumContent->cert) ? '[]' : $albumContent->cert;
  64. $pdf = empty($albumContent->pdf) ? '[]' : $albumContent->pdf;
  65. $video = empty($albumContent->video) ? '[]' : $albumContent->video;
  66. DB::table('site_album')->insert([
  67. 'folder_id' => $siteAlbumFolder->id,
  68. 'order' => 0, // 你可以根据需要设置排序
  69. 'title' => $albumContent->model,
  70. 'title_en' => $albumContent->model,
  71. 'model' => $albumContent->model,
  72. 'parameters' => null, // 你可以根据需要设置参数
  73. 'cover' => $cover,
  74. 'en_detail' => $detail,
  75. 'cn_detail' => $detail_cn,
  76. 'poster' => $poster,
  77. 'cert' => $cert,
  78. 'pdf' => $pdf,
  79. 'video' => $video,
  80. 'created_at' => now(),
  81. 'updated_at' => now(),
  82. 'enabled' => 1,
  83. ]);
  84. }
  85. }
  86. }
  87. $this->info('Sync completed successfully.');
  88. return 0;
  89. }
  90. function migrateAlbumLogToSiteAlbumLog()
  91. {
  92. // 获取所有album_log记录
  93. $albumLogs = DB::table('album_log')->get();
  94. $migrationData = [];
  95. foreach ($albumLogs as $log) {
  96. // 根据model获取site_album对应的id
  97. $siteAlbumId = DB::table('site_album')
  98. ->where('model', $log->model)
  99. ->value('id');
  100. if ($siteAlbumId) {
  101. $migrationData[] = [
  102. 'user_name' => $log->user_name,
  103. 'action' => $log->action,
  104. 'model' => $log->model,
  105. 'content' => $log->content,
  106. 'content_id' => $siteAlbumId,
  107. 'created_at' => $log->created_at,
  108. 'updated_at' => $log->updated_at
  109. ];
  110. }
  111. }
  112. // 批量插入数据
  113. if (!empty($migrationData)) {
  114. DB::table('site_album_log')->insert($migrationData);
  115. }
  116. return '迁移完成,共处理'.count($migrationData).'条记录';
  117. }
  118. }