<?php

namespace app\command;

use app\common\repositories\movie\MovieRepository;
use crmeb\services\ApiResponseService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;

class MovieFilmCommand extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('movie-film')->setDescription('电影-获取影片列表');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doMovieFilm();
    }

    /**
     * 电影-获取影片列表
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function doMovieFilm()
    {
        $page = 1;
        $limit = 100;

        /** @var MovieRepository $movieRepository */
        $movieRepository = app()->make(MovieRepository::class);
        $filmLists = $movieRepository->getFilm($page, $limit);

        if (isset($filmLists['code']) && ($filmLists['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE)) {
            $this->createMovieFilm($filmLists['data']['list']);

            $data = $filmLists['data'];
            $totalPages = $data['paging']['pages'] ?? 0;
            for ($index = ($page + 1); $index <= $totalPages; $index++) {
                $filmLists = $movieRepository->getFilm($index, $limit);
                if (isset($filmLists['code']) && ($filmLists['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE)) {
                    $this->createMovieFilm($filmLists['data']['list']);
                }
            }
        }
    }

    /**
     * 电影-获取影片列表
     *
     * @param $list
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function createMovieFilm($list)
    {
        $film_signList = array_column($list, 'film_sign');

        $localFilm_nameLists = Db::name('movie_film')->whereIn('film_sign', $film_signList)->field('id,film_sign,film_name,publish_date')->select();
        $localFilm_nameLists = !empty($localFilm_nameLists) ? array_reduce($localFilm_nameLists->toArray(), function ($carry, $item) {
            $carry[$item['film_sign']] = [
                'id' => $item['id'], 'film_sign' => $item['film_sign'], 'film_name' => $item['film_name'], 'publish_date' => $item['publish_date']
            ];
            return $carry;
        }, []) : [];
        $time = strtotime(date('Y-m-d'));

        foreach ($list as $key => $value) {
            $publish_date = strtotime($value['publish_date']);
            $params = [
                'film_sign' => $value['film_sign'],
                'film_name' => $value['film_name'],
                'publish_date' => $publish_date,
                'language' => trim($value['language']),
                'type' => $value['type'],
                'duration' => $value['duration'],
                'version' => $value['version'],
                'grade' => $value['grade'],
                'hots' => $value['hots'],
                'cover_img' => $value['cover_img'],
                'video' => $value['video'],
                'detail_img' => json_encode($value['detail_img'], JSON_FORCE_OBJECT),
                'director' => json_encode($value['director'], JSON_FORCE_OBJECT),
                'actors' => json_encode($value['actors'], JSON_FORCE_OBJECT),
                'intro' => $value['intro'],
                'is_show' => intval(($publish_date <= $time))
            ];
            if (!isset($localFilm_nameLists[$value['film_sign']])) {
                $params['create_time'] = time();
                Db::name('movie_film')->insert($params);
            } else {
                $localFilm_name = $localFilm_nameLists[$value['film_sign']];
                if (md5($value['film_sign'] . $value['film_name'] . $value['publish_date'])
                    != md5($localFilm_name['film_sign'] . $localFilm_name['film_name'] . $localFilm_name['publish_date'])) {
                    $params['update_time'] = time();
                    Db::name('movie_film')->where('id', $localFilm_name['id'])->update($params);
                }
            }
        }
    }
}