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 PEBSScale extends BaseScale {
    public PEBSScale(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 score = 0;
        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("；");
                    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.valueOf(answerEntity.getScore());
                                }
                            }
                            break;
                        }
                    }
                }

                //计算总分
                for (AnswerEntity answerEntity : answerEntities) {
                    if (answerEntity.getQuestionNo().equals(jsonObject1.getString("questionNo"))) {
                        if (answerEntity.getName().replaceAll("\\s", "").equals(jsonObject1.getString("checkItems").replaceAll("\\s", ""))) {
                            score += Double.parseDouble(answerEntity.getScore());
                            break;
                        }
                    }
                }

            }
            resultMap0.put("总分", String.valueOf(score));
            resultJson.put("总分", String.valueOf(score));
            //计算每个维度的平均分
            DecimalFormat df = new DecimalFormat("######0.00");
            double[] Mdscore = new double[dimensionScore.length];
            for (int i=0;i<dimensionScore.length;i++){
                System.out.println(dimensionScore[i]+">>>>>>>>>>>>>>>>>>");
                if(i==0){
                    Mdscore[0]= Double.parseDouble(df.format(dimensionScore[0]/7.0));
                }else if(i==1){
                    Mdscore[1]= Double.parseDouble(df.format(dimensionScore[1]/6.0));
                }else {
                    Mdscore[i]= Double.parseDouble(df.format(dimensionScore[i]/5.0));
                }
            }

            Map<String,Double> dimensionScoreMap = new HashMap<>();
            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("情绪性进食维度均分", String.valueOf(Mdscore[5]));
            resultMap0.put("主动进食能力维度均分", String.valueOf(Mdscore[6]));
            resultMap0.put("均分解释：", "各维度得分为所包含条目得分的均值，得分越高，存在饮食行为问题越严重。");

            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("情绪性进食维度均分", String.valueOf(Mdscore[5]));
            resultJson.put("主动进食能力维度均分", String.valueOf(Mdscore[6]));
            resultJson.put("均分解释：", "各维度得分为所包含条目得分的均值，得分越高，存在饮食行为问题越严重。");

            dimensionScoreMap.put("2c91f37d7bec0878017c43e9b5344a47", Mdscore[0]);
            dimensionScoreMap.put("2c91f37d7bec0878017c43e9b56c4a48", Mdscore[1]);
            dimensionScoreMap.put("2c91f37d7bec0878017c43e9b5a44a49", Mdscore[2]);
            dimensionScoreMap.put("2c91f37d7bec0878017c43e9b5dc4a4a", Mdscore[3]);
            dimensionScoreMap.put("2c91f37d7bec0878017c43e9b6144a4b", Mdscore[4]);
            dimensionScoreMap.put("2c91f37d7bec0878017c43e9b64c4a4c", Mdscore[5]);
            dimensionScoreMap.put("2c91f37d7bec0878017c43e9b6844a4d", Mdscore[6]);
            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;

    }
}
