<?php


namespace Tests\Services\StoreService;


use App\OracleBasCode;
use App\OracleDOCASNDetail;
use App\OracleDOCASNHeader;
use App\Owner;
use App\Services\CommodityService;
use App\Services\StoreService;
use App\Warehouse;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class CreateStoreTest extends TestCase
{
    /** @var StoreService $service */
    public $service;
    public $asnHeaders;
    public $stores;
    private $data=[];
    public function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        cache()->flush();
        $this->service=app(StoreService::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 testCreateStore(){
        if (!$this->asnHeaders) return null;
        $this->service->createStore($this->asnHeaders);
        $this->stores=$this->service->getByWms($this->asnHeaders);
        $this->assertNotnull($this->stores);
        $this->assertNotNull($this->asnHeaders);
    }
    public function tearDown(): void
    {
        $this->data['warehouse']->delete();
        $this->data['owner']->delete();
        $storeIds=[];
        foreach ($this->stores as $store){
            array_push($storeIds,$store->id);
        }
        $asnDetails=OracleDOCASNDetail::query()->whereIn('asnno',data_get($this->asnHeaders,'*.asnno'))->get();
        $maps = [];
        foreach ($asnDetails as $asnDetail) {
            $value = [
                'owner_code' => $asnDetail->customerid,
                'sku' => $asnDetail->sku,
            ];
            $maps[json_encode($value)] = $value;
        }
        /** CommodityService $commodityService */
        $commodityService=app(CommodityService::class);
        $commodities=$commodityService->getCommoditiesByMaps($maps);
        $commodity_ids=[];
        foreach ($commodities as $commodity){
            array_push($commodity_ids,$commodity->id);
        }
        DB::table('commodities')->whereIn('id',$commodity_ids)->delete();
        DB::table('stores')->whereIn('asn_code',data_get($this->asnHeaders,'*.asnno'))->delete();
        DB::table('store_items')->whereIn('store_id',$storeIds)->delete();
        cache()->flush();
        parent::tearDown(); // TODO: Change the autogenerated stub
    }
}
