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



class FengsuService {

    /** 加收价格 
     * 1.先判断店铺
     * 2.在判断店铺下面的规格
     * @param array $item
     * @return int
     */
    public static function getAdditionalPrice(object $shopList, object $fengsuSku, object $productConfig, object $shopDelivery, object $customerSpec, array $item):int
    {
        $status = 1;
        //判断是否存在店铺
        $shops = $shopList::getSpecsIdByShopId($item['shopId']);
        if(!$shops->isEmpty()){
            //判断是否存在规格-获取重量
            $specs = $fengsuSku::getSpecsIdByShopId($shops->shop_id, $item['tradeOrderPrintVos'][0]['skuId']);
            if(!$specs->isEmpty()) {
                //根据规格重量获取，加收价格
                $weight = $productConfig::getWeight($specs->spec_id);
                $status = 3;
                if($weight){
                    $other_price = self::getRemoteAreaFee($item['tradeOrderPrintVos'][0]['companyName'],  $weight, $item['receiverProvince'], $item['receiverCity']);
                    //获取包装箱
                    $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),
                    ]);
                }
            }else{
                $status = 2;
            }
        }
        return $status;
    }


    /** 编辑规格-加收价格 
     * @param array $item
     * @return int
     */
    public static function setAdditionalPrice(object $shopList, object $productConfig, object $shopDelivery, object $customerSpec, string $shop_id, $variety_id, $spec_id, string $spec_name, object $item):bool
    {
        //判断是否存在店铺
        $shops = $shopList::getSpecsIdByShopId($shop_id);
     
        //根据规格重量获取，加收价格
        $weight = $productConfig::getWeight((int)$spec_id);
    
        $other_price = self::getRemoteAreaFee($item['company_name'],  $weight??0, $item['province'], $item['city']);
        //获取包装箱
        $box = $customerSpec::getBoxId($shops->customer_id, (int)$variety_id, (int)$spec_id);
        $shopDelivery::create([
            'customer_id'   => $shops->customer_id,
            'shop_id'       => $shops->id,
            'plat_id'       => $shops->platform,
            'variety_id'    => $variety_id,
            'spec_id'       => $spec_id,
            'spec_name'     => $spec_name,
            'box_id'        => $box->id??0,
            'box_name'      => $box->box_name??'',
            'variety_name'  => $box->variety_name,
            'num'           => $item['num'],
            'weigh'         => $weight??0,
            'price'         => $box->price??0,
            'total_price'   => bcmul((string)$box->price, (string)$item['num'], 2),
            'company_name'  => $item['company_name'],
            'waybill_no'    => $item['waybill_no'],
            'region'        => $item['province'].' '.$item['city'],
            'other_price'   => $other_price,
            'ship_date'     => substr($item['consign_time'], 0, 10),
        ]);
        return true;
    }

    /**
     * 获取偏远地区加收费用
     * @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;
    }

}