package net.northking.cctp.dw.schedule;

import net.northking.cctp.dw.api.service.MtCaseDesignApiService;
import net.northking.cctp.dw.constant.DWConstant;
import net.northking.cctp.dw.dto.DwCaseDesignInfoDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.stream.Collectors;


//@Component
public class MtManualCaseDesginProducer {

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

    @Autowired
    private RedisLockRegistry redisLockRegistry;

    @Autowired
    private MtCaseDesignApiService mtCaseDesignApiService;

    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 此队列用于处理用例设计进度图和用例设计统计图
     */
    @Scheduled(fixedDelayString = "${nk.scheduled2:120000}")
    public void caseDesignCompleteProducer() {
        Lock lock = redisLockRegistry.obtain("case-design-complete-producer");
        boolean b = lock.tryLock();
        try {
            if (b) {
                // 从dw_case_design_info表获取系统id和租户id，循环处理
                List<DwCaseDesignInfoDto> dwCaseDesignInfoDtoList = mtCaseDesignApiService.getProjectId();
                // 根据租户id分组
                Map<String, List<DwCaseDesignInfoDto>> map = dwCaseDesignInfoDtoList.stream().collect(Collectors.groupingBy(DwCaseDesignInfoDto::getTenantId));
                Iterator<Map.Entry<String, List<DwCaseDesignInfoDto>>> iterator = map.entrySet().iterator();
                while (iterator.hasNext()) {
                    Map.Entry<String, List<DwCaseDesignInfoDto>> next = iterator.next();
                    List<DwCaseDesignInfoDto> jobList = next.getValue();
                    // 每20个系统作为一个队列
                    int count = 20;
                    Integer num = jobList.size();
                    for (int i = 0; i < num / count + 1; i++) {
                        if (i * count == num) {
                            break;
                        }
                        int stop = (i + 1) * count > num ? num : (i + 1) * count;
                        List<DwCaseDesignInfoDto> list = jobList.subList(i * count, stop);
                        // 发送消息
                        amqpTemplate.convertAndSend(DWConstant.MQ_HANDLE_DATA_EXCHANGE, DWConstant.BugRoutingKey.CASE_DESGIN_COMPLETE_PRODUCER_DATA, list);
                    }
                }
            }
        } catch (Exception e) {
            logger.info(e.getMessage());
        } finally {
            if (b) {
                lock.unlock();
            }
        }
    }


    @Scheduled(fixedDelayString = "${nk.scheduled1:60000}")
    public void caseDesignChartProducer() {
        Lock lock = redisLockRegistry.obtain("case-design-chart-producer");
        boolean b = lock.tryLock();
        try {
            if (b) {
                // 从dw_case_design_info表获取系统id和租户id，循环处理
                List<DwCaseDesignInfoDto> dwCaseDesignInfoDtoList = mtCaseDesignApiService.getProjectId();
                // 根据租户id分组
                Map<String, List<DwCaseDesignInfoDto>> map = dwCaseDesignInfoDtoList.stream().collect(Collectors.groupingBy(DwCaseDesignInfoDto::getTenantId));
                Iterator<Map.Entry<String, List<DwCaseDesignInfoDto>>> iterator = map.entrySet().iterator();
                while (iterator.hasNext()) {
                    Map.Entry<String, List<DwCaseDesignInfoDto>> next = iterator.next();
                    List<DwCaseDesignInfoDto> jobList = next.getValue();
                    // 每20个系统作为一个队列
                    int count = 20;
                    Integer num = jobList.size();
                    for (int i = 0; i < num / count + 1; i++) {
                        if (i * count == num) {
                            break;
                        }
                        int stop = (i + 1) * count > num ? num : (i + 1) * count;
                        List<DwCaseDesignInfoDto> list = jobList.subList(i * count, stop);
                        // 发送消息
                        amqpTemplate.convertAndSend(DWConstant.MQ_HANDLE_DATA_EXCHANGE, DWConstant.BugRoutingKey.CASE_DESGIN_CHART_PRODUCER_DATA, list);
                    }
                }
            }
        } catch (Exception e) {
            logger.info(e.getMessage());
        } finally {
            if (b) {
                lock.unlock();
            }
        }
    }

}
