<?php

namespace App\Console\Commands;

use App\Events\FinishAccount;
use App\Models\Order;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

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

    /**
     * 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('定时收货任务在运行 ing');
        $date=date("Y-m-d H:i:s",strtotime('-15 days'));
        Log::info($date);
//        $date=date("Y-m-d H:i:s",strtotime('-1 days'));//测试期间设为一天
//        $date=date("Y-m-d H:i:s",strtotime('-1 hours'));
        $orders=Order::with('address')
            ->where('status',2)->where('express_state','3')
            ->where('express_receive_time','<',$date)->get();
        foreach($orders as $key=>$val){
            Log::info($val->order_no.'：订单系统自动收货');

            if($val->pay_type != 3){
                Order::where('id',$val->id)->update(['status'=>3,'order_status'=>1]);
                event(new FinishAccount($val->order_no));
            }else{
                Order::where('id',$val->id)->update(['status'=>3,'order_status'=>1,'is_finish'=>1,'finish_no'=>$val->order_no]);
            }
        }
    }
}
