<?php
class Table_BgPriEntryDataDetail
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_BgPriEntryDataDetail();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_BgPriEntryDataDetail();
    }

    /**
     * @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 = "bpedd_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 = "bpedd_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 = 'bpedd_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["bpedd_id"]) && $condition["bpedd_id"] != ""){
            $select->where("bpedd_id = ?",$condition["bpedd_id"]);
        }
                if(isset($condition["bped_id"]) && $condition["bped_id"] != ""){
            $select->where("bped_id = ?",$condition["bped_id"]);
        }
                if(isset($condition["bpd_ref_no"]) && $condition["bpd_ref_no"] != ""){
            $select->where("bpd_ref_no = ?",$condition["bpd_ref_no"]);
        }
                if(isset($condition["bpdd_pack_number"]) && $condition["bpdd_pack_number"] != ""){
            $select->where("bpdd_pack_number = ?",$condition["bpdd_pack_number"]);
        }
                if(isset($condition["net_weight"]) && $condition["net_weight"] != ""){
            $select->where("net_weight = ?",$condition["net_weight"]);
        }
                if(isset($condition["gross_weight"]) && $condition["gross_weight"] != ""){
            $select->where("gross_weight = ?",$condition["gross_weight"]);
        }
                if(isset($condition["hz_status"]) && $condition["hz_status"] != ""){
            $select->where("hz_status = ?",$condition["hz_status"]);
        }
                if(isset($condition["bg_status"]) && $condition["bg_status"] != ""){
            $select->where("bg_status = ?",$condition["bg_status"]);
        }
                if(isset($condition["op_id"]) && $condition["op_id"] != ""){
            $select->where("op_id = ?",$condition["op_id"]);
        }
                if(isset($condition["add_time"]) && $condition["add_time"] != ""){
            $select->where("add_time = ?",$condition["add_time"]);
        }
                if(isset($condition["update_time"]) && $condition["update_time"] != ""){
            $select->where("update_time = ?",$condition["update_time"]);
        }
                if(isset($condition["ofild1"]) && $condition["ofild1"] != ""){
            $select->where("ofild1 = ?",$condition["ofild1"]);
        }
                if(isset($condition["ofild2"]) && $condition["ofild2"] != ""){
            $select->where("ofild2 = ?",$condition["ofild2"]);
        }
                if(isset($condition["ofild3"]) && $condition["ofild3"] != ""){
            $select->where("ofild3 = ?",$condition["ofild3"]);
        }
                if(isset($condition["ofild4"]) && $condition["ofild4"] != ""){
            $select->where("ofild4 = ?",$condition["ofild4"]);
        }
                if(isset($condition["ofild5"]) && $condition["ofild5"] != ""){
            $select->where("ofild5 = ?",$condition["ofild5"]);
        }
                /*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);
        }
    }
}