<?php
/**
 * 客户接口
 */
class Api_ServiceForCustomer
{
    protected $_paramterArr,$_date,$_customer,$_auth,$_error;

    /**
     * [_init 初始化接口数据]
     *
     * @param  [type] $headerRequest [description]
     *
     * @return [type]                [description]
     */
    private function _init ($headerRequest) {
        $this->_customerCode = (string) $headerRequest['customerCode'];
        $this->_operationCustomerCode = (string) $headerRequest['operationCustomerCode'];
        $this->_token = (string) $headerRequest['appToken'];
        $this->_key = (string) $headerRequest['appKey'];
    }

    /**
     * [_authenticate 验证接口数据]
     *
     * @return [type] [description]
     */
    private function _authenticate(){
        if(empty($this->_customerCode) || empty($this->_token) || empty($this->_key)){
            $this->_error = 'HeaderRequest parameters are incorrect.';
            return false;
        }
        $customer = Service_Customer::getByField($this->_customerCode, 'customer_code');
        if (!$customer || $customer['customer_status'] != 2) {
            $this->_error = 'No Authority For ' . $this->_customerCode . '.';
            return false;
        }
        $this->_customer = $customer;
        if(!empty($this->_operationCustomerCode)){
            if(Service_Customer::getByField($this->_operationCustomerCode, 'customer_code') == false){
                $this->_error = 'Customer [' . $this->_operationCustomerCode . '] does not exist .';
                return false;
            }
            if(Service_CustomerRegister::getByCondition(array('regis_customer_code' => $this->_customerCode, 'customer_code' => $this->_operationCustomerCode)) == false){
                $this->_error = 'No authorization operation ' . $this->_operationCustomerCode . '.';
                return false;
            }
        }
        $customerAuth = Service_CustomerApi::getByField($this->_customerCode, 'customer_code');
        if ($customerAuth && $customerAuth['ca_token'] == $this->_token && $customerAuth['ca_key'] == $this->_key) {
            //当前客户代码改为操作客户代码
            if(!empty($this->_operationCustomerCode)){
                $this->_customerCode = $this->_operationCustomerCode;
            }
            $this->_auth = true;
            return true;
        }
        $this->_error = 'App Token/App Key Not match for ' . $this->_customerCode . '.';
        return false;
    }

    /**
     * @todo
     */
    private function common ($paramter) {
        // 对象转数组
        $this->_paramterArr = Common_Common::objectToArray($paramter);
        $this->_init($this->_paramterArr['HeaderRequest']);
        $this->_date = date('Y-m-d H:i:s');
        $this->_authenticate();
    }

    /**
     * [setParamter 获取参数]
     *
     * @param  [type] $paramter [description]
     *
     * @return [type]          [description]
     */
    protected static function setParamter($maps, $paramter){
        if(!is_array($paramter) || empty($paramter)){
            return false;
        }
        $mapsRow = array();
        foreach ($maps as $key => $value) {
            $mapsRow[$value] = empty($paramter[$key]) || !isset($paramter[$key]) ? '' : $paramter[$key];
        }
        return $mapsRow;
    }

    /**
     * [register 用户注册]
     *
     * @param  [type] $request [description]
     *
     * @return [type]          [description]
     */
    public function register($request){
        $result = array('ask' => 0, 'message' => '注册失败', 'error' => array());
        $this->common($request);
        if ($this->_error) {
            $error[] = array('errorCode'=> '1001', 'errorMessage'=>$this->_error);
            $result['error'] = $error;
            return $result;
        }
        //$requestParamter = Common_Common::objectToArray($request);
        //获取数据表对应数据
        $maps = self::regMap();
        $regRow = self::setParamter($maps, $this->_paramterArr['customerInfo']);
        if(is_array($regRow) && !empty($regRow)){
            $regRow['customer_type'] = 0;
            //两步合为一步
            $process = new Service_CustomerApiProcess();
            $result = $process->createTransaction($regRow, $this->_customerCode);
        }
        return $result;
    }

    /**
     * [registerInfo 查询客户备案状态]
     *
     * @param  [type] $request [description]
     *
     * @return [type]          [description]
     */
    public function registerInfo($request){
        $result = array('ask' => 0, 'message' => 'Failed', 'Data' => array(), 'error' => array(''));
        $this->common($request);
        if ($this->_error) {
            $error[] = array('errorCode'=> '1001', 'errorMessage'=>$this->_error);
            $result['error'] = $error;
            return $result;
        }
        //$customerCode = $this->_paramterArr['customerCode'];
        $process = new Service_CustomerApiProcess();
        $result = $process->getRegsterInfo($this->_customerCode);
        return $result;
    }

    /**
     * [regMap 注册接口字段对应关系]
     *
     * @return [type] [description]
     */
    protected static function regMap(){
        return array(
            'username' => 'customer_email',
            'customerCode' => 'customer_code',
            'userpwd' => 'customer_password',
            'repwd' => 're_password',
            'companyName' => 'customer_company_name',
            'tradeName' => 'trade_name',
            'tradeCo' => 'trade_co',
            'currency' => 'customer_currency',
            'license' => 'customer_license',
            'identity' => 'customer_idcard',
            'customlsn' => 'customer_customlsn',
            'firstname' => 'customer_firstname',
            'lastname' => 'customer_lastname',
            'telphone' => 'customer_telephone',
            'countryCode' => 'customer_country',
            'province'=> 'customer_province',
            'city' => 'customer_city',
            'postno' => 'customer_postno',
            'address' => 'customer_address',
            'fax' => 'customer_fax'
        );
    }

    /**
     * [debug description]
     * @param  [type] $msg [description]
     * @return [type]      [description]
     */
    public static function debug($message = '', $preinfo = 'Debug:')
    {
        $filePath = APPLICATION_PATH.'/../data/log/'.'api-soap.php';
        file_put_contents($filePath, $preinfo.'['.date('Y-m-d H:i:s').']'.var_export($message, true)."\n\t", FILE_APPEND);
        die();
    }
}
