package com.rf.psychological.scale.resultBusiness.scaleResult;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.utils.Constant;

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 SDSScale extends BaseScale{

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

    public  JSONObject scaleCalculate() throws Exception {

        // 自定义路径使用
        /*ScaleUtil.getScaleDll("SDSScale.dll");
        System.out.println("JSONArray:"+jsonArray);
        System.out.println("resultJson:"+resultJson);
        XScale xScale = new XScale(jsonArray,resultJson);
        String result = xScale.SDSScale(jsonArray.toString(), resultJson.toString());

        //解决排序问题
        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;*/


        //得分
        int score = 0;
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            if (jsonObject1.getString("scoreDirection").equals("1")) {
                if (jsonObject1.getString("checkItems").equals("无")) {
                    score += 1;
                } else if (jsonObject1.getString("checkItems").equals("有时")) {
                    score += 2;
                } else if (jsonObject1.getString("checkItems").equals("经常")) {
                    score += 3;
                } else if (jsonObject1.getString("checkItems").equals("持续")) {
                    score += 4;
                }
            } else if (jsonObject1.getString("scoreDirection").equals("0")){
                if (jsonObject1.getString("checkItems").equals("无")) {
                    score += 4;
                } else if (jsonObject1.getString("checkItems").equals("有时")) {
                    score += 3;
                } else if (jsonObject1.getString("checkItems").equals("经常")) {
                    score += 2;
                } else if (jsonObject1.getString("checkItems").equals("持续")) {
                    score += 1;
                }

            }
        }
        //标准分
        score = (int) (score * 1.25);
        //得分列表
        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("总分", String.valueOf(score));
        resultJson.put("总分", String.valueOf(score));
        //根据得分判断信任度
        if (score >= 72){
            resultMap.put("抑郁程度", "重度抑郁");
            resultJson.put("抑郁程度", "重度抑郁");
        }else if (score >= 63 && score < 72){
            resultMap.put("抑郁程度", "中度抑郁");
            resultJson.put("抑郁程度", "中度抑郁");
        }else if (score >= 53 && score < 63){
            resultMap.put("抑郁程度", "轻度抑郁");
            resultJson.put("抑郁程度", "轻度抑郁");
        }else if (score < 53){
            resultMap.put("抑郁程度", "不抑郁");
            resultJson.put("抑郁程度", "不抑郁");
        }

        List<Map<String,String>> resultMapList = new ArrayList<>();
        resultMapList.add(resultMap);

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

        return returnJson;
    }
}
