<?php

namespace App\Console\Commands;

use App\Modules\Trade\Services\OrderService;
use DB;
use Illuminate\Console\Command;

class OfficialAccountBillsTask extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'official_account_bills_task  {--start=} {--end=} {--day=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '服务号每日充值统计';


    public function handle()
    {
        $start = $this->option('start');
        $end = $this->option('end');
        $day = $this->option('day');
        if($start && $end){
             $this->start_scope($start,$end);
        }elseif($day){
            $this->start_day($day);
        }else{
            $this->start();
        }
    }

    private function start(){
        $yesterday = date('Y-m-d', strtotime(date("Y-m-d") . " -1 day"));
        //echo '$yesterday'.$yesterday.PHP_EOL;
        $data = OrderService::getRechargeAmountGroupByOfficial($yesterday);
        DB::table('official_account_bills')->insert($data);
    }


    private function start_scope($start,$end){

        if(strtotime($start) >= strtotime($end)){
            return false;
        }
        $temp_start = $start;
        while (true){
            $this->start_day($temp_start);
            if($temp_start == $end){
                break;
            }
            $temp_start = date('Y-m-d',strtotime($temp_start)+86400);
        }
        return 1;
    }


    private function start_day($day){
        //echo $day.PHP_EOL;
        $data = OrderService::getRechargeAmountGroupByOfficial($day);
        DB::table('official_account_bills')->insert($data);
    }

}