package com.ruoyi.web.controller.front;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.learn.domain.LearnPaper;
import com.ruoyi.learn.domain.LearnQuestions;
import com.ruoyi.learn.service.ILearnPaperService;
import com.ruoyi.learn.service.ILearnQuestionsService;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "前端V2 收藏")
@RestController
@RequestMapping("front/favorites")
public class FrontFavoritesController extends BaseController {
    @Autowired
    private ILearnPaperService papersService;
    @Autowired
    private ILearnQuestionsService questionsService;

    @ApiOperation("收藏的试卷列表")
    @GetMapping("papers")
    public TableDataInfo papers(Long subjectId) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        startPage();
        LearnPaper papers = new LearnPaper();
        papers.setSubjectId(subjectId);
        papers.setExamineeId(userId);
        papers.setCollect(true);
        List<LearnPaper> list = papersService.selectPapersListFromFavorites(papers);
        list.stream().forEach(t -> t.setCollect(true));
        return getDataTable(list);
    }

    @ApiOperation("收藏的问题列表")
    @GetMapping("questions")
    public TableDataInfo questions(Long subjectId) {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        startPage();
        LearnQuestions questions = new LearnQuestions();
        questions.setSubjectId(subjectId);
        questions.setUserId(userId);
        List<LearnQuestions> list = questionsService.selectCollectedList(questions);
        questionsService.fillCollectInfo(SecurityUtils.getLoginUser().getUser().getUserId(), list);
        return getDataTable(list);
    }

}
