<?php
class Table_ServiceProvider
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_ServiceProvider();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_ServiceProvider();
    }

    /**
     * @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 = "sp_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 = "sp_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 = 'sp_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["sp_id"]) && $condition["sp_id"] != ""){
        	$select->where("sp_id = ?",$condition["sp_id"]);
        }
        if(isset($condition["sp_status"]) && $condition["sp_status"] != ""){
        	$select->where("sp_status = ?",$condition["sp_status"]);
        }
        if(isset($condition["sp_settlement_type"]) && $condition["sp_settlement_type"] != ""){
        	$select->where("sp_settlement_type = ?",$condition["sp_settlement_type"]);
        }
        if(isset($condition["sp_code"]) && $condition["sp_code"] != ""){
            $select->where("sp_code = ?",$condition["sp_code"]);
        }
        if(isset($condition["sp_name"]) && $condition["sp_name"] != ""){
            $select->where("sp_name = ?",$condition["sp_name"]);
        }
        
        if(isset($condition["sp_code_like"]) && $condition["sp_code_like"] != ""){
            $select->where("sp_code like ?","%" . $condition["sp_code_like"] . "%");
        }
        if(isset($condition["sp_name_like"]) && $condition["sp_name_like"] != ""){
            $select->where("sp_name like ?","%" . $condition["sp_name_like"] . "%");
        }
        
        /*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 array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getJoinLeftByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinLeft('user as u1', "u1.user_id=service_provider.sp_person_user",array('u1.user_name as developName'));
        $select->where("1 =?", 1);
        /*CONDITION_START*/
        
        if(isset($condition["sp_id"]) && $condition["sp_id"] != ""){
        	$select->where("sp_id = ?",$condition["sp_id"]);
        }
        if(isset($condition["sp_status"]) && $condition["sp_status"] != ""){
        	$select->where("sp_status = ?",$condition["sp_status"]);
        }
        if(isset($condition["sp_settlement_type"]) && $condition["sp_settlement_type"] != ""){
        	$select->where("sp_settlement_type = ?",$condition["sp_settlement_type"]);
        }
        if(isset($condition["sp_code"]) && $condition["sp_code"] != ""){
            $select->where("sp_code = ?",$condition["sp_code"]);
        }
        if(isset($condition["sp_name"]) && $condition["sp_name"] != ""){
            $select->where("sp_name = ?",$condition["sp_name"]);
        }
        
        if(isset($condition["sp_code_like"]) && $condition["sp_code_like"] != ""){
            $select->where("sp_code like ?","%" . $condition["sp_code_like"] . "%");
        }
        if(isset($condition["sp_name_like"]) && $condition["sp_name_like"] != ""){
            $select->where("sp_name like ?","%" . $condition["sp_name_like"] . "%");
        }
        
        /*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 array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getJoinInnerByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinInner('service_relational', 'service_relational.sp_id=service_provider.sp_id',array('warehouse_id','sm_code'));
        $select->where("1 =?", 1);
        /*CONDITION_START*/

        if(isset($condition["warehouse_id"]) && $condition["warehouse_id"] !== ""){
            $select->where("service_relational.warehouse_id = ?",$condition["warehouse_id"]);
        }
        if(isset($condition["sp_code"]) && $condition["sp_code"] != ""){
            $select->where("sp_code = ?",$condition["sp_code"]);
        }
        /*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);
        }
    }

}