package com.xzzn.ems.service.impl;

import com.xzzn.common.utils.DateUtils;
import com.xzzn.common.utils.StringUtils;
import com.xzzn.ems.domain.EmsStrategyRuntimeConfig;
import com.xzzn.ems.mapper.EmsStrategyRuntimeConfigMapper;
import com.xzzn.ems.service.IEmsStrategyRuntimeConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 策略运行参数配置Service业务层处理
 *
 * @author xzzn
 * @date 2026-02-13
 */
@Service
public class EmsStrategyRuntimeConfigServiceImpl implements IEmsStrategyRuntimeConfigService {

    private static final BigDecimal DEFAULT_SOC_DOWN = BigDecimal.ZERO;
    private static final BigDecimal DEFAULT_SOC_UP = new BigDecimal("100");
    private static final BigDecimal DEFAULT_ANTI_REVERSE_THRESHOLD = new BigDecimal("30");
    private static final BigDecimal DEFAULT_ANTI_REVERSE_RANGE_PERCENT = new BigDecimal("20");
    private static final BigDecimal DEFAULT_ANTI_REVERSE_UP = new BigDecimal("100");
    private static final BigDecimal DEFAULT_ANTI_REVERSE_POWER_DOWN_PERCENT = new BigDecimal("10");
    private static final BigDecimal DEFAULT_ANTI_REVERSE_HARD_STOP_THRESHOLD = new BigDecimal("20");
    private static final BigDecimal DEFAULT_POWER_SET_MULTIPLIER = new BigDecimal("10");
    private static final Integer DEFAULT_PROTECT_INTERVENE_ENABLE = 1;
    private static final BigDecimal DEFAULT_PROTECT_L1_DERATE_PERCENT = new BigDecimal("50");
    private static final Integer DEFAULT_PROTECT_RECOVERY_STABLE_SECONDS = 5;
    private static final Integer DEFAULT_PROTECT_L3_LATCH_ENABLE = 1;
    private static final String DEFAULT_PROTECT_CONFLICT_POLICY = "MAX_LEVEL_WIN";

    @Autowired
    private EmsStrategyRuntimeConfigMapper runtimeConfigMapper;

    @Override
    public EmsStrategyRuntimeConfig getBySiteId(String siteId) {
        EmsStrategyRuntimeConfig config = runtimeConfigMapper.selectBySiteId(siteId);
        if (config == null) {
            return buildDefaultConfig(siteId);
        }
        fillMissingWithDefault(config, siteId);
        return config;
    }

    @Override
    public int saveBySiteId(EmsStrategyRuntimeConfig config) {
        fillMissingWithDefault(config, config.getSiteId());
        EmsStrategyRuntimeConfig exist = runtimeConfigMapper.selectBySiteId(config.getSiteId());
        if (exist == null) {
            config.setCreateTime(DateUtils.getNowDate());
            return runtimeConfigMapper.insert(config);
        }
        config.setUpdateTime(DateUtils.getNowDate());
        return runtimeConfigMapper.updateBySiteId(config);
    }

    private EmsStrategyRuntimeConfig buildDefaultConfig(String siteId) {
        EmsStrategyRuntimeConfig config = new EmsStrategyRuntimeConfig();
        fillMissingWithDefault(config, siteId);
        return config;
    }

    private void fillMissingWithDefault(EmsStrategyRuntimeConfig config, String siteId) {
        if (StringUtils.isNotEmpty(siteId)) {
            config.setSiteId(siteId);
        }
        if (config.getSocDown() == null) {
            config.setSocDown(DEFAULT_SOC_DOWN);
        }
        if (config.getSocUp() == null) {
            config.setSocUp(DEFAULT_SOC_UP);
        }
        if (config.getAntiReverseThreshold() == null) {
            config.setAntiReverseThreshold(DEFAULT_ANTI_REVERSE_THRESHOLD);
        }
        if (config.getAntiReverseRangePercent() == null) {
            config.setAntiReverseRangePercent(DEFAULT_ANTI_REVERSE_RANGE_PERCENT);
        }
        if (config.getAntiReverseUp() == null) {
            config.setAntiReverseUp(DEFAULT_ANTI_REVERSE_UP);
        }
        if (config.getAntiReversePowerDownPercent() == null) {
            config.setAntiReversePowerDownPercent(DEFAULT_ANTI_REVERSE_POWER_DOWN_PERCENT);
        }
        if (config.getAntiReverseHardStopThreshold() == null) {
            config.setAntiReverseHardStopThreshold(DEFAULT_ANTI_REVERSE_HARD_STOP_THRESHOLD);
        }
        if (config.getPowerSetMultiplier() == null || config.getPowerSetMultiplier().compareTo(BigDecimal.ZERO) <= 0) {
            config.setPowerSetMultiplier(DEFAULT_POWER_SET_MULTIPLIER);
        }
        if (config.getProtectInterveneEnable() == null) {
            config.setProtectInterveneEnable(DEFAULT_PROTECT_INTERVENE_ENABLE);
        }
        if (config.getProtectL1DeratePercent() == null || config.getProtectL1DeratePercent().compareTo(BigDecimal.ZERO) < 0) {
            config.setProtectL1DeratePercent(DEFAULT_PROTECT_L1_DERATE_PERCENT);
        }
        if (config.getProtectRecoveryStableSeconds() == null || config.getProtectRecoveryStableSeconds() < 0) {
            config.setProtectRecoveryStableSeconds(DEFAULT_PROTECT_RECOVERY_STABLE_SECONDS);
        }
        if (config.getProtectL3LatchEnable() == null) {
            config.setProtectL3LatchEnable(DEFAULT_PROTECT_L3_LATCH_ENABLE);
        }
        if (StringUtils.isEmpty(config.getProtectConflictPolicy())) {
            config.setProtectConflictPolicy(DEFAULT_PROTECT_CONFLICT_POLICY);
        }
    }
}
