package com.rf.psychological.scale.resultBusiness.scaleResult;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.dto.scale.NewResultDto;
import com.rf.psychological.dao.model.AnswerEntity;
import com.rf.psychological.dao.model.DimensionEntity;
import com.rf.psychological.scale.dao.model.DimensionOrgEntity;
import com.rf.psychological.scale.dao.model.ScaleMarksEntity;
import com.rf.psychological.scale.dao.model.WarnEntity;
import com.rf.psychological.scale.scaleresult.ScaleConstant;
import com.rf.psychological.scale.service.DimensionOrgService;
import com.rf.psychological.utils.Constant;
import org.springframework.beans.BeanUtils;
import org.springframework.lang.Nullable;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zsy
 * @description:家庭环境量表中文版(已使用通用新模板计算)
 * @date 2021/7/20 15:55
 */
public class FESCVScale extends BaseScale{

    public FESCVScale(JSONArray jsonArray, JSONObject resultJson) {
        super(jsonArray, resultJson);
    }

    public  JSONObject scaleCalculate() throws Exception {

        // 自定义路径使用
        /*ScaleUtil.getScaleDll("FESCVSCALE.dll");
        //System.out.println("JSONArray:"+jsonArray);
        //System.out.println("resultJson:"+resultJson);
        XScale xScale = new XScale(jsonArray,resultJson);
        String result = xScale.FESCVScale(jsonArray.toString(), resultJson.toString());
        System.out.println("dllresult:"+result);
        //解决排序问题
        LinkedHashMap<String, Object> jsonMap = JSON.parseObject(result,LinkedHashMap.class, Feature.OrderedField);
        JSONObject jsonObject = new JSONObject(true);
        jsonObject.putAll(jsonMap);
        //System.out.println("jsonObject:"+jsonObject);
        return jsonObject;*/


        //亲密度得分
        int score1 = -12;
        int score11 = -3;
        //情感表达得分
        int score2 = -10;
        int score21 = -4;
        //矛盾性得分
        int score3 = -12;
        int score31 = -3;
        //独立性得分
        int score4 = -14;
        int score41 = -2;
        //成功性得分
        int score5 = -14;
        int score51 = -2;
        //文化性得分
        int score6 = -10;
        int score61 = -4;
        //娱乐性得分
        int score7 = -10;
        int score71 = -5;
        //道德宗教观得分
        int score8 = -14;
        int score81 = -3;
        //组织性得分
        int score9 = -10;
        int score91 = -3;
        //控制性得分
        int score10 = -10;
        int score101 = -4;
        for (int i = 1; i < jsonArray.size()+1; i++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i-1);
            if (jsonObject1.getString("checkItems").equals("是")) {
                if (i == 1 || i == 21 || i == 31 || i == 51 || i == 71 || i == 81){
                    score1 += 1;
                }else if (i == 11 || i == 41 || i == 61){
                    score11 += 1;
                }else if (i == 12 || i == 32 || i == 42 || i == 62 || i == 82){
                    score2 += 1;
                }else if (i == 2 || i == 22 || i == 52 || i == 72){
                    score21 += 1;
                }else if (i == 3 || i == 23 || i == 43 || i == 53 || i == 73 || i == 83){
                    score3 += 1;
                }else if (i == 13 || i == 33 || i == 63){
                    score31 += 1;
                }else if (i == 14 || i == 24 || i == 34 || i == 44 || i == 64 || i == 74 || i == 84){
                    score4 += 1;
                }else if (i == 4 || i == 54){
                    score41 += 1;
                }else if (i == 5 || i == 15 || i == 25 || i == 35 || i == 45 || i == 75 || i == 85){
                    score5 += 1;
                }else if (i == 65 || i == 55){
                    score51 += 1;
                }else if (i == 6 || i == 26 || i == 56 || i == 66 || i == 86){
                    score6 += 1;
                }else if (i == 16 || i == 36 || i == 46 || i == 76){
                    score61 += 1;
                }else if (i == 17 || i == 37 || i == 47 || i == 67 || i == 77){
                    score7 += 1;
                }else if (i == 7 || i == 27 || i == 57 || i == 87){
                    score71 += 1;
                }else if (i == 8 || i == 28 || i == 48 || i == 68 || i == 78){
                    score8 += 1;
                }else if (i == 58){
                    score8 += 2;
                }else if (i == 18 || i == 38 || i == 88){
                    score81 += 1;
                }else if (i == 19 || i == 39 || i == 59 || i == 69 || i == 89){
                    score9 += 1;
                }else if (i == 29 || i == 49 || i == 79){
                    score91 += 1;
                }else if (i == 30 || i == 40 || i == 50 || i == 80 || i == 90){
                    score10 += 1;
                }else if (i == 10 || i == 20 || i == 60 || i == 70){
                    score101 += 1;
                }
            }else {
                if (i == 1 || i == 21 || i == 31 || i == 51 || i == 71 || i == 81){
                    score1 += 2;
                }else if (i == 11 || i == 41 || i == 61){
                    score11 += 2;
                }else if (i == 12 || i == 32 || i == 42 || i == 62 || i == 82){
                    score2 += 2;
                }else if (i == 2 || i == 22 || i == 52 || i == 72){
                    score21 += 2;
                }else if (i == 3 || i == 23 || i == 43 || i == 53 || i == 73 || i == 83){
                    score3 += 2;
                }else if (i == 13 || i == 33 || i == 63){
                    score31 += 2;
                }else if (i == 14 || i == 24 || i == 34 || i == 44 || i == 64 || i == 74 || i == 84){
                    score4 += 2;
                }else if (i == 4 || i == 54){
                    score41 += 2;
                }else if (i == 5 || i == 15 || i == 25 || i == 35 || i == 45 || i == 75 || i == 85){
                    score5 += 2;
                }else if (i == 65 || i == 55){
                    score51 += 2;
                }else if (i == 6 || i == 26 || i == 56 || i == 66 || i == 86){
                    score6 += 2;
                }else if (i == 16 || i == 36 || i == 46 || i == 76){
                    score61 += 2;
                }else if (i == 17 || i == 37 || i == 47 || i == 67 || i == 77){
                    score7 += 2;
                }else if (i == 7 || i == 27 || i == 57 || i == 87){
                    score71 += 2;
                }else if (i == 8 || i == 28 || i == 48 || i == 68 || i == 78){
                    score8 += 2;
                }else if (i == 18 || i == 38 || i == 88){
                    score81 += 2;
                }else if (i == 58){
                    score8 += 4;
                }else if (i == 19 || i == 39 || i == 59 || i == 69 || i == 89){
                    score9 += 2;
                }else if (i == 29 || i == 49 || i == 79){
                    score91 += 2;
                }else if (i == 30 || i == 40 || i == 50 || i == 80 || i == 90){
                    score10 += 2;
                }else if (i == 10 || i == 20 || i == 60 || i == 70){
                    score101 += 2;
                }
            }
        }
        score1 = score11 - score1;
        score2 = score21 - score2;
        score3 = score31 - score3;
        score4 = score41 - score4;
        score5 = score51 - score5;
        score6 = score61 - score6;
        score7 = score71 - score7;
        score8 = score81 - score8;
        score9 = score91 - score9;
        score10 = score101 - score10;
        Map<String,Double> dimensionScoreMap = new HashMap<>();
        dimensionScoreMap.put("8af178857b94d581017b94f1ea0d132f", (double) score1);
        dimensionScoreMap.put("8af178857b94d581017b94f1ea621330", (double) score2);
        dimensionScoreMap.put("8af178857b94d581017b94f1eab71331", (double) score3);
        dimensionScoreMap.put("8af178857b94d581017b94f1eb0b1332", (double) score4);
        dimensionScoreMap.put("8af178857b94d581017b94f1eb611333", (double) score5);
        dimensionScoreMap.put("8af178857b94d581017b94f1ebb71334", (double) score6);
        dimensionScoreMap.put("8af178857b94d581017b94f1ec0c1335", (double) score7);
        dimensionScoreMap.put("8af178857b94d581017b94f1ec611336", (double) score8);
        dimensionScoreMap.put("8af178857b94d581017b94f1ecb61337", (double) score9);
        dimensionScoreMap.put("8af178857b94d581017b94f1ed0b1338", (double) score10);
        resultJson.put("dimensionScoreMap",dimensionScoreMap);



        //总分
        double score = 0;
        DecimalFormat df = new DecimalFormat("######0.00");

        Map<String, String> resultMap0 = new LinkedHashMap<>();
        List<Map<String, String>> resultMapList = new ArrayList<>();
        //新版本数据格式
        Map<String, Object> newResult = new LinkedHashMap<>();

        //获取答案列表
        List<AnswerEntity> answerEntities = (List<AnswerEntity>) resultJson.get("answerEntities");
        //获取评分规则列表
        List<ScaleMarksEntity> scaleMarksEntities = (List<ScaleMarksEntity>) resultJson.get("scaleMarksEntities");
        //获取维度信息列表
        List<DimensionEntity> dimensionEntities = (List<DimensionEntity>) resultJson.get("dimensionEntities");
        //计算每个维度得分dimensionEntities
        double[] dimensionScore = {score1,score2,score3,score4,score5,score6,score7,score8,score9,score10};
        //jsonArray:用户自己选择的题目选项
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            //计算总分
            for (AnswerEntity answerEntity : answerEntities) {
                if (answerEntity.getQuestionNo().equals(jsonObject1.getString("questionNo"))) {
                    if (answerEntity.getName().replaceAll("\\s", "").equals(jsonObject1.getString("checkItems").replaceAll("\\s", ""))) {
                        score += Double.parseDouble(answerEntity.getScore());
                        break;
                    }
                }
            }
        }

        //将量表总维度根据维度名称进行分组
        Map<String, List<ScaleMarksEntity>> scaleMarksMap = scaleMarksEntities.stream().collect(Collectors.groupingBy(ScaleMarksEntity::getName));
        //返回值以及存数据库格式
        List<NewResultDto> newResultDtos = new ArrayList<>();
        resultMap0.put("总分", df.format(score));
        resultJson.put("总分", df.format(score));
        BigDecimal scoreDecimal = new BigDecimal(df.format(score));
        //如果没有总分这一栏，需要手动赋值返回
        List<ScaleMarksEntity> totalScore = scaleMarksMap.get("总分");
        List<ScaleMarksEntity> totalScores = scaleMarksMap.get("无");
        if (totalScore == null) {
            if (totalScores == null){
                newResultDtos.add(new NewResultDto("总分", df.format(score), "无", "无",
                        "无", "无", "无", "无", "是"));
            }else {
                for (ScaleMarksEntity tempTotalScore : totalScores) {
                    BigDecimal scoreStart = new BigDecimal(tempTotalScore.getScoreStart());
                    BigDecimal scoreEnd = new BigDecimal(tempTotalScore.getScoreEnd());
                    if (scoreDecimal.compareTo(scoreEnd) <= 0 && scoreDecimal.compareTo(scoreStart) >= 0) {
                        newResultDtos.add(new NewResultDto("总分", df.format(score), tempTotalScore.getSymptom(), tempTotalScore.getImprovementSuggestions(),
                                tempTotalScore.getFlag(), tempTotalScore.getReference(), tempTotalScore.getNameExplain(), tempTotalScore.getSuggestion(), tempTotalScore.getIsTotalScoreExplain()));
                        resultMap0.put("总分解释", tempTotalScore.getSymptom());
                        resultMap0.put("总分说明", tempTotalScore.getImprovementSuggestions());
                        resultMap0.put("总分解读", tempTotalScore.getNameExplain());
                        resultMap0.put("总分建议", tempTotalScore.getSuggestion());
                        resultJson.put("总分解释", tempTotalScore.getSymptom());
                        resultJson.put("总分说明", tempTotalScore.getImprovementSuggestions());
                        resultJson.put("总分解读", tempTotalScore.getNameExplain());
                        resultJson.put("总分建议", tempTotalScore.getSuggestion());
                    }
                }
            }
        } else {
            for (ScaleMarksEntity tempTotalScore : totalScore) {
                BigDecimal scoreStart = new BigDecimal(tempTotalScore.getScoreStart());
                BigDecimal scoreEnd = new BigDecimal(tempTotalScore.getScoreEnd());
                if (scoreDecimal.compareTo(scoreEnd) <= 0 && scoreDecimal.compareTo(scoreStart) >= 0) {
                    newResultDtos.add(new NewResultDto("总分", df.format(score), tempTotalScore.getSymptom(), tempTotalScore.getImprovementSuggestions(),
                            tempTotalScore.getFlag(), tempTotalScore.getReference(), tempTotalScore.getNameExplain(), tempTotalScore.getSuggestion(), tempTotalScore.getIsTotalScoreExplain()));
                    resultMap0.put("总分解释", tempTotalScore.getSymptom());
                    resultMap0.put("总分说明", tempTotalScore.getImprovementSuggestions());
                    resultMap0.put("总分解读", tempTotalScore.getNameExplain());
                    resultMap0.put("总分建议", tempTotalScore.getSuggestion());
                    resultJson.put("总分解释", tempTotalScore.getSymptom());
                    resultJson.put("总分说明", tempTotalScore.getImprovementSuggestions());
                    resultJson.put("总分解读", tempTotalScore.getNameExplain());
                    resultJson.put("总分建议", tempTotalScore.getSuggestion());
                }
            }
        }


        JSONObject iconInfo = new JSONObject();
        //雷达图需要的维度以及最大值
        List<Map<String, Object>> indicator = new LinkedList<>();
        //雷达图所需要的常模参考值
        LinkedList<String> reference = new LinkedList<>();
        //雷达图需要的分数
        LinkedList<String> scoreList = new LinkedList<>();
        //计算维度所在评分区间
        commonComputeDimensionScore(df, resultMap0, resultJson, dimensionEntities, dimensionScore, scaleMarksMap, newResultDtos, indicator, reference, scoreList);
        //需要图表展示的
        iconInfo.put("indicator", indicator);
        //判断是否有常模参考值，即reference的内容如果全为0，则不进行返回
        if (!reference.stream().allMatch("0"::equals)) {
            iconInfo.put("reference", reference);
        }
        iconInfo.put("scoreList", scoreList);
        if (indicator.size() == 0 && reference.size() == 0) {
            newResult.put("iconInfo", "");
        } else {
            newResult.put("iconInfo", iconInfo);
        }
        newResult.put("result", newResultDtos);
        resultMapList.add(resultMap0);


        JSONObject returnJson = new JSONObject(true);
        returnJson.put("resultMapList", resultMapList);
        returnJson.put("resultJson", resultJson);
        returnJson.put(ScaleConstant.ResultEnum.RESULT_NEW_FIELD.getKeyword(), newResult);

        return returnJson;

        /* 第一版本，暂保留！

        //得分列表
        List<Map<String,String>> resultMapList = new ArrayList<>();
        Map<String, String> resultMap1 = new LinkedHashMap<>();
        resultMap1.put("亲密度得分", String.valueOf(score1));
        resultJson.put("亲密度得分", String.valueOf(score1));
        if (score1 <= 5){
            resultMap1.put("亲密度", "低分");
            resultJson.put("亲密度", "低分");
        }else if (score1 <= 8 && score1 >= 6){
            resultMap1.put("亲密度", "中等");
            resultJson.put("亲密度", "中等");
        }else {
            resultMap1.put("亲密度", "高分");
            resultJson.put("亲密度", "高分");
        }
        resultMapList.add(resultMap1);
        //得分列表
        Map<String, String> resultMap2 = new LinkedHashMap<>();
        resultMap2.put("情感表达得分", String.valueOf(score2));
        resultJson.put("情感表达得分", String.valueOf(score2));
        if (score2 <= 4){
            resultMap2.put("情感表达", "低分");
            resultJson.put("情感表达", "低分");
        }else if (score2 <= 7 && score2 >=5){
            resultMap2.put("情感表达", "中等");
            resultJson.put("情感表达", "中等");
        }else {
            resultMap2.put("情感表达", "高分");
            resultJson.put("情感表达", "高分");
        }
        resultMapList.add(resultMap2);
        //得分列表
        Map<String, String> resultMap3 = new LinkedHashMap<>();
        resultMap3.put("矛盾性得分", String.valueOf(score3));
        resultJson.put("矛盾性得分", String.valueOf(score3));
        if (score3 <= 1){
            resultMap3.put("矛盾性", "低分");
            resultJson.put("矛盾性", "低分");
        }else if (score3 <= 5 && score3 >= 2){
            resultMap3.put("矛盾性", "中等");
            resultJson.put("矛盾性", "中等");
        }else {
            resultMap3.put("矛盾性", "高分");
            resultJson.put("矛盾性", "高分");
        }
        resultMapList.add(resultMap3);
        Map<String, String> resultMap4 = new LinkedHashMap<>();
        resultMap4.put("独立性得分", String.valueOf(score4));
        resultJson.put("独立性得分", String.valueOf(score4));
        if (score4 <= 3){
            resultMap4.put("独立性", "低分");
            resultJson.put("独立性", "低分");
        }else if (score4 <= 7 && score4 >= 4){
            resultMap4.put("独立性", "中等");
            resultJson.put("独立性", "中等");
        }else {
            resultMap4.put("独立性", "高分");
            resultJson.put("独立性", "高分");
        }
        resultMapList.add(resultMap4);
        Map<String, String> resultMap5 = new LinkedHashMap<>();
        resultMap5.put("成功性得分", String.valueOf(score5));
        resultJson.put("成功性得分", String.valueOf(score5));
        if (score5 <= 5){
            resultMap5.put("成功性", "低分");
            resultJson.put("成功性", "低分");
        }else if (score5 <= 8 && score5 >= 6){
            resultMap5.put("成功性", "中等");
            resultJson.put("成功性", "中等");
        }else {
            resultMap5.put("成功性", "高分");
            resultJson.put("成功性", "高分");
        }
        resultMapList.add(resultMap5);
        Map<String, String> resultMap6 = new LinkedHashMap<>();
        resultMap6.put("文化性得分", String.valueOf(score6));
        resultJson.put("文化性得分", String.valueOf(score6));
        if (score6 <= 3){
            resultMap6.put("文化性", "低分");
            resultJson.put("文化性", "低分");
        }else if (score6 <= 7 && score6 >= 4){
            resultMap6.put("文化性", "中等");
            resultJson.put("文化性", "中等");
        }else {
            resultMap6.put("文化性", "高分");
            resultJson.put("文化性", "高分");
        }
        resultMapList.add(resultMap6);
        Map<String, String> resultMap7 = new LinkedHashMap<>();
        resultMap7.put("娱乐性得分", String.valueOf(score7));
        resultJson.put("娱乐性得分", String.valueOf(score7));
        if (score7 <= 3){
            resultMap7.put("娱乐性", "低分");
            resultJson.put("娱乐性", "低分");
        }else if (score7 <= 6 && score7 >= 4){
            resultMap7.put("娱乐性", "中等");
            resultJson.put("娱乐性", "中等");
        }else {
            resultMap7.put("娱乐性", "高分");
            resultJson.put("娱乐性", "高分");
        }
        resultMapList.add(resultMap7);
        Map<String, String> resultMap8 = new LinkedHashMap<>();
        resultMap8.put("道德宗教观得分", String.valueOf(score8));
        resultJson.put("道德宗教观得分", String.valueOf(score8));
        if (score8 <= 4){
            resultMap8.put("道德宗教观", "低分");
            resultJson.put("道德宗教观", "低分");
        }else if (score8 <= 7 && score8 >= 5){
            resultMap8.put("道德宗教观", "中等");
            resultJson.put("道德宗教观", "中等");
        }else {
            resultMap8.put("道德宗教观", "高分");
            resultJson.put("道德宗教观", "高分");
        }
        resultMapList.add(resultMap8);
        Map<String, String> resultMap9 = new LinkedHashMap<>();
        resultMap9.put("组织性得分", String.valueOf(score9));
        resultJson.put("组织性得分", String.valueOf(score9));
        if (score9 <= 5){
            resultMap9.put("组织性", "低分");
            resultJson.put("组织性", "低分");
        }else if (score9 <= 8 && score9 >= 6){
            resultMap9.put("组织性", "中等");
            resultJson.put("组织性", "中等");
        }else {
            resultMap9.put("组织性", "高分");
            resultJson.put("组织性", "高分");
        }
        resultMapList.add(resultMap9);
        Map<String, String> resultMap10 = new LinkedHashMap<>();
        resultMap10.put("控制性得分", String.valueOf(score10));
        resultJson.put("控制性得分", String.valueOf(score10));
        if (score10 <= 2){
            resultMap10.put("控制性", "低分");
            resultJson.put("控制性", "低分");
        }else if (score10 <= 5 && score10 >= 3){
            resultMap10.put("控制性", "中等");
            resultJson.put("控制性", "中等");
        }else {
            resultMap10.put("控制性", "高分");
            resultJson.put("控制性", "高分");
        }
        resultMapList.add(resultMap10);

        JSONObject returnJson = new JSONObject(true);
        returnJson.put("resultMapList",resultMapList);
        returnJson.put("scaleName", Constant.SHEET_NAME_FES_CV);
        returnJson.put("resultJson",resultJson);

        return returnJson;*/
    }

    /**
     * 通用计算量表所有维度分数，以及其维度所在分数段症状等
     *
     * @param df                用来设置小数位数，例如保留两位小数DecimalFormat df = new DecimalFormat("######0.00");
     * @param resultMap         用来向数据库存
     * @param resultJson        WebScaleResultController传递过来的评分规则等相关参数
     * @param dimensionEntities 量表维度list信息
     * @param dimensionScore    量表维度分数，需要和量表维度list对应
     * @param scaleMarksMap     评分规则的map
     * @param newResultDtos     各个维度的结论，等级解释等list
     * @param indicator         雷达图需要的维度以及最大值
     * @param reference         雷达图所需要的常模参考值
     * @param scoreList         雷达图需要的分数
     */
    public static void commonComputeDimensionScore(@Nullable DecimalFormat df, Map<String, String> resultMap, JSONObject resultJson, List<DimensionEntity> dimensionEntities, double[] dimensionScore, Map<String, List<ScaleMarksEntity>> scaleMarksMap, List<NewResultDto> newResultDtos, List<Map<String, Object>> indicator, List<String> reference, List<String> scoreList) {
        if (df == null) {
            df = new DecimalFormat("######0.00");
        }
        for (int y = 0; y < dimensionEntities.size(); y++) {
            Map<String, Object> indicatorMap = new HashMap<>();
            DimensionEntity dimensionEntity = dimensionEntities.get(y);
            NewResultDto newResultDto1 = new NewResultDto();
            //存放该维度总分
            resultMap.put(dimensionEntity.getName() + "得分", df.format(dimensionScore[y]));
            resultJson.put(dimensionEntity.getName() + "得分", df.format(dimensionScore[y]));
            newResultDto1.setScore(df.format(dimensionScore[y]));
            newResultDtos.add(newResultDto1);
            BigDecimal dimensionScoreDecimal = new BigDecimal(df.format(dimensionScore[y]));
            //获取当前维度的评分规则
            List<ScaleMarksEntity> scaleMarksEntities1 = scaleMarksMap.get(dimensionEntity.getName());
            if (scaleMarksEntities1 != null) {
                //循环遍历判断得分在哪个区间
                for (ScaleMarksEntity scaleMarksEntity : scaleMarksEntities1) {
                    BigDecimal decimalScoreEnd = new BigDecimal(scaleMarksEntity.getScoreEnd());
                    BigDecimal decimalScoreStart = new BigDecimal(scaleMarksEntity.getScoreStart());
                    if (dimensionScoreDecimal.compareTo(decimalScoreEnd) <= 0 && dimensionScoreDecimal.compareTo(decimalScoreStart) >= 0) {
                        putDimResult(resultMap, resultJson, dimensionEntity.getName(), newResultDto1, scaleMarksEntity);
                        //需要进行图表展示的维度放入map
                        if ("1".equals(dimensionEntity.getIsIconFlag())) {
                            indicatorMap.put("text", dimensionEntity.getName());
                            //将当前维度按照分数的结尾值来进行排序scaleMarksEntities1
                            List<ScaleMarksEntity> collect = scaleMarksEntities1.stream().sorted(Comparator.comparing(ScaleMarksEntity::getScoreEnd, Comparator.comparingInt(Integer::parseInt))
                                    .reversed()).collect(Collectors.toList());
                            //排序完成后第一个的scoreEnd最大，也就是该维度最大的
                            indicatorMap.put("max", collect.get(0).getScoreEnd());
                            indicator.add(indicatorMap);
                            if ("无".equals(scaleMarksEntity.getReference()) || "无".equals(scaleMarksEntity.getStandardDeviation())) {
                                //常模参考值设为0
                                reference.add("0");
                            } else {
                                reference.add(scaleMarksEntity.getReference());
                            }
                            scoreList.add(String.valueOf(dimensionScore[y]));
                        }
                    }
                }
            } else {
                //如果分数规则表中无维度，而维度表中有相应维度，则只显示维度分数
                newResultDto1.setFlag("无");
                newResultDto1.setImprovementSuggestions("无");
                newResultDto1.setName(dimensionEntity.getName());
                newResultDto1.setReference("无");
                newResultDto1.setNameExplain("无");
                newResultDto1.setSuggestion("无");
                newResultDto1.setIsTotalScoreExplain("否");
                newResultDto1.setSymptom("无");
            }
        }
    }

    /**
     * 通用计算单个维度分数，以及其维度所在分数段症状等
     *
     * @param df              默认为两位小数，可以为null，用来设置小数位数，例如保留两位小数DecimalFormat df = new DecimalFormat("######0.00");
     * @param resultMap       用来向数据库存
     * @param resultJson      WebScaleResultController传递过来的评分规则等相关参数
     * @param dimensionName   维度名称
     * @param dimensionScore  维度分数
     * @param isDimensionShow 是否在雷达图中展示
     * @param scaleMarksMap   评分规则的map
     * @param newResultDtos   各个维度的结论，等级解释等list
     * @param indicator       雷达图需要的维度以及最大值
     * @param reference       雷达图所需要的常模参考值
     * @param scoreList       雷达图需要的分数
     */
    public static void commonComputeDimensionScore(@Nullable DecimalFormat df, Map<String, String> resultMap, JSONObject resultJson, String dimensionName, double dimensionScore, Boolean isDimensionShow, Map<String, List<ScaleMarksEntity>> scaleMarksMap, List<NewResultDto> newResultDtos, List<Map<String, Object>> indicator, List<String> reference, List<String> scoreList) {
        if (df == null) {
            df = new DecimalFormat("######0.00");
        }
        Map<String, Object> indicatorMap = new HashMap<>();
        NewResultDto newResultDto = new NewResultDto();
        //存放该维度总分
        resultMap.put(dimensionName + "得分", df.format(dimensionScore));
        resultJson.put(dimensionName + "得分", df.format(dimensionScore));
        newResultDto.setScore(df.format(dimensionScore));
        newResultDtos.add(newResultDto);
        BigDecimal dimensionScoreDecimal = new BigDecimal(df.format(dimensionScore));
        //获取当前维度的评分规则
        List<ScaleMarksEntity> scaleMarksEntities1 = scaleMarksMap.get(dimensionName);
        if (scaleMarksEntities1 != null) {
            //循环遍历判断得分在哪个区间
            for (ScaleMarksEntity scaleMarksEntity : scaleMarksEntities1) {
                BigDecimal decimalScoreEnd = new BigDecimal(scaleMarksEntity.getScoreEnd());
                BigDecimal decimalScoreStart = new BigDecimal(scaleMarksEntity.getScoreStart());
                if (dimensionScoreDecimal.compareTo(decimalScoreEnd) <= 0 && dimensionScoreDecimal.compareTo(decimalScoreStart) >= 0) {
                    putDimResult(resultMap, resultJson, dimensionName, newResultDto, scaleMarksEntity);
                    //需要进行图表展示的维度放入map
                    if (isDimensionShow) {
                        indicatorMap.put("text", dimensionName);
                        //将当前维度按照分数的结尾值来进行排序scaleMarksEntities1
                        List<ScaleMarksEntity> collect = scaleMarksEntities1.stream().sorted(Comparator.comparing(ScaleMarksEntity::getScoreEnd, Comparator.comparingInt(Integer::parseInt))
                                .reversed()).collect(Collectors.toList());
                        //排序完成后第一个的scoreEnd最大，也就是该维度最大的
                        indicatorMap.put("max", collect.get(0).getScoreEnd());
                        indicator.add(indicatorMap);
                        if ("无".equals(scaleMarksEntity.getReference()) || "无".equals(scaleMarksEntity.getStandardDeviation())) {
                            //常模参考值设为0
                            reference.add("0");
                        } else {
                            reference.add(scaleMarksEntity.getReference());
                        }
                        scoreList.add(String.valueOf(dimensionScore));
                    }
                }
            }
        } else {
            //如果分数规则表中无维度，而维度表中有相应维度，则只显示维度分数
            newResultDto.setFlag("无");
            newResultDto.setImprovementSuggestions("无");
            newResultDto.setName(dimensionName);
            newResultDto.setReference("无");
            newResultDto.setNameExplain("无");
            newResultDto.setSuggestion("无");
            newResultDto.setIsTotalScoreExplain("否");
            newResultDto.setSymptom("无");
        }
    }


    private static void putDimResult(Map<String, String> resultMap0, JSONObject resultJson, String dimensionName, NewResultDto newResultDto1, ScaleMarksEntity scaleMarksEntity) {
        resultMap0.put(dimensionName + "解释", scaleMarksEntity.getSymptom());
        resultJson.put(dimensionName + "解释", scaleMarksEntity.getSymptom());
        resultMap0.put(dimensionName + "解读", scaleMarksEntity.getNameExplain());
        resultJson.put(dimensionName + "解读", scaleMarksEntity.getNameExplain());
        resultMap0.put(dimensionName + "说明", scaleMarksEntity.getImprovementSuggestions());
        resultJson.put(dimensionName + "说明", scaleMarksEntity.getImprovementSuggestions());
        resultMap0.put(dimensionName + "建议", scaleMarksEntity.getSuggestion());
        resultJson.put(dimensionName + "建议", scaleMarksEntity.getSuggestion());
        BeanUtils.copyProperties(scaleMarksEntity, newResultDto1);
    }

    private WarnEntity createWarnEntry(String flag,  String institutionNo, String userId, String id, double score) {
        WarnEntity warn = new WarnEntity();

        warn.setFlag(flag);
        warn.setInstitutionNo(institutionNo);
        warn.setUserId(userId);
        warn.setDimensionId(id);
        warn.setScore(String.valueOf(score));
        return warn;
    }

}
