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

import com.baomidou.mybatisplus.core.metadata.IPage;
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.Errorbook;
import com.ruoyi.test.domain.Questionbank;
import com.ruoyi.test.domain.Vo.ErrorbookVo;
import com.ruoyi.test.seriver.IErrorbookService;
import com.ruoyi.test.seriver.IMarkedtestService;
import com.ruoyi.test.seriver.IQuestionbankService;
import com.ruoyi.test.seriver.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "学生端错题管理")
@RestController
@RequestMapping("student/errorBook")
public class StudentErrorBookController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private IMarkedtestService iMarkedtestService;

    @Autowired
    private IErrorbookService iErrorbookService;

    @Autowired
    private IQuestionbankService iQuestionbankService;

    @ApiOperation("新增错题")
    @GetMapping("addErrorBook")
    public R<String> addErrorBook(Long qid){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long id = sysUser.getUserId();
        String sender = iUserService.user(iUserService.selectByzhid(id)).getTname();
        Questionbank questionbank = iQuestionbankService.selectById(qid);
        Errorbook errorbook = new Errorbook();
        errorbook.setSender(sender);
        errorbook.setContent(questionbank.getContent());
        errorbook.setChance(questionbank.getChance());
        errorbook.setDifficulty(questionbank.getDifficulty());
        errorbook.setAnalysis(questionbank.getAnalysis());
        errorbook.setAnswer(questionbank.getAnswer());
        errorbook.setSubject(questionbank.getSubject());
        errorbook.setType(questionbank.getQuestiontype());
        errorbook.setScore(questionbank.getScore());
        if(iErrorbookService.addErrorBook(errorbook)!=0){
            return R.ok("错题添加成功！");
        }
        return R.fail("错题添加失败！");
    }

    @ApiOperation("查询错题")
    @GetMapping("selectErrorBook")
    public R<IPage<ErrorbookVo>> selectErrorBook(int pagenum){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long id = sysUser.getUserId();
        String sender = iUserService.user(iUserService.selectByzhid(id)).getTname();
        return R.ok(iErrorbookService.selectErrorBook(pagenum,sender));
    }

    @ApiOperation("查询错题(根据学科)")
    @GetMapping("selectErrorBookBySubject")
    public R<IPage<ErrorbookVo>> selectErrorBookBySubject(int pagenum,String subject){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long id = sysUser.getUserId();
        String sender = iUserService.user(iUserService.selectByzhid(id)).getTname();
        return R.ok(iErrorbookService.selectErrorBookBySubject(pagenum,sender,subject));
    }

}
