<?php

namespace App\Listeners;

use App\Events\CommissionGotEvent;
use App\Events\CommissionUnfreezeComplete;
use App\Events\OrderCommissionSetedEvent;
use App\Jobs\SendTemplateNotice;
use App\Models\Wechat;
use EasyWeChat\Kernel\HttpClient\RequestUtil;
use Illuminate\Support\Facades\Log;

/*发 佣金通知 给合伙人*/
class SendCommissionNoticeToPartnerListener
{
    public function __construct()
    {
    }

    /**
     * @param OrderCommissionSetedEvent|CommissionGotEvent|CommissionUnfreezeComplete $event
     * @return void
     */
    public function handle(OrderCommissionSetedEvent|CommissionGotEvent|CommissionUnfreezeComplete $event): void
    {
        // 获取订单和合伙人信息
        $order_commission = $event->order_commission;
        $user_id = $order_commission->user_id;

        $openid = Wechat::query()->where('user_id', $user_id)->where('type', 'official')->value('openid');

        //准备消息
        $domain = config('app.url');
        $param = [
            'touser' => $openid,
            'template_id' => $event->template_id,
            'url' => $domain . "h5/#/profitRecord",
            'data' => [
                "keyword1" => ["value" => $event->order_commission->main_order->name],
                "keyword2" => ["value" => $event->keyword2],
                "keyword3" => ["value" => date('Y-m-d H:i:s')],
            ]
        ];
        Log::info($param);
        if (config('app.env') == 'production') {
            SendTemplateNotice::dispatch($param)->onQueue('health');
        } else {
            SendTemplateNotice::dispatch($param);
        }

    }
}
