Demo entry 5865322

adf

   

Submitted by anonymous on Aug 16, 2016 at 04:35
Language: Java. Code size: 3.5 kB.

package com.ly.utils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ly.model.Hotel;
import com.ly.model.WXPolicy;
import com.ly.utils.GetHotelPolicysUtil2.GetPolicysThread;




public class GetHotelPolicysUtil4 {
	public static String baseUrl = PropertiesHelper.wxBaseUrl;
	static List<WXPolicy> policys = new ArrayList<>();
	static List<Hotel> hotels = null;
	static String lowerDateStr;
	static String larageDateStr;
	
	private static final java.util.concurrent.ExecutorService executorService = Executors.newFixedThreadPool(100);
	
	
	public static List<WXPolicy> getHotelPolicys(String... args) throws IOException, InterruptedException{
		
		//设置入离时间
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
		Date date = new Date();
		Date lowerDate = DateUtil.getNextDates(date, 7);
		Date larageDate = DateUtil.getNextDates(date, 8);
		lowerDateStr = dateFormat.format(lowerDate);   
		larageDateStr =dateFormat.format(larageDate);   
		
		
		
		if(args.length ==2){
			 hotels = GetHotelsUtil.getHotels(args[0],args[1]);
		}else if(args.length ==1){
			 hotels = GetHotelsUtil.getHotels(args[0]);
		}else{
			 hotels = GetHotelsUtil.getHotels();
		}
		
		
		
		for(int i=0;i<hotels.size();i++){
			GetPolicysThread my = new GetPolicysThread(hotels.get(i).getId());  
			executorService.execute(my);
		}
		return policys;
	}
	
	
	private static JSONArray doGetHotelPolicys(String hotelId,String arrivalDate,String departureDate){
		JSONArray policyListJSONArray = null;
		 try{
			 String hotelPolicys = "/hotelroombedinfo.html?HotelID="+hotelId+"&ArrivalDate="+arrivalDate+"&DepartureDate="+departureDate+"&RequestNum=1&SCompletedCount=0&RoomAdultNum=1&RoomChildNum=0&RoomChildAges=&RoomNum=1";
			 String rspStr = HttpClientUtil.doGet(baseUrl+hotelPolicys);
			 JSONObject rspJSON = JSON.parseObject(rspStr);
			 policyListJSONArray =JSON.parseObject(rspStr).getJSONObject("data").getJSONArray("RoomTypeList").getJSONObject(0).getJSONArray("PolicyList");
		 }catch(Exception e){
			 e.printStackTrace();
			 return null;
		 }
		
		 return policyListJSONArray;
	}
	
	
	
	
	static class GetPolicysThread implements Runnable{
		
		public String hotelId;
		
		public GetPolicysThread(String hotelId) {
			super();
			this.hotelId = hotelId;
		}


		@Override
		public void run() {
				//String hotelId = hotels.get(i).getId();
				JSONArray policysJSONArray = null;
				
				try{
					 policysJSONArray  = doGetHotelPolicys(hotelId, lowerDateStr, larageDateStr);
				}catch(Exception e){
					e.printStackTrace();
					//continue;
				}
				

				 for(int j=0;j<policysJSONArray.size();j++){
					 try{
						 JSONObject policyJSON = policysJSONArray.getJSONObject(j);
						 WXPolicy policy = JSON.parseObject(policyJSON.toString(), WXPolicy.class);
						 policy.setHotelId(hotelId);
						 System.out.println(policy);
						 policys.add(policy);
					 }catch(Exception e){
						 e.printStackTrace();
						 
						 //continue;
					 }
					
				 }
				
			
		}
		
	
	public static void main(String [] args) throws IOException, InterruptedException{
		List<WXPolicy> policys = GetHotelPolicysUtil4.getHotelPolicys("ALL","1000");
	}
	
}




}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).