<?php


namespace Tests\Services\OwnerPriceLogisticService;

use App\Logistic;
use App\Owner;
use App\OwnerPriceLogistic;
use App\OwnerPriceLogisticDetail;
use App\Services\OwnerPriceLogisticService;
use App\Unit;
use Illuminate\Support\Str;
use Tests\TestCase;

class OwnerPriceLogisticServiceTest extends  TestCase
{
    /** @var OwnerPriceLogisticService */
    public $service;
    public $data;

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

        $units = factory(Unit::class,2)->create();
        $this->data["units"] = $units->toArray();
        $this->data["details"] = [];
        $this->data["owners"] = [];
        $this->data["logistics"] = [];
        $this->data["models"] = factory(OwnerPriceLogistic::class,3)->create([
            "unit_id" => $units[0]->id,
            "other_unit_id" => $units[1]->id,
        ])->toArray();
    }

    /**
     * @group customer
     */
    public function testPaginate()
    {
        $models = $this->service->paginate();
        $this->assertGreaterThanOrEqual(3,count($models));
        $this->assertLessThanOrEqual(50,count($models));
    }
    /**
     * @group customer
     */
    public function create()
    {
        $model = $this->service->create([
            "name" => md5(Str::random(7)),
            "unit_range" => "0-5,5-10,10-15,15-20",
            "unit_id" => $this->data["units"][0]["id"],
            "other_unit_range" => "0-5,5-10,10-15,15-20,30-",
            "other_unit_id" => $this->data["units"][1]["id"],
            "pick_up_price" => mt_rand(2,230) / 3,
            "fuel_price" => mt_rand(2,230) / 3,
            "service_price" => mt_rand(2,230) / 3,
        ]);
        $this->assertNotNull($model);
        $this->data["models"][] = $model->toArray();
    }
    /**
     * @group customer
     */
    public function testFind()
    {
        $model = $this->service->find($this->data["models"][0]["id"]);
        $this->assertNotNull($model);
    }
    /**
     * @group customer
     */
    public function testUpdate()
    {
        $result = $this->service->update(["id"=>$this->data["models"][0]["id"]],[
            "fuel_price" => 2.222,
        ]);
        $this->assertEquals(1,$result);
    }
    /**
     * @group customer
     */
    public function testUpdateDetail()
    {
        $province = \App\Province::query()->first();
        $city = \App\City::query()->where("province_id",$province->id)->first();
        $detail = factory(OwnerPriceLogisticDetail::class)->create([
            "owner_price_logistic_id" => $this->data["models"][0]["id"],  //物流计费
            "unit_id" => 1,                  //单位ID
            "range" => "0-5",                    //区间
            "province_id" => $province->id,              //省份ID
            "city_id" => $city->id,
        ]);
        $this->assertNotNull($detail);
        $this->data["details"][] = $detail->toArray();
        $result = $this->service->updateDetail(["id"=>$detail->id],["range"=>"30-"]);
        $this->assertEquals(1,$result);
    }
    /**
     * @group customer
     */
    public function testIsExistDetail()
    {
        $detail = factory(OwnerPriceLogisticDetail::class)->create([
            "owner_price_logistic_id" => $this->data["models"][0]["id"],  //物流计费
            "unit_id" => 1,                  //单位ID
        ]);
        $this->data["details"][] = $detail->toArray();
        $result = $this->service->isExistDetail(["unit_id"=>1,"owner_price_logistic_id"=> $this->data["models"][0]["id"]]);
        $this->assertGreaterThan(0,$result);
    }
    /**
     * @group customer
     */
    public function testCreateDetail()
    {
        $detail = $this->service->createDetail([
            "owner_price_logistic_id" => 1,
            "unit_id" => 1,
            "range" => "0-5",
            "province_id" => 1,
            "city_id" => 1,
            "unit_price" => mt_rand(1,36) / 3,
            "delivery_fee" => mt_rand(1,136) / 3,
            "initial_fee" => mt_rand(1,136) / 3,
            "initial_amount" => mt_rand(1,136) / 3,
            "rate" =>  mt_rand(1,136) / 3,
        ]);
        $this->assertNotNull($detail);
        $this->data["details"][] = $detail->toArray();
    }
    /**
     * @group customer
     */
    public function testDestroyDetail()
    {
        $detail = factory(OwnerPriceLogisticDetail::class)->create([
            "owner_price_logistic_id" => $this->data["models"][0]["id"],  //物流计费
            "unit_id" => 1,                  //单位ID
        ]);
        $this->data["details"][] = $detail->toArray();
        $result = $this->service->destroyDetail($detail->id);
        $this->assertEquals(1,$result);
    }
    /**
     * @group customer
     */
    public function testCheckRange()
    {
        $result = $this->service->checkRange("0-5,5-10,10-15,15-30,30-");
        $this->assertEquals(true,$result);
        $result = $this->service->checkRange("0-5,5-10,10-15,15-30");
        $this->assertEquals(false,$result);
        $result = $this->service->checkRange("0-5,5-10,10-15,16-30,30-");
        $this->assertEquals(false,$result);
        $result = $this->service->checkRange("0-55-10,10-15,16-30,30-");
        $this->assertEquals(false,$result);
    }
    /**
     * @group customer
     */
    public function testMatching()
    {
        /** @var OwnerPriceLogistic $model */
        $model = factory(OwnerPriceLogistic::class)->create([
            "name" => md5(date('Ymd')).Str::random(4),             //名称
            "unit_range" => "0-5,5-10,10-15,15-20",       //单价一区间
            "unit_id" => 1,          //单位一ID
            "other_unit_range" => "0-5,5-10,10-15,15-20,30-", //单位二区间
            "other_unit_id" => 2,    //单位二ID
            "pick_up_price" => 1.5,    //提货费
            "fuel_price" => 2.5,       //燃油附加费
            "service_price" => 3.5,    //信息服务费
        ]);
        $this->data["models"][] = $model->toArray();
        $owner = factory(Owner::class)->create([
            "user_owner_group_id" => 1
        ]);
        $this->data["owners"][] = $owner->toArray();
        $logistic = factory(Logistic::class)->create();
        $this->data["logistics"][] = $owner->toArray();
        $model->owners()->sync([$owner->id]);
        $model->logistics()->sync([$logistic->id]);
        $detail = factory(OwnerPriceLogisticDetail::class)->create([
            "owner_price_logistic_id" => $model->id,  //物流计费
            "unit_id" => 1,                  //单位ID
            "range" => "10-30",                    //区间
            "province_id" => 1,              //省份ID
            "city_id" => 1,                  //城市ID
            "unit_price" => 32.6,               //单价
            "delivery_fee" => 123,             //送货费
            "initial_fee" => 500,              //起始计费
            "initial_amount" => 6,           //起始计数
            "rate" =>  0,                       //费率
        ]);//7.5
        $this->data["details"][] = $detail->toArray();
        $result = $this->service->matching(20,$owner->id,$logistic->id,1,1,1);
        $this->assertEquals([782.5,null],$result);
        $result = $this->service->matching(31,$owner->id,$logistic->id,1,1,1);
        $this->assertEquals([null,null],$result);
    }

    public function tearDown(): void
    {
        Unit::destroy(array_column($this->data["units"],"id"));
        foreach ($this->data["models"] as $model){
            /** @var OwnerPriceLogistic $item */
            $item = OwnerPriceLogistic::query()->find($model["id"]);
            $item->owners()->sync([]);
            $item->logistics()->sync([]);
        }
        OwnerPriceLogistic::destroy(array_column($this->data["models"],"id"));
        OwnerPriceLogisticDetail::destroy(array_column($this->data["details"],"id"));
        Owner::destroy(array_column($this->data["owners"],"id"));
        Logistic::destroy(array_column($this->data["logistics"],"id"));
        parent::tearDown();
    }
}
