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

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

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

        JSONArray jsonArray = jsonObject.getJSONArray("rawData");
        List<RDTEntity> rdtEntityList = jsonArray.toJavaList(RDTEntity.class);
        double totalEarned = jsonObject.getDouble("totalEarned");
        int totalAerated = jsonObject.getInteger("totalAerated");
        double BART = 0;
        for (RDTEntity rdtent:rdtEntityList){
            if (!rdtent.getExplosion()){
                BART ++;
            }
        }
        DecimalFormat df = new DecimalFormat("######0.00");
        if (BART != 0){
            BART = Double.parseDouble(df.format(10 / BART));
        }else {
            BART = 10;
        }

        resultDBJson.put("versionNo",Constant.COGNITION_RESULT_VERSION);
        JSONArray totalScores = new JSONArray();
        JSONObject totalScore = new JSONObject(true);
        String desc = null;
        totalScore.put("totalScore",BART);

        if (BART>4){
            desc = "风险偏好决策风格:您在决策时比较冒险激进，会更看重失去的部分，因为不愿意失去而甘愿冒更大风险。比如给你两种选择，一种是给你100％的几率拿走50元，另一种是给你两个盲盒，一个盲盒里有500块，一个盲盒里一分钱没有，对于您这种决策风格的人会更倾向选第二种。";
        }else if(BART>1 && BART<=4){
            desc = "中性决策风格:您在决策时不会太保守也不会太冒险，做事追求稳中求进，是非常理想的决策风格。";
        }else if(BART>0 && BART<=1){
            desc = "风险规避决策风格:您在决策时比较保守谨慎，不会太冒险，更看重能获得的部分，比如在投资时您会更看重确定性的回报而不是不确定的期望收益。";
        }
        totalScore.put("totalScoreDesc",desc);
        totalScores.add(totalScore);
        resultDBJson.put("totalScore",totalScores);
        //维度得分及结论模块
        JSONArray dimensions = new JSONArray();
        JSONObject scoreA = new JSONObject(true);
        scoreA.put("name","得分");
        scoreA.put("score",String.valueOf(totalEarned));
        scoreA.put("Describe","无");
        scoreA.put("maximum","无");
        scoreA.put("groupName","第一组");
        dimensions.add(scoreA);
        JSONObject scoreB = new JSONObject(true);
        scoreB.put("name","总点击次数");
        scoreB.put("score",String.valueOf(totalAerated));
        scoreB.put("Describe","无");
        scoreB.put("maximum","无");
        scoreB.put("groupName","第一组");
        dimensions.add(scoreB);
        resultDBJson.put("dimensions", dimensions);
        //表格数据
        JSONArray tableModule = new JSONArray();
        JSONObject table1 = new JSONObject(true);
        JSONArray columnName = new JSONArray();
        columnName.add("指标名称");
        columnName.add("指标值");
        JSONArray columnValue = new JSONArray();
        columnValue.add("总分;总点击次数");
        columnValue.add(totalEarned +";"+totalAerated);
        table1.put("tableName","table1");
        table1.put("columnName",columnName);
        table1.put("columnValue",columnValue);
        tableModule.add(table1);
        resultDBJson.put("tableModule", tableModule);

        JSONObject ifShow = new JSONObject(true);
        ifShow.put("totalScore",true);
        ifShow.put("dimensions",true);
        ifShow.put("note",false);
        ifShow.put("radar",false);
        ifShow.put("table",true);
        resultDBJson.put("ifShow",ifShow);

        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("总分", String.valueOf(totalEarned));
        resultDBJson.put("总分", String.valueOf(totalEarned));
        resultMap.put("总点击次数", String.valueOf(totalAerated));
        resultDBJson.put("总点击次数", String.valueOf(totalAerated));
        resultMapList = new ArrayList<>();
        resultMapList.add(resultMap);
        fileName = fileName + "-" + Constant.SHEET_NAME_RDT + ".xlsx";
        name = Constant.SHEET_NAME_RDT;
        String datas = jsonArray.toString();
        ExcelUtil.createExcelFile(RDTEntity.class, rdtEntityList, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_RDT), fileName, Constant.SHEET_NAME_RDT);

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

        return returnJson;
    }


}
