package com.zhiwei.weixin.util;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

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

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

/** 
 * @Package com.zhiwei.weixin.util
 * @ClassName: TestWhiteList
 * @Description: TODO(添加测试白名单)
 * @author 志伟
 * @date 2015-3-27 上午10:12:24
 */
public class TestWhiteList {

	//拉取门店信息
			private static String testwhitelist_url = "https://api.weixin.qq.com/card/testwhitelist/set?access_token=TOKEN";
			 // 第三方用户唯一凭证  
		    private static String appId = "wx7d6a533ed451e07e";    
		    // 第三方用户唯一凭证密钥  
		    private static String appSecret = "217b1d655755033d74cb1fc27636288f";  //49a0abfc4507e90e6cab812414f005fb

		    // 调用接口获取access_token  
		    private static AccessToken at ;  
			//记录日志
			private static Logger log = LoggerFactory.getLogger(WeixinUtil.class);
			
			public TestWhiteList() 
			{
				if(at == null)
				{
					at = WeixinUtil.getAccessToken(appId, appSecret);
				}else
				{
					long b =System.currentTimeMillis() - at.getExpiresIn();
					if((b/1000)<=60)
					{
						at = WeixinUtil.getAccessToken(appId, appSecret);
					}
				}
			}
			/**
			 * 拉取门店信息
			 * 
			 * 
			 * **/
			public static int testWhiteList(List openidlist)
			{
				int result = 1;
				//p_VmbjmyRWFIpiixBEwLslPPUnqw  不可发的卡券
				String card_id = "p_VmbjslVeds9pFFJ73nGffSNfUY";     
				String url = testwhitelist_url.replace("TOKEN", at.getToken());
				
				
				// 调用接口拉取门店信息
				JSONObject json = new JSONObject();
				/**card json**/
				json.put("openid", openidlist);
				
				String testwhiteList = json.toString();
				System.out.println(testwhiteList);
				
				JSONObject jsonObject = HttpRequest.httpRequest(url, "POST", testwhiteList);
				System.out.println(jsonObject);
				if(jsonObject != null)
				{
					if(jsonObject.getInt("errcode") == 0)
					{
						result = 0;
					}
				}else
				{
					log.error("添加测试白名单失败  errcode:{} errmsg:{}",
							jsonObject.getInt("errcode"),
							jsonObject.getString("errmsg"));
				}
				
				return result;
			}
			
			
			/***/
			
			/**测试**/
			public static void main(String[] args)
			{
				String openid = "o_Vmbjj3F5q60Y9sxfOheXXp-G94";   //自己
				String openid2 = "o_VmbjjHKhsAoGF1HeJCWrdwHc8A";  //小号
				String openid3 = "o_VmbjnY46qtZJ3Joqqa0uJ9_hTs";  //唐哥
				String openid4 = "o_Vmbjnk1djDVaH965edgpQdshpg";  //满姐
				List list = new ArrayList();
				list.add(openid);
				list.add(openid2);
				list.add(openid3);
				list.add(openid4);
				int result = new TestWhiteList().testWhiteList(list);
				System.out.println(result);
			}

}
