<?php
class Table_LogisticsOrder
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_LogisticsOrder();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_LogisticsOrder();
    }

    /**
     * @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 = "lo_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 = "lo_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 = 'lo_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 getByWhere($where, $colums = "*")
    {
    	$select = $this->_table->getAdapter()->select();
    	$table = $this->_table->info('name');
    	$select->from($table, $colums);
    	foreach($where as $field=>$value) {
    		$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);
    }
    
	/**
	 * or where 
	 * @param array $where 条件数组
	 * @param array $colums 查询字段
	 * @return mixed
	 */
    public function getByOrWhere($where, $colums){
    	$select = $this->_table->getAdapter()->select();
    	$table = $this->_table->info('name');
    	$select->from($table, $colums);
    	foreach($where as $field=>$value) {
    		$select->orWhere("{$field} = ?", $value);
    	}
    	return $this->_table->getAdapter()->fetchRow($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["lo_id"]) && $condition["lo_id"] != ""){
            $select->where("lo_id = ?",$condition["lo_id"]);
        }
        if (isset($condition["logistics_code"]) && $condition["logistics_code"] != "") {
            $select->where("logistics_code = ?", $condition["logistics_code"]);
        }
        if (isset($condition["customer_id"]) && $condition["customer_id"] != "") {
            $select->where("customer_id = ?", $condition["customer_id"]);
        }
       
        if (isset($condition["warehouse_id"]) && $condition["warehouse_id"] != "") {
            $select->where("warehouse_id = ?", $condition["warehouse_id"]);
        }

        if (isset($condition["sm_code"]) && $condition["sm_code"] != "") {
            $select->where("sm_code = ?", $condition["sm_code"]);
        }
        if(isset($condition['sm_code_Arr']) && $condition["sm_code_Arr"] != "" && is_array($condition['sm_code_Arr'])){
             $select->where("sm_code IN (?)", $condition["sm_code_Arr"]);
        }
        if (isset($condition["lo_status"]) && $condition["lo_status"] != "") {
            $select->where("lo_status = ?", $condition["lo_status"]);
        }
        if (isset($condition["except_deleted_status"]) && $condition["except_deleted_status"]) {
            $select->where("lo_status != ?", -1);
        }
        if (isset($condition["ref_tracking_number"]) && $condition["ref_tracking_number"] != "") {
            $select->where("ref_tracking_number = ?", $condition["ref_tracking_number"]);
        }
        if (isset($condition["reference_no"]) && $condition["reference_no"] != "") {
            $select->where("reference_no = ?", $condition["reference_no"]);
        }
        
        /*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 getJoinOrderByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")	{
	
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->join('logistics_order_address', 'logistics_order.logistics_code=logistics_order_address.logistics_code',array('loa_name','loa_company','loa_country','loa_zipcode','loa_phone','loa_address'));
        $select->where("1 =?", 1);
        /*CONDITION_START*/

       if(isset($condition["lo_id"]) && $condition["lo_id"] != ""){
            $select->where("lo_id = ?",$condition["lo_id"]);
        }
        if (isset($condition["order_code"]) && $condition["order_code"] != "") {
            $select->where("order_code = ?", $condition["order_code"]);
        }
        if (isset($condition["customer_id"]) && $condition["customer_id"] != "") {
            $select->where("customer_id = ?", $condition["customer_id"]);
        }
        if(isset($condition['logistics_code_arr'])&&$condition['logistics_code_arr']!=""){
            $select->where("logistics_order.logistics_code in (?)",$condition['logistics_code_arr']);
        }
        if (isset($condition["customer_code"]) && $condition["customer_code"] != "") {
            $select->where("customer_code = ?", $condition["customer_code"]);
        } 
       
        if (isset($condition["warehouse_id"]) && $condition["warehouse_id"] != "") {
            $select->where("warehouse_id = ?", $condition["warehouse_id"]);
        }

        if (isset($condition["sm_code"]) && $condition["sm_code"] != "") {
            $select->where("sm_code = ?", $condition["sm_code"]);
        }
        if (isset($condition["lo_status"]) && $condition["lo_status"] != "") {
            $select->where("lo_status = ?", $condition["lo_status"]);
        }
        if (isset($condition["order_status_arr"]) && $condition["order_status_arr"] != "") {
        	$select->where("lo_status in (?)", $condition["order_status_arr"]);
        }
        if (isset($condition["country_id"]) && $condition["country_id"] != "") {
            $select->where("loa_country = ?", $condition["country_id"]);
        }
        if (isset($condition["orders_code_CustomerReference"]) && $condition["orders_code_CustomerReference"] != "") {
		 	$select->where("(logistics_order.logistics_code = ?",$condition['orders_code_CustomerReference']);
            $select->orwhere("logistics_order.reference_no = ?)",$condition['orders_code_CustomerReference']);
        }
        if (isset($condition["order_pick_type"]) && $condition["order_pick_type"] != "") {
            $select->where("order_pick_type = ?", $condition["order_pick_type"]);
        }
        if (isset($condition["reference_no"]) && $condition["reference_no"] != "") {
            $select->where("reference_no = ?", $condition["reference_no"]);
        }
        if (isset($condition["add_time_start"]) && $condition["add_time_start"] != "") {
        	$select->where("lo_create_time >=?", $condition["add_time_start"] . ' 00:00:00');
        }
        
        if (isset($condition["add_time_end"]) && $condition["add_time_end"] != "") {
        	$select->where("lo_create_time <=?", $condition["add_time_end"] . ' 23:59:59');
        }
        if(isset($condition['loa_type'])&&$condition['loa_type']!=""){
            $select->where("loa_type = ?",$condition['loa_type']);
        }
		
        if(isset($condition['service_number'])&&$condition['service_number']!=""){
            $select->where("ref_tracking_number = ?",$condition['service_number']);
        }		



        /*CONDITION_END*/
        if ('count(*)' == $type) {
           // echo   $select->__toString();die;
            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();
			//echo $sql;
            return $this->_table->getAdapter()->fetchAll($sql);
        }  
	
	
	
	
	}
	
	
	
	
	
}