package com.qmth.paper.library.api;


import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.paper.library.business.bean.result.EditResult;
import com.qmth.paper.library.common.entity.BasicExam;
import com.qmth.paper.library.business.service.BasicExamService;
import com.qmth.paper.library.common.contant.ApiPrefixConstant;
import com.qmth.paper.library.common.contant.SystemConstant;
import com.qmth.paper.library.common.util.Result;
import com.qmth.paper.library.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
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 + ApiPrefixConstant.PREFIX_BASIC + "/exam")
@Validated
public class BasicExamController {
    @Resource
    private BasicExamService basicExamService;

    @ApiOperation(value = "分页查询")
    @PostMapping("/page")
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result page(@ApiParam(value = "学期ID") @RequestParam(value = "semesterId", required = false) Long semesterId,
                       @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                       @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        return ResultUtil.ok(basicExamService.pageData(semesterId, pageNumber, pageSize));
    }


    @ApiOperation(value = "新增/编辑")
    @PostMapping("/save")
    @ApiResponses({@ApiResponse(code = 200, message = "保存/更新成功", response = EditResult.class)})
    public Result save(@Valid @RequestBody BasicExam basicExam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultUtil.ok(basicExamService.saveData(basicExam));
    }


    @ApiOperation(value = "删除")
    @PostMapping("/delete")
    @ApiResponses({@ApiResponse(code = 200, message = "删除成功", response = Result.class)})
    public Result delete(@ApiParam(value = "主键") @RequestParam String ids) {
        return ResultUtil.ok(basicExamService.delete(ids));
    }

}
