package com.chinacreator.process.job;

import java.net.URLEncoder;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

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

import com.chinacreator.process.dao.ActiveMango3Dao;
import com.chinacreator.process.util.DesUtil;
import com.chinacreator.process.util.SHAUtil;
import com.chinacreator.process.util.URLUtil;

@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class ActiveMango3Job {

	private static Logger log = Logger.getLogger(ActiveMango3Job.class);
	
	@Autowired
	private ActiveMango3Dao dao;
	
	
	public void doProcess() throws Exception {
		log.info("开始查询参加过芒果3天活动");
		List<String> list =  null;
		long stime = System.currentTimeMillis();
		
		try {
			list = dao.queryNeedSmsDeal();
			log.info("需要芒果3天活动===="+(list == null ? 0 : list.size()));
			if(list != null && list.size() > 0){
				CountDownLatch threadSignal = new CountDownLatch(list.size());
				ExecutorService executorService = new ThreadPoolExecutor(8, 15, 10L, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.CallerRunsPolicy());
				for (String userid : list) {
					ActiveMango3Service fakeidService = new ActiveMango3Service(list.size(),threadSignal,userid,dao);
					executorService.execute(fakeidService);
				}
				executorService.shutdown();
				try {
					executorService.awaitTermination(1L, TimeUnit.MINUTES);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		log.info("电商伪码查询结束===="+((System.currentTimeMillis())-stime));
	}
	
	
}

class ActiveMango3Service implements Runnable {
	
	private static Logger logger = Logger.getLogger(ActiveMango3Service.class);
	private int totalSize;
	private CountDownLatch threadSignal;
	private String userid;
	private ActiveMango3Dao dao;
	
	
	public ActiveMango3Service(int totalSize,CountDownLatch threadSignal,String userid,ActiveMango3Dao dao){
		this.totalSize = totalSize;
		this.threadSignal = threadSignal;
		this.userid = userid;
		this.dao = dao;
	}
	
	public void run() {
		long startime = System.currentTimeMillis();
	
		try {
			String smsid="5003102";
			String key= "soews92d";
			String baseurl ="http://111.206.133.54/smsbusi/sms/send";
			String content = "【中国联通】"+this.userid+"，您的芒果TV会员即将到期。订购芒果TV定向流量会员包继续享受会员权益，还可免流看视频，同时还有759元惊喜大礼包等您来拿。快来点击订购吧 https://v.wo.cn/mg16hd20";
			String timestamp = System.currentTimeMillis()/1000+"";
			
			String usermob =  DesUtil.encode(userid,  key);
			String sign = SHAUtil.shaEncode(smsid+usermob+timestamp+content+key).toLowerCase();
			
			usermob = URLEncoder.encode(usermob,"utf-8");
			content = URLEncoder.encode(content,"utf-8");
			String url = baseurl + "?userid="+usermob+"&smsid="+smsid+"&timestamp="+timestamp+"&sign="+sign+"&content="+content;
			String resp = URLUtil.get(url);
			
			System.out.println(url+","+resp);
			
			dao.sendSmsSuccess(this.userid);
		} catch (Exception e) {
			e.printStackTrace();
			
			dao.sendSmsfail(this.userid);
		}
		
		threadSignal.countDown();
		logger.info("["+this.userid+"],["+ totalSize+"/"+(totalSize - threadSignal.getCount())+"],花费时间："+(System.currentTimeMillis()-startime+"/ms"));
	}
}
	

