<?php

namespace App\Console;

use App\Console\Commands\ReceivedGoods;
use App\Handlers\ProfitSharing;
use App\Handlers\TradeBill;
use App\Handlers\EcommerceBillHandler;
use App\Models\Address;
use App\Models\AgentNo;
use App\Models\DwbsUser;
use App\Models\Store;
use App\Models\User;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Log;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        ReceivedGoods::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('tip_send')->withoutOverlapping()->hourly();
        $schedule->command('received_goods')->withoutOverlapping()->everyMinute();

        $schedule->command('withdraw_correction_two')->withoutOverlapping()->hourly();
        $schedule->command('withdraw_correction_three')->withoutOverlapping()->hourly();//->dailyAt('10:00');//
        $schedule->command('withdraw_correction_five')->withoutOverlapping()->twiceDaily(10, 16);//->dailyAt('10:00');
        $schedule->command('withdraw_correction_eight')->withoutOverlapping()->dailyAt('11:00');//->everyMinute();

        $schedule->command('tip_withdraw')->withoutOverlapping()->everyMinute();
        $schedule->command('tip_withdraw_days')->withoutOverlapping()->dailyAt('15:00');

        $schedule->command('fifteen_no_finish')->withoutOverlapping()->dailyAt('10:00');

        $schedule->command('finished_order')->withoutOverlapping()->everyMinute();

        $schedule->call(function (){
            Log::error('开始处理手机号');
            $user=Address::where('id','>','672081')->whereNull('phone_code')->get();
            foreach ($user as $k=>$v){
                if ($v->phone){
                    $phone_code=Crypt::encryptString($v->phone);
                    $phone=mb_substr($v->phone,0,3).'****'.mb_substr($v->phone,'-4');
                }else{
                    $phone=null;
                }
                if ($v->address){
                    $address_code=Crypt::encryptString($v->address);
                    $address='********';
                }else{
                    $address=null;
                }

                Address::where('id',$v->id)->update([
                    'phone'=>$phone,
                    'address'=>$address,
                    'phone_code'=>$phone_code,
                    'address_code'=>$address_code,

                ]);

            }
            Log::error('结束处理手机号');
        })->hourly();


        $schedule->call(function (){
            Log::error('开始处理手机号');
            $user=User::where('id','>','909982')->whereNotNull('phone')->whereNull('phone_code')->get();
            foreach ($user as $k=>$v){
                if ($v->phone){
                    $phone_code=Crypt::encryptString($v->phone);
                    $phone=mb_substr($v->phone,0,3).'****'.mb_substr($v->phone,'-4');
                }else{
                    $phone=null;
                }
                User::where('id',$v->id)->update([
                    'phone'=>$phone,
                    'phone_code'=>$phone_code,

                ]);

            }
            Log::error('结束处理手机号');
        })->hourly();

        $schedule->call(function (){
            Log::error('开始处理手机号');
            $user=AgentNo::whereNotNull('phone')->whereNull('phone_code')->get();
            foreach ($user as $k=>$v){
                if ($v->phone){
                    $phone_code=Crypt::encryptString($v->phone);
                    $phone=mb_substr($v->phone,0,3).'****'.mb_substr($v->phone,'-4');
                }else{
                    $phone=null;
                }
                AgentNo::where('id',$v->id)->update([
                    'phone'=>$phone,
                    'phone_code'=>$phone_code,

                ]);

            }
            Log::error('结束处理手机号');
        })->hourly();

        $schedule->call(function (){
            $date= date("Y-m-d",strtotime("-1 day"));
            Log::info('下载二级商户资金账单，日期：'.$date);
            $eco  = new EcommerceBillHandler();
            $eco -> getecommercebill($date,'ALL','AEAD_AES_256_GCM');
        })->dailyAt('11:00');

        $schedule->call(function (){
            $date= date("Y-m-d",strtotime("-1 day"));
            $trade  = new TradeBill();
            $trade -> getDownWithdraw($date);
        })->dailyAt('15:00');

        $schedule->call(function (){
            $date= date("Y-m-d",strtotime("-1 day"));
            $trade  = new ProfitSharing();
            $trade -> getprofitsharing($date);
        })->dailyAt('15:00');

        $schedule->call(function (){
            $store=Store::where('is_apply',3)->get();
            foreach ($store as $k=>$v){
                DwbsUser::where('id',$v->user_id)->update(['province'=>$v->province,'city'=>$v->city,'country'=>$v->area,'address'=>$v->address]);
            }
            Log::error('地址同步结束');
        })->dailyAt('16:15');
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}
