<?php

namespace app\queue\redis;

use app\extra\tools\PrintService;
use app\model\saas\SaasPrint;
use Webman\RedisQueue\Consumer;

class PrintState implements Consumer
{

    public $queue = "print-state";

    public $connection = "default";

    public function consume($data): bool
    {
        echo getDateFull()."===批量查询打印件状态\n";
        $print = (new SaasPrint)->select();
        $sn = [];
        if ($print->isEmpty()) return true;
        foreach ($print as $key => $value) {
            $sn[$key] = $value['sn'];
        }
        $resp = (new PrintService)->config(['appid' => sConf('dy.ex_appid'),'secret' => sConf('dy.ex_secret')])->deviceState($sn);
        if (!$resp['status']) return true;
        if (empty($resp['data'])) return true;
        foreach ($resp['data'] as $key => $val) {
            echo getDateFull()."===更新序列号=={$val['sn']}=={$val['status']}=={$val['deviceStatus']}\n";
            $state = (new SaasPrint)->where("sn",$val['sn'])->update(["online" => $val['status'],"device_state" => $val['deviceStatus']]);
            echo "更新状态==={$state}\n";
        }
        return true;
    }

}