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:态度与意见调查中的期望性作答
 * @date 2021/7/20 15:55
 */
public class RDScale extends BaseScale{

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

    public  JSONObject scaleCalculate() throws Exception {

        // 自定义路径使用
        /*ScaleUtil.getScaleDll("RDScale.dll");
        System.out.println("JSONArray:"+jsonArray);
        System.out.println("resultJson:"+resultJson);
        XScale xScale = new XScale(jsonArray,resultJson);
        String result = xScale.RDScale(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;
                }
            }
        }
        DecimalFormat df   = new DecimalFormat("######0.00");
        //标准分
        double score0 = Double.parseDouble(df.format(50+10*(score-12.7)/7.5));
        Map<String, String> resultMap0 = new LinkedHashMap<>();
        resultMap0.put("总分", String.valueOf(score0));
        resultJson.put("总分", String.valueOf(score0));

        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_RD);
        returnJson.put("resultJson",resultJson);

        return returnJson;
    }
}
