<?php
class Service_OrderAddressBook extends Common_Service
{
    /**
     * @var null
     */
    public static $_modelClass = null;

    /**
     * @return Table_OrderAddressBook|null
     */
    public static function getModelInstance()
    {
        if (is_null(self::$_modelClass)) {
            self::$_modelClass = new Table_OrderAddressBook();
        }
        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 = "oab_id")
    {
        $row['oab_update_time']=date('Y-m-d H:i:s');
        $model = self::getModelInstance();
        return $model->update($row, $value, $field);
    }

    /**
     * @param $value
     * @param string $field
     * @return mixed
     */
    public static function delete($value, $field = "oab_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 = 'oab_id', $colums = "*")
    {
        $model = self::getModelInstance();
        return $model->getByField($value, $field, $colums);
    }
    
    /**
     * @param array $where
     * @param string $colums
     */
    public static function getByWhere($where, $colums = "*")
    {
    	$model = self::getModelInstance();
    	return $model->getByWhere($where, $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" =>'收件人', "value" =>$val["oab_firstname"], "regex" => array("require",));
        $validateArr[] = array("name" =>'订单号', "value" =>$val["order_id"], "regex" => array("require",));
        $validateArr[] = array("name" =>'地址1', "value" =>$val["oab_street_address1"], "regex" => array("require",));
        return  Common_Validator::formValidator($validateArr);
    }


    /**
     * @param array $params
     * @return array
     */
    public  function getFields()
    {
        $row = array(
        
              'E0'=>'oab_id',
              'E1'=>'order_id',
              'E2'=>'order_code',
              'E3'=>'oab_firstname',
              'E4'=>'oab_lastname',
              'E5'=>'oab_company',
              'E6'=>'oab_country_id',
              'E7'=>'oab_zone_id',
              'E8'=>'oab_postcode',
              'E9'=>'oab_state',
              'E10'=>'oab_city',
              'E11'=>'oab_suburb',
              'E12'=>'oab_street_address1',
              'E13'=>'oab_street_address2',
              'E14'=>'oab_phone',
              'E15'=>'oab_cell_phone',
              'E16'=>'oab_fax',
              'E17'=>'oab_email',
              'E18'=>'oab_note',
              'E19'=>'oa_modify_time',
        );
        return $row;
    }

    /**
     * @desc 修改订单收件人信息/更新订单打印次数
     * @param array $params
     */
    public static function edit($params = array())
    {
        $return = array(
            'state' => 0,
            'message' => '',
            'errorMessage' => array('操作失败')
        );
        $row = array(
            'oab_id' => '',
            'order_id' => '',
            'oab_firstname' => '',
            'oab_company' => '',
            'oab_postcode' => '',
            'oab_state' => '',
            'oab_city' => '',
            'oab_street_address1' => '',
            'oab_street_address2' => '',
            'oab_phone' => '',
            'oab_fax' => '',
            'oab_email' => '',
            'oab_cell_phone' =>''
        );
        $addrObj = new Service_OrderAddressBook();
        $row = $addrObj->getMatchEditFields($params, $row);
       // $deliveryAddr = trim($params['delivery_address']);
        //echo '<pre>',print_r($params['delivery_address'],TRUE),'</pre>';
        unset($params);
        $paramId = $row['oab_id'];

        $errorArr = self::validator($row);
        unset($row['oab_id'], $row['order_id']);

        if (!empty($errorArr)) {
            $return = array(
                'state' => 0,
                'message' => '',
                'errorMessage' => $errorArr
            );
            return $return;
        }

        // Update delivery_address
       /* Service_Orders::update(array(
          'delivery_address' => $deliveryAddr,
        ), $row['order_id'], 'order_id');*/

        unset($smRow);
        $addrRow = array();
        if (!empty($paramId) && $addrRow = self::getByField($paramId)) {
            $row['oab_lastname'] = ''; //已合并
            $result = self::update($row, $paramId);
        } else {
            $errorArr[] = '操作失败';
            $return = array(
                'state' => 0,
                'message' => '',
                'errorMessage' => $errorArr
            );
            return $return;
        }
        if ($result) {
            $orderRow = Service_Orders::getByField($addrRow['order_id'], 'order_id');
            if (empty($orderRow)) {
                return $return;
            }
            $return['state'] = 1;
            $return['message'] = array('Success.');
            if ($orderRow['print_quantity'] != '0') {
                Service_Orders::update(array('print_quantity' => 0,'update_time'=>date('Y-m-d H:i:s')), $addrRow['order_id'], 'order_id');
            }
            $row = array(
                'order_id' => $addrRow['order_id'],
                'order_code' => $addrRow['order_code'],
                'ol_type' => 1,
                'order_status_from' => $orderRow['order_status'],
                'order_status_to' => $orderRow['order_status'],
                'user_id' => Service_User::getUserId(),
                'ol_ip' => Common_Common::getIP(),
                'ol_comments' => 'edit order address',
            );
            Service_OrderLog::add($row);
        }
        return $return;
    }


}