<?php
class Table_ApiJibaoGatherDetailsBefore
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_ApiJibaoGatherDetailsBefore();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_ApiJibaoGatherDetailsBefore();
    }

    /**
     * @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 = "ajgdb_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 = "ajgdb_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 = 'ajgdb_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["ajgdb_id"]) && $condition["ajgdb_id"] != ""){
            $select->where("ajgdb_id = ?",$condition["ajgdb_id"]);
        }
        if(isset($condition["ajh_id"]) && $condition["ajh_id"] != ""){
            $select->where("ajh_id = ?",$condition["ajh_id"]);
        }
        if(isset($condition["jibao_id"]) && $condition["jibao_id"] != ""){
            $select->where("jibao_id = ?",$condition["jibao_id"]);
        }
        if(isset($condition["CODE_TS"]) && $condition["CODE_TS"] != ""){
            $select->where("CODE_TS = ?",$condition["CODE_TS"]);
        }
        if(isset($condition["CURR"]) && $condition["CURR"] != ""){
            $select->where("CURR = ?",$condition["CURR"]);
        }
        if(isset($condition["DECL_PRICE"]) && $condition["DECL_PRICE"] != ""){
            $select->where("DECL_PRICE = ?",$condition["DECL_PRICE"]);
        }
        if(isset($condition["DECL_TOTAL"]) && $condition["DECL_TOTAL"] != ""){
            $select->where("DECL_TOTAL = ?",$condition["DECL_TOTAL"]);
        }
        if(isset($condition["FORM_ID"]) && $condition["FORM_ID"] != ""){
            $select->where("FORM_ID = ?",$condition["FORM_ID"]);
        }
        if(isset($condition["FORM_ID_XML"]) && $condition["FORM_ID_XML"] != ""){
            $select->where("FORM_ID_XML = ?",$condition["FORM_ID_XML"]);
        }
        if(isset($condition["GOODS_ID"]) && $condition["GOODS_ID"] != ""){
            $select->where("GOODS_ID = ?",$condition["GOODS_ID"]);
        }
        if(isset($condition["G_MODEL"]) && $condition["G_MODEL"] != ""){
            $select->where("G_MODEL = ?",$condition["G_MODEL"]);
        }
        if(isset($condition["G_NAME_CN"]) && $condition["G_NAME_CN"] != ""){
            $select->where("G_NAME_CN = ?",$condition["G_NAME_CN"]);
        }
        if(isset($condition["G_NO"]) && $condition["G_NO"] != ""){
            $select->where("G_NO = ?",$condition["G_NO"]);
        }
        if(isset($condition["G_QTY"]) && $condition["G_QTY"] != ""){
            $select->where("G_QTY = ?",$condition["G_QTY"]);
        }
        if(isset($condition["G_UNIT"]) && $condition["G_UNIT"] != ""){
            $select->where("G_UNIT = ?",$condition["G_UNIT"]);
        }
        if(isset($condition["ORIGIN_COUNTRY"]) && $condition["ORIGIN_COUNTRY"] != ""){
            $select->where("ORIGIN_COUNTRY = ?",$condition["ORIGIN_COUNTRY"]);
        }
        if(isset($condition["QTY_1"]) && $condition["QTY_1"] != ""){
            $select->where("QTY_1 = ?",$condition["QTY_1"]);
        }
        if(isset($condition["QTY_2"]) && $condition["QTY_2"] != ""){
            $select->where("QTY_2 = ?",$condition["QTY_2"]);
        }
        if(isset($condition["UNIT_1"]) && $condition["UNIT_1"] != ""){
            $select->where("UNIT_1 = ?",$condition["UNIT_1"]);
        }
        if(isset($condition["UNIT_2"]) && $condition["UNIT_2"] != ""){
            $select->where("UNIT_2 = ?",$condition["UNIT_2"]);
        }
        /*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);
        }
    }
}