package com.qmth.distributed.print.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.params.analyze.GradePaperDefineParam;
import com.qmth.distributed.print.business.bean.result.analyze.GradeInitResult;
import com.qmth.distributed.print.business.bean.result.analyze.GradePaperDefineResult;
import com.qmth.distributed.print.business.service.GradeInitializeService;
import com.qmth.distributed.print.business.service.GradePaperService;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import com.qmth.teachcloud.common.util.ServletUtil;
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>
 *
 * @author wangliang
 * @since 2022-05-20
 */
@Api(tags = "分析课程（试卷）管理Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.grade}/paper")
@Validated
//@Aac(strict = BOOL.FALSE, auth = BOOL.FALSE)
public class GradePaperController {
    @Resource
    private GradePaperService gradePaperService;
    @Resource
    private GradeInitializeService gradeInitializeService;

    @ApiOperation(value = "成绩分析课程数据初始化")
    @RequestMapping(value = "/initialize", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = GradeInitResult.class)})
    public Result findGradePaperInitialize(@ApiParam(value = "学期id") @RequestParam(required = false) String semesterId,
                                           @ApiParam(value = "考试id") @RequestParam(required = false) String examId,
                                           @ApiParam(value = "课程编号") @RequestParam(required = false) String courseCode,
                                           @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) {

        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(gradeInitializeService.findNeedAnalyzedPaper(SystemConstant.convertIdToLong(semesterId), SystemConstant.convertIdToLong(examId), courseCode, requestUser, pageNumber, pageSize));
    }

    @ApiOperation(value = "成绩分析课程-查询定义")
    @RequestMapping(value = "/paper_define", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = GradePaperDefineResult.class)})
    public Result findGradePaperDefine(@ApiParam(value = "课程编号", required = true) @RequestParam String paperNumber,
                                       @ApiParam(value = "课程编号", required = true) @RequestParam String paperType) {
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(gradePaperService.findGradePaperDefine(paperNumber, paperType, requestUser));
    }


    @ApiOperation(value = "成绩分析课程-新建定义")
    @RequestMapping(value = "/save_define", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "更新成功", response = Result.class)})
    public Result saveGradePaperDefine(@Valid @RequestBody GradePaperDefineParam gradePaperDefineParam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        gradePaperService.saveGradePaperDefine(gradePaperDefineParam, requestUser);
        return ResultUtil.ok();
    }
}
