<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

/**
 * 导入相册内容到产品表
 * 先在log中复制$tableMapping到这里,然后运行命令
 * php artisan sync:album-content
 */
class SyncAlbumContent extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sync:album-content';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Sync album content to product tables';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        exit;
        $this->migrateAlbumLogToSiteAlbumLog();
        exit;
        // 任务1: 同步 cover 字段
        $albumPaths = DB::table('album_path')->get();

        foreach ($albumPaths as $albumPath) {
            DB::table('site_album_folder')
                ->where('title', $albumPath->name)
                ->update(['cover' => $albumPath->cover]);
        }

        $this->info('Cover data synced successfully.');

        // 任务2: 同步 album_content 数据到 site_album
        $siteAlbumFolders = DB::table('site_album_folder')->get();

        foreach ($siteAlbumFolders as $siteAlbumFolder) {
            $albumPath = DB::table('album_path')
                ->where('name', $siteAlbumFolder->title)
                ->first();

            if ($albumPath) {
                $albumContents = DB::table('album_content')
                    ->where('path_id', $albumPath->id)
                    ->get();

                foreach ($albumContents as $albumContent) {
                    $cover = empty($albumContent->cover) ? [] : [$albumContent->cover];
                    $cover = json_encode($cover);
                    if (empty($albumContent->photo) == false) {
                        $cover = $albumContent->photo;
                    }
                    $detail = empty($albumContent->detail) ? '[]' : $albumContent->detail;
                    $detail_cn = empty($albumContent->detail_cn) ? '[]' : $albumContent->detail_cn;
                    $poster = empty($albumContent->poster) ? '[]' : $albumContent->poster;
                    $cert = empty($albumContent->cert) ? '[]' : $albumContent->cert;
                    $pdf = empty($albumContent->pdf) ? '[]' : $albumContent->pdf;
                    $video = empty($albumContent->video) ? '[]' : $albumContent->video;

                    DB::table('site_album')->insert([
                        'folder_id' => $siteAlbumFolder->id,
                        'order' => 0, // 你可以根据需要设置排序
                        'title' => $albumContent->model,
                        'title_en' => $albumContent->model,
                        'model' => $albumContent->model,
                        'parameters' => null, // 你可以根据需要设置参数
                        'cover' => $cover,
                        'en_detail' => $detail,
                        'cn_detail' => $detail_cn,
                        'poster' => $poster,
                        'cert' => $cert,
                        'pdf' => $pdf,
                        'video' => $video,
                        'created_at' => now(),
                        'updated_at' => now(),
                        'enabled' => 1,
                    ]);
                }
            }
        }

        $this->info('Sync completed successfully.');
        return 0;
    }



    function migrateAlbumLogToSiteAlbumLog()
    {
        // 获取所有album_log记录
        $albumLogs = DB::table('album_log')->get();

        $migrationData = [];
        foreach ($albumLogs as $log) {
            // 根据model获取site_album对应的id
            $siteAlbumId = DB::table('site_album')
                ->where('model', $log->model)
                ->value('id');

            if ($siteAlbumId) {
                $migrationData[] = [
                    'user_name'   => $log->user_name,
                    'action'      => $log->action,
                    'model'       => $log->model,
                    'content'     => $log->content,
                    'content_id'  => $siteAlbumId,
                    'created_at'  => $log->created_at,
                    'updated_at'  => $log->updated_at
                ];
            }
        }

        // 批量插入数据
        if (!empty($migrationData)) {
            DB::table('site_album_log')->insert($migrationData);
        }

        return '迁移完成，共处理'.count($migrationData).'条记录';
    }
}
