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 lhh
 * @description:驾驶危险行为量表
 * @date 2021/11/04 15:55
 */

public class DDBS2Scale extends BaseScale{
    public DDBS2Scale(JSONArray jsonArray, JSONObject resultJson) {
        super(jsonArray, resultJson);
    }
    public JSONObject scaleCalculate() throws Exception{

        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){
            //计算每个维度得分
            System.out.println(">>>>>>>>>>>>"+dimensionEntities.size());
            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());
                                    System.out.println("维度："+y+">>>>>>>>"+"题号"+answerEntity.getQuestionNo()+"》》》》得分："+Double.valueOf(answerEntity.getScore()));
                                }
                            }
                            break;
                        }
                    }
                }

            }
            System.out.println(dimensionScore[0]+">>>>>>>>>"+dimensionScore[1]);
            //计算每个维度的平均分
            DecimalFormat df = new DecimalFormat("######0.00");
            double[] Mdscore = new double[dimensionScore.length];
            Mdscore[0]= Double.parseDouble(df.format(dimensionScore[0]/5.0));
            Mdscore[1]= Double.parseDouble(df.format(dimensionScore[1]/6.0));
            Mdscore[2]= Double.parseDouble(df.format(dimensionScore[2]/7.0));
            dimensionScoreMap.put("2c91f37d7bec0878017c43e742f348e8",Mdscore[0]);
            dimensionScoreMap.put("2c91f37d7bec0878017c43e7432848e9",Mdscore[1]);
            dimensionScoreMap.put("2c91f37d7bec0878017c43e7435e48ea",Mdscore[2]);

            resultMap0.put("驾驶超速维度均分", String.valueOf(Mdscore[0]));
            resultMap0.put("违反交通规则维度均分", String.valueOf(Mdscore[1]));
            resultMap0.put("过度自信维度均分", String.valueOf(Mdscore[2]));
            resultMap0.put("均分解释：", "维度得分越高，表明驶员在该维度上的行为倾向越明显。");

            resultJson.put("驾驶超速维度均分", String.valueOf(Mdscore[0]));
            resultJson.put("违反交通规则维度均分", String.valueOf(Mdscore[1]));
            resultJson.put("过度自信维度均分", String.valueOf(Mdscore[2]));
            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;

    }

}
