<?php

namespace App\Console\Commands;

use App\Http\Controllers\Admin\StoreController;
use App\Models\Store;
use App\Models\Amount;
use App\Models\FinishOrder;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class FinishedOrder extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'finished_order';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '查询完结分账';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        Log::info('查询完结分账正在运行 ing');
        $date=date("Y-m-d H:i:s",strtotime('-30 minutes'));
//        if(18<=date("H") || date("H")<=9){  //早九点到晚6点
//            return false;
//        }
//        $date='2022-12-17 12:00:00';
        $amounts= Amount::where('type',2)->where('created_at','>=',$date)->get();
        $finishOrder=new StoreController();
        foreach($amounts as $key=>$val){
            $sub_mchid=Store::withTrashed()->where('id',$val->store_id)->value('sub_mchid');
            $finish= FinishOrder::where('transaction_id',$val->transaction_id)->where('out_order_no',$val->order_no)->first();
            if(!$finish){
                $result= $finishOrder->fenzhang($sub_mchid,$val->transaction_id,$val->order_no);
                Log::info('分账查询结果。。。');
                Log::info($result);
                if($result && array_key_exists('status',$result)){
                    FinishOrder::create([
                        'sub_mchid'=>$result['sub_mchid'],
                        'transaction_id'=>$result['transaction_id'],
                        'out_order_no'=>$result['out_order_no'],
                        'order_id'=>$result['order_id'],
                        'status'=>$result['status'],
                        'receivers'=>array_key_exists('receivers',$result)?json_encode($result['receivers']):null,
                        'finish_amount'=>array_key_exists('finish_amount',$result)?$result['finish_amount']:null,
                        'finish_description'=>$result['finish_description'],
                    ]);
                    Amount::where('id',$val->id)->update(['order_id'=>$result['order_id'],
                        'status_code'=>$result['status'],
                        'actual_money'=>array_key_exists('finish_amount',$result)?$result['finish_amount']/100:null]);
                }
            }else{
                if($finish->status=='PROCESSING'){
                    $result= $finishOrder->fenzhang($sub_mchid,$val->transaction_id,$val->out_order_no);
                    if($result && array_key_exists('status',$result)){
                        if($result['status']=='SUCCESS'){
                            $finish->order_id=$result['order_id'];
                            $finish->status=$result['status'];
                            $finish->receivers=array_key_exists('receivers',$result)?json_encode($result['receivers']):null;
                            $finish->finish_amount=array_key_exists('finish_amount',$result)?$result['finish_amount']:null;
                            $finish->finish_description=$result['finish_description'];
                            $finish->save();
                            Amount::where('id',$val->id)->update(['order_id'=>$result['order_id'],
                                'status_code'=>$result['status'],
                                'actual_money'=>array_key_exists('finish_amount',$result)?$result['finish_amount']/100:null]);
                        }
                    }
                }
            }
        }
    }
}
