<?php


namespace App\Http\Controllers\Api;


use App\Facade\TableName;
use App\Http\helper\HttpCurl;
use App\Http\helper\NumConfig;
use App\Models\GamePhoneVerityCode;
use App\Models\PrivateMail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class AccCallbackController
{
    private $plantsConfig;
    // 验证码查询
    public function send()
    {
        $lastid=0;
        $key="taskCallbackrun_013";
        if(Redis::exists($key)){
            $lastid=Redis::get($key);
        }
       $list=DB::table("QPRecordDB.dbo.AccountsCallbackRecords")
           ->where("State",1)
           ->where("ID",'>',$lastid)
           ->where('PhoneNum','>',0)
           ->orderBy('ID')
            ->limit(100)->get();

        foreach ($list as $item) {
            if($item->ID>$lastid)$lastid=$item->ID;
            DB::connection("write")->table("QPRecordDB.dbo.AccountsCallbackRecords")
                ->where('ID',$item->ID)
                ->update(['State'=>9]);
        }
        Redis::set($key,$lastid);


//        $this->plantsConfig = config('InterfaceAPI.plant_message');
        $this->plantsConfig = config('InterfaceAPI.plant_message_notify');

        foreach ($list as $item) {
            $res=$this->plants_send($item->PhoneNum,$item->Msg);
            DB::connection("write")->table("QPRecordDB.dbo.AccountsCallbackRecords")
                ->where('ID',$item->ID)
                ->update(['State'=>$res?2:0,'SendDate'=> date('Y-m-d H:i:s',time())]);
        }
//            $record = [
//                'UserID' => $item->UserID,
//                'Channel' => $item->Channel,
//                'Msg' => $msg,
//                'PhoneNum' => $item->PhoneNum,
//                'Reason' => $reason,
//                'Recharge' => $pay,
//                'Profit' => $pay - $item->Withdraw / 100,
//                'State' => 1,
//                'LeftCoin' => $item->Score,
//                'Bonus' => $bonus,
//                'TrackToken' => $this->from10_to62($lastid),
////'SendDate'=>$item->,
////'Result'=>$item->,
//            ];

    }

    public function plants_send($phone,  $Content)
    {

        $accesskey = $this->plantsConfig['appKey'];
        $secret = $this->plantsConfig['secretKey'];
        $time = time();


        $data = [
            'appkey' => $accesskey,
            'secretkey' => $secret,
            'phone' => '55' . $phone,
            'content' => $Content,
        ];


        $build = http_build_query($data);
        Log::info('plants发送回call ' . $build);
        $gateway = $this->plantsConfig['gateway'] ;

//        $header = ['sign' => $sign, 'Timestamp' =>$time, 'Api-Key' => $appKey];
//        Log::info('header-签名 ' . json_encode($header)."###".$appKey .'-'. $password .'-'. $time);
        $HttpCurl = new HttpCurl();
        $res = $HttpCurl->curlPost($gateway, $build);

        Log::info('plants发送回call结果：' . $res);

        $data = \GuzzleHttp\json_decode($res, true);
        if (isset($data['code']) && $data['code'] == 0){

            return true;
        }

        return false;

    }
}
