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.text.DecimalFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zsy
 * @description:明尼苏达多相人格测验说谎L量表
 * @date 2021/7/20 15:55
 */
public class MMPILScale extends BaseScale{

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

    public  JSONObject scaleCalculate() throws Exception {

        /*// 自定义路径使用
        ScaleUtil.getScaleDll("MMPILScale.dll");
        System.out.println("JSONArray:"+jsonArray);
        System.out.println("resultJson:"+resultJson);
        XScale xScale = new XScale(jsonArray,resultJson);
        String result = xScale.MMPILScale(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;
        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("scoreDirection").equals("0")){
                if (jsonObject1.getString("checkItems").equals("是")) {
                    score += 1;
                }
            }
        }
        System.out.println("得分：" + score);
        if (score > 4 && score <= 8){
            resultJson.put("提示", "缺乏对自己行为的自知力");
        }else if (score > 8){
            resultJson.put("提示", "存在刻意的自我表现");
        }
        DecimalFormat df   = new DecimalFormat("######0.00");
        //女大学生标准分
        double score0 = Double.parseDouble(df.format(50+10*(score-2.7)/1.8));
        //男大学生标准分
        double score1 = Double.parseDouble(df.format(50+10*(score-2.5)/1.9));
        //女性普通人标准分
        double score2 = Double.parseDouble(df.format(50+10*(score-4.5)/2.6));
        //男性普通人标准分
        double score3 = Double.parseDouble(df.format(50+10*(score-4.2)/2.6));
        //女性病人标准分
        double score4 = Double.parseDouble(df.format(50+10*(score-4.8)/2.3));
        //男性病人标准分
        double score5 = Double.parseDouble(df.format(50+10*(score-4.2)/2.3));
        //得分列表
        Map<String, String> resultMap0 = new LinkedHashMap<>();
        resultMap0.put("女大学生得分", String.valueOf(score0));
        resultJson.put("女大学生得分", String.valueOf(score0));
        Map<String, String> resultMap1 = new LinkedHashMap<>();
        resultMap1.put("男大学生得分", String.valueOf(score1));
        resultJson.put("男大学生得分", String.valueOf(score1));
        //得分列表
        Map<String, String> resultMap2 = new LinkedHashMap<>();
        resultMap2.put("女性普通人得分", String.valueOf(score2));
        resultJson.put("女性普通人得分", String.valueOf(score2));
        //得分列表
        Map<String, String> resultMap3 = new LinkedHashMap<>();
        resultMap3.put("男性普通人得分", String.valueOf(score3));
        resultJson.put("男性普通人得分", String.valueOf(score3));
        Map<String, String> resultMap4 = new LinkedHashMap<>();
        resultMap4.put("女性病人得分", String.valueOf(score4));
        resultJson.put("女性病人得分", String.valueOf(score4));
        Map<String, String> resultMap5 = new LinkedHashMap<>();
        resultMap5.put("男性病人得分", String.valueOf(score5));
        resultJson.put("男性病人得分", String.valueOf(score5));

        List<Map<String,String>> resultMapList = new ArrayList<>();
        resultMapList.add(resultMap0);
        resultMapList.add(resultMap1);
        resultMapList.add(resultMap2);
        resultMapList.add(resultMap3);
        resultMapList.add(resultMap4);
        resultMapList.add(resultMap5);

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

        resultJson.put("测试结果指导", "此结果分为六类，请按照自己情况查看对应结果");
        return returnJson;
    }
}
