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/1 20:00
 */
public class IQCODEScale extends BaseScale{
    public IQCODEScale(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<>();

        double score = 0;
        //获取答案列表
        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.0;
        if (jsonArray.size() > 0){
            //计算总分
            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)) && answerEntity.getName().equals(jsonObject1.getString("checkItems"))){
                        if (Double.valueOf(answerEntity.getScore()) > 0){
                            score += Double.valueOf(answerEntity.getScore());
                            sum = sum + 1;
                        }
                    }
                }
            }
            DecimalFormat df = new DecimalFormat("######0.00");
            score = Double.parseDouble(df.format(score/sum));
            resultMap0.put("IQCODE得分", String.valueOf(score));
            resultJson.put("IQCCODE得分", String.valueOf(score));
            if (score > 3.3){
                resultMap0.put("IQCODE得分 > 3.3", "需进行下一步MMSE筛查");
                resultJson.put("IQCCODE得分 > 3.3", "需进行下一步MMSE筛查");
            }

            resultMapList.add(resultMap0);
        }

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

        return returnJson;
    }
}
