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.dao.model.StroopEntity;
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.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zsy
 * @description:Stroop试验
 * @date 2021/7/20 15:55
 */
public class STROOPCognize extends BaseScale {

    public STROOPCognize(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<>();
        System.out.println(jsonObject);
        JSONArray jsonArray = jsonObject.getJSONArray("data");
        List<StroopEntity> stroopEntities = jsonArray.toJavaList(StroopEntity.class);
        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("StroopD完成时间（秒）", stroopEntities.get(0).getFinishTime());
        resultDBJson.put("StroopD完成时间（秒）", stroopEntities.get(0).getFinishTime());
        resultMap.put("StroopD错误数目", stroopEntities.get(0).getErrorNumber());
        resultDBJson.put("StroopD错误数目", stroopEntities.get(0).getErrorNumber());
        resultMapList.add(resultMap);
        Map<String, String> resultMap2 = new LinkedHashMap<>();
        resultMap2.put("StroopW完成时间（秒）", stroopEntities.get(1).getFinishTime());
        resultDBJson.put("StroopW完成时间（秒）", stroopEntities.get(1).getFinishTime());
        resultMap2.put("StroopW错误数目", stroopEntities.get(1).getErrorNumber());
        resultDBJson.put("StroopW错误数目", stroopEntities.get(1).getErrorNumber());
        resultMapList.add(resultMap2);
        Map<String, String> resultMap3 = new LinkedHashMap<>();
        resultMap3.put("StroopC完成时间（秒）", stroopEntities.get(2).getFinishTime());
        resultDBJson.put("StroopC完成时间（秒）", stroopEntities.get(2).getFinishTime());
        resultMap3.put("StroopC错误数目", stroopEntities.get(2).getErrorNumber());
        resultDBJson.put("StroopC错误数目", stroopEntities.get(2).getErrorNumber());
        resultMapList.add(resultMap3);
        fileName = fileName + "-" + Constant.SHEET_NAME_STROOP + ".xlsx";
        name = Constant.SHEET_NAME_STROOP;
        String datas = jsonArray.toString();
        ExcelUtil.createExcelFile(StroopEntity.class, stroopEntities, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_STROOP), fileName, Constant.SHEET_NAME_STROOP);

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

        return returnJson;
    }


}
