package com.common.service.impl;

import com.common.entity.AnalysisAnalysisRule;
import com.common.mapper.AnalysisAnalysisRuleMapper;
import com.common.service.AnalysisEngine;
import com.common.service.AnalysisRuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
/**
 * 分析规则服务实现
 */
@Slf4j
@Service
public class AnalysisRuleServiceImpl implements AnalysisRuleService {

    @Autowired
    private AnalysisAnalysisRuleMapper ruleMapper;

    @Autowired
    @Qualifier("realtimeAnalysisEngine")
    private AnalysisEngine realtimeAnalysisEngine;

    @Autowired
    @Qualifier("offlineAnalysisEngine")
    private AnalysisEngine offlineAnalysisEngine;

    @Override
    public List<Map<String, Object>> executeRealtimeAnalysis() {
        log.info("开始执行实时分析任务");
        List<AnalysisAnalysisRule> rules = getActiveRules("realtime");
        log.info("查询到 {} 条实时分析规则", rules.size());
        return realtimeAnalysisEngine.executeRules(rules);
    }

    @Override
    public List<Map<String, Object>> executeOfflineAnalysis() {
        log.info("开始执行离线分析任务");
        List<AnalysisAnalysisRule> rules = getActiveRules("offline");
        log.info("查询到 {} 条离线分析规则", rules.size());
        return offlineAnalysisEngine.executeRules(rules);
    }

    @Override
    public List<AnalysisAnalysisRule> getActiveRules(String runMode) {
        return ruleMapper.selectActiveRulesByRunMode(runMode);
    }

    @Override
    public void stopRule(String ruleId) {
        // 查询规则确定运行模式
        AnalysisAnalysisRule rule = ruleMapper.selectByRuleId(ruleId);
        if (rule != null) {
            String runMode = rule.getRunMode();
            if ("realtime".equalsIgnoreCase(runMode)) {
                realtimeAnalysisEngine.stopRule(ruleId);
            } else if ("offline".equalsIgnoreCase(runMode)) {
                offlineAnalysisEngine.stopRule(ruleId);
            }
        }
    }



    @Override
    public Map<String, Object> executeRealtimeRule(String ruleId) {
        log.info("执行实时分析规则: ruleId={}", ruleId);

        // 查询规则
        AnalysisAnalysisRule rule = ruleMapper.selectByRuleId(ruleId);
        if (rule == null) {
            log.warn("规则不存在: ruleId={}", ruleId);
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "规则不存在");
            return result;
        }

        if (!"realtime".equalsIgnoreCase(rule.getRunMode())) {
            log.warn("规则不是实时分析模式: ruleId={}, runMode={}", ruleId, rule.getRunMode());
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "规则不是实时分析模式");
            return result;
        }

        try {
            // 执行单个规则
            List<AnalysisAnalysisRule> ruleList = new ArrayList<>();
            ruleList.add(rule);
            List<Map<String, Object>> results = realtimeAnalysisEngine.executeRules(ruleList);

            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("ruleId", ruleId);
            result.put("ruleName", rule.getRuleName());
            result.put("resultCount", results.size());
            result.put("results", results);
            return result;
        } catch (Exception e) {
            log.error("执行实时分析规则失败: ruleId={}", ruleId, e);
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("ruleId", ruleId);
            result.put("message", e.getMessage());
            return result;
        }
    }
}
