package com.rf.psychological.scale.rest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rf.psychological.base.rest.BaseController;
import com.rf.psychological.dao.cognizeModel.*;
import com.rf.psychological.dao.model.*;
import com.rf.psychological.file.excel.ExcelBean;
import com.rf.psychological.institution.service.InstitutionService;
import com.rf.psychological.opLog.annotation.OperationLogAnnotation;
import com.rf.psychological.plan.dao.model.TestPlanEntity;
import com.rf.psychological.plan.service.TestPlanContendService;
import com.rf.psychological.plan.service.TestPlanService;
import com.rf.psychological.plan.service.TestPlanUserService;
import com.rf.psychological.rest.ServerController;
import com.rf.psychological.scale.dao.model.PsychologicalCapitalEntity;
import com.rf.psychological.scale.dao.model.ScaleConfigEntity;
import com.rf.psychological.scale.dao.model.UserRecordEntity;
import com.rf.psychological.scale.service.PsychologicalCapitalService;
import com.rf.psychological.scale.service.ScaleConfigService;
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.UserRecordService;
import com.rf.psychological.user.dao.model.UserEntity;
import com.rf.psychological.user.service.UserService;
import com.rf.psychological.socket.MyClient;
import com.rf.psychological.utils.*;
import com.rf.psychological.file.excel.ExcelClass;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zzf
 * @description:接收测试结果--认知任务
 * @date 2021/1/29 10:30
 */
@Slf4j
@RestController
@RequestMapping("/cognize")
@Api(tags = "认知任务测评结果计算")
public class CognizeResultController extends BaseController {

    @Autowired
    private UserRecordService userRecordService;

    @Autowired
    private UserService userService;

    @Autowired
    private CognitiveTaskService cognitiveTaskService;

    @Autowired
    private TestPlanContendService testPlanContendService;

    @Autowired
    private TestPlanUserService testPlanUserService;

    @Autowired
    private TestPlanService testPlanService;

    @Autowired
    private PsychologicalCapitalService psychologicalCapitalService;

    @Autowired
    private ScaleConfigService scaleConfigService;

    @Autowired
    private InstitutionService institutionService;


    @SafetyProcess
    @OperationLogAnnotation("认知任务测评结果保存")
    @ApiOperation(value = "type:认知任务标志，jsonObject：测试结果")
    @PostMapping("/{type}")
    public Result completeTest(@PathVariable String type, @RequestBody String json) throws Exception {
        JSONObject jsonObject = JSONObject.parseObject(AesEncryptUtils.decrypt(JSONObject.parseObject(json).getString("data")));
        String testPlanId = null;
        UserEntity userEntity = this.userService.getOne(jsonObject.getString("userId"));
        String userName = userEntity.getPetName();
        String userBirthday = userEntity.getBirthday();
        String userSex = userEntity.getGender();
        String phone = userEntity.getPhone();
        String institutionNo = userEntity.getInstitutionNo();
        //提交是校验测试次数是否达到上限
        if (institutionService.checkTestNum(institutionNo)){
            return fail(Constant.DEFAULT_VALUE_ONE,"测试次数已达上限，请联系机构管理员");
        }
        if (userSex.equals("0")) {
            userSex = "男";
        } else {
            userSex = "女";
        }
        if(userEntity == null){
            return fail("用户不存在");
        }
        if (jsonObject.containsKey("testPlanId") && StringUtils.isNotBlank(jsonObject.getString("testPlanId"))) {
            testPlanId = jsonObject.getString("testPlanId");
            TestPlanEntity testPlanEntity = this.testPlanService.findById(testPlanId);
            if(testPlanEntity == null ){
                return fail("测试计划不存在");
            }
            if(testPlanEntity.getStatus() == Constant.TEST_PLAN_STATUS_UNSTART)
                return fail("测试计划尚未开始");
            if(testPlanEntity.getStatus() < Constant.TEST_PLAN_STATUS_UNSTART)
                return fail("测试计划已结束");
            UserRecordEntity checkTestPlanId = userRecordService.findByPhoneAndTestPlanIdAndInstitution(institutionNo, phone, testPlanId, type);
            if (checkTestPlanId != null) {
                //靶向神经振荡任务完成所有27关才算结束测试计划
                if (type.equals("RHYTHM")){
                    if (checkTestPlanId.getImg().equals("27")){
                        return fail("", "该测试已完成");
                    }
                }else {
                    return fail("", "该测试已完成");
                }
            }
            WebScaleResultController.updateUserPlanStatus(testPlanId,institutionNo,phone,type,userEntity.getId(),testPlanContendService,testPlanUserService);
        }

        String testTime = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒").format(new Date());
        if (jsonObject.containsKey("startTime")) {
            testTime = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒").format(jsonObject.getDate("startTime"));
        }
        // 创建入库的json对象
        JSONObject resultDBJson = new JSONObject(true);
        // 创建入参的json对象
        JSONObject paramJson = new JSONObject(true);
        String fileName = userName + Constant.SPLIT_CHAR + userBirthday + Constant.SPLIT_CHAR + userSex + Constant.SPLIT_CHAR + testTime;
        String datas = null;
        String name = null;
        String testResult = "";
        String b64Img = "";
        paramJson.put("fileName", fileName);
        paramJson.put("userEntity", userEntity);
        paramJson.put("testTime", testTime);
        paramJson.put("type", type);
        paramJson.put("jsonObject", jsonObject);
        if (Constant.QUEST_TYPE_PSY_CAPITAL.equals(type)) {
            String taskMark = jsonObject.getString("taskMark");
            List<PsychologicalCapitalEntity> psyCapitalList = this.psychologicalCapitalService.getPsychologicalCapitalList(taskMark);
            List<UserRecordEntity> userRecordEntities = new ArrayList<UserRecordEntity>();
            for (PsychologicalCapitalEntity psyCapital : psyCapitalList) {
                if (!psyCapital.getHierarchy().equals("1.0")){
                    UserRecordEntity userRecordEntity = this.userRecordService.getUserRecordById(psyCapital.getRecordId());
                    userRecordEntities.add(userRecordEntity);
                }
            }
            paramJson.put("psyCapitalList", psyCapitalList);
            paramJson.put("userRecordEntities", userRecordEntities);
        }

        ScaleConfigEntity scaleConfigByFlag = scaleConfigService.findScaleConfigByFlag(type);
        if (scaleConfigByFlag == null) {
            return fail("该任务开发中，请联系管理员");
        }
        String className = scaleConfigByFlag.getResultConfig();
        Class<?> cls; // 取得Class对象
        try {
            cls = Class.forName(className);
            Constructor<?> cons = cls.getConstructor(JSONArray.class,JSONObject.class);
            //为构造方法传递参数
            Object obj = cons.newInstance(null,paramJson);
            Method method = cls.getDeclaredMethod("scaleCalculate");
            resultDBJson = (JSONObject) method.invoke(obj);
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException |
                InvocationTargetException e) {
            e.printStackTrace();
        }
        if (resultDBJson.containsKey("fileName")) {
            fileName = resultDBJson.getString("fileName");
            resultDBJson.remove("fileName");
        }
        if (resultDBJson.containsKey("name")) {
            name = resultDBJson.getString("name");
            resultDBJson.remove("name");
        }
        if (resultDBJson.containsKey("datas")) {
            datas = resultDBJson.getString("datas");
            resultDBJson.remove("datas");
        }
        if (resultDBJson.containsKey("b64Img")) {
            b64Img = resultDBJson.getString("b64Img");
            resultDBJson.remove("b64Img");
        }

        resultDBJson = resultDBJson.getJSONObject("resultJson");

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

        //计算结果
        JSONArray dbJsonList = new JSONArray();
        JSONObject dbJson = new JSONObject(true);
        JSONArray tableContext = new JSONArray();
        //列名
        List colNames = new ArrayList();
        if (Constant.QUEST_TYPE_MEMORY_YATAI.equals(type)) {
            colNames.add("类型");
            colNames.add("0-BACK");
            colNames.add("1-BACK");
            colNames.add("2-BACK");
            dbJson.put("colNames", colNames);
            //列值
            List colValue1 = new ArrayList();
            colValue1.add("低");
            colValue1.add("中");
            colValue1.add("高");
            JSONObject row1 = new JSONObject(true);
            row1.put("name", "难度等级");
            row1.put("value", colValue1);
            tableContext.add(row1);

            List colValue2 = new ArrayList();
            colValue2.add(resultDBJson.get("0-back总测试数"));
            colValue2.add(resultDBJson.get("1-back总测试数"));
            colValue2.add(resultDBJson.get("2-back总测试数"));
            JSONObject row2 = new JSONObject(true);
            row2.put("name", "总测试数");
            row2.put("value", colValue2);
            tableContext.add(row2);

            List colValue3 = new ArrayList();
            colValue3.add(resultDBJson.get("0-back正确数"));
            colValue3.add(resultDBJson.get("1-back正确数"));
            colValue3.add(resultDBJson.get("2-back正确数"));
            JSONObject row3 = new JSONObject(true);
            row3.put("name", "正确数");
            row3.put("value", colValue3);
            tableContext.add(row3);

            List colValue4 = new ArrayList();
            colValue4.add(resultDBJson.get("0-back正确率"));
            colValue4.add(resultDBJson.get("1-back正确率"));
            colValue4.add(resultDBJson.get("2-back正确率"));
            JSONObject row4 = new JSONObject(true);
            row4.put("name", "正确率");
            row4.put("value", colValue4);
            tableContext.add(row4);

            List colValue5 = new ArrayList();
            colValue5.add(resultDBJson.get("0-back平均反应时（ms）"));
            colValue5.add(resultDBJson.get("1-back平均反应时（ms）"));
            colValue5.add(resultDBJson.get("2-back平均反应时（ms）"));
            JSONObject row5 = new JSONObject(true);
            row5.put("name", "平均反应时（ms）");
            row5.put("value", colValue5);
            tableContext.add(row5);

            List colValue6 = new ArrayList();
            colValue6.add(resultDBJson.get("0-back测试用时"));
            colValue6.add(resultDBJson.get("1-back测试用时"));
            colValue6.add(resultDBJson.get("2-back测试用时"));
            JSONObject row6 = new JSONObject(true);
            row6.put("name", "测试用时");
            row6.put("value", colValue6);
            tableContext.add(row6);

        } else if (Constant.QUEST_TYPE_ALERTNESS_YATAI.equals(type)) {
            colNames.add("任务指标");
            colNames.add("结果");
            dbJson.put("colNames", colNames);
            // 获取第一层每个key对应的值 的类型
            for (String key : keys) {
                //列值
                List colValue = new ArrayList();
                colValue.add(resultDBJson.get(key));
                JSONObject row = new JSONObject(true);
                row.put("name", key);
                row.put("value", colValue);
                tableContext.add(row);
            }
        } else if (Constant.QUEST_TYPE_SHAPE_YATAI.equals(type)) {
            colNames.add("难度等级");
            colNames.add("总次数");
            colNames.add("正确次数");
            colNames.add("正确率");
            dbJson.put("colNames", colNames);
            //列值
            List colValue1 = new ArrayList();
            colValue1.add(resultDBJson.get("一级难度测试总数"));
            colValue1.add(resultDBJson.get("一级难度正确数"));
            colValue1.add(resultDBJson.get("一级难度正确率"));
            JSONObject row1 = new JSONObject(true);
            row1.put("name", "1");
            row1.put("value", colValue1);
            tableContext.add(row1);

            List colValue2 = new ArrayList();
            colValue2.add(resultDBJson.get("二级难度测试总数"));
            colValue2.add(resultDBJson.get("二级难度正确数"));
            colValue2.add(resultDBJson.get("二级难度正确率"));
            JSONObject row2 = new JSONObject(true);
            row2.put("name", "2");
            row2.put("value", colValue2);
            tableContext.add(row2);

            List colValue3 = new ArrayList();
            colValue3.add(resultDBJson.get("三级难度测试总数"));
            colValue3.add(resultDBJson.get("三级难度正确数"));
            colValue3.add(resultDBJson.get("三级难度正确率"));
            JSONObject row3 = new JSONObject(true);
            row3.put("name", "3");
            row3.put("value", colValue3);
            tableContext.add(row3);

            List colValue4 = new ArrayList();
            colValue4.add(resultDBJson.get("四级难度测试总数"));
            colValue4.add(resultDBJson.get("四级难度正确数"));
            colValue4.add(resultDBJson.get("四级难度正确率"));
            JSONObject row4 = new JSONObject(true);
            row4.put("name", "4");
            row4.put("value", colValue4);
            tableContext.add(row4);

            List colValue5 = new ArrayList();
            colValue5.add(resultDBJson.get("五级难度测试总数"));
            colValue5.add(resultDBJson.get("五级难度正确数"));
            colValue5.add(resultDBJson.get("五级难度正确率"));
            JSONObject row5 = new JSONObject(true);
            row5.put("name", "5");
            row5.put("value", colValue5);
            tableContext.add(row5);

            List colValue6 = new ArrayList();
            colValue6.add(resultDBJson.get("六级难度测试总数"));
            colValue6.add(resultDBJson.get("六级难度正确数"));
            colValue6.add(resultDBJson.get("六级难度正确率"));
            JSONObject row6 = new JSONObject(true);
            row6.put("name", "6");
            row6.put("value", colValue6);
            tableContext.add(row6);
        } else if (Constant.QUEST_TYPE_ANT_YATAI.equals(type) || Constant.QUEST_TYPE_ANT_FISH.equals(type) || Constant.QUEST_TYPE_ANT_FISH_YATAI.equals(type)) {
            colNames.add("测试条件");
            colNames.add("命中率");
            colNames.add("反应时（ms）");
            dbJson.put("colNames", colNames);
            //列值
            List colValue1 = new ArrayList();
            colValue1.add(resultDBJson.get("命中率"));
            colValue1.add(resultDBJson.get("反应时"));
            JSONObject row1 = new JSONObject(true);
            row1.put("name", "整体实验");
            row1.put("value", colValue1);
            tableContext.add(row1);

            List colValue2 = new ArrayList();
            colValue2.add(resultDBJson.get("一致性命中率"));
            colValue2.add(resultDBJson.get("一致性反应时"));
            JSONObject row2 = new JSONObject(true);
            row2.put("name", "一致条件");
            row2.put("value", colValue2);
            tableContext.add(row2);

            List colValue3 = new ArrayList();
            colValue3.add(resultDBJson.get("不一致性命中率"));
            colValue3.add(resultDBJson.get("不一致性反应时"));
            JSONObject row3 = new JSONObject(true);
            row3.put("name", "不一致条件");
            row3.put("value", colValue3);
            tableContext.add(row3);

            List colValue4 = new ArrayList();
            colValue4.add(resultDBJson.get("NoCue命中率"));
            colValue4.add(resultDBJson.get("NoCue反应时"));
            JSONObject row4 = new JSONObject(true);
            row4.put("name", "无线索");
            row4.put("value", colValue4);
            tableContext.add(row4);

            List colValue5 = new ArrayList();
            colValue5.add(resultDBJson.get("NeutralCue命中率"));
            colValue5.add(resultDBJson.get("NeutralCue反应时"));
            JSONObject row5 = new JSONObject(true);
            row5.put("name", "中立线索");
            row5.put("value", colValue5);
            tableContext.add(row5);

            List colValue6 = new ArrayList();
            colValue6.add(resultDBJson.get("SpatialCue命中率"));
            colValue6.add(resultDBJson.get("SpatialCue反应时"));
            JSONObject row6 = new JSONObject(true);
            row6.put("name", "空间线索");
            row6.put("value", colValue6);
            tableContext.add(row6);

            JSONObject dbJson2 = new JSONObject(true);
            JSONArray tableContext2 = new JSONArray();
            List colNames2 = new ArrayList();
            colNames2.add("警觉分数");
            colNames2.add("定向分数");
            colNames2.add("冲突分数");
            dbJson2.put("colNames", colNames2);
            List colValue7 = new ArrayList();
            colValue7.add(resultDBJson.get("定向分数"));
            colValue7.add(resultDBJson.get("冲突分数"));
            JSONObject row7 = new JSONObject(true);
            row7.put("name", resultDBJson.get("警觉分数"));
            row7.put("value", colValue7);
            tableContext2.add(row7);
            dbJson2.put("tableContext", tableContext2);
            dbJsonList.add(dbJson2);
        } else if (Constant.QUEST_TYPE_GTCT.equals(type)) {
            colNames.add("难度等级");
            colNames.add("正确率");
            dbJson.put("colNames", colNames);

            // 获取第一层每个key对应的值 的类型
            for (int i = 0; i < keys.size(); i++) {

            }
            int y = 1;
            for (String key : keys) {
                if (!key.equals(y + "难度等级")) {
                    //列值
                    List colValue = new ArrayList();
                    colValue.add(resultDBJson.get(key));
                    JSONObject row = new JSONObject(true);
                    row.put("name", y);
                    row.put("value", colValue);
                    tableContext.add(row);
                    y++;
                }
            }
        } else if (Constant.QUEST_TYPE_STROOP.equals(type)) {
            colNames.add("试验");
            colNames.add("完成时间/秒");
            colNames.add("错误数目");
            dbJson.put("colNames", colNames);
            //列值
            List colValue1 = new ArrayList();
            colValue1.add(resultDBJson.get("StroopD完成时间（秒）"));
            colValue1.add(resultDBJson.get("StroopD错误数目"));
            JSONObject row1 = new JSONObject(true);
            row1.put("name", "StroopD");
            row1.put("value", colValue1);
            tableContext.add(row1);

            List colValue2 = new ArrayList();
            colValue2.add(resultDBJson.get("StroopW完成时间（秒）"));
            colValue2.add(resultDBJson.get("StroopW错误数目"));
            JSONObject row2 = new JSONObject(true);
            row2.put("name", "StroopW");
            row2.put("value", colValue2);
            tableContext.add(row2);

            List colValue3 = new ArrayList();
            colValue3.add(resultDBJson.get("StroopC完成时间（秒）"));
            colValue3.add(resultDBJson.get("StroopC错误数目"));
            JSONObject row3 = new JSONObject(true);
            row3.put("name", "StroopC");
            row3.put("value", colValue3);
            tableContext.add(row3);

        } else if (Constant.QUEST_TYPE_STROOP_TASK.equals(type)) {
            colNames.add("类型");
            colNames.add("平均用时");
            colNames.add("平均正确率");
            dbJson.put("colNames", colNames);
            //列值
            List colValue1 = new ArrayList();
            colValue1.add(resultDBJson.get("同色同义平均用时"));
            colValue1.add(resultDBJson.get("同色同义平均正确率"));
            JSONObject row1 = new JSONObject(true);
            row1.put("name", "同色同义");
            row1.put("value", colValue1);
            tableContext.add(row1);

            List colValue2 = new ArrayList();
            colValue2.add(resultDBJson.get("不同义且互补色平均用时"));
            colValue2.add(resultDBJson.get("不同义且互补色平均正确率"));
            JSONObject row2 = new JSONObject(true);
            row2.put("name", "不同义且互补色");
            row2.put("value", colValue2);
            tableContext.add(row2);

            List colValue3 = new ArrayList();
            colValue3.add(resultDBJson.get("不同义且不为互补色平均用时"));
            colValue3.add(resultDBJson.get("不同义且不为互补色平均正确率"));
            JSONObject row3 = new JSONObject(true);
            row3.put("name", "不同义且不为互补色");
            row3.put("value", colValue3);
            tableContext.add(row3);

        } else {
            colNames.add("项目");
            colNames.add("内容");
            dbJson.put("colNames", colNames);

            // 获取第一层每个key对应的值 的类型
            for (String key : keys) {
                //列值
                List colValue = new ArrayList();
                colValue.add(resultDBJson.get(key));
                JSONObject row = new JSONObject(true);
                row.put("name", key);
                row.put("value", colValue);
                tableContext.add(row);
            }
        }

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

        int i = 1;
        // 获取第一层每个key对应的值 的类型
        for (String key : keys) {
            if (i == keys.size()) {
                testResult += key + ":" + resultDBJson.get(key);
            } else {
                testResult += key + ":" + resultDBJson.get(key) + ";";
            }
            i++;
        }
        //测试记录
        UserRecordEntity userRecordEntity ;
        String filePath = null;
        filePath = "./心理检测/检测结果/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + name + "/" + fileName;
        if (Constant.QUEST_TYPE_RIVEN.equals(type) || Constant.QUEST_TYPE_GO_NO_GO2.equals(type) || Constant.QUEST_TYPE_SC.equals(type)
                ||Constant.QUEST_TYPE_REM.equals(type) || Constant.QUEST_TYPE_MOVEMENT.equals(type) || Constant.QUEST_TYPE_MEMORY.equals(type)
                || Constant.QUEST_TYPE_MEMORYT.equals(type)  || Constant.QUEST_TYPE_RDT.equals(type) || Constant.QUEST_TYPE_GTCT.equals(type)
                || Constant.QUEST_TYPE_SHAPE.equals(type) || Constant.QUEST_TYPE_ANT.equals(type) || Constant.QUEST_TYPE_SHAPE2.equals(type)
                || Constant.QUEST_TYPE_SHAPE_RANDOM.equals(type) || Constant.QUEST_TYPE_SHAPE_CHILD.equals(type) || Constant.QUEST_TYPE_BIC.equals(type)
                || Constant.QUEST_TYPE_BIC_V2.equals(type) || Constant.QUEST_TYPE_GO_NO_GO.equals(type) || Constant.QUEST_TYPE_GO_NO_GO_ADDICTION.equals(type)
                || Constant.QUEST_TYPE_MEMORYT2.equals(type) || Constant.QUEST_TYPE_MTT_CHILD.equals(type) || Constant.QUEST_TYPE_MTT.equals(type)
                || Constant.QUEST_TYPE_ANT_FISH_V2.equals(type) || Constant.QUEST_TYPE_DST_VOICE.equals(type) || Constant.QUEST_TYPE_FDOT_CHILD.equals(type)
                || Constant.QUEST_TYPE_ALERTNESS.equals(type) || Constant.QUEST_TYPE_ALERTNESS_CHILD.equals(type) || Constant.QUEST_TYPE_ALERTNESS_ADDICTION.equals(type)
                || Constant.QUEST_TYPE_DST.equals(type) || Constant.QUEST_TYPE_DSST.equals(type) || Constant.QUEST_TYPE_STTA.equals(type)
                || Constant.QUEST_TYPE_STTB.equals(type) || Constant.QUEST_TYPE_VAS.equals(type) || Constant.QUEST_TYPE_ADDITION.equals(type)
                || Constant.QUEST_TYPE_ANT_FISH.equals(type) || Constant.QUEST_TYPE_MOVEMENT_CHILD.equals(type) || Constant.QUEST_TYPE_MEMORYTV3.equals(type)
                || Constant.QUEST_TYPE_FACEDOT_ADDICTION.equals(type) || Constant.QUEST_TYPE_FDOT.equals(type) || Constant.QUEST_TYPE_AMWSTask.equals(type)){
            userRecordEntity = testRecord(type, phone, institutionNo, testTime, filePath, resultDBJson.toString(), name, b64Img, datas, testResult, testPlanId);
        }else {
            userRecordEntity = testRecord(type, phone, institutionNo, testTime, filePath, dbJsonList.toString(), name, b64Img, datas, testResult, testPlanId);
        }
        return success(userRecordEntity.getId(), "完成");

    }

    //瑞文根据年龄分值转换
    private int RIVENScore(String userBirthday, int score) throws Exception {
        int resultScore = 0;
        double age = AgeUtil.getAge(userBirthday);
        if (age <= 5.5) {
            if (score >= 9 && score < 12) {
                resultScore = 5;
            } else if (score >= 12 && score < 13) {
                resultScore = 10;
            } else if (score >= 13 && score < 16) {
                resultScore = 25;
            } else if (score >= 16 && score < 25) {
                resultScore = 50;
            } else if (score >= 25 && score < 29) {
                resultScore = 75;
            } else if (score >= 29 && score < 34) {
                resultScore = 90;
            } else if (score >= 34) {
                resultScore = 95;
            }
        } else if (age == 6) {
            if (score >= 9 && score < 12) {
                resultScore = 5;
            } else if (score >= 12 && score < 13) {
                resultScore = 10;
            } else if (score >= 13 && score < 17) {
                resultScore = 25;
            } else if (score >= 17 && score < 25) {
                resultScore = 50;
            } else if (score >= 25 && score < 31) {
                resultScore = 75;
            } else if (score >= 31 && score < 36) {
                resultScore = 90;
            } else if (score >= 36) {
                resultScore = 95;
            }
        } else if (age == 6.5) {
            if (score >= 10 && score < 12) {
                resultScore = 5;
            } else if (score >= 12 && score < 13) {
                resultScore = 10;
            } else if (score >= 13 && score < 18) {
                resultScore = 25;
            } else if (score >= 18 && score < 25) {
                resultScore = 50;
            } else if (score >= 25 && score < 31) {
                resultScore = 75;
            } else if (score >= 31 && score < 37) {
                resultScore = 90;
            } else if (score >= 37) {
                resultScore = 95;
            }
        } else if (age == 7) {
            if (score >= 10 && score < 12) {
                resultScore = 5;
            } else if (score >= 12 && score < 13) {
                resultScore = 10;
            } else if (score >= 13 && score < 19) {
                resultScore = 25;
            } else if (score >= 19 && score < 25) {
                resultScore = 50;
            } else if (score >= 25 && score < 36) {
                resultScore = 75;
            } else if (score >= 36 && score < 43) {
                resultScore = 90;
            } else if (score >= 43) {
                resultScore = 95;
            }
        } else if (age == 7.5) {
            if (score >= 10 && score < 12) {
                resultScore = 5;
            } else if (score >= 12 && score < 13) {
                resultScore = 10;
            } else if (score >= 13 && score < 21) {
                resultScore = 25;
            } else if (score >= 21 && score < 31) {
                resultScore = 50;
            } else if (score >= 31 && score < 38) {
                resultScore = 75;
            } else if (score >= 38 && score < 44) {
                resultScore = 90;
            } else if (score >= 44) {
                resultScore = 95;
            }
        } else if (age == 8) {
            if (score >= 10 && score < 13) {
                resultScore = 5;
            } else if (score >= 13 && score < 15) {
                resultScore = 10;
            } else if (score >= 15 && score < 23) {
                resultScore = 25;
            } else if (score >= 23 && score < 31) {
                resultScore = 50;
            } else if (score >= 31 && score < 39) {
                resultScore = 75;
            } else if (score >= 39 && score < 44) {
                resultScore = 90;
            } else if (score >= 44) {
                resultScore = 95;
            }
        } else if (age == 8.5) {
            if (score >= 12 && score < 14) {
                resultScore = 5;
            } else if (score >= 14 && score < 20) {
                resultScore = 10;
            } else if (score >= 20 && score < 29) {
                resultScore = 25;
            } else if (score >= 29 && score < 33) {
                resultScore = 50;
            } else if (score >= 33 && score < 40) {
                resultScore = 75;
            } else if (score >= 40 && score < 45) {
                resultScore = 90;
            } else if (score >= 45) {
                resultScore = 95;
            }

        } else if (age == 9) {
            if (score >= 12 && score < 14) {
                resultScore = 5;
            } else if (score >= 14 && score < 25) {
                resultScore = 10;
            } else if (score >= 25 && score < 33) {
                resultScore = 25;
            } else if (score >= 33 && score < 37) {
                resultScore = 50;
            } else if (score >= 37 && score < 43) {
                resultScore = 75;
            } else if (score >= 43 && score < 47) {
                resultScore = 90;
            } else if (score >= 47) {
                resultScore = 95;
            }

        } else if (age == 9.5) {
            if (score >= 13 && score < 17) {
                resultScore = 5;
            } else if (score >= 17 && score < 27) {
                resultScore = 10;
            } else if (score >= 27 && score < 35) {
                resultScore = 25;
            } else if (score >= 35 && score < 39) {
                resultScore = 50;
            } else if (score >= 39 && score < 47) {
                resultScore = 75;
            } else if (score >= 47 && score < 50) {
                resultScore = 90;
            } else if (score >= 50) {
                resultScore = 95;
            }

        } else if (age == 10) {
            if (score >= 13 && score < 17) {
                resultScore = 5;
            } else if (score >= 17 && score < 27) {
                resultScore = 10;
            } else if (score >= 27 && score < 35) {
                resultScore = 25;
            } else if (score >= 35 && score < 42) {
                resultScore = 50;
            } else if (score >= 42 && score < 48) {
                resultScore = 75;
            } else if (score >= 48 && score < 50) {
                resultScore = 90;
            } else if (score >= 50) {
                resultScore = 95;
            }

        } else if (age == 10.5) {
            if (score >= 18 && score < 25) {
                resultScore = 5;
            } else if (score >= 25 && score < 32) {
                resultScore = 10;
            } else if (score >= 32 && score < 39) {
                resultScore = 25;
            } else if (score >= 39 && score < 42) {
                resultScore = 50;
            } else if (score >= 42 && score < 49) {
                resultScore = 75;
            } else if (score >= 49 && score < 50) {
                resultScore = 90;
            } else if (score >= 50) {
                resultScore = 95;
            }

        } else if (age == 11) {
            if (score >= 19 && score < 25) {
                resultScore = 5;
            } else if (score >= 25 && score < 33) {
                resultScore = 10;
            } else if (score >= 33 && score < 39) {
                resultScore = 25;
            } else if (score >= 39 && score < 43) {
                resultScore = 50;
            } else if (score >= 43 && score < 50) {
                resultScore = 75;
            } else if (score >= 50 && score < 52) {
                resultScore = 90;
            } else if (score >= 52) {
                resultScore = 95;
            }

        } else if (age == 11.5) {
            if (score >= 19 && score < 25) {
                resultScore = 5;
            } else if (score >= 25 && score < 35) {
                resultScore = 10;
            } else if (score >= 35 && score < 42) {
                resultScore = 25;
            } else if (score >= 42 && score < 45) {
                resultScore = 50;
            } else if (score >= 45 && score < 50) {
                resultScore = 75;
            } else if (score >= 50 && score < 53) {
                resultScore = 90;
            } else if (score >= 53) {
                resultScore = 95;
            }

        } else if (age == 12) {
            if (score >= 21 && score < 27) {
                resultScore = 5;
            } else if (score >= 27 && score < 37) {
                resultScore = 10;
            } else if (score >= 37 && score < 42) {
                resultScore = 25;
            } else if (score >= 42 && score < 46) {
                resultScore = 50;
            } else if (score >= 46 && score < 50) {
                resultScore = 75;
            } else if (score >= 50 && score < 53) {
                resultScore = 90;
            } else if (score >= 53) {
                resultScore = 95;
            }

        } else if (age == 12.5) {
            if (score >= 28 && score < 33) {
                resultScore = 5;
            } else if (score >= 33 && score < 40) {
                resultScore = 10;
            } else if (score >= 40 && score < 45) {
                resultScore = 25;
            } else if (score >= 45 && score < 50) {
                resultScore = 50;
            } else if (score >= 50 && score < 52) {
                resultScore = 75;
            } else if (score >= 52 && score < 53) {
                resultScore = 90;
            } else if (score >= 53) {
                resultScore = 95;
            }

        } else if (age == 13) {
            if (score >= 30 && score < 35) {
                resultScore = 5;
            } else if (score >= 35 && score < 40) {
                resultScore = 10;
            } else if (score >= 40 && score < 45) {
                resultScore = 25;
            } else if (score >= 45 && score < 50) {
                resultScore = 50;
            } else if (score >= 50 && score < 52) {
                resultScore = 75;
            } else if (score >= 52 && score < 53) {
                resultScore = 90;
            } else if (score >= 53) {
                resultScore = 95;
            }

        } else if (age == 13.5) {
            if (score >= 32 && score < 35) {
                resultScore = 5;
            } else if (score >= 35 && score < 42) {
                resultScore = 10;
            } else if (score >= 42 && score < 46) {
                resultScore = 25;
            } else if (score >= 46 && score < 50) {
                resultScore = 50;
            } else if (score >= 50 && score < 52) {
                resultScore = 75;
            } else if (score >= 52 && score < 54) {
                resultScore = 90;
            } else if (score >= 54) {
                resultScore = 95;
            }

        } else if (age == 14) {
            if (score >= 34 && score < 36) {
                resultScore = 5;
            } else if (score >= 36 && score < 43) {
                resultScore = 10;
            } else if (score >= 43 && score < 48) {
                resultScore = 25;
            } else if (score >= 48 && score < 50) {
                resultScore = 50;
            } else if (score >= 50 && score < 52) {
                resultScore = 75;
            } else if (score >= 52 && score < 55) {
                resultScore = 90;
            } else if (score >= 55) {
                resultScore = 95;
            }

        } else if (age == 14.5) {
            if (score >= 34 && score < 36) {
                resultScore = 5;
            } else if (score >= 36 && score < 43) {
                resultScore = 10;
            } else if (score >= 43 && score < 48) {
                resultScore = 25;
            } else if (score >= 48 && score < 51) {
                resultScore = 50;
            } else if (score >= 51 && score < 53) {
                resultScore = 75;
            } else if (score >= 53 && score < 55) {
                resultScore = 90;
            } else if (score >= 55) {
                resultScore = 95;
            }

        } else if (age == 15) {
            if (score >= 34 && score < 36) {
                resultScore = 5;
            } else if (score >= 36 && score < 43) {
                resultScore = 10;
            } else if (score >= 43 && score < 48) {
                resultScore = 25;
            } else if (score >= 48 && score < 51) {
                resultScore = 50;
            } else if (score >= 51 && score < 54) {
                resultScore = 75;
            } else if (score >= 54 && score < 57) {
                resultScore = 90;
            } else if (score >= 57) {
                resultScore = 95;
            }

        } else if (age == 15.5) {
            if (score >= 34 && score < 41) {
                resultScore = 5;
            } else if (score >= 41 && score < 43) {
                resultScore = 10;
            } else if (score >= 43 && score < 49) {
                resultScore = 25;
            } else if (score >= 49 && score < 52) {
                resultScore = 50;
            } else if (score >= 52 && score < 55) {
                resultScore = 75;
            } else if (score >= 55 && score < 57) {
                resultScore = 90;
            } else if (score >= 57) {
                resultScore = 95;
            }

        } else if (age == 16) {
            if (score >= 36 && score < 41) {
                resultScore = 5;
            } else if (score >= 41 && score < 44) {
                resultScore = 10;
            } else if (score >= 44 && score < 49) {
                resultScore = 25;
            } else if (score >= 49 && score < 53) {
                resultScore = 50;
            } else if (score >= 53 && score < 56) {
                resultScore = 75;
            } else if (score >= 56 && score < 57) {
                resultScore = 90;
            } else if (score >= 57) {
                resultScore = 95;
            }

        } else if (age == 16.5) {
            if (score >= 37 && score < 41) {
                resultScore = 5;
            } else if (score >= 41 && score < 45) {
                resultScore = 10;
            } else if (score >= 45 && score < 49) {
                resultScore = 25;
            } else if (score >= 49 && score < 53) {
                resultScore = 50;
            } else if (score >= 53 && score < 56) {
                resultScore = 75;
            } else if (score >= 56 && score < 57) {
                resultScore = 90;
            } else if (score >= 57) {
                resultScore = 95;
            }

        } else if (age == 17) {
            if (score >= 37 && score < 40) {
                resultScore = 5;
            } else if (score >= 40 && score < 47) {
                resultScore = 10;
            } else if (score >= 47 && score < 52) {
                resultScore = 25;
            } else if (score >= 52 && score < 55) {
                resultScore = 50;
            } else if (score >= 55 && score < 57) {
                resultScore = 75;
            } else if (score >= 57 && score < 58) {
                resultScore = 90;
            } else if (score >= 58) {
                resultScore = 95;
            }

        } else if (age > 17 && age <= 24.5) {//20
            if (score >= 33 && score < 38) {
                resultScore = 5;
            } else if (score >= 38 && score < 44) {
                resultScore = 10;
            } else if (score >= 44 && score < 50) {
                resultScore = 25;
            } else if (score >= 50 && score < 54) {
                resultScore = 50;
            } else if (score >= 54 && score < 56) {
                resultScore = 75;
            } else if (score >= 56 && score < 57) {
                resultScore = 90;
            } else if (score >= 57) {
                resultScore = 95;
            }

        } else if (age >= 25 && age <= 34.5) {//30
            if (score >= 28 && score < 37) {
                resultScore = 5;
            } else if (score >= 37 && score < 43) {
                resultScore = 10;
            } else if (score >= 43 && score < 48) {
                resultScore = 25;
            } else if (score >= 48 && score < 52) {
                resultScore = 50;
            } else if (score >= 52 && score < 55) {
                resultScore = 75;
            } else if (score >= 55 && score < 57) {
                resultScore = 90;
            } else if (score >= 57) {
                resultScore = 95;
            }

        } else if (age >= 35 && age <= 44.5) {//40
            if (score >= 28 && score < 31) {
                resultScore = 5;
            } else if (score >= 31 && score < 41) {
                resultScore = 10;
            } else if (score >= 41 && score < 47) {
                resultScore = 25;
            } else if (score >= 47 && score < 50) {
                resultScore = 50;
            } else if (score >= 50 && score < 54) {
                resultScore = 75;
            } else if (score >= 54 && score < 57) {
                resultScore = 90;
            } else if (score >= 57) {
                resultScore = 95;
            }

        } else if (age >= 45 && age <= 54.5) {//50
            if (score >= 21 && score < 24) {
                resultScore = 5;
            } else if (score >= 24 && score < 34) {
                resultScore = 10;
            } else if (score >= 34 && score < 42) {
                resultScore = 25;
            } else if (score >= 42 && score < 48) {
                resultScore = 50;
            } else if (score >= 48 && score < 52) {
                resultScore = 75;
            } else if (score >= 52 && score < 54) {
                resultScore = 90;
            } else if (score >= 54) {
                resultScore = 95;
            }

        } else if (age >= 55 && age <= 64.5) {//60
            if (score >= 19 && score < 22) {
                resultScore = 5;
            } else if (score >= 22 && score < 30) {
                resultScore = 10;
            } else if (score >= 30 && score < 37) {
                resultScore = 25;
            } else if (score >= 37 && score < 46) {
                resultScore = 50;
            } else if (score >= 46 && score < 52) {
                resultScore = 75;
            } else if (score >= 52 && score < 54) {
                resultScore = 90;
            } else if (score >= 54) {
                resultScore = 95;
            }

        } else {//70
            if (score >= 17 && score < 18) {
                resultScore = 5;
            } else if (score >= 18 && score < 26) {
                resultScore = 10;
            } else if (score >= 26 && score < 33) {
                resultScore = 25;
            } else if (score >= 33 && score < 44) {
                resultScore = 50;
            } else if (score >= 44 && score < 49) {
                resultScore = 75;
            } else if (score >= 49 && score < 52) {
                resultScore = 90;
            } else if (score >= 52) {
                resultScore = 95;
            }
        }

        return resultScore;
    }

    private UserRecordEntity testRecord(String taskCode, String phone, String institutionNo, String testTime, String fileName, String testResult, String name, String b64Img, String datas, String testResults, String testPlanId) {
        //根据taskCode记录测试数
        this.cognitiveTaskService.addTestNum(taskCode);
        //添加用户测试记录
        UserRecordEntity userRecordEntity = new UserRecordEntity();
        userRecordEntity.setFlag(taskCode);
        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;
    }


}
