<?php

namespace App\Console\Commands;

use Exception;
use App\Models\Lang;
use App\Models\ProductDescription;
use App\Models\NewsDescription;
use App\Models\CasesDescription;
use App\Models\DownloadDescription;
use App\Models\VideoDescription;
use App\Models\JobDescription;
use App\Models\PageDescription;
use App\Models\CaseCateDescription;
use App\Models\DownloadCateDescription;
use App\Models\JobCateDescription;
use App\Models\NewsCateDescription;
use App\Models\PageCateDescription;
use App\Models\ProductCateDescription;
use App\Models\VideoCateDescription;
use App\Models\BlockDescription;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class Initialization extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'initialization';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '初始化没有翻译的内容';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $langs = Lang::where('buy', 1)->where('slug', '!=', 'en')->get()->pluck('slug')->toArray();

        foreach ($langs as $key => $lang) {
            $this->copy($lang);
        }
    }

    /**
     * 填充没有翻译的数据
     */
    private function copy($lang)
    {
        // 同步产品
        $this->syncDescription(ProductDescription::class, 'product_id', $lang, ['title', 'summary', 'content', 'seo_title', 'seo_keywords', 'seo_description']);
        // 同步新闻
        $this->syncDescription(NewsDescription::class, 'news_id', $lang, ['title', 'summary', 'content', 'seo_title', 'seo_keywords', 'seo_description']);
        // 同步案例
        $this->syncDescription(CasesDescription::class, 'case_id', $lang, ['title', 'summary', 'content', 'seo_title', 'seo_keywords', 'seo_description']);
        // 同步下载中心
        $this->syncDescription(DownloadDescription::class, 'download_id', $lang, ['title', 'summary', 'content', 'seo_title', 'seo_keywords', 'seo_description']);
        // 同步视频中心
        $this->syncDescription(VideoDescription::class, 'video_id', $lang, ['title', 'summary', 'content', 'seo_title', 'seo_keywords', 'seo_description']);
        // 同步招聘
        $this->syncDescription(JobDescription::class, 'job_id', $lang, ['city', 'title', 'summary', 'content', 'seo_title', 'seo_keywords', 'seo_description']);
        // 同步自定义页面
        $this->syncDescription(PageDescription::class, 'page_id', $lang, ['title', 'summary', 'content', 'seo_title', 'seo_keywords', 'seo_description']);

        // 同步分类
        $this->syncDescription(CaseCateDescription::class, 'case_cid', $lang, ['cname', 'summary', 'seo_title', 'seo_keywords', 'seo_description']);
        $this->syncDescription(DownloadCateDescription::class, 'download_cid', $lang, ['cname', 'summary', 'seo_title', 'seo_keywords', 'seo_description']);
        $this->syncDescription(JobCateDescription::class, 'job_cid', $lang, ['cname', 'summary', 'seo_title', 'seo_keywords', 'seo_description']);
        $this->syncDescription(NewsCateDescription::class, 'news_cid', $lang, ['cname', 'summary', 'seo_title', 'seo_keywords', 'seo_description']);
        $this->syncDescription(PageCateDescription::class, 'page_cid', $lang, ['cname', 'summary']);
        $this->syncDescription(ProductCateDescription::class, 'product_cid', $lang, ['cname', 'summary', 'seo_title', 'seo_keywords', 'seo_description']);
        $this->syncDescription(VideoCateDescription::class, 'video_cid', $lang, ['cname', 'seo_title', 'seo_keywords', 'seo_description']);

        // block description
        $this->syncDescription(BlockDescription::class, 'block_id', $lang, ['title', 'content', 'extend']);
    }

    /**
     * 同步没有翻译的语言描述
     */
    private function syncDescription($model, $foreignKey, $lang, $fields)
    {
        try {
            DB::beginTransaction();

            $contents = $model::where('lang', 'en')->get();
            $name = $fields[0];

            foreach ($contents as $content) {
                $item = $model::where([$foreignKey => $content->$foreignKey, 'lang' => $lang])->first();

                if (!$item) {
                    $model = new $model;
                    $model->lang = $lang;
                    $model->$foreignKey= $content->$foreignKey;

                    foreach ($fields as $filed) {
                        $model->$filed = $content->$filed;
                    }
                    $model->save();
                    $this->info('insert'.$content->$foreignKey.$lang);
                } elseif ($item && empty($item->$name)) {
                    $this->info('copy'.$content->$foreignKey.$lang);
                    $data = [];
                    foreach ($fields as $filed) {
                        $data[$filed] = $content->$filed;
                    }
                    $model::where([$foreignKey => $content->$foreignKey, 'lang' => $lang])->update($data);
                }
            }

            DB::commit();
        } catch (Exception $e) {

            DB::rollBack();

            Log::error($e->getMessage());
        }

    }
}
