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.MBTIResultDetail;
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.apache.commons.lang.StringUtils;
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;

    @Autowired
    private MBTIResultDetailService detailService;

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

    @GetMapping("/updateItem")
    public  void updateItem(String fileName,String flag){
        File file = new File("C:\\Users\\Administrator\\Desktop\\"+fileName+".xlsx");
        try {
            List<List<List<Object>>> datas = ExcelUtil.getBankListByExcelSheet(new FileInputStream(file.getAbsolutePath()), file.getName());
            List<ScaleEntity> scaleEntities = scaleService.getScaleByFlag(flag);
            List<List<Object>> an = datas.get(1);
            for (ScaleEntity scaleEntity : scaleEntities) {
                log.info("------" + scaleEntity.getCheckItems());
                String no = scaleEntity.getQuestionNo();
                String item = null;
                for (List<Object> objects : an) {
                    if (objects.get(0).equals(no)) {
                        if (StringUtils.isEmpty(item)) {
                            item = objects.get(1).toString();
                        } else {
                            item = item + "；" + objects.get(1).toString();
                        }
                    }
                }
                scaleEntity.setCheckItems(item);
                if (item != null){
                    scaleService.saveScale(scaleEntity);
                }
                log.info("------" + scaleEntity.toString());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }



    @GetMapping("/updateAnswer")
    public  void updateAnswer(String fileName,String flag){
        File file = new File("C:\\Users\\Administrator\\Desktop\\"+fileName+".xlsx");
        try {
            List<List<List<Object>>> datas = ExcelUtil.getBankListByExcelSheet(new FileInputStream(file.getAbsolutePath()), file.getName());
            List<AnswerEntity> answerEntities = answerService.getAnswerByFlag(flag);
            List<List<Object>> answerObj = datas.get(1);
            for (int i =0;i<answerEntities.size();i++){
                AnswerEntity answerEntity = answerEntities.get(i);
                answerEntity.setName(DESede.encryptString(answerObj.get(i).get(1).toString()));
                answerEntity.setScore(DESede.encryptString(answerObj.get(i).get(2).toString()));
                log.info(answerEntity.getName());
                //answerService.updateAnswer(answerEntity);
            }

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

    @GetMapping("/update")
    public String update() throws Exception {

        File file = new File("C:\\Users\\Administrator\\Desktop\\标准情商测试.xlsx");
        try {
            List<List<List<Object>>> datas = ExcelUtil.getBankListByExcelSheet(new FileInputStream(file.getAbsolutePath()), file.getName());
            List<ScaleMarksEntity> scaleEntities = scaleMarksService.getScaleMarksByFlag("20210820143117");
            List<List<Object>> an = datas.get(2);
            for (int i = 0;i<scaleEntities.size();i++){
                ScaleMarksEntity entity = scaleEntities.get(i);
                entity.setImprovementSuggestions(an.get(i).get(4).toString());
               // scaleMarksService.saveScaleMarks(entity);
            }

            log.info("111");
//            marksObj.forEach(item ->{
//                ScaleMarksEntity entity = new ScaleMarksEntity();
//                entity.setName("总分");
//                entity.setScoreStart(item.get(1).toString());
//                entity.setScoreEnd(item.get(2).toString());
//                entity.setSymptom(item.get(3).toString());
//                entity.setImprovementSuggestions(item.get(4).toString());
//                entity.setIsTotalScoreExplain("是");
//                entity.setNameExplain("无");
//                entity.setStandardDeviation("无");
//                entity.setScoringType("0");
//                entity.setFlag("20210820143117");
//                entity.setSuggestion("无");
//                entity.setReference("无");
//                scaleMarksService.saveScaleMarks(entity);
//
//                log.info(entity.toString());
//            });



//            for (int i =0;i<answerEntities.size();i++){
//                AnswerEntity answerEntity = answerEntities.get(i);
//                answerEntity.setName(DESede.encryptString(answerObj.get(i).get(1).toString()));
//                answerEntity.setScore(DESede.encryptString(answerObj.get(i).get(2).toString()));
//                log.info(answerEntity.getName());
//                answerService.updateAnswer(answerEntity);
//            }
            //log.info(datas.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "hello";
    }

    @GetMapping("updateMarks")
    public void updateMarks(String fileName,String flag){
        File file = new File("C:\\Users\\Administrator\\Desktop\\"+fileName+".xlsx");
        try {
            List<List<List<Object>>> datas = ExcelUtil.getBankListByExcelSheet(new FileInputStream(file.getAbsolutePath()), file.getName());
            List<ScaleMarksEntity> scaleMarksEntities = scaleMarksService.getScaleMarksByFlag(flag);
            List<List<Object>> markObj = datas.get(2);
            for (int i =0;i<scaleMarksEntities.size();i++){
                ScaleMarksEntity entity = scaleMarksEntities.get(i);
                entity.setSymptom(markObj.get(i).get(0).toString());
                entity.setImprovementSuggestions(markObj.get(i).get(3).toString());
                log.info(entity.toString());
                //scaleMarksService.saveScaleMarks(entity);
            }

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

    @GetMapping("updateMBTI")
    public void updateMBTI(String fileName){
        File file = new File("C:\\Users\\Administrator\\Desktop\\"+fileName+".xlsx");
        try {
            List<List<List<Object>>> datas = ExcelUtil.getBankListByExcelSheet(new FileInputStream(file.getAbsolutePath()), file.getName());
            List<List<Object>> markObj = datas.get(1);
            for (int i =0;i<markObj.size();i++){
                List<Object> objects = markObj.get(i);
                MBTIResultDetail detail = new MBTIResultDetail();
                detail.setFlag(objects.get(0).toString());
                detail.setConclusion(objects.get(1).toString());
                detail.setCharacteristic(objects.get(2).toString());
                detail.setEvaluate(objects.get(3).toString());
                detail.setRepresentative(objects.get(4).toString());
                detail.setPursuit(objects.get(5).toString());
                detail.setAdvantage(objects.get(6).toString());
                detail.setDisadvantages(objects.get(7).toString());
                detail.setRecommendation(objects.get(8).toString());
                detail.setSuggest(objects.get(9).toString());
                detail.setRevered(objects.get(10).toString());
                log.info(detail.toString());
                //detailService.save(detail);
            }

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

}
