<?php
class Table_InvtSb
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_InvtSb();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_InvtSb();
    }

    /**
     * @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 = "is_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 = "is_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 = 'is_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 getByFieldLock($value, $field = 'is_id', $colums = "*"){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $colums);
        $select->where("{$field} = ?", $value);
        $select->forUpdate();
        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["is_id"]) && $condition["is_id"] != ""){
            $select->where("is_id = ?",$condition["is_id"]);
        }
        if(isset($condition["PassportTypecd"]) && $condition["PassportTypecd"] != ""){
            $select->where("PassportTypecd = ?",$condition["PassportTypecd"]);
        }
        if(isset($condition["MasterCuscd"]) && $condition["MasterCuscd"] != ""){
            $select->where("MasterCuscd = ?",$condition["MasterCuscd"]);
        }
        if(isset($condition["EtpsPreentNo"]) && $condition["EtpsPreentNo"] != ""){
            $select->where("EtpsPreentNo = ?",$condition["EtpsPreentNo"]);
        }
        if(isset($condition["DclTypecd"]) && $condition["DclTypecd"] != ""){
            $select->where("DclTypecd = ?",$condition["DclTypecd"]);
        }
        if(isset($condition["IoTypecd"]) && $condition["IoTypecd"] != ""){
            $select->where("IoTypecd = ?",$condition["IoTypecd"]);
        }
        if(isset($condition["BindTypecd"]) && $condition["BindTypecd"] != ""){
            $select->where("BindTypecd = ?",$condition["BindTypecd"]);
        }
        if(isset($condition["RltTbTypecd"]) && $condition["RltTbTypecd"] != ""){
            $select->where("RltTbTypecd = ?",$condition["RltTbTypecd"]);
        }
        if(isset($condition["AreainEtpsno"]) && $condition["AreainEtpsno"] != ""){
            $select->where("AreainEtpsno = ?",$condition["AreainEtpsno"]);
        }
        if(isset($condition["AreainEtpsNm"]) && $condition["AreainEtpsNm"] != ""){
            $select->where("AreainEtpsNm = ?",$condition["AreainEtpsNm"]);
        }
        if(isset($condition["VehicleNo"]) && $condition["VehicleNo"] != ""){
            $select->where("VehicleNo = ?",$condition["VehicleNo"]);
        }
        if(isset($condition["time_start"]) && $condition["time_start"] != ""){
            $select->where("DclTime >= ?",$condition["time_start"]);
        }
        if(isset($condition["time_end"]) && $condition["time_end"] != ""){
            $select->where("DclTime >= ?",$condition["time_end"]);
        }
        if(isset($condition["is_status"]) && $condition["is_status"] != ""){
            $select->where("is_status in (?)",$condition["is_status"]);
        }
        if(isset($condition["batch_code"]) && $condition["batch_code"] != ""){
            $select->where("batch_code = ?",$condition["batch_code"]);
        }
        if(isset($condition["upload_file_id_no_empty"]) && $condition["upload_file_id_no_empty"] != ""){
            $select->where("upload_file_id !=''");
        }
        /*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);
        }
    }
}