<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/12/21
 * Time: 下午2:12
 */

namespace App\Console\Commands;

use App\Libs\PayHelper;
use App\Modules\Finance\Services\LiquidatedStatService;
use App\Modules\Finance\Services\PaymentService;
use App\Modules\Finance\Services\WithdrawCashService;
use Log;
use Illuminate\Console\Command;

class PaymentQueryTask extends Command
{

    /**
     * 执行命令   php artisan payment_query_task
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'payment_query_task';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '支付通道打款结果查询';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        print_r("======支付通道打款结果查询 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
        Log::info("======支付通道打款结果查询 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));

        //自动打款中 状态
        $paymentList = PaymentService::getPaymentList('','', '', WithdrawCashService::getWithdrawCashStatusStrList(21), true);

//        print_r(json_encode($paymentList));

        if(!empty($paymentList)) {
            foreach ($paymentList as $payment) {
                $payType = $payment['pay_merchant_source'];
                $transactionSerialNumber = $payment['trade_no'];
                $reqSn = date('YmdHis').mt_rand(100000,999999);
                if(!empty($payType) && !empty($transactionSerialNumber)) {
                    $resultQueryData = PayHelper::payQuery($payType, $transactionSerialNumber, $reqSn);

                    $resultDataCode = '';
                    $resultDataMsg = '';
                    $resultDataHttpDataMsg = json_encode($resultQueryData['http_result_data']);

                    if(!empty($resultQueryData) && !empty($resultQueryData['msg'])) {
                        $resultDataMsg = $resultQueryData['msg'];
                    }
                    if(!empty($resultQueryData)) {
                        $resultDataCode = $resultQueryData['code'];
                    }

//                    print_r($resultQueryData);

                    if($resultDataCode == 1) {
                        WithdrawCashService::updateWithdrawCashStatus($payment['withdraw_cash_id'], '', 31, '', '');
                        PaymentService::updatePaymentStatus($payment['id'], WithdrawCashService::getWithdrawCashStatusStr(31), $resultDataHttpDataMsg, $resultDataMsg);
                    } else if($resultDataCode == 2) {
                        PaymentService::updatePaymentStatus($payment['id'], '', $resultDataHttpDataMsg, $resultDataMsg);
                    } else {
                        PaymentService::updatePaymentStatus($payment['id'], WithdrawCashService::getWithdrawCashStatusStr(41), $resultDataHttpDataMsg, $resultDataMsg);
                        WithdrawCashService::updateWithdrawCashStatus($payment['withdraw_cash_id'], '', 41, '', '');
                    }
                }
            }
        }

        Log::info("======支付通道打款结果查询 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));
        print_r("======支付通道打款结果查询 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));


        print_r("======支付通道余额结果查询 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
        Log::info("======支付通道余额结果查询 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
        //tonglianpay
        $payTypes = [config('common.tonglianpay'), config('common.lianlianpay')];
        foreach ($payTypes as $payType) {
            $reqSn = date('YmdHis').mt_rand(100000,999999);
            $resultQueryData = PayHelper::payAccountInfo($payType, $reqSn);
            if(!empty($resultQueryData) && $resultQueryData['code'] == 1) {
                $balance = $resultQueryData['balance'];
                if(is_numeric($balance)) {
                    $balance = (float)$balance / 100;

                    LiquidatedStatService::updateAccountBalanceAmount($payType, $balance);
                }
            }
            print_r("======支付通道余额结果查询 [".json_encode($resultQueryData)."]".date("y-m-d H:i:s"."\n"));
            Log::info("======支付通道余额结果查询 [".json_encode($resultQueryData)."]".date("y-m-d H:i:s"."\n"));
        }
        Log::info("======支付通道余额结果查询 【任务执行结束】=====[".date("y-m-d H:i:s"."\n"));
        print_r("======支付通道余额结果查询 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));
    }
}