<?php
class Ship_CountryController extends Ec_Controller_Action
{
    public function preDispatch()
    {
        $this->tplDirectory = "ship/views/";
        $this->serviceClass = new Service_SmCountryMap();
    }

    public function listAction()
    {
        $warehouse=Common_DataCache::getWarehouseSimple();
        //$type=Common_Type::SMCountryMapType('auto');
        $smFeeType=Common_Type::SMFeeType('auto');
        $smCalcFeeType=Common_Type::SMCountryMapType('auto');
        if ($this->_request->isPost()) {
            $page = $this->_request->getParam('page', 1);
            $pageSize = $this->_request->getParam('pageSize', 20);

            $page = $page ? $page : 1;
            $pageSize = $pageSize ? $pageSize : 20;

            $return = array(
                "state" => 0,
                "message" => "No Data"
            );

            $params = $this->_request->getParams();

            $condition = $this->serviceClass->getMatchFields($params);
            unset($params);
            $count = $this->serviceClass->getLeftJoinCountryByCondition($condition, 'count(*)');
            $return['total'] = $count;
            if ($count) {
                $showFields=array(
                'country_id',
                'warehouse_id',
                'province_id',
                'city_id',
                'country_name_cn',
                'country_name',
                'country_code',
                'area',
                'sm_code',
                //'smcm_fee_type',
                'smcm_id',
                //'smcm_calc_type',
                 'pre_post_code',
                 'aircode',
                );
                $showFields = $this->serviceClass->getFieldsAlias($showFields);
                $rows = $this->serviceClass->getLeftJoinCountryByCondition($condition,$showFields, $pageSize, $page, array('smcm_id desc'));
                $return['data'] = $rows;
                $return['state'] = 1;
                //$return['smFeeType'] =$smFeeType;
                //$return['smCalcFeeType'] = $smCalcFeeType;                
                $return['warehouse'] = $warehouse;
                $return['message'] = "";
            }
            die(Zend_Json::encode($return));
        }
        /*$this->view->type=$type;
        $this->view->smFeeType=$smFeeType;
        $this->view->smCalcFeeType=$smCalcFeeType;*/
        $this->view->SM=Common_DataCache::getShippingMethodSimple();
        $shipMethod = Service_ShippingMethod::getAll();
        $sm = array();
        $smCodeArr = array();
        foreach($shipMethod as $key=>$value){
            $o = array(
                'sm_code'=>$value['sm_code'],
                'sm_name_cn'=>$value['sm_name_cn']
            );
            if(!in_array($value['sm_code'],$smCodeArr)){
                $sm[] = $o;
                $smCodeArr[] = $value['sm_code'];
            }
        }
        $sm=Common_Common::multisort($sm);
        $this->view->shipMethod = $sm;
        $this->view->countryArr=Common_DataCache::getCountry();
        $this->view->warehouse=$warehouse;
        echo Ec::renderTpl($this->tplDirectory . "country_index.tpl", 'layout');
    }

    public function editAction()
    {
        $return = array(
            'state' => 0,
            'message' => '',
            'errorMessage'=>array('Fail.')
        );

        if ($this->_request->isPost()) {
            $params = $this->_request->getParams();
            $row = array(
                
              'smcm_id'=>'',
              'country_id'=>'',
              'warehouse_id'=>'',
			  'warehouse_code'=>'',
              'province_id'=>'',
              'city_id'=>'',
              'country_code'=>'',
              'area'=>'',
              'sm_id'=>'',
              'smcm_fee_type'=>'',
              'smcm_calc_type'=>'',
                'pre_post_code'=>'',
                'aircode'=>'',
            );
            $row=$this->serviceClass->getMatchEditFields($params,$row);
            unset($params);
            $paramId = $row['smcm_id'];
            if (!empty($row['smcm_id'])) {
                unset($row['smcm_id']);
            }
            $errorArr = $this->serviceClass->validator($row);
			
			//仓库检查
			if(!$row['warehouse_id']){
				$row['warehouse_code'] = '';			
			}else{
				$warehouseArr=Service_Warehouse::getByField($row['warehouse_id'],'warehouse_id');
				if($warehouseArr){
					$row['warehouse_code'] = $warehouseArr['warehouse_code'];
				}else{
					$row['warehouse_code'] = '';
				}
			
			}
            //检查运输方式
            if(!$row['sm_id']){
                $errorArr[] = '运输方式必须选择';
            }else{
                $smRow = Service_ShippingMethod::getByField($row['sm_id']);
                if($smRow){
                    $row['sm_code']=$smRow['sm_code'];
                }else{
                    $errorArr[] = '运输方式找不到'.$row['sm_id'];
                }
            }
            //检查国家
			if(!$row['country_id']){
				$errorArr[] = '国家必须选择';
			}else{
            	$countryArr=Service_Country::getByField($row['country_id']);
				if($countryArr){
            		$row['country_code']=$countryArr['country_code'];
           			$row['country_name']=$countryArr['country_name_en'];
					$row['country_name_cn']=$countryArr['country_name'];	
				}else{
					$errorArr[] = '国家信息找不到';
				}	
                if($countryArr['country_id']==253){
                    //美国要输入城市
                    if($row['city_id']==''){
                        $errorArr[] = '美国的城市必须填写';
                    }else{
                        if($row['city_id']=='0'){
                            $row['city_id'] = '0';
                        }else{
                            $cityRows = Service_City::getByCondition(array('country_id' =>$countryArr['country_id'],'city_name'=>$row['city_id'],'sm_code' => $smRow['sm_code']));
                            $is_exist = empty($cityRows) && Service_City::getByWhere(array('country_id' => $countryArr['country_id'],'sm_code' => $smRow['sm_code']));
                            if(empty($cityRows) && empty($is_exist)){
                                $condition = array(
                                    'country_id'=>'253',
                                    'city_name'=>$row['city_id'],
                                    'sm_code' =>'0'
                                );
                                $cityRows=Service_City::getByCondition($condition,'*');
                            }
                            $cityRow = $cityRows[0];
                            if(empty($cityRow)){
                                $errorArr[] = '城市不存在';
                            }else{
                                if($cityRow['country_id']!='253'){
                                    $errorArr[] = '该城市不属于美国';
                                }else{
                                    $row['city_id'] = $cityRow['city_id'];
                                }
                            }
                        }
                    }
                }elseif($countryArr['country_id']==106){
                    //香港要输入城市
                    if($row['city_id']==''){
                        $errorArr[] = '香港的城市必须填写';
                    }else{
                        if($row['city_id']=='0'){
                            $row['city_id'] = '0';
                        }else{
                            $cityRows = Service_City::getByCondition(array('country_id' =>$countryArr['country_id'],'city_name'=>$row['city_id'],'sm_code' => $smRow['sm_code']));
                            $is_exist = empty($cityRows) && Service_City::getByWhere(array('country_id' => $countryArr['country_id'],'sm_code' => $smRow['sm_code']));
                            if(empty($cityRows) && empty($is_exist)){
                                $condition = array(
                                    'country_id'=>'106',
                                    'city_name'=>$row['city_id'],
                                    'sm_code'=>'0'
                                );
                                $cityRows=Service_City::getByCondition($condition,'*');
                            }
                            $cityRow = $cityRows[0];
                            if(empty($cityRow)){
                                $errorArr[] = '城市不存在';
                            }else{
                                if($cityRow['country_id']!='106'){
                                    $errorArr[] = '该城市不属于香港';
                                }else{
                                    $row['city_id'] = $cityRow['city_id'];
                                }
                            }
                        }
                    }
                }elseif($countryArr['country_id']==252&&isset($smRow)&&$smRow['sm_code']=="DHL-PDUK"){
                    if($row['city_id']==''){
                        $errorArr[] = 'DHL-PDUK英国的城市必须填写';
                    }else{
                        if($row['city_id']=='0'){
                            $row['city_id'] = '0';
                        }else{
                            $cityRows = Service_City::getByCondition(array('country_id' =>$countryArr['country_id'],'city_name'=>$row['city_id'],'sm_code' => $smRow['sm_code']));
                            $is_exist = empty($cityRows) && Service_City::getByWhere(array('country_id' => $countryArr['country_id'],'sm_code' => $smRow['sm_code']));
                            if(empty($cityRows) && empty($is_exist)){
                                $condition = array(
                                    'country_id'=>'252',
                                    'city_name'=>$row['city_id'],
                                    'sm_code'=>'0'
                                );
                                $cityRows=Service_City::getByCondition($condition,'*');
                            }
                            
                            $cityRow = $cityRows[0];
                            if(empty($cityRow)){
                                $errorArr[] = '城市不存在';
                            }else{
                                if($cityRow['country_id']!='252'){
                                    $errorArr[] = '该城市不属于英国';
                                }else{
                                    $row['city_id'] = $cityRow['city_id'];
                                }
                            }
                        }
                    }
                }else{
                    //其它只能是0
                    if($row['city_id']!='0'){
                        $row['city_id']= '0';
                        $errorArr[] = '不是美国和香港只能输入0,代表全部';
                    }
                }
			}
            $row["smcm_fee_type"]=$smRow['sm_fee_type'];
            $row["smcm_calc_type"]=$smRow['sm_calc_type'];


            if (!empty($errorArr)) {
                $return = array(
                    'state' => 0,
                    'message'=>'',
                    'errorMessage' => $errorArr
                );
                die(Zend_Json::encode($return));
            }

		
			
            unset($countryArr,$smRow);
            if (!empty($paramId)) {
                $result = $this->serviceClass->update($row, $paramId);
            } else {
                $result = $this->serviceClass->add($row);
            }
            if ($result) {
                $return['state'] = 1;
                $return['message'] = array('Success.');
            }

        }
        die(Zend_Json::encode($return));
    }

    public function getByJsonAction()
    {
        $result = array('state' => 0, 'message' => 'Fail', 'data' => array());
        $paramId = $this->_request->getParam('paramId', '');
        if (!empty($paramId) && $rows = $this->serviceClass->getByField($paramId, 'smcm_id')) {
            $rows=$this->serviceClass->getVirtualFields($rows);
            if($rows['E1']=='253'){
                if($rows['E4']!='0'){
                    $city=Service_City::getByField($rows['E4']);
                    if(!empty($city)){
                        $rows['E4']=$city['city_name'];
                    }
                }
            }
            if($rows['E1']=='106'){
                if($rows['E4']!='0'){
                    $city=Service_City::getByField($rows['E4']);
                    if(!empty($city)){
                        $rows['E4']=$city['city_name'];
                    }
                }
            }
            $result = array('state' => 1, 'message' => '', 'data' => $rows);
        }
        die(Zend_Json::encode($result));
    }

    public function deleteAction()
    {
        $result = array(
            "state" => 0,
            "message" => "Fail."
        );
        if ($this->_request->isPost()) {
            $paramId = $this->_request->getPost('paramId');
            if (!empty($paramId)) {
                if ($this->serviceClass->delete($paramId)) {
                    $result['state'] = 1;
                    $result['message'] = 'Success.';
                }
            }
        }
        die(Zend_Json::encode($result));
    }
	
	/*批量导入运输区域模板 colin*/
    public function batchImportTemplateAction(){
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . 'GMT');
        header('Cache-Control: no-cache, must-revalidate');
        header('Pragma: no-cache');
        $fullPath = APPLICATION_PATH."/../data/xls".'/ship_area_batch_import.xls';
        //方法2
        $filename = basename($fullPath);
        header("Content-Type: APPLICATION/OCTET-STREAM");
        //Force the download
        $header="Content-Disposition: attachment; filename=".$filename.";";
        header($header );
        // 	header("Content-Transfer-Encoding: binary");
        // 	header("Content-Length: ".$len);
        echo file_get_contents($fullPath);
        exit;
    }

	/*批量导入运输区域 colin*/
  public function batchImportAreaAction(){
        $result = array("ask"=>1,"message"=>"",'error'=>array(),'data'=>array());
        $readData = Service_SmCustomerMap::getXLSDataAndType($_FILES,'filename');
        if(isset($readData['value'])){
            $xlsData=$readData['value'];
        }else{
            $xlsData=$readData;
        }

	    if(count($xlsData[0])!=7||($xlsData[0][0]!="国家"||$xlsData[0][1]!="城市"||$xlsData[0][2]!="仓库"||$xlsData[0][3]!="运输代码"||$xlsData[0][4]!="运输分区"||$xlsData[0][5]!="邮编前缀"||$xlsData[0][6]!="机场三字码")){
            $result['ask'] = "0";
            $result['error'][] = "文件格式错误，首行不是按照国家,城市,仓库,运输代码,运输分区，邮编前缀、机场三字码排列!";
            die(json_encode($result));
        }
        foreach($xlsData as $key=>$value){
            if($key==0) continue;
            $existError = 0;
            $preCode=$value[5]?$value[5]:'';
            $aircode = $value[6]?$value[6]:'';
            
            $resultData = array(
                'country'=>$value[0],
                'city'=>$value[1],
                'warehouse'=>$value[2],
                'ship_code'=>$value[3],
            'ship_area'=>$value[4],
                'pre_post_code'=>$preCode,
                'aircode'=>$aircode,
                'ok'=>1
            );
            $resultData['message'] = "";
            if(isset($readData['type'][$key])){
                if(!($readData['type'][$key][5]['type']=='s'&&$readData['type'][$key][5]['fomart']=='@')){
                     $existError = 1;
                     $resultData['message'] .= "F{$key}单元格格式不正确，应设置成文本格式";
                }
            }
            if($value[0]==""){//国家标识
                $existError = 1;
                $resultData['message'] .= "国家代码为空;";
            }else{
				$country_element_array= explode('/',$value[0]);
				if(sizeof($country_element_array)!=2){
					 $existError = 1;
					 $resultData['message'] .= "国家代码格式不对;";
				}
			}
            if($value[1]==""){//城市
                $existError = 1;
                $resultData['message'] .= "城市为空;";
            }
            if($value[2]==""){//仓库代码
                $existError = 1;
                $resultData['message'] .= "仓库代码为空;";
            }
            if($value[3]===""){//
                $existError = 1;
                $resultData['message'] .= "运输代码为空;";
            }
            if($value[4]===""){
                $existError = 1;
                $resultData['message'] .= "运输区域为空;";
            }			
            if(strlen($value[5])>32){
                $existError = 1;
                $resultData['message'] .= "邮编前缀不能大于32个字符;";
            }
            if($aircode==='' && $value[3]=='DGF-CARGO'){
                $existError = 1;
                $resultData['message'] .= "DGF-CARGO运输方式必须填写机场三字码";
            }
            if($aircode!='' && $value[3]!='DGF-CARGO'){
                $existError = 1;
                $resultData['message'] .= "不是DGF-CARGO运输方式机场三字码不必填写";
            }
            // if($existError) {
            //     $resultData['ok'] = '0';
            //     $result['data'][] = $resultData;
            //     continue;
            // }
			$country_code = $country_element_array[0];	//国家代码
            $country = Service_Country::getByField($country_code,'country_code',"*");
            if(empty($country)){
                $resultData['message'] .= "不存在国家代码".$value[0];
                $resultData['ok'] = '0';
                $result['data'][] = $resultData;
                continue;
            }	
			if($country['country_id']==253){
                //美国要输入城市
                if($value['1']==''){
                    $resultData['message'] .= "美国的城市必须填写";
                    $resultData['ok'] = '0';
                    $result['data'][] = $resultData;
                    continue;
                }else{
                    if($value['1']!='全部'){
                        $cityRows = Service_City::getByCondition(array('country_id' =>$country['country_id'],'city_name'=>$value['1'],'sm_code' => $value[3]));
                        $is_exist = empty($cityRows) && Service_City::getByWhere(array('country_id' => $country['country_id'],'sm_code' => $value[3]));
                        if(empty($cityRows) && empty($is_exist)){
                            $condition = array(
                                'country_id'=>$country['country_id'],
                                'city_name'=>$value['1'],
                                'sm_code'=>'0'
                            );
                            $cityRows=Service_City::getByCondition($condition,'*');
                        }
                        $cityRow = $cityRows[0];
                        if(empty($cityRow)){
                            $resultData['message'] .= "城市不存在".$value[1];
                            $resultData['ok'] = '0';
                            $result['data'][] = $resultData;
                            continue;
                        }else{
                            if($cityRow['country_id']!='253'){
                                $resultData['message'] .= "该城市不属于美国".$value[1];
                                $resultData['ok'] = '0';
                                $result['data'][] = $resultData;
                                continue;
                            }
                        }
                    }
                }
            }elseif($country['country_id']==106){
                //香港要输入城市
                if($value['1']==''){
                    $resultData['message'] .= "香港的城市必须填写";
                    $resultData['ok'] = '0';
                    $result['data'][] = $resultData;
                    continue;
                }else{
                    if($value['1']!='全部'){
                        $cityRows = Service_City::getByCondition(array('country_id' =>$country['country_id'],'city_name'=>$value['1'],'sm_code' => $value[3]));
                        $is_exist = empty($cityRows) && Service_City::getByWhere(array('country_id' => $country['country_id'],'sm_code' => $value[3]));
                        if(empty($cityRows) && empty($is_exist)){
                            $condition = array(
                                'country_id'=>$country['country_id'],
                                'city_name'=>$value['1'],
                                'sm_code'=>'0'
                            );
                            $cityRows=Service_City::getByCondition($condition,'*');
                        }
                        $cityRow = $cityRows[0];
                        if(empty($cityRow)){
                            $resultData['message'] .= "城市不存在".$value[1];
                            $resultData['ok'] = '0';
                            $result['data'][] = $resultData;
                            continue;
                        }else{
                            if($cityRow['country_id']!='106'){
                                $resultData['message'] .= "该城市不属于香港".$value[1];
                                $resultData['ok'] = '0';
                                $result['data'][] = $resultData;
                                continue;
                            }
                        }
                    }
                }
            }elseif($country['country_id']==252&&$value[3]=="DHL-PDUK"){
                //美国要输入城市
                if($value['1']==''){
                    $resultData['message'] .= "DHL-PDUK英国的城市必须填写";
                    $resultData['ok'] = '0';
                    $result['data'][] = $resultData;
                    continue;
                }else{
                    if($value['1']!='全部'){
                        $cityRows = Service_City::getByCondition(array('country_id' =>$country['country_id'],'city_name'=>$value['1'],'sm_code' => $value[3]));
                        $is_exist = empty($cityRows) && Service_City::getByWhere(array('country_id' => $country['country_id'],'sm_code' => $value[3]));
                        if(empty($cityRows) && empty($is_exist)){
                            $condition = array(
                                'country_id'=>$country['country_id'],
                                'city_name'=>$value['1'],
                                'sm_code'=>'0'
                            );
                            $cityRows=Service_City::getByCondition($condition,'*');
                        }
                        $cityRow = $cityRows[0];
                        if(empty($cityRow)){
                            $resultData['message'] .= "城市不存在".$value[1];
                            $resultData['ok'] = '0';
                            $result['data'][] = $resultData;
                            continue;
                        }else{
                            if($cityRow['country_id']!='252'){
                                $resultData['message'] .= "该城市不属于英国".$value[1];
                                $resultData['ok'] = '0';
                                $result['data'][] = $resultData;
                                continue;
                            }
                        }
                    }
                }
            }else{
                if($value[1]!='全部'){
                    $resultData['message'] .= "国家不属于美国、香港、英国,城市必须是全部".$value[1];
                    $resultData['ok'] = '0';
                    $result['data'][] = $resultData;
                    continue;
                }
            }
			/*检查仓库代码*/	
			if($value[2]=='全部'){
				$warehouse_id = 0;
				$warehouse_code = '';
			}else{
			
				$warehouse = Service_Warehouse::getByField($value[2],'warehouse_code');
            	if(empty($warehouse)){
                	$resultData['message'] .= "仓库".$value[2]."不存在";
                	$resultData['ok'] = '0';
                	$result['data'][] = $resultData;
                	continue;
            	}
				$warehouse_id = $warehouse['warehouse_id']; 
				$warehouse_code = $warehouse['warehouse_id']; 				
							
			}	
			/*检查运输代码*/
            $condition = array(
                'warehouse_id_arr'=>array(0,$warehouse_id),
                'sm_code'=>$value[3],
            );
            $shippingMethod = Service_ShippingMethod::getByCondition($condition,"*");
			//$shippingMethod = Service_ShippingMethod::getByField($value[2],'sm_code');
            if(empty($shippingMethod)){
                $resultData['message'] .= "运输代码".$value[3]."不存在";
                $resultData['ok'] = '0';
                $result['data'][] = $resultData;
                continue;
            }
            if($value[3]=='DGF-CARGO'){
                $air = Service_AirportCountryMap::getByField($aircode,'airport_destination');
                if(empty($air)){
                    $resultData['message'] .= "机场三字码".$aircode."不存在";
                    $resultData['ok'] = '0';
                    $result['data'][] = $resultData;
                    continue;
                }
            }
			
			$row = array();	
			$row['country_id'] = $country['country_id'];
			$row['country_code'] = $country['country_code'];
			$row['country_name'] = $country['country_name_en'];
			$row['country_name_cn'] = $country['country_name'];
            $row['aircode'] = $air['airport_destination'];
            $row['city_id'] = '0';
            if($country['country_id']==106||$country['country_id']==253||($country['country_id']==252&&$value[3]=="DHL-PDUK")){
                if($value['1']!='全部'){
                    $row['city_id'] = $cityRow['city_id'];
                }
            }

			$row['warehouse_id'] = $warehouse_id;
			$row['warehouse_code'] = $warehouse_code;	
			
			$row['sm_code'] = $value[3];
			$row['sm_id'] = $shippingMethod[0]['sm_id'];
			
			$row['smcm_calc_type'] = $shippingMethod[0]['sm_calc_type'];
			$row['smcm_fee_type'] = $shippingMethod[0]['sm_fee_type'];
			$row['area'] = $value[4];
            if($preCode!==''){
                $row['pre_post_code']=$preCode;
            }
            if($aircode!=''){
                $row['aircode']=$aircode;
            }else{
                $row['aircode']='';
            }
            $status = Service_SmCountryMap::add($row);
            if($status){
                $resultData['message'] = "新增成功";
            }else{
                $resultData['message'] = "新增失败";
            }
            $result['data'][] = $resultData;
        }//foreach
        die(json_encode($result));
    }//function end	
	
	/*导出*/
    public function exportAction(){

		set_time_limit(0);
		$customerCode = $this->_request->getParam("customer_code","");
	
    	$codes = $this->_request->getParam('codes','');    	
    	$exportformat = 'xls';
    	$fileName = 'shiparea'.date('Y-m-d-H-i-s');	
		
    	
    	if(!empty($codes)){		//通过选择的订单获取
					
    		if($exportformat=='xls'){
    			$orderHtml = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><table><tr>';
                $htmlTitle .= '<td>国家二字码</td>';	
				$htmlTitle .= '<td>国家</td>';
                $htmlTitle .= '<td>城市</td>';
				$htmlTitle .= '<td>仓库</td>';
				$htmlTitle .= '<td>运输代码</td>';
    			$htmlTitle .= '<td>运输分区</td>';
                        	$htmlTitle .= '<td>邮编前缀</td>';
				$htmlTitle .= '</tr>';					
    			$titlesku = '';
    			$orderContent = '';
    			$max = 1;				
    			foreach($codes as $key=>$id){
    				 $SmCountryMap = Service_SmCountryMap::getByField($id, 'smcm_id');				 
					 if(!$SmCountryMap){continue;}
					 
					 	if($SmCountryMap['warehouse_id']){						
							 $warehouse = Service_Warehouse::getByField($SmCountryMap['warehouse_id'],'warehouse_id');
							 if($warehouse){
							 	$SmCountryMap['warehouse_code'] =  $warehouse['warehouse_code'];
							 }else{
							 	$SmCountryMap['warehouse_code'] = '全部';
							 }
						}else{
							$SmCountryMap['warehouse_code'] = '全部';
						}
						
                      
                            if($SmCountryMap['city_id']){                       
                                 $city = Service_City::getByField($SmCountryMap['city_id'],'city_id');
                                 if($city){
                                    $SmCountryMap['city_name'] =  $city['city_name'];
                                 }else{
                                    $SmCountryMap['city_name'] = '全部';
                                 }
                            }else{
                                    $SmCountryMap['city_name'] = '全部';
                            }
                         
                       //  print_r($SmCountryMap);exit;
    				 $orderContent.="<tr>";
                     //国家二字码
                      $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$SmCountryMap['country_code']}</td>";
					 //国家
					 $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$SmCountryMap['country_name_cn']}</td>";
                     //城市
                      $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$SmCountryMap['city_name']}</td>";
					 //仓库
					 $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$SmCountryMap['warehouse_code']}</td>";
					 //运输代码
					 $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$SmCountryMap['sm_code']}</td>";
					 //运输分区
					 $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$SmCountryMap['area']}</td>";
					 $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$SmCountryMap['pre_post_code']}</td>";
    				 
    				 $orderContent.="</tr>";
    			}   	
				$html = $orderHtml.$htmlTitle.$orderContent.'</table>';				
    	        header('Pragma:public');		
    			header('Content-Type:application/x-msexecl;name="' . $fileName . ".{$exportformat}");
    	        header("Content-Disposition:inline;filename=" . $fileName . ".{$exportformat}");   
    			//file_put_contents(APPLICATION_PATH . "/../data/{$fileName}",$html);
				echo $html;
    			exit;
    			//echo $orderHtml;exit;
    			
    		}elseif($exportformat=='csv'){
    			
    		}
    			
    	}else{		
				$country_id = $this->_request->getParam('E1','');	
				$sm_code = trim($this->_request->getParam('E9','')); 
				$warehouse_id = $this->_request->getParam('E2','');  
				$area=trim($this->_request->getParam('E7',''));		
				$sm=Service_ShippingMethod::getByField($sm_code,'sm_code');
        		$condition	= array(
            		'country_id'=>$country_id,
					'warehouse_id'=>$warehouse_id,
					'sm_id'=>$sm['sm_id'],
                    'area'=>$area							
        		);
        	
			//print_r($condition);exit;
    		$count = Service_SmCountryMap::getByCondition($condition, 'count(*)');
    		if($count>0){
    			$pageSize = 20;
    			$pages = ceil($count/20);

    			$orderHtml = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><table><tr>';	
				$htmlTitle .= '<td>国家二字码</td>'; 
                $htmlTitle .= '<td>国家</td>';
                $htmlTitle .= '<td>城市</td>';
                $htmlTitle .= '<td>仓库</td>';
				$htmlTitle .= '<td>运输代码</td>';
    			$htmlTitle .= '<td>运输分区</td>';
				$htmlTitle .= '</tr>';	
    			$orderContent = '';

    			$max = 1;

    			for($page=1;$page<=$pages;$page++){
    				$rows = Service_SmCountryMap::getByCondition($condition, '*', $pageSize,$page,'smcm_id desc');
					if(!$rows){continue;}
		
    				foreach($rows as $keyo=>$row){ 
						 
							if($row['warehouse_id']){						
								 $warehouse = Service_Warehouse::getByField($row['warehouse_id'],'warehouse_id');
								 if($warehouse){
									$row['warehouse_code'] =  $warehouse['warehouse_code'];
								 }else{
									$row['warehouse_code'] = '全部';
								 }
							}else{
									$row['warehouse_code'] = '全部';
							}
                       
                            if($row['city_id']){                       
                                 $city = Service_City::getByField($row['city_id'],'city_id');
                                 if($city){
                                    $row['city_name'] =  $city['city_name'];
                                 }else{
                                    $row['city_name'] = '全部';
                                 }
                            }else{
                                    $row['city_name'] = '全部';
                            }
                         //国家二字码
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$row['country_code']}</td>";						
						//国家											
						$orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$row['country_name_cn']}</td>";
                        //城市
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$row['city_name']}</td>";
						//仓库
						$orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$row['warehouse_code']}</td>";
						//运输代码
						$orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$row['sm_code']}</td>";						
						//运输分区
					 	$orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$row['area']}</td>";	
	    				$orderContent.="</tr>";
    				}

    			}   			
    		
    	        header('Pragma:public');		
    			header('Content-Type:application/x-msexecl;name="' . $fileName . ".{$exportformat}");
    	        header("Content-Disposition:inline;filename=" . $fileName . ".{$exportformat}");
    			echo $orderHtml.$htmlTitle.$orderContent.'</table>';
    		}
    	}
    	exit;

    }  


	/*批量删除*/
	public function batchDeleteAction(){
		$info = "系统错误";		
		$codesArray = $this->_request->getParam('codes','');
		if(empty($codesArray) || !is_array($codesArray)){  echo "没有选择待删除记录!!!";exit; }
		Service_SmCountryMap::batchDelete($codesArray,'smcm_id');
		$info = "已经删除";
		echo $info;
	}  
	
}