<?php
class Service_OtherFee extends Common_Service
{
    /**
     * @var null
     */
    public static $_modelClass = null;

    /**
     * @return Table_ShippingMethod|null
     */
    public static function getModelInstance()
    {
        if (is_null(self::$_modelClass)) {
            self::$_modelClass = new Table_OtherFee();
        }
        return self::$_modelClass;
    }

    /**
     * @param $row
     * @return mixed
     */
    public static function add($row)
    {
        $model = self::getModelInstance();
        return $model->add($row);
    }
	/**
	 * @author william-fan
	 * @todo 用于得到费用单位
	 */
    public static function getFeeUnit(){
    	$feeUnit = array(
    		'1'=>'公斤',
    		'2'=>'单',
    		'3'=>'海关编码',
            '4'=>'平米/月',
            '5'=>'立方米',
            '6'=>'个',
    	    '7'=>'个/日',
    	    '8'=>'个/月'
    	);
    	return $feeUnit;
    }
	/**
	 * @author william-fan
	 * @todo 是否默认费用
	 */
    public static function getDefaultFee(){
    	$defaultFee = array(
    		'0'=>'否',
    		'1'=>'是'	
    	);
    	return $defaultFee;
    }
    /**
     * @param $row
     * @param $value
     * @param string $field
     * @return mixed
     */
    public static function update($row, $value, $field = "fee_id")
    {
        $model = self::getModelInstance();
        return $model->update($row, $value, $field);
    }

    /**
     * @param $value
     * @param string $field
     * @return mixed
     */
    public static function delete($value, $field = "fee_id")
    {
        $model = self::getModelInstance();
        return $model->delete($value, $field);
    }

    /**
     * @param $value
     * @param string $field
     * @param string $colums
     * @return mixed
     */
    public static function getByField($value, $field = 'fee_id', $colums = "*")
    {
        $model = self::getModelInstance();
        return $model->getByField($value, $field, $colums);
    }

    /**
     * @return mixed
     */
    public static function getAll()
    {
        $model = self::getModelInstance();
        return $model->getAll();
    }
    
    /**
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $order
     * @return mixed
     */
    public static function getByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $order = "")
    {
        $model = self::getModelInstance();
        return $model->getByCondition($condition, $type, $pageSize, $page, $order);
    }

    /**
     * @param $val
     * @return array
     */
    public static function validator($val)
    {
        $validateArr = $error = array();

        $validateArr[] = array("name" =>EC::Lang('warehouse'), "value" =>$val["warehouse_id"], "regex" => array("require"));        
        $validateArr[] = array("name" =>EC::Lang('FeeChineseName'), "value" =>$val["fee_name"], "regex" => array("require",));
        $validateArr[] = array("name" =>EC::Lang('FeeCode'), "value" =>$val["fee_code"], "regex" => array("require"));
        $validateArr[] = array("name" =>EC::Lang('Carrier'), "value" =>$val["fee_application_id"], "regex" => array("require"));
        $validateArr[] = array("name" =>EC::Lang('FeeUnit'), "value" =>$val["fee_unit"], "regex" => array("require"));
        $validateArr[] = array("name" =>EC::Lang('price'), "value" =>$val["fee_value"], "regex" => array("require",'positive1'));
        $validateArr[] = array("name" =>EC::Lang('currency'), "value" =>$val["fee_currency_code"], "regex" => array("require"));
        $validateArr[] = array("name" =>EC::Lang('IsDefaultFee'), "value" =>$val["is_default_fee"], "regex" => array("require"));
        $validateArr[] = array("name" =>EC::Lang('discountMin'), "value" =>$val["fee_discount"], "regex" => array("require"));
        $validateArr[] = array("name" =>EC::Lang('status'), "value" =>$val["status"], "regex" => array("require",));
        $validateArr[] = array("name" =>'成本', "value" =>$val["fee_amount"], "regex" => array("require",'positive'));
        $validateArr[] = array("name" =>'服务商', "value" =>$val["sp_code"], "regex" => array("require",));
        return  Common_Validator::formValidator($validateArr);
    }


    /**
     * @param array $params
     * @return array
     */
    public  function getFields()
    {
        $row = array(
        
              'E0'=>'fee_id',
              'E1'=>'fee_name',
              'E2'=>'fee_code',
              'E3'=>'warehouse_id',
              'E4'=>'fee_application_id',
              'E5'=>'fee_unit',
              'E6'=>'fee_value',
              'E7'=>'fee_currency_code',
              'E8'=>'is_default_fee',
              'E9'=>'fee_discount',
              'E10'=>'status',
              'E11'=>'fee_amount',
              'E12'=>'sp_code',
              'E13'=>'cost_currency_code',
              'E14'=>'remark'
        );
        return $row;
    }
	
	
    public static function getJoinApplicationByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $order = "")
    {
        $model = self::getModelInstance();
        return $model->getJoinApplicationByCondition($condition, $type, $pageSize, $page, $order);
    }
    public static function getFeeId($ft_code,$weight){
        $fee_id = 0;
        if(!empty($ft_code)){
           $fee =  Service_OtherFee::getByField($ft_code, 'fee_code');
           if(!empty($fee)){
               $fee_id = $fee['fee_id'];
           }else{
               switch($ft_code){
                   case "BAOGUAN":
                       $weight_1       = 2;    /* 第一重量段：小于等于 2kg */
                       $weight_2       = 80;  /* 第二重量段：小于等于 200kg */
                       if ($weight <= $weight_1) {
                           $ft_code = 'BAOGUAN1';
                       }elseif ($weight>=$weight_1 && $weight<=$weight_2){
                           $ft_code = 'BAOGUAN2';
                       }elseif ($weight >$weight_2){
                           $ft_code = 'BAOGUAN3';
                       }
                       $fee =  Service_OtherFee::getByField($ft_code, 'fee_code');
                       $fee_id = $fee['fee_id'];
                       break;
               }
           }
           return  $fee_id;
        }
        return $fee_id;
    }
}