package com.qqchen.deploy.backend.deploy.service.impl;

import com.qqchen.deploy.backend.deploy.dto.ServerAlertRuleDTO;
import com.qqchen.deploy.backend.deploy.entity.ServerAlertRule;
import com.qqchen.deploy.backend.deploy.query.ServerAlertRuleQuery;
import com.qqchen.deploy.backend.deploy.repository.IServerAlertRuleRepository;
import com.qqchen.deploy.backend.deploy.service.IServerAlertRuleService;
import com.qqchen.deploy.backend.framework.annotation.ServiceType;
import com.qqchen.deploy.backend.framework.enums.ResponseCode;
import com.qqchen.deploy.backend.framework.exception.BusinessException;
import com.qqchen.deploy.backend.framework.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 服务器告警规则服务实现
 */
@Slf4j
@Service
@ServiceType(ServiceType.Type.DATABASE)
public class ServerAlertRuleServiceImpl
    extends BaseServiceImpl<ServerAlertRule, ServerAlertRuleDTO, ServerAlertRuleQuery, Long>
    implements IServerAlertRuleService {

    private final IServerAlertRuleRepository alertRuleRepository;

    public ServerAlertRuleServiceImpl(IServerAlertRuleRepository alertRuleRepository) {
        this.alertRuleRepository = alertRuleRepository;
    }

    @Override
    protected void validateUniqueConstraints(ServerAlertRuleDTO dto) {
        // 可以添加唯一性校验，比如同一个服务器的同一类型告警只能有一条规则
        // 这里暂不添加约束
    }

    @Override
    public ServerAlertRuleDTO create(ServerAlertRuleDTO dto) {
        // 校验阈值: 严重阈值必须大于警告阈值
        if (dto.getCriticalThreshold().compareTo(dto.getWarningThreshold()) <= 0) {
            throw new BusinessException(ResponseCode.INVALID_PARAM,
                new Object[]{"严重阈值必须大于警告阈值"});
        }

        return super.create(dto);
    }

    @Override
    public ServerAlertRuleDTO update(Long id, ServerAlertRuleDTO dto) {
        // 校验阈值
        if (dto.getCriticalThreshold().compareTo(dto.getWarningThreshold()) <= 0) {
            throw new BusinessException(ResponseCode.INVALID_PARAM,
                new Object[]{"严重阈值必须大于警告阈值"});
        }

        return super.update(id, dto);
    }
}
