package com.common.service;



import com.common.entity.Alarm;
import com.common.entity.AlarmVisit;
import com.common.mapper.AlarmMapper;
import com.common.mapper.AlarmVisitMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

@Slf4j
@Component
public class DataLoader {

    @Autowired
    private AlarmMapper alarmMapper;
    @Autowired
    private AlarmVisitMapper alarmVisitMapper;
    /**
     * 批量入库告警数据
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void batchInsertAlarms(List<Alarm> alarmList) {
        if (alarmList == null || alarmList.isEmpty()) {
            log.info("没有需要入库的告警类型数据");
            return;
        }

        int batchSize = 500; // 每批500条
        List<List<Alarm>> batches = partitionList(alarmList, batchSize);
        int totalSuccess = 0;

        for (int i = 0; i < batches.size(); i++) {
            List<Alarm> batch = batches.get(i);
            try {
                alarmMapper.batchInsert(batch);
                totalSuccess += batch.size();
                log.debug("第 {} 批数据入库成功，本批 {} 条", i + 1, batch.size());
            } catch (Exception e) {
                log.error("第 {} 批数据批量入库失败，尝试单条插入", i + 1, e);
                int singleSuccess = insertOneByOne(batch);
                totalSuccess += singleSuccess;
            }
        }

        log.info("告警数据入库完成，成功: {} 条，总数: {} 条", totalSuccess, alarmList.size());
    }


    /**
     * 批量入库访问日志告警数据
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void batchInsertAlarmVisits(List<AlarmVisit> alarmList) {
        if (alarmList == null || alarmList.isEmpty()) {
            log.info("没有需要入库的访问日志数据");
            return;
        }

        int batchSize = 500; // 每批500条
        List<List<AlarmVisit>> batches = partitionList(alarmList, batchSize);
        int totalSuccess = 0;

        for (int i = 0; i < batches.size(); i++) {
            List<AlarmVisit> batch = batches.get(i);
            try {
                alarmVisitMapper.batchInsert(batch);
                totalSuccess += batch.size();
                log.debug("第 {} 批数据入库成功，本批 {} 条", i + 1, batch.size());
            } catch (Exception e) {
                log.error("第 {} 批数据批量入库失败，尝试单条插入", i + 1, e);
                int singleSuccess = insertVisitOneByOne(batch);
                totalSuccess += singleSuccess;
            }
        }

        log.info("访问日志告警数据入库完成，成功: {} 条，总数: {} 条", totalSuccess, alarmList.size());
    }


    private int insertOneByOne(List<Alarm> alarmList) {
        int successCount = 0;
        for (Alarm alarm : alarmList) {
            try {
                alarmMapper.insert(alarm);
                successCount++;
            } catch (Exception e) {
                log.error("单条插入告警失败: {}", alarm.getId(), e);
            }
        }
        return successCount;
    }

    private int insertVisitOneByOne(List<AlarmVisit> alarmList) {
        int successCount = 0;
        for (AlarmVisit alarm : alarmList) {
            try {
                alarmVisitMapper.insert(alarm);
                successCount++;
            } catch (Exception e) {
                log.error("单条插入告警失败: {}", alarm.getId(), e);
            }
        }
        return successCount;
    }
    private <T> List<List<T>> partitionList(List<T> list, int size) {
        return IntStream.range(0, (list.size() + size - 1) / size)
                .mapToObj(i -> list.subList(i * size, Math.min((i + 1) * size, list.size())))
                .collect(Collectors.toList());
    }
}