<?php

namespace Tests\Services\CommodityService;

use App\Commodity;
use App\OracleBasSKU;
use App\Owner;
use App\Services\CommodityService;
use App\Services\OwnerService;
use App\ValueStore;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class SyncCreateCommodityByBasSkusTest extends TestCase
{
    /**
     * @var CommodityService $service
     */
    public $service;
    public $bas_skus;
    public $created_at;
    public $last_time;
    public $insert_params;
    private $data=[];
    public function setUp(): void
    {
        parent::setUp();
        cache()->flush();
        $this->service=app(CommodityService::class);
        $owner =  factory(Owner::class)->create();
        $basSKU = factory(OracleBasSKU::class)->make(['customerid'=>$owner->code,'packid'=>'STANDARD']);
        $this->data['owner'] = $owner;
        $this->data['basSKU'] = $basSKU;
        $this->bas_skus=collect();
        $this->bas_skus->add($basSKU);
    }

    /**
     * @test
     */
    public function testSyncCreateCommodityByBasSkus()
    {
//        $this->created_at = config('sync.commodity_sync.created_at');
//        $this->last_time = ValueStore::query()->where('name',$this->created_at)->value('value');
//        if (!$this->last_time) $time=Carbon::now()->subSeconds(65);
//        $this->bas_skus=OracleBasSKU::query()
//            ->select('customerid','sku','descr_c','alternate_sku1','alternate_sku2','alternate_sku3','skulength','skuwidth','skuhigh','cube','packid','addtime','edittime')
//            ->where('addTime', '>=', $this->last_time??$time)
//            ->orderByDesc('addtime')
//            ->get();
        $this->insert_params=$this->service->getParamsByBasSkus($this->bas_skus);
        $this->service->syncCreateCommodity($this->bas_skus);
        if ($this->insert_params) $this->assertNotNull($this->insert_params);
        if ($this->insert_params)$this->assertEquals(count($this->insert_params),count($this->bas_skus));
        if (!$this->insert_params)$this->assertNull($this->insert_params);
    }

    public function tearDown(): void
    {
//        ValueStore::query()->updateOrCreate([
//            'name' => $this->created_at,
//        ], [
//            'name' => $this->created_at,
//            'value' => $this->last_time,
//        ]);
        $this->data['owner']->delete();
        $sku=[];
        $owner_id=[];
        if (!$this->insert_params) return;
        foreach ($this->insert_params as $insert_param){
            $owner_id[]=$insert_param['owner_id'];
            $sku[]=$insert_param['sku'];
        }
        $commodities=Commodity::query()->whereIn('owner_id',$owner_id)->whereIn('sku',$sku)->get();
        $commodity_id=[];
        foreach ($commodities as $commodity){
            $commodity_id[]=$commodity->id;
        }
        DB::table('commodities')->whereIn('id',$commodity_id)->delete();
        DB::table('commodity_barcodes')->whereIn('commodity_id',$commodity_id)->delete();
        cache()->flush();
        parent::tearDown(); // TODO: Change the autogenerated stub
    }
}
