<?php


namespace Tests\Services\OwnerPriceOperationItemService;


use App\OwnerPriceOperation;
use App\OwnerPriceOperationItem;
use App\Services\OwnerPriceOperationItemService;
use App\Unit;
use Tests\TestCase;

class OwnerPriceOperationItemServiceTest extends  TestCase
{
    /** @var OwnerPriceOperationItemService */
    public $service;
    public $data;

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

        $this->data["units"] = [];
        $unit1 = Unit::query()->where("name","件")->first();
        if (!$unit1){
            $unit1 = factory(Unit::class)->create([
                "name" => "件"
            ]);
            $this->data["units"][] = $unit1->toArray();
        }
        $unit2 = Unit::query()->where("name","箱")->first();
        if (!$unit2){
            $unit2 = factory(Unit::class)->create([
                "name" => "箱"
            ]);
            $this->data["units"][] = $unit2->toArray();
        }
        $ownerPriceOperation = factory(OwnerPriceOperation::class)->create();


        $this->data["ownerPriceOperations"] = [$ownerPriceOperation->toArray()];
        $this->data["models"] = [];
        $this->data["models"][] = factory(OwnerPriceOperationItem::class)->create([
            "owner_price_operation_id" => $ownerPriceOperation->id,
            "unit_id" => $unit1->id,
        ])->toArray();
        $this->data["models"][] = factory(OwnerPriceOperationItem::class)->create([
            "owner_price_operation_id" => $ownerPriceOperation->id,
            "unit_id" => $unit2->id,
        ])->toArray();
    }

    /**
     * @group customer
     */
    public function testGet()
    {
        $models = $this->service->get(["owner_price_operation_id"=>$this->data["ownerPriceOperations"][0]["id"]]);
        $this->assertCount(2,$models);
    }
    /**
     * @group customer
     */
    public function testUpdate()
    {
        $model = $this->data["models"][0];
        $result = $this->service->update(["id"=>$model["id"]],[
            "unit_price" => 1.33,
            "priority" => 99,
            "amount" => 50
        ]);
        $this->assertEquals(1,$result);
        $expected = OwnerPriceOperationItem::query()->find($model["id"]);
        $this->assertEquals(99,$expected->priority);
        $this->assertEquals(1.33,$expected->unit_price);
        $this->assertEquals(50,$expected->amount);
    }
    /**
     * @group customer
     */
    public function testCreate()
    {
        $model = $this->service->create([
            "owner_price_operation_id" => 1,         //作业计费ID
            "strategy" => "特征",                         //出库策略
            "amount" => 20,                           //起步数
            "unit_id" => 2,                          //单位ID
            "unit_price" => 2,                       //单价
            "feature" => "1&3",                          //特征
            "priority" => 6,                         //优先级 值越大越高
        ]);
        $this->assertNotNull($model);
        $this->data["models"][] = $model->toArray();
    }
    /**
     * @group customer
     */
    public function testFindUpdate()
    {
        $model = OwnerPriceOperationItem::query()->find($this->data["models"][0]["id"]);
        $result = $this->service->findUpdate($model,[
            "unit_price" => 0.566,
        ]);
        $this->assertEquals(1,$result);
    }
    /**
     * @group customer
     */
    public function testFind()
    {
        $models = $this->service->find(array_column($this->data["models"],"id"));
        $this->assertCount(2,$models);
    }
    /**
     * @group customer
     */
    public function testIsExist()
    {
        $result = $this->service->isExist([
            "id" => $this->data["models"][0]["id"],
            "owner_price_operation_id" => $this->data["models"][0]["owner_price_operation_id"],
            "strategy" => $this->data["models"][0]["strategy"],
        ]);
        $this->assertGreaterThan(0,$result);
    }

    public function tearDown(): void
    {
        Unit::destroy(array_column($this->data["units"],"id"));
        OwnerPriceOperation::destroy(array_column($this->data["ownerPriceOperations"],"id"));
        OwnerPriceOperationItem::destroy(array_column($this->data["models"],"id"));
        parent::tearDown();
    }
}