package com.rf.psychological.scale.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.rf.psychological.base.rest.BaseController;
import com.rf.psychological.file.excel.ExcelClass;
import com.rf.psychological.dao.model.MemoryCheckEntity;
import com.rf.psychological.dao.model.ObjectEntity;
import com.rf.psychological.user.dao.model.UserEntity;
import com.rf.psychological.scale.dao.model.UserRecordEntity;
import com.rf.psychological.opLog.annotation.OperationLogAnnotation;
import com.rf.psychological.security.AesEncryptUtils;
import com.rf.psychological.security.SafetyProcess;
import com.rf.psychological.scale.service.CognitiveTaskService;
import com.rf.psychological.scale.service.MemoryCheckService;
import com.rf.psychological.scale.service.UserRecordService;
import com.rf.psychological.user.service.UserService;
import com.rf.psychological.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @author zsy
 * @description:记忆检查信息管理
 * @date 2021/6/17 15:55
 */

@Slf4j
@RestController
@RequestMapping("/memory")
@Api(tags = "记忆检查信息管理")
public class MemoryCheckController extends BaseController {

    @Autowired
    private MemoryCheckService memoryCheckService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserRecordService userRecordService;

    @Autowired
    private CognitiveTaskService cognitiveTaskService;

    /**
     * 添加记忆检查信息
     *
     * @param
     * @return
     */
    @SafetyProcess
    @OperationLogAnnotation
    @PostMapping("/saveMemoryCheck")
    @ApiOperation("添加记忆检查信息")
    public Result saveMemoryCheck(@RequestBody String json) throws Exception {
        //public Result saveMemoryCheck(@RequestBody JSONObject jsonObject ) throws Exception {
        String data = AesEncryptUtils.decrypt(JSONObject.parseObject(json).getString("data"));
        JSONObject jsonObject = JSONObject.parseObject(data);
        System.out.println("----------------result-------------------");
        System.out.println(jsonObject);
        String userId = jsonObject.getString("userId");
        String type = Constant.QUEST_TYPE_RTC;
        if (jsonObject.containsKey("type")) {
            type = jsonObject.getString("type");
        }
        String testType = Constant.DEFAULT_VALUE_ZERO;
        if (jsonObject.containsKey("testType")) {
            testType = jsonObject.getString("testType");
        }
        String testPlanId = "0";
        if (jsonObject.containsKey("testPlanId")) {
            testPlanId = jsonObject.getString("testPlanId");
        }
        UserEntity userEntity = this.userService.getOne(userId);
        String phone = userEntity.getPhone();
        String institutionNo = userEntity.getInstitutionNo();

        DateFormat df = new SimpleDateFormat("yyyyMMddHHmm");
        //当前时间
        String testTime = df.format(new Date());
        JSONArray jsonArray = jsonObject.getJSONArray("memoryCheck");
        //答题结果列表
        List<MemoryCheckEntity> dataList = jsonArray.toJavaList(MemoryCheckEntity.class);
        //创建测试记录所需信息
        List<MemoryCheckEntity> MemoryCheckEntitys = new ArrayList<>();
        int y = 0;
        for (MemoryCheckEntity memoryCheckEntity : dataList) {
            memoryCheckEntity.setPhone(phone);
            memoryCheckEntity.setTestDate(testTime);
            memoryCheckEntity.setType(type);
            memoryCheckEntity.setTestType(testType);
            memoryCheckEntity.setInstitutionNo(institutionNo);
            if (memoryCheckEntity.getMemoryType() == 2) {
                if (testType.equals("1")) {
                    this.memoryCheckService.save(memoryCheckEntity);
                    //根据phone获取用户进行中记忆检查
                    MemoryCheckEntitys = this.memoryCheckService.getMemoryCheckList(phone, type, testType,memoryCheckEntity.getQuestionNo(),institutionNo);
                } else {
                    if (!memoryCheckEntity.getQuestionNo().equals("5")) {
                        this.memoryCheckService.save(memoryCheckEntity);
                        return success();
                    } else {
                        this.memoryCheckService.save(memoryCheckEntity);
                        //根据phone获取用户进行中记忆检查
                        MemoryCheckEntitys = this.memoryCheckService.getMemoryCheckList(phone, type, testType,null,institutionNo);
                    }
                }
                for (MemoryCheckEntity memoryCheckEntity1 : MemoryCheckEntitys) {
                    this.memoryCheckService.updateStatus(memoryCheckEntity1.getId());
                }
            } else {
                if (testType.equals("1") && (memoryCheckEntity.getQuestionNo().equals("6") || memoryCheckEntity.getQuestionNo().equals("7"))) {
                    memoryCheckEntity.setStatus(0);
                    this.memoryCheckService.save(memoryCheckEntity);
                    MemoryCheckEntitys.add(memoryCheckEntity);
                } else {
                    y++;
                    if (memoryCheckEntity.getQuestionNo().equals("6") || memoryCheckEntity.getQuestionNo().equals("7") || memoryCheckEntity.getQuestionNo().equals("8")) {
                        memoryCheckEntity.setCorrelationMemory(dataList.get(0).getMina() + testTime);
                    }
                    this.memoryCheckService.save(memoryCheckEntity);
                    if (dataList.size() == y) {
                        return success();
                    }
                }
            }
        }
        String datas = new String();
        //生成测试记录文件并在数据库插入用户测试记录
        String userName = userEntity.getPetName();
        String userBirthday = userEntity.getBirthday();
        String userSex = userEntity.getGender();
        if (userSex.equals("0")) {
            userSex = "男";
        } else {
            userSex = "女";
        }
        testTime = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒").format(new Date());
        String fileName = userName + Constant.SPLIT_CHAR + userBirthday + Constant.SPLIT_CHAR + userSex + Constant.SPLIT_CHAR + testTime;
        String testResult = new String();
        String name = new String();
        List<Map<String, String>> resultMapList = new ArrayList<>();
        // 创建返回的json对象
        JSONObject resultJson = new JSONObject(true);
        // 创建入库的json对象
        JSONObject resultDBJson = new JSONObject(true);

        List<ObjectEntity> objectEntityList = new ArrayList<>();
        for (MemoryCheckEntity memoryCheck : MemoryCheckEntitys) {
            if (memoryCheck.getQuestionNo().equals("1")) {
                ObjectEntity objectEntity1 = new ObjectEntity();
                ObjectEntity objectEntity2 = new ObjectEntity();
                ObjectEntity objectEntity3 = new ObjectEntity();
                if (memoryCheck.getMemoryType() == 1) {
                    objectEntity1.setKey("指向记忆-即时记忆指向成绩");
                    objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                    objectEntity2.setKey("指向记忆-即时记忆非指向成绩");
                    objectEntity2.setValue(String.valueOf(memoryCheck.getGradeb()));
                    objectEntity3.setKey("指向记忆-即时记忆总成绩");
                    objectEntity3.setValue(String.valueOf(Integer.valueOf(memoryCheck.getGradea()) + Integer.valueOf(memoryCheck.getGradeb())));
                    resultJson.put("指向记忆-即时记忆指向成绩", String.valueOf(memoryCheck.getGradea()));
                    resultJson.put("指向记忆-即时记忆非指向成绩", String.valueOf(memoryCheck.getGradeb()));
                    resultJson.put("指向记忆-即时记忆总成绩", String.valueOf(Integer.valueOf(memoryCheck.getGradea()) + Integer.valueOf(memoryCheck.getGradeb())));
                } else {
                    objectEntity1.setKey("指向记忆-延时记忆指向成绩");
                    objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                    objectEntity2.setKey("指向记忆-延时记忆非指向成绩");
                    objectEntity2.setValue(String.valueOf(memoryCheck.getGradeb()));
                    objectEntity3.setKey("指向记忆-延时记忆总成绩");
                    objectEntity3.setValue(String.valueOf(Integer.valueOf(memoryCheck.getGradea()) + Integer.valueOf(memoryCheck.getGradeb())));
                    resultJson.put("指向记忆-延时记忆指向成绩", String.valueOf(memoryCheck.getGradea()));
                    resultJson.put("指向记忆-延时记忆非指向成绩", String.valueOf(memoryCheck.getGradeb()));
                    resultJson.put("指向记忆-延时记忆总成绩", String.valueOf(Integer.valueOf(memoryCheck.getGradea()) + Integer.valueOf(memoryCheck.getGradeb())));
                }
                objectEntityList.add(objectEntity1);
                objectEntityList.add(objectEntity2);
                objectEntityList.add(objectEntity3);
            } else if (memoryCheck.getQuestionNo().equals("2")) {
                ObjectEntity objectEntity1 = new ObjectEntity();
                ObjectEntity objectEntity2 = new ObjectEntity();
                ObjectEntity objectEntity3 = new ObjectEntity();
                if (memoryCheck.getMemoryType() == 1) {
                    objectEntity1.setKey("词汇联想-即时记忆简单成绩");
                    objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                    objectEntity2.setKey("词汇联想-即时记忆复杂成绩");
                    objectEntity2.setValue(String.valueOf(memoryCheck.getGradeb()));
                    objectEntity3.setKey("词汇联想-即时记忆总成绩");
                    objectEntity3.setValue(String.valueOf(memoryCheck.getGradec()));
                    resultJson.put("词汇联想-即时记忆简单成绩", String.valueOf(memoryCheck.getGradea()));
                    resultJson.put("词汇联想-即时记忆复杂成绩", String.valueOf(memoryCheck.getGradeb()));
                    resultJson.put("词汇联想-即时记忆总成绩", String.valueOf(memoryCheck.getGradec()));
                } else {
                    objectEntity1.setKey("词汇联想-延时记忆简单成绩");
                    objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                    objectEntity2.setKey("词汇联想-延时记忆复杂成绩");
                    objectEntity2.setValue(String.valueOf(memoryCheck.getGradeb()));
                    objectEntity3.setKey("词汇联想-延时记忆总成绩");
                    objectEntity3.setValue(String.valueOf(memoryCheck.getGradec()));
                    resultJson.put("词汇联想-延时记忆简单成绩", String.valueOf(memoryCheck.getGradea()));
                    resultJson.put("词汇联想-延时记忆复杂成绩", String.valueOf(memoryCheck.getGradeb()));
                    resultJson.put("词汇联想-延时记忆总成绩", String.valueOf(memoryCheck.getGradec()));
                }
                objectEntityList.add(objectEntity1);
                objectEntityList.add(objectEntity2);
                objectEntityList.add(objectEntity3);
            } else if (memoryCheck.getQuestionNo().equals("3")) {
                ObjectEntity objectEntity1 = new ObjectEntity();
                if (memoryCheck.getMemoryType() == 1) {
                    objectEntity1.setKey("图像自由回忆-即时记忆图像成绩");
                    objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                    resultJson.put("图像自由回忆-即时记忆图像成绩", String.valueOf(memoryCheck.getGradea()));
                } else {
                    objectEntity1.setKey("图像自由回忆-延时记忆图像成绩");
                    objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                    resultJson.put("图像自由回忆-延时记忆图像成绩", String.valueOf(memoryCheck.getGradea()));
                }
                objectEntityList.add(objectEntity1);
            } else if (memoryCheck.getQuestionNo().equals("4")) {
                ObjectEntity objectEntity1 = new ObjectEntity();
                ObjectEntity objectEntity2 = new ObjectEntity();
                ObjectEntity objectEntity3 = new ObjectEntity();
                if (memoryCheck.getMemoryType() == 1) {
                    objectEntity1.setKey("图片联想-即时记忆即时成绩");
                    objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                    objectEntity2.setKey("图片联想-即时记忆即时再确认成绩");
                    objectEntity2.setValue(String.valueOf(memoryCheck.getGradeb()));
                    objectEntity3.setKey("图片联想-即时记忆总成绩");
                    objectEntity3.setValue(String.valueOf(Integer.valueOf(memoryCheck.getGradea()) + Integer.valueOf(memoryCheck.getGradeb())));
                    resultJson.put("图片联想-即时记忆即时成绩", String.valueOf(memoryCheck.getGradea()));
                    resultJson.put("图片联想-即时记忆即时再确认成绩", String.valueOf(memoryCheck.getGradeb()));
                    resultJson.put("图片联想-即时记忆总成绩", String.valueOf(Integer.valueOf(memoryCheck.getGradea()) + Integer.valueOf(memoryCheck.getGradeb())));
                } else {
                    objectEntity1.setKey("图片联想-延时记忆即时成绩");
                    objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                    objectEntity2.setKey("图片联想-延时记忆即时再确认成绩");
                    objectEntity2.setValue(String.valueOf(memoryCheck.getGradeb()));
                    objectEntity3.setKey("图片联想-延时记忆总成绩");
                    objectEntity3.setValue(String.valueOf(Integer.valueOf(memoryCheck.getGradea()) + Integer.valueOf(memoryCheck.getGradeb())));
                    resultJson.put("图片联想-延时记忆即时成绩", String.valueOf(memoryCheck.getGradea()));
                    resultJson.put("图片联想-延时记忆即时再确认成绩", String.valueOf(memoryCheck.getGradeb()));
                    resultJson.put("图片联想-延时记忆总成绩", String.valueOf(Integer.valueOf(memoryCheck.getGradea()) + Integer.valueOf(memoryCheck.getGradeb())));
                }
                objectEntityList.add(objectEntity1);
                objectEntityList.add(objectEntity2);
                objectEntityList.add(objectEntity3);
            } else if (memoryCheck.getQuestionNo().equals("5")) {
                ObjectEntity objectEntity1 = new ObjectEntity();
                if (memoryCheck.getMemoryType() == 1) {
                    objectEntity1.setKey("人像特点回忆-即时记忆人像成绩");
                    objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                    resultJson.put("人像特点回忆-即时记忆人像成绩", String.valueOf(memoryCheck.getGradea()));
                } else {
                    objectEntity1.setKey("人像特点回忆-延时记忆人像成绩");
                    objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                    resultJson.put("人像特点回忆-延时记忆人像成绩", String.valueOf(memoryCheck.getGradea()));
                }
                objectEntityList.add(objectEntity1);
            } else if (memoryCheck.getQuestionNo().equals("7")) {
                ObjectEntity objectEntity1 = new ObjectEntity();
                ObjectEntity objectEntity2 = new ObjectEntity();
                ObjectEntity objectEntity3 = new ObjectEntity();
                objectEntity1.setKey("前瞻记忆-四字成语[全部正确率，部分正确率]");
                objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                objectEntity2.setKey("前瞻记忆-模糊数字[全部正确率，部分正确率]");
                objectEntity2.setValue(String.valueOf(memoryCheck.getGradeb()));
                objectEntity3.setKey("前瞻记忆-总分");
                objectEntity3.setValue(String.valueOf(memoryCheck.getGradec()));
                resultJson.put("前瞻记忆-四字成语[全部正确率，部分正确率]", String.valueOf(memoryCheck.getGradea()));
                resultJson.put("前瞻记忆-模糊数字[全部正确率，部分正确率]", String.valueOf(memoryCheck.getGradeb()));
                resultJson.put("前瞻记忆-总分", String.valueOf(memoryCheck.getGradec()));
                objectEntityList.add(objectEntity1);
                objectEntityList.add(objectEntity2);
                objectEntityList.add(objectEntity3);
            } else if (memoryCheck.getQuestionNo().equals("8")) {
                ObjectEntity objectEntity1 = new ObjectEntity();
                ObjectEntity objectEntity2 = new ObjectEntity();
                ObjectEntity objectEntity3 = new ObjectEntity();
                objectEntity1.setKey("AD临床记忆问卷-第一题");
                objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                objectEntity2.setKey("AD临床记忆问卷-第二题");
                objectEntity2.setValue(String.valueOf(memoryCheck.getGradeb()));
                objectEntity3.setKey("AD临床记忆问卷-第三题");
                objectEntity3.setValue(String.valueOf(memoryCheck.getGradec()));
                resultJson.put("AD临床记忆问卷-第一题", String.valueOf(memoryCheck.getGradea()));
                resultJson.put("AD临床记忆问卷-第二题", String.valueOf(memoryCheck.getGradeb()));
                resultJson.put("AD临床记忆问卷-第三题", String.valueOf(memoryCheck.getGradec()));
                objectEntityList.add(objectEntity1);
                objectEntityList.add(objectEntity2);
                objectEntityList.add(objectEntity3);
            } else {
                ObjectEntity objectEntity1 = new ObjectEntity();
                objectEntity1.setKey("视觉空间工作记忆-视觉空间成绩");
                objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                resultJson.put("视觉空间工作记忆-视觉空间成绩", String.valueOf(memoryCheck.getGradea()));
                objectEntityList.add(objectEntity1);
            }
        }
        datas = MemoryCheckEntitys.toString();
        if (Constant.QUEST_TYPE_RTC.equals(type)) {
            fileName = fileName + "-" + Constant.SHEET_NAME_RTC + ".xlsx";
            name = Constant.SHEET_NAME_RTC;
        } else {
            fileName = fileName + "-" + Constant.SHEET_NAME_RTC_CHILD + ".xlsx";
            name = Constant.SHEET_NAME_RTC_CHILD;
        }

        ExcelUtil.createExcelFile(ObjectEntity.class, objectEntityList, null, new ExcelClass().contentExcel(Constant.QUEST_TYPE_RTC), fileName, name);

        // 获取JSON第一层所有的key
        Set<String> keys = resultJson.keySet();

        //计算结果
        JSONArray dbJsonList = new JSONArray();
        JSONObject dbJson = new JSONObject(true);
        //列名
        List colNames = new ArrayList();
        colNames.add("");
        colNames.add("即时记忆");
        colNames.add("延时记忆");
        dbJson.put("colNames", colNames);
        JSONArray tableContext = new JSONArray();

        if (Constant.QUEST_TYPE_RTC_CHILD.equals(type)) {
            for (MemoryCheckEntity memoryCheck : MemoryCheckEntitys) {
                if (memoryCheck.getMemoryType() == 2 || memoryCheck.getQuestionNo().equals("6") || memoryCheck.getQuestionNo().equals("7")){
                    if (memoryCheck.getQuestionNo().equals("1")) {
                        List colValue1 = new ArrayList();
                        colValue1.add(resultJson.get("指向记忆-即时记忆总成绩"));
                        colValue1.add(resultJson.get("指向记忆-延时记忆总成绩"));
                        JSONObject row1 = new JSONObject();
                        row1.put("name", "指向记忆");
                        row1.put("value", colValue1);
                        tableContext.add(row1);
                    } else if (memoryCheck.getQuestionNo().equals("2")) {
                        List colValue2 = new ArrayList();
                        colValue2.add(resultJson.get("词汇联想-即时记忆总成绩"));
                        colValue2.add(resultJson.get("词汇联想-延时记忆总成绩"));
                        JSONObject row2 = new JSONObject();
                        row2.put("name", "词汇联想");
                        row2.put("value", colValue2);
                        tableContext.add(row2);
                    } else if (memoryCheck.getQuestionNo().equals("3")) {
                        List colValue3 = new ArrayList();
                        colValue3.add(resultJson.get("图像自由回忆-即时记忆图像成绩"));
                        colValue3.add(resultJson.get("图像自由回忆-延时记忆图像成绩"));
                        JSONObject row3 = new JSONObject();
                        row3.put("name", "图象自由回忆");
                        row3.put("value", colValue3);
                        tableContext.add(row3);
                    } else if (memoryCheck.getQuestionNo().equals("4")) {
                        List colValue4 = new ArrayList();
                        colValue4.add(resultJson.get("图片联想-即时记忆即时成绩") + "、" + resultJson.get("图片联想-即时记忆即时再确认成绩") + "、" + resultJson.get("图片联想-即时记忆总成绩"));
                        colValue4.add(resultJson.get("图片联想-延时记忆即时成绩") + "、" + resultJson.get("图片联想-延时记忆即时再确认成绩") + "、" + resultJson.get("图片联想-延时记忆总成绩"));
                        JSONObject row4 = new JSONObject();
                        row4.put("name", "图片联想");
                        row4.put("value", colValue4);
                        tableContext.add(row4);
                    } else if (memoryCheck.getQuestionNo().equals("5")) {
                        List colValue5 = new ArrayList();
                        colValue5.add(resultJson.get("人像特点回忆-即时记忆人像成绩"));
                        colValue5.add(resultJson.get("人像特点回忆-延时记忆人像成绩"));
                        JSONObject row5 = new JSONObject();
                        row5.put("name", "人像特点");
                        row5.put("value", colValue5);
                        tableContext.add(row5);
                    } else if (memoryCheck.getQuestionNo().equals("7")) {
                        List colValue7 = new ArrayList();
                        colValue7.add(resultJson.get("前瞻记忆-总分"));
                        colValue7.add(resultJson.get("/"));
                        JSONObject row7 = new JSONObject();
                        row7.put("name", "前瞻记忆");
                        row7.put("value", colValue7);
                        tableContext.add(row7);
                    } else {
                        List colValue6 = new ArrayList();
                        colValue6.add(resultJson.get("视觉空间工作记忆-视觉空间成绩"));
                        colValue6.add(resultJson.get("/"));
                        JSONObject row6 = new JSONObject();
                        row6.put("name", "视觉空间工作记忆");
                        row6.put("value", colValue6);
                        tableContext.add(row6);
                    }
                }
            }
        } else {
            //列值
            List colValue1 = new ArrayList();
            colValue1.add(resultJson.get("指向记忆-即时记忆总成绩"));
            colValue1.add(resultJson.get("指向记忆-延时记忆总成绩"));
            JSONObject row1 = new JSONObject();
            row1.put("name", "指向记忆");
            row1.put("value", colValue1);
            tableContext.add(row1);

            List colValue2 = new ArrayList();
            colValue2.add(resultJson.get("词汇联想-即时记忆总成绩"));
            colValue2.add(resultJson.get("词汇联想-延时记忆总成绩"));
            JSONObject row2 = new JSONObject();
            row2.put("name", "词汇联想");
            row2.put("value", colValue2);
            tableContext.add(row2);

            List colValue3 = new ArrayList();
            colValue3.add(resultJson.get("图像自由回忆-即时记忆图像成绩"));
            colValue3.add(resultJson.get("图像自由回忆-延时记忆图像成绩"));
            JSONObject row3 = new JSONObject();
            row3.put("name", "图象自由回忆");
            row3.put("value", colValue3);
            tableContext.add(row3);

            List colValue4 = new ArrayList();
            colValue4.add(resultJson.get("图片联想-即时记忆即时成绩") + "、" + resultJson.get("图片联想-即时记忆即时再确认成绩") + "、" + resultJson.get("图片联想-即时记忆总成绩"));
            colValue4.add(resultJson.get("图片联想-延时记忆即时成绩") + "、" + resultJson.get("图片联想-延时记忆即时再确认成绩") + "、" + resultJson.get("图片联想-延时记忆总成绩"));
            JSONObject row4 = new JSONObject();
            row4.put("name", "图片联想");
            row4.put("value", colValue4);
            tableContext.add(row4);

            List colValue5 = new ArrayList();
            colValue5.add(resultJson.get("人像特点回忆-即时记忆人像成绩"));
            colValue5.add(resultJson.get("人像特点回忆-延时记忆人像成绩"));
            JSONObject row5 = new JSONObject();
            row5.put("name", "人像特点");
            row5.put("value", colValue5);
            tableContext.add(row5);

            List colValue6 = new ArrayList();
            colValue6.add(resultJson.get("视觉空间工作记忆-视觉空间成绩"));
            colValue6.add(resultJson.get("/"));
            JSONObject row6 = new JSONObject();
            row6.put("name", "视觉空间工作记忆");
            row6.put("value", colValue6);
            tableContext.add(row6);

            List colValue7 = new ArrayList();
            colValue7.add(resultJson.get("前瞻记忆-总分"));
            colValue7.add(resultJson.get("/"));
            JSONObject row7 = new JSONObject();
            row7.put("name", "前瞻记忆");
            row7.put("value", colValue7);
            tableContext.add(row7);
        }

        dbJson.put("tableContext", tableContext);
        dbJsonList.add(dbJson);

        int i = 1;
        // 获取第一层每个key对应的值 的类型
        for (String key : keys) {
            if (i == keys.size()) {
                testResult += key + ":" + resultJson.get(key);
            } else {
                testResult += key + ":" + resultJson.get(key) + ";";
            }
            i++;
        }
        //测试记录
        String filePath;
        filePath = "./心理检测/检测结果/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + name + "/" + fileName;
        UserRecordEntity userRecordEntity = testRecord(type, phone, institutionNo, testTime, filePath, dbJsonList.toString(), name, "", datas, testResult, testPlanId);
        System.out.println("resultJson:" + resultJson);
        return success(userRecordEntity.getId(), "完成");

    }


//=====================第一版本逻辑，勿删========================================
    /*@SafetyProcess
    @OperationLogAnnotation
    @PostMapping("/saveMemoryCheck")
    @ApiOperation("添加记忆检查信息")
    public Result saveMemoryCheck(@RequestBody String json) throws Exception {
        //public Result saveMemoryCheck(@RequestBody JSONObject jsonObject ) throws Exception {
        String data = AesEncryptUtils.decrypt(JSONObject.parseObject(json).getString("data"));
        JSONObject jsonObject = JSONObject.parseObject(data);
        System.out.println("----------------result-------------------");
        System.out.println(jsonObject);
        String userId = jsonObject.getString("userId");
        String type = Constant.QUEST_TYPE_RTC;
        if (jsonObject.containsKey("type")) {
            type = jsonObject.getString("type");
        }
        String testType = Constant.DEFAULT_VALUE_ZERO;
        if (jsonObject.containsKey("testType")) {
            testType = jsonObject.getString("testType");
        }
        String testPlanId = "0";
        if (jsonObject.containsKey("testPlanId")) {
            testPlanId = jsonObject.getString("testPlanId");
        }
        UserEntity userEntity = this.userService.getOne(userId);
        String phone = userEntity.getPhone();
        String institutionNo = userEntity.getInstitutionNo();

        DateFormat df = new SimpleDateFormat("yyyyMMddHHmm");
        //当前时间
        String testTime = df.format(new Date());
        JSONArray jsonArray = jsonObject.getJSONArray("memoryCheck");
        //答题结果列表
        List<MemoryCheckEntity> dataList = jsonArray.toJavaList(MemoryCheckEntity.class);
        //创建测试记录所需信息
        List<MemoryCheckEntity> MemoryCheckEntitys = new ArrayList<>();
        int y = 0;
        for (MemoryCheckEntity memoryCheckEntity : dataList) {
            memoryCheckEntity.setPhone(phone);
            memoryCheckEntity.setTestDate(testTime);
            memoryCheckEntity.setType(type);
            memoryCheckEntity.setTestType(testType);
            if (memoryCheckEntity.getMemoryType() == 2) {
                if (testType.equals("1")) {
                    this.memoryCheckService.save(memoryCheckEntity);
                    //根据phone获取用户进行中记忆检查
                    MemoryCheckEntitys = this.memoryCheckService.getMemoryCheckList(phone, type, testType,memoryCheckEntity.getQuestionNo());
                } else {
                    if (!memoryCheckEntity.getQuestionNo().equals("5")) {
                        this.memoryCheckService.save(memoryCheckEntity);
                        return success();
                    } else {
                        this.memoryCheckService.save(memoryCheckEntity);
                        //根据phone获取用户进行中记忆检查
                        MemoryCheckEntitys = this.memoryCheckService.getMemoryCheckList(phone, type, testType,null);
                    *//*if (memoryCheckEntity.getCorrelationMemory() != null || !memoryCheckEntity.getCorrelationMemory().equals("")) {
                        MemoryCheckEntity memoryCheck = this.memoryCheckService.selectById(memoryCheckEntity.getCorrelationMemory());
                        MemoryCheckEntitys.add(memoryCheck);
                        if (memoryCheck.getQuestionNo().equals("1")) {
                            //查询当前用户第六题信息
                            MemoryCheckEntity memory = this.memoryCheckService.selectMemory(phone, memoryCheck.getMina() + memoryCheck.getTestDate());
                            if (memory != null) {
                                MemoryCheckEntitys.add(memory);
                                this.memoryCheckService.updateStatus(memory.getId());
                            }
                        }
                    }
                        for (MemoryCheckEntity memoryCheckEntity1 : MemoryCheckEntitys) {
                            this.memoryCheckService.updateStatus(memoryCheckEntity1.getId());
                        }*//*

                    }
                }
                for (MemoryCheckEntity memoryCheckEntity1 : MemoryCheckEntitys) {
                    this.memoryCheckService.updateStatus(memoryCheckEntity1.getId());
                }
            } else {
                if (testType.equals("1") && (memoryCheckEntity.getQuestionNo().equals("6") || memoryCheckEntity.getQuestionNo().equals("7"))) {
                    memoryCheckEntity.setStatus(0);
                    this.memoryCheckService.save(memoryCheckEntity);
                    MemoryCheckEntitys.add(memoryCheckEntity);
                } else {
                    y++;
                    if (memoryCheckEntity.getQuestionNo().equals("6") || memoryCheckEntity.getQuestionNo().equals("7") || memoryCheckEntity.getQuestionNo().equals("8")) {
                        memoryCheckEntity.setCorrelationMemory(dataList.get(0).getMina() + testTime);
                    }
                    this.memoryCheckService.save(memoryCheckEntity);
                    if (dataList.size() == y) {
                        return success();
                    }
                }
            }
        }
        String datas = new String();
        //生成测试记录文件并在数据库插入用户测试记录
        String userName = userEntity.getPetName();
        String userBirthday = userEntity.getBirthday();
        String userSex = userEntity.getGender();
        if (userSex.equals("0")) {
            userSex = "男";
        } else {
            userSex = "女";
        }
        testTime = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒").format(new Date());
        String fileName = userName + Constant.SPLIT_CHAR + userBirthday + Constant.SPLIT_CHAR + userSex + Constant.SPLIT_CHAR + testTime;
        String testResult = new String();
        String name = new String();
        List<Map<String, String>> resultMapList = new ArrayList<>();
        // 创建返回的json对象
        JSONObject resultJson = new JSONObject(true);
        // 创建入库的json对象
        JSONObject resultDBJson = new JSONObject(true);

        List<ObjectEntity> objectEntityList = new ArrayList<>();
        for (MemoryCheckEntity memoryCheck : MemoryCheckEntitys) {
            if (memoryCheck.getQuestionNo().equals("1")) {
                ObjectEntity objectEntity1 = new ObjectEntity();
                ObjectEntity objectEntity2 = new ObjectEntity();
                ObjectEntity objectEntity3 = new ObjectEntity();
                if (memoryCheck.getMemoryType() == 1) {
                    objectEntity1.setKey("指向记忆-即时记忆指向成绩");
                    objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                    objectEntity2.setKey("指向记忆-即时记忆非指向成绩");
                    objectEntity2.setValue(String.valueOf(memoryCheck.getGradeb()));
                    objectEntity3.setKey("指向记忆-即时记忆总成绩");
                    objectEntity3.setValue(String.valueOf(Integer.valueOf(memoryCheck.getGradea()) + Integer.valueOf(memoryCheck.getGradeb())));
                    resultJson.put("指向记忆-即时记忆指向成绩", String.valueOf(memoryCheck.getGradea()));
                    resultJson.put("指向记忆-即时记忆非指向成绩", String.valueOf(memoryCheck.getGradeb()));
                    resultJson.put("指向记忆-即时记忆总成绩", String.valueOf(Integer.valueOf(memoryCheck.getGradea()) + Integer.valueOf(memoryCheck.getGradeb())));
                } else {
                    objectEntity1.setKey("指向记忆-延时记忆指向成绩");
                    objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                    objectEntity2.setKey("指向记忆-延时记忆非指向成绩");
                    objectEntity2.setValue(String.valueOf(memoryCheck.getGradeb()));
                    objectEntity3.setKey("指向记忆-延时记忆总成绩");
                    objectEntity3.setValue(String.valueOf(Integer.valueOf(memoryCheck.getGradea()) + Integer.valueOf(memoryCheck.getGradeb())));
                    resultJson.put("指向记忆-延时记忆指向成绩", String.valueOf(memoryCheck.getGradea()));
                    resultJson.put("指向记忆-延时记忆非指向成绩", String.valueOf(memoryCheck.getGradeb()));
                    resultJson.put("指向记忆-延时记忆总成绩", String.valueOf(Integer.valueOf(memoryCheck.getGradea()) + Integer.valueOf(memoryCheck.getGradeb())));
                }
                objectEntityList.add(objectEntity1);
                objectEntityList.add(objectEntity2);
                objectEntityList.add(objectEntity3);
            } else if (memoryCheck.getQuestionNo().equals("2")) {
                ObjectEntity objectEntity1 = new ObjectEntity();
                ObjectEntity objectEntity2 = new ObjectEntity();
                ObjectEntity objectEntity3 = new ObjectEntity();
                if (memoryCheck.getMemoryType() == 1) {
                    objectEntity1.setKey("词汇联想-即时记忆简单成绩");
                    objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                    objectEntity2.setKey("词汇联想-即时记忆复杂成绩");
                    objectEntity2.setValue(String.valueOf(memoryCheck.getGradeb()));
                    objectEntity3.setKey("词汇联想-即时记忆总成绩");
                    objectEntity3.setValue(String.valueOf(memoryCheck.getGradec()));
                    resultJson.put("词汇联想-即时记忆简单成绩", String.valueOf(memoryCheck.getGradea()));
                    resultJson.put("词汇联想-即时记忆复杂成绩", String.valueOf(memoryCheck.getGradeb()));
                    resultJson.put("词汇联想-即时记忆总成绩", String.valueOf(memoryCheck.getGradec()));
                } else {
                    objectEntity1.setKey("词汇联想-延时记忆简单成绩");
                    objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                    objectEntity2.setKey("词汇联想-延时记忆复杂成绩");
                    objectEntity2.setValue(String.valueOf(memoryCheck.getGradeb()));
                    objectEntity3.setKey("词汇联想-延时记忆总成绩");
                    objectEntity3.setValue(String.valueOf(memoryCheck.getGradec()));
                    resultJson.put("词汇联想-延时记忆简单成绩", String.valueOf(memoryCheck.getGradea()));
                    resultJson.put("词汇联想-延时记忆复杂成绩", String.valueOf(memoryCheck.getGradeb()));
                    resultJson.put("词汇联想-延时记忆总成绩", String.valueOf(memoryCheck.getGradec()));
                }
                objectEntityList.add(objectEntity1);
                objectEntityList.add(objectEntity2);
                objectEntityList.add(objectEntity3);
            } else if (memoryCheck.getQuestionNo().equals("3")) {
                ObjectEntity objectEntity1 = new ObjectEntity();
                if (memoryCheck.getMemoryType() == 1) {
                    objectEntity1.setKey("图像自由回忆-即时记忆图像成绩");
                    objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                    resultJson.put("图像自由回忆-即时记忆图像成绩", String.valueOf(memoryCheck.getGradea()));
                } else {
                    objectEntity1.setKey("图像自由回忆-延时记忆图像成绩");
                    objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                    resultJson.put("图像自由回忆-延时记忆图像成绩", String.valueOf(memoryCheck.getGradea()));
                }
                objectEntityList.add(objectEntity1);
            } else if (memoryCheck.getQuestionNo().equals("4")) {
                ObjectEntity objectEntity1 = new ObjectEntity();
                ObjectEntity objectEntity2 = new ObjectEntity();
                ObjectEntity objectEntity3 = new ObjectEntity();
                if (memoryCheck.getMemoryType() == 1) {
                    objectEntity1.setKey("图片联想-即时记忆即时成绩");
                    objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                    objectEntity2.setKey("图片联想-即时记忆即时再确认成绩");
                    objectEntity2.setValue(String.valueOf(memoryCheck.getGradeb()));
                    objectEntity3.setKey("图片联想-即时记忆总成绩");
                    objectEntity3.setValue(String.valueOf(Integer.valueOf(memoryCheck.getGradea()) + Integer.valueOf(memoryCheck.getGradeb())));
                    resultJson.put("图片联想-即时记忆即时成绩", String.valueOf(memoryCheck.getGradea()));
                    resultJson.put("图片联想-即时记忆即时再确认成绩", String.valueOf(memoryCheck.getGradeb()));
                    resultJson.put("图片联想-即时记忆总成绩", String.valueOf(Integer.valueOf(memoryCheck.getGradea()) + Integer.valueOf(memoryCheck.getGradeb())));
                } else {
                    objectEntity1.setKey("图片联想-延时记忆即时成绩");
                    objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                    objectEntity2.setKey("图片联想-延时记忆即时再确认成绩");
                    objectEntity2.setValue(String.valueOf(memoryCheck.getGradeb()));
                    objectEntity3.setKey("图片联想-延时记忆总成绩");
                    objectEntity3.setValue(String.valueOf(Integer.valueOf(memoryCheck.getGradea()) + Integer.valueOf(memoryCheck.getGradeb())));
                    resultJson.put("图片联想-延时记忆即时成绩", String.valueOf(memoryCheck.getGradea()));
                    resultJson.put("图片联想-延时记忆即时再确认成绩", String.valueOf(memoryCheck.getGradeb()));
                    resultJson.put("图片联想-延时记忆总成绩", String.valueOf(Integer.valueOf(memoryCheck.getGradea()) + Integer.valueOf(memoryCheck.getGradeb())));
                }
                objectEntityList.add(objectEntity1);
                objectEntityList.add(objectEntity2);
                objectEntityList.add(objectEntity3);
            } else if (memoryCheck.getQuestionNo().equals("5")) {
                ObjectEntity objectEntity1 = new ObjectEntity();
                if (memoryCheck.getMemoryType() == 1) {
                    objectEntity1.setKey("人像特点回忆-即时记忆人像成绩");
                    objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                    resultJson.put("人像特点回忆-即时记忆人像成绩", String.valueOf(memoryCheck.getGradea()));
                } else {
                    objectEntity1.setKey("人像特点回忆-延时记忆人像成绩");
                    objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                    resultJson.put("人像特点回忆-延时记忆人像成绩", String.valueOf(memoryCheck.getGradea()));
                }
                objectEntityList.add(objectEntity1);
            } else if (memoryCheck.getQuestionNo().equals("7")) {
                ObjectEntity objectEntity1 = new ObjectEntity();
                ObjectEntity objectEntity2 = new ObjectEntity();
                ObjectEntity objectEntity3 = new ObjectEntity();
                objectEntity1.setKey("前瞻记忆-四字成语[全部正确率，部分正确率]");
                objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                objectEntity2.setKey("前瞻记忆-模糊数字[全部正确率，部分正确率]");
                objectEntity2.setValue(String.valueOf(memoryCheck.getGradeb()));
                objectEntity3.setKey("前瞻记忆-总分");
                objectEntity3.setValue(String.valueOf(memoryCheck.getGradec()));
                resultJson.put("前瞻记忆-四字成语[全部正确率，部分正确率]", String.valueOf(memoryCheck.getGradea()));
                resultJson.put("前瞻记忆-模糊数字[全部正确率，部分正确率]", String.valueOf(memoryCheck.getGradeb()));
                resultJson.put("前瞻记忆-总分", String.valueOf(memoryCheck.getGradec()));
                objectEntityList.add(objectEntity1);
                objectEntityList.add(objectEntity2);
                objectEntityList.add(objectEntity3);
            } else if (memoryCheck.getQuestionNo().equals("8")) {
                ObjectEntity objectEntity1 = new ObjectEntity();
                ObjectEntity objectEntity2 = new ObjectEntity();
                ObjectEntity objectEntity3 = new ObjectEntity();
                objectEntity1.setKey("AD临床记忆问卷-第一题");
                objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                objectEntity2.setKey("AD临床记忆问卷-第二题");
                objectEntity2.setValue(String.valueOf(memoryCheck.getGradeb()));
                objectEntity3.setKey("AD临床记忆问卷-第三题");
                objectEntity3.setValue(String.valueOf(memoryCheck.getGradec()));
                resultJson.put("AD临床记忆问卷-第一题", String.valueOf(memoryCheck.getGradea()));
                resultJson.put("AD临床记忆问卷-第二题", String.valueOf(memoryCheck.getGradeb()));
                resultJson.put("AD临床记忆问卷-第三题", String.valueOf(memoryCheck.getGradec()));
                objectEntityList.add(objectEntity1);
                objectEntityList.add(objectEntity2);
                objectEntityList.add(objectEntity3);
            } else {
                ObjectEntity objectEntity1 = new ObjectEntity();
                objectEntity1.setKey("视觉空间工作记忆-视觉空间成绩");
                objectEntity1.setValue(String.valueOf(memoryCheck.getGradea()));
                resultJson.put("视觉空间工作记忆-视觉空间成绩", String.valueOf(memoryCheck.getGradea()));
                objectEntityList.add(objectEntity1);
            }
        }
        datas = MemoryCheckEntitys.toString();
        if (Constant.QUEST_TYPE_RTC.equals(type)) {
            fileName = fileName + "-" + Constant.SHEET_NAME_RTC + ".xlsx";
            name = Constant.SHEET_NAME_RTC;
        } else {
            fileName = fileName + "-" + Constant.SHEET_NAME_RTC_CHILD + ".xlsx";
            name = Constant.SHEET_NAME_RTC_CHILD;
        }

        ExcelUtil.createExcelFile(ObjectEntity.class, objectEntityList, null, new ExcelClass().contentExcel(Constant.QUEST_TYPE_RTC), fileName, name);

        // 获取JSON第一层所有的key
        Set<String> keys = resultJson.keySet();

        //计算结果
        JSONArray dbJsonList = new JSONArray();
        JSONObject dbJson = new JSONObject(true);
        //列名
        List colNames = new ArrayList();
        colNames.add("");
        colNames.add("即时记忆");
        colNames.add("延时记忆");
        dbJson.put("colNames", colNames);
        JSONArray tableContext = new JSONArray();

        if (Constant.QUEST_TYPE_RTC_CHILD.equals(type)) {
            for (MemoryCheckEntity memoryCheck : MemoryCheckEntitys) {
                if (memoryCheck.getMemoryType() == 2 || memoryCheck.getQuestionNo().equals("6") || memoryCheck.getQuestionNo().equals("7")){
                    if (memoryCheck.getQuestionNo().equals("1")) {
                        List colValue1 = new ArrayList();
                        colValue1.add(resultJson.get("指向记忆-即时记忆总成绩"));
                        colValue1.add(resultJson.get("指向记忆-延时记忆总成绩"));
                        JSONObject row1 = new JSONObject();
                        row1.put("name", "指向记忆");
                        row1.put("value", colValue1);
                        tableContext.add(row1);
                    } else if (memoryCheck.getQuestionNo().equals("2")) {
                        List colValue2 = new ArrayList();
                        colValue2.add(resultJson.get("词汇联想-即时记忆总成绩"));
                        colValue2.add(resultJson.get("词汇联想-延时记忆总成绩"));
                        JSONObject row2 = new JSONObject();
                        row2.put("name", "词汇联想");
                        row2.put("value", colValue2);
                        tableContext.add(row2);
                    } else if (memoryCheck.getQuestionNo().equals("3")) {
                        List colValue3 = new ArrayList();
                        colValue3.add(resultJson.get("图像自由回忆-即时记忆图像成绩"));
                        colValue3.add(resultJson.get("图像自由回忆-延时记忆图像成绩"));
                        JSONObject row3 = new JSONObject();
                        row3.put("name", "图象自由回忆");
                        row3.put("value", colValue3);
                        tableContext.add(row3);
                    } else if (memoryCheck.getQuestionNo().equals("4")) {
                        List colValue4 = new ArrayList();
                        colValue4.add(resultJson.get("图片联想-即时记忆即时成绩") + "、" + resultJson.get("图片联想-即时记忆即时再确认成绩") + "、" + resultJson.get("图片联想-即时记忆总成绩"));
                        colValue4.add(resultJson.get("图片联想-延时记忆即时成绩") + "、" + resultJson.get("图片联想-延时记忆即时再确认成绩") + "、" + resultJson.get("图片联想-延时记忆总成绩"));
                        JSONObject row4 = new JSONObject();
                        row4.put("name", "图片联想");
                        row4.put("value", colValue4);
                        tableContext.add(row4);
                    } else if (memoryCheck.getQuestionNo().equals("5")) {
                        List colValue5 = new ArrayList();
                        colValue5.add(resultJson.get("人像特点回忆-即时记忆人像成绩"));
                        colValue5.add(resultJson.get("人像特点回忆-延时记忆人像成绩"));
                        JSONObject row5 = new JSONObject();
                        row5.put("name", "人像特点");
                        row5.put("value", colValue5);
                        tableContext.add(row5);
                    } else if (memoryCheck.getQuestionNo().equals("7")) {
                        List colValue7 = new ArrayList();
                        colValue7.add(resultJson.get("前瞻记忆-总分"));
                        colValue7.add(resultJson.get("/"));
                        JSONObject row7 = new JSONObject();
                        row7.put("name", "前瞻记忆");
                        row7.put("value", colValue7);
                        tableContext.add(row7);
                    } else {
                        List colValue6 = new ArrayList();
                        colValue6.add(resultJson.get("视觉空间工作记忆-视觉空间成绩"));
                        colValue6.add(resultJson.get("/"));
                        JSONObject row6 = new JSONObject();
                        row6.put("name", "视觉空间工作记忆");
                        row6.put("value", colValue6);
                        tableContext.add(row6);
                    }
                }
            }
        } else {
            //列值
            List colValue1 = new ArrayList();
            colValue1.add(resultJson.get("指向记忆-即时记忆总成绩"));
            colValue1.add(resultJson.get("指向记忆-延时记忆总成绩"));
            JSONObject row1 = new JSONObject();
            row1.put("name", "指向记忆");
            row1.put("value", colValue1);
            tableContext.add(row1);

            List colValue2 = new ArrayList();
            colValue2.add(resultJson.get("词汇联想-即时记忆总成绩"));
            colValue2.add(resultJson.get("词汇联想-延时记忆总成绩"));
            JSONObject row2 = new JSONObject();
            row2.put("name", "词汇联想");
            row2.put("value", colValue2);
            tableContext.add(row2);

            List colValue3 = new ArrayList();
            colValue3.add(resultJson.get("图像自由回忆-即时记忆图像成绩"));
            colValue3.add(resultJson.get("图像自由回忆-延时记忆图像成绩"));
            JSONObject row3 = new JSONObject();
            row3.put("name", "图象自由回忆");
            row3.put("value", colValue3);
            tableContext.add(row3);

            List colValue4 = new ArrayList();
            colValue4.add(resultJson.get("图片联想-即时记忆即时成绩") + "、" + resultJson.get("图片联想-即时记忆即时再确认成绩") + "、" + resultJson.get("图片联想-即时记忆总成绩"));
            colValue4.add(resultJson.get("图片联想-延时记忆即时成绩") + "、" + resultJson.get("图片联想-延时记忆即时再确认成绩") + "、" + resultJson.get("图片联想-延时记忆总成绩"));
            JSONObject row4 = new JSONObject();
            row4.put("name", "图片联想");
            row4.put("value", colValue4);
            tableContext.add(row4);

            List colValue5 = new ArrayList();
            colValue5.add(resultJson.get("人像特点回忆-即时记忆人像成绩"));
            colValue5.add(resultJson.get("人像特点回忆-延时记忆人像成绩"));
            JSONObject row5 = new JSONObject();
            row5.put("name", "人像特点");
            row5.put("value", colValue5);
            tableContext.add(row5);

            List colValue6 = new ArrayList();
            colValue6.add(resultJson.get("视觉空间工作记忆-视觉空间成绩"));
            colValue6.add(resultJson.get("/"));
            JSONObject row6 = new JSONObject();
            row6.put("name", "视觉空间工作记忆");
            row6.put("value", colValue6);
            tableContext.add(row6);

            List colValue7 = new ArrayList();
            colValue7.add(resultJson.get("前瞻记忆-总分"));
            colValue7.add(resultJson.get("/"));
            JSONObject row7 = new JSONObject();
            row7.put("name", "前瞻记忆");
            row7.put("value", colValue7);
            tableContext.add(row7);
        }

        dbJson.put("tableContext", tableContext);
        dbJsonList.add(dbJson);

        int i = 1;
        // 获取第一层每个key对应的值 的类型
        for (String key : keys) {
            if (i == keys.size()) {
                testResult += key + ":" + resultJson.get(key);
            } else {
                testResult += key + ":" + resultJson.get(key) + ";";
            }
            i++;
        }
        //测试记录
        String filePath;
        filePath = "./心理检测/检测结果/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + name + "/" + fileName;
        UserRecordEntity userRecordEntity = testRecord(type, phone, institutionNo, testTime, filePath, dbJsonList.toString(), name, "", datas, testResult, testPlanId);
        System.out.println("resultJson:" + resultJson);
        return success(userRecordEntity.getId(), "完成");

    }*/

    private UserRecordEntity testRecord(String flag, String phone, String institutionNo, String testTime, String fileName, String testResult, String name, String b64Img, String datas, String testResults, String testPlanId) {
        //根据taskCode记录测试数
        this.cognitiveTaskService.addTestNum(flag);
        //添加用户测试记录
        UserRecordEntity userRecordEntity = new UserRecordEntity();
        userRecordEntity.setFlag(flag);
        userRecordEntity.setPhone(phone);
        userRecordEntity.setInstitutionNo(institutionNo);
        userRecordEntity.setTestDate(testTime);
        userRecordEntity.setTestResult(testResult);
        userRecordEntity.setType("1");
        userRecordEntity.setFileName(fileName);
        userRecordEntity.setName(name);
        userRecordEntity.setImg(b64Img);
        userRecordEntity.setTestRecord(datas);
        userRecordEntity.setTestResults(testResults);
        if (testPlanId == null || testPlanId.equals("")) {
            userRecordEntity.setTestPlanId("0");
        } else {
            userRecordEntity.setTestPlanId(testPlanId);
        }
        this.userRecordService.save(userRecordEntity);
        return userRecordEntity;
    }


    /**
     * 查询延时记忆是否可用
     *
     * @return
     */
    @SafetyProcess
    @PostMapping("/getIfUsable")
    @ApiOperation(value = "查询延时记忆是否可用")
    public Result getIfUsable(@RequestBody String json, HttpServletRequest request) throws Exception {
        String data = AesEncryptUtils.decrypt(JSONObject.parseObject(json).getString("data"));
        JSONObject jsonObject = JSONObject.parseObject(data);
        System.out.println("----------------result-------------------");System.out.println(jsonObject);
        String phone = jsonObject.getString("phone");
        String type = Constant.QUEST_TYPE_RTC;
        if (jsonObject.containsKey("type")) {
            type = jsonObject.getString("type");
        }
        String testType = Constant.DEFAULT_VALUE_ZERO;
        if (jsonObject.containsKey("testType")) {
            testType = jsonObject.getString("testType");
        }
        String questionNo = "";
        if (jsonObject.containsKey("questionNo")) {
            questionNo = jsonObject.getString("questionNo");
        }
        String token = request.getHeader("Authorization");
        token = token.split(" ")[1];//以空格划分Bearer token，获取token
        //从请求头中获取token
        DecodedJWT verify = JWTUtil.verify(token);
        String institutionNo = verify.getClaim("institutionNo").asString();
        //根据phone获取用户进行中记忆检查
        List<MemoryCheckEntity> memoryCheckEntityList = this.memoryCheckService.getMemoryCheckList(phone, type, testType,questionNo,institutionNo);
        if (memoryCheckEntityList.size() > 0) {

            DateFormat df = new SimpleDateFormat("yyyyMMddHHmm");
            Date beginTime = null;
            for (MemoryCheckEntity memoryCheckEntity : memoryCheckEntityList) {
                if (memoryCheckEntity.getMemoryType() == 1) {
                    //开始时间
                    beginTime = df.parse(memoryCheckEntity.getTestDate());
                    break;
                }
            }
            //当前时间
            Date endTime = df.parse(DateUtil.getNowTime("yyyyMMddHHmm"));
            //计算时间差---分钟
            int diff = DateUtil.getDistanceByUnit(beginTime, endTime, 2);
            if (diff < 0 || diff >= 30) {
                return success(memoryCheckEntityList);
            } else {
                int a = 30 - diff;
                return success(a, "还未到时间");
            }
        }
        return success(memoryCheckEntityList, "需要先进行即时测试");
    }

    /**
     * 清除进行中任务
     *
     * @return
     */
    @SafetyProcess
    @OperationLogAnnotation
    @PostMapping("/clearMemory")
    @ApiOperation(value = "清除进行中任务")
    public Result clearMemory(@RequestBody String json, HttpServletRequest request) throws Exception {
        //public Result saveMemoryCheck(@RequestBody JSONObject jsonObject ) throws Exception {
        String data = AesEncryptUtils.decrypt(JSONObject.parseObject(json).getString("data"));
        JSONObject jsonObject = JSONObject.parseObject(data);
        String phone = jsonObject.getString("phone");
        String type = Constant.QUEST_TYPE_RTC;
        if (jsonObject.containsKey("type")) {
            type = jsonObject.getString("type");
        }
        String testType = Constant.DEFAULT_VALUE_ZERO;
        if (jsonObject.containsKey("testType")) {
            testType = jsonObject.getString("testType");
        }
        String questionNo = "";
        if (jsonObject.containsKey("questionNo")) {
            questionNo = jsonObject.getString("questionNo");
        }
        String token = request.getHeader("Authorization");
        token = token.split(" ")[1];//以空格划分Bearer token，获取token
        //从请求头中获取token
        DecodedJWT verify = JWTUtil.verify(token);
        String institutionNo = verify.getClaim("institutionNo").asString();
        //清除进行中任务
        this.memoryCheckService.clearMemory(phone, type, testType,questionNo,institutionNo);
        return success();
    }
}
