package com.zhiwei.weixin.card;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zhiwei.weixin.accesstoken.CreatAccessToken;
import com.zhiwei.weixin.http.HttpRequest;
import com.zhiwei.weixin.pojo.AccessToken;

/** 
 * @Package com.zhiwei.weixin.util
 * @ClassName: GetBatch
 * @Description: TODO(拉取门店信息)
 * @author 志伟
 * @date 2015-3-26 上午10:22:04

 */
public class GetBatch {
	//拉取门店信息
	private static String getbatch_url = "https://api.weixin.qq.com/card/location/batchget?access_token=TOKEN";

	//记录日志
	private static Logger log = LoggerFactory.getLogger(GetBatch.class);
	
	private static String token;
    
	public GetBatch() 
	{
		token = new CreatAccessToken().getAccessToken();
	}
	
	/**
	 * 拉取门店信息
	 * 
	 * 
	 * **/
	public static int getBatch()
	{
		int result = 1;
		
		String url = getbatch_url.replace("TOKEN", token);
		
		// 调用接口拉取门店信息
		JSONObject json = new JSONObject();
		json.put("offset", 1);
		json.put("count", 1);
		String getBatch = json.toString();
		JSONObject jsonObject = HttpRequest.httpRequest(url, "POST", getBatch);
		if(jsonObject != null)
		{
			if(jsonObject.getInt("errcode") == 0)
			{
				
				JSONArray list = (JSONArray) jsonObject.get("location_list");
				
				for(int i = 0;i<list.size();i++)
				{
					JSONObject js = list.getJSONObject(i);
//					if(js.getString("address").equals("广东省广州市海珠区广州市海珠区新港西路135号中山大学南校区怀士堂"))
//					{
						System.out.println("======"+js.get("address"));
						result = js.getInt("id");
						
//					}
				}
			}
		}else
		{
			log.error("拉取门店信息失败 errcode:{} errmsg:{}",
					jsonObject.getInt("errcode"),
					jsonObject.getString("errmsg"));
		}
		
		return result;
	}

	
	
	/**测试**/
	public static void main(String[] args)
	{
		int result = new GetBatch().getBatch();
		System.out.println(result);
	}
}
