package com;
import com.common.entity.DmColumn;
import com.common.entity.DmNormalizeRule;
import com.common.mapper.DmNormalizeRuleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import  com.kafka.kafkalogconsumer;

import com.common.entity.XdrHoneypot;
import com.common.mapper.XdrHoneypotMapper;
import com.common.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.mybatis.spring.annotation.MapperScan;
import com.Modules.NormalData.logNormalData;
import com.common.service.DmColumnService;

@MapperScan("com.common.mapper")
@EnableTransactionManagement  // 启用事务管理
@SpringBootApplication
public class syslogRuleApplication {

    private static final Logger logger = LoggerFactory.getLogger(syslogRuleApplication.class);
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static final Random random = new Random();
@Autowired
 private static DmColumnService dmColumnService;

    /**
     *
     * 应用程序主入口方法
     *
     * @param args 命令行参数
     */
    public static void main(String[] args) {

        // 启动Spring Boot应用
        SpringApplication.run(syslogRuleApplication.class, args);
        //insertSingleRecord();
        //testTransaction();
       // logNormalData  logData =new logNormalData( "");
        //List<Map<String, Object>> rulelst= logData.getRuleList(1);
      // kafkalogconsumer  logconsumer=new kafkalogconsumer();
       //logconsumer.Run();

    }

    public static void testTransaction() {

        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            DmNormalizeRuleMapper rulemapper = sqlSession.getMapper(DmNormalizeRuleMapper.class);
            List<Map<String, Object>>  rulelst= rulemapper.selectByDeviceId((long)1);
            DmNormalizeRule rule1 = rulemapper.selectById((long)1);
            sqlSession.commit();

            if (!rule1.equals(null)) {
                System.out.println("查询结果: " + rule1);
            } else {

            }
        } catch (Exception e) {
            System.out.println("异常消息: " + e.getMessage());
        }
        // DmNormalizeRule rule = dmNormalizeRuleService.getById(1L);

        /**测试更新（写事务）
         if (rule != null) {
         rule.setDescription("测试更新描述");
         boolean success = dmNormalizeRuleService.updateById(rule);
         System.out.println("更新结果: " + success);
         }
         **/
    }
    /**
     * 单条记录插入演示
     */
    private static void insertSingleRecord() {
        logger.info("=== 单条记录插入演示 ===");

        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            XdrHoneypotMapper Xdrmapper = sqlSession.getMapper(XdrHoneypotMapper.class);

            // 创建测试数据
            XdrHoneypot record = createTestXdrHoneypot(1);
            // 插入记录
            int result = Xdrmapper.insert(record);
            sqlSession.commit();

            if (result > 0) {
                logger.info("单条记录插入成功，ID: {}", record.getId());
                logger.info("插入的数据: {}", record);
            } else {
                logger.error("单条记录插入失败");
            }
        } catch (Exception e) {
            logger.error("单条记录插入出错", e);
        }
    }
    /**
     * 创建测试用的 XdrHoneypot 数据
     */
    private static XdrHoneypot createTestXdrHoneypot(int index) {
        String timestamp = LocalDateTime.now().format(formatter);

        return new XdrHoneypot(
                "vc-source-" + index,
                timestamp,
                timestamp,
                getRandomRiskLevel(),
                "connection-" + index,
                "file-info-" + index,
                "额外信息-" + index + " - 测试数据",
                "vctype-" + (index % 3 + 1),
                "agent-sn-" + index,
                "agent-name-" + index,
                "honeypot-id-" + index,
                "honeypot-name-" + index,
                "192.168." + (index % 255) + "." + (index % 255),
                String.valueOf(8000 + index),
                "00:1B:44:11:3A:B" + (index % 10),
                "10.0.0." + index,
                String.valueOf(80 + index),
                "proxy-" + index + ".example.com",
                "node-" + index
        );
    }
    /**
     * 获取随机风险等级
     */
    private static String getRandomRiskLevel() {
        String[] levels = {"LOW", "MEDIUM", "HIGH", "CRITICAL"};
        return levels[random.nextInt(levels.length)];
    }
}
