<?php

namespace app\event;

use app\model\saas\SaasOrderDetail;
use Webman\RedisQueue\Redis;
use yzh52521\EasyHttp\Http;

class MergePdf
{

    public function imgMergePdf(array $data = [])
    {
        if (empty($data['shop'])) return true;
        $detail = (new SaasOrderDetail)->where(["shop_id" => $data['shop'],"openid" => $data['openid'],"order_sn" => $data['order'],'status' => 0])->select();
        if ($detail->isEmpty()) return true;
        foreach ($detail as $key=>$val) {
            if ($val['extension'] !== 'pdf') {
                $pathUri = [];
                for ($i = $val['start_page']; $i <= $val['end_page']; $i ++) {
//                for ($i = 1; $i <= $val['total_page']; $i ++) {
                    $pathUri[$i] = "https://".sConf("storage.cos_http_domain")."/".$val['path']."?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"   => $data['order']."_".$val['id'],
                    "options"   => [
                        "compression"   => false,
                        "quality"       => 100
                    ]
                ])->array();
                echo getDateFull()."==={$data['order']}===word转码\n";
                print_r($resp);
                if ($resp['success']) {
                    (new SaasOrderDetail)->where("id",$val['id'])->update(['path' => $resp['data']['pdfUrl'],'old_path' => $val['path'], 'status' => 1]);
                    // 推送打印
                    Redis::send("push-print",['type' => 'id' , 'order' => $val['id']]);
                }
            }
        }
    }

}