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

import cn.hutool.core.map.BiMap;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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/10/25 15:55
 */
public class TSCSScale extends BaseScale {
    public TSCSScale(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,Double> dimensionScoreMap = new HashMap<>();
      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.parseDouble(answerEntity.getScore());
                              }
                          }
                          break;
                      }
                  }
              }
              //计算总分
              for (AnswerEntity answerEntity:answerEntities){
                  if (answerEntity.getQuestionNo().equals(String.valueOf(i+1))){
                      if (answerEntity.getName().equals(jsonObject1.getString("checkItems"))){
                          score += Integer.parseInt(answerEntity.getScore());
                      }
                  }
              }
          }

          resultMap0.put("TSCS总分", String.valueOf(score));
          resultJson.put("TSCS总分", String.valueOf(score));
          //计算每个维度的平均分
          DecimalFormat df = new DecimalFormat("######0.00");
          double[] Mdscore = new double[dimensionScore.length];
          for (int y = 0; y < dimensionScore.length; y++) {
              Mdscore[y]= Double.parseDouble(df.format(dimensionScore[y]/4.0));
          }


          resultMap0.put("书面文化均分", String.valueOf(Mdscore[0]));
          resultMap0.put("听觉文化均分", String.valueOf(Mdscore[1]));
          resultMap0.put("口头文化均分", String.valueOf(Mdscore[2]));
          resultMap0.put("向外型文化均分", String.valueOf(Mdscore[3]));
          resultMap0.put("责任心文化均分", String.valueOf(Mdscore[4]));
          resultMap0.put("内向型文化均分", String.valueOf(Mdscore[5]));

          resultJson.put("书面文化均分", String.valueOf(Mdscore[0]));
          resultJson.put("听觉文化均分", String.valueOf(Mdscore[1]));
          resultJson.put("口头文化均分", String.valueOf(Mdscore[2]));
          resultJson.put("向外型文化均分", String.valueOf(Mdscore[3]));
          resultJson.put("责任心文化均分", String.valueOf(Mdscore[4]));
          resultJson.put("内向型文化均分", String.valueOf(Mdscore[5]));

          dimensionScoreMap.put("2c91f37d7bec0878017c43d69e7b3fec",Mdscore[0]);
          dimensionScoreMap.put("2c91f37d7bec0878017c43d69ebf3fed",Mdscore[1]);
          dimensionScoreMap.put("2c91f37d7bec0878017c43d69f043fee",Mdscore[2]);
          dimensionScoreMap.put("2c91f37d7bec0878017c43d69f493fef",Mdscore[3]);
          dimensionScoreMap.put("2c91f37d7bec0878017c43d69f8d3ff0",Mdscore[4]);
          dimensionScoreMap.put("2c91f37d7bec0878017c43d69fd23ff1",Mdscore[5]);



          resultMapList.add(resultMap0);
          resultJson.put("dimensionScoreMap",dimensionScoreMap);
      }

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

      return returnJson;

  }

}
