package net.northking.cctp.dw.schedule;

import net.northking.cctp.dw.constant.DWConstant;
import net.northking.cctp.dw.db.entity.DwTestPoint;
import net.northking.cctp.dw.db.service.DwTestPointService;
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 MtManualTestPointProducer {

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

    @Autowired
    private RedisLockRegistry redisLockRegistry;

    @Autowired
    private DwTestPointService dwTestPointService;

    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 需求分析完成情况
     */
    @Scheduled(fixedDelayString = "${nk.scheduled2:120000}")
    public void testPointCompleteProducer() {
        //  加锁解决集群后多次触发问题
        Lock lock = redisLockRegistry.obtain("test-point-complete-producer");
        boolean b = lock.tryLock();
        try {
            if (b) {
                //从dw_test_point表中提获取系统id和租户id
                List<DwTestPoint> dwTestPointList = dwTestPointService.findProIdList();
                if (!dwTestPointList.isEmpty()) {
                    Map<String, List<DwTestPoint>> map = dwTestPointList.stream().collect(Collectors.groupingBy(DwTestPoint::getTenantId));
                    Iterator<Map.Entry<String, List<DwTestPoint>>> iterator = map.entrySet().iterator();
                    while (iterator.hasNext()) {
                        Map.Entry<String, List<DwTestPoint>> next = iterator.next();
                        List<DwTestPoint> 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<DwTestPoint> list = jobList.subList(i * count, stop);
                            // 发送消息
                            amqpTemplate.convertAndSend(DWConstant.MQ_HANDLE_DATA_EXCHANGE, DWConstant.BugRoutingKey.TEST_POINT_COMPLETE_PRODUCER_DATA, list);
                        }
                    }
                }
            }
        } catch (Exception e) {
            logger.info(e.getMessage());
        } finally {
            if (b) {
                lock.unlock();
            }
        }
    }

    /**
     * 需求分析统计图
     */
    @Scheduled(fixedDelayString = "${nk.scheduled2:120000}")
    public void testPointStatisticsProducer() {
        //  加锁解决集群后多次触发问题
        Lock lock = redisLockRegistry.obtain("test-point-statistics-producer");
        boolean b = lock.tryLock();
        try {
            if (b) {
                //从dw_test_point表中提获取系统id和租户id
                List<DwTestPoint> dwTestPointList = dwTestPointService.findProIdList();
                if (!dwTestPointList.isEmpty()) {
                    Map<String, List<DwTestPoint>> map = dwTestPointList.stream().collect(Collectors.groupingBy(DwTestPoint::getTenantId));
                    Iterator<Map.Entry<String, List<DwTestPoint>>> iterator = map.entrySet().iterator();
                    while (iterator.hasNext()) {
                        Map.Entry<String, List<DwTestPoint>> next = iterator.next();
                        List<DwTestPoint> 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<DwTestPoint> list = jobList.subList(i * count, stop);
                            // 发送消息
                            amqpTemplate.convertAndSend(DWConstant.MQ_HANDLE_DATA_EXCHANGE, DWConstant.BugRoutingKey.TEST_POINT_STATISTICS_PRODUCER_DATA, list);
                        }
                    }
                }
            }
        } catch (Exception e) {
            logger.info(e.getMessage());
        } finally {
            if (b) {
                lock.unlock();
            }
        }
    }

}
