package com.chinacreator.process.job;

import java.net.URLEncoder;
import java.sql.SQLException;
import java.util.HashMap;
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.util.URLUtil;
import com.chinacreator.process.dao.AsynDao;
import com.chinacreator.process.util.JsonUtil;

@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class Tencent12NotifyJob {

	private static final String notifyurl = "http://10.199.99.142:8090/video-activity/notify";
	private static final String orderyurl = "http://10.199.99.142:8090/video-activity/notifyOrder";
	private static final String vipurl = "http://10.199.99.142:8090/video-activity/notifyVip";

	private static Logger log = Logger.getLogger("tenc12log");

	@Autowired
	private AsynDao asynDao;

	public void doProcess() throws Exception {
		try{
			order();
		}catch(Exception e){
			e.printStackTrace();
		}
		try{
			 vip();
		}catch(Exception e){
			e.printStackTrace();
		}
		try{
			 sync();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void order() throws SQLException{
		List<String> orderlist = asynDao.getOrderOrderid();
		Map<String,String> orderMap = new HashMap<String, String>();
		if(orderlist != null){
			for (String orderid : orderlist) {
				orderMap.put("action", "order");
				orderMap.put("orderid", orderid);
				String  result = "";
				try{
					 result = URLUtil.get(orderyurl+"?orderid="+orderid, 5000);
				}catch(Exception e){
					e.printStackTrace();
					result = e.getMessage();
				}finally {
					orderMap.put("result", result);
					log.info(JsonUtil.objectToJson(orderMap));
				}
			}
		}
	}
	
	public void vip() throws SQLException{
		List<String> orderlist = asynDao.getvipOrderid();
		Map<String,String> orderMap = new HashMap<String, String>();
		if(orderlist != null){
			for (String orderid : orderlist) {
				orderMap.put("action", "vip");
				orderMap.put("orderid", orderid);
				String  result = "";
				try{
					 result = URLUtil.get(vipurl+"?orderid="+orderid, 5000);
				}catch(Exception e){
					e.printStackTrace();
					result = e.getMessage();
				}finally {
					orderMap.put("result", result);
					log.info(JsonUtil.objectToJson(orderMap));
				}
			}
		}
	}
	
	public void sync() throws SQLException{
		List<String> orderlist = asynDao.getsyncOrderid();
		Map<String,String> orderMap = new HashMap<String, String>();
		if(orderlist != null){
			for (String orderid : orderlist) {
				orderMap.put("action", "sync");
				orderMap.put("orderid", orderid);
				String  result = "";
				try{
					 result = URLUtil.get(notifyurl+"?orderid="+orderid, 5000);
				}catch(Exception e){
					e.printStackTrace();
					result = e.getMessage();
				}finally {
					orderMap.put("result", result);
					log.info(JsonUtil.objectToJson(orderMap));
				}
			}
		}
	}
}
