package net.northking.cctp.dw.bus;

import net.northking.cctp.dw.api.service.MtTestPointApiService;
import net.northking.cctp.dw.constant.DWConstant;
import net.northking.cctp.dw.db.entity.DwTestPoint;
import net.northking.cctp.dw.db.entity.MtTestPointStatistics;
import net.northking.cctp.dw.db.entity.PointCompleteDetails;
import net.northking.cctp.dw.db.service.MtTestPointStatisticsService;
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 TestPointDataListener {

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

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TransactionTemplate transactionTemplate;

    @Autowired
    private MtTestPointApiService mtTestPointApiService;

    @Autowired
    private MtTestPointStatisticsService mtTestPointStatisticsService;


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

                // 每次导入数据之前先清空表数据
                PointCompleteDetails pointCompleteDetails = new PointCompleteDetails();
                pointCompleteDetails.setProjectIds(projectIds);
                mtTestPointApiService.deleteAllCompleteByProjectIds(pointCompleteDetails);

                // 插入数据
                mtTestPointApiService.testPointComplete(dwTestPoint);

                return Boolean.TRUE;
            });
        }
    }

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

                // 每次导入数据之前先清空表数据
                MtTestPointStatistics mtTestPointStatistics = new MtTestPointStatistics();
                mtTestPointStatistics.setCreatedTime(DateDealUtils.getCurrentDate());
                mtTestPointStatistics.setProjectIds(projectIds);
                mtTestPointStatisticsService.deleteByExampleByProjectIds(mtTestPointStatistics);

                // 再插入数据
                mtTestPointStatisticsService.syncPointNotice(dwTestPoint);


                return Boolean.TRUE;
            });
        }
    }

}
