<?php

namespace App\Console\Commands;

use App\Models\Activity;
use App\Models\Goods;
use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $date = date('Y-m-d H:i:s');
        //获取生效的活动
        $activity = Activity::where('starttime', '<=', $date)->where('endtime', '>', $date)->where('status', 1)->first();
        //获取skus
        if(!$activity) exit('finished!');
        $skus = Goods::getSkusByActivity($activity->id);
        //请求leyou价格接口
        $skuPriceList = Goods::getSkuPrice($skus);
        //入库
        foreach ($skuPriceList as $v){
            Goods::query()->where('sku', $v['sku'])->update(['price' => $v['sale_price']]);
        }
    }
}
