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

/**
 * @author zsy
 * @description:学龄前儿童活动调查表
 * @date 2021/7/20 15:55
 */
public class PSAIScale extends BaseScale {

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

    public JSONObject scaleCalculate() throws Exception {

        // 自定义路径使用
        /*ScaleUtil.getScaleDll("PSAISCALE.dll");
        System.out.println("JSONArray:"+jsonArray);
        System.out.println("resultJson:"+resultJson);
        XScale xScale = new XScale(jsonArray,resultJson);
        String result = xScale.PSAIScale(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;
        //男性化得分
        int score1 = 0;
        //女性化得分
        int score2 = 0;
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            if (i == 2 || i == 4 || i == 13 || i == 16 || i == 18 || (i >= 7 && i <= 10) || (i >= 22 && i <= 24)) {
                if (jsonObject1.getString("checkItems").equals("没有")) {
                    score2 += 1;
                } else if (jsonObject1.getString("checkItems").equals("很少")) {
                    score2 += 2;
                } else if (jsonObject1.getString("checkItems").equals("有时有")) {
                    score2 += 3;
                } else if (jsonObject1.getString("checkItems").equals("常有")) {
                    score2 += 4;
                } else if (jsonObject1.getString("checkItems").equals("很常见")) {
                    score2 += 5;
                }
            } else {
                if (jsonObject1.getString("checkItems").equals("没有")) {
                    score1 += 1;
                } else if (jsonObject1.getString("checkItems").equals("很少")) {
                    score1 += 2;
                } else if (jsonObject1.getString("checkItems").equals("有时有")) {
                    score1 += 3;
                } else if (jsonObject1.getString("checkItems").equals("常有")) {
                    score1 += 4;
                } else if (jsonObject1.getString("checkItems").equals("很常见")) {
                    score1 += 5;
                }
            }
        }
        DecimalFormat df = new DecimalFormat("######0.00");
        score = Double.parseDouble(df.format((score1 - score2) * 1.1 + 48.25));
        //得分列表
        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("总分", String.valueOf(score));
        resultJson.put("总分", String.valueOf(score));
        resultMap.put("男性化得分", String.valueOf(score1));
        resultJson.put("男性化得分", String.valueOf(score1));
        resultMap.put("女性化得分", String.valueOf(score2));
        resultJson.put("女性化得分", String.valueOf(score2));
        Map<String, Double> dimensionScoreMap = new HashMap<>();
        List<Map<String, String>> resultMapList = new ArrayList<>();
        resultMapList.add(resultMap);
        dimensionScoreMap.put("8af178857b94d581017b95029b52270a", (double) score1);
        dimensionScoreMap.put("8af178857b94d581017b95029ba6270b", (double) score2);
        resultJson.put("dimensionScoreMap",dimensionScoreMap);
        JSONObject returnJson = new JSONObject(true);
        returnJson.put("resultMapList", resultMapList);
        returnJson.put("scaleName", Constant.SHEET_NAME_PSAI);
        returnJson.put("resultJson", resultJson);

        return returnJson;
    }
}
