<?php


namespace Tests\Services\CacheShelfService;

use App\MaterialBox;
use App\Services\CacheShelfService;
use App\Station;
use App\StationTask;
use App\StationTaskChildren;
use App\StationTaskMaterialBox;
use App\StationType;
use Faker\Factory;
use Illuminate\Database\Eloquent\Collection;
use Tests\TestCase;

class GetChildStationTest extends TestCase
{
    /** @var CacheShelfService $service */
    protected $service;

    protected $data = [];

    protected function setup(): void
    {
        parent::setup();
        $this->service = app(CacheShelfService::class);

        $stationType = StationType::query()->firstOrCreate(['name'=> '缓存架']);

        $this->data['parentStation'] = factory(Station::class)->create(['station_type_id' => $stationType['id']]);
        $this->data['station'] = factory(Station::class)->create(['parent_id'=>$this->data['parentStation']['id']]);
        $this->data['materialBox'] = factory(MaterialBox::class)->create();
        $this->data['stationTask'] = factory(StationTask::class)->create();
        $this->data['stationTask']['station_id'] = $this->data['station']['id'];
        $this->data['stationTask']->save();

        $this->data['stationTaskMaterialBox'] = factory(StationTaskMaterialBox::class)->create([
            'station_id' => $this->data['station']['id'],
            'material_box_id' => $this->data['materialBox']['id'],
            'status' => '待处理'
        ]);

        $this->data['stationTaskMaterialBox']['station_task_id'] = $this->data['stationTask']['id'];
        $this->data['stationTaskMaterialBox']->save();

        $this->data['stationTaskChildren']  = StationTaskChildren::query()->create([
            "station_task_id" => $this->data['stationTask']['id'],
        ]);
    }

    public function testGetTasks()
    {
        $grids = $this->service->getChildStation($this->data['station']['id']);
        $this->assertTrue($grids ? true : false);
    }


    protected function tearDown(): void
    {
        $materialBox = MaterialBox::query()->where('id', $this->data['materialBox']['id'])->first();
        if ($materialBox) {
            $stationTaskMaterialBoxes = StationTaskMaterialBox::query()->where('material_box_id', $materialBox['id'])->get();
            foreach ($stationTaskMaterialBoxes as $stationTaskMaterialBox) {
                if($stationTaskMaterialBox->station->parent)$stationTaskMaterialBox->station->parent->delete();
                if ($stationTaskMaterialBox->station) $stationTaskMaterialBox->station->delete();
                if ($stationTaskMaterialBox->stationTask) $stationTaskMaterialBox->stationTask->delete();
                $stationTaskMaterialBox->delete();
            }
            $materialBox->delete();
        }
        if($this->data['stationTaskChildren'])StationTaskChildren::query()->where('id',$this->data['stationTaskChildren']['id'])->delete();
        parent::tearDown(); // TODO: Change the autogenerated stub
    }


}
