<?php
class Table_ApiJibaoFormid
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_ApiJibaoFormid();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_ApiJibaoFormid();
    }

    /**
     * @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 = "ajf_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 = "ajf_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 = 'ajf_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 getByWhere($where, $colums = "*") {
    	$select = $this->_table->getAdapter()->select();
    	$table = $this->_table->info('name');
    	$select->from($table, $colums);
    	foreach($where as $field=>$value) {
    		$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);
    }

    /**
     * @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["FORM_ID"]) && $condition["FORM_ID"] != ""){
            $select->where("FORM_ID = ?",$condition["FORM_ID"]);
        }
        if(isset($condition["status"]) && $condition["status"] !== ""){
        	$select->where("status = ?",$condition["status"]);
        }
        
        /*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);
        }
    }
    /**
     * @author william-fan
     * @todo 得到集报数据
     */
    public function getJibaoByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
    	$select = $this->_table->getAdapter()->select();
    	$table = $this->_table->info('name');
		if($type=='*'){
			$select->from($table, array('FORM_ID','jibao_path_hz','jibao_path_zk'));
		}else{
			$select->from($table, $type);
		}
    	$select->where("1 =?", 1);
    	/*CONDITION_START*/
    
    	$select->joinInner('api_jibao_header','api_jibao_header.FORM_ID='.$table.".FORM_ID");
    	
    	$select->where("1 =?", 1);
    	
    	if(isset($condition["FORM_ID"]) && $condition["FORM_ID"] != ""){
    		$select->where($table.".FORM_ID = ?",$condition["FORM_ID"]);
    	}
    	if(isset($condition["status"]) && $condition["status"] !== ""){
    		$select->where("api_jibao_formid.status = ?",$condition["status"]);
    	}
    	if(isset($condition["OWNER_CODE"]) && $condition["OWNER_CODE"] != ""){
    		$select->where("api_jibao_header.OWNER_CODE = ?",$condition["OWNER_CODE"]);
    	}
        if(isset($condition["OWNER_NAME_LIKE"]) && $condition["OWNER_NAME_LIKE"] != ""){
            $select->where("api_jibao_header.OWNER_NAME like ?","%".$condition["OWNER_NAME_LIKE"]."%");
        }
        if(isset($condition["CreateTime_Start"]) && $condition["CreateTime_Start"] != ""){
            $select->where("api_jibao_header.CreateTime >= ?",$condition["CreateTime_Start"]." 00:00:00");
        }
        if(isset($condition["CreateTime_end"]) && $condition["CreateTime_end"] != ""){
            $select->where("api_jibao_header.CreateTime <= ?",$condition["CreateTime_end"]." 23:59:59");
        }
    	//echo $select;exit;
    	/*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();
    		//echo $sql;
    		return $this->_table->getAdapter()->fetchAll($sql);
    	}
    }
    
}