<?php


namespace Tests\Services\CacheShelfService;


use App\Material;
use App\MaterialBox;
use App\Services\CacheShelfService;
use App\Services\StationTaskChildService;
use App\Services\StationTaskMaterialBoxService;
use App\Services\StationTaskService;
use App\Station;
use App\StationTask;
use App\StationTaskChildren;
use App\StationTaskMaterialBox;
use App\StationType;
use Tests\TestCase;

class ClearTaskTest extends TestCase
{

    /** @var CacheShelfService $service */
    /** @var StationTaskService $stationTaskService  */
    /** @var StationTaskMaterialBoxService $stationTaskMaterialBoxService  */
    /** @var StationTaskChildService $stationTaskChildService  */
    public $service;
    public $stationTaskService;
    public $stationTaskMaterialBoxService;
    public $stationTaskChildService;
    public $data =[];

    protected function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        $this->service = app(CacheShelfService::class);
        $this->stationTaskService = app(StationTaskService::class);
        $this->stationTaskMaterialBoxService = app(StationTaskMaterialBoxService::class);
        $this->stationTaskChildService = app(StationTaskChildService::class);

        $stationType = StationType::query()->firstOrCreate(['name'=>'立库']);

        $this->data['LiKuStation'] = factory(Station::class)->create(['station_type_id'=>$stationType['id']]);
        $this->data['station'] = factory(Station::class)->create();

        $this->data['stationTask'] = [];
        $this->data['stationTaskMaterialBoxes'] = [];
        $this->data['materialBox'] = factory(MaterialBox::class)->create();

        $this->data['stationTask'] = $this->stationTaskService->create(1);                                                    // 生成站任务
        $this->data['stationTaskMaterialBoxes'][] = $this->stationTaskMaterialBoxService->createByStationAndMaterialBox($this->data['station'],$this->data['materialBox']);     // 创建料箱任务
        $this->stationTaskService->registerStations($this->data['stationTask'],[$this->data['station']['id']]);                                            // 注册站任务站
        $this->data['stationTaskMaterialBoxes'][0]['station_task_id'] = $this->data['stationTask'][0]['id'];
        $this->data['stationTaskMaterialBoxes'][0]->update();
        $params = [[
            'station_task_id'=>$this->data['stationTask'][0]->first()['id'],
            'station_taskable_type'=>StationTaskMaterialBox::class,
            'station_taskable_id'=>$this->data['stationTaskMaterialBoxes'][0]['id']
        ]];

        $this->stationTaskChildService->insert($params);                                                                        // 任务任务注册
    }

    public function testClearTask()
    {
        $result = $this->service->clearTask($this->data['station']->code);
        $stationTask = StationTask::query()->where('station_id',$this->data['station']['id'])->first();
        $this->assertEmpty($stationTask);
        $stationTaskMaterialBox = StationTaskMaterialBox::query()->where('material_box_id',$this->data['materialBox']['id'])->get();
        $this->assertEmpty($stationTaskMaterialBox);
    }

    public function testClearTask_taskIsRunning()
    {
        StationTask::query()->where('station_id',$this->data['station']['id'])->update(['status' => '处理中']);
        $result = $this->service->clearTask($this->data['station']->code);

        $stationTask = StationTask::query()->where('station_id',$this->data['station']['id'])->first();

        $this->assertEquals('处理中',$stationTask->status);
        $this->assertFalse($result['success']);
    }


    protected function tearDown(): void
    {
        if($this->data['station']){
            $stationTasks = StationTask::query()->where('station_id',$this->data['station']['id'])->get();
            foreach ($stationTasks as $stationTask) {
                $stationTaskMaterialBoxes = $stationTask->stationTaskMaterialBoxes;
                foreach ($stationTaskMaterialBoxes as $stationTaskMaterialBox) {
                    StationTaskChildren::query()->where(['station_task_id' => $stationTask['id'],'station_taskable_type'=> StationTaskMaterialBox::class,'station_taskable_id'=>$stationTaskMaterialBox['id']])->delete();
                    $stationTaskMaterialBox->delete();
                }
            }
            Station::query()->where('id',$this->data['station']['id'])->delete();
        }

        if($this->data['LiKuStation']){
            $stationTasks = StationTask::query()->where('station_id',$this->data['LiKuStation']['id'])->get();
            foreach ($stationTasks as $stationTask) {
                $stationTaskMaterialBoxes = $stationTask->stationTaskMaterialBoxes;
                foreach ($stationTaskMaterialBoxes as $stationTaskMaterialBox) {
                    StationTaskChildren::query()->where(['station_task_id' => $stationTask['id'],'station_taskable_type'=> StationTaskMaterialBox::class,'station_taskable_id'=>$stationTaskMaterialBox['id']])->delete();
                    $stationTaskMaterialBox->delete();
                }
            }
            Station::query()->where('id',$this->data['LiKuStation']['id'])->delete();
        }
        if($this->data['materialBox'])MaterialBox::query()->where('id',$this->data['materialBox'])->delete();
        parent::tearDown(); // TODO: Change the autogenerated stub
    }

}
