<?php

namespace Tests\Services\BatchService;
use App\MaterialBox;
use App\Order;
use App\OrderCommodity;
use App\Owner;
use App\Services\BatchService;
use App\Services\ForeignHaiRoboticsService;
use App\Services\StationTaskBatchService;
use App\StationRuleBatch;
use App\StationTask;
use App\StationTaskBatch;
use App\StationTaskCommodity;
use App\StationTaskMaterialBox;
use Ramsey\Uuid\Uuid;
use Tests\TestCase;
use App\Batch;
use App\Traits\TestMockSubServices;

class AssignTasksTest extends TestCase
{
    use TestMockSubServices;
    /** @var BatchService $service */
    public $service;
    private $data;
    private $batchAmount=2;
    private $orderAmount=4;
    private $orderCommodityAmount=8;
    function setUp(): void
    {
        parent::setUp();
        $this->service=$this->subMock([
            'class'=>BatchService::class,
            'subServices'=>[
                [
                    'serviceName'=>'stationTaskBatchService',
                    'class'=>StationTaskBatchService::class,
                    'methods'=>[
                        'firstOrCreate'=>new MaterialBox(['id'=>1]),
                    ],
                    'subServices'=>[
                        [
                            'serviceName'=>'foreignHaiRoboticsService',
                            'class'=>ForeignHaiRoboticsService::class,
                            'methods'=>[
                                'fetchGroup'=>true,
                            ],
                        ],
                    ]
                ],
            ]
        ]);
        $this->data['owner']
            = factory(Owner::class)
            ->create();
        $this->data['batches']
            = factory(Batch::class, $this->batchAmount)
            ->create(['owner_id'=>$this->data['owner']['id']]);
        $this->data['orders'] =
            factory(Order::class)
                ->createMany($this->makeArray($this->orderAmount,[
                    'status'=>'未处理',
                    'batch_id' => function(){return $this->getTargetFieldCirculately($this->data['batches']);}
                ]));
        $this->data['orderCommodities'] =
            factory(OrderCommodity::class)
                ->createMany($this->makeArray($this->orderCommodityAmount,[
                    'order_id' => function(){return $this->getTargetFieldCirculately($this->data['orders']);},
                    'location' => function(){return 'IDE' . rand(1, 6);},
                ]));
        $this->data['station_rule_batches']
            = factory(StationRuleBatch::class)
            ->createMany($this->makeArray($this->batchAmount,[
                'owner_id' => function(){return $this->getTargetFieldCirculately($this->data['batches'],'','owner_id');}
            ]));

    }

    public function testReturned()
    {
        $this->service->assignTasks($this->data['batches']);
        ($波次任务指向了波次=function(){
            $this->data['batches']->load('stationTaskBatch');
            dump(data_get($this->data['batches'],'*.id'),data_get($this->data['batches'],'*.stationTaskBatch.batch_id'));
            dd(array_diff(data_get($this->data['batches'],'*.id'),data_get($this->data['batches'],'*.stationTaskBatch.batch_id')));
            $this->assertEquals(
                data_get($this->data['batches'],'*.id'),
                data_get($this->data['batches'],'*.stationTaskBatch.batch_id')
            );
        })();
    }

    function tearDown(): void
    {
        MaterialBox::query()
            ->whereIn('id',data_get($this->data['batches'],'*.stationTaskBatch.stationTask.stationTaskMaterialBoxes.*.materialBox.id')??[])
            ->delete();
        StationTaskMaterialBox::query()
            ->whereIn('id',data_get($this->data['batches'],'*.stationTaskBatch.stationTask.stationTaskMaterialBoxes.*.id')??[])
            ->delete();
        StationTaskCommodity::query()
            ->whereIn('id',data_get($this->data['batches'],'*.stationTaskBatch.stationTask.stationTaskCommodities.*.id')??[])
            ->delete();
        StationTaskBatch::query()
            ->whereIn('id',data_get($this->data['batches'],'*.stationTaskBatch.id')??[])
            ->delete();
        StationTask::query()
            ->whereIn('id',data_get($this->data['batches'],'*.stationTaskBatch.stationTask.id')??[])
            ->delete();
        StationRuleBatch::query()
            ->whereIn('id',data_get($this->data['station_rule_batches'],'*.id')??[])
            ->delete();
        OrderCommodity::query()
            ->whereIn('id',data_get($this->data['orderCommodities'],'*.id')??[])
            ->delete();
        Order::query()
            ->whereIn('id',data_get($this->data['orders'],'*.id')??[])
            ->delete();
        Batch::query()
            ->whereIn('id',data_get($this->data['batches'],'*.id')??[])
            ->delete();
        Owner::query()
            ->whereIn('id',data_get($this->data['owner'],'*.id')??[])
            ->delete();
        parent::tearDown();
    }
}
