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.ScaleMarksEntity;

import java.text.DecimalFormat;
import java.util.*;

/**
 * @author lhl
 * @description:教师的压力评估
 * @date 2021/11/01
 */
public class TSAScale extends BaseScale {
    public TSAScale(JSONArray jsonArray, JSONObject resultJson) {
        super(jsonArray, resultJson);
    }
    public JSONObject scaleCalculate() throws Exception{
        //总分
        double score = 0;
        Map<String, String> resultMap0 = new LinkedHashMap<>();
        List<Map<String,String>> resultMapList = new ArrayList<>();
        //获取答案列表
        List<AnswerEntity> answerEntities = (List<AnswerEntity>) resultJson.get("answerEntities");
        //获取评分规则列表
        List<ScaleMarksEntity> scaleMarksEntities = (List<ScaleMarksEntity>) resultJson.get("scaleMarksEntities");
        //获取维度信息列表
        List<DimensionEntity> dimensionEntities = (List<DimensionEntity>) resultJson.get("dimensionEntities");

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

        JSONObject iconInfo = new JSONObject();
        //雷达图需要的维度以及最大值
        List<Map<String, Object>> indicator = new LinkedList<>();
        //雷达图需要的分数
        LinkedList<String> scoreList = new LinkedList<>();
        //返回值以及存数据库格式
        List<NewResultDto> newResultDtos = new ArrayList<>();
        if (dimensionEntities.size() > 0){
            //计算每个维度得分
            double[] dimensionScore = new double[dimensionEntities.size()];
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                for (int y = 0; y < dimensionEntities.size(); y++) {
                    //for (DimensionEntity dimensionEntity:dimensionEntities){
                    DimensionEntity dimensionEntity = dimensionEntities.get(y);
                    String questionNos = dimensionEntity.getQuestionNo();
                    String[] questionNo = questionNos.split("；");
                    for (String question:questionNo){
                        if (question.equals(jsonObject1.getString("questionNo"))){
                            for (AnswerEntity answerEntity:answerEntities){
                                ///只用比较选项内容，进而判定该题目的分数值
                                if (answerEntity.getQuestionNo().equals(question) &&answerEntity.getName().equals(jsonObject1.getString("checkItems"))){
                                    dimensionScore[y] += Double.valueOf(answerEntity.getScore());
                                    System.out.println(Double.valueOf(answerEntity.getScore())+">>>>>>>>>>>>>>>>>>"+jsonObject1.getString("checkItems")+"维度为："+y);
                                }
                            }
                            break;
                        }
                    }
                }

                //计算总分
               /* for (AnswerEntity answerEntity:answerEntities){
                    if (answerEntity.getQuestionNo().equals(String.valueOf(i+1))){
                        if (answerEntity.getName().equals(jsonObject1.getString("checkItems"))){
                            score += Integer.valueOf(answerEntity.getScore());
                        }
                    }
                }*/
            }

           /* resultMap0.put("DBS总分", String.valueOf(score));
            resultJson.put("DBS总分", String.valueOf(score));*/
            //计算每个维度的平均分
            DecimalFormat df = new DecimalFormat("######0.00");
            double[] Mdscore = new double[dimensionScore.length];

            Mdscore[0]= Double.parseDouble(df.format((dimensionScore[0]+dimensionScore[1]+dimensionScore[2])/3.0));
            Mdscore[1]= Double.parseDouble(df.format((dimensionScore[3]+dimensionScore[4]+dimensionScore[5]+dimensionScore[6])/4.0));
            Mdscore[2]= Double.parseDouble(df.format((Mdscore[0]+Mdscore[1])/2.0));
            resultMap0.put("社会生活维度总分", String.valueOf(df.format(Mdscore[0])));
            resultMap0.put("个性维度总分", String.valueOf(df.format(Mdscore[1])));
            resultMap0.put("压力综合指数", String.valueOf(df.format(Mdscore[2])));
            resultMap0.put("维度的压力指数解释", "分数越高，说明这方面的压力越大，对心理健康越不利。");
            resultJson.put("社会生活维度总分", String.valueOf(df.format(Mdscore[0])));
            resultJson.put("个性维度总分", String.valueOf(df.format(Mdscore[1])));
            resultJson.put("维度的压力指数解释", "分数越高，说明这方面的压力越大，对心理健康越不利。");



            newResultDtos.add(new NewResultDto("压力综合指数", String.valueOf(df.format(Mdscore[2])), "无", "分数越高，说明这方面的压力越大，对心理健康越不利。",
                    "无", "无", "无", "分数越高，说明这方面的压力越大，对心理健康越不利。", "是"));
            newResultDtos.add(new NewResultDto("社会生活维度总分", String.valueOf(df.format(Mdscore[0])), "无", "无",
                    "无", "无", "无", "无", "否"));
            newResultDtos.add(new NewResultDto("个性维度总分", String.valueOf(df.format(Mdscore[1])), "无", "无",
                    "无", "无", "无", "无", "否"));

            //雷达图数据
            Map<String, Object> indicatorMap = new HashMap<>();
            indicatorMap.put("text", "个性维度总分");
            indicatorMap.put("max", "8");
            indicator.add(indicatorMap);
            Map<String, Object> indicatorMap1 = new HashMap<>();
            scoreList.add(String.valueOf(Mdscore[1]));
            indicatorMap1.put("text", "社会生活维度总分");
            indicatorMap1.put("max", "6");
            scoreList.add(String.valueOf(Mdscore[0]));
            indicator.add(indicatorMap1);
            Map<String, Object> indicatorMap2 = new HashMap<>();
            indicatorMap2.put("text", "压力综合指数");
            indicatorMap2.put("max", "7");
            scoreList.add(String.valueOf(Mdscore[2]));
            indicator.add(indicatorMap2);

            //需要图表展示的
            iconInfo.put("indicator", indicator);
            iconInfo.put("scoreList", scoreList);
            newResult.put("iconInfo", iconInfo);
            newResult.put("result", newResultDtos);


            resultMapList.add(resultMap0);
        }



        JSONObject returnJson = new JSONObject(true);
        returnJson.put("resultMapList",resultMapList);
        returnJson.put("scaleName",resultJson.getString("scaleName"));
        returnJson.put("resultJson",resultJson);
        returnJson.put("newResultJson", newResult);

        return returnJson;

    }


}
