package ink.wgink.module.examine.controller.api.paper.question;

import ink.wgink.annotation.CheckRequestBodyAnnotation;
import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.exceptions.ParamsException;
import ink.wgink.exceptions.RemoveException;
import ink.wgink.exceptions.SearchException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.examine.pojo.dtos.paper.question.PaperQuestionDTO;
import ink.wgink.module.examine.pojo.dtos.question.QuestionDTO;
import ink.wgink.module.examine.pojo.vos.paper.question.PaperQuestionRandomSaveVO;
import ink.wgink.module.examine.service.paper.question.IPaperQuestionService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.pojo.vos.IdsVO;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: PaperQuestionController
 * @Description: 试卷试题
 * @Author: wanggeng
 * @Date: 2022/5/23 11:12
 * @Version: 1.0
 */
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "试卷试题接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/paper/question")
public class PaperQuestionController extends DefaultBaseController {

    @Autowired
    private IPaperQuestionService paperQuestionService;

    @ApiOperation(value = "保存试题", notes = "保存试题接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paperId", value = "试卷ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("save/paper-id/{paperId}")
    public SuccessResult save(@PathVariable("paperId") String paperId, @RequestBody IdsVO idsVO) {
        if(idsVO.getIds().isEmpty()) {
            throw new ParamsException("试题列表不能为空");
        }
        paperQuestionService.save(paperId, idsVO.getIds());
        return new SuccessResult();
    }

    @ApiOperation(value = "随机保存试题", notes = "随机保存试题接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paperId", value = "试卷ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("save-random/paper-id/{paperId}")
    @CheckRequestBodyAnnotation
    public SuccessResult saveRandom(@PathVariable("paperId") String paperId, @RequestBody PaperQuestionRandomSaveVO paperQuestionRandomSaveVO) {
        paperQuestionService.saveRandom(paperId, paperQuestionRandomSaveVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "删除试卷试题(id列表)", notes = "删除试卷试题(id列表)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("delete/{ids}")
    public SuccessResult delete(@PathVariable("ids") String ids) throws RemoveException {
        paperQuestionService.delete(Arrays.asList(ids.split("\\_")));
        return new SuccessResult();
    }

    @ApiOperation(value = "试卷试题列表", notes = "试卷试题列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paperId", value = "试卷ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list/paper-id/{paperId}")
    public List<PaperQuestionDTO> listPage(@PathVariable("paperId") String paperId) throws SearchException {
        Map<String, Object> params = requestParams();
        return paperQuestionService.list(paperId, params);
    }

    @ApiOperation(value = "试卷试题分页列表", notes = "试卷试题分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paperId", value = "试卷ID", paramType = "path"),
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage/paper-id/{paperId}")
    public SuccessResultList<List<PaperQuestionDTO>> listPage(@PathVariable("paperId") String paperId, ListPage page) throws SearchException {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return paperQuestionService.listPage(paperId, page);
    }

    @ApiOperation(value = "不包含试卷的试题分页列表", notes = "不包含试卷的试题分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "excludePaperId", value = "不包含的试卷ID", paramType = "path"),
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage-question/exclude-paper-id/{excludePaperId}")
    public SuccessResultList<List<QuestionDTO>> listPageQuestionByExcludePaperId(@PathVariable("excludePaperId") String excludePaperId, ListPage page) throws SearchException {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return paperQuestionService.listPageQuestionByExcludePaperId(excludePaperId, page);
    }

}
