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.scale.dao.model.ScaleMarksEntity;
import com.rf.psychological.utils.Constant;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zsy
 * @description:儿童少年生活质量量表
 * @date 2021/7/20 15:55
 */
public class QLSCAScale extends BaseScale{

    public QLSCAScale(JSONArray jsonArray, JSONObject resultJson) {
        super(jsonArray, resultJson);
    }

    public  JSONObject scaleCalculate() throws Exception {

        // 自定义路径使用
        /*ScaleUtil.getScaleDll("QLSCAScale.dll");
        System.out.println("JSONArray:"+jsonArray);
        System.out.println("resultJson:"+resultJson);
        XScale xScale = new XScale(jsonArray,resultJson);
        String result = xScale.QLSCAScale(jsonArray.toString(), resultJson.toString());
        //System.out.println("dllresult:"+result);
        //解决排序问题
        LinkedHashMap<String, Object> jsonMap = JSON.parseObject(result,LinkedHashMap.class, Feature.OrderedField);
        JSONObject jsonObject = new JSONObject(true);
        jsonObject.putAll(jsonMap);
        System.out.println("jsonObject:"+jsonObject);
        return jsonObject;*/

        //得分
        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");
        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))){
                    if (answerEntity.getName().equals(jsonObject1.getString("checkItems"))){
                        score += Integer.valueOf(answerEntity.getScore());
                    }
                }
            }
        }
        DecimalFormat df = new DecimalFormat("######0.00");

        score = Double.parseDouble(df.format(50+(score-141.86)/17.54*10));
        resultMap0.put("总分", String.valueOf(score));
        resultJson.put("总分", String.valueOf(score));
        if (scaleMarksEntities.size() > 0){
            for (ScaleMarksEntity scaleMarksEntity:scaleMarksEntities){
                if (score <= Integer.valueOf(scaleMarksEntity.getScoreEnd()) && score >= Integer.valueOf(scaleMarksEntity.getScoreStart())){
                    if (!scaleMarksEntity.getSymptom().equals("无")){
                        resultMap0.put("结论", scaleMarksEntity.getSymptom());
                        resultJson.put("结论", scaleMarksEntity.getSymptom());
                    }
                    if (scaleMarksEntity.getImprovementSuggestions() != null && !scaleMarksEntity.getImprovementSuggestions().equals("无")){
                        resultMap0.put("建议", scaleMarksEntity.getImprovementSuggestions());
                        resultJson.put("建议", scaleMarksEntity.getImprovementSuggestions());
                    }
                }
            }
        }
        resultMapList.add(resultMap0);


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

        return returnJson;
    }
}
