<?php
class Table_DesMessageGoodslimitvin
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_DesMessageGoodslimitvin();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_DesMessageGoodslimitvin();
    }

    /**
     * @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 = "dmg_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 = "dmg_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 = 'dmg_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["dmg_id"]) && $condition["dmg_id"] != ""){
            $select->where("dmg_id = ?",$condition["dmg_id"]);
        }
                if(isset($condition["dmh_id"]) && $condition["dmh_id"] != ""){
            $select->where("dmh_id = ?",$condition["dmh_id"]);
        }
                if(isset($condition["licence_no"]) && $condition["licence_no"] != ""){
            $select->where("licence_no = ?",$condition["licence_no"]);
        }
                if(isset($condition["lic_type_code"]) && $condition["lic_type_code"] != ""){
            $select->where("lic_type_code = ?",$condition["lic_type_code"]);
        }
                if(isset($condition["vin_no"]) && $condition["vin_no"] != ""){
            $select->where("vin_no = ?",$condition["vin_no"]);
        }
                if(isset($condition["bill_lad_date"]) && $condition["bill_lad_date"] != ""){
            $select->where("bill_lad_date = ?",$condition["bill_lad_date"]);
        }
                if(isset($condition["quality_qgp"]) && $condition["quality_qgp"] != ""){
            $select->where("quality_qgp = ?",$condition["quality_qgp"]);
        }
                if(isset($condition["motor_no"]) && $condition["motor_no"] != ""){
            $select->where("motor_no = ?",$condition["motor_no"]);
        }
                if(isset($condition["vin_code"]) && $condition["vin_code"] != ""){
            $select->where("vin_code = ?",$condition["vin_code"]);
        }
                if(isset($condition["chassis_no"]) && $condition["chassis_no"] != ""){
            $select->where("chassis_no = ?",$condition["chassis_no"]);
        }
                if(isset($condition["invoice_num"]) && $condition["invoice_num"] != ""){
            $select->where("invoice_num = ?",$condition["invoice_num"]);
        }
                if(isset($condition["prod_cnnm"]) && $condition["prod_cnnm"] != ""){
            $select->where("prod_cnnm = ?",$condition["prod_cnnm"]);
        }
                if(isset($condition["prod_ennm"]) && $condition["prod_ennm"] != ""){
            $select->where("prod_ennm = ?",$condition["prod_ennm"]);
        }
                if(isset($condition["model_en"]) && $condition["model_en"] != ""){
            $select->where("model_en = ?",$condition["model_en"]);
        }
                if(isset($condition["price_per_unit"]) && $condition["price_per_unit"] != ""){
            $select->where("price_per_unit = ?",$condition["price_per_unit"]);
        }
                if(isset($condition["invoice_no"]) && $condition["invoice_no"] != ""){
            $select->where("invoice_no = ?",$condition["invoice_no"]);
        }
                /*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);
        }
    }
}