<?php


namespace Tests\Services\OwnerPriceExpressService;


use App\Logistic;
use App\Owner;
use App\OwnerPriceExpress;
use App\OwnerPriceExpressProvince;
use App\Province;
use App\Services\OwnerPriceExpressService;
use Illuminate\Support\Str;
use Tests\TestCase;

class OwnerPriceExpressServiceTest extends  TestCase
{
    /** @var OwnerPriceExpressService */
    public $service;
    public $data;

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

        $this->data["models"] = factory(OwnerPriceExpress::class,2)->create()->toArray();
    }

    /**
     * @group customer
     */
    public function testPaginate()
    {
        $models = $this->service->paginate();
        $this->assertGreaterThanOrEqual(2,count($models));
        $models = $this->service->paginate($this->data["models"][0]["id"]);
        $this->assertCount(1,$models);
    }
    /**
     * @group customer
     */
    public function testFind()
    {
        $model = $this->service->find($this->data["models"][0]["id"]);
        $this->assertNotNull($model);
    }
    /**
     * @group customer
     */
    public function testUpdateDetail()
    {
        $detail = factory(OwnerPriceExpressProvince::class)->create([
            "owner_price_express_id" => $this->data["models"][0]["id"],   //快递价格ID
        ]);
        $result = $this->service->updateDetail(["id"=>$detail->id],["initial_weight_price"=>5]);
        $this->assertEquals(1,$result);
        OwnerPriceExpressProvince::destroy($detail->id);
    }
    /**
     * @group customer
     */
    public function testCreate()
    {
        $model = $this->service->create([
            "name" => md5(Str::random(8)),
            "initial_weight" => mt_rand(6,260) / 5,
            "additional_weight" => mt_rand(6,260) / 5,
        ]);
        $this->assertNotNull($model);
        $this->data["models"][] = $model->toArray();
    }
    /**
     * @group customer
     */
    public function testCreateDetail()
    {
        $model = $this->service->createDetail([
            "owner_price_express_id" => $this->data["models"][0]["id"],
            "province_id" => 1,
            "initial_weight_price" => mt_rand(6,260) / 5,
            "additional_weight_price"=>mt_rand(6,260) / 5,
        ]);
        $this->assertNotNull($model);
        $this->assertNotNull($model->id);
        OwnerPriceExpressProvince::destroy($model->id);
    }
    /**
     * @group customer
     */
    public function testIsExistDetail()
    {
        $province = Province::query()->first();
        $detail = factory(OwnerPriceExpressProvince::class)->create([
            "owner_price_express_id" => $this->data["models"][0]["id"],
            "province_id" => $province->id
        ]);
        $result = $this->service->isExistDetail([
            "owner_price_express_id"=>$this->data["models"][0]["id"],
            "province_id" => $province->id
        ]);
        $this->assertGreaterThan(0,$result);
        OwnerPriceExpressProvince::destroy($detail->id);
    }
    /**
     * @group customer
     */
    public function testDestroyDetail()
    {
        $detail = factory(OwnerPriceExpressProvince::class)->create([
            "owner_price_express_id" => $this->data["models"][0]["id"],
        ]);
        $result = $this->service->destroyDetail($detail->id);
        $this->assertEquals(1,$result);
    }
    /**
     * @group customer
     */
    public function testGetExistOwnerName()
    {
        $owner = factory(Owner::class)->create([
            "user_owner_group_id" => 1,
        ]);
        $logistic = factory(Logistic::class)->create();
        /** @var OwnerPriceExpress $model */
        $model = OwnerPriceExpress::query()->find($this->data["models"][0]["id"]);
        $model->owners()->sync([$owner->id]);
        $model->logistics()->sync([$logistic->id]);
        $result = $this->service->getExistOwnerName([$owner->id],$logistic->id,$this->data["models"][0]["id"]);
        $this->assertCount(0,$result);
        $result = $this->service->getExistOwnerName([$owner->id],$logistic->id);
        $this->assertCount(1,$result);
        $this->assertStringContainsString($owner->name,$result[0]);
        Owner::destroy($owner->id);
        $model->owners()->sync([]);
        $model->logistics()->sync([]);
    }
    /**
     * @group customer
     */
    public function testGetExistLogisticName()
    {
        $logistic = factory(Logistic::class)->create();
        /** @var OwnerPriceExpress $model */
        $model = OwnerPriceExpress::query()->find($this->data["models"][0]["id"]);
        $model->logistics()->sync([$logistic->id]);

        $result = $this->service->getExistLogisticName([$logistic->id],$this->data["models"][0]["id"]);
        $this->assertCount(0,$result);
        $result = $this->service->getExistLogisticName([$logistic->id]);
        $this->assertCount(1,$result);
        $this->assertEquals($logistic->name,$result[0]);

        Logistic::destroy($logistic->id);
        $model->logistics()->sync([]);
    }
    /**
     * @group customer
     */
    public function testDestroy()
    {
        $result = $this->service->destroy($this->data["models"][0]["id"]);
        $this->assertEquals(1,$result);
        unset($this->data["models"][0]);
    }
    /**
     * @group customer
     */
    public function testUpdate()
    {
        $result = $this->service->update(["id"=>$this->data["models"][0]["id"]],["initial_weight"=>3]);
        $this->assertEquals(1,$result);
    }
    /**
     * @group customer
     */
    public function testMatching()
    {
        /** @var OwnerPriceExpress $model */
        $model = factory(OwnerPriceExpress::class)->create([
            "initial_weight" => 2.4,
            "additional_weight" => 3.1,//16.2
        ]);
        $this->data["models"][] = $model->toArray();
        $detail = factory(OwnerPriceExpressProvince::class)->create([
            "owner_price_express_id" => $model->id,
            "province_id" => 1,
            "initial_weight_price" => 2,     //初始单价 4.8
            "additional_weight_price"=>2.2,  //续重单价
        ]);
        $owner = factory(Owner::class)->create([
            "user_owner_group_id" => 1
        ]);
        $logistic = factory(Logistic::class)->create();
        $model->owners()->sync([$owner->id]);
        $model->logistics()->sync([$logistic->id]);
        $result = $this->service->matching(18.6,$owner->id,$logistic->id,1);
        $this->assertEquals([0,null],$result);
        $model->owners()->sync([]);
        $model->logistics()->sync([]);
        OwnerPriceExpressProvince::destroy($detail->id);
        Owner::destroy($owner->id);
        Logistic::destroy($logistic->id);
    }


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