<?php

namespace Tests\Services\StationTaskBatchService;
use App\Batch;
use App\MaterialBox;
use App\Order;
use App\OrderCommodity;
use App\Owner;
use App\Services\ForeignHaiRoboticsService;
use App\Services\MaterialBoxService;
use App\Services\StationTaskBatchService;
use App\Services\StationTaskCommodityService;
use App\Services\StationTaskMaterialBoxService;
use App\Services\StationTaskService;
use App\StationRuleBatch;
use App\StationTask;
use App\StationTaskCommodity;
use App\StationTaskMaterialBox;
use Tests\TestCase;
use App\StationTaskBatch;
use App\Traits\TestMockSubServices;

class RunManyTest extends TestCase
{
    use TestMockSubServices;
    /** @var StationTaskBatchService $stationTaskBatchService */
    public $stationTaskBatchService;
    /** @var StationTaskService $stationTaskService */
    public $stationTaskService;
    /** @var StationTaskCommodityService $stationTaskCommodityService */
    public $stationTaskCommodityService;
    /** @var StationTaskMaterialBoxService $stationTaskMaterialBoxService */
    public $stationTaskMaterialBoxService;
    private $data;

    private $bathAmount=3;
    function setUp(): void
    {
        parent::setUp();
        $this->stationTaskService = app('StationTaskService');
        $this->stationTaskCommodityService = app('StationTaskCommodityService');
        $this->stationTaskMaterialBoxService = app('StationTaskMaterialBoxService');
        $this->stationTaskBatchService=$this->subMock([
            'class'=>StationTaskBatchService::class,
//            'subServices'=>[
//                [
//                    'serviceName'=>'foreignHaiRoboticsService',
//                    'class'=>ForeignHaiRoboticsService::class,
//                    'methods'=>[
//                        'fetchGroup'=>true,
//                    ],
//                ],
//            ]
        ]);

        $this->data['owner'] =
            factory(Owner::class)
                ->create();
        $this->data['batches'] =
            factory(Batch::class,
                $this->bathAmount)
                ->create([
                    'status'=>'未处理',
                    'owner_id'=>$this->data['owner']['id']
                ]);
        $this->data['stationRuleBatch'] =
            factory(StationRuleBatch::class)
                -> create([
                    'owner_id'=>$this->data['owner']['id'],
                ]);
        $this->data['stationTask'] =
            $this->stationTaskService->create($this->bathAmount);
        $this->data['stationTaskCommodities']=$this->stationTaskCommodityService->createByBatches($this->data['batches'],$this->data['stationTask']); //注册商品任务
        $this->data['stationTaskMaterialBoxes']=$this->stationTaskMaterialBoxService->createByBatches($this->data['batches'],$this->data['stationTask']); //注册料箱任务
        $this->data['stationTaskBathes']=$this->stationTaskBatchService->createByBatches($this->data['batches'],$this->data['stationTask']); //注册波次任务


    }

    public function testReturned()
    {

        $taskBatches_failed=$this->stationTaskBatchService->runMany(
            $this->data['stationTaskBathes']
        );
        $this->assertTrue($taskBatches_failed->isEmpty());
        $this->assertTrue((
            $全部是处理中状态
                =function(){
            $arr=array_unique(
                data_get($this->data['stationTaskBathes'],'*.status')
            );
            return
                count($arr)==1
                && $arr[0]=='处理中';
        })());
    }

    function tearDown(): void
    {
        StationTaskBatch::query()
            ->whereIn('id',data_get($this->data['stationTaskBathes'],'*.id')??[])
            ->delete();

        StationRuleBatch::query()
            ->whereIn('id',$this->data['stationRuleBatch']['id'])
            ->delete();

        StationTaskMaterialBox::query()
            ->whereIn('id',data_get($this->data['batches'],'*.id')??[])
            ->delete();
        Order::query()
            ->whereIn('id',data_get($this->data['orders'],'*.id')??[])
            ->delete();
        MaterialBox::query()
            ->whereIn('id',data_get($this->data['materialBoxes'],'*.id')??[])
            ->delete();
        OrderCommodity::query()
            ->whereIn('id',data_get($this->data['orderCommodities'],'*.id')??[])
            ->delete();
        StationTask::query()
            ->whereIn('id',data_get($this->data['stationTask'],'*.id')??[])
            ->delete();
        StationTaskCommodity::query()
            ->whereIn('id',data_get($this->data['stationTaskCommodities'],'*.id')??[])
            ->delete();
        parent::tearDown();
    }

}
