<?php
class Table_BilServeBillDifference
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_BilServeBillDifference();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_BilServeBillDifference();
    }

    /**
     * @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 = "bsd_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 = "bsd_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 = 'bsd_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);
    }
    
    /**
     * IN 子句查询多个
     * @param sting $value 条件字符串
     * @param string $field 查询条件字段
     * @param string $colums 查询字段
     * @return multitype:
     */
    public function getByInField($value, $field = 'bsd_id', $colums = "*"){
    	$select = $this->_table->getAdapter()->select();
    	$table = $this->_table->info('name');
    	$select->from($table, $colums);
    	$select->where("{$field} in (?)", $value);
    	return $this->_table->getAdapter()->fetchAll($select);
    }
	
	/**
	 * 获取全部数据
	 * @return multitype:
	 */
    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["bsb_id"]) && $condition["bsb_id"] != ""){
            $select->where("bsb_id = ?",$condition["bsb_id"]);
        }
        if(isset($condition["bsbd_id"]) && $condition["bsbd_id"] != ""){
            $select->where("bsbd_id = ?",$condition["bsbd_id"]);
        }
        if(isset($condition["ft_code"]) && $condition["ft_code"] != ""){
            $select->where("ft_code = ?",$condition["ft_code"]);
        }
        if(isset($condition["bsd_withdraw_sign"]) && $condition["bsd_withdraw_sign"] != ""){
            $select->where("bsd_withdraw_sign = ?",$condition["bsd_withdraw_sign"]);
        }
        if(isset($condition["bsd_status"]) && $condition["bsd_status"] != ""){
            $select->where("bsd_status = ?",$condition["bsd_status"]);
        }
        if(isset($condition["bsd_process"]) && $condition["bsd_process"] != ""){
            $select->where("bsd_process = ?",$condition["bsd_process"]);
        }
        if(isset($condition["bsd_diff_type"]) && $condition["bsd_diff_type"] != ""){
            $select->where("bsd_diff_type = ?",$condition["bsd_diff_type"]);
        }
        if(isset($condition["currency_code"]) && $condition["currency_code"] != ""){
            $select->where("currency_code = ?",$condition["currency_code"]);
        }
        if(isset($condition["bsd_value"]) && $condition["bsd_value"] != ""){
            $select->where("bsd_value = ?",$condition["bsd_value"]);
        }
        if(isset($condition["bsd_amount"]) && $condition["bsd_amount"] != ""){
            $select->where("bsd_amount = ?",$condition["bsd_amount"]);
        }
        if(isset($condition["bsd_note"]) && $condition["bsd_note"] != ""){
            $select->where("bsd_note = ?",$condition["bsd_note"]);
        }
        if(isset($condition["bsd_creator_id"]) && $condition["bsd_creator_id"] != ""){
            $select->where("bsd_creator_id = ?",$condition["bsd_creator_id"]);
        }
        if(isset($condition["bsd_process_id"]) && $condition["bsd_process_id"] != ""){
            $select->where("bsd_process_id = ?",$condition["bsd_process_id"]);
        }
        if(isset($condition["bsd_verifier_id"]) && $condition["bsd_verifier_id"] != ""){
            $select->where("bsd_verifier_id = ?",$condition["bsd_verifier_id"]);
        }
        /*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);
        }
    }
}