<?php
class Service_Orders
{
	public static $orderType = array(
	        '0'=>'Normal',
// 	        '1'=>'RTV',
	        '2'=>'Transfer',
	        '3'=>'Self Pickup',
	        );
	public static function getOrderType(){
		$status[0] = '普通';
		$status[1] = '转仓';
		$status[2] = '退货';
		return $status;
	}
	/*
	 * Create A Record
	 *
	 * @param
	 *       	 rowSet
	 * @return boolean
	 */
	public static function add($row)
	{
		$model = new Table_Orders();
		return $model->add($row);
	}
	
	/*
	 * Update One Row
	 *
	 * @param $row rowSet       	
	 * @param $order_id int       	
	 * @return boolean
	 */
	public static function update($row, $value , $field = "order_id")
	{
		$model = new Table_Orders();
		return $model->update($row, $value,$field);
	}
	
	public static function delete($value , $field = "order_id")
	{
		$model = new Table_Orders();
		return $model->delete($value,$field);
	}
	
	public static function getByField($value, $field = 'order_id', $colums = "*")
	{
		$model = new Table_Orders();
		return $model->getByField($value, $field, $colums);
	}

	public static function getAll()
	{
		$model = new Table_Orders();
		return $model->getAll();
	}
	public static function getByCondition($condition = array (), $type = '*', $pageSize = 0, $page = 1, $order = "")
	{
		$model = new Table_Orders();
		$rows = $model->getByCondition($condition, $type, $pageSize, $page, $order);
		return $rows;
	}
    public  static function getByFieldUpdate($order_id,$field = 'order_id', $colums = "*"){
        $model = new Table_Orders();
        return $model->getByFieldUpdate($order_id,$field,$colums);
    }

	public static function getOrderAllInfo($ordersCode,$customerId){
	   $order =  self::getByField($ordersCode,'order_code');
	   //print_r($order);
	   
	   if($order['customer_id']!=$customerId){
	       return false;
	   }
	  
	   $orderElse = Common_DataCache::getOrderElseInfo($ordersCode,$customerId);
	   //exit;
	   $order = array_merge($orderElse, $order);
	   /* echo "<pre>";
	   print_r($order);
	   echo '</pre>'; */
	   if(in_array($order['order_status'], array(4,5,6))){
            //$orderShip = service_OrderShip::getByField($ordersCode, 'orders_code');
            //$order['order_ship'] = $orderShip;
			//print_r($order);exit;
        }
        if( $order['sm_code'] == 'DGF-CARGO' ) {
            $shipOrder = Service_OrderShipping::getByField( $order['order_code'], 'order_code');
            if( $shipOrder ) {
                $order['shipInfo'] = array(
                    'notify_tel'          => $shipOrder['notify_tel'],
                    'notify_contact'     => $shipOrder['notify_contact'],
                    'notify_addr'         => $shipOrder['notify_addr'],
                    'destination'        => $shipOrder['destination'],
                    'is_dgd'               => $shipOrder['is_dgd'],
                    'hs_description'      => $shipOrder['hs_description'],
                    'insured_account'     => $shipOrder['insured_account'],
                );
            }
        }
	   return $order;
	}
	
	public static function getCustomerOrderStatusGroup($condition)
	{
	    $model = new Table_Orders();
	    $type = array('customer_id','order_status','count(*) count');
	    $group = "order_status" ;
	    $rows = $model->getCustomerOrderStatusGroup($condition, $type,$group);
	    $result = array();
	    foreach($rows as $v){
	        $result[$v['order_status']] = $v['count'];
	    }
	    return $result;
	}
	
	/**
	 * 计算订单的重量
	 * @author solar
	 * @param string $order_code
	 * @return number
	 */
	public static function calculateWeight($order_code) {
		$model = new Table_Orders();
		return $model->calculateWeight($order_code);
	}
	/**
	 * 状态统计
	 * @author solar
	 * @param int $customer_id
	 * @param int $model_type
	 * @param int $warehouse_id
	 */
	public static function stat($customer_id, $model_type, $warehouse_id) {
		$model = new Table_Orders();
		return $model->stat($customer_id, $model_type, $warehouse_id);
	}	
	/**
	 * 统计订单的HsCode数量
	 * @author solar
	 * @param string $order_code
	 * @return int
	 */
	public static function countHsCode($order_code) {
		$model = new Table_Orders();
		return $model->countHsCode($order_code);
	}
	/*取得一行colin*/
	public static function getRowByCondition($condition, $type = '*')
	{
		$model = new Table_Orders();
		return $model->getRowByCondition($condition, $type);
	}	

	/*获取查询sql*/
	public static function getSql($condition)
	{
		$model = new Table_Orders();
		return $model->getSql($condition);
	}
}