package com.ynfy.app.api.v1.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ynfy.app.api.v1.entity.dto.PracticeDTO;
import com.ynfy.buss.exam.question.service.IQuestionService;
import com.ynfy.buss.practice.userpractice.entity.UserPractice;
import com.ynfy.buss.practice.userpractice.entity.dto.UserPracticeDTO;
import com.ynfy.buss.practice.userpractice.service.IUserPracticeService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/v1/practice")
public class ApiPracticeController extends ApiBaseController {

    @Autowired
    public IUserPracticeService userPracticeService;

    @Autowired
    private IQuestionService questionService;


    /**
     * 按练习模式获取指定题库题目
     *
     * @return
     */
    @ApiOperation(value = "按练习模式获取指定题库题目", notes = "按练习模式获取指定题库题目")
    @PostMapping(value = "/listQuestionByMode")
    public Result<?> listQuestionByPracticeMode(@RequestBody PracticeDTO dto) {
        return Result.OK(questionService.listQuestionByPracticeMode(dto.getPageNo(), dto.getPageSize(),
                dto.getRepositoryId(), dto.getQuestionType(), dto.getMode()));
    }

    /**
     * 提交练习
     *
     * @return
     */
    @ApiOperation(value = "提交练习")
    @PostMapping(value = "/submitPractice")
    public Result<?> submitPractice(@RequestBody UserPracticeDTO dto) {
        dto.setUserId(TokenUtil.getUserId(TokenUtil.getToken(request)));
        return Result.OK("提交练习成功！", userPracticeService.submitPractice(dto));
    }


    /**
     * 获取练习结果
     *
     * @return
     */
    @ApiOperation(value = "获取练习结果", notes = "获取练习结果")
    @GetMapping(value = "/getPracticeResult")
    public Result<?> getPracticeResult(@RequestParam String id) {
        return Result.OK(userPracticeService.getPracticeResult(id));
    }


    @ApiOperation(value = "用户练习表-分页列表查询", notes = "用户练习表-分页列表查询")
    @PostMapping(value = "/userPractice/list")
    public Result<IPage<UserPractice>> userPracticeList(@RequestBody PracticeDTO dto) {
        Page<UserPractice> page = new Page<>(dto.getPageNo(), dto.getPageSize());
        UserPractice userPractice = new UserPractice();
        userPractice.setUserId(TokenUtil.getUserId(TokenUtil.getToken(request)));
        userPractice.setPracticeName(dto.getPracticeName());
        userPractice.setRepositoryName(dto.getRepositoryName());
        IPage<UserPractice> pageList = userPracticeService.selectPageList(page, userPractice);
        return Result.OK(pageList);
    }

}
