package cn.com.countrygarden.bi.main.util;

import cn.com.countrygarden.bi.main.util.erp.StringUtil;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.ArrayList;
import java.util.List;

/*
 *  绩效 计算工具类
 *
 * */
public class Calculate {
    //计算年度,把""的值设置为-1 测试可用绩效有：大客户满意度，
    public static String Year(double s1, double s2, double s3, double s4) {
        double[] S = {s1, s2, s3, s4};//初始化一个长度为4的数组
        int denominator = 4;//Denominator:分母,初始化分母为4
        int[] point = new int[4];//记录数组中非空的值的下标
        double sum = 0.0;//总分
        double score;//细分
        //判断空的值
        for (int i = 0; i < S.length; i++) {
            if (S[i] == -1) {
                denominator--;
                S[i] = 0;
            }
        }
        //开始计算
        for (double v : S) {
            sum += v;
        }
        score = sum / denominator;
        if(Double.isNaN(score) || Double.isInfinite(score)){
            score = 0.0;
        }
        return String.format("%.2f", score);
    }

    public static Double Year(String S1, String S2, String S3, String S4) {
        return Double.valueOf(Year(Double.parseDouble(StringUtil.isBlankOrEmpty(S1) ? "-1" : S1), Double.parseDouble(StringUtil.isBlankOrEmpty(S2) ? "-1" : S2), Double.parseDouble(StringUtil.isBlankOrEmpty(S3) ? "-1" : S3), Double.parseDouble(StringUtil.isBlankOrEmpty(S4) ? "-1" : S4)));
    }

    //线性插入法 把""的值设置为-1，没有权重的话默认为weight=1 测试可用绩效有：大客户满意度，小客户，物业缴费率，欠费追缴，税前利润，外拓收入，
    public static String InsertionMethod(double actual, double threshold, double atLeast, double scream, double howl, double weight) {
        double score;
        //如果没有门槛值，那么当实际值小于低保值时得分为零
        if (threshold == -1) {
            if (actual <= atLeast) {
                return String.valueOf(0);
            }
            if (actual >= atLeast && actual < scream) {
                score = (100 + 10 * ((actual - atLeast) / (scream - atLeast))) * weight;
                return String.valueOf(String.format("%.2f", score));
            }
            if (actual >= scream && actual < howl) {
                score = (110 + 10 * ((actual - scream) / (howl - scream))) * weight;
                return String.valueOf(String.format("%.2f", score));
            }
            if (actual >= howl) {
                return String.valueOf(String.format("%.2f", 120 * weight));
            }
        } else {
            if (actual <= threshold) {
                return String.valueOf(0);
            }
            if (actual >= threshold && actual < atLeast) {
                score = (60 + 40 * ((actual - threshold) / (atLeast - threshold))) * weight;
                return String.valueOf(String.format("%.2f", score));
            }
            if (actual >= atLeast && actual < scream) {
                score = (100 + 10 * ((actual - atLeast) / (scream - atLeast))) * weight;
                return String.valueOf(String.format("%.2f", score));
            }
            if (actual >= scream && actual < howl) {
                score = (110 + 10 * ((actual - scream) / (howl - scream))) * weight;
                return String.valueOf(String.format("%.2f", score));
            }
            if (actual >= howl) {
                score = 120 * weight;
                return String.valueOf(String.format("%.2f", score));
            }
        }
        return null;
    }

    //五档计算 人均管理面积，元均产值
    public static String Average(double actual, double worse, double lower, double average, double good, double excellent) {
        double score = 0;//初始化得分为0分
        if (actual <= worse) {
            score = 60.00;
            return String.valueOf(score);
        }
        if (actual >= worse && actual <= average) {
            score = 60 + 30 * ((actual - worse) / (average - worse));
            return String.valueOf(String.format("%.2f", score));
        }
        if (actual >= average && actual <= excellent) {
            score = 90 + 30 * ((actual - average) / (excellent - average));
            return String.valueOf(String.format("%.2f", score));
        }
        if (actual >= excellent) {
            return String.valueOf(120);
        }

        return null;
    }

    /////计算收费的 综合已收楼收缴率
    private static String CalS_Score(String S1_score, String S2_score, String S3_score, String S4_score, Double weight) {
        Double S_score = 0.0;
        double[] scores = {Double.parseDouble(S1_score.equals("-1") ? "0" : S1_score), Double.parseDouble(S2_score.equals("-1") ? "0" : S2_score),
                Double.parseDouble(S3_score.equals("-1") ? "0" : S3_score), Double.parseDouble(S4_score.equals("-1") ? "0" : S4_score)};
        List<Double> tmp = new ArrayList<>();
        int denominator = 4;

        for (int i = 0; i < scores.length; i++) {
            if (scores[i] == 0) {
                denominator--;
            } else {
                tmp.add(scores[i]);
            }
        }

//        if (denominator == 4) {
        if (weight > 0.65) {
            S_score = scores[0] * 0.8 * 0.1 / 0.65 + scores[1] * 0.8 * 0.1 / 0.65 + scores[2] * 0.8 * (scores[3] == 0 ? 0.45 : 0.1) / 0.65 + scores[3] * 0.8 * 0.35 / 0.65;  //0.8 怎么分
        } else {
            S_score = scores[0] * 0.1 + scores[1] * 0.1 + scores[2] * (scores[3] == 0 ? 0.45 : 0.1) + scores[3] * 0.35;
        }
//        } else {
//            for (Double score : tmp) {
//                S_score += score * weight / denominator;
//            }
//        }


        return String.format("%.2f", S_score);
    }

    ////////计算物业服务费收缴率的单项指标得分
    public static String getCostIndividualScore(String ZH_score, String S1_score, String S2_score, String S3_score, String S4_score, String WT_score) {
        //ZH_score 是综合收缴率
        //S_score是综合收楼收缴率
        //WT_score是外拓
        String IndividualScore = "-1";
        String S_score = "-1";
        if ("-1".equals(ZH_score) && "-1".equals(WT_score) && "-1".equals(S1_score) && "-1".equals(S2_score) && "-1".equals(S3_score) && "-1".equals(S4_score)) {
            return "-1";
        }

        if ("-1".equals(ZH_score)) {
            ZH_score = "0";
        }

        if ("-1".equals(WT_score)) {
            S_score = CalS_Score(S1_score, S2_score, S3_score, S4_score, 0.8);
            IndividualScore = String.format("%.2f", Double.parseDouble(ZH_score) * 0.2 + Double.parseDouble(S_score));       //有疑问
        } else {
            S_score = CalS_Score(S1_score, S2_score, S3_score, S4_score, 0.65);
            IndividualScore = String.format("%.2f", Double.parseDouble(ZH_score) * 0.2 + Double.parseDouble(S_score) + Double.parseDouble(WT_score));
        }
        return IndividualScore;
    }

    /////城市公司指标-计算物业服务费收缴率的单项指标得分
    public static String getCityCostIndividualScore(String ZH_score, String S1_score, String S2_score, String S3_score, String S4_score) {
        //ZH_Score是综合收费率得分
        //S_Score是综合已收楼收缴率得分
        String IndividualScore = "-1";
        if ("-1".equals(ZH_score) && "-1".equals(S1_score) && "-1".equals(S2_score) && "-1".equals(S3_score) && "-1".equals(S4_score)) {
            return "-1";
        }

        if ("-1".equals(ZH_score)) {
            ZH_score = "0";
        }
        Double ZH_score2 = Double.parseDouble(ZH_score);

        Double S_score = 0.0;
        double[] scores = {Double.parseDouble(S1_score.equals("-1") ? "0" : S1_score), Double.parseDouble(S2_score.equals("-1") ? "0" : S2_score),
                Double.parseDouble(S3_score.equals("-1") ? "0" : S3_score), Double.parseDouble(S4_score.equals("-1") ? "0" : S4_score)};
        int denominator = 3;

        for (int i = denominator; i > 0; i--) {
            if (scores[i] == 0) {
                denominator--;
            }
        }

        switch (denominator) {
            case 3://S1、S2、S3、S4
                S_score = scores[0] * 0.1 + scores[1] * 0.1 + scores[2] * 0.1 + scores[3] * 0.5;
                break;
            case 2://S1 S2 S3
                S_score = scores[0] * 0.33 + scores[1] * 0.33 + scores[2] * 0.33;
                break;
            case 1://S1 S2
                S_score = scores[0] * 0.5 + scores[1] * 0.5;
                break;
            case 0://S1
                S_score = scores[0];
            default:
                break;
        }
        IndividualScore = String.valueOf(ZH_score2 * 0.2 + S_score * 0.8);
        return IndividualScore;

    }

    /////城市公司指标-计算物业服务费欠费追缴率的单项指标得分
    public static String getCityPropertyArrearsRecoveryRateScore(String S1_score, String S2_score, String S3_score, String S4_score) {
        //S_Score是综合已收楼收缴率得分
        String IndividualScore = "-1";
        if ("-1".equals(S1_score) && "-1".equals(S2_score) && "-1".equals(S3_score) && "-1".equals(S4_score)) {
            return "-1";
        }

        Double S_score = 0.0;
        double[] scores = {Double.parseDouble(S1_score.equals("-1") ? "0" : S1_score), Double.parseDouble(S2_score.equals("-1") ? "0" : S2_score),
                Double.parseDouble(S3_score.equals("-1") ? "0" : S3_score), Double.parseDouble(S4_score.equals("-1") ? "0" : S4_score)};
        int denominator = 3;

        for (int i = denominator; i > 0; i--) {
            if (scores[i] == 0) {
                denominator--;
            }
        }

        switch (denominator) {
            case 3://S1、S2、S3、S4
                S_score = scores[0] * 0.1 + scores[1] * 0.1 + scores[2] * 0.1 + scores[3] * 0.7;
                break;
            case 2://S1 S2 S3
                S_score = scores[0] * 0.33 + scores[1] * 0.33 + scores[2] * 0.33;
                break;
            case 1://S1 S2
                S_score = scores[0] * 0.5 + scores[1] * 0.5;
                break;
            case 0://S1
                S_score = scores[0];
            default:
                break;
        }
        IndividualScore = String.valueOf(S_score);
        return IndividualScore;

    }

    ///////////////////////////////物业追收费
    public static String CalnewS_Score(String S1_score, String S2_score, String S3_score, String S4_score) {
        String S_score = null;
        if (S1_score != null && S2_score != null && S3_score != null && S4_score != null) {
            S_score = String.valueOf(Double.parseDouble(S1_score) + Double.parseDouble(S2_score) + Double.parseDouble(S3_score) * 0.1 + Double.parseDouble(S4_score) * 0.70);
        }
        if (S1_score != null && S2_score != null && S3_score != null && S4_score == null) {
            S_score = String.valueOf(Double.parseDouble(S1_score) + Double.parseDouble(S2_score) * 0.1 + Double.parseDouble(S3_score) * 0.80);
        }
        if (S1_score != null && S2_score != null && S3_score == null && S4_score != null) {
            S_score = String.valueOf(Double.parseDouble(S1_score) * 0.1 + Double.parseDouble(S2_score) * 0.1 + Double.parseDouble(S4_score) * 0.80);
        }
        if (S1_score != null && S2_score == null && S3_score != null && S4_score != null) {
            S_score = String.valueOf(Double.parseDouble(S1_score) * 0.1 + Double.parseDouble(S3_score) * 0.1 + Double.parseDouble(S4_score) * 0.80);
        }
        if (S1_score != null && S2_score != null && S3_score == null && S4_score == null) {
            S_score = String.valueOf(Double.parseDouble(S1_score) * 0.1 + Double.parseDouble(S2_score) * 0.90);
        }
        if (S1_score != null && S2_score == null && S3_score == null && S4_score == null) {
            S_score = String.valueOf(Double.parseDouble(S1_score) * 1);
        }
        ////////////////////////////////////////////
        if (S1_score == null && S2_score != null && S3_score != null && S4_score != null) {
            S_score = String.valueOf(Double.parseDouble(S2_score) * 0.1 + Double.parseDouble(S3_score) * 0.1 + Double.parseDouble(S4_score) * 0.80);
        }
        if (S1_score == null && S2_score != null && S3_score == null && S4_score != null) {
            S_score = String.valueOf(Double.parseDouble(S2_score) * 0.1 + Double.parseDouble(S3_score) * 0.90);
        }
        if (S1_score == null && S2_score != null && S3_score == null && S4_score == null) {
            S_score = String.valueOf(Double.parseDouble(S2_score) * 1);
        }
        //////////////////////////////////////
        if (S1_score == null && S2_score == null && S3_score != null && S4_score != null) {
            S_score = String.valueOf(Double.parseDouble(S3_score) * 0.1 + Double.parseDouble(S4_score) * 0.90);
        }
        if (S1_score == null && S2_score == null && S3_score != null && S4_score == null) {
            S_score = String.valueOf(Double.parseDouble(S3_score) * 1);
        }////////////////////////////////////
        if (S1_score == null && S2_score == null && S3_score == null && S4_score != null) {
            S_score = String.valueOf(Double.parseDouble(S4_score) * 1);
        }
        return S_score;
    }

    public static double CalBurglary(double burglaryIncident_Count, double hurt_OR_Dead, double cal_Count) {
        double score = 0.0;//加减分
        double rate = cal_Count == 0 ? 0 : burglaryIncident_Count / cal_Count;//案发率
        if (rate == 0) {
            return score;
        }//案发率为0时不计算
        score = -1 * (burglaryIncident_Count - hurt_OR_Dead) / cal_Count * 10000;
        score = score < -10 ? -10 : score;
        score -= 2 * hurt_OR_Dead;
        if (score < -20) {
            score = -20;
        }

        return score;
    }

    public static double bazaars(String baza, String cvapp) {
        double result = 0;// 结果

        double before = 0;// 输入值
        try {
            before = Double.parseDouble(baza);
        } catch (NumberFormatException e) {
            before = -1;
        }
        double cvapp_atLeasts = 0;// 目标值
        try {
            cvapp_atLeasts = Double.parseDouble(cvapp);
        } catch (NumberFormatException e) {
            cvapp_atLeasts = 0;
        }

        if (before < cvapp_atLeasts) {
            result = 0;
        } else if (before >= cvapp_atLeasts) {
            result = 100;
        }
        return result;
    }

    public static void main(String[] args) {

        String result = Calculate.InsertionMethod(4.573699, 4.2, 4.2, 4.5, 4.8, 1);

//        String result = Calculate.InsertionMethod(4.22,4.2,4.2,4.5,4.8,1);

        System.out.println(result);

    }
}
