<?php

namespace Tests\Services\OrderService;

use App\Logistic;
use App\OracleBasCode;
use App\OracleBasCustomer;
use App\OracleDOCOrderHeader;
use App\Order;
use App\Owner;
use App\Services\OrderService;
use App\Shop;
use App\Warehouse;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Str;
use Tests\TestCase;

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

        $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['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']]);
    }
    /**
     * @test
     */
    public function createOrFindOrder()
    {
        $this->orderService->createOrFindOrder($this->data['orderHeader']);
        /** @var Order $order */
        $order = Order::query()->where('code',$this->data['orderHeader']['orderno'])->first();
        $this->assertNotNull($order);
        $this->assertEquals($order->code,$this->data['orderHeader']['orderno']);
        $this->assertEquals($order->client_code,$this->data['orderHeader']['soreference1']);
    }

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

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