<?php
/**
 * @link https://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license https://www.yiiframework.com/license/
 */

namespace app\commands;

use app\common\FileHelper;
use app\common\SaltHelper;
use app\enums\SourceEnum;
use app\models\Brand;
use app\models\BrandAlias;
use app\models\BrandRunwayImages;
use app\models\BrandSource;
use app\models\logics\commands\SpiderVogue;
use app\models\User;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use yii\console\Controller;
use yii\console\ExitCode;

/**
 * This command echoes the first argument that you have entered.
 *
 * This command is provided as an example for you to learn how to create console commands.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class HelloController extends Controller
{
    /**
     * This command echoes what you have entered as the message.
     * @param string $message the message to be echoed.
     * @return int Exit code
     */
    public function actionIndex($message = 'hello world')
    {

        foreach(BrandRunwayImages::find()->each() as $item) {
            var_dump($item['image']);
            $cont = file_get_contents($item['image']);
            $imageName = md5(SaltHelper::addSalt($item['image']));
            $imagePath = md5(SaltHelper::addSalt($item['runway_id']));

            if (!is_dir(\Yii::getAlias('@runtime') . "/{$imagePath}")) {
                FileHelper::createDirectory(\Yii::getAlias('@runtime') . "/{$imagePath}");
            }

            file_put_contents(\Yii::getAlias('@runtime') . "/{$imagePath}/{$imageName}", $cont);
            $accessKey = '7GLHrN7BqrI9JnWZ9Pki2q5rqPazhIFroo19a-Av';
            $secretKey = 'gmg6PLgg666Cme-gsyTlsBLhshDv-6_zsEmW4jRY';
            $auth = new Auth($accessKey, $secretKey);
            $bucket = '23cm';
// 生成上传Token
            $token = $auth->uploadToken($bucket);
            // 要上传文件的本地路径
            $filePath = \Yii::getAlias('@runtime') . "/{$imagePath}/{$imageName}";
// 上传到存储后保存的文件名
            $key = "{$imagePath}/{$imageName}";
// 初始化 UploadManager 对象并进行文件的上传。
            $uploadMgr = new UploadManager();
// 调用 UploadManager 的 putFile 方法进行文件的上传。
            list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath, null, 'application/octet-stream', true, null, 'v2');
            echo "\n====> putFile result: \n";
            if ($err !== null) {
                var_dump($err);
            } else {
                var_dump($ret);
            }
// 构建 UploadManager 对象
//            $uploadMgr = new UploadManager();
            $qu = BrandRunwayImages::findOne($item['id']);
            $qu->name = $key;
            $qu->save();
//            die;
        }
        die;
        foreach(BrandSource::find()->each() as $item) {
            $brandName = Brand::find()->where(['id' => $item['brand_id']])->one()->name;
            $brandName = strtr($brandName, [
                ' ' => '-',
                '.' => '-'
            ]);

            var_dump(strtolower($brandName));
            $q = BrandSource::findOne($item['id']);
            $q->source_url = '/fashion-shows/designer/'.strtolower($brandName);
            $q->save();
        }
        die;
        $model = new SpiderVogue();
        $model->setBrandName('gucci');
        var_dump($model->start());
        die;
        ini_set('memory_limit', '1024M');

        $content = file_get_contents(\Yii::$app->basePath . '/brand.txt');
        var_dump($content);
        $content = (json_decode($content, true));
//        var_dump(json_last_error());
//        var_dump(json_last_error_msg());

//        var_dump($content);die;
        foreach ($content as $item) {
            $model = new Brand();
            if (stripos($item['name'], '/') !== false) {
                $alias = explode('/', $item['name']);
                foreach ($alias as $i => $aliasItem) {
                    if ($i == 0) {
                        $model->name = $aliasItem;
                        $model->show_name = $item['name'];
                        $model->save();
                        $lastId = $model->id;
                        var_dump($model->errors);
                    } else {
                        $aliasModel = new BrandAlias();
                        $aliasModel->brand_id = $lastId;
                        $aliasModel->name = $aliasItem;
                        $aliasModel->save();
                        var_dump($aliasModel->errors);
                    }
                }
            } else {
                $model->name = $item['name'];
                $model->show_name = $item['name'];
                $model->save();
//                var_dump($model->errors);
            }
        }

        return ExitCode::OK;
    }

}
