<?php

namespace App\Console\Commands;

use App\Http\Controllers\Admin\StoreController as Sc;
use App\Models\Order;
use App\Models\Store;
use App\Models\TipWithdraw;
use App\Models\DwbsUser;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Overtrue\EasySms\EasySms;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '提醒提现7日';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        Log::info('7日后提示提现发送短信');
        $tips=TipWithdraw::whereDate('send_at',Carbon::now()->subDays(7))->get();
        foreach($tips as $key=>$val){
            if($val->num >= 3){
                TipWithdraw::where('id',$val->id)->delete();
                continue;
            }

            $store=Store::withTrashed()->where('is_failure',0)->where('user_id',$val->user_id)->first();
            if($store){
                $total = Order::where('store_id',$store->id)->where('status','<','3')->where('is_pay',1)->whereIn('apply_cancel',[0,1,3])->count();
            }else{
                $total = 0;
            }
            $sc=new Sc();
            $result=$sc->getAccount($val->user_id);
//            $account=$result['pending_amount']+$result['available_amount'];
            Log::info($result);
            if(!empty($result)){
                if(array_key_exists('pending_amount',$result) && array_key_exists('available_amount',$result)){
                    $account=$result['pending_amount']+$result['available_amount'];
                }else{
                    $account=0;
                }
            }else{
                Log::info('获取账户金额失败-'.$val->user_id);
                $account=0;
            }

            if($account > 0 || $total > 0){
                $num=$val->num+1;
                TipWithdraw::where('id',$val->id)->update([
                    'send_at'=>Carbon::now(),
                    'num'=>$num,
                ]);
                $phone=DwbsUser::withTrashed()->where('id',$val->user_id)->value('mobile');
                $config=config('easysms');
                $easySms = new EasySms($config);
                $content="您的微店铺账号已被删除，请及时处理订单，并完成提现。如已处理请忽略。";
                try{
                    $easySms->send($phone, ['content' =>$content ]);
                    Log::info($phone.'-7日后提示提现短信发送成功');
                } catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $exception) {
                    $message = $exception->getException('qcloud')->getMessage();
                    Log::info($phone.'-7日后提示提现短信发送失败-'.$message);
                }
            }else{
                TipWithdraw::where('id',$val->id)->delete();
            }
        }
    }
}
