<?php

namespace App\Console\Commands;

use App\Models\GoodsSku;
use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新goods_sku的external_sku_id';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        GoodsSku::query()->whereNull('external_sku_id')->chunk(500, static function ($skus) {
            foreach ($skus as $sku) {
                $sku->external_sku_id = $sku->goods->goods_code . '_' . $sku->sku_code;
                $sku->save();
            }
        });
        GoodsSku::query()->where('external_sku_id', '')->chunk(500, static function ($skus) {
            foreach ($skus as $sku) {
                $sku->external_sku_id = $sku->goods->goods_code . '_' . $sku->sku_code;
                $sku->save();
            }
        });
    }
}
