<?php

class Table_BgPriEntryData
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_BgPriEntryData();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_BgPriEntryData();
    }

    /**
     * @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 = "bped_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 = "bped_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 = 'bped_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);
    }

    /**
     * @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["bped_id"]) && $condition["bped_id"] != "") {
            $select->where("bped_id = ?", $condition["bped_id"]);
        }
        if (isset($condition["ref_no"]) && $condition["ref_no"] != "") {
            $select->where("ref_no = ?", $condition["ref_no"]);
        }
        if (isset($condition["declare_port"]) && $condition["declare_port"] != "") {
            $select->where("declare_port = ?", $condition["declare_port"]);
        }
        if (isset($condition["ie_port"]) && $condition["ie_port"] != "") {
            $select->where("ie_port = ?", $condition["ie_port"]);
        }
        if (isset($condition["pre_status"]) && $condition["pre_status"] != "") {
            $select->where("pre_status = ?", $condition["pre_status"]);
        }
        if (isset($condition["pt_code"]) && $condition["pt_code"] != "") {
            $select->where("pt_code = ?", $condition["pt_code"]);
        }
        if (isset($condition["ie_flag"]) && $condition["ie_flag"] != "") {
            $select->where("ie_flag = ?", $condition["ie_flag"]);
        }
        if (isset($condition["mf_status"]) && $condition["mf_status"] != "") {
            $select->where("mf_status = ?", $condition["mf_status"]);
        }
        if (isset($condition["hf_status"]) && $condition["hf_status"] != "") {
            $select->where("hf_status = ?", $condition["hf_status"]);
        }
        if (isset($condition["add_time_start"]) && $condition["add_time_start"] != "") {
            $select->where("add_time >= ?", $condition["add_time_start"]);
        }
        if (isset($condition["add_time_end"]) && $condition["add_time_end"] != "") {
            $select->where("add_time <= ?", $condition["add_time_end"]);
        }
        if (isset($condition["op_id"]) && $condition["op_id"] != "") {
            $select->where("op_id = ?", $condition["op_id"]);
        }
//        echo $select;
        /*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);
        }
    }
}