package com.rf.psychological.scale.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.base.rest.BaseController;
import com.rf.psychological.file.excel.ExcelClass;
import com.rf.psychological.dao.model.*;
import com.rf.psychological.scale.dao.model.ScaleEntity;
import com.rf.psychological.security.SafetyProcess;
import com.rf.psychological.scale.service.ScaleService;
import com.rf.psychological.utils.Constant;
import com.rf.psychological.utils.ExcelUtil;
import com.rf.psychological.utils.Result;
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.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author zzf
 * @description:接收测试结果计算结果--量表测试
 * @date 2021/1/21 16:07
 */
@Slf4j
@RestController
@RequestMapping("/appResult")
@Api(tags = "量表测试结果计算")
public class ScaleResultController extends BaseController {

    @Autowired
    private ScaleService scaleService;

    //@SafetyProcess
    @PostMapping("/{type}")
    @ApiOperation(value = "type:量表标志，jsonObject：测试结果")
    public Result completeTest(@RequestBody JSONObject jsonObject, @PathVariable String type) throws Exception {
        System.out.println("----------------result-------------------");System.out.println(jsonObject);
        String userName = jsonObject.getString("userName");
        String userBirthday=jsonObject.getString("userBirthday");
        String userSex=jsonObject.getString("userSex");
        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"));
        }
        String fileName = userName+ Constant.SPLIT_CHAR+userBirthday+Constant.SPLIT_CHAR+userSex+Constant.SPLIT_CHAR+testTime;
        JSONArray jsonArray = jsonObject.getJSONArray("data");
        List<Map<String,String>> resultMapList = null;
        //艾森克
        if(Constant.QUEST_TYPE_ASK.equals(type)){
            int scoreE=0;
            int scoreN=0;
            int scoreP=0;
            int scoreL=0;
            for(int i=0;i<jsonArray.size();i++){
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                if(jsonObject1.getString("scale").equals(Constant.SCALE_TYPE_E)&&jsonObject1.getString("checkItems").equals(Constant.YES)){
                    scoreE +=1;
                }else if(jsonObject1.getString("scale").equals(Constant.SCALE_TYPE_N)&&jsonObject1.getString("checkItems").equals(Constant.YES)){
                    scoreN +=1;
                }else if(jsonObject1.getString("scale").equals(Constant.SCALE_TYPE_P)&&jsonObject1.getString("checkItems").equals(Constant.YES)){
                    scoreP +=1;
                }else if(jsonObject1.getString("scale").equals(Constant.SCALE_TYPE_L)&&jsonObject1.getString("checkItems").equals(Constant.YES)){
                    scoreL +=1;
                }
            }
            //答题结果列表
            List<ASKEntity> dataList= jsonArray.toJavaList(ASKEntity.class);
            //得分列表
            Map<String,String> resultMap = new LinkedHashMap<>();
            resultMap.put("分量E得分",String.valueOf(scoreE));
            resultMap.put("分量N得分",String.valueOf(scoreN));
            resultMap.put("分量P得分",String.valueOf(scoreP));
            resultMap.put("分量L得分",String.valueOf(scoreL));
            System.out.println("分量E得分："+scoreE+";分量N得分："+scoreN+";分量P得分："+scoreP+";分量L得分："+scoreL);
            fileName = fileName+"-"+Constant.SHEET_NAME_ASK+".xlsx";
            resultMapList = new ArrayList<>();
            resultMapList.add(resultMap);
//            导出文件
            ExcelUtil.createExcelFile(ASKEntity.class,dataList,resultMapList,new ExcelClass().contentExcel(Constant.QUEST_TYPE_ASK),fileName,Constant.SHEET_NAME_ASK);
            //匹兹堡睡眠质量指数
        }else if(Constant.QUEST_TYPE_PSQI.equals(type)){
            //睡眠质量
            int scoreQuality = checkQuality(jsonArray);
            //入睡时间
            int scoreTime = checkTime(jsonArray);
            //睡眠时间
            int scoreHour = checkHour(jsonArray);
            //睡眠效率
            int scoreEfficiency = checkEfficiency(jsonArray);
            //睡眠障碍
            int scoreObstruct = checkObstruct(jsonArray);
            //催眠药物
            int scoreMedicine = checkMedicine(jsonArray);
            //日间功能障碍
            int scoreDay = checkDay(jsonArray);
            System.out.println("A睡眠质量："+scoreQuality+";B入睡时间："+scoreTime+";C睡眠时间："+scoreHour+";D睡眠效率："+scoreEfficiency+";E失眠障碍："+scoreObstruct+";F催眠药物："+scoreMedicine+";日间工程障碍："+scoreDay);
            int score = scoreQuality+scoreTime+scoreHour+scoreEfficiency+scoreObstruct+scoreMedicine+scoreDay;
            System.out.println("PSQI总分："+score);
            Map<String,String> resultMap = new LinkedHashMap<>();
            resultMap.put("A睡眠质量",String.valueOf(scoreQuality));
            resultMap.put("B入睡时间",String.valueOf(scoreTime));
            resultMap.put("C睡眠时间",String.valueOf(scoreHour));
            resultMap.put("D睡眠效率",String.valueOf(scoreEfficiency));
            resultMap.put("E失眠障碍",String.valueOf(scoreObstruct));
            resultMap.put("F催眠药物",String.valueOf(scoreMedicine));
            resultMap.put("日间工程障碍",String.valueOf(scoreDay));
            resultMap.put("PSQI总分",String.valueOf(score));
            fileName = fileName+"-"+Constant.SHEET_NAME_PSQI+".xlsx";
            List<PSQIEntity> dataList = jsonArray.toJavaList(PSQIEntity.class);
            resultMapList = new ArrayList<>();
            resultMapList.add(resultMap);
            ExcelUtil.createExcelFile(PSQIEntity.class,dataList,resultMapList,new ExcelClass().contentExcel(Constant.QUEST_TYPE_PSQI),fileName,Constant.SHEET_NAME_PSQI);
            //心境状态
        }else if(Constant.QUEST_TYPE_POMS.equals(type)){
            int [] scores= {0,0,0,0,0,0,0,0};
            for (int i=0;i<jsonArray.size();i++) {
                JSONObject jsonObject1= jsonArray.getJSONObject(i);
                int model = jsonObject1.getIntValue("type");
                String checked = jsonObject1.getString("checked");
                    if(checked.equals("几乎没有")){

                    }else if (checked.equals("有点")){
                        scores[model-1] +=1;
                    }else if (checked.equals("适中")){
                        scores[model-1] +=2;
                    }else if (checked.equals("相当多")){
                        scores[model-1] +=3;
                    }else if (checked.equals("⾮常多")){
                        scores[model-1] +=4;
                    }
            }
            //TMD(情绪纷乱的总分)=5个消极的情绪得分之和减去两个积极情绪(精⼒，⾃尊感)得分之和+100
            scores[7]=scores[0]+scores[1]+scores[2]+scores[3]+scores[5]-scores[4]-scores[6]+100;
            System.out.println("POMS得分："+scores.toString());
            fileName = fileName+"-"+Constant.SHEET_NAME_POMS+".xlsx";
            Map<String,String> resultMap = new LinkedHashMap<>();
            resultMap.put("紧张",String.valueOf(scores[0]));
            resultMap.put("愤怒",String.valueOf(scores[1]));
            resultMap.put("疲劳",String.valueOf(scores[2]));
            resultMap.put("抑郁",String.valueOf(scores[3]));
            resultMap.put("精力",String.valueOf(scores[4]));
            resultMap.put("慌乱",String.valueOf(scores[5]));
            resultMap.put("自我情绪相关",String.valueOf(scores[6]));
            resultMap.put("总分",String.valueOf(scores[7]));
            resultMapList = new ArrayList<>();
            resultMapList.add(resultMap);
            ExcelUtil.createExcelFile(POMSEntity.class,jsonArray.toJavaList(POMSEntity.class),resultMapList,new ExcelClass().contentExcel(Constant.QUEST_TYPE_POMS),fileName,Constant.SHEET_NAME_POMS);
            //症状自评
        }else if (Constant.QUEST_TYPE_SCL.equals(type)){
            int [] scores = {0,0,0,0,0,0,0,0,0,0};
            for (int i=0;i<jsonArray.size();i++){
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                int model = Integer.parseInt(jsonObject1.getString("type"));
                String checked = jsonObject1.getString("checked");
                if(checked.equals("从无")){

                }else if (checked.equals("很轻")){
                    scores[model] +=1;
                }else if (checked.equals("中等")){
                    scores[model] +=2;
                }else if (checked.equals("偏重")){
                    scores[model] +=3;
                }else if (checked.equals("严重")){
                    scores[model] +=4;
                }
            }
            System.out.println("SCL-90各项得分："+scores.toString());
            //分量编号 0躯体化 1强迫症状 2人际关系敏感 3抑郁 4焦虑 5敌对 6恐怖 7偏执 8精神病性 9 其他
            Map<String,String> resultMap = new LinkedHashMap<>();
            resultMap.put("躯体化",String.valueOf(scores[0]));
            resultMap.put("强迫症状",String.valueOf(scores[1]));
            resultMap.put("人际关系敏感",String.valueOf(scores[2]));
            resultMap.put("抑郁",String.valueOf(scores[3]));
            resultMap.put("焦虑",String.valueOf(scores[4]));
            resultMap.put("敌对",String.valueOf(scores[5]));
            resultMap.put("恐怖",String.valueOf(scores[6]));
            resultMap.put("偏执",String.valueOf(scores[7]));
            resultMap.put("精神病性",String.valueOf(scores[8]));
            resultMap.put("其他",String.valueOf(scores[9]));
            fileName = fileName+"-"+Constant.SHEET_NAME_SCL+".xlsx";
            resultMapList = new ArrayList<>();
            resultMapList.add(resultMap);
            ExcelUtil.createExcelFile(SCLEntity.class,jsonArray.toJavaList(SCLEntity.class),resultMapList,new ExcelClass().contentExcel(Constant.QUEST_TYPE_SCL),fileName,Constant.SHEET_NAME_SCL);
            //汉化版美国航空航天局任务负荷指数量表
        }else if(Constant.QUEST_TYPE_AVAI.equals(type)){
            int score = 0 ;
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                score += jsonObject1.getInteger("checked");
            }
            //平均分保留两位小数
            double avgScore = new BigDecimal((double)score/(double)jsonArray.size()).setScale(2, RoundingMode.HALF_UP).doubleValue();
            System.out.println("总得分："+score+";平均得分："+avgScore);
            fileName = fileName+"-"+Constant.SHEET_NAME_AVAI+".xlsx";
            Map<String,String> resultMap = new LinkedHashMap<>();
            resultMap.put("总得分",String.valueOf(score));
            resultMap.put("平均得分",String.valueOf(avgScore));
            resultMapList = new ArrayList<>();
            resultMapList.add(resultMap);
            ExcelUtil.createExcelFile(AviationEntity.class,jsonArray.toJavaList(AviationEntity.class),resultMapList,new ExcelClass().contentExcel(Constant.QUEST_TYPE_AVAI),fileName,Constant.SHEET_NAME_AVAI);
            //社会支持评定量表
        }else if (Constant.QUEST_TYPE_SUPP.equals(type)){
            //总分
            int sumScore = 0;
            //主管得分
            int chargeScore = 0;
            //客观得分
            int impersonalityScore = 0;
            //利用度
            int utilizationScore = 0;
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                String checked = jsonObject1.getString("checked");
                if(i==0||(i>=2&&i<=5)||(i==8)){
                    if(checked.contains("(1)")){
                        sumScore += 1 ;
                        chargeScore +=1;
                    }else if(checked.contains("(2)")){
                        sumScore += 2 ;
                        chargeScore += 2;
                    }else if(checked.contains("(3)")){
                        sumScore += 3 ;
                        chargeScore+= 3;
                    }else if(checked.contains("(4)")){
                        sumScore += 4 ;
                        chargeScore+=4;
                    }
                }else if(i==1){
                    if(checked.contains("(1)")){
                       impersonalityScore +=1;
                        sumScore += 1 ;
                    }else if(checked.contains("(2)")){
                        impersonalityScore+=2;
                        sumScore += 2 ;
                    }else if(checked.contains("(3)")){
                        impersonalityScore+=3;
                        sumScore += 3 ;
                    }else if(checked.contains("(4)")){
                        impersonalityScore+=4;
                        sumScore += 4 ;
                    }
                }else if(i==6||i==7){
                    if(checked.equals("无任何来源")){

                    }else {
                        String [] strings= checked.replace("[","").replace("]","").split("，");
                        int length = strings.length;
                        sumScore += length;
                        impersonalityScore +=length;
                    }

                }else if(i==9||i==10){
                    if(checked.contains("(1)")){

                    }else if(checked.contains("(2)")){
                        impersonalityScore += checked.split(";").length;
                        sumScore +=  checked.split(";").length;;
                    }
                }else {
                    if(checked.contains("(1)")){
                        utilizationScore +=1;
                        sumScore += 1 ;
                    }else if(checked.contains("(2)")){
                        utilizationScore+=2;
                        sumScore += 2 ;
                    }else if(checked.contains("(3)")){
                        utilizationScore+=3;
                        sumScore += 3 ;
                    }else if(checked.contains("(4)")){
                        utilizationScore+=4;
                        sumScore += 4 ;
                    }

                }
            }
            System.out.println("总分："+sumScore+";客观得分："+impersonalityScore+";主观得分："+chargeScore+";支持的利用度："+utilizationScore);
            fileName=fileName+"-"+Constant.SHEET_NAME_SUPP+".xlsx";
            Map<String,String> resultMap = new LinkedHashMap<>();
            resultMap.put("客观得分",String.valueOf(impersonalityScore));
            resultMap.put("主观得分",String.valueOf(chargeScore));
            resultMap.put("支持的利用度",String.valueOf(utilizationScore));
            resultMap.put("总分",String.valueOf(sumScore));
            resultMapList = new ArrayList<>();
            resultMapList.add(resultMap);
            ExcelUtil.createExcelFile(SupportEntity.class,jsonArray.toJavaList(SupportEntity.class),resultMapList,new ExcelClass().contentExcel(Constant.QUEST_TYPE_SUPP),fileName,Constant.SHEET_NAME_SUPP);
            //抑郁焦虑压力表
        }else if (Constant.QUEST_TYPE_DEPR.equals(type)){
            //压力的分  焦虑得分  抑郁得分
            int [] score = {0,0,0};
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                String checked = jsonObject1.getString("checked");
                int model = jsonObject1.getIntValue("type");
                if(checked.equals("非常符合")){
                    score[model-1] += 3;
                }else if(checked.equals("大部分符合")){
                    score[model-1] += 2;
                }else if(checked.equals("小部分符合")){
                    score[model-1] +=1;
                }
            }
            System.out.println("压力得分："+score[0]+";焦虑得分："+score[1]+";抑郁得分："+score[2]);
            fileName=fileName+"-"+Constant.SHEET_NAME_DEPR+".xlsx";
            Map<String,String> resultMap = new LinkedHashMap<>();
            resultMap.put("压力指数",String.valueOf(score[0]));
            resultMap.put("焦虑指数",String.valueOf(score[1]));
            resultMap.put("抑郁指数",String.valueOf(score[2]));
            resultMapList = new ArrayList<>();
            resultMapList.add(resultMap);
            ExcelUtil.createExcelFile(DepressEntity.class,jsonArray.toJavaList(DepressEntity.class),resultMapList,new ExcelClass().contentExcel(Constant.QUEST_TYPE_DEPR),fileName,Constant.SHEET_NAME_DEPR);
            //正性负性情绪量表
        }else if (Constant.QUEST_TYPE_MOOD.equals(type)){
            //反向情绪  正向情绪
            int [] score = {0,0};
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                int model = jsonObject1.getIntValue("type");
                String checked = jsonObject1.getString("checked");
                if(checked.equals("几乎没有")){
                    score[model] += 1;
                }else if(checked.equals("比较少")){
                    score[model] += 2;
                }else if(checked.equals("中等程度")){
                    score[model] += 3;
                }else if(checked.equals("比较多")){
                    score[model] += 4;
                }else if(checked.equals("及其多")){
                    score[model] += 5;
                }
            }
            System.out.println("负向情绪得分："+score[0]+";正向情绪得分："+score[1]);
            fileName=fileName+"-"+Constant.SHEET_NAME_MOOD+".xlsx";
            Map<String,String> resultMap = new LinkedHashMap<>();
            resultMap.put("负向情绪得分",String.valueOf(score[0]));
            resultMap.put("正向情绪得分",String.valueOf(score[1]));
            resultMapList = new ArrayList<>();
            resultMapList.add(resultMap);
            ExcelUtil.createExcelFile(MoodEntity.class,jsonArray.toJavaList(MoodEntity.class),resultMapList,new ExcelClass().contentExcel(Constant.QUEST_TYPE_MOOD),fileName,Constant.SHEET_NAME_MOOD);
            //状态-特质焦虑量表
        }else if (Constant.QUEST_TYPE_STATUS.equals(type)){
            //状态焦虑
            int [] statusScore = {0,0};
            for (int i = 0; i < 20; i++) {
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                String checked = jsonObject1.getString("checked");
                String model = jsonObject1.getString("type");
                if(model.equals("0")){
                    if(checked.equals("完全没有")){
                        statusScore[0]+=4;
                    }else if (checked.equals("有些")){
                        statusScore[0]+=3;
                    }else if (checked.equals("经常")){
                        statusScore[0]+=2;
                    }else if (checked.equals("非常明显")){
                        statusScore[0]+=1;
                    }
                }else {
                    if(checked.equals("完全没有")){
                        statusScore[1]+=1;
                    }else if (checked.equals("有些")){
                        statusScore[1]+=2;
                    }else if (checked.equals("经常")){
                        statusScore[1]+=3;
                    }else if (checked.equals("非常明显")){
                        statusScore[1]+=4;
                    }
                }
            }

            //状态焦虑
            int []  particularityScore = {0,0};
            for (int i = 20; i < jsonArray.size(); i++) {
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                String checked = jsonObject1.getString("checked");
                String model = jsonObject1.getString("type");
                if(model.equals("0")){
                    if(checked.equals("完全没有")){
                        particularityScore[0]+=4;
                    }else if (checked.equals("有些")){
                        particularityScore[0]+=3;
                    }else if (checked.equals("经常")){
                        particularityScore[0]+=2;
                    }else if (checked.equals("非常明显")){
                        particularityScore[0]+=1;
                    }
                }else {
                    if(checked.equals("完全没有")){
                        particularityScore[1]+=1;
                    }else if (checked.equals("有些")){
                        particularityScore[1]+=2;
                    }else if (checked.equals("经常")){
                        particularityScore[1]+=3;
                    }else if (checked.equals("非常明显")){
                        particularityScore[1]+=4;
                    }
                }
            }
            System.out.println("状态焦虑：反向得分"+statusScore[0]+";正向得分："+statusScore[1]);
            System.out.println("特质焦虑：反向得分"+particularityScore[0]+";正向得分："+particularityScore[1]);
            fileName = fileName+"-"+Constant.SHEET_NAME_STATUS+".xlsx";
            Map<String,String> resultMap = new LinkedHashMap<>();
            resultMap.put("状态焦虑",String.valueOf(statusScore[0]+statusScore[1]));
            resultMap.put("特质焦虑",String.valueOf(particularityScore[0]+particularityScore[1]));
            resultMapList = new ArrayList<>();
            resultMapList.add(resultMap);
            ExcelUtil.createExcelFile(StatusEntity.class,jsonArray.toJavaList(StatusEntity.class),resultMapList,new ExcelClass().contentExcel(Constant.QUEST_TYPE_STATUS),fileName,Constant.SHEET_NAME_MOOD);
        }else if(Constant.QUEST_TYPE_LIFE.equals(type)){
            int score = 0;
            List<LifeEntity> dataList = jsonArray.toJavaList(LifeEntity.class);
            for (LifeEntity lifeEntity:dataList) {
                score = score + lifeEntity.getScore()*Integer.parseInt(lifeEntity.getChecked());
            }
            Map<String,String> resultMap = new LinkedHashMap<>();
            resultMap.put("总分",String.valueOf(score));
            fileName = fileName+"-"+Constant.SHEET_NAME_LIFE+".xlsx";
            resultMapList = new ArrayList<>();
            resultMapList.add(resultMap);
            ExcelUtil.createExcelFile(LifeEntity.class,dataList,resultMapList,new ExcelClass().contentExcel(Constant.QUEST_TYPE_LIFE),fileName,Constant.SHEET_NAME_LIFE);
        }else if (Constant.QUEST_TYPE_CATTELL.equals(type)){
            List<CattellEntity>dataList = jsonArray.toJavaList(CattellEntity.class);
            Map<String,String> resultMap = new LinkedHashMap<>();
            //第1、2、187 题不参与计算
            for (int i =2;i<dataList.size()-1;i++) {
                CattellEntity cattellEntity = dataList.get(i);
                int no = cattellEntity.getNo();
                resultMap.put("因素B得分",String.valueOf(0));
                String factor = cattellEntity.getType();
                String checked = cattellEntity.getChecked();
                int score = 0;
                //B因素一下选项加1分，其他选项均为0分:28.B   53.B   54.B   77.C   78.B   102.C   103.B   127.C   128.B   152.B   153.C   177.A   178.A
                if(factor.equals("B")){
                    if(((no == 28||no == 53 || no ==54 || no ==78||no ==103 ||no == 128 || no == 152 )&&checked.equals("B"))||((no == 77 || no ==102 || no ==127 || no == 153)&&checked.equals("C"))||((no == 177 || no == 178)&& checked.equals("A"))){
                        score = 1;
                    }
                }else{
                    if(checked.equals("B")){
                        score =1;
                    }else{
                        if(no ==3 || no ==4 || no == 7 || no == 13 || no == 17 || no == 18 || no == 20 || no == 21 || no == 25 || no == 30 || no == 33 || (no >= 36&&no<=40)|| no == 42 || no == 43 || (no>=46 && no <= 50) || no == 52 || no == 55 || no == 56 || no == 58 || no == 59
                                || no == 65 || (no >= 69 && no <= 74)|| no == 88 || no == 91 || (no >= 98 && no <= 101) || no == 104|| no == 105 || (no >= 107 && no <= 119)|| no == 124 || no == 126 ||(no>=129&&no<=134)
                                || no == 136 || no == 138 || no == 140 || no == 142 || no == 143 || (no>=145 && no <= 150)|| no == 155 || no == 156 || no == 160 || no ==163 || no ==164
                                || (no>=167 && no <= 169) || no == 171 || no == 173 || no == 174 || no == 176 || no>=179){
                            if(checked.equals("A")){
                                score = 2;
                            }
                        }else{
                            if(checked.equals("C")){
                                score = 2;
                            }
                        }
                    }
                }
                String key = "因素"+factor+"得分";
                if(resultMap.get(key)==null){
                    resultMap.put(key,String.valueOf(score));
                }else {
                    resultMap.put(key,String.valueOf(Integer.parseInt(resultMap.get("因素B得分")+score)));
                }
            }
            fileName = fileName+"-"+Constant.SHEET_NAME_CATTELL+".xlsx";
            resultMapList = new ArrayList<>();
            resultMapList.add(resultMap);
            ExcelUtil.createExcelFile(CattellEntity.class,dataList,resultMapList,new ExcelClass().contentExcel(Constant.QUEST_TYPE_CATTELL),fileName,Constant.SHEET_NAME_CATTELL);
        }
        return success("success","完成");
    }

    private static int checkQuality(JSONArray jsonArray){
        //睡眠质量,第六题，很好0分 较好1分  较差2分 很差3分
        int scoreQuality = 0 ;
        JSONObject jsonObject1 = jsonArray.getJSONObject(14);
        String checked = jsonObject1.getString("checked");
        if(checked.equals("(2) 较好")){
            scoreQuality +=1;
        }else if(checked.equals("(3) 较差")){
            scoreQuality +=2;
        }else if(checked.equals("(4) 很差")){
            scoreQuality +=3;
        }
        return scoreQuality;
    }
    private static int checkTime(JSONArray jsonArray){
        //入睡时间 ，
        // 1、第二题 计分为≤ 15 分计 0 分，16～30 分计 1 分，31~60计 2 分，≥60 分计 3 分
        // 2、 5a⽆计 0 分，<1 周/ 次计 1 分，1~2 周/ 次计 2 分，≥3周/ 次计 3 分3.
        // 累加条⽬ 2 和 5a 的计分，若累加分为 0 计 0 分，1~2计 1 分，3~4分 2 ，5~6 计 3 分
        int scoreTime = 0;
        int scoreTime5a=0;
        int scoreTime2=0;
        JSONObject jsonObject1 = jsonArray.getJSONObject(1);
        String minute = jsonObject1.getString("checked");
        if(minute.equals("(1) ≤15min")){
            scoreTime2 =1;
        }else if(minute.equals("(2) 16~30")){
            scoreTime2 =2;
        }else {
            scoreTime2 =3;
        }
        jsonObject1=jsonArray.getJSONObject(4);
        String checked = jsonObject1.getString("checked").trim();
        if(checked.equals("(2) 〈1 次/ 周")){
            scoreTime5a = 1;
        }else if(checked.equals("(3)1-2 次/ 周")){
            scoreTime5a = 2;
        }else if(checked.equals("(4) ≥ 3 次/ 周")){
            scoreTime5a=3;
        }
        int scoreTemp = scoreTime2+scoreTime5a;
        if(scoreTemp==0){
            scoreTime = 0;
        }else if(scoreTemp>=1 && scoreTemp<= 2 ){
            scoreTime = 1;
        }else if(scoreTemp>=3 && scoreTemp<= 4 ){
            scoreTime = 2;
        }else if(scoreTemp>=5 && scoreTemp<= 6 ){
            scoreTime = 3;
        }
        return scoreTime;
    }

    private int checkHour(JSONArray jsonArray) {

        //睡眠时间 根据条⽬ 4 的应答计分， >7 ⼩时计 0 分，6~7 计 1 分，5~6计 2 分，<5 ⼩时计 3 分。
        int scoreHour = 0;
        JSONObject jsonObject1 = jsonArray.getJSONObject(3);
        int hour =jsonObject1.getIntValue("checkItems");
        if(hour>6&&hour<=7){
            scoreHour = 1;
        }else if (hour>5&&hour<=6){
            scoreHour = 2 ;
        }else if (hour<5){
            scoreHour = 3 ;
        }
        return scoreHour;
    }

    private static int checkEfficiency(JSONArray jsonArray){
//        床上时间 = 条⽬ 3（起床时间） - 条⽬ 1（上床时间）
        String goBad = "2021-01-26 "+jsonArray.getJSONObject(2).getString("checked")+"";
        LocalDateTime beginDate = LocalDateTime.parse(goBad, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
        String leaveBad = "2021-01-27 "+jsonArray.getJSONObject(0).getString("checked");
        LocalDateTime engDate = LocalDateTime.parse(leaveBad,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
        Duration duration = Duration.between(beginDate,engDate);
        //相差的分钟数
        long hourOnBed = duration.toMinutes();
        String hourSleepString = jsonArray.getJSONObject(3).getString("checked");
        int hourSleep = Integer.parseInt(hourSleepString.substring(0,hourSleepString.indexOf(":")))*60 + Integer.parseInt(hourSleepString.substring(hourSleepString.indexOf(":")+1));
        double efficiencyTemp = hourSleep/hourOnBed;
        if(efficiencyTemp>0.85){
            return 0;
        }else if(efficiencyTemp>=0.75&&efficiencyTemp<=0.84){
            return 1;
        }else if(efficiencyTemp>=0.65&&efficiencyTemp<0.74){
            return 2;
        }else{
            return 3;
        }

    }
    private static  int checkObstruct(JSONArray jsonArray){
        int scoreTemp = 0;
        int score = 0;
        for(int i=5;i<14;i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String checked = jsonObject.getString("checked").trim();
            if(checked.equals("(2) 〈1 次/ 周")){
                scoreTemp += 1;
            }else if(checked.equals("(3)1-2 次/ 周")){
                scoreTemp += 2;
            }else if(checked.equals("(4) ≥ 3 次/ 周")){
                scoreTemp += 3;
            }
            if(scoreTemp==0){
                score = 0;
            }else if(scoreTemp>=1 && scoreTemp<10){
                score = 1;
            }else if(scoreTemp>=10 && scoreTemp<=18){
                score = 2;
            }else if(scoreTemp >=19 && scoreTemp<=27){
                score = 3;
            }
        }
        return score;
    }

    private int checkMedicine(JSONArray jsonArray){
        int scoreMedicine = 0 ;
        JSONObject jsonObject = jsonArray.getJSONObject(16);
        String checked = jsonObject.getString("checked").trim();
        if(checked.equals("(2) 〈1 次/ 周")){
            scoreMedicine = 1;
        }else if(checked.equals("(3)1-2 次/ 周")){
            scoreMedicine = 2;
        }else if(checked.equals("(4) ≥ 3 次/ 周")){
            scoreMedicine = 3;
        }
        return scoreMedicine;
    }

    private static int checkDay(JSONArray jsonArray){
        JSONObject jsonObject = jsonArray.getJSONObject(15);
        String checked = jsonObject.getString("checked").trim();
        int scoreDay=0;
        if(checked.equals("(2) 〈1 次/ 周")){
            scoreDay += 1;
        }else if(checked.equals("(3)1-2 次/ 周")){
            scoreDay += 2;
        }else if(checked.equals("(4) ≥ 3 次/ 周")){
            scoreDay += 3;
        }

        jsonObject = jsonArray.getJSONObject(16);
        checked = jsonObject.getString("checked").trim();
        if(checked.equals("(2) 〈1 次/ 周")){
            scoreDay += 1;
        }else if(checked.equals("(3)1-2 次/ 周")){
            scoreDay += 2;
        }else if(checked.equals("(4) ≥ 3 次/ 周")){
            scoreDay += 3;
        }

        jsonObject = jsonArray.getJSONObject(17);
        checked = jsonObject.getString("checked").trim();
        if(checked.equals("(1) 没有")){
            scoreDay += 0;
        }else if(checked.equals("(2) 偶尔有")){
            scoreDay += 1;
        }else if(checked.equals("(3) 有时有")){
            scoreDay += 2;
        }else if(checked.equals("(4) 经常有")){
            scoreDay += 3;
        }
        if(scoreDay == 0){
            scoreDay = 0;
        }else if(scoreDay == 1 || scoreDay == 2){
            scoreDay = 1;
        }else if(scoreDay == 3 || scoreDay == 4){
            scoreDay = 2;
        }else if(scoreDay == 5 || scoreDay == 6){
            scoreDay = 3;
        }
        return scoreDay;
    }

    /**
     * 根据flag查询检测列表
     * @param flag
     * @return
     */
    @SafetyProcess
    @GetMapping("/{flag}")
    @ApiOperation("根据flag查询检测列表")
    public Result getScaleByFlag(@PathVariable String flag){

        List<ScaleEntity> scaleEntityList = this.scaleService.getScaleByFlag(flag);
        return success(scaleEntityList);
    }
}
