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

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 zsy
 * @description:突发性公共卫生事件心理问卷
 * @date 2021/11/03 15:55
 */
public class PQEEPHScale extends BaseScale{
    public PQEEPHScale(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("；");
                   // System.out.println(y+"维度的题号数量>>>>>>>>>>>>>>>>>"+ questionNo.length);
                    for (String question:questionNo){
                      //  System.out.println("维度>>>>>>>>>>>>>>>>>"+y+"  题号"+question);
                        if (question.equals(jsonObject1.getString("questionNo"))){
                            System.out.println("维度>>>>>>>>>>>>>>>>>"+y+"  题号"+question);
                            for (AnswerEntity answerEntity:answerEntities){
                                ///只用比较选项内容，进而判定该题目的分数值
                                if (answerEntity.getQuestionNo().equals(question) &&answerEntity.getName().equals(jsonObject1.getString("checkItems"))){
                                    dimensionScore[y] += Double.parseDouble(answerEntity.getScore());
                                  //  System.out.println("维度："+y+"answerEntity的答案与分数："+answerEntity.getName()+">>>>>>>>>"+Double.valueOf(answerEntity.getScore()));
                                }
                            }
                            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];
            System.out.println(dimensionScore[1]+">>>>>>>>>"+dimensionScore[2]+">>>>>>>>>"+dimensionScore[3]+">>>>>>>>>"+dimensionScore[4]+">>>>>>>>>");
            Mdscore[0]= Double.parseDouble(df.format((dimensionScore[0]/6.0)));
            Mdscore[1]= Double.parseDouble(df.format((dimensionScore[1]/5.0)));
            Mdscore[2]= Double.parseDouble(df.format((dimensionScore[2]/6.0)));
            Mdscore[3]= Double.parseDouble(df.format((dimensionScore[3]/6.0)));
            Mdscore[4]= Double.parseDouble(df.format((dimensionScore[4]/2.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("结果解释", "结果解释某一维度评分越高，说明被试在该维度上情绪反应越严重。");
            dimensionScoreMap.put("2c91f37d7bec0878017c43e372e2475d",Mdscore[0]);
            dimensionScoreMap.put("2c91f37d7bec0878017c43e37307475e",Mdscore[1]);
            dimensionScoreMap.put("2c91f37d7bec0878017c43e3732c475f",Mdscore[2]);
            dimensionScoreMap.put("2c91f37d7bec0878017c43e373514760",Mdscore[3]);
            dimensionScoreMap.put("2c91f37d7bec0878017c43e373764761",Mdscore[4]);
            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("结果解释", "结果解释某一维度评分越高，说明被试在该维度上情绪反应越严重。");



            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;

    }

}
