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.GradeBatchPaperParam;
import com.qmth.distributed.print.business.bean.result.EditResult;
import com.qmth.distributed.print.business.bean.result.analyze.GradeBatchPaperResult;
import com.qmth.distributed.print.business.entity.GradeBatch;
import com.qmth.distributed.print.business.entity.GradeBatchPaper;
import com.qmth.distributed.print.business.service.GradeBatchPaperService;
import com.qmth.distributed.print.business.service.GradeBatchService;
import com.qmth.distributed.print.business.templete.execute.AsyncTeachCloudReportService;
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;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 分析-批次试卷关联关系表 前端控制器
 * </p>
 *
 * @author wangliang
 * @since 2022-05-20
 */
@Api(tags = "分析批次课程管理Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.grade}/batch/paper")
@Validated
//@Aac(strict = BOOL.FALSE, auth = BOOL.FALSE)
public class GradeBatchPaperController {
    @Resource
    private GradeBatchPaperService gradeBatchPaperService;

    @Resource
    private GradeBatchService gradeBatchService;

    @Resource
    private AsyncTeachCloudReportService asyncTeachCloudReportService;

    @ApiOperation(value = "成绩分析批次课程-查询")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = GradeBatchPaperResult.class)})
    public Result findGradeBatchPaperPage(@ApiParam(value = "批次id", required = true) @RequestParam String batchId,
                                          @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) {

        if (SystemConstant.isOneNull(batchId)) {
            throw ExceptionResultEnum.ERROR.exception("参数不完整");
        }
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(gradeBatchPaperService.findGradeBatchPaperPage(SystemConstant.convertIdToLong(batchId), SystemConstant.convertIdToLong(examId), courseCode, pageNumber, pageSize, requestUser));
    }

    @ApiOperation(value = "成绩分析批次课程-查询可分析试卷")
    @RequestMapping(value = "/able", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = GradeBatchPaperResult.class)})
    public Result findAbleAnalyzePaperList(@ApiParam(value = "批次id", required = true) @RequestParam String batchId,
                                           @ApiParam(value = "考试id") @RequestParam(required = false) String examId,
                                           @ApiParam(value = "学院id") @RequestParam(required = false) String collegeId,
                                           @ApiParam(value = "课程编号") @RequestParam(required = false) String courseCode,
                                           @ApiParam(value = "命题教师") @RequestParam(required = false) String teacherName) {

        if (SystemConstant.isOneNull(batchId)) {
            throw ExceptionResultEnum.ERROR.exception("参数不完整");
        }
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(gradeBatchPaperService.findAbleAnalyzePaperList(SystemConstant.convertIdToLong(batchId), SystemConstant.convertIdToLong(examId), SystemConstant.convertIdToLong(collegeId), courseCode, teacherName, requestUser));
    }

    @ApiOperation(value = "成绩分析批次课程-新建")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "更新成功", response = Result.class)})
    public Result saveGradeBatchPaper(@Valid @RequestBody GradeBatchPaperParam gradeBatchPaperParam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        gradeBatchPaperService.saveGradeBatchPaper(gradeBatchPaperParam, requestUser);
        return ResultUtil.ok();
    }

    @ApiOperation(value = "成绩分析批次课程-删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "删除成功", response = EditResult.class)})
    public Result deleteGradeBatchPaper(@ApiParam(value = "批次ID", required = true) @RequestParam Long batchId,
                                        @ApiParam(value = "选择的要删除的成绩分析课程id集合", required = true) @RequestParam List<String> idList) {
        gradeBatchPaperService.deleteGradeBatchPaper(batchId, idList.stream().map(SystemConstant::convertIdToLong).collect(Collectors.toList()));
        return ResultUtil.ok();
    }

    /**
     * 成绩分析批次课程-发布
     *
     * @param gradeBatchPaperId 批次试卷ID
     */
    @ApiOperation(value = "成绩分析批次课程-发布")
    @RequestMapping(value = "/publish", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "发布成功", response = EditResult.class)})
    public Result publish(@RequestParam Long batchId, @RequestParam Long gradeBatchPaperId) {
        GradeBatchPaper gradeBatchPaper = gradeBatchPaperService.getById(gradeBatchPaperId);
        if(gradeBatchPaper == null){
            throw ExceptionResultEnum.ERROR.exception("没有批次课程信息");
        }
        asyncTeachCloudReportService.publish(gradeBatchPaper);
        return ResultUtil.success(true);
    }
}
