package com.chinacreator.process.service;

import com.chinacreator.common.util.URLUtil;
import com.chinacreator.process.bean.OrderBean;
import com.chinacreator.process.dao.DictionaryDao;
import com.chinacreator.process.util.JsonUtil;
import com.chinacreator.process.util.fakeid.MD5;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.Date;
import java.util.Map;

/**
 * N天后订购送N天会员
 * 20190903
 * zzb
 */
@Component
public class OrderToVipService {
		
	private Logger logger = Logger.getLogger("recivemq");
	private Logger log = Logger.getLogger(OrderToVipService.class);
	
	@Autowired
	private DictionaryDao dictionaryDao;
	
	/**
	 * N天后订购送N天会员
	 * @param orderInfo
	 * @throws SQLException
	 */
	public String sendVip(OrderBean orderBean) throws SQLException {
		String resultcode = "1"; //无权限领取
		//目前支持优酷20元
		if(orderBean.getCpid().equals("youtu") && orderBean.getSpid().equals("1167")){
			String days = this.dictionaryDao.getValue("youkutwoDay");//多少天后订购送会员
			//String vips = this.dictionaryDao.getValue("youkutwoVip");//送多少天会员,由于送会员不能指定多少天，暂时默认七天
			Integer nowday = Integer.parseInt(days);
			int day = new Date().getDay();
			if(day>=nowday){
				try {
					logger.info("优酷20订购送7天会员，userId:"+orderBean.getUserid());
					String userid = orderBean.getUserid();
					String orderid = getOrderIdByUUId();
					//测试接口地址：http://114.255.201.228:86/vip/giveVip
					//正式接口地址：http://114.255.201.238:8090/videovip/giveVip
					String vipurl = this.dictionaryDao.getValue("youkutwoUrl");
					String apptype="2";
					String channel=this.dictionaryDao.getValue("youkutwoChannel");
					String timestamp = (System.currentTimeMillis() / 1000) + "";
					String usertype="4";
					String userval=userid;
					String vipid=this.dictionaryDao.getValue("youkutwoVipid");
					String viptype="1";//1：星期 2: 月 3：季 4：年
					String vipval="1";
					String pwd=this.dictionaryDao.getValue("youkutwoPwd");
					//md5(orderid+channel+vipid+userval+pwd+timestamp)
					String signature = MD5.MD5Encode(orderid + channel + vipid + userval + pwd + timestamp);
					signature = signature.toLowerCase();
					vipurl = vipurl + "?apptype="+apptype+"&orderid="+orderid+"&channel="+channel + "&timestamp=" + timestamp+ "&usertype=" + usertype+ "&userval=" + userval+ 
							"&vipid=" + vipid+ "&viptype=" + viptype+ "&vipval=" + vipval + "&signature=" + signature;
					logger.info("vipurl: "+vipurl);
					String result = URLUtil.get(vipurl,30*1000); //调赠送会员接口，超时时间设置为10秒
					logger.info("赠送会员结果=> userid: " +userid+", result: "+result);
					Map<?,?> map = JsonUtil.jsonToMap(result);
					resultcode = (String)map.get("resultcode");
					if(resultcode.equals("0")){
						log.info("赠送成功");
					}			
				} catch (Exception e) {
					resultcode="2";//失败
					e.printStackTrace();
					logger.error("userid: "+orderBean.getUserid()+"赠送会员失败，"+e);
				}
			}
		}
		
		return resultcode;
		
	} 
	
	public static String getOrderIdByUUId() {
		long a = System.currentTimeMillis();
		int b = (int)(Math.random()*900 + 100);
		StringBuffer bf = new StringBuffer();
		bf.append(a);
		bf.append(b);
        return bf.toString();
    }
	
	/*public static void main(String[] args) throws SQLException {
		String paths[]={"applicationContext.xml"};
		//加载spring的配置文件
		ApplicationContext context=new ClassPathXmlApplicationContext(paths);
		BeanFactory factory = (BeanFactory) context; 
		OrderToVipService atu=(OrderToVipService) factory.getBean(OrderToVipService.class);
		OrderBean orderBean = new OrderBean();
		orderBean.setCpid("youtu");
		orderBean.setSpid("1167");
		orderBean.setUserid("18974646566");
		String aaa = atu.sendVip(orderBean);
		System.out.println(aaa);
	}*/
}
