<?php
/**
 * Created by JetBrains PhpStorm.
 * User: user
 * Date: 15-6-4
 * Time: 下午5:06
 * To change this template use File | Settings | File Templates.
 */
class Table_SwapOrder
{
    protected $_table = null;

    public function __construct(){
        $this->_table = new DbTable_SwapOrder();
    }

    public function getAdapter(){
        return $this->_table->getAdapter();
    }

    public static function getInstance(){
        return new Table_SwapOrder();
    }

    public function add($row){
        return $this->_table->insert($row);
    }

    /**
     * @param $row
     * @param $value
     * @param string $field
     * @return mixed
     */
    public function update($row, $value, $field = "swo_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 = "swo_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 = 'swo_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["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["customer_id"]) && $condition["customer_id"] != ""){
            $select->where("customer_id = ?",$condition["customer_id"]);
        }
        if(isset($condition["customer_code"]) && $condition["customer_code"] != ""){
            $select->where("customer_code = ?",$condition["customer_code"]);
        }
        if(isset($condition["from_customer_code"]) && $condition["from_customer_code"] != ""){
            $select->where("from_customer_code = ?",$condition["from_customer_code"]);
        }
        if(isset($condition["swo_status"]) && $condition["swo_status"] != ""){
            $select->where("swo_status = ?",$condition["swo_status"]);
        }
        if(isset($condition["remark"]) && $condition["remark"] != ""){
            $select->where("remark = ?",$condition["remark"]);
        }
        if(isset($condition['has_send'])&&$condition['has_send']!=""){
            $select->where(" has_send = ?",$condition['has_send']);
        }
        if(isset($condition['serial_code'])&&$condition['serial_code']!=""){
            $select->where(" serial_code = ? ",$condition['serial_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);
        }
    }

    public function getLeftJoinSwapOrderDetailByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->joinLeft('swap_order_detail', 'swap_order_detail.swo_id='.$table.'.swo_id','from_customer_code');
        $select->from($table, $type);
        $select->where("1 =?", 1);
        /*CONDITION_START*/

        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($table.".swo_code = ?",$condition["swo_code"]);
        }
        if(isset($condition["customer_id"]) && $condition["customer_id"] != ""){
            $select->where("customer_id = ?",$condition["customer_id"]);
        }
        if(isset($condition["customer_code"]) && $condition["customer_code"] != ""){
            $select->where("customer_code = ?",$condition["customer_code"]);
        }
        if(isset($condition["from_customer_code"]) && $condition["from_customer_code"] != ""){
            $select->where("from_customer_code = ?",$condition["from_customer_code"]);
        }
        if(isset($condition["swo_status"]) && $condition["swo_status"] != ""){
            $select->where("swo_status = ?",$condition["swo_status"]);
        }
        if(isset($condition["remark"]) && $condition["remark"] != ""){
            $select->where("remark = ?",$condition["remark"]);
        }
        if(isset($condition['has_send'])&&$condition['has_send']!=""){
            $select->where(" has_send = ?",$condition['has_send']);
        }
        if(isset($condition['serial_code'])&&$condition['serial_code']!=""){
            $select->where(" serial_code = ? ",$condition['serial_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);
        }
    }
    public function getJoinDetailByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
            if ('count(*)' == $type) {
                //统计总数
                $select->from($table, 'count(DISTINCT(swap_order.swo_code))');
            } else {
                $select->from($table, $type);
            }
        //$select->from($table, $type);
        $select->joinLeft('swap_order_detail', 'swap_order_detail.swo_id='.$table.'.swo_id',"*");
        $select->where("1 =?", 1);
    
        /*CONDITION_START*/
        if(isset($condition["swo_code"]) && $condition["swo_code"] != ""){
            $select->where($table.".swo_code = ?",$condition["swo_code"]);
        }
        if(isset($condition["customer_code"]) && $condition["customer_code"] != ""){
            $select->where("customer_code = ?",$condition["customer_code"]);
        }
        if(isset($condition["swo_status"]) && $condition["swo_status"] != ""){
            $select->where("swo_status = ?",$condition["swo_status"]);
        }
        if(isset($condition["from_customer_code"]) && $condition["from_customer_code"] != ""){
            $select->where("swap_order_detail.from_customer_code = ?",$condition["from_customer_code"]);
        }
        if(isset($condition["to_product_barcode"]) && $condition["to_product_barcode"] != ""){
            $select->where("swap_order_detail.to_product_barcode = ?",$condition["to_product_barcode"]);
        }
        if(isset($condition["product_id"]) && $condition["product_id"] != ""){
            $select->where("swap_order_detail.product_id = ?",$condition["product_id"]);
        }
        if(isset($condition['has_send'])&&$condition['has_send']!=""){
            $select->where(" has_send = ?",$condition['has_send']);
        }
        if(isset($condition['serial_code'])&&$condition['serial_code']!=""){
            $select->where(" serial_code = ? ",$condition['serial_code']);
        }
          if(isset($condition['putway_time_start'])&&$condition['putway_time_start']!=""){
            $select->where("putway_time >= ? ",$condition['putway_time_start']);
        }
         if(isset($condition['putway_time_end'])&&$condition['putway_time_end']!=""){
            $select->where(" putway_time <= ? ",$condition['putway_time_end']);
        }
        if(isset($condition['add_time_start'])&&$condition['add_time_start']!=""){
            $select->where(" swap_order.add_time >= ? ",$condition['add_time_start']);
        }
         if(isset($condition['add_time_end'])&&$condition['add_time_end']!=""){
            $select->where(" swap_order.add_time <= ? ",$condition['add_time_end']);
        }
        if(isset($condition['is_qc'])&&$condition['is_qc']!=""){
            $select->where(" is_qc = ? ",$condition['is_qc']);
        }
        if(isset($condition['has_customer_qc'])&&$condition['has_customer_qc']!=""){
            $select->where(" has_customer_qc = ? ",$condition['has_customer_qc']);
        }
        /*CONDITION_END*/
        if ('count(*)' == $type) {
            return $this->_table->getAdapter()->fetchOne($select);
        } else {
             $select->group($table.'.swo_code');
            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);
        }
    }
}
