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

/**
 * @author zsy
 * @description:简易精神状态测试 & 简易精神状态测试V2
 * @date 2021/7/20 15:55
 */
public class MMSECognize extends BaseScale {

    public MMSECognize(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;
        JSONArray jsonArray = jsonObject.getJSONArray("data");
        String name;
        String type = resultJson.getString("type");

        JSONObject resultDBJson = new JSONObject(true);

        int score = jsonObject.getInteger("valueSum");
        if (Constant.QUEST_TYPE_MMSE.equals(type)){
            fileName = resultJson.getString("fileName") + "-" + Constant.SHEET_NAME_MMSE + ".xlsx";
            name = Constant.SHEET_NAME_MMSE;
        }else {
            fileName = resultJson.getString("fileName") + "-" + Constant.SHEET_NAME_MMSE_V2 + ".xlsx";
            name = Constant.SHEET_NAME_MMSE_V2;
        }

        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("总分", String.valueOf(score));
        resultDBJson.put("总分", String.valueOf(score));
        resultMapList.add(resultMap);
        String datas = jsonArray.toString();
        ExcelUtil.createExcelFile(ObjectEntity.class, jsonArray.toJavaList(ObjectEntity.class), resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_MMSE), fileName, name);

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

        return returnJson;
    }


}
