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 xbw
 * @description:成就倾同个体差异问卷
 * @date 2021/10/31 11:30
 */
public class IDIATScale extends  BaseScale{
    public IDIATScale(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){
            //计算每个维度得分
            double[] dimensionScore = new double[dimensionEntities.size()];
            for (int y = 0; y < dimensionEntities.size(); y++) {
                DimensionEntity dimensionEntity = dimensionEntities.get(y);
                String questionNos = dimensionEntity.getQuestionNo();
                String[] questionNo = questionNos.split("；");
                for (String question:questionNo){
                    JSONObject jsonObject1 = jsonArray.getJSONObject(Integer.parseInt(question)-1);
                    for (AnswerEntity answerEntity:answerEntities){
                        if (answerEntity.getQuestionNo().equals(question) && answerEntity.getName().equals(jsonObject1.getString("checkItems"))){
                            dimensionScore[y] += Double.parseDouble(answerEntity.getScore());
                            dimensionScoreMap.put(dimensionEntity.getId(),dimensionScore[y]);
                        }
                    }
                }
                String dimensionSName1 = dimensionEntity.getName().concat("总分");
                resultMap0.put(dimensionSName1, String.valueOf(dimensionScore[y]));
                resultJson.put(dimensionSName1, String.valueOf(dimensionScore[y]));
            }
            String dimensionSName2 = new String("成就动机总分");
            double res = dimensionScore[0]-dimensionScore[1];
            resultMap0.put(dimensionSName2, String.valueOf(res));
            resultJson.put(dimensionSName2, String.valueOf(res));
            resultJson.put("dimensionScoreMap",dimensionScoreMap);
            resultMapList.add(resultMap0);
        }

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

        return returnJson;
    }
}
