package com.ruoyi.web.modules.epidemic.support;

import java.math.BigDecimal;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.epidemic.domain.BizRiskFactorWeight;
import com.ruoyi.web.modules.epidemic.domain.dto.RiskFactorWeightSaveRequest;
import com.ruoyi.web.modules.epidemic.domain.vo.RiskFactorWeightVo;

/**
 * 风险因子权重校验与对象转换。
 */
public final class RiskFactorWeightValidation
{
    private RiskFactorWeightValidation()
    {
    }

    public static void validateForSave(RiskFactorWeightSaveRequest request)
    {
        if (request == null)
        {
            throw new ServiceException("参数不能为空");
        }
        validateRequired("养殖密度", request.getBreedingDensityWeight());
        validateRequired("交通流动性", request.getTrafficMobilityWeight());
        validateRequired("检疫强度", request.getQuarantineIntensityWeight());
        validateRequired("气象适宜度", request.getMeteorologicalSuitabilityWeight());
        validateRequired("既往发病水平", request.getPastDiseaseLevelWeight());

        validatePositive("养殖密度", request.getBreedingDensityWeight());
        validatePositive("交通流动性", request.getTrafficMobilityWeight());
        validatePositive("检疫强度", request.getQuarantineIntensityWeight());
        validatePositive("气象适宜度", request.getMeteorologicalSuitabilityWeight());
        validatePositive("既往发病水平", request.getPastDiseaseLevelWeight());

        BigDecimal sum = RiskFactorWeightRules.calcSum(
                request.getBreedingDensityWeight(),
                request.getTrafficMobilityWeight(),
                request.getQuarantineIntensityWeight(),
                request.getMeteorologicalSuitabilityWeight(),
                request.getPastDiseaseLevelWeight());
        if (!RiskFactorWeightRules.isSumEqualsOne(
                request.getBreedingDensityWeight(),
                request.getTrafficMobilityWeight(),
                request.getQuarantineIntensityWeight(),
                request.getMeteorologicalSuitabilityWeight(),
                request.getPastDiseaseLevelWeight()))
        {
            throw new ServiceException("五项权重之和须等于 1，当前合计为 " + sum);
        }
    }

    public static BizRiskFactorWeight toEntity(RiskFactorWeightSaveRequest request)
    {
        BizRiskFactorWeight row = new BizRiskFactorWeight();
        row.setId(RiskFactorWeightRules.SINGLETON_ID);
        row.setWeightBreedingDensity(scaleWeight(request.getBreedingDensityWeight()));
        row.setWeightTrafficMobility(scaleWeight(request.getTrafficMobilityWeight()));
        row.setWeightQuarantineIntensity(scaleWeight(request.getQuarantineIntensityWeight()));
        row.setWeightMeteorologicalSuitability(scaleWeight(request.getMeteorologicalSuitabilityWeight()));
        row.setWeightPastDiseaseLevel(scaleWeight(request.getPastDiseaseLevelWeight()));
        return row;
    }

    public static RiskFactorWeightVo toVo(BizRiskFactorWeight row)
    {
        if (row == null)
        {
            return null;
        }
        RiskFactorWeightVo vo = new RiskFactorWeightVo();
        vo.setBreedingDensityWeight(row.getWeightBreedingDensity());
        vo.setTrafficMobilityWeight(row.getWeightTrafficMobility());
        vo.setQuarantineIntensityWeight(row.getWeightQuarantineIntensity());
        vo.setMeteorologicalSuitabilityWeight(row.getWeightMeteorologicalSuitability());
        vo.setPastDiseaseLevelWeight(row.getWeightPastDiseaseLevel());
        vo.setWeightSum(RiskFactorWeightRules.calcSum(
                row.getWeightBreedingDensity(),
                row.getWeightTrafficMobility(),
                row.getWeightQuarantineIntensity(),
                row.getWeightMeteorologicalSuitability(),
                row.getWeightPastDiseaseLevel()));
        vo.setUpdateBy(row.getUpdateBy());
        vo.setUpdateTime(row.getUpdateTime() != null ? row.getUpdateTime() : row.getCreateTime());
        return vo;
    }

    private static void validateRequired(String factorName, BigDecimal weight)
    {
        if (weight == null)
        {
            throw new ServiceException("请填写【" + factorName + "】权重");
        }
    }

    private static void validatePositive(String factorName, BigDecimal weight)
    {
        if (!RiskFactorWeightRules.isPositiveWeight(weight))
        {
            throw new ServiceException("【" + factorName + "】权重须为正数");
        }
    }

    private static BigDecimal scaleWeight(BigDecimal weight)
    {
        return weight.setScale(4, java.math.RoundingMode.HALF_UP);
    }
}
