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.card.pojo.WxCardGroupon;
import com.zhiwei.weixin.http.HttpRequest;
import com.zhiwei.weixin.pojo.AccessToken;

/** 
 * @Package com.zhiwei.weixin.card
 * @ClassName: CreateCard
 * @Description: TODO(创建微信卡劵)
 * @author 志伟
 * @date 2015-3-25 下午3:16:08

 */
public class CreateCard {
	
	private static String create_cart_url = "https://api.weixin.qq.com/card/create?access_token=ACCESS_TOKEN";

    // 调用接口获取access_token  
	//记录日志
	private static Logger log = LoggerFactory.getLogger(CreateCard.class);
	
	private static String token;
    
	public CreateCard() 
	{
		token = new CreatAccessToken().getAccessToken();
	}

	
	public String addCart(WxCardGroupon wxCard)
	{
		
		String result = null;
		String url = create_cart_url.replace("ACCESS_TOKEN", token);
		
		String jsonCart = wxCard.toJsonString();
		System.out.println(jsonCart);
		
		JSONObject jsonObject = HttpRequest.httpRequest(url, "POST", jsonCart);
		System.out.println(jsonObject);
		if(jsonObject != null)
		{
			if(jsonObject.getInt("errcode") == 0)
			{
				System.out.println(jsonObject.getString("card_id"));
				result = jsonObject.getString("card_id");
			}else
			{
				result = null;
				log.error("创建卡劵失败 errcode:{} errmsg:{}",
						jsonObject.getInt("errcode"),
						jsonObject.getString("errmsg"));
			}
		}
		return result;
	}

}
