package com.zhiwei.weixin.card;

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;

/** 
 * @Package com.zhiwei.weixin.util
 * @ClassName: SendCard
 * @Description: TODO(通过二维码的方式发送卡劵)
 * @author 志伟
 * @date 2015-3-26 下午6:01:09
 */
public class SendCard {

	    //拉取门店信息
		private static String sendcard_url = "https://api.weixin.qq.com/card/qrcode/create?access_token=TOKEN";

		//记录日志
		private static Logger log = LoggerFactory.getLogger(SendCard.class);
		
		
		/**
		 * 拉取门店信息
		 * 
		 * 
		 * **/
		public static String sendCard(String openid)
		{
			String ticket = null;
			//p_VmbjmyRWFIpiixBEwLslPPUnqw  不可发的卡券
			String card_id = "p_Vmbjs0WI73ZfC7PuKFg8Y_8qvs";        //p_VmbjrMBygdqEbxI15mUvCF4Dm0
			String url = sendcard_url.replace("TOKEN", new CreatAccessToken().getAccessToken());
			
			// 调用接口拉取门店信息
			JSONObject json = new JSONObject();
			/**card json**/
			
			JSONObject card = new JSONObject();
			card.put("card_id", card_id);
//			card.put("openid", openid);
//			card.put("is_unique_code", false);
			
			
			/**action_info json**/
			JSONObject cardjson = new JSONObject();
			cardjson.put("card", card);
			/*action_name**/
			json.put("action_name", "QR_CARD");
			json.put("action_info", cardjson);
			
			String sendCard = json.toString();
			System.out.println(sendCard);
			
			JSONObject jsonObject = HttpRequest.httpRequest(url, "POST", sendCard);
			System.out.println(jsonObject);
			if(jsonObject != null)
			{
				if(jsonObject.getInt("errcode") == 0)
				{
					ticket = jsonObject.getString("ticket");
				}
			}else
			{
				log.error("发送卡劵失败 errcode:{} errmsg:{}",
						jsonObject.getInt("errcode"),
						jsonObject.getString("errmsg"));
				ticket = null;
			}
			return ticket;
		}
		
		
		/***/
		
		/**测试**/
		public static void main(String[] args)
		{
//			String openid = "o_Vmbjj3F5q60Y9sxfOheXXp-G94";
//			String result = new SendCard().sendCard(openid);
//			
//			System.out.println(result);
		}

}
