package com.ruoyi.web.controller.test.gl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.gson.Gson;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.test.domain.*;
import com.ruoyi.test.domain.DO.AnswerDO;
import com.ruoyi.test.domain.DO.AnswerSheetDO;
import com.ruoyi.test.domain.DO.QuestionAnswerDO;
import com.ruoyi.test.domain.Vo.AnswerVo;
import com.ruoyi.test.domain.Vo.QuestionanswerVo;
import com.ruoyi.test.seriver.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "管理端答卷管理")
@RestController
@RequestMapping("/gl/answerSheet")
public class AnswerSheetController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private IAnswerSheetDOService iAnswerSheetDOService;

    @Autowired
    private IAnswerService iAnswerService;

    @Autowired
    private IExamCreateService iExamCreateService;

    @Autowired
    private IQuestionanswerService iQuestionanswerService;

    @Autowired
    private IMarkedtestService iMarkedtestService;

    @ApiOperation("试卷")
    @GetMapping("testPaper")
    public R<AnswerSheetDO> testPaper(String subject, String grade, String name) {
        return R.ok(iAnswerSheetDOService.testPaper(subject, grade, name));
    }

    @ApiOperation("答案")
    @PostMapping("answer")
    public R<String> answer(@RequestBody AnswerDO answerDO) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long id = sysUser.getUserId();
        String sender = iUserService.user(iUserService.selectByzhid(id)).getTname();

        List<AnswerVo> answerVos = answerDO.getList();
        Gson gson = new Gson();
        String answerVo = gson.toJson(answerVos);

        Answer answer = new Answer();
        answer.setSender(sender);
        answer.setTestid(answerDO.getTestid());
        answer.setIdanswer(answerVo);
        answer.setTime(answerDO.getTime());

        if (iAnswerService.answer(answer) != 0) {
            return R.ok("答案提交成功！");
        }
        return R.fail("答案提交失败！");
    }

    //主观题自动更改
    @ApiOperation("主观题批改")
    @GetMapping("answerDisplay")
    public R<String> answerDisplay(String sender, Long id) {
        List<Questionbank> list = iExamCreateService.selectListByPid(id);
        Answer answer = iAnswerService.selectByTestId(sender, id);
        QuestionAnswerDO questionAnswerDO = iQuestionanswerService.createTest(list, answer);
        if (iQuestionanswerService.addTest(questionAnswerDO) != 0) {
            return R.ok("该学生客观题已改完！");
        }
        return R.fail("查不到该学生的考试记录！");
    }


    @ApiOperation("查看学生的考试情况(最新)")
    @GetMapping("selectQuestionanswer")
    public R<QuestionAnswerDO> selectQuestionanswer(String sender, Long testid) {
        return R.ok(iQuestionanswerService.selectQuestionanswer(sender,testid));
    }

    @ApiOperation("批改客观题")
    @PostMapping("markObjectiveQuestions")
    public R<String> markObjectiveQuestions(@RequestBody QuestionAnswerDO questionAnswerDO) {
        if(iQuestionanswerService.markObjectiveQuestions(questionAnswerDO)!=0){
            return R.ok("批改完成！");
        }
        return R.fail("批改提交失败！");
    }


    @ApiOperation("试卷批改完毕提交")
    @PostMapping("addmarkedtest")
    public R<String> addmarkedtest(String sender,Long testid){
        if(iMarkedtestService.addmarkedtest(sender,testid)!=0){
            return R.ok("提交完成！");
        }
        return R.fail("提交失败！");
    }

    @ApiOperation("学生成绩列表查询")
    @GetMapping("selectmarkedtest")
    public R<IPage<Markedtest>> selectmarkedtest(int pagenum){
        return R.ok(iMarkedtestService.selectmarkedtest(pagenum));
    }

    @ApiOperation("通过学科查询学生成绩列表")
    @GetMapping("selectmarkedtestBySubject")
    public R<IPage<Markedtest>> selectmarkedtestBySubject(int pagenum,String subject){
        return R.ok(iMarkedtestService.selectmarkedtestBySubject(pagenum,subject));
    }
}
