<?php
/**
 * Created by JetBrains PhpStorm.
 * User: user
 * Date: 15-12-16
 * Time: 下午8:07
 * To change this template use File | Settings | File Templates.
 */
class Table_ShipBatchXml
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_ShipBatchXml();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_ShipBatchXml();
    }

    /**
     * @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 = "sbx_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 = "sbx_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 = 'sbx_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["sbx_id"]) && $condition["sbx_id"] != ""){
            $select->where("sbx_id = ?",$condition["sbx_id"]);
        }
        if(isset($condition["sb_code"]) && $condition["sb_code"] != ""){
            $select->where("sb_code = ?",$condition["sb_code"]);
        }
        if(isset($condition["sub_sb_code"]) && $condition["sub_sb_code"] != ""){
            $select->where("sub_sb_code = ?",$condition["sub_sb_code"]);
        }
        if(isset($condition["gross_wt"]) && $condition["gross_wt"] != ""){
            $select->where("gross_wt = ?",$condition["gross_wt"]);
        }
        if(isset($condition["net_wt"]) && $condition["net_wt"] != ""){
            $select->where("net_wt = ?",$condition["net_wt"]);
        }
        if(isset($condition["pack_no"]) && $condition["pack_no"] != ""){
            $select->where("pack_no = ?",$condition["pack_no"]);
        }
        if(isset($condition["ship_batch_xml_status"]) && $condition["ship_batch_xml_status"] != ""){
            $select->where("ship_batch_xml_status = ?",$condition["ship_batch_xml_status"]);
        }
        if(isset($condition["sbx_add_time"]) && $condition["sbx_add_time"] != ""){
            $select->where("sbx_add_time = ?",$condition["sbx_add_time"]);
        }
        if(isset($condition["sbx_update_time"]) && $condition["sbx_update_time"] != ""){
            $select->where("sbx_update_time = ?",$condition["sbx_update_time"]);
        }
        if(isset($condition["total_price"]) && $condition["total_price"] != ""){
            $select->where("total_price = ?",$condition["total_price"]);
        }
        if(isset($condition['ship_batch_xml_status_arr'])&&$condition['ship_batch_xml_status_arr']!=""){
            $select->where("ship_batch_xml_status in (?)",$condition["ship_batch_xml_status_arr"]);
        }
        /*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);
        }
    }
}
