<?php

namespace Tests\Services\CommodityService;

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

class SyncUpdateCommodityBasSkusTest extends TestCase
{
    /**
     * @var CommodityService $service
     */
    public $service;
    public $bas_skus;
    public $updated_at;
    public $last_time;
    public $commodities;
    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 testSyncCommodityUpdated()
    {
//        $this->updated_at = config('sync.commodity_sync.updated_at');
//        $this->last_time = ValueStore::query()->where('name',$this->updated_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('EditTime', '>=', $this->last_time??$time)
//            ->whereColumn('EditTime', '<>', 'addTime')
//            ->orderByDesc('EditTime')
//            ->get();
        $this->service->syncUpdateCommodity($this->bas_skus);

        $owner_sku_map = [];
        $sku = [];
        $this->bas_skus->each(function ($addBasSku) use (&$owner_sku_map, &$sku) {
            if (!empty($addBasSku['customerid']) && !empty($addBasSku['sku'])) {
                $key = "owner_code_{$addBasSku['customerid']}_sku_{$addBasSku['sku']}";
                $owner_sku_map[$key] = ['owner_code' => $addBasSku['customerid'], 'sku' => $addBasSku['sku']];
                $sku[] = $addBasSku['sku'];
            }
        });
        /**
         * @var OwnerService $ownerService
         */
        $ownerService = app(OwnerService::class);
        $owner_codes = (function () use ($owner_sku_map) {
            $owner_codes = [];
            if (count($owner_sku_map) == 0) return $owner_codes;
            foreach ($owner_sku_map as $item) {
                $owner_codes[$item['owner_code']] = $item['owner_code'];
            }
            return $owner_codes;
        })();
        $owner_id = (function () use ($ownerService, $owner_codes) {
            $owners = $ownerService->getOwnerByCodes($owner_codes);
            $map = [];
            $owners->each(function ($owner) use (&$map) {
                $map[] = $owner['id'];
            });
            return $map;
        })();
        $this->commodities = Commodity::query()
            ->whereIn('owner_id', array_unique($owner_id))
            ->whereIn('sku', array_unique($sku))
            ->groupBy('owner_id', 'sku')
            ->get();
        $this->assertNotNull($this->commodities);
        $this->assertEquals(count($this->bas_skus),count($this->commodities));
    }

    public function tearDown(): void
    {
//        ValueStore::query()->updateOrCreate([
//            'name' => $this->updated_at,
//        ], [
//            'name' => $this->updated_at,
//            'value' => $this->last_time,
//        ]);
        $this->data['owner']->delete();
        $commodityIds=[];
        foreach ($this->commodities as $commodity){
            array_push($commodityIds,$commodity->id);
        }
        DB::table('commodities')->whereIn('id',$commodityIds)->delete();
        DB::table('commodity_barcodes')->whereIn('commodity_id',$commodityIds)->delete();
        cache()->flush();
        parent::tearDown(); // TODO: Change the autogenerated stub
    }
}
