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

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

    public  JSONObject scaleCalculate() throws Exception {

        // 自定义路径使用
        /*ScaleUtil.getScaleDll("AUDITScale.dll");
        System.out.println("JSONArray:"+jsonArray);
        System.out.println("resultJson:"+resultJson);
        XScale xScale = new XScale(jsonArray,resultJson);
        String result = xScale.AUDITScale(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;*/

        //得分
        int score = 0;
        int [] scores = {0,0,0,0,0,0,0,0,0,0};
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            if (i == 0){
                if (jsonObject1.getString("checkItems").equals("每月约一次")) {
                    scores[i] = 1;
                } else if (jsonObject1.getString("checkItems").equals("每月2-4次")) {
                    scores[i] = 2;
                } else if (jsonObject1.getString("checkItems").equals("每周2-3次")) {
                    scores[i] = 3;
                } else if (jsonObject1.getString("checkItems").equals("每周4次以上")) {
                    scores[i] = 4;
                }
            }else if (i == 1){
                if (jsonObject1.getString("checkItems").equals("3或4")) {
                    scores[i] = 1;
                } else if (jsonObject1.getString("checkItems").equals("5或6")) {
                    scores[i] = 2;
                } else if (jsonObject1.getString("checkItems").equals("7到9")) {
                    scores[i] = 3;
                } else if (jsonObject1.getString("checkItems").equals("10以上")) {
                    scores[i] = 4;
                }
            }else if (i == 2){
                if (jsonObject1.getString("checkItems").equals("每月不到一次")) {
                    scores[i] = 1;
                } else if (jsonObject1.getString("checkItems").equals("每月一次")) {
                    scores[i] = 2;
                } else if (jsonObject1.getString("checkItems").equals("几乎每周一次")) {
                    scores[i] = 3;
                } else if (jsonObject1.getString("checkItems").equals("每天或几乎每天")) {
                    scores[i] = 4;
                }
            }else if (i == 3 || i == 4){
                if (jsonObject1.getString("checkItems").equals("每月不到一次")) {
                    scores[i] = 1;
                } else if (jsonObject1.getString("checkItems").equals("每月一次")) {
                    scores[i] = 2;
                } else if (jsonObject1.getString("checkItems").equals("几乎每周一次")) {
                    scores[i] = 3;
                } else if (jsonObject1.getString("checkItems").equals("每天或几乎每天一次")) {
                    scores[i] = 4;
                }
            }else if (i == 5 || i == 6 || i == 7){
                if (jsonObject1.getString("checkItems").equals("每月不到一次")) {
                    scores[i] = 1;
                } else if (jsonObject1.getString("checkItems").equals("每月一次")) {
                    scores[i] = 2;
                } else if (jsonObject1.getString("checkItems").equals("每周一次")) {
                    scores[i] = 3;
                } else if (jsonObject1.getString("checkItems").equals("每天或几乎每天一次")) {
                    scores[i] = 4;
                }
            }else if (i == 8){
                if (jsonObject1.getString("checkItems").equals("有，但不在过去一年")) {
                    scores[i] = 1;
                } else if (jsonObject1.getString("checkItems").equals("有，是在过去的一年中")) {
                    scores[i] = 2;
                }
            }else if (i == 9){
                if (jsonObject1.getString("checkItems").equals("有，但不在过去的一年")) {
                    scores[i] = 1;
                } else if (jsonObject1.getString("checkItems").equals("有，是在过去的一年中")) {
                    scores[i] = 2;
                }
            }
        }

        score = scores[0] + scores[1] +scores[2] +scores[3] +scores[4] +scores[5] +scores[6] +scores[7] +scores[8] +scores[9];
        Map<String, String> resultMap1 = new LinkedHashMap<>();
        resultMap1.put("总分", String.valueOf(score));
        resultJson.put("总分", String.valueOf(score));
        if (score >= 8){
            resultMap1.put("结论", "酒精依赖阳性患者");
            resultJson.put("结论", "酒精依赖阳性患者");
        }
        if (scores[0] > 3 && scores[1] > 3 && scores[2] > 3 && scores[3] <= 3 && scores[4] <= 3 && scores[5] <= 3 && scores[6] <= 3 && scores[7]<= 3){
            resultMap1.put("提示", "严重危害性饮酒");
            resultJson.put("提示", "严重危害性饮酒");
        }else if (scores[0] <= 3 && scores[1] <= 3 && scores[2] <= 3 && scores[3] > 3 && scores[4] > 3 && scores[5] > 3 && scores[6] <= 3 && scores[7]<= 3){
            resultMap1.put("提示", "酒精依赖");
            resultJson.put("提示", "酒精依赖");
        }

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

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

        return returnJson;
    }
}
