<?php
/**
 * Created by JetBrains PhpStorm.
 * User: user
 * Date: 16-7-21
 * Time: 上午11:37
 * To change this template use File | Settings | File Templates.
 */
class Table_OrderShipping
{
    protected $_table = null;

    public function __construct(){
        $this->_table= new DbTable_OrderShipping();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance(){
        return new Table_OrderShipping();
    }

    /**
     * @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 = "os_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 = "os_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 = 'os_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["os_id"]) && $condition["os_id"] != ""){
            $select->where("os_id = ?",$condition["os_id"]);
        }
        if(isset($condition["order_code"]) && $condition["order_code"] != ""){
            $select->where("order_code = ?",$condition["order_code"]);
        }
        if(isset($condition["notify_tel"]) && $condition["notify_tel"] != ""){
            $select->where("notify_tel = ?",$condition["notify_tel"]);
        }
        if(isset($condition["notify_contact"]) && $condition["notify_contact"] != ""){
            $select->where("notify_contact = ?",$condition["notify_contact"]);
        }
        if(isset($condition["notify_addr"]) && $condition["notify_addr"] != ""){
            $select->where("notify_addr = ?",$condition["notify_addr"]);
        }
        if(isset($condition["discharge_port"]) && $condition["discharge_port"] != ""){
            $select->where("discharge_port = ?",$condition["discharge_port"]);
        }
        if(isset($condition["destination"]) && $condition["destination"] != ""){
            $select->where("destination = ?",$condition["destination"]);
        }
        if(isset($condition["service_model"]) && $condition["service_model"] != ""){
            $select->where("service_model = ?",$condition["service_model"]);
        }
        if(isset($condition["delivery_terms"]) && $condition["delivery_terms"] != ""){
            $select->where("delivery_terms = ?",$condition["delivery_terms"]);
        }
        if(isset($condition["hs_code"]) && $condition["hs_code"] != ""){
            $select->where("hs_code = ?",$condition["hs_code"]);
        }
        if(isset($condition["is_dgd"]) && $condition["is_dgd"] != ""){
            $select->where("is_dgd = ?",$condition["is_dgd"]);
        }
        if(isset($condition["hs_description"]) && $condition["hs_description"] != ""){
            $select->where("hs_description = ?",$condition["hs_description"]);
        }
        if(isset($condition["pkg_total_amt"]) && $condition["pkg_total_amt"] != ""){
            $select->where("pkg_total_amt = ?",$condition["pkg_total_amt"]);
        }
        if(isset($condition["pkg_total_vol"]) && $condition["pkg_total_vol"] != ""){
            $select->where("pkg_total_vol = ?",$condition["pkg_total_vol"]);
        }
        if(isset($condition["pkg_gw"]) && $condition["pkg_gw"] != ""){
            $select->where("pkg_gw = ?",$condition["pkg_gw"]);
        }
        if(isset($condition["pkg_vw"]) && $condition["pkg_vw"] != ""){
            $select->where("pkg_vw = ?",$condition["pkg_vw"]);
        }
        /*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);
        }
    }

}
