<?php
/**
 * ----------------------------------------------------------------------------
 * 行到水穷处，坐看云起时
 * 开发软件，找贵阳云起信息科技，官网地址:https://www.56q7.com/
 * ----------------------------------------------------------------------------
 * Author: 老成
 * email：85556713@qq.com
 */
declare(strict_types=1);
namespace app\admin\service;

use app\admin\service\JuShuiTanService;

class FengsuService {

    /** 加收价格 
     * 1.先判断店铺
     * 2.在判断店铺下面的规格
     * @param array $item
     * @return int
     */
    public static function getAdditionalPrice(object $shopList, object $sku, object $productConfig, object $shopDelivery, object $customerSpec, array $item)
    {
        // dump('店铺0');

        $status = 1;
        $shopDelivery=[];
        //判断是否存在店铺
        $shops = $shopList::getSpecsIdByShopId($item['shopId']);
        // dump('店铺00');

        if(!$shops->isEmpty()){
            // dump('店铺1');

            //判断是否存在规格-获取重量
            $specs = $sku::getSpecsIdByShopId($shops->shop_id, $item['tradeOrderPrintVos'][0]['skuId']);
            if(!$specs->isEmpty()) {
                // dump('店铺2');

                //根据规格重量获取，加收价格
                $weight = $productConfig::getWeight($specs->spec_id);
                $status = 3;
                if($weight){
                    // dump('店铺3');

                    $other_price = JuShuiTanService::getRemoteAreaFee($item['tradeOrderPrintVos'][0]['companyName'],  $weight, $item['receiverProvince'], $item['receiverCity']);

                    // 保温加收价格
                    $getPackSpecsFee=JuShuiTanService::getPackSpecsFee($sku,$item['shopId'], $item['tradeOrderPrintVos'][0]['skuId'],$item['receiverProvince']);
                    $pack_specs=strval($getPackSpecsFee['surcharge_money']);

                    // $other_price=(string)$other_price;
                    // $other_price=strval($other_price);
                    // $other_price=bcadd($other_price,$pack_specs, 2);

                    //获取包装箱
                    $box = $customerSpec::getBoxId($shops->customer_id, $specs->variety_id, $specs->spec_id);
                    // $shopDelivery::create([
                    //     'customer_id' => $shops->customer_id,
                    //     'shop_id' => $shops->id,
                    //     'plat_id' => $shops->platform,
                    //     'variety_id'    => $specs->variety_id,
                    //     'spec_id'       => $specs->spec_id,
                    //     'spec_name'     => $specs->spec_name,
                    //     'box_id'        => $box->id??0,
                    //     'box_name'      => $box->box_name??'',
                    //     'variety_name'  => $box->variety_name,
                    //     'num'           => $item['tradeOrderPrintVos'][0]['total'],
                    //     'weigh'         => $weight,
                    //     'price'         => $box->price??0,
                    //     'total_price'   => bcmul($box->price, (string)$item['tradeOrderPrintVos'][0]['total'], 2),
                    //     'company_name'  => $item['tradeOrderPrintVos'][0]['companyName'],
                    //     'waybill_no'    => $item['waybillNo'],
                    //     'region'        => $item['receiverProvince'].' '.$item['receiverCity'],
                    //     'other_price'   => $other_price,
                    //     'ship_date'     => substr($item['consignTime'], 0, 10),
                    // ]);
                    $shopDelivery=[
                        'customer_id' => $shops->customer_id,
                        'shop_id' => $shops->id,
                        'plat_id' => $shops->platform,
                        'variety_id'    => $specs->variety_id,
                        'spec_id'       => $specs->spec_id,
                        'spec_name'     => $specs->spec_name,
                        'box_id'        => $box->id??0,
                        'box_name'      => $box->box_name??'',
                        'variety_name'  => $box->variety_name,
                        'num'           => $item['tradeOrderPrintVos'][0]['total'],
                        'weigh'         => $weight,
                        'price'         => $box->price??0,
                        'total_price'   => bcmul((string)$box->price, (string)$item['tradeOrderPrintVos'][0]['total'], 2),
                        'company_name'  => $item['tradeOrderPrintVos'][0]['companyName'],
                        'waybill_no'    => $item['waybillNo'],
                        'region'        => $item['receiverProvince'].' '.$item['receiverCity'],
                        'other_price'   => $other_price,
                        'ship_date'     => $item['consignTime'],
                        'incubator'   => $getPackSpecsFee['pack_specs_type'],
                        'insulation_money'   => $getPackSpecsFee['surcharge_money'],
                        'labor_cost_money'   => $getPackSpecsFee['data']['labor_cost_money'],
                        'order_status'        => $item['tradeOrderPrintVos'][0]['orderStatus']
                    ];
                }
            }else{
                $status = 2;
            }
        }
        // return $status;
        $data=[
            'shopDelivery'=>$shopDelivery,
            'status'=>$status
        ];
        return $data;
    }

    /**
     * 获取偏远地区加收费用
     * @param string $express 快递公司名称
     * @param string $area 省份
     * @param float $weight 重量
     * @param string $city 城市
     * @return float
     */
    protected static function getRemoteAreaFee(string $express, string $weight, string $area, string $city = ''){
        $remote_area = config('app.remote_area');
        $fee = 0;
        if($express == '顺丰快递'){
            if(in_array($area, $remote_area[1])) {
                if($weight >= 3.5) $fee = 5;
                if($weight >= 4.5) $fee = 6;
            };
        }else{
            //普通(广东省-深圳) -浙江-舟山
            if(in_array($area, $remote_area[0])) $fee = 1.5;
            if($area == '广东省' && $city == '深圳市') $fee = 1.5;
            if($area == '浙江省' && $city == '舟山市') $fee = 1.5;
        }
        return $fee;
    }
}