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 21:30
 */
public class ABEScale extends BaseScale{
    public ABEScale(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");
        double sum = 0;
        DecimalFormat df = new DecimalFormat("######0.00");
        if (dimensionEntities.size() > 0){
            //计算每个维度得分
            double[] dimensionScore = new double[dimensionEntities.size()];
            for (int y = 0; y < dimensionEntities.size(); y++) {
                DimensionEntity dimensionEntity = dimensionEntities.get(y);
                sum = 0;
                String questionNos = dimensionEntity.getQuestionNo();
                String[] questionNo = questionNos.split("；");
                for (String question:questionNo){
                    JSONObject jsonObject1 = jsonArray.getJSONObject(Integer.valueOf(question)-1);
                    for (AnswerEntity answerEntity:answerEntities){
                        if (answerEntity.getQuestionNo().equals(question) && answerEntity.getName().equals(jsonObject1.getString("checkItems"))){
                            dimensionScore[y] += Double.valueOf(answerEntity.getScore());
                            sum = sum + 1;
                        }
                    }
                }
                //计算当前维度平均分
                dimensionScore[y] = Double.parseDouble(df.format(dimensionScore[y]/sum));
                String dimensionSName1 = dimensionEntity.getName().concat("平均分");
                resultMap0.put(dimensionSName1, String.valueOf(dimensionScore[y]));
                resultJson.put(dimensionSName1, String.valueOf(dimensionScore[y]));
            }

            resultMapList.add(resultMap0);
        }

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

        return returnJson;
    }
}
