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

use app\common\model\ImportList;
use app\common\model\ShopDelivery;
class JuShuiTanService {

    /** 加收价格 
     * 1.先判断店铺
     * 2.在判断店铺下面的规格
     * @param array $item
     * @return int
     */
  

    public static function getAdditionalPrice(object $shopList, object $sku, object $productConfig, object $shopDelivery, object $customerSpec, array $item)
    {
        $status = 1;
        $shopDelivery=[];
        
        //判断是否存在店铺
        $shops = $shopList::getSpecsIdByShopId($item['shopId']);
        if(!$shops->isEmpty()){

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

                //根据规格重量获取，加收价格
                $weight = $productConfig::getWeight($specs->spec_id);
                $status = 3;
                if($weight){
                    $other_price = self::getRemoteAreaFee($item['expressCompany'],  $weight, $item['receiverState'], $item['receiverCity']);

                    // 保温加收价格
                    $getPackSpecsFee=self::getPackSpecsFee($sku,$item['shopId'], $item['disInnerOrderGoodsViewList'][0]['shopSkuId'],$item['receiverState']);
                    $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'        => $specs->box_id??0,
                    //     'box_name'      => $box->box_name??'',
                    //     'variety_name'  => $box->variety_name,
                    //     'num'           => $item['disInnerOrderGoodsViewList'][0]['itemCount'],
                    //     'weigh'         => $weight,
                    //     'price'         => $box->price??0,
                    //     'total_price'   => bcmul($box->price, (string)$item['disInnerOrderGoodsViewList'][0]['itemCount'], 2),
                    //     'company_name'  => $item['expressCompany'],
                    //     'waybill_no'    => ltrim($item['trackNo'], '@'),
                    //     'region'        => $item['receiverState'].' '.$item['receiverCity'],
                    //     'other_price'   => $other_price,
                    //     'ship_date'     => substr($item['deliveryDate'], 0, 10),
                    //     'incubator'   => $getPackSpecsFee['pack_specs_type']
                    // ]);
                    $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'        => $specs->box_id??0,
                        'box_name'      => $box->box_name??'',
                        'variety_name'  => $box->variety_name,
                        'num'           => $item['disInnerOrderGoodsViewList'][0]['itemCount'],
                        'weigh'         => $weight,
                        'price'         => $box->price??0,
                        'total_price'   => bcmul((string)$box->price, (string)$item['disInnerOrderGoodsViewList'][0]['itemCount'], 2),
                        'company_name'  => $item['expressCompany'],
                        // 'waybill_no'    => ltrim($item['trackNo'], '@'),
                        'waybill_no'    => $item['trackNo'],
                        'region'        => $item['receiverState'].' '.$item['receiverCity'],
                        'other_price'   => $other_price,
                        'ship_date'     => $item['deliveryDate'],
                        'incubator'   => $getPackSpecsFee['pack_specs_type'],
                        'insulation_money'   => $getPackSpecsFee['surcharge_money'],
                        'labor_cost_money'   => $getPackSpecsFee['data']['labor_cost_money'],
                        'order_status'   => $item['disInnerOrderGoodsViewList'][0]['status'],
                        
                    ];
                }
            }else{
                $status = 2;
            }
        }
        $data=[
            'shopDelivery'=>$shopDelivery,
            'status'=>$status
        ];
        return $data;
        // return $status;
    }


    /** 编辑规格-加收价格 
     * @param array $item
     * @return int
     */
    public static function setAdditionalPrice(object $sku,object $shopList, object $productConfig, object $shopDelivery, object $customerSpec, string $shop_id,$sku_id, $variety_id, $spec_id, string $spec_name, object $item)
    {
        //判断是否存在规格-获取重量
        $specs = $sku::getSpecsIdByShopId($shop_id, $sku_id);
        //判断是否存在店铺
        $shops = $shopList::getSpecsIdByShopId($shop_id);
     
        //根据规格重量获取，加收价格
        $weight = $productConfig::getWeight((int)$spec_id);
    
        
        $other_price =self::getRemoteAreaFee($item['company_name'],  $weight??0, $item['province'], $item['city']);

        // 保温加收价格
        $getPackSpecsFee=self::getPackSpecsFee($sku,$shop_id, $sku_id,$item['province']);
        $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, (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'     => $specs->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),
        //     'incubator'   => $getPackSpecsFee['pack_specs_type']
            
           
            
        // ]);
        $shopDelivery=[
            'customer_id'   => $shops->customer_id,
            'shop_id'       => $shops->id,
            'plat_id'       => $shops->platform,
            'variety_id'    => $variety_id,
            'spec_id'       => $spec_id,
            'spec_name'     => $specs->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'     => $item['consign_time'],
            'incubator'   => $getPackSpecsFee['pack_specs_type'],
            'insulation_money'   => $getPackSpecsFee['surcharge_money'],
            'labor_cost_money'   => $getPackSpecsFee['data']['labor_cost_money'],
            'order_status'   => $item['order_status'],
           
            
        ];
        $data=[
            'shopDelivery'=>$shopDelivery,
            'getPackSpecsFee'=>$getPackSpecsFee
        ];
        return $data;
    }

    /**
     * 获取偏远地区加收费用
     * @param string $express 快递公司名称
     * @param string $area 省份
     * @param float $weight 重量
     * @param string $city 城市
     * @return float
     */
    public static function getRemoteAreaFee(string $express, string $weight, string $area, string $city = ''){
        // $remote_area = config('app.remote_area');
        $remote_area=[];
        $remote_area[0]=site_config("addonsd.remote_area_0");
        $remote_area[1]=site_config("addonsd.remote_area_1");
        $fee = 0;
        if($express == '顺丰快递'||$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;
    }
    //防止重复记录，订单号+快递单号
    public static function prevent_duplicate_additions($sku_id,$waybill_no){
        $importList=new ImportList();
        $where = [
            'sku_id' => ['=', $sku_id],
            'waybill_no' => ['=', $waybill_no]
        ];
        $result = $importList->where($where)->count();
        return $result=$result>0?true:false;
    }
    // 保温加收区域设置，加收金额
    public static function getPackSpecsFee($sku,$shop_id,$sku_id,$province){
        $res=[
            'data'=>[
                'id'=>0,
                'title'=>'',
                'labor_cost_money'=>0,
            ],
            'one_surcharge_money'=>0,
            'two_surcharge_money'=>0,
            'pack_specs_type'=>0,
            'surcharge_money'=>0
        ];
        $pack_specs_one_region=site_config("addonsd.pack_specs_one_region");
        $pack_specs_two_region=site_config("addonsd.pack_specs_two_region");
        $pack_specs_one_type=in_array($province,$pack_specs_one_region);
        $pack_specs_two_type=in_array($province,$pack_specs_two_region);
        $pack_specs_type=0;
        if($pack_specs_one_type){
            $pack_specs_type=1;
        }else if($pack_specs_two_type){
            $pack_specs_type=2;
        }
        // if(empty($pack_specs_type))return $res;
        $result=$sku::getPackSpecs($shop_id, $sku_id);
        if(empty($result)) return $res;
        $surcharge_money=0;
        $one_surcharge_money=0;
        $two_surcharge_money=0;
        switch ($pack_specs_type) {
            case 1:
                $surcharge_money=$result->one_surcharge_money;
                $one_surcharge_money=$surcharge_money;
                break;
            case 2:
                $surcharge_money=$result->two_surcharge_money;
                $two_surcharge_money=$surcharge_money;
                break;
            default:
                $surcharge_money=0;
                break;
        }
        $res=[
            'data'=>$result,
            'pack_specs_type'=>$pack_specs_type,
            'one_surcharge_money'=>$one_surcharge_money,
            'two_surcharge_money'=>$two_surcharge_money,
            'surcharge_money'=>$surcharge_money
            
        ];
        
        return $res;
    }
    // 根据快递订单号获取发货记录中的订单id
    
    public static function get_shopDelivery_id($waybill_no){
        $shopDeliveryModel=new ShopDelivery();
        $order_data=$shopDeliveryModel->where('waybill_no',$waybill_no)->find();
        if(empty($order_data)){
            return 0;
        }else{
            return $order_data['id'];
        }
        
    }
}