<?php
/**
 * Created by JetBrains PhpStorm.
 * User: user
 * Date: 15-6-4
 * Time: 下午5:09
 * To change this template use File | Settings | File Templates.
 */
class Table_SwapOrderDetail
{
    protected $_table = null;

    public function __construct(){
        $this->_table = new DbTable_SwapOrderDetail();
    }

    public function getAdapter(){
        return $this->_table->getAdapter();
    }

    public function getInstance(){
        return new Table_SwapOrderDetail();
    }

    public function add($row){
        return $this->_table->insert($row);
    }

    /**
     * @param $row
     * @param $value
     * @param string $field
     * @return mixed
     */
    public function update($row, $value, $field = "swod_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 = "swod_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 = 'swod_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["swod_id"]) && $condition["swod_id"] != ""){
            $select->where("swod_id = ?",$condition["swod_id"]);
        }
        if(isset($condition["swo_id"]) && $condition["swo_id"] != ""){
            $select->where("swo_id = ?",$condition["swo_id"]);
        }
        if(isset($condition["swo_code"]) && $condition["swo_code"] != ""){
            $select->where("swo_code = ?",$condition["swo_code"]);
        }
        if(isset($condition["from_customer_code"]) && $condition["from_customer_code"] != ""){
            $select->where("from_customer_code = ?",$condition["from_customer_code"]);
        }
        if(isset($condition["quantity"]) && $condition["quantity"] != ""){
            $select->where("quantity = ?",$condition["quantity"]);
        }
        if(isset($condition["from_sku"]) && $condition["from_sku"] != ""){
            $select->where("from_sku = ?",$condition["from_sku"]);
        }
        if(isset($condition["to_sku"]) && $condition["to_sku"] != ""){
            $select->where("to_sku = ?",$condition["to_sku"]);
        }
        if(isset($condition["swod_status"]) && $condition["swod_status"] != ""){
            $select->where("swod_status = ?",$condition["swod_status"]);
        }
        if(isset($condition['product_id'])&&$condition['product_id']!=""){
            $select->where("product_id = ?",$condition['product_id']);
        }
        if(isset($condition['to_product_id'])&&$condition['to_product_id']!=""){
            $select->where("to_product_id = ?",$condition['to_product_id']);
        }
        /*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);
        }
    }

    public function getLeftJoinProductByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinLeft('product', 'product.product_id='.$table.'.product_id','product_title');
        $select->where("1 =?", 1);
        /*CONDITION_START*/
        if(isset($condition["swod_id"]) && $condition["swod_id"] != ""){
            $select->where("swod_id = ?",$condition["swod_id"]);
        }
        if(isset($condition["swo_id"]) && $condition["swo_id"] != ""){
            $select->where("swo_id = ?",$condition["swo_id"]);
        }
        if(isset($condition["swo_code"]) && $condition["swo_code"] != ""){
            $select->where("swo_code = ?",$condition["swo_code"]);
        }
        if(isset($condition["from_customer_code"]) && $condition["from_customer_code"] != ""){
            $select->where("from_customer_code = ?",$condition["from_customer_code"]);
        }
        if(isset($condition["quantity"]) && $condition["quantity"] != ""){
            $select->where("quantity = ?",$condition["quantity"]);
        }
        if(isset($condition["from_sku"]) && $condition["from_sku"] != ""){
            $select->where("from_sku = ?",$condition["from_sku"]);
        }
        if(isset($condition["to_sku"]) && $condition["to_sku"] != ""){
            $select->where("to_sku = ?",$condition["to_sku"]);
        }
        if(isset($condition["swod_status"]) && $condition["swod_status"] != ""){
            $select->where("swod_status = ?",$condition["swod_status"]);
        }
        if(isset($condition['product_id'])&&$condition['product_id']!=""){
            $select->where("product_id = ?",$condition['product_id']);
        }

        /*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);
        }
    }

    public function getJoinProductAndSwapOrderByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinLeft('product', 'product.product_id='.$table.'.to_product_id',array('product_sku','product_barcode',"customer_code","goods_id"));
        $select->joinLeft('swap_order', 'swap_order.swo_id='.$table.'.swo_id','');
        $select->where("1 =?", 1);
        /*CONDITION_START*/
        if(isset($condition["swod_id"]) && $condition["swod_id"] != ""){
            $select->where("swod_id = ?",$condition["swod_id"]);
        }
        if(isset($condition["swo_id"]) && $condition["swo_id"] != ""){
            $select->where("swo_id = ?",$condition["swo_id"]);
        }
        if(isset($condition["swo_code"]) && $condition["swo_code"] != ""){
            $select->where("swo_code = ?",$condition["swo_code"]);
        }
        if(isset($condition["from_customer_code"]) && $condition["from_customer_code"] != ""){
            $select->where("from_customer_code = ?",$condition["from_customer_code"]);
        }
        if(isset($condition["quantity"]) && $condition["quantity"] != ""){
            $select->where("quantity = ?",$condition["quantity"]);
        }
        if(isset($condition["from_sku"]) && $condition["from_sku"] != ""){
            $select->where("from_sku = ?",$condition["from_sku"]);
        }
        if(isset($condition["to_sku"]) && $condition["to_sku"] != ""){
            $select->where("to_sku = ?",$condition["to_sku"]);
        }
        if(isset($condition["swod_status"]) && $condition["swod_status"] != ""){
            $select->where("swod_status = ?",$condition["swod_status"]);
        }
        if(isset($condition['product_id'])&&$condition['product_id']!=""){
            $select->where("product_id = ?",$condition['product_id']);
        }
        if(isset($condition['swo_status'])&&$condition['swo_status']!=""){
            $select->where("swap_order.swo_status = ?",$condition['swo_status']);
        }
        if(isset($condition['product_sku'])&&$condition['product_sku']!=""){
            $select->where("product.product_sku = ?",$condition['product_sku']);
        }
        if(isset($condition['product_barcode'])&&$condition['product_barcode']!=""){
            $select->where("product.product_barcode = ?",$condition['product_barcode']);
        }
        if(isset($condition['customer_code'])&&$condition['customer_code']!=""){
            $select->where("product.customer_code = ?",$condition['customer_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);
        }
    }

}
