<?php

namespace app\wss;
use app\model\saas\SaasOrderDetail;
use Workerman\Connection\TcpConnection;

class PrintWss
{
    public function onConnect(TcpConnection $connection)
    {
//        echo "onConnect-111\n";
    }

    public function onWebSocketConnect(TcpConnection $connection, $http_buffer)
    {
//        echo "onWebSocketConnect\n";
    }

    public function onMessage(TcpConnection $connection, $data)
    {
        if (empty($data)) $connection->send(['type' => 'empty','data' => []]);
        $param = json_decode($data,true);
        if ($param['type'] == "heartbeat") {
            $version = sConf("service.version");
            $data = [
                "type"      => "update",
                "version"   => $version?:'1.0.1',
                "del"       => 0
            ];
        }
        if ($param['type'] == 'get_data') {
            $printer = explode("-",$param['printer']);
            echo getDateFull()."===wss参数\n";
            print_r($printer);
            $printJob = (new SaasOrderDetail)->where("shop_id",$printer[1])->where("print_id",$printer[0])->where("status",1)->findOrEmpty();
            if (!$printJob->isEmpty()) {
                $range = Null;
                if ($printJob['end_page'] > $printJob['start_page']) {
                    $range = $printJob['start_page']."-".$printJob['end_page'];
                }
                if ($printJob['end_page'] == $printJob['start_page']) {
                    $range = (int) $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
                }
                $data = [
                    "type"  => "print",
                    "data"  => [
                        "jobId"     => $printJob['id'],
                        "action"    => 'create',
                        "remoteUrl" => "https://cdn-zhy.huiyinduo.cn/".$printJob['path'],
//                        "remoteUrl" => "https://".sConf("storage.cos_http_domain")."/".$printJob['path'],
                        "customFileName" => $printJob['order_sn']."_".$printJob['id'],
//                        "exe"       => ['-print-to',$printJob['print_name'],'-print-settings',"{$range},{$printJob['number']}x,{$color_mode},$duplex,paper={$printJob['paper_size']}",'-silent'],
                        "exe"       => ['-print-to',$printJob['print_name'],'-print-settings',"{$range},{$printJob['number']}x,{$color_mode},$duplex,fit,paper={$printJob['paper_size']}",'-silent'],
                    ]
                ];
                $printJob->status = 2;
                $printJob->save();
            } else {
                $data = [];
            }
        }
        $connection->send($data);
    }

    public function onClose(TcpConnection $connection)
    {
//        echo "onClose-2222\n";
    }

}