<?php
class Service_Currency extends Common_Service
{
    /**
     * @var null
     */
    public static $_modelClass = null;

    /**
     * @return Table_Currency|null
     */
    public static function getModelInstance()
    {
        if (is_null(self::$_modelClass)) {
            self::$_modelClass = new Table_Currency();
        }
        return self::$_modelClass;
    }

    /**
     * @param $row
     * @return mixed
     */
    public static function add($row)
    {
        $model = self::getModelInstance();
        return $model->add($row);
    }


    /**
     * @param $row
     * @param $value
     * @param string $field
     * @return mixed
     */
    public static function update($row, $value, $field = "currency_id")
    {
        $model = self::getModelInstance();
        return $model->update($row, $value, $field);
    }

    /**
     * @param $value
     * @param string $field
     * @return mixed
     */
    public static function delete($value, $field = "currency_id")
    {
        //$model = self::getModelInstance();
        //return $model->delete($value, $field);
        //基础数据已经建立，不允许删除
        return false;
    }

    /**
     * @param $value
     * @param string $field
     * @param string $colums
     * @return mixed
     */
    public static function getByField($value, $field = 'currency_id', $colums = "*")
    {
        $model = self::getModelInstance();
        return $model->getByField($value, $field, $colums);
    }

    /**
     * @return mixed
     */
    public static function getAll()
    {
        $model = self::getModelInstance();
        return $model->getAll();
    }
    
    public static function getKeyVal() {
    	$aKeyVal = array();
    	$list = self::getAll();
    	foreach($list as $row)
    		$aKeyVal[$row['currency_code']] = $row['currency_hs_code'];
    	return $aKeyVal;
    }

    /**
     * @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('currencyName'), "value" =>$val["currency_name_en"], "regex" => array("require",));
        $validateArr[] = array("name" =>EC::Lang('currencyNameEn'), "value" =>$val["currency_name"], "regex" => array("require",));
        $validateArr[] = array("name" =>EC::Lang('currencyCode'), "value" =>$val["currency_code"], "regex" => array("require",));
        $validateArr[] = array("name" =>EC::Lang('currencyDecimalPlaces'), "value" =>$val["currency_decimal_places"], "regex" => array("positive"));
        $validateArr[] = array("name" =>EC::Lang('currencyRate'), "value" =>$val["currency_rate"], "regex" => array("require","positive1",));
        return  Common_Validator::formValidator($validateArr);
    }


    /**
     * @param array $params
     * @return array
     */
    public  function getFields()
    {
        $row = array(
        
              'E0'=>'currency_id',
              'E1'=>'currency_name_en',
              'E2'=>'currency_name',
              'E3'=>'currency_code',
              'E4'=>'currency_symbol_left',
              'E5'=>'currency_symbol_right',
              'E6'=>'currency_decimal_point',
              'E7'=>'currency_thousands_point',
              'E8'=>'currency_decimal_places',
              'E9'=>'currency_rate',
              'E10'=>'currency_update_time',
              'E11'=>'currency_add_time',
              'E12'=>'currency_stop_time',
        );
        return $row;
    }
    /**
     * @author william-fan
     * @todo 币种转换
     * @param $time_or_batch 费用发生的时间，或者货币汇率的批次标记
     */
    public static function converByCode($code_start,$code_end,$value,$time_or_batch=''){
    	if($code_start == '' || $code_end == '' || $value == ''){
    		return 0;
    	}
    	if($code_start == $code_end){
    		return $value;
    	}
        if(empty($time_or_batch)){
            $row_start  = self::getByField($code_start,'currency_code');
            $row_end    = self::getByField($code_end,'currency_code');
        } else {
            if(preg_match("/^[0-9]{4}(\-)[0-9]{1,2}(\-)[0-9]{1,2}([0-9]{1,2}(:[0-9]{1,2}(:[0-9]{1,2})))$/",$time_or_batch)){
                //Y-m-d H:i:s 取货币历史表
                $row_from   = Service_CurrencyLog::getByCondition(array('currency_code'=>$code_from, 'currency_date'=>$time_or_batch));
                $row_to     = Service_CurrencyLog::getByCondition(array('currency_code'=>$code_to, 'currency_date'=>$time_or_batch));
            } else if(preg_match("/^[0-9]{11}$/",$time_or_batch)){
                //按汇率编号查询 取货币历史表
                $row_start  = Service_CurrencyLog::getByCondition(array('currency_code'=>$code_from, 'currency_batch_number'=>$time_or_batch));
                $row_end    = Service_CurrencyLog::getByCondition(array('currency_code'=>$code_to, 'currency_batch_number'=>$time_or_batch));
            } else {
                //参数无效，直接取当前汇率
                $row_start  = self::getByField($code_from,'currency_code');
                $row_end    = self::getByField($code_to,'currency_code');
            }
        }
    	if(empty($row_start) || empty($row_end)){
    		return 0;
    	}
    	$transValue = ($value*$row_start['currency_rate'])/$row_end['currency_rate'];
    	return sprintf('%.3f',$transValue);
    }

}