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 lhh
 * @description:中学生龋齿简易风险自评问卷
 * @date 2021/11/04 15:55
 */
public class SCRSQScale extends BaseScale {
    public SCRSQScale(JSONArray jsonArray, JSONObject resultJson) {
        super(jsonArray, resultJson);
    }
    public JSONObject scaleCalculate() throws Exception{
        //总分
        double score = 0;
        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");
        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());
                                   // System.out.println("维度： "+y+"  得分"+ Double.valueOf(answerEntity.getScore())+"  题号："+answerEntity.getQuestionNo());
                                }
                            }
                            break;
                        }
                    }
                }

                //计算总分
                for (AnswerEntity answerEntity:answerEntities){
                    if (answerEntity.getQuestionNo().equals(String.valueOf(i+1))){
                        if (answerEntity.getName().equals(jsonObject1.getString("checkItems"))){
                            score += Integer.valueOf(answerEntity.getScore());
                        }
                    }
                }
            }
            System.out.println(">>>>>>>>>>>"+score);

            //计算每个维度的平均分
            DecimalFormat df = new DecimalFormat("######0.00");

            dimensionScore[0]= Double.parseDouble(df.format(dimensionScore[0]));
            dimensionScore[1]= Double.parseDouble(df.format(dimensionScore[1]));
            score=Double.parseDouble(df.format((score-8)/24.0));
            resultMap0.put("因子一分", String.valueOf(dimensionScore[0]));
            resultMap0.put("因子二分", String.valueOf(dimensionScore[1]));
            resultMap0.put("总分", String.valueOf(score));
            resultMap0.put("总分解释：", "①没有龋齿风险，标准得分介于0～0.25；②有轻度龋齿风险，标准得分介于0.25~0.50；③有中度龋齿风险，标准得分介于0.50~0.75；④有重度龋齿风险，标准得分介于0.75～1.00。");


            resultJson.put("因子一分", String.valueOf(dimensionScore[0]));
            resultJson.put("因子二分", String.valueOf(dimensionScore[1]));
            resultJson.put("总分", String.valueOf(score));
            resultJson.put("总分解释：", "①没有龋齿风险，标准得分介于0～0.25；②有轻度龋齿风险，标准得分介于0.25~0.50；③有中度龋齿风险，标准得分介于0.50~0.75；④有重度龋齿风险，标准得分介于0.75～1.00。");



            resultMapList.add(resultMap0);
        }

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

        return returnJson;

    }



}
