<?php
/**
 * Created by JetBrains PhpStorm.
 * User: user
 * Date: 15-1-16
 * Time: 下午2:53
 * To change this template use File | Settings | File Templates.
 */
class Table_ShipOrderLog
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_ShipOrderLog();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_ShipOrderLog();
    }

    /**
     * @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 = "sol_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 = "sol_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 = 'sol_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["sol_id"]) && $condition["sol_id"] != ""){
            $select->where("sol_id = ?",$condition["sol_id"]);
        }
        if(isset($condition["so_id"]) && $condition["so_id"] != ""){
            $select->where("so_id = ?",$condition["so_id"]);
        }
        if(isset($condition["so_code"]) && $condition["so_code"] != ""){
            $select->where("so_code = ?",$condition["so_code"]);
        }
        if(isset($condition["so_status_start"]) && $condition["so_status_start"] != ""){
            $select->where("so_status_start = ?",$condition["so_status_start"]);
        }
        if(isset($condition["so_status_end"]) && $condition["so_status_end"] != ""){
            $select->where("so_status_end = ?",$condition["so_status_end"]);
        }
        if(isset($condition["user_id"]) && $condition["user_id"] != ""){
            $select->where("user_id = ?",$condition["user_id"]);
        }
        if(isset($condition["note"]) && $condition["note"] != ""){
            $select->where("note = ?",$condition["note"]);
        }
        if(isset($condition["add_time"]) && $condition["add_time"] != ""){
            $select->where("add_time = ?",$condition["add_time"]);
        }
        if(isset($condition["visit_ip"]) && $condition["visit_ip"] != ""){
            $select->where("visit_ip = ?",$condition["visit_ip"]);
        }
        /*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);
        }
    }
}
