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.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
 * @author xbw
 * @description:优秀运动员意志品质评价量表
 * @date 2021/11/4 10:00
 */
public class EVQEAScale extends BaseScale{
    public EVQEAScale(JSONArray jsonArray, JSONObject resultJson) {
        super(jsonArray, resultJson);
    }

    public  JSONObject scaleCalculate() throws Exception {

        //总分
        double score = 0;
        double ave = 0;
        double SDE = 0;
        Map<String, String> resultMap0 = new LinkedHashMap<>();
        List<Map<String,String>> resultMapList = new ArrayList<>();
        DecimalFormat df = new DecimalFormat("######0.00");
        //获取答案列表
        List<AnswerEntity> answerEntities = (List<AnswerEntity>) resultJson.get("answerEntities");
        //获取评分规则列表
        List<ScaleMarksEntity> scaleMarksEntities = (List<ScaleMarksEntity>) resultJson.get("scaleMarksEntities");
        //获取维度信息列表
        List<DimensionEntity> dimensionEntities = (List<DimensionEntity>) resultJson.get("dimensionEntities");

        int b = 0;
        if (dimensionEntities.size() > 0){
            //计算每个维度得分
            double[] dimensionScore = new double[dimensionEntities.size()];
            double[][] EveryScore = new double[dimensionEntities.size()][15];
            for (int y = 0; y < dimensionEntities.size(); y++) {
                DimensionEntity dimensionEntity = dimensionEntities.get(y);
                String questionNos = dimensionEntity.getQuestionNo();
                String[] questionNo = questionNos.split("；");
                b = 0;
                for (String question:questionNo){
                    JSONObject jsonObject1 = jsonArray.getJSONObject(Integer.valueOf(question)-1);
                    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());
                                EveryScore[y][b] = Double.valueOf(answerEntity.getScore());
                                b = b + 1;
                                break;
                            }
                        }
                    }
                }
                String dimensionSName1 = dimensionEntity.getName().concat("维度总分");
                resultMap0.put(dimensionSName1, String.valueOf(dimensionScore[y]));
                resultJson.put(dimensionSName1, String.valueOf(dimensionScore[y]));

                ave = dimensionScore[y]/questionNo.length;

                for(int a = 0;a < b;a++){
                    SDE = SDE + Math.pow((EveryScore[y][a]-ave),2);
                }
                SDE = Math.sqrt(SDE/b);
                ave = Double.parseDouble(df.format(ave));
                SDE = Double.parseDouble(df.format(SDE));
                dimensionSName1 = dimensionEntity.getName().concat("维度平均分");
                resultMap0.put(dimensionSName1, String.valueOf(ave));
                resultJson.put(dimensionSName1, String.valueOf(ave));
                dimensionSName1 = dimensionEntity.getName().concat("维度标准差");
                resultMap0.put(dimensionSName1, String.valueOf(SDE));
                resultJson.put(dimensionSName1, String.valueOf(SDE));
            }
                //计算总分
            double[] SDES = new double[jsonArray.size()];
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                for (AnswerEntity answerEntity:answerEntities){
                    if (answerEntity.getQuestionNo().equals(String.valueOf(i+1))){
                        if (answerEntity.getName().equals(jsonObject1.getString("checkItems"))){
                            score += Double.valueOf(answerEntity.getScore());
                            SDES[i] = Double.valueOf(answerEntity.getScore());
                        }
                    }
                }
            }
            resultMap0.put("EVQEA总分", String.valueOf(score));
            resultJson.put("EVQEA总分", String.valueOf(score));
            double aveS = score / 38;
            SDE = 0;
            for(int a = 0;a < jsonArray.size();a++){
                SDE = SDE + Math.pow((SDES[a]-aveS),2);
            }
            SDE = Math.sqrt(SDE/jsonArray.size());
            aveS = Double.parseDouble(df.format(aveS));
            SDE = Double.parseDouble(df.format(SDE));
            resultMap0.put("EVQEA平均分", String.valueOf(aveS));
            resultJson.put("EVQEA平均分", String.valueOf(aveS));
            resultMap0.put("EVQEA标准差", String.valueOf(SDE));
            resultJson.put("EVQEA标准差", String.valueOf(SDE));

            resultMapList.add(resultMap0);
        }

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

        return returnJson;
    }
}
