<?php
class Table_Customer
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_Customer();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_Customer();
    }

    /**
     * @param $row
     * @return mixed
     */
    public function add($row)
    {
        return $this->_table->insert($row);
    }


    /**
     * @param $row
     * @param $value
     * @param string $field
     * @return mixed
     */
    public function update($row, $value, $field = "customer_id")
    {
        $where = $this->_table->getAdapter()->quoteInto("{$field}= ?", $value);
        return $this->_table->update($row, $where);
    }

    /**
     * @param $value
     * @param string $field
     * @return mixed
     */
    public function delete($value, $field = "customer_id")
    {
        $where = $this->_table->getAdapter()->quoteInto("{$field}= ?", $value);
        return $this->_table->delete($where);
    }
    
    public function getLockInShareMode($customer_id) {
    	$sql = 'SELECT * FROM customer WHERE customer_id='.$customer_id.' LOCK IN SHARE MODE;';
    	return $this->_table->getAdapter()->fetchRow($sql);
    }

    public function getCustomerByShortCode($customer_short){
        $sql = "SELECT * FROM customer WHERE customer_short='".$customer_short."' order by customer_id asc LOCK IN SHARE MODE;";
        return $this->_table->getAdapter()->fetchRow($sql);
    }

    /**
     * @param $value
     * @param string $field
     * @param string $colums
     * @return mixed
     */
    public function getByField($value, $field = 'customer_id', $colums = "*")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $colums);
        $select->where("{$field} = ?", $value);
        return $this->_table->getAdapter()->fetchRow($select);
    }

    public function getAll()
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, "*");
        return $this->_table->getAdapter()->fetchAll($select);
    }
    
    /**
     * 构造自定义SQL语句查询
     *
     * @access	public
     * @param   string	($field		待返回的字段，默认为所有)
     * @param   string	($condition	查询条件)
     * @param	mixed	($value		待转换查询字段值)
     * @return	mixed
     */
    public function getCustomQuery($field = '*', $condition = '', $value = '')
    {
    	$table	= $this->_table->info('name');
    	$sql	= "SELECT $field FROM $table $condition";
        file_put_contents("D:/test2.txt",var_export($sql,true),FILE_APPEND);
        $sql	= $this->_table->getAdapter()->quoteInto($sql, $value);
    	return $this->_table->getAdapter()->fetchAll($sql);
    }

    /**
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->where("1 =?", 1);
        /*CONDITION_START*/
        
        if(isset($condition["customer_code"]) && $condition["customer_code"] != ""){
            $select->where("customer_code = ?",$condition["customer_code"]);
        }
        if(isset($condition["customer_password"]) && $condition["customer_password"] != ""){
            $select->where("customer_password = ?",$condition["customer_password"]);
        }
        if(isset($condition["customer_firstname"]) && $condition["customer_firstname"] != ""){
            $select->where("customer_firstname = ?",$condition["customer_firstname"]);
        }
        if(isset($condition["customer_lastname"]) && $condition["customer_lastname"] != ""){
            $select->where("customer_lastname = ?",$condition["customer_lastname"]);
        }
        if(isset($condition["customer_email"]) && $condition["customer_email"] != ""){
            $select->where("customer_email = ?",$condition["customer_email"]);
        }
        if(isset($condition["customer_currency"]) && $condition["customer_currency"] != ""){
            $select->where("customer_currency = ?",$condition["customer_currency"]);
        }
        if(isset($condition["customer_activate_code"]) && $condition["customer_activate_code"] != ""){
            $select->where("customer_activate_code = ?",$condition["customer_activate_code"]);
        }
        if(isset($condition["customer_telephone"]) && $condition["customer_telephone"] != ""){
            $select->where("customer_telephone = ?",$condition["customer_telephone"]);
        }
        if(isset($condition["customer_fax"]) && $condition["customer_fax"] != ""){
            $select->where("customer_fax = ?",$condition["customer_fax"]);
        }
        if(isset($condition["customer_status"]) && $condition["customer_status"] != ""){
            $select->where("customer_status = ?",$condition["customer_status"]);
        }
        if(isset($condition["customer_type"]) && $condition["customer_type"] !== ""){
            $select->where("customer_type = ?",$condition["customer_type"]);
        }
        if(isset($condition["customer_company_name"]) && $condition["customer_company_name"] != ""){
            $select->where("customer_company_name = ?",$condition["customer_company_name"]);
        }
        if(isset($condition["customer_company_name_like"]) && $condition["customer_company_name_like"] != ""){
            $select->where("customer_company_name like ?",'%'.$condition["customer_company_name_like"].'%');
        }
        if(isset($condition["customer_logo"]) && $condition["customer_logo"] != ""){
            $select->where("customer_logo = ?",$condition["customer_logo"]);
        }
        if(isset($condition["customer_saler_user_id"]) && $condition["customer_saler_user_id"] != ""){
            $select->where("customer_saler_user_id = ?",$condition["customer_saler_user_id"]);
        }
        if(isset($condition["customer_cser_user_id"]) && $condition["customer_cser_user_id"] != ""){
            $select->where("customer_cser_user_id = ?",$condition["customer_cser_user_id"]);
        }
        if(isset($condition["customer_verify_code"]) && $condition["customer_verify_code"] != ""){
            $select->where("customer_verify_code = ?",$condition["customer_verify_code"]);
        }
        if(isset($condition["customer_signature"]) && $condition["customer_signature"] != ""){
            $select->where("customer_signature = ?",$condition["customer_signature"]);
        }
        if(isset($condition["reg_step"]) && $condition["reg_step"] != ""){
            $select->where("reg_step = ?",$condition["reg_step"]);
        }
        if(isset($condition["password_update_time"]) && $condition["password_update_time"] != ""){
            $select->where("password_update_time = ?",$condition["password_update_time"]);
        }
        if(isset($condition["forget_password_auth_code"]) && $condition["forget_password_auth_code"] != ""){
            $select->where("forget_password_auth_code = ?",$condition["forget_password_auth_code"]);
        }
        if(isset($condition['trade_co'])&&$condition['trade_co']!=""){
            $select->where(" trade_co = ? ",(string)$condition['trade_co']);
        }
		
        /*CONDITION_END*/
        if ('count(*)' == $type) {
            return $this->_table->getAdapter()->fetchOne($select);
        } else {
            if (!empty($orderBy)) {
                $select->order($orderBy);
            }
            if ($pageSize > 0 and $page > 0) {
                $start = ($page - 1) * $pageSize;
                $select->limit($pageSize, $start);
            }
            $sql = $select->__toString();
            return $this->_table->getAdapter()->fetchAll($sql);
        }
    }
}