package com.huatek.busi.task.transformer.utils;

import com.google.common.collect.Maps;
import com.huatek.busi.util.EvaluationNameUtil;
import com.huatek.busi.util.TransformerFormulaCal;
import com.huatek.busi.vo.BusiParamTransformerVo;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Map;
import java.util.Objects;

@Slf4j
public class CommonUtils {

    public static final String STEP = "step";
    public static final String TOTAL = "total";

    public static TransformerFormulaCal transformerFormulaCalInstance = TransformerFormulaCal.getInstance();

    /**
     * @param key
     * @return BusiParamTransformerVo
     * @Title: getEvaluateObject
     * @Description: 获取评测对象
     * @createDate: Sep 4, 2019 4:04:29 PM
     */
    public static BusiParamTransformerVo getEvaluateObject(String key, Map<String, BusiParamTransformerVo> map) {
        if (map.containsKey(key)) {
            return map.get(key);
        }
        return null;
    }

    /**
     * @param
     * @return Double
     * @throws
     * @Title: calcWeight
     * @Description: 计算权重
     * @createDate: Sep 6, 2019 3:28:47 PM
     */
    public static Double calcWeight(double value, BusiParamTransformerVo vo) {
        if (vo.getIsAttend().equals("1")) {
            if (vo.getValue() == null) {
                return 0d;
            }
            double weight = vo.getValue().doubleValue();
            double d = value * weight;
            return new BigDecimal(d).setScale(4, RoundingMode.HALF_UP).doubleValue();
        }
        return 0d;
    }


    /**
     * @param testValue 测试值
     * @return Double
     * @Title: calcWeight
     * @Description: 计算权重
     * @createDate: Sep 4, 2019 5:53:23 PM
     */
    public static Double calcWeight(String testValue, BusiParamTransformerVo vo) {
        if (StringUtils.isBlank(testValue)) {
            return 0d;
        }
        Double aValue = vo.getAValue() == null ? 0d : vo.getAValue().doubleValue();
        Double bValue = vo.getBValue() == null ? 0d : vo.getBValue().doubleValue();
        Double weight = vo.getValue() == null ? 0d : vo.getValue().doubleValue();
        String isAttend = vo.getIsAttend();
        String model = vo.getModel();
        Double value = Double.valueOf(testValue);
        //评分模型：1：升   2:降
        if ("1".equals(model)) {
            return transformerFormulaCalInstance.clacFormulaAsc(aValue, bValue, value, weight, isAttend);
        } else if ("2".equals(model)) {
            return transformerFormulaCalInstance.clacFormulaDesc(aValue, bValue, value, weight, isAttend);
        }
        return 0d;
    }


    public static String calcWeight(double weight, double temp) {
        com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
        jsonObject.put("weight", weight);
        jsonObject.put("tmp", temp);
        return jsonObject.toJSONString();
    }

    /**
     * 计算步骤
     *
     * @param jsonArray
     * @param stepId          步骤编号->第几步
     * @param stepName        每个步骤名称
     * @param inputParam      入参
     * @param calculateResult 计算结果
     */
    public static void setStep(JSONArray jsonArray, int stepId, String stepName, String inputParam, String calculateResult) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("stepId", stepId);
        jsonObject.put("stepName", stepName);
        jsonObject.put("inputParam", inputParam);
        jsonObject.put("calculateResult", calculateResult);
        jsonArray.add(jsonObject);
    }

    public static String calculateModel(String v, BusiParamTransformerVo tmp) {
        com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
        jsonObject.put("aValue", tmp.getAValue() == null ? 0d : tmp.getAValue().doubleValue());
        jsonObject.put("bValue", tmp.getBValue() == null ? 0d : tmp.getBValue().doubleValue());
        jsonObject.put("weight", tmp.getValue() == null ? 0d : tmp.getValue().doubleValue());
        jsonObject.put("isAttend", tmp.getIsAttend());
        jsonObject.put("model", tmp.getModel());
        jsonObject.put("value", v);
        return jsonObject.toJSONString();
    }


    public static boolean checkValue(String value) {
        return StringUtils.isBlank(value);
    }

    public static boolean checkParams(Object value) {
        return Objects.isNull(value);
    }

    public static boolean checkAttend(BusiParamTransformerVo vo) {
        return vo.getIsAttend().equals("1");
    }


    public static Map<String, Object> buildResult(Integer step, double total) {
        Map<String, Object> resultMap = Maps.newConcurrentMap();
        resultMap.put(STEP, step);
        resultMap.put(TOTAL, total);
        return resultMap;
    }

    /**
     * @param value
     * @param type
     * @param typeDesc
     * @param threeLevelMap
     * @param jsonArray
     * @param step
     * @param stepName
     * @Description: 计算每项结果
     * @param:
     * @Return: java.lang.Double
     */
    public static Double calcItemResult(String value, String type, String typeDesc, Map<String, BusiParamTransformerVo> threeLevelMap, JSONArray jsonArray, int step, String stepName) {
        Double calcWeight = 0d;
        BusiParamTransformerVo params = getEvaluateObject(type, threeLevelMap);
        if (checkParams(params)) {
            log.error("-->>{}未设置评估参数", typeDesc);
            return calcWeight;
        }
        if (checkAttend(params)) {
            if (checkValue(value)) {
                log.error("-->>{}未设置未获取到评估数据", typeDesc);
                BigDecimal value1 = params.getValue() == null ? BigDecimal.ZERO :params.getValue();
                calcWeight = value1.doubleValue();
            } else {
                calcWeight = calcWeight(value, params);
            }
            step = step + 1;
            EvaluationNameUtil.setStep(step, params, calcWeight.toString(), value);
            return calcWeight;
        }
        return calcWeight;
    }


    /**
     * @param secondVo       二级评估参数
     * @param threeResultMap 三级计算结果
     * @param jsonArray
     * @param stepName
     * @Description: 计算二级中个分项
     * @param:
     * @Return: java.util.Map<java.lang.String, java.lang.Object>
     */
    public static Map<String, Object> calcSecond(BusiParamTransformerVo secondVo, Map<String, Object> threeResultMap, JSONArray jsonArray, String stepName) {
        Double threeTotal = Double.valueOf(threeResultMap.get(CommonUtils.TOTAL).toString());
        double secondTotal = CommonUtils.calcWeight(threeTotal, secondVo);
        log.info("-- >{}结果:[{}]", stepName, secondTotal);
        int tmp = Integer.parseInt(threeResultMap.get(CommonUtils.STEP).toString());
        EvaluationNameUtil.setStep(tmp, secondVo, secondTotal + "", threeTotal.toString());
        return CommonUtils.buildResult(tmp, secondTotal);
    }
}
