package com.qmth.distributed.print.api;

import com.qmth.boot.api.annotation.Aac;
import com.qmth.boot.api.annotation.BOOL;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.params.analyze.GradeModuleEvaluationParam;
import com.qmth.distributed.print.business.bean.result.analyze.GradeModuleEvaluationResult;
import com.qmth.distributed.print.business.service.GradeModuleEvaluationService;
import com.qmth.distributed.print.business.service.PrintCommonService;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
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;

/**
 * <p>
 * 校验-考察点模块评价建议表 前端控制器
 * </p>
 *
 * @author wangliang
 * @since 2022-05-20
 */
@Api(tags = "分析课程维度模块评价管理Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.grade}/module/evaluation")
@Validated
//@Aac(strict = BOOL.FALSE, auth = BOOL.FALSE)
public class GradeModuleEvaluationController {
    @Resource
    private GradeModuleEvaluationService gradeModuleEvaluationService;

    @Resource
    PrintCommonService printCommonService;

    @ApiOperation(value = "成绩分析模块评价-查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = GradeModuleEvaluationResult.class)})
    public Result findGradeModuleEvaluationList(@ApiParam(value = "试卷编号", required = true) @RequestParam String paperNumber,
                                                @ApiParam(value = "试卷类型", required = true) @RequestParam String paperType) {

        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        if (SystemConstant.isOneNull(paperNumber, paperType)) {
            throw ExceptionResultEnum.ERROR.exception("试卷参数不完整");
        }
        return ResultUtil.ok(gradeModuleEvaluationService.findGradeModuleEvaluation(paperNumber, paperType, requestUser));
    }

    @ApiOperation(value = "成绩分析模块评价-新建模块评价")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "更新成功", response = Result.class)})
    public Result saveGradeModuleEvaluationBatch(@Valid @RequestBody GradeModuleEvaluationParam gradeModuleEvaluationParam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        gradeModuleEvaluationService.saveGradeModuleEvaluationBatch(gradeModuleEvaluationParam, requestUser);
        // 更新批次状态
        printCommonService.updateGradeBatchStatus(requestUser.getSchoolId(), gradeModuleEvaluationParam.getPaperNumber(), gradeModuleEvaluationParam.getPaperType());
        return ResultUtil.ok();
    }
}
