<?php

namespace app\queue\redis;

use app\model\saas\SaasOrderDetail;
use Webman\Push\Api;
use Webman\RedisQueue\Consumer;

class PushPrint  implements Consumer
{

    public $queue = "push-print";

    public $connection = "default";


    public function consume($data): bool
    {
        try {
            if (empty($data['order'])) return true;
            if (empty($data['type'])) return true;
            $api = new Api('http://127.0.0.1:3232', config('plugin.webman.push.app.app_key'),config('plugin.webman.push.app.app_secret'));
            $printData = [];
            if ($data['type'] == 'order') { // 按订单
                $detail = (new SaasOrderDetail)->where(["order_sn" => $data['order'],'status' => 1])->select();
                if ($detail->isEmpty()) return true;
                foreach ($detail as $key=>$printJob) {
                    $range = Null;
                    if ($printJob['end_page'] > $printJob['start_page']) {
                        $range = $printJob['start_page']."-".$printJob['end_page'];
                    }
                    if ($printJob['end_page'] == $printJob['start_page']) {
//                        $range = (string) $printJob['end_page'];
                        $range = $printJob['start_page']."-".$printJob['end_page'];
                    }
                    $color_mode = ($printJob['color']==1?'color':'monochrome');
                    $duplex = "simplex";
                    if ($printJob['paper_size'] == 'A4' && $printJob['duplex'] == 2) {
                        $duplex = 'shortedge'; // duplexlong  longedge
                    }
                    if ($printJob['paper_size'] == 'A3' && $printJob['duplex'] == 2) {
                        $duplex = 'longedge'; // duplexshort  shortedge
                    }
                    $printData[$key] = [
                        "printerName"   => $printJob['print_name'],
                        "copies"        => $printJob['number'],
                        "landscape"     => false,
                        "paperSize"     => $printJob['paper_size'],
                        "pageRange"     => $range,
                        "duplex"        => $duplex,
                        "monochrome"    => $color_mode,
                        "colorMode"     => $color_mode,
                        "jobId"         => $printJob['id'],
                        "dpi"           => 300,
                        "action"        => 'create',
                        "scaleMode"     => 'fit',
                        "remoteUrl"     => "https://zhy-1355132020.cos.ap-guangzhou.myqcloud.com/".$printJob['path'],
                        "customFileName" => $printJob['order_sn']."_".$printJob['id'],
                    ];
                    (new SaasOrderDetail)->where("id",$printJob['id'])->update(['status' => 2]);
                }
                $printKey = "{$detail[0]['print_id']}-{$detail[0]['shop_id']}";
            } else { // 单笔打印
                $printJob = (new SaasOrderDetail)->where(["id" => $data['order'],'status' => 1])->findOrEmpty();
                if ($printJob->isEmpty()) return true;
                $range = Null;
                if ($printJob['end_page'] > $printJob['start_page']) {
                    $range = $printJob['start_page']."-".$printJob['end_page'];
                }
                if ($printJob['end_page'] == $printJob['start_page']) {
                    $range = (string) $printJob['end_page'];
                }
                $color_mode = ($printJob['color']==1?'color':'monochrome');
                $duplex = "simplex";
                if ($printJob['paper_size'] == 'A4' && $printJob['duplex'] == 2) {
                    $duplex = 'duplexlong'; // duplexlong
                }
                if ($printJob['paper_size'] == 'A3' && $printJob['duplex'] == 2) {
                    $duplex = 'duplexshort'; // duplexshort
                }
                $printData = [
                    [
                        "printerName"   => $printJob['print_name'],
                        "copies"        => $printJob['number'],
                        "landscape"     => false,
                        "paperSize"     => $printJob['paper_size'],
                        "pageRange"     => $range,
                        "duplex"        => $duplex,
                        "monochrome"    => $color_mode,
                        "colorMode"     => $color_mode,
                        "jobId"         => $printJob['id'],
                        "dpi"           => 300,
                        "action"        => 'create',
                        "scaleMode"     => 'fit',
                        "remoteUrl"     => "https://cdn-zhy.huiyinduo.cn/".$printJob['path'],
                        "customFileName" => $printJob['order_sn']."_".$printJob['id'],    
                    ]
                ];
                $printKey = "{$printJob['print_id']}-{$printJob['shop_id']}";
                (new SaasOrderDetail)->where("id",$printJob['id'])->update(['status' => 2]);
            }
            echo getDateFull()."==={$printKey}==打印任务\n";
            echo json_encode($printData);
            $api->trigger("client-{$printKey}",'message',[
                "type"  => "print",
                "data"  => $printData
            ]);
            return true;
        } catch (\Throwable $throwable) {
            return true;
        }
    }
}