//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.chinacreator.process.job;

import com.chinacreator.process.bean.ContinueBean;
import com.chinacreator.process.bean.ContinueLogBean;
import com.chinacreator.process.dao.ContinueOrderDao;
import com.chinacreator.process.dao.DictionaryDao;
import com.chinacreator.process.util.JsonUtil;
import com.chinacreator.process.util.SpringUtils;
import com.chinacreator.process.util.URLUtil;
import org.apache.log4j.Logger;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.PersistJobDataAfterExecution;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class ContinueOrder {
	
    private static Logger log = Logger.getLogger(ContinueOrder.class);
    
    private static Logger logger = Logger.getLogger("continueOrder");
    
    @Autowired
    private ContinueOrderDao continueOrderDao;

    public ContinueOrder() {
    }

    public void doProcess() throws Exception {
    	logger.info("========》续送活动计费开始");
        List<ContinueBean> list = this.continueOrderDao.queryNeedOrderDo();
        if (list != null && list.size() > 0) {
        	logger.info("获取数据条数："+list.size());
            CountDownLatch threadSignal = new CountDownLatch(list.size());
            ExecutorService executorService = new ThreadPoolExecutor(12, 16, 10L, TimeUnit.MINUTES, new LinkedBlockingQueue(), new CallerRunsPolicy());
            Iterator var5 = list.iterator();

            while(var5.hasNext()) {
                ContinueBean continueBean = (ContinueBean)var5.next();
                ContinueOrderService continueService = new ContinueOrderService(list.size(), threadSignal, continueBean);
                executorService.execute(continueService);
            }

            executorService.shutdown();

            try {
                executorService.awaitTermination(1L, TimeUnit.DAYS);
            } catch (InterruptedException var7) {
                var7.printStackTrace();
            }
        }

    }
}




class ContinueOrderService implements Runnable {
    private static Logger logger = Logger.getLogger("continueOrder");
    //private static Logger log = Logger.getLogger(ContinueOrderService.class);
    private int totalSize;
    private CountDownLatch threadSignal;
    private ContinueBean continueBean;
   // private static final String url = "http://10.199.99.144:8090/video-activity/continueOrder?";

    public ContinueOrderService(int totalSize, CountDownLatch threadSignal, ContinueBean continueBean) {
        this.totalSize = totalSize;
        this.threadSignal = threadSignal;
        this.continueBean = continueBean;
    }

    @Override
    public void run() {
        ContinueLogBean logBean = new ContinueLogBean();
        logBean.setUserid(this.continueBean.getUserid());
        long startime = System.currentTimeMillis();
        //续订接口地址
        String continueOrderUrl = "";

        try {
            DictionaryDao dictionaryDao = (DictionaryDao)SpringUtils.getBean(DictionaryDao.class);
            continueOrderUrl = dictionaryDao.getValue("continueOrderUrl");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("查字典表获取接口地址出现异常，" + e.getMessage());
            continueOrderUrl = "http://172.16.1.81:809/video-activity/continueOrder?";
        }
        
        String href = continueOrderUrl+"userid=" + this.continueBean.getUserid() + "&cpid=" + this.continueBean.getCpid() + "&spid=" + this.continueBean.getSpid() + "&activityType=" + this.continueBean.getActivityType();
        logBean.setReq(href);
        String result = "1";
        String errorinfo = "";

        try {
            result = URLUtil.get(href, 15000);
            logBean.setRsp(result);
            Map<?, ?> map = JsonUtil.jsonToMap(result);
            result = (String)map.get("resultcode");
            if (!result.equals("0")) {
                errorinfo = (String)map.get("errorinfo");
            }

            logBean.setTimes(String.valueOf(System.currentTimeMillis() - startime));
        } catch (Exception e) {
            result = "2";
            e.printStackTrace();
            logger.error(this.continueBean.getUserid()+"调续订接口出现异常，"+e.getMessage());
        } finally {
            this.threadSignal.countDown();
            logger.info("续送[" + this.continueBean.getUserid() + "," + this.continueBean.getCpid() + "]:结果=" + result + "[" + this.totalSize + "/" + ((long)this.totalSize - this.threadSignal.getCount()) + "],花费时间：" + (System.currentTimeMillis() - startime) + "/ms");
            logger.info(JsonUtil.objectToJson(logBean));

            try {
                ContinueOrderDao continueOrderDao = (ContinueOrderDao)SpringUtils.getBean(ContinueOrderDao.class);
                continueOrderDao.saveResult(this.continueBean.getUserid(), result, errorinfo, this.continueBean.getCpid(), this.continueBean.getSpid());
            } catch (Exception e) {
                e.printStackTrace();
                logger.error(this.continueBean.getUserid()+"更新续订状态出现异常，"+e.getMessage());
            }

        }

    }
}

