package com.qmth.distributed.print.api;


import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.result.EditResult;
import com.qmth.distributed.print.business.service.BasicExamService;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.BasicExam;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

/**
 * <p>
 * 考试管理 前端控制器
 * </p>
 */
@Api(tags = "考试管理controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.basic}/exam")
public class BasicExamController {
    @Resource
    private BasicExamService basicExamService;

    /**
     * 考试分页查询
     *
     * @param semesterId 学期ID
     * @param enable     是否启用
     * @param pageNumber 分页参数
     * @param pageSize   分页参数
     */
    @ApiOperation(value = "考试管理-查询")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result pageExam(@RequestParam(value = "semesterId", required = false) Long semesterId,
                           @RequestParam(value = "enable", required = false) Boolean enable,
                           @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                           @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        return ResultUtil.ok(basicExamService.basicExamPage(semesterId, enable, pageNumber, pageSize));
    }

    /**
     * 模糊查询
     *
     * @param inUsed 1:只查询当前使用的学期  0：只查询不是当前使用的学期  null:查询所有
     */
    @ApiOperation(value = "考试模糊查询")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result query(@RequestParam(value = "semesterId", required = false) Long semesterId,
                        @RequestParam(value = "inUsed", required = false) Boolean inUsed) {
        return ResultUtil.ok(basicExamService.query(semesterId, inUsed));
    }

    /**
     * 新增、编辑
     *
     * @param basicExam 考试entity
     */
    @ApiOperation(value = "考试管理-新增/编辑")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "更新成功", response = EditResult.class)})
    public Result saveBasicExam(@Valid @RequestBody BasicExam basicExam, BindingResult bindingResult) throws IllegalAccessException {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultUtil.ok(basicExamService.saveBasicExam(basicExam));
    }

    /**
     * 启用/禁用
     *
     * @param id     主键
     * @param enable 禁用标识
     */
    @ApiOperation(value = "启用/禁用")
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    public Result enable(@ApiParam(value = "考试ID", required = true) @RequestParam Long id,
                         @ApiParam(value = "启用/禁用", required = true) @RequestParam Boolean enable) {
        boolean isSuccess = basicExamService.enable(id, enable);
        return ResultUtil.ok(isSuccess);
    }

    /**
     * 考试删除
     *
     * @param id 考试ID
     */
    @ApiOperation(value = "考试管理-删除（逻辑）")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "删除成功", response = EditResult.class)})
    public Result deleteBasicSemester(@ApiParam(value = "考试id", required = true) @RequestParam String id) {
        return ResultUtil.ok(basicExamService.deleteBasicExamById(SystemConstant.convertIdToLong(id)));
    }

    /**
     * 设置为当前使用考试
     *
     * @param id 考试ID
     */
    @ApiOperation(value = "考试管理-设置为当前使用考试")
    @RequestMapping(value = "/set_in_used", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "更新成功", response = EditResult.class)})
    public Result saveBasicExam(@RequestParam("id") Long id) {
        return ResultUtil.ok(basicExamService.setInUsed(id));
    }
}
