package com.chinacreator.process.job;


import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.PersistJobDataAfterExecution;
import org.springframework.beans.factory.annotation.Autowired;

import com.chinacreator.common.exception.BusinessException;
import com.chinacreator.common.util.AESUtil;
import com.chinacreator.common.util.MD5;
import com.chinacreator.process.bean.Tencent12Bean;
import com.chinacreator.process.dao.Tencent12Dao;
import com.chinacreator.process.util.DesUtil;
import com.chinacreator.process.util.JsonUtil;
import com.chinacreator.process.util.URLUtil;

@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class Tencent12OrderJob {

	@Autowired
	private Tencent12Dao tencent12Dao;
	
	private static Logger log = Logger.getLogger(Tencent12OrderJob.class);
	
	private final static String orderurl = "http://114.255.201.228:86/activity/order?";
	private final static String viperurl = "http://114.255.201.228:86/activity/order?";
	
	public void doProcess() throws Exception {
		List<Tencent12Bean> list = tencent12Dao.queryUnSync();
		if(list != null && list.size()>0){
			for (Tencent12Bean tencent12Bean : list) {
				try{
					order(tencent12Bean);
					giveVip(tencent12Bean);
				}catch(Exception e){
					
				}
			}
		}
	}
		
	public void order(Tencent12Bean tencent12Bean) throws Exception{
		String url = orderurl;
		String userid = tencent12Bean.getUserid();
		String cpid ="tencent";
		String spid="85";
		String pwd = "tenc1234";
		String timestamp = (System.currentTimeMillis()/1000)+"";
		userid = DesUtil.encode(AESUtil.encrypt(userid, "ac22273abb2f4960"), pwd);
		String signature = MD5.MD5Encode(cpid+spid+userid+pwd+timestamp);
		System.out.println(signature);
		url = url+"cpid="+cpid+"&spid="+spid+"&userid="+URLEncoder.encode(userid, "utf-8")+"&signature="+signature+"&timestamp="+timestamp+"&ordertype=2";
		System.out.println(url);
		String result = URLUtil.get(url);
		Map<?,?> map = JsonUtil.jsonToMap(result);
		if(!map.get("resultcode").equals("0")){
			throw new BusinessException((String)map.get("resultcode"), (String)map.get("errorinfo"));
		}
	}
	
	public void giveVip(Tencent12Bean tencent12Bean) throws UnsupportedEncodingException, BusinessException, Exception{
		String url = viperurl;
		url = url +"joinActivity?";
		String userid = "18574414678";
		String pwd = "tenc1234";
		userid = DesUtil.encode(AESUtil.encrypt(userid, "ac22273abb2f4960"), pwd);
		String timestamp = "1";
		String activetype = "6";
		String usertype = "1";
		String userval="306581007";
		String signature = MD5.MD5Encode(userid+userval+pwd+timestamp);
		url = url+"userid="+URLEncoder.encode(userid, "utf-8")+"&signature="+signature+"&timestamp="+timestamp+"&activetype="+activetype+"&usertype="+usertype+"&userval="+userval;
		System.out.println(url);
		String result = URLUtil.get(url);
		System.out.println(result);
	}
	
}
