<?php
class Service_Customer extends Common_Service
{
    /**
     * @var null
     */
    public static $_modelClass = null;

    /**
     * @return Table_Customer|null
     */
    public static function getModelInstance()
    {
        if (is_null(self::$_modelClass)) {
            self::$_modelClass = new Table_Customer();
        }
        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 = "customer_id")
    {
        $model = self::getModelInstance();
        return $model->update($row, $value, $field);
    }

    /**
     * @param $value
     * @param string $field
     * @return mixed
     */
    public static function delete($value, $field = "customer_id")
    {
        $model = self::getModelInstance();
        return $model->delete($value, $field);
    }

    /**
     * 共享锁记录
     * @author solar
     * @param int $customer_id
     * @return array
     */
    public static function getLockInShareMode($customer_id) {
        $model = self::getModelInstance();
        return $model->getLockInShareMode($customer_id);
    }

    /**
     * @param $value
     * @param string $field
     * @param string $colums
     * @return mixed
     */
    public static function getByField($value, $field = 'customer_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);
    }

    public static function getLeftJoinByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $order = "")
    {
        $model = self::getModelInstance();
        return $model->getLeftJoinByCondition($condition, $type, $pageSize, $page, $order);
    }

    public static function  getCustomerByShortCode($customer_short){
        $model = self::getModelInstance();
        return $model->getCustomerByShortCode($customer_short);
    }

    /**
     * @param $val
     * @return array
     */
    public static function validator($val)
    {
        $validateArr = $error = array();
        
        $validateArr[] = array("name" =>EC::Lang('email'), "value" =>$val["customer_email"], "regex" => array("require",));
        $validateArr[] = array("name" =>EC::Lang('status'), "value" =>$val["customer_status"], "regex" => array("positive"));
        $validateArr[] = array("name" =>EC::Lang('salesRep'), "value" =>$val["customer_saler_user_id"], "regex" => array("positive"));
        $validateArr[] = array("name" =>EC::Lang('customerServiceRep'), "value" =>$val["customer_cser_user_id"], "regex" => array("positive"));
        return  Common_Validator::formValidator($validateArr);
    }
    /* @todo；更新额外的信息
     * @param array $value
     */
    public static function updateExtraColumn( $value, $customerCode ) {
        if( !is_array( $value ) || empty( $value ) || empty( $customerCode ) )
            return false;
        foreach( $value as $column => $v ) {
            $extraColRow = Service_CustomerExtraCol::getByField( $column, 'cec_col_name' );
            if( empty( $extraColRow ) )
                return false;
            $extraColVal = Service_CustomerExtraColVal::getByCondition( array( 'cec_id' => $extraColRow['cec_id'], 'customer_code' => $customerCode ) );
            if( empty( $extraColVal ) )
                return false;
            $result = Service_CustomerExtraColVal::update( array( 'cecv_value' => $v ), $extraColVal[0]['cecv_id'], 'cecv_id');
            if( !$result )
                return false;
        }
        return true;
    }
    /**
     * @param array $params
     * @return array
     */
    public  function getFields()
    {
        $row = array(
        
              'E0'=>'customer_id',
              'E1'=>'customer_code',
              'E2'=>'customer_password',
              'E3'=>'customer_firstname',
              'E4'=>'customer_lastname',
              'E5'=>'customer_email',
              'E6'=>'customer_currency',
              'E7'=>'customer_activate_code',
              'E8'=>'customer_telephone',
              'E9'=>'customer_fax',
              'E10'=>'customer_status',
              'E11'=>'customer_company_name',
              'E12'=>'customer_logo',
              'E13'=>'customer_saler_user_id',
              'E14'=>'customer_cser_user_id',
              'E15'=>'customer_verify_code',
              'E16'=>'customer_signature',
              'E17'=>'reg_step',
              'E18'=>'customer_reg_time',
              'E19'=>'customer_update_time',
              'E20'=>'password_update_time',
              'E21'=>'last_login_time',
              'E22'=>'financial_service',
              'E23'=>'cash_type',
              'E24'=>'is_frozen',
              'E25'=>'form_type',
        );
        return $row;
    }

    public static function getTestAccount() {
        return array(
            'E0207',
            'E0110',
        );
    }

    /**
     * 判断指定的客户代码是否为测试账号
     *
     * @param string $customerCode    客户代码
     * @return boolean
     */
    public static function isTestAccount($customerCode) {
        $customerCode = strtoupper(trim($customerCode));

        return in_array($customerCode, self::getTestAccount(), TRUE);
    }

}