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

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.Answer;
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.Markedtest;
import com.ruoyi.test.domain.Questionbank;
import com.ruoyi.test.domain.Vo.AnswerVo;
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;

@Api(tags = "学生端主页")
@RestController
@RequestMapping("/student/homepage")
public class StudentHomePageController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private IAnswerService iAnswerService;

    @Autowired
    private IMarkedtestService iMarkedtestService;

    @Autowired
    private IAnswerSheetDOService iAnswerSheetDOService;

    @Autowired
    private IQuestionanswerService iQuestionanswerService;

    @Autowired
    private IExamCreateService iExamCreateService;


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

    @ApiOperation("任务中心答题")
    @PostMapping("task_answer")
    public R<String> taskAnswer(@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("任务中心分数")
    @PostMapping("task_score")
    public R<Double> taskScore(Long testid){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long id = sysUser.getUserId();
        String sender = iUserService.user(iUserService.selectByzhid(id)).getTname();
        return R.ok(iMarkedtestService.selectmarkedtestBySenderAndTestid(sender,testid).getScore());
    }

    @ApiOperation("查看学生的考试情况(最新)")
    @GetMapping("selectQuestionanswer")
    public R<QuestionAnswerDO> selectQuestionanswer(Long testid) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long id = sysUser.getUserId();
        String sender = iUserService.user(iUserService.selectByzhid(id)).getTname();
        return R.ok(iQuestionanswerService.selectQuestionanswer(sender,testid));
    }

    //主观题自动更改
    @ApiOperation("任务中心主观题批改")
    @GetMapping("task_answerDisplay")
    public R<String> taskAnswerDisplay(Long id) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long id1 = sysUser.getUserId();
        String sender = iUserService.user(iUserService.selectByzhid(id1)).getTname();
        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("任务中心批改客观题")
    @PostMapping("task_markObjectiveQuestions")
    public R<String> taskMarkObjectiveQuestions(@RequestBody QuestionAnswerDO questionAnswerDO) {
        if(iQuestionanswerService.markObjectiveQuestions(questionAnswerDO)!=0){
            return R.ok("批改完成！");
        }
        return R.fail("批改提交失败！");
    }

}
