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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.model.SHAPE2Entity;
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.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 SHAPEYATAICognize extends BaseScale {

    public SHAPEYATAICognize(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);

        String score = jsonObject.getString("userScore");
        JSONArray jsonArray = jsonObject.getJSONArray("userResponseRecords");
        List<SHAPE2Entity> shape2Entities = jsonArray.toJavaList(SHAPE2Entity.class);
        fileName = fileName + "-" + Constant.SHEET_NAME_SHAPE_YATAI + ".xlsx";
        name = Constant.SHEET_NAME_SHAPE_YATAI;

        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("总分", score);
        resultDBJson.put("总分", score);
        resultMapList = new ArrayList<>();
        resultMapList.add(resultMap);
        JSONArray totalIndexResult = jsonObject.getJSONArray("totalIndexResult");
        JSONObject resultBack0 = totalIndexResult.getJSONObject(5);
        Map<String, String> resultMap0 = new LinkedHashMap<>();
        resultMap0.put("难度等级", String.valueOf(resultBack0.get("diffIndex")));
        resultDBJson.put("一级难度等级", String.valueOf(resultBack0.get("diffIndex")));
        resultMap0.put("测试总数", String.valueOf(resultBack0.get("totalCount")));
        resultDBJson.put("一级难度测试总数", String.valueOf(resultBack0.get("totalCount")));
        resultMap0.put("正确数", String.valueOf(resultBack0.get("rightCount")));
        resultDBJson.put("一级难度正确数", String.valueOf(resultBack0.get("rightCount")));
        resultMap0.put("正确率", String.valueOf(resultBack0.get("rightRate")));
        resultDBJson.put("一级难度正确率", String.valueOf(resultBack0.get("rightRate")));
        resultMapList.add(resultMap0);
        JSONObject resultBack1 = totalIndexResult.getJSONObject(4);
        Map<String, String> resultMap1 = new LinkedHashMap<>();
        resultMap1.put("难度等级", String.valueOf(resultBack1.get("diffIndex")));
        resultDBJson.put("二级难度等级", String.valueOf(resultBack1.get("diffIndex")));
        resultMap1.put("测试总数", String.valueOf(resultBack1.get("totalCount")));
        resultDBJson.put("二级难度测试总数", String.valueOf(resultBack1.get("totalCount")));
        resultMap1.put("正确数", String.valueOf(resultBack1.get("rightCount")));
        resultDBJson.put("二级难度正确数", String.valueOf(resultBack1.get("rightCount")));
        resultMap1.put("正确率", String.valueOf(resultBack1.get("rightRate")));
        resultDBJson.put("二级难度正确率", String.valueOf(resultBack1.get("rightRate")));
        resultMapList.add(resultMap1);
        JSONObject resultBack2 = totalIndexResult.getJSONObject(3);
        Map<String, String> resultMap2 = new LinkedHashMap<>();
        resultMap2.put("难度等级", String.valueOf(resultBack2.get("diffIndex")));
        resultDBJson.put("三级难度等级", String.valueOf(resultBack2.get("diffIndex")));
        resultMap2.put("测试总数", String.valueOf(resultBack2.get("totalCount")));
        resultDBJson.put("三级难度测试总数", String.valueOf(resultBack2.get("totalCount")));
        resultMap2.put("正确数", String.valueOf(resultBack2.get("rightCount")));
        resultDBJson.put("三级难度正确数", String.valueOf(resultBack2.get("rightCount")));
        resultMap2.put("正确率", String.valueOf(resultBack2.get("rightRate")));
        resultDBJson.put("三级难度正确率", String.valueOf(resultBack2.get("rightRate")));
        resultMapList.add(resultMap2);
        JSONObject resultBack3 = totalIndexResult.getJSONObject(2);
        Map<String, String> resultMap3 = new LinkedHashMap<>();
        resultMap3.put("难度等级", String.valueOf(resultBack3.get("diffIndex")));
        resultDBJson.put("四级难度等级", String.valueOf(resultBack3.get("diffIndex")));
        resultMap3.put("测试总数", String.valueOf(resultBack3.get("totalCount")));
        resultDBJson.put("四级难度测试总数", String.valueOf(resultBack3.get("totalCount")));
        resultMap3.put("正确数", String.valueOf(resultBack3.get("rightCount")));
        resultDBJson.put("四级难度正确数", String.valueOf(resultBack3.get("rightCount")));
        resultMap3.put("正确率", String.valueOf(resultBack3.get("rightRate")));
        resultDBJson.put("四级难度正确率", String.valueOf(resultBack3.get("rightRate")));
        resultMapList.add(resultMap3);
        JSONObject resultBack4 = totalIndexResult.getJSONObject(1);
        Map<String, String> resultMap4 = new LinkedHashMap<>();
        resultMap4.put("难度等级", String.valueOf(resultBack4.get("diffIndex")));
        resultDBJson.put("五级难度等级", String.valueOf(resultBack4.get("diffIndex")));
        resultMap4.put("测试总数", String.valueOf(resultBack4.get("totalCount")));
        resultDBJson.put("五级难度测试总数", String.valueOf(resultBack4.get("totalCount")));
        resultMap4.put("正确数", String.valueOf(resultBack4.get("rightCount")));
        resultDBJson.put("五级难度正确数", String.valueOf(resultBack4.get("rightCount")));
        resultMap4.put("正确率", String.valueOf(resultBack4.get("rightRate")));
        resultDBJson.put("五级难度正确率", String.valueOf(resultBack4.get("rightRate")));
        resultMapList.add(resultMap4);
        JSONObject resultBack5 = totalIndexResult.getJSONObject(0);
        Map<String, String> resultMap5 = new LinkedHashMap<>();
        resultMap5.put("难度等级", String.valueOf(resultBack5.get("diffIndex")));
        resultDBJson.put("六级难度等级", String.valueOf(resultBack5.get("diffIndex")));
        resultMap5.put("测试总数", String.valueOf(resultBack5.get("totalCount")));
        resultDBJson.put("六级难度测试总数", String.valueOf(resultBack5.get("totalCount")));
        resultMap5.put("正确数", String.valueOf(resultBack5.get("rightCount")));
        resultDBJson.put("六级难度正确数", String.valueOf(resultBack5.get("rightCount")));
        resultMap5.put("正确率", String.valueOf(resultBack5.get("rightRate")));
        resultDBJson.put("六级难度正确率", String.valueOf(resultBack5.get("rightRate")));
        resultMapList.add(resultMap5);

        String datas = jsonArray.toString();
        ExcelUtil.createExcelFile(SHAPE2Entity.class, shape2Entities, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_SHAPE), fileName, Constant.SHEET_NAME_SHAPE_YATAI);

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

        return returnJson;
    }


}
