<?php

namespace Tests\Services\OrderService\GetParamByOrderHeaderTest;

use App\Commodity;
use App\Logistic;
use App\OracleBasCode;
use App\OracleDOCOrderHeader;
use App\Order;
use App\Owner;
use App\Services\LogisticService;
use App\Services\OracleDOCOrderHeaderService;
use App\Services\OrderIssueService;
use App\Services\OrderService;
use App\Services\OwnerService;
use App\Services\ShopService;
use App\Services\WarehouseService;
use App\Shop;
use App\Warehouse;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class GetParamByOrderHeaderTest extends TestCase
{

    /** @var OrderService $service */
    private $service;
    private $data = [];
    public function setUp(): void
    {
        parent::setUp();
        $this->service = app(OrderService::class);
        $wmsOrderHeader = factory(OracleDOCOrderHeader::class)->make([
            'customerid'=>'TEST_OW','sostatus'=>99,'userdefine1'=>'TEST_CA','warehouseid'=>'WH_TEST'
        ]);

        $orderType = factory(OracleBasCode::class)->make(['code'=>'sysy', 'codeid'=>'SO_TYP', 'codename_c'=>'测试状态']);
        $wmsOrderHeader->setRelation('orderType',$orderType);

        $code_BasCode  = factory(OracleBasCode::class)->make(['codeid'=>'OW','code'=>'99','descr_c'=>'订单完成']);
        $wmsOrderHeader->setRelation('oracleBASCode',$code_BasCode);
        $this->data['orderHeader'] = $wmsOrderHeader;
        $this->data['warehouse']    = factory(Warehouse::class)->create(['code'=>'WH_TEST']);
        $this->data['logistic']     = factory(Logistic::class)->create(['code'=>'TEST_CA','name'=>'测试承运商']);
        $this->data['owner']        = factory(Owner::class)->create(['code'=>'TEST_OW','name'=>'测试货主']);
        $this->data['shop']         = factory(Shop::class)->create(['owner_id'=>$this->data['owner']['id']]);

        $this->mock(OracleDOCOrderHeaderService::class,function($mock)use($wmsOrderHeader){
            $mock->shouldReceive('first')->andReturn($wmsOrderHeader);
        });
    }

    /**
     * @test
     */
    public function testGetParamByOrderHeader()
    {
        $orderHeader = $this->data['orderHeader'] ;
        $orderHeaders = collect([$orderHeader]);
        app(ShopService::class)->getByWmsOrders($orderHeaders);
        app(OwnerService::class)->getByWmsOrders($orderHeaders);
        app(LogisticService::class)->getByWmsOrders($orderHeaders);
        app(WarehouseService::class)->getByWmsOrders($orderHeaders);

        $param = $this->service->getParamByOrderHeader($orderHeader);
        $this->assertNotNull($param);
        $this->assertNotEmpty($param['code']);
        $this->assertNotEmpty($param['owner_id']);
        $this->assertNotNull($param['logistic_id']);
        $this->assertNotEmpty($param['consignee_name']);
        $this->assertNotEmpty($param['consignee_phone']);
        $this->assertNotEmpty($param['province']);
        $this->assertNotEmpty($param['city']);
        $this->assertNotEmpty($param['district']);
        $this->assertNotEmpty($param['address']);
        $this->assertNotEmpty($param['client_code']);
        $this->assertNotEmpty($param['wms_edittime']);
        $this->assertNotEmpty($param['wms_status']);
        $this->assertNotEmpty($param['updated_at']);
        $this->assertNotEmpty($param['created_at']);
        $this->assertNotEmpty($param['warehouse_id']);
    }

    public function tearDown(): void
    {
        cache()->forget("getLogisticByCode_{$this->data['logistic']['code']}");
        cache()->forget("getOwnerByCode_{$this->data['owner']['code']}");
        cache()->forget("getShopByCodeMap_{$this->data['shop']['code']}");
        cache()->forget("WareHouse_{$this->data['warehouse']['code']}");

//        $order = Order::query()->where('code',$this->data['orderHeader']['orderno'])->first();
//        $order->delete();
        $this->data['logistic']->delete();
        $this->data['owner']->delete();
        $this->data['shop']->delete();
        $this->data['warehouse']->delete();
        parent::tearDown(); // TODO: Change the autogenerated stub
    }
}
