<?php

namespace Tests\Feature;

use App\Jobs\LogisticZopSync;
use App\Order;
use App\OrderPackage;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class LogisticZopSyncTest extends TestCase
{
    protected $order;
    /**
     * A basic feature test example.
     *
     * @return void
     */
//    public function testExample()
//    {
//        LogisticZopSync::dispatch('73228022760994');
//    }

    protected function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        OrderPackage::query()->where('logistic_number', '000000111')->delete();
    }


    protected function tearDown(): void
    {
        OrderPackage::query()->where('logistic_number', '000000111')->delete();
        $this->order && $this->order->delete();
        parent::tearDown(); // TODO: Change the autogenerated stub
    }

    public function test_get()
    {
        LogisticZopSync::dispatch('75467972757443');
    }


    /**
     * @test
     */
    public function transformRoutes疑似库内丢件Test()
    {
        $order = factory(Order::class)->create(['province' => '上海']);
        $this->order = $order;
        factory(OrderPackage::class)->create(['logistic_number' => '000000111', 'sent_at' => now()->subHours(5),'order_id' => $order->id]);
        $logisticZopSync = new LogisticZopSync('000000111');
        $data[] = [
            'routes' => [
            ],
            "logisticNum" => "000000111",
        ];
        $result = $logisticZopSync->transformRoutes($data);
        $this->assertEquals('疑似库内丢件', $result[0]['exception_type']);
    }

    /**
     * @test
     */
    public function transformRoutes中转异常Test()
    {
        $order = factory(Order::class)->create(['province' => '上海']);
        $this->order = $order;
        factory(OrderPackage::class)->create(['logistic_number' => '000000111', 'sent_at' => now()->subHours(5), 'order_id' => $order->id]);

        $logisticZopSync = new LogisticZopSync('000000111');
        $data[] = [
            'routes' => [
                json_decode($this->itemScanDate25hBefore()),
            ],
            "logisticNum" => "000000111",
        ];
        $result = $logisticZopSync->transformRoutes($data);
        $this->assertEquals('中转异常', $result[0]['exception_type']);
    }

    /**
     * @test
     */
    public function transformRoutes疑似丢件Test()
    {
        $order = factory(Order::class)->create(['province' => '上海']);
        $this->order = $order;

        factory(OrderPackage::class)->create(['logistic_number' => '000000111', 'sent_at' => now()->subHours(5), 'order_id' => $order->id]);
        $logisticZopSync = new LogisticZopSync('000000111');
        $data[] = [
            'routes' => [
                json_decode($this->itemScanDate73hBefore()),
            ],
            "logisticNum" => "000000111",
        ];
        $result = $logisticZopSync->transformRoutes($data);
        $this->assertEquals('疑似丢件', $result[0]['exception_type']);
    }

    /**
     * @test
     */
    public function transformRoutes派送异常Test()
    {
        $order = factory(Order::class)->create(['province' => '上海']);
        $this->order = $order;

        factory(OrderPackage::class)->create(['logistic_number' => '000000111', 'sent_at' => now()->subHours(5), 'order_id' => $order->id]);
        $logisticZopSync = new LogisticZopSync('000000111');
        $data[] = [
            'routes' => [
                json_decode($this->itemScanDate49hBefore()),
            ],
            "logisticNum" => "000000111",
        ];
        $result = $logisticZopSync->transformRoutes($data);
        $this->assertEquals('派件异常', $result[0]['exception_type']);
    }

    /**
     * @return false|string
     */
    private function itemScanDate25hBefore()
    {
        return json_encode([
            'country' => 'China',
            'signMan' => null,
            'operateUserPhone' => '17717571635',
            'weight' => null,
            'billCode' => null,
            'operateUserCode' => '02141',
            'preOrNextSite' => null,
            'scanDate' => now()->subHours(25)->getTimestamp() * 1000,
            'scanSite' => [
                'isCenter' => 'F',
                'code' => '02141',
                'isTransfer' => 0,
                'phone' => '60664064',
                'city' => '""',
                'name' => '九亭',
                'siteId' => '2745',
                'id' => '2745',
                'prov' => '上海',
            ],
            'extend' => "{'optReasonEn':'Parcel scanned by site'}",
            'operateUser' => '笔记本',
            'scanType' => '收件',
        ]);
    }

    /**
     * @return false|string
     */
    private function itemScanDate73hBefore()
    {
        return json_encode([
            'country' => 'China',
            'signMan' => null,
            'operateUserPhone' => '17717571635',
            'weight' => null,
            'billCode' => null,
            'operateUserCode' => '02141',
            'preOrNextSite' => null,
            'scanDate' => now()->subHours(73)->getTimestamp() * 1000,
            'scanSite' => [
                'isCenter' => 'F',
                'code' => '02141',
                'isTransfer' => 0,
                'phone' => '60664064',
                'city' => '""',
                'name' => '九亭',
                'siteId' => '2745',
                'id' => '2745',
                'prov' => '上海',
            ],
            'extend' => "{'optReasonEn':'Parcel scanned by site'}",
            'operateUser' => '笔记本',
            'scanType' => '收件',
        ]);
    }

    /**
     * @return false|string
     */
    private function itemScanDate49hBefore()
    {
        return json_encode([
            'country' => 'China',
            'signMan' => null,
            'operateUserPhone' => '17717571635',
            'weight' => null,
            'billCode' => null,
            'operateUserCode' => '02141',
            'preOrNextSite' => null,
            'scanDate' => now()->subHours(49)->getTimestamp() * 1000,
            'scanSite' => [
                'isCenter' => 'F',
                'code' => '02141',
                'isTransfer' => 0,
                'phone' => '60664064',
                'city' => '""',
                'name' => '九亭',
                'siteId' => '2745',
                'id' => '2745',
                'prov' => '上海',
            ],
            'extend' => "{'optReasonEn':'Parcel scanned by site'}",
            'operateUser' => '笔记本',
            'scanType' => '派件',
        ]);
    }

    /**
     * @return false|string
     */
    private function item2()
    {
        return json_encode([]);
    }

    /**
     * @test
     */
    public function arr_test()
    {
        $data = [];
        $data['aa'] = "vbbb";
        dd(array_key_exists('aa',$data));
    }
}
