package com.ruoyi.web.controller.front;

import com.alibaba.fastjson2.util.DateUtils;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.learn.service.ILearnWrongBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

@Api(tags = "前端V2 错题本")
@RestController
@RequestMapping("front/v2/wrongBook")
public class FrontWrongBookController extends BaseController {
    private final ILearnWrongBookService wrongBookService;

    public FrontWrongBookController(ILearnWrongBookService wrongBookService) {
        this.wrongBookService = wrongBookService;
    }

    /**
     *
     * @return
     */
    @ApiOperation("错题涉及的学科")
    @GetMapping("subjects")
    public AjaxResult subjects() {
        return AjaxResult.success(wrongBookService.findSubject());
    }

    @ApiOperation("错题列表")
    @GetMapping("wrongQuestions")
    public TableDataInfo wrongQuestions(@ApiParam("科目id") @RequestParam(required = false) Long subjectId,
                                        @ApiParam("开始日期yyyy-MM-dd") @RequestParam(required = false) String start,
                                        @ApiParam("结束时间yyyy-MM-dd") @RequestParam(required = false) String end,
                                        @ApiParam(value = "页数", example = "1") @RequestParam Integer pageNum,
                                        @ApiParam(value = "页大小", example = "15") @RequestParam Integer pageSize) {
        startPage();
        return getDataTable(wrongBookService.findWrongQuestions(subjectId,
                StringUtils.isNotBlank(start) ? DateUtils.parseDate(start, "yyyy-MM-dd") : null,
                StringUtils.isNotBlank(end) ? DateUtils.parseDate(end, "yyyy-MM-dd") : null));
    }

    @ApiOperation("错题考卷列表")
    @GetMapping("wrongExaminees")
    public TableDataInfo wrongExaminees(@ApiParam("错题id") @RequestParam Long wrongId) {
        return getDataTable(wrongBookService.findWrongExaminees(wrongId));
    }

    @ApiOperation("删除错题")
    @PostMapping("deleteWrongQuestion")
    public AjaxResult deleteWrongQuestion(Long questionId) {
        return  AjaxResult.success(wrongBookService.deleteByQuestion(SecurityUtils.getLoginUser().getUserId(), questionId));
    }
}
