<?php

namespace Tests\Services\OrderPackageCommoditiesService\RegroupOrderCommoditiesTest;

use App\Order;
use App\OrderPackage;
use App\OrderPackageCommodities;
use App\Services\OracleDOCOrderHeaderService;
use App\Services\OrderPackageCommoditiesService;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class RegroupOrderCommoditiesTest extends TestCase
{
    /**
     * @var OrderPackageCommoditiesService $service
     * @var OracleDOCOrderHeaderService $orderHeaderService
     */
    public $service;
    public $orderHeaderService;

    public function setUp(): void
    {
        parent::setUp();
        $this->service = app('OrderPackageCommoditiesService');
        $this->orderHeaderService = app('OracleDocOrderHeaderService');
    }

    public function testRegroupOrderCommodities()
    {
        /*
        * createOrderPackage($orderHeaders,$orders,$packages)
        * */
        $carbon =Carbon::now()->subHours(10);
        $date = '2020-06-18 18:13:50';
        $orderHeaders = $this->orderHeaderService->getWMSOrderOnStartDate($date);
        if($orderHeaders->count()==0){
            var_dump('查询数据为空');
            return;
        }
        $order_nos = Order::all()->map(function($order){
            return $order->code;
        });
        dd(Order::all()->map(function($order){
            return $order->created_at;
        }));

        $order_nos = data_get($orderHeaders,'*.orderno');
        $order_commodities = OrderPackageCommodities::query()->with(['commodity','package.order'])->whereHas('package',function($query)use($order_nos){
            $query->whereHas('order',function ($query)use($order_nos){
                $query->whereIn('code',$order_nos);
            });
        })->get();
        $orders = Order::query()->whereIn('code',$order_nos)->get();


        $order = Order::query()->whereIn('code',$order_nos)->sql();
        $params = $this->service->regroupOrderCommodities($order_commodities);
        $_params = [];
        $orderHeaders->each(function($order_header)use(&$_params){
            $order_header->actAllocationDetails->each(function($detail)use(&$_params){
                $order_no = $detail->orderno;
                $logistic_number = $detail->picktotraceid;
                $sku = $detail->sku;
                $key = ' orderno='.$order_no.' logsitic_number='.$logistic_number.' sku='.$sku.' ';
                if(isset($_params[$key])){
                    $_params[$key]['amount'] += $detail->qty_each;
                }else{
                    $_params[$key] = [
                        'orderno'=>$order_no,
                        'logistic_number'=>$logistic_number,
                        'sku' => $sku,
                        'amount' => $sku,
                        'owner_code' => $detail->customerid
                    ];
                }
            });
        });

        foreach ($_params as $key=>$param) {
            $this->assertNotEmpty($params[$key]);
        }

    }
}
