package com.rf.psychological.rest;

import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.base.rest.BaseController;
import com.rf.psychological.dao.model.AnswerEntity;
import com.rf.psychological.dao.model.DimensionEntity;
import com.rf.psychological.scale.dao.model.ScaleEntity;
import com.rf.psychological.scale.dao.model.ScaleMarksEntity;
import com.rf.psychological.scale.dao.model.SubjectEntity;
import com.rf.psychological.scale.service.*;
import com.rf.psychological.security.DESede;
import com.rf.psychological.utils.ExcelUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

/**
 * @author zzf
 * @description:控制器
 * @date 2021/1/18 19:19
 */
@Slf4j
@RestController
@RequestMapping("/device")
@Api(tags = "设备信息")
public class DeviceController extends BaseController {

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private ScaleService scaleService;

    @Autowired
    private AnswerService answerService;

    @Autowired
    private ScaleMarksService scaleMarksService;

    @Autowired
    private DimensionService dimensionService;

    /**
     * APP查找服务器使用
     * @return
     */
    @RequestMapping("/hello")
    public String hello(){
        System.out.println("hello");
        return "hello";
    }

    //@Autowired
    @RequestMapping("/update")
    public String update() throws Exception {

        /*File file = new File("d:/11/竞品量表筛选后基本信息.xlsx");
        try {
            List<List<List<Object>>> datas = ExcelUtil.getBankListByExcelSheet(new FileInputStream(file.getAbsolutePath()), file.getName());
            //根据flag获取量表名称
            List<SubjectEntity> subjectEntities = this.subjectService.findAll("0");
            //获取题目信息并入库
            List<List<Object>> sheet0 = datas.get(0);
            for (int i = 0; i < sheet0.size(); i++) {
                List<Object> scales = sheet0.get(i);
                for (SubjectEntity subject:subjectEntities) {
                    if (subject.getName().equals(scales.get(0).toString())){
                        System.out.println("YXL:"+scales.get(0).toString());
                    }
                }
            }
            List<List<Object>> sheet1 = datas.get(1);
            for (int i = 0; i < sheet1.size(); i++) {
                List<Object> scales = sheet1.get(i);
                for (SubjectEntity subject:subjectEntities) {
                    if (subject.getName().equals(scales.get(0).toString())){
                        System.out.println("JL:"+scales.get(0).toString());
                    }
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }*/

        //新增判断标准
        /*String flag = "111aaa";
        File file = new File("d:/22/心理年龄测试.xlsx");
        List<List<List<Object>>> datas = ExcelUtil.getBankListByExcelSheet(new FileInputStream(file.getAbsolutePath()), file.getName());
        //获取判断标准信息并入库
        List<List<Object>> sheet2 = datas.get(2);
        for (int i = 0; i < sheet2.size(); i++) {
            List<Object> scaleMarks = sheet2.get(i);
            if (scaleMarks.size() < 10){
                //删除量表判断标准信息
                this.scaleMarksService.deleteScaleMarkByFlag(flag);
            }
            ScaleMarksEntity scaleMarksEntity = new ScaleMarksEntity();
            scaleMarksEntity.setFlag(flag);
            scaleMarksEntity.setName(scaleMarks.get(1).toString());
            scaleMarksEntity.setImprovementSuggestions(scaleMarks.get(7).toString());
            scaleMarksEntity.setSymptom(scaleMarks.get(5).toString());
            scaleMarksEntity.setScoreEnd(scaleMarks.get(3).toString());
            scaleMarksEntity.setScoreStart(scaleMarks.get(2).toString());
            scaleMarksEntity.setIsTotalScoreExplain(scaleMarks.get(9).toString());
            scaleMarksEntity.setNameExplain(scaleMarks.get(6).toString());
            scaleMarksEntity.setReference(scaleMarks.get(4).toString());
            scaleMarksEntity.setStandardDeviation("无");
            scaleMarksEntity.setSuggestion(scaleMarks.get(8).toString());
            scaleMarksEntity.setScoringType("0");
            this.scaleMarksService.saveScaleMarks(scaleMarksEntity);

        }*/
        //获取维度信息并入库
        /*List<List<Object>> sheet3 = datas.get(3);
        for (int i = 0; i < sheet3.size(); i++) {
            List<Object> dimensions = sheet3.get(i);
            if (dimensions.size() < 2){
                //删除量表维度信息
                this.dimensionService.deleteDimensionByFlag(flag);
            }
            DimensionEntity dimensionEntity = new DimensionEntity();
            dimensionEntity.setFlag(flag);
            //dimensionEntity.setName(DESede.encryptString(dimensions.get(0).toString()));
            //dimensionEntity.setQuestionNo(DESede.encryptString(dimensions.get(1).toString()));
            dimensionEntity.setName(dimensions.get(0).toString());
            dimensionEntity.setIsIconFlag("0");
            dimensionEntity.setQuestionNo(dimensions.get(1).toString());
            this.dimensionService.saveDimension(dimensionEntity);
        }*/

        /*String flag = "20230907180052";
        //根据flag获取全部选项
        List<ScaleEntity> scaleEntities = this.scaleService.getScaleByFlag(flag);
        //根据flag获取全部答案
        List<AnswerEntity> answerEntities = this.answerService.getAnswerByFlag(flag);
        //根据flag获取全部评分规则
        List<ScaleMarksEntity> scaleMarksEntities = this.scaleMarksService.getScaleMarksByFlag(flag);
        //根据flag获取全部维度信息
        List<DimensionEntity> dimensionEntities = this.dimensionService.getDimensionByFlag(flag);
        //根据flag获取量表名称
        List<SubjectEntity> subjectEntities = this.subjectService.getSubjectByFlag(flag);
        System.out.println("answerEntities==="+JSONObject.toJSON(answerEntities));
        System.out.println("dimensionEntities==="+JSONObject.toJSON(dimensionEntities));
        System.out.println("scaleMarksEntities==="+JSONObject.toJSON(scaleMarksEntities));
        System.out.println("scaleEntities==="+JSONObject.toJSON(scaleEntities));
        System.out.println("subjectEntities==="+JSONObject.toJSON(subjectEntities));*/


        //根据flag获取全部选项
        //List<ScaleEntity> scaleEntities = this.scaleService.findAll();
        //根据flag获取全部答案
        //List<AnswerEntity> answerEntities = this.answerService.findAll();
        //根据flag获取全部评分规则
        //List<ScaleMarksEntity> scaleMarksEntities = this.scaleMarksService.findAll();
        //根据flag获取全部维度信息
        //List<DimensionEntity> dimensionEntities = this.dimensionService.findAll();
        //根据flag获取量表名称
        //List<SubjectEntity> subjectEntities = this.subjectService.findAll("1");


        /*for (SubjectEntity subjectEntity:subjectEntities){
            //String description = DESede.encryptString(subjectEntity.getDescription());
            subjectEntity.setDescription(DESede.encryptString(subjectEntity.getDescription()));
            this.subjectService.update(subjectEntity);
        }

        for (AnswerEntity answerEntity:answerEntities){
            answerEntity.setName(DESede.encryptString(answerEntity.getName()));
            answerEntity.setScore(DESede.encryptString(answerEntity.getScore()));
            this.answerService.updateAnswer(answerEntity);

        }
        for (ScaleEntity scaleEntity:scaleEntities){
            scaleEntity.setAnswer(DESede.encryptString(scaleEntity.getAnswer()));
            scaleEntity.setCheckItems(DESede.encryptString(scaleEntity.getCheckItems()));
            this.scaleService.updateScale(scaleEntity);

        }
        for (ScaleMarksEntity scaleMarksEntity:scaleMarksEntities){
            scaleMarksEntity.setName(DESede.encryptString(scaleMarksEntity.getName()));
            scaleMarksEntity.setScoreStart(DESede.encryptString(scaleMarksEntity.getScoreStart()));
            scaleMarksEntity.setScoreEnd(DESede.encryptString(scaleMarksEntity.getScoreEnd()));
            scaleMarksEntity.setSymptom(DESede.encryptString(scaleMarksEntity.getSymptom()));
            scaleMarksEntity.setImprovementSuggestions(DESede.encryptString(scaleMarksEntity.getImprovementSuggestions()));
            scaleMarksEntity.setNameExplain(DESede.encryptString(scaleMarksEntity.getNameExplain()));
            scaleMarksEntity.setReference(DESede.encryptString(scaleMarksEntity.getReference()));
            scaleMarksEntity.setSuggestion(DESede.encryptString(scaleMarksEntity.getSuggestion()));
            scaleMarksEntity.setStandardDeviation(DESede.encryptString(scaleMarksEntity.getStandardDeviation()));
            this.scaleMarksService.updateMark(scaleMarksEntity);

        }
        for (DimensionEntity dimensionEntity:dimensionEntities){
            dimensionEntity.setName(DESede.encryptString(dimensionEntity.getName()));
            dimensionEntity.setQuestionNo(DESede.encryptString(dimensionEntity.getQuestionNo()));
            this.dimensionService.updateDimension(dimensionEntity);

        }*/

        return "hello";
    }

}
