<?php

namespace app\queue\redis;

use app\extra\tools\CodeExtend;
use app\model\saas\SaasWordChange;
use Webman\RedisQueue\Consumer;
use yzh52521\EasyHttp\Http;

class WordChange  implements Consumer
{

    public $queue = "word-change";

    public $connection = "default";

    public function consume($data): bool
    {
        print_r($data);
        $change = (new SaasWordChange)->where("key",md5($data['key']))->findOrEmpty();
        if ($change->isEmpty()) return true;
        if ($change['status'] == 1) return true;
        if ($data['ext'] !== 'pdf') {
            $pathUri = [];
            for ($i = 1; $i <= $data['total']; $i ++) {
                $pathUri[$i] = "https://".sConf("storage.cos_http_domain")."/".$data['key']."?ci-process=doc-preview&dstType=png&imageDpi=300&page={$i}";
            }
            $resp = Http::asJson()->withHeaders(['x-api-key' => "your-api-key-here"])->post("http://127.0.0.1:3000/api/images-to-pdf",[
                "imageUrls" => array_values($pathUri),
                "pdfName"   => "upload_".CodeExtend::uniqidDate(18),
                "options"   => [
                    "compression"   => false,
                    "quality"       => 100
                ]
            ])->array();
            echo getDateFull()."==={$data['key']}===word转码\n";
            print_r($resp);
            $path = $resp['data']['pdfUrl'];
            $change->status = 1;
            $change->path = $path;
            $change->save();
        }
        return true;
    }

}