<?php

namespace App\Console\Commands;

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

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

    /**
     * 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('提示提现发送短信');
        $operation=AgentOperationLog::query()->where('type',4)
            ->where('created_at','>=',Carbon::now()->subMinutes(2))->get();
        foreach($operation as $key=>$val){
            $store=Store::withTrashed()->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();
                Log::info($store);
                $store->remark .= '|'.date("Y-m-d H:i:s").'关联系统账号自动删除店铺';
                $store->save();
                $store->delete();
                if($store->is_apply != 3){
                    continue;
                    Log::info($store->id.'-店铺未完成审核跳出当前循环');
                }
            }else{
                $total = 0;
                continue;
                Log::info($val->user_id.'-账号无店铺信息跳出当前循环');
            }

            $sc=new Sc();
            $result=$sc->getAccount($val->user_id);
            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;
            }

            Log::info('......1......'.$val->user_id.'......'.$account);
            if($account > 0 || $total > 0){
                Log::info('..........2...............');
                $tip=Tip::where('user_id',$val->user_id)->first();
                if(empty($tip)){
                    Log::info('.............3............');
                    DB::beginTransaction();
                    Tip::create([
                        'user_id'=>$val->user_id,
                        'num'=>1,
                        'send_at'=>Carbon::now(),
                    ]);
                    $phone=DwbsUser::withTrashed()->where('id',$val->user_id)->value('mobile');
                    $config=config('easysms');
                    $easySms = new EasySms($config);
//                    $content="您的微店铺账号已被删除，请及时处理订单，并完成提现。点击链接进入微店铺 https://dllzff.cn/U1oTQeNj 如已处理请忽略。";
                    $content="您的微店铺账号已被删除，请及时处理订单，并完成提现。如已处理请忽略。";
                    Log::info('.........4................');
                    try{
                        $easySms->send($phone, ['content' =>$content ]);
                        DB::commit();
                        Log::info($phone.'-提示提现短信发送成功');
                    } catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $exception) {
                        $message = $exception->getException('qcloud')->getMessage();
                        DB::rollBack();
                        Log::info($phone.'-提示提现短信发送失败-'.$message);
                    }
                }
            }
        }
    }
}
