package com.dxhy.core.task;

import java.util.HashSet;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.core.dao.ConfirmDao;
import com.dxhy.core.job.entity.TAcOrg;
import com.dxhy.core.job.service.DataPlatformCollectService;
import com.dxhy.core.job.thread.DataPlatformInvoiceHandle;
import com.dxhy.core.job.thread.DataPlatformInvoiceParseHandle;
import com.dxhy.core.job.thread.DataPlatformInvoiceStateHandle;

import lombok.extern.slf4j.Slf4j;

/**
 * @author user
 */

@Component("dataPlatformInterfaceTask")
@Slf4j
@SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")

public class DataPlatformInterfaceTask {
    @Value("${jxjk.threadNum}")
    private Integer threadNum;
    @Resource
    private DataPlatformCollectService dataPlatformCollectService;
    @Resource
    private ConfirmDao confirmDao;

    /**
     * 请求采集进项数据
     *
     * @return 返回
     */

    public String collectJX() throws InterruptedException {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        List<TAcOrg> list = dataPlatformCollectService.getOrgTaxno();
        log.info("--对接数据平台采集任务开始--");
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 15, 5, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(list.size() * 4), new ThreadPoolExecutor.CallerRunsPolicy());
        // 获取购方税号信息，采集进销项发票

        HashSet<String> set = new HashSet<>();
        set.add("04");
        set.add("10");
        set.add("11");
        set.add("15");
        for (TAcOrg org : list) {
            for (String type : set) {

                // 采集进项数据
                String taxno = org.getTaxno();
                String db = org.getDbName();
                String company = org.getCompany();
                String aceId = org.getDataAceId();
                String aceKey = org.getDataAceKey();
                threadPoolExecutor
                    .execute(new DataPlatformInvoiceHandle(type, taxno, db, company, "", "1", aceId, aceKey));

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

        return "";
    }

    /**
     * 请求采集销项数据
     *
     * @return 返回
     * @throws InterruptedException
     *             异常
     */
    public String collectXX() throws InterruptedException {
        log.info("--对接数据平台采集任务开始--");
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        List<TAcOrg> list = dataPlatformCollectService.getOrgTaxno();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 15, 5, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(list.size() * 7), new ThreadPoolExecutor.CallerRunsPolicy());
        // 获取购方税号信息，采集进销项发票

        HashSet<String> set = new HashSet<>();
        set.add("04");
        set.add("10");
        set.add("11");
        set.add("15");
        set.add("01");
        set.add("03");
        set.add("14");
        set.add("08");
        for (TAcOrg org : list) {
            for (String type : set) {

                // 采集进项数据
                String taxno = org.getTaxno();
                String db = org.getDbName();
                String company = org.getCompany();
                String aceId = org.getDataAceId();
                String aceKey = org.getDataAceKey();
                threadPoolExecutor
                    .execute(new DataPlatformInvoiceHandle(type, taxno, db, company, "", "2", aceId, aceKey));

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

        return "";
    }

    /**
     * @throws InterruptedException
     *             异常
     * @Desc 发票获取--解析定时任务
     */
    public void parseInvoices() throws InterruptedException {
        log.info("数据平台发票解析任务执行开始-");

        // 查询数据库 遍历数据源
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        List<String> dbList = confirmDao.getDbSource();
        for (String db : dbList) {
            DynamicContextHolder.push(db + DbConstant.BUSINESS_READ);
            List<String> taxnoes = dataPlatformCollectService.getNeedParseTaxno();

            if (taxnoes.size() > 0) {
                // 准备请求参数
                ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 15, 5, TimeUnit.SECONDS,
                    new LinkedBlockingQueue<>(taxnoes.size()), new ThreadPoolExecutor.CallerRunsPolicy());
                for (String taxno : taxnoes) {
                    if (!threadPoolExecutor.isShutdown()) {
                        threadPoolExecutor.execute(new DataPlatformInvoiceParseHandle(taxno, db));
                    }

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

        }

        log.info("数据平台发票解析任务执行结束--");

    }

    /**
     * 获取状态信息 --进项
     */
    public String syncJXStatus() throws InterruptedException {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        List<TAcOrg> list = dataPlatformCollectService.getOrgTaxno();
        // 准备请求参数
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 15, 5, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(list.size()), new ThreadPoolExecutor.CallerRunsPolicy());

        for (TAcOrg org : list) {
            if (!threadPoolExecutor.isShutdown()) {
                String taxno = org.getTaxno();
                String db = org.getDbName();
                String taxname = org.getTaxname();
                String company = org.getCompany();
                String aceId = org.getDataAceId();
                String aceKey = org.getDataAceKey();
                threadPoolExecutor
                    .execute(new DataPlatformInvoiceStateHandle(taxno, db, taxname, "1", "", company, aceId, aceKey));
            }
        }
        threadPoolExecutor.shutdown();
        threadPoolExecutor.awaitTermination(60, TimeUnit.SECONDS);
        log.info("--对接数据平台进项获取状态结束--");
        return "";
    }

    /**
     * 获取状态信息 --销项
     */
    public String syncXXStatus() throws InterruptedException {
        log.info("--对接数据平台销项获取状态开始--");
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        List<TAcOrg> list = dataPlatformCollectService.getOrgTaxno();
        // 准备请求参数
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 15, 5, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(list.size()), new ThreadPoolExecutor.CallerRunsPolicy());

        for (TAcOrg org : list) {
            if (!threadPoolExecutor.isShutdown()) {
                String taxno = org.getTaxno();
                String db = org.getDbName();
                String taxname = org.getTaxname();
                String company = org.getCompany();
                String aceId = org.getDataAceId();
                String aceKey = org.getDataAceKey();
                threadPoolExecutor
                    .execute(new DataPlatformInvoiceStateHandle(taxno, db, taxname, "2", "", company, aceId, aceKey));
            }
        }
        threadPoolExecutor.shutdown();
        threadPoolExecutor.awaitTermination(60, TimeUnit.SECONDS);
        log.info("--对接数据平台销项获取状态结束--");
        return "";
    }
}
