<?php

namespace App\Jobs;

use App\Procurement;
use App\Services\ProcurementWeiXinSendMessageService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ProcurementConfirmInform implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $procurement;

    /**
     * Create a new job instance.
     *
     * @param Procurement $procurement
     */
    public function __construct(Procurement $procurement)
    {
        $this->procurement=$procurement;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $pro=$this->procurement->loadMissing(['ownerMaterial.material.supplier.user.userDetail','ownerMaterial.owner.customer']);
        $suppliers=$pro->ownerMaterial->material->supplier ?? false;
        if(!$suppliers) return ;
        $open_ids=[];
        foreach ($suppliers as $supplier) {
            foreach ($supplier->user as $user){
                $open_id = $user->userDetail->procurement_wechat_open_id ?? false;
                if($open_id)$open_ids[]=$open_id;
            }
        }
        $open_ids=array_unique($open_ids);
        foreach ($open_ids as $open_id){
            $param['touser'] =$open_id;
            $param['mp_template_msg'] =[
                "appid"=>config('weiXin.fuWuHao.appId'),
                "template_id"=>config('weiXin.fuWuHaoTemplate.order_confirm_inform'),
                "url"=>"http://weixin.qq.com/download",
                "miniprogram"=>[
                    "appid"=>config('weiXin.appId.appId'),
                    "pagepath"=>"pages/login/login"
                ],
                'data'=>[
                    'first' => [
                        "value"=>"尊敬的供应商，你好!",
                    ],
                    'keyword1' => [
                        "value"=>$pro->code ?? '',
                        "color"=>"#173177"
                    ],
                    'keyword2' => [
                        "value"=>$pro->ownerMaterial->owner->customer->company_name ?? '',
                        "color"=>"#173177"
                    ],
                    'remark' => [
                        "value"=>'请及时确认，谢谢。',
                    ],
                ]
            ];
            app(ProcurementWeiXinSendMessageService::class)->sendWenChantTemplate($param);
        }

    }



}
