package com.chinacreator.process.job;


import com.chinacreator.process.bean.DeveuploadBean;
import com.chinacreator.process.dao.DeveuploadDao;
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.util.HashMap;
import java.util.List;
import java.util.concurrent.*;

@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class DeveuploadJob {


    private Logger logger = Logger.getLogger("deveuploadlog");

    @Autowired
    private DeveuploadDao deveuploadDao;

    public void doProcess() throws Exception {
        logger.info("开始处理科创每日新增订购数据入库job");
        try {
            //获取表中需要订购的数据
            List<HashMap> dataList = deveuploadDao.queryList();
            logger.info("获取数据条数" + dataList.size());
            if (dataList != null && dataList.size() > 0) {
//                CountDownLatch threadSignal = new CountDownLatch(dataList.size());
                ExecutorService executorService = new ThreadPoolExecutor(10, 20, 10L, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.CallerRunsPolicy());

                for (HashMap<String, String> data : dataList) {
                    deveuploadDao.updataQueryList(data.get("ID"), "2");
                }
                for (HashMap<String, String> data : dataList) {
                    DeveuploadService continueService = new DeveuploadService(deveuploadDao,data);
                    executorService.execute(continueService);
                }
                executorService.shutdown();
                try {
                    executorService.awaitTermination(5L, TimeUnit.MINUTES);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            Thread.sleep(100);


            //获取执行状态为2且执行时间超过10分钟的数据为待处理
            int i = deveuploadDao.updExecTimeout();

            if (i > 0) {
                logger.info("更新状态为0处理中且执行时间超过10分钟的数据为待处理的异常数据条数【" + i + "】");
            }
        }catch (Exception ee){
            ee.printStackTrace();
            logger.info("科创每日新增订购数据入库job执行出现异常，"+ee.getMessage());
        }
        logger.info("结束开始处理科创每日新增订购数据入库job");
    }
}

class DeveuploadService implements Runnable {

    private static Logger logger = Logger.getLogger("deveuploadlog");
    private DeveuploadDao deveuploadDao;
    private HashMap<String,String> data; //订购数据

    public DeveuploadService(DeveuploadDao deveuploadDao, HashMap<String, String> data) {
        this.deveuploadDao = deveuploadDao;
        this.data = data;
    }

    @Override
    public void run() {
        String resultcode = "0";
        String resultinfo = "成功";
        String id = data.get("ID");
        String insertcount = data.get("INSERTCOUNT");
        HashMap<String,Object> logmap = new HashMap<String, Object>();
        logmap.put("data",data);
        try {
            String spid = data.get("SPID");
            String channel = "kc";
            String userid = data.get("USERID");
            String busiunit = "畅视事业部";
            String province = data.get("PROVINCE");
            String area = data.get("AREA");
            String orderchannel = data.get("ORDERCHANNEL");
            String orderchannel2 = data.get("ORDERCHANNEL2");
            String prodname = "";
            String prodprmid = "";
            String prodnoprmid = "";
            String orderchannelid = "";
            String orderchannelname = "";
            String orderchannelkind = "";
            String ordertime = data.get("ORDERTIME");
            List<HashMap> list = deveuploadDao.querySpinfo(spid);
            logmap.put("spdata",list);
            if (list!=null&&list.size()>0){
                HashMap spinfo = list.get(0);
                prodname = (String) spinfo.get("SPNAME");
                prodprmid = (String)spinfo.get("VACPRODUCTID");
                if (StringUtils.isEmpty(prodprmid)){
                    prodnoprmid = spid;
                }
            }
            if (!StringUtils.isEmpty(orderchannel2)){
                orderchannelid = orderchannel2;
                List<HashMap> list1 = deveuploadDao.queryChannel2(orderchannel2);
                logmap.put("channeldata",list1);
                if (list1!=null&&list1.size()>0){
                    HashMap hashMap = list1.get(0);
                    orderchannelname = (String) hashMap.get("SUBCHANNELNAME");
                    orderchannelkind = (String) hashMap.get("CHANNELTYPE");
                }else {
                    list1 = deveuploadDao.queryChannel(orderchannel2);
                    if (list1!=null&&list1.size()>0){
                        HashMap hashMap = list1.get(0);
                        orderchannelname = (String) hashMap.get("NAME");
                        orderchannelkind = (String) hashMap.get("BELONGTO");
                    }
                }
            }else if (!StringUtils.isEmpty(orderchannel)){
                orderchannelid = orderchannel;
                List<HashMap> list1 = deveuploadDao.queryChannel(orderchannel);
                logmap.put("channeldata",list1);
                if (list1!=null&&list1.size()>0){
                    HashMap hashMap = list1.get(0);
                    orderchannelname = (String) hashMap.get("NAME");
                    orderchannelkind = (String) hashMap.get("BELONGTO");
                }
            }

            DeveuploadBean bean = new DeveuploadBean();
            bean.setChannel(channel);
            bean.setUserid(userid);
            bean.setBusiunit(busiunit);
            bean.setProvince(province);
            bean.setArea(area);
            bean.setProdname(prodname);
            bean.setProdprmid(prodprmid);
            bean.setProdnoprmid(prodnoprmid);
            bean.setOrderchannelid(orderchannelid);
            bean.setOrderchannelname(orderchannelname);
            bean.setOrderchannelkind(orderchannelkind);
            bean.setOrdertime(ordertime);
            logmap.put("bean",bean);
            deveuploadDao.insertBeans(bean);
        }catch (Exception e){
            e.printStackTrace();
            resultcode = "8000";
            resultinfo = e.getMessage();
        }finally {
            logmap.put("resultcode",resultcode);
            logmap.put("resultinfo",resultinfo);
            try {
                deveuploadDao.updata(id,resultcode,resultinfo,Integer.parseInt(insertcount)+1);
            }catch (Exception e){
                e.printStackTrace();
                logger.error("id: "+id+"修改失败："+e.getMessage());
            }
            logger.info(logmap);
        }

    }
}
