<?php
class Table_CustomerBalance
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_CustomerBalance();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_CustomerBalance();
    }

    /**
     * @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 = "cb_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 = "cb_id")
    {
        $where = $this->_table->getAdapter()->quoteInto("{$field}= ?", $value);
        return $this->_table->delete($where);
    }

    /**
     * @param $value
     * @param string $field
     * @param string $colums
     * @return mixed
     */
    public function getByField($value, $field = 'cb_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 getForUpdate($cb_id) {
    	$sql = 'select * from customer_balance where cb_id='.$cb_id.' for update;';
    	return $this->_table->getAdapter()->fetchRow($sql);
    }

    public function getAll()
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, "*");
        return $this->_table->getAdapter()->fetchAll($select);
    }

    /**
     * @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_id"]) && $condition["customer_id"] != ""){
            $select->where("customer_id = ?",$condition["customer_id"]);
        }
        if(isset($condition["cb_value"]) && $condition["cb_value"] != ""){
            $select->where("cb_value = ?",$condition["cb_value"]);
        }
        if(isset($condition["cb_hold_value"]) && $condition["cb_hold_value"] != ""){
            $select->where("cb_hold_value = ?",$condition["cb_hold_value"]);
        }
        if(isset($condition["cb_update_time"]) && $condition["cb_update_time"] != ""){
            $select->where("cb_update_time = ?",$condition["cb_update_time"]);
        }
        if(isset($condition['availableLt']) && $condition['availableLt'] != ''){
            $select->where("(cb_value - cb_hold_value) < ?",$condition["availableLt"]);
        }
        /*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);
        }
    }
    
    
    public function getLeftJoinByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "") {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinLeft('customer as c', 'c.customer_id='.$table.'.customer_id',array('c.cash_type','c.customer_currency'));
        $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_id"]) && $condition["customer_id"] != ""){
            $select->where("customer_id = ?",$condition["customer_id"]);
        }
        if(isset($condition["cb_value"]) && $condition["cb_value"] != ""){
            $select->where("cb_value = ?",$condition["cb_value"]);
        }
        if(isset($condition["cb_hold_value"]) && $condition["cb_hold_value"] != ""){
            $select->where("cb_hold_value = ?",$condition["cb_hold_value"]);
        }
        if(isset($condition["cb_update_time"]) && $condition["cb_update_time"] != ""){
            $select->where("cb_update_time = ?",$condition["cb_update_time"]);
        }
        if(isset($condition['availableLt']) && $condition['availableLt'] != ''){
            $select->where("(cb_value - cb_hold_value) < ?",$condition["availableLt"]);
        }        
        if(isset($condition['cash_type']) && $condition['cash_type'] != ''){
            $select->where("c.cash_type = ?",$condition["cash_type"]);
        }
    
        /*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);
        }
    }
}