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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

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

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

    public  JSONObject scaleCalculate() throws Exception {

        // 自定义路径使用
        /*ScaleUtil.getScaleDll("SRBDSCALE.dll");
        System.out.println("JSONArray:"+jsonArray);
        System.out.println("resultJson:"+resultJson);
        XScale xScale = new XScale(jsonArray,resultJson);
        String result = xScale.SRBDScale(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;
        //Y总分
        double scoreY = 0;
        //N总分
        double scoreN = 0;
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            if (jsonObject1.getString("checkItems").equals("Y")) {
                scoreY += 1;
            }else if (jsonObject1.getString("checkItems").equals("N")){
                scoreN += 1;
            }
        }
        DecimalFormat df   = new DecimalFormat("######0.00");
        score = scoreY+scoreN;
        if (score != 0){
            score = Double.parseDouble(df.format(scoreY/score));
        }
        //得分列表
        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("总分", String.valueOf(score));
        resultJson.put("总分", String.valueOf(score));
        if (score > 0.33) {
            resultMap.put("结论", "阳性,存在儿童睡眠相关呼吸障碍风险较高。");
            resultJson.put("结论", "阳性,存在儿童睡眠相关呼吸障碍风险较高。");
        }else {
            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",resultJson.getString("scaleName"));
        returnJson.put("resultJson",resultJson);

        return returnJson;
    }
}
