<?php


namespace Tests\Services\StoreService;


use App\OracleBasCode;
use App\OracleDOCASNHeader;
use App\Owner;
use App\Services\OwnerService;
use App\Services\StoreService;
use App\Services\WarehouseService;
use App\Store;
use App\Warehouse;
use Carbon\Carbon;
use Tests\TestCase;

class GetParamsByAsnHeaderTest extends TestCase
{
    /**
     * @var StoreService $service
     * @var OwnerService $ownerService
     * @var WarehouseService $warehouseService
     *
     */
    public $service;
    public $asnHeaders;
    public $ownerService;
    public $warehouseService;
    private $data=[];
    public function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        cache()->flush();
        $this->service=app(StoreService::class);
        $this->ownerService=app(OwnerService::class);
        $this->warehouseService=app(WarehouseService::class);
        $this->data['warehouse']  = factory(Warehouse::class)->create(['code'=>'test_wh']);
        $this->data['owner'] = factory(Owner::class)->create(['code'=>'test_owner','name'=>'测试货主']);
        $wmsAsnHeader = factory(OracleDOCASNHeader::class)->make([
            'customerid'=>'test_owner','warehouseid'=>'test_wh','asntype'=>'CSRK','asnstatus'=>'11',
        ]);
        $asnType  = factory(OracleBasCode::class)->make(['codeid'=>'ASN_TYP','code'=>'CSRK','descr_c'=>'测试入库']);
        $asnStatus = factory(OracleBasCode::class)->make(['code'=>'11', 'codeid'=>'ASN_STS', 'codename_c'=>'测试状态']);
        $wmsAsnHeader->setRelation('asnType',$asnType);
        $wmsAsnHeader->setRelation('asnStatus',$asnStatus);
        $this->asnHeaders=collect();
        $this->asnHeaders->add($wmsAsnHeader);
    }
    public function testGetParamsByAsnHeader(){
        if (empty($this->asnHeaders))return null;
        $stores = Store::query()->whereIn('asn_code',data_get($this->asnHeaders,'*.asnno'))->get();
        $owner_codes = [];
        $warehouse_codes = [];
        foreach ($this->asnHeaders as $asnHerder) {
            if (!empty($asnHerder['customerid']))
                $owner_codes[$asnHerder['customerid']] = $asnHerder['customerid'];
            if (!empty($asnHerder['warehouseid']))
                $warehouse_codes[$asnHerder['warehouseid']] = $asnHerder['warehouseid'];
        }
        $owners = $this->ownerService->getOwnerByCodes($owner_codes);
        $warehouses = $this->warehouseService->getWareHouseByCodes($warehouse_codes);
        $this->assertNotEmpty($this->asnHeaders);
        $owners_code_map=[];
        foreach ($owners as $owner) {
            $owners_code_map[$owner->code] = $owner;
        }
        $warehouses_code_map=[];
        foreach ($warehouses as $warehouse) {
            $warehouses_code_map[$warehouse->code] = $warehouse;
        }
        if (count($this->asnHeaders)>count($stores)){
            $params=$this->service->getParamsByAsnHeader($this->asnHeaders,$owners_code_map,$warehouses_code_map);
            $this->assertNotEmpty($params);
        }
    }
    public function tearDown(): void
    {
        $this->data['warehouse']->delete();
        $this->data['owner']->delete();
        cache()->flush();
        parent::tearDown(); // TODO: Change the autogenerated stub
    }
}
