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.DESUtil;
import com.chinacreator.process.bean.SmsSendBean;
import com.chinacreator.process.dao.SmsSendDao;
import com.chinacreator.process.util.JsonUtil;
import com.chinacreator.process.util.URLUtil;

@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class ActivateSendJob {
	
	private Logger log = Logger.getLogger("smslog");
	
	@Autowired
	private SmsSendDao smsSendDao;

	public void doProcess() throws Exception {
		System.out.println("ActivateSendJob开始启动");
		List<SmsSendBean> list = null;
		list = smsSendDao.selectActivateSms();
		if (list != null) {
			Map<String, String> map = null;
			for (SmsSendBean smsSendBean : list) {
				map = new HashMap<String, String>();
				//map.put("content", "尊敬的联通用户：您好！感谢您订购WO+腾讯定向流量包月视频风暴产品，在使用前请务必阅读使用说明，避免因不正当使用而导致的扣费现象，祝您使用愉快！使用说明：http://v.wo.cn/5cgYRh");
				String resultcode = "0";
				String errorinfo = "";
				try {
					map.put("userid", smsSendBean.getUserid());
					map.put("channel","asms");
					String url = "http://114.255.201.228:86/videoif/channelSmsSend.do";
					url = addUlrPar(url,"userid",URLEncoder.encode(DESUtil.encode(map.get("userid"),"idfdse33"),"utf-8"));
					url = addUlrPar(url,"apptype","2");
					url = addUlrPar(url,"channel",map.get("channel"));
					url = addUlrPar(url,"content",map.get("content"));
					String result = URLUtil.get(url,15000);
					Map<?, ?> inMap = JsonUtil.jsonToMap(result);
					resultcode = (String) inMap.get("resultCode");
					errorinfo = (String) inMap.get("errorInfo");
				} catch (Exception e) {
					e.printStackTrace();
					resultcode = "8000";
					errorinfo = "系统错误";
				} finally {
					map.put("resultcode", resultcode);
					map.put("errorinfo", errorinfo);
					smsSendDao.updateActivateSms(smsSendBean.getId(),resultcode.equals("0") ? "1" : "2");
					log.info(JsonUtil.objectToJson(map));
				}
			}
		}
	}
	
	public static String addUlrPar(String url, String key,String value){
		if (value == null)
			return url;
		if (url.indexOf("?") == -1) {
			url += "?";
		} else {
			url += "&";
		}
		return url + key + "=" + value;
	}
	
	public static void main(String[] args) throws SQLException {
	}
}
