package cn.cecep.talroad.util;


import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.domain.BMainAllowablePollutionAir;
import cn.cecep.talroad.domain.BMainAllowablePollutionWater;
import cn.cecep.talroad.domain.BMainPollDisGasTout;
import cn.cecep.talroad.domain.BMainPollDisWaterTout;
import cn.cecep.talroad.vo.analysis.OutletPermitPollutantVO;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 排放量计算的相关工具类
 */
public class EmissionsUtils {


    /**
     * 返回该污染因子是否需要计算排放量
     * @param interval
     * @param target
     * @return
     */
    public static boolean isNeedMathPermitEmissions(int interval, OutletPermitPollutantVO target){
        return !intervalToObjectValue(interval,"getPollutantsTotal",target).equalsIgnoreCase("/");
    }

    /**
     * 根据目前年份与开始时间计算出获取排污许可第几年的指标
     * @return
     */
    public static String choseWaterYearPollutantsTotalData(int interval, BMainAllowablePollutionWater bMainAllowablePollutionWater){
        return intervalToObjectValue(interval,"getTotal",bMainAllowablePollutionWater);
    }

    /**
     * 根据目前年份与开始时间计算出获取排污许可第几年的指标
     * @return
     */
    public static String choseGasYearPollutantsTotalData(int interval, BMainAllowablePollutionAir bMainAllowablePollutionAir){
        return intervalToObjectValue(interval,"getTotal",bMainAllowablePollutionAir);
    }

    /**
     * 间隔转换 反射获取value
     * @param interval
     * @return
     */
    private static String intervalToObjectValue(int interval ,String paramName, Object target){
        Class<?> aClass = target.getClass();
        String methodName = "";
        switch (interval){
            case 1:
            default:
                methodName = "Frist";
                break;
            case 2:
                methodName = "Second";
                break;
            case 3:
                methodName = "Third";
                break;
            case 4:
                methodName = "Fourth";
                break;
            case 5:
                methodName = "Fifth";
                break;
        }
        try {
            Method method = aClass.getMethod(paramName + methodName);
            Object result = method.invoke(target);
            if(result instanceof String){
                return StringUtils.isBlank((String)result)?"/":(String)result;
            }
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return "/";
    }

}
