<?php

namespace app\queue\redis;

use app\extra\douyin\Client;
use app\extra\life\OrderLife;
use app\model\saas\SaasOrderLife;
use app\model\saas\SaasOrderLog;
use app\model\saas\SaasStore;
use Webman\RedisQueue\Consumer;
use Webman\RedisQueue\Redis;

class SyncOrder implements Consumer
{


    public $queue = "sync-order";

    public $connection = "default";


    public function consume($data): bool
    {
        if (empty($data['order'])) return true;
        if (empty($data['openid'])) return true;
        $order = (new SaasOrderLife)->where("order_id",$data['order'])->findOrEmpty();
        if (!$order->isEmpty()) return true;
        $resp = (new Client)->config($this->getDyConfig())->token()->queryOrderStore($data['order']);
        if (empty($resp['data']['certificates'])) return true;
        echo getDateFull()."同步订单\n";
        print_r($data);
        echo json_encode($resp);
        $lifeOrder = $resp['data'];
        $orderData = [];
        foreach ($lifeOrder['certificates'] as $key=>$val) {
            $orderEx = (new SaasOrderLife)->where("order_id",$lifeOrder['order_id'])->where("certificate_id",$val['certificate_id'])->findOrEmpty();
            if ($orderEx->isEmpty()) {
                $store = (new SaasStore)->where("store_id",$val['sku_info']['account_id'])->findOrEmpty();
                $orderData[$key] = [
                    "open_id"   => $data['openid'],
                    "agent_id"  => $store['agent_id']??'',
                    "store_id"  => $store['store_id']??'',
                    "order_id"  => $lifeOrder['order_id'],
                    "pay_amount"    => $val['amount']['pay_amount'],
                    "order_amount"  => $val['amount']['original_amount'],
                    "expire_at"     => date("Y-m-d H:i:s",$val['expire_time']),
                    "out_id"        => $val['sku_info']['sku_id'],
                    "product_name"  => $val['sku_info']['title'],
                    "groupon_type"  => $val['sku_info']['groupon_type'],
                    "certificate_id"  => $val['certificate_id']??'',
                    "status"        => 1,
                    "pay_at"        => date("Y-m-d H:i:s",$data['pay_time']),
                    "start_time"    => date("Y-m-d H:i:s",$val['start_time']),
                ];
            } else {
                $orderEx->save([
                    "expire_at"     => date("Y-m-d H:i:s",$val['expire_time']),
                    "out_id"        => $val['sku_info']['sku_id'],
                    "product_name"  => $val['sku_info']['title'],
                    "groupon_type"  => $val['sku_info']['groupon_type'],
                    "certificate_id"  => $val['certificate_id']??'',
                    "start_time"    => date("Y-m-d H:i:s",$val['start_time']),
                ]);
            }
        }
        if (!empty($orderData)) {
            (new SaasOrderLife)->insertAll(array_values($orderData));
        }
        return true;
    }


    protected function getDyConfig(): array
    {
        return ["appid" => sConf('dy.appid'),'secret' => sConf('dy.secret')];
    }

}