// DeviceTask.java - 原有设备任务
package cn.iocoder.yudao.module.iot.controller.admin.device.scheduled.coretask;

import cn.iocoder.yudao.framework.common.enums.DeviceConnectionStatusEnum;
import cn.iocoder.yudao.framework.common.util.opc.OpcUtils;
import cn.iocoder.yudao.module.iot.controller.admin.device.enums.DeviceBasicStatusEnum;
import cn.iocoder.yudao.module.iot.controller.admin.device.enums.DeviceStatusEnum;
import cn.iocoder.yudao.module.iot.controller.admin.device.enums.TaskTypeEnum;
import cn.iocoder.yudao.module.iot.controller.admin.device.scheduled.core.Task;
import cn.iocoder.yudao.module.iot.controller.admin.device.scheduled.scheduler.TaskSchedulerManager;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.DeviceSaveReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.devicemodelrules.vo.PointRulesRespVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.device.DeviceDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.devicecontactmodel.DeviceContactModelDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.deviceoperationrecord.DeviceOperationRecordDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.devicepointrules.DevicePointRulesDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.devicewarinningrecord.DeviceWarinningRecordDO;
import cn.iocoder.yudao.module.iot.dal.mysql.device.DeviceMapper;
import cn.iocoder.yudao.module.iot.dal.mysql.devicecontactmodel.DeviceContactModelMapper;
import cn.iocoder.yudao.module.iot.dal.mysql.deviceoperationrecord.DeviceOperationRecordMapper;
import cn.iocoder.yudao.module.iot.dal.mysql.devicepointrules.DevicePointRulesMapper;
import cn.iocoder.yudao.module.iot.dal.mysql.devicewarinningrecord.DeviceWarinningRecordMapper;
import cn.iocoder.yudao.module.iot.service.device.DeviceService;
import cn.iocoder.yudao.module.iot.service.device.TDengineService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class DeviceTask implements Task {
    private static final Logger logger = LoggerFactory.getLogger(DeviceTask.class);

    @Resource
    private DeviceContactModelMapper deviceContactModelMapper;

    @Resource
    private DeviceMapper deviceMapper;

    @Resource
    private TDengineService tDengineService;

    @Resource
    @Lazy
    private DeviceService deviceService;

    @Resource
    private DevicePointRulesMapper devicePointRulesMapper;

    @Resource
    private DeviceOperationRecordMapper deviceOperationRecordMapper;

    @Resource
    private DeviceWarinningRecordMapper deviceWarinningRecordMapper;

    @Resource
    @Lazy
    private TaskSchedulerManager taskSchedulerManager;

    @Override
    public String getTaskType() {
        return TaskTypeEnum.DEVICE.getCode();
    }


//    static {
//        // 定期清理过期连接
//        ScheduledExecutorService cleanupExecutor = Executors.newSingleThreadScheduledExecutor();
//        cleanupExecutor.scheduleAtFixedRate(DeviceTask::cleanupExpiredConnections,
//                CLEANUP_INTERVAL, CLEANUP_INTERVAL, TimeUnit.MILLISECONDS);
//    }

//    // 会话信息
//    private static class SessionInfo {
//        long lastUsedTime;
//        String deviceUrl;
//        String username;
//
//        SessionInfo(String deviceUrl, String username) {
//            this.deviceUrl = deviceUrl;
//            this.username = username;
//            this.lastUsedTime = System.currentTimeMillis();
//        }
//
//        boolean isExpired() {
//            return System.currentTimeMillis() - lastUsedTime > CONNECTION_TIMEOUT;
//        }
//
//        void updateLastUsed() {
//            this.lastUsedTime = System.currentTimeMillis();
//        }
//    }


    @Override
    public void execute(Long taskId, String taskParam) {
        Long deviceId = taskId - 1000000L;

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String currentTime = sdf.format(new Date());

            logger.info("执行设备任务，任务ID: {}, 参数: {}, 时间: {}",
                    taskId, taskParam, currentTime);

            executeDeviceLogic(taskId, taskParam);


        } catch (Exception e) {
            logger.error("执行设备任务异常，任务ID: {}", taskId, e);
        }
//        finally {
//            //确保出问题不会打满opcv服务器连接数
//            OpcUtils.disconnect();
//        }
    }

    /**
     * 具体的设备执行逻辑
     */
    private void executeDeviceLogic(Long sourceDeviceId, String param) {
        logger.info("执行设备逻辑，源设备ID: {}，参数: {}", sourceDeviceId, param);

        // 1. 计算实际设备ID
        Long deviceId = sourceDeviceId - 1000000L;
        logger.info("处理后设备ID: {}", deviceId);

        // 2. 获取设备信息
        DeviceDO device = getDeviceInfo(deviceId);

        // 3. 连接OPC服务器
        connectOpcServer(device);

        // 4. 处理数据读取和入库
        processDeviceData(deviceId, device);

        // 5. 断开连接
//        OpcUtils.disconnect();
    }


    /**
     * 获取设备信息
     */
    private DeviceDO getDeviceInfo(Long deviceId) {
        DeviceDO device = deviceMapper.selectById(deviceId);

        if (device == null) {
            throw new RuntimeException("设备不存在，ID: " + deviceId);
        }

        if (StringUtils.isBlank(device.getUrl())) {
            throw new RuntimeException("设备URL不能为空，ID: " + deviceId);
        }

        return device;
    }

    /**
     * 连接OPC服务器
     */
    private void connectOpcServer(DeviceDO device) {
        String username = StringUtils.defaultString(device.getUsername());
        String password = StringUtils.defaultString(device.getPassword());

        boolean connected = false;


        try {
            connected = OpcUtils.connect(
                    device.getId(),
                    device.getUrl(),
                    username,
                    password,
                    10
            );
            if (!connected) {

                log.error("设备 {} 连接OPC服务器失败，URL: {}", device.getId(), device.getUrl());
                device.setStatus(String.valueOf(DeviceConnectionStatusEnum.DISCONNECTED.getStatus()));
                deviceMapper.updateById(device);
                taskSchedulerManager.stopDeviceTask(device.getId());
                DeviceOperationRecordDO deviceOperationRecordDO = new DeviceOperationRecordDO();
                deviceOperationRecordDO.setDeviceId(device.getId());
                deviceOperationRecordDO.setRule(DeviceStatusEnum.OFFLINE.getCode());
                //TODO 默认内置管理员
                deviceOperationRecordDO.setCreator("1");
                deviceOperationRecordDO.setUpdater("1");
                deviceOperationRecordMapper.insert(deviceOperationRecordDO);
                //抛出异常终止任务
                throw new RuntimeException("连接opcuv服务器异常");

            }

            log.info("设备 {} 成功连接OPC服务器，URL: {}", device.getId(), device.getUrl());

        } catch (Exception e) {
            log.error("设备 {} 连接OPC服务器异常，URL: {}", device.getId(), device.getUrl(), e);
            device.setStatus(String.valueOf(DeviceConnectionStatusEnum.DISCONNECTED.getStatus()));
            deviceMapper.updateById(device);
            taskSchedulerManager.stopDeviceTask(device.getId());
            DeviceOperationRecordDO deviceOperationRecordDO = new DeviceOperationRecordDO();
            deviceOperationRecordDO.setDeviceId(device.getId());
            deviceOperationRecordDO.setRule(DeviceStatusEnum.OFFLINE.getCode());
            //TODO 默认内置管理员
            deviceOperationRecordDO.setCreator("1");
            deviceOperationRecordDO.setUpdater("1");
            deviceOperationRecordMapper.insert(deviceOperationRecordDO);
            //抛出异常终止任务
            throw new RuntimeException(e);
        }


    }

    /**
     * 处理设备数据 - 安全批量读取 OPC UA 点位
     */
    private void processDeviceData(Long deviceId, DeviceDO device) {
        // 1. 查询点位配置
        List<DeviceContactModelDO> points = getDevicePoints(deviceId);
        if (CollectionUtils.isEmpty(points)) {
            logger.warn("设备 {} 未配置点位", deviceId);
            // 更新状态为待机中
            DeviceDO deviceDO = deviceMapper.selectById(deviceId);
            DeviceOperationRecordDO record = new DeviceOperationRecordDO();
            record.setDeviceId(deviceId);
            record.setRule(DeviceStatusEnum.STANDBY.getCode());
            record.setTotalStandbyTime(deviceDO.getSampleCycle());
            record.setCreator("1");
            record.setUpdater("1");
            deviceOperationRecordMapper.insert(record);
            return;
        }

        logger.info("设备 {} 需要读取 {} 个点位", deviceId, points.size());

        // 2. 构建有效地址列表及索引映射，确保顺序对应
        List<String> addresses = new ArrayList<>();
        List<Integer> indexMap = new ArrayList<>(); // 对应原 points 的索引
        for (int i = 0; i < points.size(); i++) {
            String address = StringUtils.trimToEmpty(points.get(i).getAddress());
            if (!address.isEmpty()) {
                addresses.add(address);
                indexMap.add(i);
            }
        }

        // 3. 批量读取 OPC UA 点位
        List<Object> values = new ArrayList<>();
        try {
            if (!addresses.isEmpty()) {
                values = OpcUtils.readValues(device.getId(), addresses);
            }
        } catch (Exception e) {
            logger.error("设备 {} 批量读取 OPC UA 点位异常", deviceId, e);
        }

        // 4. 处理读取到的数据
        int successCount = 0;
        List<DeviceContactModelDO> validDataList = new ArrayList<>();
        for (int i = 0; i < values.size(); i++) {
            DeviceContactModelDO point = points.get(indexMap.get(i));
            Object value = values.get(i);

            try {
                String processedValue = processOpcValue(value);

                // 规则判断和告警/运行记录处理
                judgmentRules(processedValue, point.getAttributeCode(), device, point.getId());

                point.setAddressValue(processedValue.isEmpty() ? null : processedValue);

                if (point.getAddressValue() != null) {
                    successCount++;
                }
                validDataList.add(point);

            } catch (Exception e) {
                logger.error("处理点位 {} 异常，地址: {}", point.getId(), point.getAddress(), e);
            }
        }

        // 5. 入库处理
        if (!validDataList.isEmpty()) {
            saveToDatabase(deviceId, validDataList, successCount);
        } else {
            logger.warn("设备 {} 未读取到有效数据", deviceId);
        }
    }


    /**
     * 获取设备点位
     */
    private List<DeviceContactModelDO> getDevicePoints(Long deviceId) {
        LambdaQueryWrapper<DeviceContactModelDO> query = new LambdaQueryWrapper<>();
        query.eq(DeviceContactModelDO::getDeviceId, deviceId);
        return deviceContactModelMapper.selectList(query);
    }

//    /**
//     * 处理单个点位
//     */
//    private void processSinglePoint(DeviceContactModelDO point, List<DeviceContactModelDO> validDataList, DeviceDO device) {
//        try {
//            String address = StringUtils.trimToEmpty(point.getAddress());
//            if (address.isEmpty()) {
//                logger.warn("点位ID {} 地址为空", point.getId());
//                return;
//            }
//
//            Object value = OpcUtils.readValue(device.getId(),address);
////            if (value == null) {
//            logger.info("读取点位 {} ，地址: {}", point.getId(), address);
////            } else {
//            String processedValue = processOpcValue(value);
//
//            //判断规则
//            judgmentRules(processedValue,point.getAttributeCode(),device,point.getId());
//            point.setAddressValue(processedValue.isEmpty() ? null : processedValue);
//
////            }
//
//            validDataList.add(point);
//        } catch (Exception e) {
//            logger.error("处理点位 {} 异常，地址: {}",
//                    point.getId(), point.getAddress(), e);
//        }
//    }


    /**
     * 处理OPC值
     */
    private String processOpcValue(Object value) {
        if (value == null) {
            return "";
        }

        if (value instanceof String) {
            return ((String) value).trim();
        }

        return value.toString();
    }

    /**
     * 保存到数据库
     */
    private void saveToDatabase(Long deviceId, List<DeviceContactModelDO> dataList, int successCount) {
        try {
            for (DeviceContactModelDO deviceContactModelDO : dataList) {
                deviceContactModelDO.setAddress(null);
            }
            String json = JSON.toJSONString(dataList);
            boolean inserted = tDengineService.insertDeviceData(deviceId, json);

            if (inserted) {
                logger.info("设备 {} 数据入库成功，总数: {}，有效: {}",
                        deviceId, dataList.size(), successCount);
            } else {
                logger.error("设备 {} 数据入库失败", deviceId);
            }
        } catch (Exception e) {
            logger.error("设备 {} 数据入库异常", deviceId, e);
        }
    }

    private void judgmentRules(String processedValue, String attributeCode, DeviceDO device, Long modelId) {
        if (StringUtils.isBlank(processedValue)) {
            logger.warn("待判断的值为空，编码attributeCode: {}, deviceId: {}", attributeCode, device.getId());
//            return;
        }

        // 1. 查询设备规则
        List<DevicePointRulesDO> devicePointRulesDOList = devicePointRulesMapper.selectList(
                Wrappers.<DevicePointRulesDO>lambdaQuery()
                        .eq(DevicePointRulesDO::getDeviceId, device.getId()).orderByDesc(DevicePointRulesDO::getCreateTime));

        if (CollectionUtils.isEmpty(devicePointRulesDOList)) {
            logger.debug("设备 {} 未配置规则", device.getId());
            return;
        }

        // 2. 遍历规则
        for (DevicePointRulesDO devicePointRulesDO : devicePointRulesDOList) {
            if (StringUtils.isBlank(devicePointRulesDO.getFieldRule())) {
                continue;
            }

            // 3. 解析规则列表
            List<PointRulesRespVO> pointRulesVOList = JSON.parseArray(
                    devicePointRulesDO.getFieldRule(), PointRulesRespVO.class);

            if (CollectionUtils.isEmpty(pointRulesVOList)) {
                continue;
            }

            // 4. 找到对应modelId的规则并进行判断
            for (PointRulesRespVO pointRulesRespVO : pointRulesVOList) {
                if (pointRulesRespVO.getCode() != null &&
                        pointRulesRespVO.getCode().equals(attributeCode)) {

                    boolean matched = matchRule(processedValue, pointRulesRespVO);

                    if (matched) {
                        logger.info("规则匹配成功: modelId={}, value={}, rule={}",
                                attributeCode, processedValue,
                                JSON.toJSONString(pointRulesRespVO));

                        // 执行匹配成功后的逻辑
                        handleMatchedSuccessRule(devicePointRulesDO, pointRulesRespVO, processedValue, device, attributeCode, modelId);
                        break;
                    } else {
                        logger.debug("规则不匹配: modelId={}, value={}, rule={}",
                                attributeCode, processedValue,
                                JSON.toJSONString(pointRulesRespVO));
                        // 执行匹配失败后的逻辑
                        handleMatchedFailureRule(devicePointRulesDO, pointRulesRespVO, processedValue, device, attributeCode);

                    }
                }
            }
        }
    }

    private void handleMatchedFailureRule(DevicePointRulesDO devicePointRulesDO, PointRulesRespVO pointRulesRespVO, String processedValue, DeviceDO device, String attributeCode) {
        //TODO 离线待优化
//        if (devicePointRulesDO.getIdentifier().equals(DeviceBasicStatusEnum.RUNNING.getCode())){
//            DeviceOperationRecordDO record = new DeviceOperationRecordDO();
//            record.setDeviceId(device.getId());
//            record.setModelId(modelId);
//            record.setRule(pointRulesRespVO.getRule());
//            record.setAddressValue(processedValue);
//            record.setRecordType(getRecordType(devicePointRulesDO));
//            record.setRuleId(devicePointRulesDO.getId());
//
//        }


    }

    private void handleMatchedSuccessRule(DevicePointRulesDO devicePointRulesDO,
                                          PointRulesRespVO pointRulesRespVO,
                                          String processedValue,
                                          DeviceDO device,
                                          String attributeCode,
                                          Long modelId) {
        DeviceContactModelDO deviceContactModelDO = deviceContactModelMapper.selectById(modelId);
        if (deviceContactModelDO == null) {
            return;
        }


        //分别处理运行记录和告警记录
        if (StringUtils.isBlank(devicePointRulesDO.getAlarmLevel())) {
            DeviceOperationRecordDO record = new DeviceOperationRecordDO();
            record.setDeviceId(device.getId());
            record.setModelId(modelId);
            record.setRule(pointRulesRespVO.getRule());
            record.setAddressValue(processedValue);
            record.setRecordType(getRecordType(devicePointRulesDO));
            record.setRuleId(devicePointRulesDO.getId());
            //TODO 创建人和更新人为内置默认管理员
            record.setCreator("1");
            record.setUpdater("1");

            // 处理累计时间
            calculateAndSetTotalTime(record, pointRulesRespVO.getRule(), device.getSampleCycle());

            deviceOperationRecordMapper.insert(record);
        } else {

            DeviceWarinningRecordDO deviceWarinningRecordDO = new DeviceWarinningRecordDO();
            deviceWarinningRecordDO.setDeviceId(device.getId());
            deviceWarinningRecordDO.setModelId(modelId);
            deviceWarinningRecordDO.setRule(pointRulesRespVO.getRule());
            deviceWarinningRecordDO.setAlarmLevel(devicePointRulesDO.getAlarmLevel());
            deviceWarinningRecordDO.setAddressValue(processedValue);
            deviceWarinningRecordDO.setRuleId(devicePointRulesDO.getId());
            deviceWarinningRecordDO.setDeviceName(device.getDeviceName());
            deviceWarinningRecordDO.setModelName(deviceContactModelDO.getAttributeName());
            deviceWarinningRecordDO.setRuleName(devicePointRulesDO.getFieldName());
            //TODO 创建人和更新人为内置默认管理员
            deviceWarinningRecordDO.setCreator("1");
            deviceWarinningRecordDO.setUpdater("1");
            deviceWarinningRecordMapper.insert(deviceWarinningRecordDO);
        }
    }

    private void calculateAndSetTotalTime(DeviceOperationRecordDO record, String ruleCode, Double sampleCycle) {
        if (!isTimeRelatedStatus(ruleCode)) {
            return;
        }

//        DeviceOperationRecordDO lastRecord = deviceOperationRecordMapper.selectOne(
//                Wrappers.<DeviceOperationRecordDO>lambdaQuery()
//                        .eq(DeviceOperationRecordDO::getRule, ruleCode)
//                        .orderByDesc(DeviceOperationRecordDO::getCreateTime)
//                        .last("LIMIT 1")
//        );

        if (ruleCode.equals(DeviceStatusEnum.RUNNING.getCode())) {
//            Double totalTime = (lastRecord != null && lastRecord.getTotalRunningTime() != null)
//                    ? lastRecord.getTotalRunningTime() + sampleCycle
//                    : sampleCycle;
            record.setTotalRunningTime(sampleCycle);

        } else if (ruleCode.equals(DeviceStatusEnum.STANDBY.getCode())) {
//            Double totalTime = (lastRecord != null && lastRecord.getTotalStandbyTime() != null)
//                    ? lastRecord.getTotalStandbyTime() + sampleCycle
//                    : sampleCycle;
            record.setTotalStandbyTime(sampleCycle);

        } else if (ruleCode.equals(DeviceStatusEnum.FAULT_STANDBY.getCode())) {
//            Double totalTime = (lastRecord != null && lastRecord.getTotalFaultTime() != null)
//                    ? lastRecord.getTotalFaultTime() + sampleCycle
//                    : sampleCycle;
            record.setTotalFaultTime(sampleCycle);

        } else if (ruleCode.equals(DeviceStatusEnum.ALARM_RUNNING.getCode())) {
//            Double totalTime = (lastRecord != null && lastRecord.getTotalWarningTime() != null)
//                    ? lastRecord.getTotalWarningTime() + sampleCycle
//                    : sampleCycle;
            record.setTotalWarningTime(sampleCycle);
        }
    }

    private Integer getRecordType(DevicePointRulesDO devicePointRulesDO) {
        return devicePointRulesDO.getIdentifier()
                .equals(DeviceBasicStatusEnum.RUNNING.getDescription())
                ? Integer.parseInt(DeviceBasicStatusEnum.RUNNING.getCode())
                : Integer.parseInt(DeviceBasicStatusEnum.ALARM.getCode());
    }

    private boolean isTimeRelatedStatus(String ruleCode) {
        return ruleCode.equals(DeviceStatusEnum.RUNNING.getCode()) ||
                ruleCode.equals(DeviceStatusEnum.STANDBY.getCode()) ||
                ruleCode.equals(DeviceStatusEnum.FAULT_STANDBY.getCode()) ||
                ruleCode.equals(DeviceStatusEnum.ALARM_RUNNING.getCode());
    }


    /**
     * 判断值是否符合规则
     * 支持操作符: EQ(等于), NE(不等于), GT(大于), GE(大于等于),
     * LT(小于), LE(小于等于), TRUE(为真), FALSE(为假)
     */
    private boolean matchRule(String value, PointRulesRespVO rule) {
        if (StringUtils.isBlank(value) || rule == null ||
                StringUtils.isBlank(rule.getOperator())) {
            return false;
        }

        try {
            String operator = rule.getOperator().toUpperCase();
            String inputValue = value.trim().toLowerCase();
            String ruleValue = StringUtils.trimToEmpty(rule.getOperatorRule());

            // 1. 处理布尔值判断
            if ("TRUE".equals(operator) || "FALSE".equals(operator)) {
                return matchBooleanRule(inputValue, operator);
            }

            // 2. 如果operatorRule为空，且不是布尔操作符，则返回false
            if (StringUtils.isBlank(ruleValue)) {
                logger.warn("规则比较值为空，但操作符不是布尔类型: operator={}", operator);
                return false;
            }

            ruleValue = ruleValue.trim();

            // 3. 尝试数值比较
            if (isNumeric(inputValue) && isNumeric(ruleValue)) {
                Double num1 = Double.parseDouble(inputValue);
                Double num2 = Double.parseDouble(ruleValue);

                return compareNumbers(num1, num2, operator);
            }
            // 4. 字符串比较
            else {
                return compareStrings(inputValue, ruleValue, operator);
            }

        } catch (Exception e) {
            logger.error("规则匹配异常: value={}, rule={}, error={}",
                    value, JSON.toJSONString(rule), e.getMessage());
            return false;
        }
    }

    /**
     * 处理布尔值判断
     */
    private boolean matchBooleanRule(String value, String operator) {
        // 常见布尔值表示
        boolean booleanValue = parseBoolean(value);

        if ("TRUE".equals(operator)) {
            return booleanValue;
        } else if ("FALSE".equals(operator)) {
            return !booleanValue;
        }

        return false;
    }

    /**
     * 解析字符串为布尔值
     * 支持: true, false, 1, 0, yes, no, on, off等
     */
    private boolean parseBoolean(String value) {
        if (StringUtils.isBlank(value)) {
            return false;
        }

        String lowerValue = value.toLowerCase();

        // 常见真值表示
        if ("true".equals(lowerValue) ||
                "1".equals(lowerValue) ||
                "yes".equals(lowerValue) ||
                "on".equals(lowerValue) ||
                "是".equals(lowerValue) ||  // 中文支持
                "成功".equals(lowerValue)) {
            return true;
        }

        // 常见假值表示
        if ("false".equals(lowerValue) ||
                "0".equals(lowerValue) ||
                "no".equals(lowerValue) ||
                "off".equals(lowerValue) ||
                "否".equals(lowerValue) ||  // 中文支持
                "失败".equals(lowerValue)) {
            return false;
        }

        // 尝试转换为布尔值
        try {
            return Boolean.parseBoolean(lowerValue);
        } catch (Exception e) {
            logger.warn("无法解析为布尔值: {}", value);
            return false;
        }
    }

    /**
     * 数值比较
     */
    private boolean compareNumbers(Double value, Double ruleValue, String operator) {
        switch (operator) {
            case "EQ":
                return Math.abs(value - ruleValue) < 0.000001; // 处理浮点数精度
            case "NE":
                return Math.abs(value - ruleValue) >= 0.000001;
            case "GT":
                return value > ruleValue;
            case "GE":
                return value >= ruleValue;
            case "LT":
                return value < ruleValue;
            case "LE":
                return value <= ruleValue;
            default:
                logger.warn("不支持的操作符: {}", operator);
                return false;
        }
    }

    /**
     * 字符串比较
     */
    private boolean compareStrings(String value, String ruleValue, String operator) {
        switch (operator) {
            case "EQ":
                return value.equals(ruleValue);
            case "NE":
                return !value.equals(ruleValue);
            case "GT":
                return value.compareTo(ruleValue) > 0;
            case "GE":
                return value.compareTo(ruleValue) >= 0;
            case "LT":
                return value.compareTo(ruleValue) < 0;
            case "LE":
                return value.compareTo(ruleValue) <= 0;
            default:
                logger.warn("不支持的操作符: {}", operator);
                return false;
        }
    }

    /**
     * 判断字符串是否为数字
     */
    private boolean isNumeric(String str) {
        if (StringUtils.isBlank(str)) {
            return false;
        }
        try {
            Double.parseDouble(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }


}