package com.dxhy.core.task;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.util.UserInfoUtil;
import com.dxhy.core.dao.ConfirmDao;
import com.dxhy.core.job.entity.ScheduleJobEntity;
import com.dxhy.core.job.entity.TAcOrg;
import com.dxhy.core.job.pojo.InvoiceHandle;
import com.dxhy.core.job.pojo.SupplierRiskHandle;
import com.dxhy.core.job.service.InvoiceInterfaceService;
import com.dxhy.core.job.service.ScheduleJobService;
import com.dxhy.core.job.thread.InvoiceParseHandle;
import com.dxhy.core.job.thread.InvoiceStateHandle;
import com.dxhy.core.job.thread.SelectInvoiceHandle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author fth
 * @date 2018年04月13日 下午10:57:22
 */
@SuppressWarnings({"rawtypes", "DuplicatedCode"})
@Component("supplierRiskTask")
@Slf4j
public class SupplierRiskTask extends AbstractController {
    @Resource
    private InvoiceInterfaceService invoiceInterfaceService;
    @Resource
    private ConfirmDao confirmDao;
    @Resource
    private ScheduleJobService scheduleJobService;
    /**
     * 核心线程池大小
     */
    private int corePoolSize = 10;
    /**
     * 最大线程池大小
     */

    private int maximumPoolSize = 20;

    /**
     * @throws InterruptedException 异常
     * @Desc 发票获取接口
     */

    public void getSupplierRisk() throws InterruptedException {
        log.info("获取供应商风险接口执行开始-");
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        ScheduleJobEntity scheduleJobEntity = scheduleJobService.queryByBeanName("supplierRiskTask", "getSupplierRisk");
        if (scheduleJobEntity != null && "0".equals(scheduleJobEntity.getJobStatus())) {
            try {
                scheduleJobEntity.setJobStatus("1");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
                // 准备请求参数
                // 需判断 设置不认证状态

                List<TAcOrg> list = invoiceInterfaceService.getTaxnoForQpm();
                log.info("当前企业为:{}", JSONObject.toJSONString(list));
                if(list != null && list.size()>0) {
                    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 60L,
                            TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(  4),
                            r -> new Thread(r, "获取供应商风险-" + r.hashCode()));

                    Map<String, Object> data = new HashMap<>(16);
                    data.put("dbName", "business");
                    List<String> gfshList = new ArrayList<>();
                    for(TAcOrg org:list){
                        gfshList.add(org.getTaxno());
                    }
                    data.put("gfsh", gfshList);
                    data.put("taxNo", "");
                    data.put("taxName", "");
                    // 设置日期格式
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                    String date = df.format(new Date());
                    data.put("currentTime", date);

                    log.info("请求供应商风险的参数为:{}",data);

                    if (!threadPoolExecutor.isShutdown()) {
                        threadPoolExecutor.execute(new SupplierRiskHandle(data));
                    }

                    threadPoolExecutor.shutdown();
                    threadPoolExecutor.awaitTermination(60, TimeUnit.SECONDS);

                }
                log.info("获取供应商风险执行结束--");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                log.info("获取供应商风险--重置执行状态");
                scheduleJobEntity.setJobStatus("0");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
            }
        }
    }

}