package ink.wgink.module.examine.controller.api.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.enums.question.QuestionTypeEnum;
import ink.wgink.module.examine.pojo.dtos.question.QuestionDTO;
import ink.wgink.module.examine.pojo.vos.question.QuestionVO;
import ink.wgink.module.examine.service.question.IQuestionService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultData;
import ink.wgink.pojo.result.SuccessResultList;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
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: TestQuestionController
 * @Description: 试题管理
 * @Author: WenG
 * @Date: 2020-09-09 21:00
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "试题管理接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/question")
public class QuestionController extends DefaultBaseController {

    @Autowired
    private IQuestionService questionService;

    @ApiOperation(value = "新增试题管理", notes = "新增试题管理接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("save")
    @CheckRequestBodyAnnotation
    public SuccessResult saveQuestion(@RequestBody QuestionVO questionVO) throws Exception {
        boolean hanOption = StringUtils.equals(questionVO.getType(), QuestionTypeEnum.CHOICE.getValue())
                || StringUtils.equals(questionVO.getType(), QuestionTypeEnum.TURE_OR_FALSE.getValue());
        if (hanOption && questionVO.getOptions().isEmpty()) {
            throw new ParamsException("选择或判断选项不能为空");
        }
        questionService.save(questionVO);
        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("remove/{ids}")
    public SuccessResult removeQuestion(@PathVariable("ids") String ids) throws RemoveException {
        questionService.remove(Arrays.asList(ids.split("\\_")));
        return new SuccessResult();
    }

    @ApiOperation(value = "修改试题管理", notes = "修改试题管理接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "questionId", value = "试题管理ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("update/{questionId}")
    @CheckRequestBodyAnnotation
    public SuccessResult updateQuestion(@PathVariable("questionId") String questionId, @RequestBody QuestionVO questionVO) throws Exception {
        boolean hanOption = StringUtils.equals(questionVO.getType(), QuestionTypeEnum.CHOICE.getValue())
                || StringUtils.equals(questionVO.getType(), QuestionTypeEnum.TURE_OR_FALSE.getValue());
        if (hanOption && questionVO.getOptions().isEmpty()) {
            throw new ParamsException("选择或判断选项不能为空");
        }
        questionService.update(questionId, questionVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "试题管理详情(通过ID)", notes = "试题管理详情(通过ID)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "questionId", value = "试题管理ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get/{questionId}")
    public QuestionDTO getById(@PathVariable("questionId") String questionId) throws SearchException {
        return questionService.getById(questionId);
    }

    @ApiOperation(value = "试题管理列表", notes = "试题管理列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list")
    public List<QuestionDTO> listQuestion(@RequestParam(name = "questionType", required = false) String questionType) throws SearchException {
        Map<String, Object> params = requestParams();
        setRequestParams(params, questionType);
        return questionService.list(params);
    }

    @ApiOperation(value = "试题管理分页列表", notes = "试题管理分页列表接口")
    @ApiImplicitParams({
            @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")
    public SuccessResultList<List<QuestionDTO>> listPageQuestion(@RequestParam(name = "questionType", required = false) String questionType, ListPage page) throws SearchException {
        Map<String, Object> params = requestParams();
        setRequestParams(params, questionType);
        page.setParams(params);
        return questionService.listPage(page);
    }

    @ApiOperation(value = "试题管理统计", notes = "试题管理统计接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("count")
    SuccessResultData<Integer> countQuestion() throws SearchException {
        Map<String, Object> params = requestParams();
        Integer count = questionService.count(params);
        return new SuccessResultData<>(count);
    }

    private void setRequestParams(Map<String, Object> params, String questionType) {
        if (!StringUtils.isBlank(questionType)) {
            params.remove("questionType");
            params.put("questionTypeIds", Arrays.asList(questionType.split(",")));
        }
    }

}