<?php

/**
 * luffy丶大叔 
 * <><><><><><><><><><><><><>有妹子加微信不哦！~<><><><><><><><><><><><><><><><>
 * $Author: luffy丶大叔 
 * $Z 2015-4-8 9:51:48
 * $email luffy00789@126.com
 * $encoding UTF-8
 * $ Copyright (c) 2014-2015, http://www.globex.cn. All Rights Reserved.
 */
class Table_ExceptionType {
    ######name is not a number, it is freemarker.template.SimpleScalar#######
    protected $_table = null;

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

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

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

    /**
     * @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 = "et_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 = 'et_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 getjoinUserPositionByField($value, $field = 'et_id', $colums = "*") {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $colums);
        $select->joinLeft('exception_type_user_position as exception_type_user_position', 'exception_type_user_position.et_id='.$table.'.et_id',array('exception_type_user_position.up_id','exception_type_user_position.user_id'));
        $select->where("{$table}.{$field} = ?", $value);
        $sql = $select->__toString();  
        return $this->_table->getAdapter()->fetchRow($sql);
    }
    /**
     * 
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param 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["et_code"]) && $condition["et_code"] != ""){
            $select->where("et_code = ?",$condition["et_code"]);
        }        
        if(isset($condition["like_et_code"]) && $condition["like_et_code"] != ""){
            $select->where("et_code LIKE '%?%' " , $condition["like_et_code"]);
        }        
        if(isset($condition['like_et_name']) && $condition['like_et_name'] != ''){
            $select->where("et_name LIKE '%?%' ", $condition["like_et_name"]);
        }        
        if(isset($condition['et_model']) && $condition['et_model'] != ""){
            $select->where("et_model = ?",$condition["et_model"]);
        }
        if(isset($condition['et_status']) && $condition['et_status'] != ""){
            $select->where("et_status LIKE '%?%'",$condition["et_status"]);
        }
        if(isset($condition['et_auto'])&& $condition['et_auto'] !== ""){
            $select->where("et_auto = ?",$condition["et_auto"]);
        }
        if(isset($condition['at_that_time']) && $condition['at_that_time'] === true){
            $select->where("execute_time+last_e_time < ?",  time());
        }

        /*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);
        }
    }
    
    /**
     * 
     * @param type $colums
     * @return type
     */
    public function getDistinctByCondition($colums) {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,'*');
        $select->distinct($colums);
        $sql = $select->__toString();  
        return $this->_table->getAdapter()->fetchAll($sql);
    }
    /**
     * 
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $order
     */
    public function getJoinUserPositionByCondition($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_type_user_position as exception_type_user_position', 'exception_type_user_position.et_id='.$table.'.et_id',array('exception_type_user_position.up_id','exception_type_user_position.user_id'));
        $select->where("1 =?", 1);
       
        if(isset($condition["et_code"]) && $condition["et_code"] != ""){
            $select->where("et_code = ?",$condition["et_code"]);
        }        
        if(isset($condition["like_et_code"]) && $condition["like_et_code"] != ""){
            $select->where("et_code LIKE '%?{$condition["like_et_code"]}%' ");
        }   
        if(isset($condition['up_id']) &&  $condition["up_id"] != ""){
            $select->where("exception_type_user_position.up_id = ?",$condition["up_id"]);
        }
        if(isset($condition['like_et_name']) && $condition['like_et_name'] != ''){
            $select->where("et_name LIKE '%{$condition["like_et_name"]}%' ");
        }        
        if(isset($condition['et_model'])&& $condition['et_model'] != ''){
            $select->where("et_model = ? ", $condition["et_model"]);
        }
        if(isset($condition['user_id']) && $condition['user_id'] != ""){
            $select->where("user_id = ?",$condition["user_id"]);
        }    
        if(isset($condition['et_status']) && $condition['et_status'] != ''){
            $select->where("et_status = ?",$condition["et_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);
        }
        
    }
}
