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.*;

/**
 * @author zsy
 * @description:情感量表
 * @date 2021/7/20 15:55
 */
public class ASScale extends BaseScale{

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

    public  JSONObject scaleCalculate() throws Exception {

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

        //情感平衡得分
        int score = 0;
        //正向情感得分
        int score1 = 0;
        //负向情感得分
        int score2 = 0;
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            if (i % 2 == 0){
                if (jsonObject1.getString("scoreDirection").equals("1")) {
                    if (jsonObject1.getString("checkItems").equals("是")) {
                        score1 += 1;
                    }
                } else if (jsonObject1.getString("scoreDirection").equals("0")){
                    if (jsonObject1.getString("checkItems").equals("否")) {
                        score1 += 1;
                    }
                }
            }else {
                if (jsonObject1.getString("scoreDirection").equals("1")) {
                    if (jsonObject1.getString("checkItems").equals("是")) {
                        score2 += 1;
                    }
                } else if (jsonObject1.getString("scoreDirection").equals("0")){
                    if (jsonObject1.getString("checkItems").equals("否")) {
                        score2 += 1;
                    }
                }
            }
        }
        score = score1 - score2 + 5;
        Map<String,Double> dimensionScoreMap = new HashMap<>();
        Map<String, String> resultMap0 = new LinkedHashMap<>();
        resultMap0.put("情感平衡得分", String.valueOf(score));
        resultJson.put("情感平衡得分", String.valueOf(score));
        dimensionScoreMap.put("8af178857b94d581017b94fa49621e18", (double) score1);
        dimensionScoreMap.put("8af178857b94d581017b94fa49b71e19", (double) score2);
        resultJson.put("dimensionScoreMap",dimensionScoreMap);
        resultMap0.put("正向情感得分", String.valueOf(score1));
        resultJson.put("正向情感得分", String.valueOf(score1));
        resultMap0.put("负向情感得分", String.valueOf(score2));
        resultJson.put("负向情感得分", String.valueOf(score2));

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

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

        return returnJson;
    }
}
