package com.xhkjedu.sexam.controller.paper;

import com.xhkjedu.annotation.Action;
import com.xhkjedu.sexam.model.paper.*;
import com.xhkjedu.sexam.service.paper.EPaperQtypeService;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Description 题库试卷
 * @Author WN
 * Date 2022/7/19 9:05
 **/
@Slf4j
@RestController
@RequestMapping("/epqt")
public class EPaperQtypeController {

    @Resource
    private EPaperQtypeService ePaperQtypeService;

    /**
     * @Description  保存试卷
     * @Param [paper]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/7/19 9:09
     **/
    @PostMapping("/save")
    @Action("考试测评-保存试卷")
    public ResultVo savePaperQtype(@RequestBody EPaper paper){
        N_Utils.validation(new Object[]{paper.getEpid(),"考试试卷id",1});
        List<EPaperQtype> qtypes = paper.getQtypelist();
        if(qtypes == null || qtypes.size() == 0){
            return new ResultVo(1,"未设置试题");
        }
        return ePaperQtypeService.saveQtPaper(paper);
    }

    /**
     * @Description 附件试卷详情
     * @Param [paper]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/7/19 8:36
     **/
    @PostMapping("/detail")
    public ResultVo getPaperQtDetail(@RequestBody EPaper paper){
        N_Utils.validation(new Object[]{paper.getEsid(),"考试科目id",1});
        Map map = ePaperQtypeService.getPaperQtDetail(paper.getEsid());
        return new ResultVo(0,"成功获取试卷详情",map);
    }

    /**
     * @Description  设置正确答案
     * @Param [paper]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/7/25 15:09
     **/
    @PostMapping("/answer")
    @Action("考试测评-设置试卷答案")
    public ResultVo setPaperQuestionAnswer(@RequestBody EPaper paper){
        try {
            N_Utils.validation(new Object[]{paper.getEpid(),"试卷id",1});
            ePaperQtypeService.saveQuestionAnswerAndUpdateStuAnswer(paper);
            return new ResultVo(0,"保存成功");
        }catch (Exception e){
            log.error("保存答案出错："+e.getMessage());
            return new ResultVo(1,"保存失败");
        }
    }

    /**
     * @Description 获取试卷答题卡
     * @Param [ePaper]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/7/25 11:21
     **/
    @PostMapping("/pqs")
    public ResultVo getPaperAndQuesitons(@RequestBody EPaper ePaper) {
        N_Utils.validation(new Object[]{ePaper.getEpid(),"试卷id",1});
        Map rtn = ePaperQtypeService.getPaperAndQuesitons(ePaper.getEpid());
        return new ResultVo(0,"获取成功",rtn);
    }

    /**
     * @Description 保存试卷答题卡
     * @Param [ePaper]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/7/25 17:43
     **/
    @PostMapping("/scan")
    @Action("考试测评-保存试卷答题卡")
    public ResultVo savePaperScan(@RequestBody EPaper ePaper) {
        N_Utils.validation(new Object[]{ePaper.getEpid(),"试卷id",1});
        List<EPaperQtypeQuestion> ques = ePaper.getQuestions();
        if(ques == null || ques.size() == 0){
            return new ResultVo(1,"未设置试题");
        }
        ePaperQtypeService.savePaperScan(ePaper);
        return new ResultVo(0,"保存成功");
    }

    /*
     * 试卷中所有试题题号信息
     * @Param [ePaper]
     * @Author ywx
     * @Date 2022/11/21 13:26
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_qn")
    public ResultVo listQn(@RequestBody EPaper ePaper) {
        Integer epid = ePaper.getEpid();
        N_Utils.validation(new Object[]{epid, "试卷id", 1});
        List<Map> list = ePaperQtypeService.listQn(epid);
        return new ResultVo(0, "获取成功", list);
    }

    /**
     * @Description 上传答案附件
     * @Date 2024/6/19 14:54
     * @Author YWX
     * @Param [file]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/up_af")
    @Action("考试测评-上传答案附件")
    public ResultVo upAnswerFile(@RequestBody EPaperFile file) {
        try {
            N_Utils.validation(new Object[]{file.getEpid(), "试卷id", 1, file.getFilepath(), "文件地址", 2
                    , file.getSourcepath(), "原文档地址", 2});
            ePaperQtypeService.upAnswerFile(file);
            return new ResultVo(0, "上传成功");
        } catch (Exception e) {
            log.error("上传答案附件出错：{}", e.getMessage());
            return new ResultVo(1, "上传失败");
        }
    }

    /**
     * @Description 删除答案附件
     * @Date 2024/6/19 14:55
     * @Author YWX
     * @Param [file]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/del_af")
    @Action("考试测评-删除答案附件")
    public ResultVo delAnswerFile(@RequestBody EPaperFile file) {
        try {
            Integer epid = file.getEpid();
            N_Utils.validation(new Object[]{epid, "试卷id", 1});
            ePaperQtypeService.delAnswerFile(epid);
            return new ResultVo(0, "删除成功");
        } catch (Exception e) {
            log.error("删除答案附件出错：{}", e.getMessage());
            return new ResultVo(1, "删除失败");
        }
    }


}
