
package com.chinacreator.process.job;

import com.chinacreator.process.bean.ActivityConfigBean;
import com.chinacreator.process.bean.AreaInfo;
import com.chinacreator.process.bean.ContinueBean;
import com.chinacreator.process.bean.ContinueLogBean;
import com.chinacreator.process.dao.ActivityConfigDao;
import com.chinacreator.process.dao.CallerAreacodeDao;
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.commons.lang.StringUtils;
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 ContinueVIP {
    private static Logger log = Logger.getLogger(ContinueVIP.class);
    @Autowired
    private ContinueOrderDao continueOrderDao;

    public ContinueVIP() {
    }

    public void doProcess() throws Exception {
        log.info("========》续送VIP\t开始");
        List<ContinueBean> list = this.continueOrderDao.queryNeedVipDo();
        if (list != null && list.size() > 0) {
            log.info("========》续送VIP 数据条数：" + list.size());
            CountDownLatch threadSignal = new CountDownLatch(list.size());
            ExecutorService executorService = new ThreadPoolExecutor(40, 50, 10L, TimeUnit.MINUTES, new LinkedBlockingQueue(), new CallerRunsPolicy());
            Iterator var5 = list.iterator();

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

            executorService.shutdown();

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

    }
}


class ContinueService implements Runnable {
    private static Logger logger = Logger.getLogger("continueVip");
    private static Logger log = Logger.getLogger(ContinueService.class);
    private int totalSize;
    private CountDownLatch threadSignal;
    private ContinueBean continueBean;

    public ContinueService(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 continueVipUrl = "";

        try {
            DictionaryDao dictionaryDao = (DictionaryDao)SpringUtils.getBean(DictionaryDao.class);
            continueVipUrl = dictionaryDao.getValue("continueVipUrl");
        } catch (Exception var18) {
            var18.printStackTrace();
            log.error("查字典表获取接口地址出现异常，" + var18.getMessage());
            continueVipUrl = "http://10.199.99.144:8090/video-activity/continueVip?";
        }

        try {
            CallerAreacodeDao callerAreacodeDao = (CallerAreacodeDao)SpringUtils.getBean(CallerAreacodeDao.class);
            AreaInfo areaInfo = callerAreacodeDao.findByCallerhead(this.continueBean.getUserid().substring(0, 7));
            if (areaInfo != null) {
                this.continueBean.setProvince(areaInfo.getProvince());
                this.continueBean.setArea(areaInfo.getArea());
            }

            ActivityConfigDao activityConfigDao = (ActivityConfigDao)SpringUtils.getBean(ActivityConfigDao.class);
            ActivityConfigBean activityConfigBean = null;
            if (!StringUtils.isEmpty(this.continueBean.getFhcpid()) && !StringUtils.isEmpty(this.continueBean.getFhspid())) {
                activityConfigBean = activityConfigDao.findByCallerhead(this.continueBean.getFhcpid(), this.continueBean.getFhspid(), this.continueBean.getProvince());
            } else {
                activityConfigBean = activityConfigDao.findByCallerhead(this.continueBean.getCpid(), this.continueBean.getSpid(), this.continueBean.getProvince());
            }

            if (activityConfigBean != null) {
                this.continueBean.setActivityType(String.valueOf(activityConfigBean.getActiveType()));
            }
        } catch (Exception var22) {
            log.error("查询ACTIVETYPE出现异常，" + var22.getMessage());
        }

        String href = continueVipUrl + "userid=" + this.continueBean.getUserid() + "&cpid=" + this.continueBean.getCpid() + "&spid=" + this.continueBean.getSpid() + "&activityType=" + this.continueBean.getActivityType() + "&vippool=" + this.continueBean.getVippool()+"&orderchannel2="+this.continueBean.getOrderchannel2()+"&cancelchannel2="+this.continueBean.getCancelchannel2()+"&acid="+this.continueBean.getAcid();
        if (!StringUtils.isEmpty(this.continueBean.getFhcpid()) && !StringUtils.isEmpty(this.continueBean.getFhspid())) {
            href = continueVipUrl + "userid=" + this.continueBean.getUserid() + "&cpid=" + this.continueBean.getCpid() + "&spid=" + this.continueBean.getSpid() + "&activityType=" + this.continueBean.getActivityType() + "&vippool=" + this.continueBean.getVippool() + "&fhcpid=" + this.continueBean.getFhcpid() + "&fhspid=" + this.continueBean.getFhspid()+"&orderchannel2="+this.continueBean.getOrderchannel2()+"&cancelchannel2="+this.continueBean.getCancelchannel2()+"&acid="+this.continueBean.getAcid();
        }

        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 var20) {
            result = "2";
            if (StringUtils.isEmpty(logBean.getRsp())) {
                errorinfo = "调续送接口超时";
            } else {
                errorinfo = "解析返回值出现异常";
            }

            var20.printStackTrace();
        } finally {
            this.threadSignal.countDown();
            log.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);
                if (!StringUtils.isEmpty(this.continueBean.getFhcpid()) && !StringUtils.isEmpty(this.continueBean.getFhspid())) {
                    continueOrderDao.saveVipResultByFh(this.continueBean.getUserid(), result, errorinfo, this.continueBean.getCpid(), this.continueBean.getSpid(), this.continueBean.getFhcpid(), this.continueBean.getFhspid());
                } else {
                    continueOrderDao.saveVipResult(this.continueBean.getUserid(), result, errorinfo, this.continueBean.getCpid(), this.continueBean.getSpid());
                }
            } catch (SQLException var19) {
                var19.printStackTrace();
            }

        }

    }
}
