<?php
class Table_GsInventoryNotify {
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_GsInventoryNotify();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_GsInventoryNotify();
    }

    /**
     * @param $row
     * @return mixed
     */
    public function add($row)
    {
        return $this->_table->insert($row);
    }
    
    /**
     * @param $value
     * @param string $field
     * @return mixed
     */
    public function delete($value, $field = "gin_id")
    {
        $where = $this->_table->getAdapter()->quoteInto("{$field}= ?", $value);
        return $this->_table->delete($where);
    }
    /**
     * @param $row
     * @param $value
     * @param string $field
     * @return mixed
     */
    public function update($row, $value, $field = "gin_id")
    {
        $where = $this->_table->getAdapter()->quoteInto("{$field}= ?", $value);
        return $this->_table->update($row, $where);
    }

    /**
     * @param $value
     * @param string $field
     * @param string $colums
     * @return mixed
     */
    public function getByField($value, $field = 'gin_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 getAllByEid($e_id) {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, '*');
        $select->joinLeft('exception_user as eu', 'eu.e_id='.$table.".e_id",array('eu.eu_status','eu_id'));
        $select->joinLeft('exception_type as et', 'et.et_code='.$table.".et_code");
        $select->joinLeft('user as u', 'u.user_id='.$table.".user_id" , array('user_name'));
        $select->where("1 =?", 1);
        $select->where("{$table}.e_id = ?",$e_id);
        $select->where("eu.user_id = ?",  Service_User::getUserId());
        return $this->_table->getAdapter()->fetchRow($select);
    }
    
    /**
     * 
     * @param type array $condition
     * @param type string $type
     * @param type int $pageSize
     * @param type int $page
     * @param type string $order
     */
    public function getByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $order = "") {
        
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->where("1 =?", 1);

        if(isset($condition["gin_id_gt"]) && $condition["gin_id_gt"] != ""){
            $select->where("gin_id > ?",$condition["gin_id_gt"]);
        }
        if(isset($condition["product_id"]) && $condition["product_id"] != ""){
            $select->where("product_id = ?",$condition["product_id"]);
        }
        if(isset($condition['product_sku']) && $condition['product_sku'] != ""){
            $select->where("product_sku = ?",$condition["product_sku"]);
        }
        if(isset($condition['customer_code']) && $condition['customer_code'] != ""){
            $select->where("customer_code = ?",$condition["like_e_refer_no"]);
        }
        if(isset($condition['status']) && $condition['status'] != ''){
            $select->where("status = ?",$condition["status"]);
        }
        if(isset($condition['referecne_no']) && $condition['referecne_no'] != ''){
            $select->where("referecne_no = ?",$condition["referecne_no"]);
        }
        /*CONDITION_END*/
        if ('count(*)' == $type) {
            return $this->_table->getAdapter()->fetchOne($select);
        } else {
            if (!empty($order)) {
                $select->order($order);
            }
            if ($pageSize > 0 and $page > 0) {
                $start = ($page - 1) * $pageSize;
                $select->limit($pageSize, $start);
            }
            $sql = $select->__toString();
            return $this->_table->getAdapter()->fetchAll($sql);
        }
        
    }
    
    /**
     * 
     * @param type array $condition
     * @param type string $type
     * @param type int $pageSize
     * @param type int $page
     * @param type string $order
     */
    public function getAllByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $order = "") {
        
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinLeft('exception_user as eu', 'eu.e_id='.$table.".e_id",array('eu.eu_status'));
        $select->joinLeft('exception_type as et', 'et.et_code='.$table.".et_code");
        $select->where("1 =?", 1);
       
        if(isset($condition["et_code"]) && $condition["et_code"] != ""){
            $select->where("et.et_code = ?",$condition["et_code"]);
        }        
        if(isset($condition["like_et_code"]) && $condition["like_et_code"] != ""){
            $select->where("et.et_code LIKE '%{$condition["like_et_code"]}%' ");
        }        
        if(isset($condition['like_e_title']) && $condition['like_e_title'] != ''){
            $select->where("e_title LIKE '%{$condition["like_e_title"]}%' ");
        }        
        if(isset($condition['user_id']) && $condition['user_id'] != ""){
            $select->where("user_id = ?",$condition["user_id"]);
        }
        if(isset($condition['et_model']) && $condition['et_model'] != ""){
            $select->where("et_model = ?",$condition["et_model"]);
        }
        if(isset($condition['like_e_refer_no']) && $condition['like_e_refer_no'] != ""){
            $select->where("e_refer_no LIKE '%{$condition["like_e_refer_no"]}%'");
        }
        if(isset($condition['eu_user_id']) && $condition['eu_user_id'] != ''){
            $select->where("eu.user_id = ?",$condition["eu_user_id"]);
        }
        if(isset($condition['eu_status']) && $condition['eu_status'] != ""){
            $select->where("eu.eu_status = ?" , $condition['eu_status']);
        }
        if(isset($condition['e_status']) && $condition['e_status'] != ''){
            $select->where("e_status = ?",$condition["e_status"]);
        }
      

        /*CONDITION_END*/
        if ('count(*)' == $type) {            
            return $this->_table->getAdapter()->fetchOne($select);
        } else {
            if (!empty($order)) {
                $select->order($order);
            }
            if ($pageSize > 0 and $page > 0) {
                $start = ($page - 1) * $pageSize;
                $select->limit($pageSize, $start);
            }
            $sql = $select->__toString();            
            return $this->_table->getAdapter()->fetchAll($sql);
        }
        
    }
    
    
}
