<?php

namespace App\Console\Commands;

use App\Models\Order;
use App\Models\Store;
use App\Models\DwbsUser;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Log;
use Overtrue\EasySms\EasySms;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '15天未完成订单';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        Log::info('提示订单支付成功15日未完成发送短信');
        $date15 =  Carbon::today()->subDays(15)->toDateString();
        log::info($date15);

        $store_ids=Order::where('is_pay',1)->whereIn('apply_cancel',[0,1,3])->where('is_finish',0)
            ->whereDate('pay_at',$date15)->groupBy('store_id')->pluck('store_id');

//        $phones=Store::withTrashed()->whereIn('id',$store_ids)->pluck('phone');
        $users_ids=Store::withTrashed()->whereIn('id',$store_ids)->pluck('user_id');
        $phones=DwbsUser::whereIn('id',$users_ids)->pluck('uuid');

        Log::info('数量：'.count($phones));

        $config=config('easysms');
        $easySms = new EasySms($config);
        $days=15;
        $content="您的微店铺有订单超过{$days}天未完成，请及时处理。";
        Config::set('app.key', 'base64:i8eBBFVN/3Lq7yDuT6MfDoPdLqfZlIWdUky78HkkDVo=');
        foreach($phones as $key=>$val){
            $val = Crypt::decryptString($val);
            try{
                $easySms->send($val, ['content' =>$content ]);
                Log::info($val.'-15日未完成短信发送成功');
            } catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $exception) {
                $message = $exception->getException('qcloud')->getMessage();
                Log::info($val.'-15日未完成短信发送失败-'.$message);
            }
        }

    }
}
