package com.rf.psychological.scale.resultBusiness.cognitiveResult;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.model.ObjectEntity;
import com.rf.psychological.file.excel.ExcelClass;
import com.rf.psychological.scale.resultBusiness.scaleResult.BaseScale;
import com.rf.psychological.utils.Constant;
import com.rf.psychological.utils.ExcelUtil;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * @author zsy
 * @description:精神运动警觉度测试（亚太）
 * @date 2021/7/20 15:55
 */
public class ALERTNESSYATAICognize extends BaseScale {

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

    public  JSONObject scaleCalculate() throws Exception {
        List<Map<String, String>> resultMapList = new ArrayList<>();
        JSONObject jsonObject = resultJson.getJSONObject("jsonObject");
        String fileName = resultJson.getString("fileName");
        String name;

        JSONObject resultDBJson = new JSONObject(true);

        resultMapList = new ArrayList<>();
        //有效总时长
        int allTime = 0;
        //大于600ms次数
        int time600 = 0;
        //最快
        int fast = 0;
        //最慢
        int slow = 0;
        JSONArray jsonArray = jsonObject.getJSONArray("data");
        List<ObjectEntity> dataList = jsonArray.toJavaList(ObjectEntity.class);
        List<ObjectEntity> objectEntities = new ArrayList<>();
        List list = new ArrayList();
        for (ObjectEntity roa : dataList) {
            int temp = Integer.parseInt(roa.getValue());
            if (temp >= 100) {
                allTime += temp;
                list.add(temp);
                //objectEntities.add(roa);
                if (temp > 600) {
                    time600 += 1;
                }
            }
        }
        //最快和最慢10%个数
        int a = Math.round(list.size() / 10);
        Collections.sort(list);
        for (int i = 0; i < a; i++) {
            fast += (int) list.get(i);
            int y = i + 1;
            slow += (int) list.get(list.size() - y);

        }

        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("平均反应时(ms)", String.valueOf(new BigDecimal((double) allTime / (double) list.size()).setScale(2, RoundingMode.HALF_UP).doubleValue()));
        resultDBJson.put("平均反应时(ms)", String.valueOf(new BigDecimal((double) allTime / (double) list.size()).setScale(2, RoundingMode.HALF_UP).doubleValue()));
        resultMap.put("最快10%反应时(ms)", String.valueOf(new BigDecimal((double) fast / (double) a).setScale(2, RoundingMode.HALF_UP).doubleValue()));
        resultDBJson.put("最快10%反应时(ms)", String.valueOf(new BigDecimal((double) fast / (double) a).setScale(2, RoundingMode.HALF_UP).doubleValue()));
        resultMap.put("最慢10%反应时(ms)", String.valueOf(new BigDecimal((double) slow / (double) a).setScale(2, RoundingMode.HALF_UP).doubleValue()));
        resultDBJson.put("最慢10%反应时(ms)", String.valueOf(new BigDecimal((double) slow / (double) a).setScale(2, RoundingMode.HALF_UP).doubleValue()));
        resultMap.put("忽视频次", String.valueOf(new BigDecimal((double) time600 / (double) list.size()).setScale(2, RoundingMode.HALF_UP).doubleValue()));
        resultDBJson.put("忽视频次", String.valueOf(new BigDecimal((double) time600 / (double) list.size()).setScale(2, RoundingMode.HALF_UP).doubleValue()));
        resultMapList.add(resultMap);
        fileName = fileName + "-" + Constant.SHEET_NAME_ALERTNESS_YATAI + ".xlsx";
        name = Constant.SHEET_NAME_ALERTNESS_YATAI;
        String datas = jsonArray.toString();
        ExcelUtil.createExcelFile(ObjectEntity.class, dataList, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_ALERTNESS), fileName, Constant.SHEET_NAME_ALERTNESS_YATAI);

        JSONObject returnJson = new JSONObject(true);
        returnJson.put("fileName",fileName);
        returnJson.put("name", name);
        returnJson.put("resultJson",resultDBJson);
        returnJson.put("datas",datas);

        return returnJson;
    }


}
