package net.northking.cctp.dw.bus;

import net.northking.cctp.dw.api.service.MtManualExecJobApiService;
import net.northking.cctp.dw.constant.DWConstant;
import net.northking.cctp.dw.db.entity.*;
import net.northking.cctp.dw.db.service.MtCaseExecChartService;
import net.northking.cctp.dw.db.service.MtCaseExecTrendChartService;
import net.northking.cctp.dw.db.service.MtCaseNonExecService;
import net.northking.cctp.dw.utils.DateDealUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.List;
import java.util.stream.Collectors;


//@Component
public class ExecJobDataListener {

    private final static Logger logger = LoggerFactory.getLogger(ExecJobDataListener.class);

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TransactionTemplate transactionTemplate;

    @Autowired
    private MtManualExecJobApiService mtManualExecJobApiService;

    @Autowired
    private MtCaseExecChartService mtCaseExecChartService;

    @Autowired
    private MtCaseExecTrendChartService mtCaseExecTrendChartService;

    @Autowired
    private MtCaseNonExecService mtCaseNonExecService;

    //mt_case_exec_pass_ratio、mt_case_exec_overall、mt_case_exec_chart、mt_case_exec_trend_chart
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(name = DWConstant.MQ_HANDLE_DATA_EXCHANGE, type = ExchangeTypes.TOPIC),
            value = @Queue(name = DWConstant.BugQueue.EXEC_JOB_RATIO_PRODUCER_DATA_QUEUE),
            key = DWConstant.BugRoutingKey.EXEC_JOB_RATIO_PRODUCER_DATA))
    public void execJobRatioDataListener(List<DwManualExecJob> dataList) {
        if (dataList != null && !dataList.isEmpty()) {
            transactionTemplate.execute(status -> {
                // 获取projectIds
                List<String> projectIds = dataList.stream().map(DwManualExecJob::getProjectId).collect(Collectors.toList());
                DwManualExecJob dwManualExecJob = new DwManualExecJob();
                dwManualExecJob.setProjectIds(projectIds);
                dwManualExecJob.setTenantId(dataList.get(0).getTenantId());

                // 处理用例执行通过率
                // 每次导入数据之前先清空表数据
                MtCaseExecPassRatio ratio = new MtCaseExecPassRatio();
                ratio.setProjectIds(projectIds);
                mtManualExecJobApiService.deleteAllExecPassRatioDataByProjectIds(ratio);

                // 插入数据
                mtManualExecJobApiService.caseExecPassRatio(dwManualExecJob);

                return Boolean.TRUE;
            });
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(name = DWConstant.MQ_HANDLE_DATA_EXCHANGE, type = ExchangeTypes.TOPIC),
            value = @Queue(name = DWConstant.BugQueue.EXEC_JOB_OVERALL_PRODUCER_DATA_QUEUE),
            key = DWConstant.BugRoutingKey.EXEC_JOB_OVERALL_PRODUCER_DATA))
    public void execJobOverallDataListener(List<DwManualExecJob> dataList) {
        if (dataList != null && !dataList.isEmpty()) {
            transactionTemplate.execute(status -> {
                // 获取projectIds
                List<String> projectIds = dataList.stream().map(DwManualExecJob::getProjectId).collect(Collectors.toList());
                DwManualExecJob dwManualExecJob = new DwManualExecJob();
                dwManualExecJob.setProjectIds(projectIds);
                dwManualExecJob.setTenantId(dataList.get(0).getTenantId());

                // 用例执行总体情况
                // 先清空数据
                MtCaseExecOverall overall = new MtCaseExecOverall();
                overall.setProjectIds(projectIds);
                mtManualExecJobApiService.deleteAllOverallByProjectIds(overall);

                // 插入数据
                mtManualExecJobApiService.caseExecOverall(dwManualExecJob);

                return Boolean.TRUE;
            });
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(name = DWConstant.MQ_HANDLE_DATA_EXCHANGE, type = ExchangeTypes.TOPIC),
            value = @Queue(name = DWConstant.BugQueue.EXEC_JOB_CHART_PRODUCER_DATA_QUEUE),
            key = DWConstant.BugRoutingKey.EXEC_JOB_CHART_PRODUCER_DATA))
    public void execJobChartDataListener(List<DwManualExecJob> dataList) {
        if (dataList != null && !dataList.isEmpty()) {
            transactionTemplate.execute(status -> {
                // 获取projectIds
                List<String> projectIds = dataList.stream().map(DwManualExecJob::getProjectId).collect(Collectors.toList());
                DwManualExecJob dwManualExecJob = new DwManualExecJob();
                dwManualExecJob.setProjectIds(projectIds);
                dwManualExecJob.setTenantId(dataList.get(0).getTenantId());

                // 用例执行统计图
                // 每次导入数据之前先清空表数据
                MtCaseExecChart mtCaseExecChart = new MtCaseExecChart();
                mtCaseExecChart.setCreatedTime(DateDealUtils.getCurrentDate());
                mtCaseExecChart.setProjectIds(projectIds);
                mtCaseExecChartService.deleteByExampleByProjectIds(mtCaseExecChart);

                // 插入数据
                mtCaseExecChartService.syncExecNotice(dwManualExecJob);

                return Boolean.TRUE;
            });
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(name = DWConstant.MQ_HANDLE_DATA_EXCHANGE, type = ExchangeTypes.TOPIC),
            value = @Queue(name = DWConstant.BugQueue.EXEC_JOB_TREND_CHART_PRODUCER_DATA_QUEUE),
            key = DWConstant.BugRoutingKey.EXEC_JOB_TREND_CHART_PRODUCER_DATA))
    public void execJobTrendChartDataListener(List<DwManualExecJob> dataList) {
        if (dataList != null && !dataList.isEmpty()) {
            // 获取projectIds
            List<String> projectIds = dataList.stream().map(DwManualExecJob::getProjectId).collect(Collectors.toList());
            DwManualExecJob dwManualExecJob = new DwManualExecJob();
            dwManualExecJob.setProjectIds(projectIds);
            dwManualExecJob.setTenantId(dataList.get(0).getTenantId());

            transactionTemplate.execute(status -> {
                // 测试执行趋势图
                // 每次导入数据之前先清空表数据
                MtCaseExecTrendChart mtCaseExecTrendChart = new MtCaseExecTrendChart();
                mtCaseExecTrendChart.setCreatedTime(DateDealUtils.getCurrentDate());
                mtCaseExecTrendChart.setProjectIds(projectIds);
                mtCaseExecTrendChart.setTenantId(dwManualExecJob.getTenantId());
                mtCaseExecTrendChartService.deleteByExampleByProjectIds(mtCaseExecTrendChart);
                // 插入数据
                mtCaseExecTrendChartService.syncExecTendencyNotice(dwManualExecJob);

                return Boolean.TRUE;
            });
            transactionTemplate.execute(status->{
                //插入用例执行状态为未完成的数据
                // 每次导入数据之前先清空表数据
                MtCaseNonExec mtCaseNonExec = new MtCaseNonExec();
                mtCaseNonExec.setCreatedTime(DateDealUtils.getCurrentDate());
                mtCaseNonExec.setProjectIds(projectIds);
                mtCaseNonExec.setTenantId(dwManualExecJob.getTenantId());
                mtCaseNonExecService.deleteByExampleByProjectIds(mtCaseNonExec);
                // 插入数据
                mtCaseNonExecService.syncExecTendencyNotice(dwManualExecJob);

                return Boolean.TRUE;
            });
        }
    }


}
