<?php

namespace Tests\Services\OrderService;

use App\Order;
use App\Services\OrderService;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class InsertTest extends TestCase
{

    /** @var OrderService $service */
    private $service;
    private $data = [];

    protected function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        $this->service = app('OrderService');
        $orders = factory(Order::class)->times(3000)->make();
        $this->data['orders'] = $orders->map(function ($item){
            return [
            'batch_id' =>$item->batch_id,
            'owner_id' =>$item->owner_id,
            'status' =>$item->status,
            'created_at' =>$item->created_at,
            'code' =>$item->code,
            'shop_id' =>$item->shop_id,
            'client_code' =>$item->client_code,
            'logistic_id' =>$item->logistic_id,
            'consignee_name' =>$item->consignee_name,
            'consignee_phone' =>$item->consignee_phone,
            'province' =>$item->province,
            'city' =>$item->city,
            'district' =>$item->district,
            'address' =>$item->address,
            'warehouse_id' =>$item->warehouse_id,
            'wms_edittime' =>$item->wms_edittime,
            'wms_status' =>$item->wms_status,
            'order_type' =>$item->order_type,
            ];
        })->toArray();
    }

    /**
     * @test
     */
    public function insert()
    {
        $insert_params = array_chunk($this->data['orders'],1000);
        foreach ($insert_params as $insert_param){
            $this->service->insert($insert_param);
        }
        $this->data['order_codes'] = data_get($this->data['orders'],'*.code');

        $orders = Order::query()->whereIn('code',$this->data['order_codes'])->get();
        $this->assertEquals($orders->count(),count($this->data['orders']));
    }

    protected function tearDown(): void
    {
        Order::query()->whereIn('code',$this->data['order_codes'])->delete();
        parent::tearDown(); // TODO: Change the autogenerated stub

    }

}
