<?php

namespace App\Services\Business\MiaoXuan;

use App\Models\BusinessGoodsSku;
use App\Models\BusinessOrderItem;
use App\Models\GoodsSku;
use App\Services\Business\BusinessClient;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;

class MiaoXuan extends BusinessClient
{
    public $domain = "http://shop.chutang66.com";

    public function auth()
    {
        // TODO: Implement auth() method.
    }

    public function downloadGoodsListAndBind($activityNo)
    {
    }

    public function bindGoods($goods)
    {
        Goods::bindGoods($goods, $this->shop->id);
    }

    public function incrQuantity($businessGoodsSku, $num, $incremental)
    {
        $appendParams = Goods::incrQuantity($this->shop->id, $num, $businessGoodsSku);
        $url = 'http://shop.chutang66.com/miaoxuan/stock';
        $this->formDataPostRequest($url, $appendParams);
    }

    public function downloadOrdersAndSave($beginTime, $endTime, $downloadType = 'default', $page = 1)
    {
    }

    public function downloadAfterSaleOrdersAndSave($beginTime, $endTime, $page = 1)
    {

    }

    public function queryStatusAndSync($order)
    {
        $appendParams = ["order_sn" => $order->order_sn, "type" => "erpQuery"];
        $url = 'http://shop.chutang66.com/miaoxuan/queryOrder';
        $queryOrder = $this->formDataPostRequest($url, $appendParams);
        $needUpdate = false;
        if ($order->shipping_status != $queryOrder['shipping_status']) {
            $order->shipping_status = $queryOrder['shipping_status'];
            $needUpdate = true;
        }
        if ($order->cancel_status != $queryOrder['cancel_status']) {
            $order->cancel_status = $queryOrder['cancel_status'];
            $needUpdate = true;
        }
        if ($order->after_sales_status != $queryOrder['after_sales_status']) {
            //售后状态更新
            $order->after_sales_status = $queryOrder['after_sales_status'];
            $needUpdate = true;
        }
        if ($needUpdate) {
            $goodsSkuNum = 0;
            foreach ($queryOrder['sub_order_list'] as $item) {
                $orderItem = BusinessOrderItem::firstOrNew(['shop_id' => $order->shop_id, 'business_order_id' => $order->id, 'goods_id' => $item['goods_id'], 'sku_id' => $item['sku_id']], $item);
                if ($item['external_sku_id']) {
                    $goodsSku = GoodsSku::query()
                        ->with('combinationGoods')
                        ->where('external_sku_id', $item['external_sku_id'])
                        ->first('external_sku_id');
                    $combinationNum = $goodsSku ? ($goodsSku->combinationGoods->count() ?: 1) : 1;
                    $goodsSkuNum += $combinationNum;
                } else {
                    $goodsSkuNum++;
                }

                if (!empty($orderItem->id)) {
                    $orderItem->update($item);
                }
            }
            $order->goods_sku_num = $goodsSkuNum;
            $order->save();
        }
    }

    public function batchIncrQuantity($businessGoodsSkus, $num, $incremental)
    {
        $batchAppendParams = [];
        foreach ($businessGoodsSkus as $businessGoodsSku) {
            $appendParams = Goods::incrQuantity($this->shop->id, $num, $businessGoodsSku);
            $batchAppendParams[] = $appendParams;
        }
        $this->batchAsyncPostRequest('http://shop.chutang66.com/miaoxuan/stock', $batchAppendParams);
    }

    public function downloadGoods($skuId)
    {
        // TODO: Implement downloadGoods() method.
    }

    public function createLogistic($orderSn, $waybillNo)
    {
        $path = "/miaoxuan/ship";
        $url = $this->domain . $path;
        $appendParams = [
            "data" => [
                "order_sn" => $orderSn,
                "ship_sn" => $waybillNo,
                "ship_time" => Carbon::now()->toDateTimeString(),
                "company_code" => "JD",
            ],
            "type"=>"miaoxuan.ship"
        ];
        $res =$this->formDataPostRequest($url, $appendParams);
        Log::info("同步物流发货信息".$orderSn,[$res,$appendParams]);
    }

    public function cancelLogistic($orderSn, $waybillNo)
    {
        // TODO: Implement cancelLogistic() method.
    }
}
