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.DimensionOrgEntity;
import com.rf.psychological.scale.dao.model.ScaleMarksEntity;
import com.rf.psychological.scale.dao.model.WarnEntity;
import com.rf.psychological.scale.service.DimensionOrgService;

import java.text.DecimalFormat;
import java.util.*;

/**
 * @author zsy
 * @description:SF-12 生活质量量表保存处理接口
 * @date 2021/7/20 15:55
 */
public class SF12Scale extends BaseScale {


    public SF12Scale(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<DimensionEntity> dimensionEntities = (List<DimensionEntity>) resultJson.get("dimensionEntities");
        double[] dimensionScore = new double[dimensionEntities.size()];
        Map<String,Double> dimensionScoreMap = new HashMap<>();
        if (dimensionEntities.size() > 0) {
            //计算每个维度得分
            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.replaceAll("\\s", "").split("；");
                    for (String question : questionNo) {
                        if (question.equals(jsonObject1.getString("questionNo"))) {
                            for (AnswerEntity answerEntity : answerEntities) {
                                if (answerEntity.getQuestionNo().equals(question) && answerEntity.getName().replaceAll("\\s", "").equals(jsonObject1.getString("checkItems").replaceAll("\\s", ""))) {
                                    dimensionScore[y] += Double.parseDouble(answerEntity.getScore());
                                }
                            }
                            break;
                        }
                    }
                }
                //计算总分
                for (AnswerEntity answerEntity : answerEntities) {
                    if (answerEntity.getQuestionNo().equals(String.valueOf(i + 1))) {
                        if (answerEntity.getName().replaceAll("\\s", "").equals(jsonObject1.getString("checkItems").replaceAll("\\s", ""))) {
                            score += Integer.parseInt(answerEntity.getScore());
                        }
                    }
                }
            }

            resultMap0.put("总分", String.valueOf(score));
            resultJson.put("总分", String.valueOf(score));

            for (int y = 0; y < dimensionEntities.size(); y++) {
                DimensionEntity dimensionEntity = dimensionEntities.get(y);
                DecimalFormat df = new DecimalFormat("######0.00");
                /*总体健康（GH）	1	1-5	（实际得分-1）/4*100
                生理功能（PF）	3；4	2-6	（实际得分-2）/4*100
                生理职能（RP）	5；6	2-4	（实际得分-2）/2*100
                躯体疼痛（BP）	9	1-5	（实际得分-1）/4*100
                活力（VT）	11	1-6	（实际得分-1）/5*100
                社会功能（SF）	13	1-6	（实际得分-1）/5*100
                情感职能（RE）	7；8	2-4	（实际得分-2）/2*100
                心理健康（MH）	10；12	2-12	（实际得分-2）/10*100
                生理健康（PCS）	1；3；4；5；6；9	6-20	（实际得分-1）/4*100
                心理健康（MCS）	7；8；10；11；12；13	6-28	（实际得分-1）/4*100
*/
                double scoreTemp = 0;
                if (dimensionEntity.getName().equals("总体健康（GH）")){
                    scoreTemp = Double.parseDouble(df.format((dimensionScore[y]-1) / 4 * 100));
                    resultMap0.put(dimensionEntity.getName() + "得分", String.valueOf(scoreTemp));
                    resultJson.put(dimensionEntity.getName() + "得分", String.valueOf(scoreTemp));
                }
                if (dimensionEntity.getName().equals("生理功能（PF）")){
                    scoreTemp = Double.parseDouble(df.format((dimensionScore[y]-2) / 4 * 100));
                    resultMap0.put(dimensionEntity.getName() + "得分", String.valueOf(scoreTemp));
                    resultJson.put(dimensionEntity.getName() + "得分", String.valueOf(scoreTemp));
                }
                if (dimensionEntity.getName().equals("生理职能（RP）")){
                    scoreTemp = Double.parseDouble(df.format((dimensionScore[y]-2) / 2 * 100));
                    resultMap0.put(dimensionEntity.getName() + "得分", String.valueOf(scoreTemp));
                    resultJson.put(dimensionEntity.getName() + "得分", String.valueOf(scoreTemp));
                }
                if (dimensionEntity.getName().equals("躯体疼痛（BP）")){
                    scoreTemp =  Double.parseDouble(df.format((dimensionScore[y]-1) / 4 * 100));
                    resultMap0.put(dimensionEntity.getName() + "得分", String.valueOf(scoreTemp));
                    resultJson.put(dimensionEntity.getName() + "得分", String.valueOf(scoreTemp));
                }
                if (dimensionEntity.getName().equals("活力（VT）")){
                    scoreTemp =  Double.parseDouble(df.format((dimensionScore[y]-1) / 5 * 100));
                    resultMap0.put(dimensionEntity.getName() + "得分", String.valueOf(scoreTemp));
                    resultJson.put(dimensionEntity.getName() + "得分", String.valueOf(scoreTemp));
                }
                if (dimensionEntity.getName().equals("社会功能（SF）")){
                    scoreTemp =  Double.parseDouble(df.format((dimensionScore[y]-1) / 5 * 100));
                    resultMap0.put(dimensionEntity.getName() + "得分", String.valueOf(scoreTemp));
                    resultJson.put(dimensionEntity.getName() + "得分", String.valueOf(scoreTemp));
                }
                if (dimensionEntity.getName().equals("情感职能（RE）")){
                    scoreTemp =  Double.parseDouble(df.format((dimensionScore[y]-2) / 2 * 100));
                    resultMap0.put(dimensionEntity.getName() + "得分", String.valueOf(scoreTemp));
                    resultJson.put(dimensionEntity.getName() + "得分", String.valueOf(scoreTemp));
                }
                if (dimensionEntity.getName().equals("心理健康（MH）")){
                    scoreTemp =  Double.parseDouble(df.format((dimensionScore[y]-2) / 10 * 100));
                    resultMap0.put(dimensionEntity.getName() + "得分", String.valueOf(scoreTemp));
                    resultJson.put(dimensionEntity.getName() + "得分", String.valueOf(scoreTemp));
                }
                if (dimensionEntity.getName().equals("生理健康（PCS）")){
                    scoreTemp =  Double.parseDouble(df.format((dimensionScore[y]-1) / 4 * 100));
                    resultMap0.put(dimensionEntity.getName() + "得分", String.valueOf(scoreTemp));
                    resultJson.put(dimensionEntity.getName() + "得分", String.valueOf(scoreTemp));
                }
                if (dimensionEntity.getName().equals("心理健康（MCS）")){
                    scoreTemp =  Double.parseDouble(df.format((dimensionScore[y]-1) / 4 * 100));
                    resultMap0.put(dimensionEntity.getName() + "得分", String.valueOf(scoreTemp));
                    resultJson.put(dimensionEntity.getName() + "得分", String.valueOf(scoreTemp));
                }
                dimensionScoreMap.put(dimensionEntity.getId(),dimensionScoreMap.get(dimensionEntity.getId())==null?scoreTemp:(dimensionScoreMap.get(dimensionEntity.getId()) +scoreTemp));
            }
            if(dimensionScoreMap.size()>0){
                resultJson.put("dimensionScoreMap",dimensionScoreMap);
            }
            resultMapList.add(resultMap0);
        }
        JSONObject returnJson = new JSONObject(true);
        returnJson.put("resultMapList", resultMapList);
        returnJson.put("resultJson", resultJson);

        return returnJson;
    }

}
