package com.qmth.distributed.print.api.mark;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qmth.boot.api.annotation.Aac;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.teachcloud.common.annotation.OperationLogDetail;
import com.qmth.teachcloud.mark.dto.mark.manage.MarkGroupClassProgressDto;
import com.qmth.teachcloud.mark.dto.mark.manage.MarkGroupQuestionsDto;
import com.qmth.teachcloud.mark.dto.mark.manage.MarkGroupSummaryProgressDto;
import com.qmth.teachcloud.mark.dto.mark.setting.MarkGroupSingleDto;
import com.qmth.teachcloud.mark.dto.mark.setting.MarkGroupTaskDto;
import com.qmth.teachcloud.common.bean.params.mark.setting.GroupPictureConfigParams;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.enums.log.OperationTypeEnum;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import com.qmth.teachcloud.mark.service.MarkGroupService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * <p>
 * 评卷分组表 前端控制器
 * </p>
 *
 * @author xf
 * @since 2023-09-22
 */
@Api(tags = "评卷-分组管理")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_MARK + "/group")
public class MarkGroupController extends BaseController {

    @Resource
    private MarkGroupService markGroupService;

    /**
     * 分组查询
     */
    @ApiOperation(value = "查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result list(@ApiParam(value = "考试ID", required = true) @RequestParam Long examId,
                       @ApiParam(value = "试卷编号", required = true) @RequestParam String paperNumber) {
        MarkGroupTaskDto markGroupList = markGroupService.listGroupTaskByExamIdAndPaperNumber(examId, paperNumber);
        return ResultUtil.ok(markGroupList);
    }

    /**
     * 新增
     */
    @ApiOperation(value = "新增")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.ADD, detail = "新增分组操作，考试ID：{{markGroupSingleDto.examId}}、试卷编号：{{markGroupSingleDto.paperNumber}}")
    public Result add(@RequestBody MarkGroupSingleDto markGroupSingleDto) {
        validMarkPaperForMark(markGroupSingleDto.getExamId(), markGroupSingleDto.getPaperNumber());
        return ResultUtil.ok(markGroupService.saveGroup(markGroupSingleDto));
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.UPDATE, detail = "修改分组操作，考试ID：{{markGroupSingleDto.examId}}、试卷编号：{{markGroupSingleDto.paperNumber}}、分组号：{{markGroupSingleDto.groupInfo.groupNumber}}")
    public Result update(@RequestBody MarkGroupSingleDto markGroupSingleDto) {
        validMarkPaperForMark(markGroupSingleDto.getExamId(), markGroupSingleDto.getPaperNumber());
        return ResultUtil.ok(markGroupService.updateGroup(markGroupSingleDto));
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.DELETE, detail = "删除分组操作，考试ID：{{examId}}、试卷编号：{{paperNumber}}、分组号：{{groupNumber}}")
    public Result delete(@ApiParam(value = "考试ID", required = true) @RequestParam Long examId,
                         @ApiParam(value = "试卷编号", required = true) @RequestParam String paperNumber,
                         @ApiParam(value = "分组号", required = true) @RequestParam Integer groupNumber) {
        validMarkPaperForMark(examId, paperNumber);
        markGroupService.deleteGroupByExamIdAndPaperNumberAndGroupNumber(examId, paperNumber, groupNumber);
        return ResultUtil.ok(true);
    }

    /**
     * 评卷区设置
     */
    @ApiOperation(value = "评卷区设置")
    @RequestMapping(value = "/update_picture_config", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.UPDATE, detail = "设置评卷区操作，考试ID：{{examId}}、试卷编号：{{paperNumber}}、分组号：{{groupNumber}}")
    public Result updatePictureConfig(@RequestBody GroupPictureConfigParams groupPictureConfigParams) {
        validMarkPaperForMark(groupPictureConfigParams.getExamId(), groupPictureConfigParams.getPaperNumber());
        markGroupService.updatePicListByExamIdAndPaperNumberAndGroupNumber(groupPictureConfigParams);
        return ResultUtil.ok(true);
    }

    /**
     * 开启/关闭分班阅
     */
    @ApiOperation(value = "开启/关闭分班阅")
    @RequestMapping(value = "/update_open_mark_class", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.UPDATE, detail = "开启/关闭分班阅操作，考试ID：{{examId}}、试卷编号：{{paperNumber}}、是否开启分班阅：{{openMarkClass}}")
    public Result delete(@ApiParam(value = "考试ID", required = true) @RequestParam Long examId,
                         @ApiParam(value = "试卷编号", required = true) @RequestParam String paperNumber,
                         @ApiParam(value = "是否开启分班阅", required = true) @RequestParam Boolean openMarkClass) {
        validMarkPaperForMark(examId, paperNumber);
        markGroupService.updateOpenMarkClassByExamIdAndPaperNumber(examId, paperNumber, openMarkClass);
        return ResultUtil.ok(true);
    }

    /**
     * 进度列表查询
     */
    @ApiOperation(value = "进度列表")
    @RequestMapping(value = "/summary", method = RequestMethod.POST)
    public Result summary(@ApiParam(value = "考试ID", required = true) @RequestParam Long examId,
                          @ApiParam(value = "试卷编号", required = true) @RequestParam String paperNumber) {
        MarkGroupSummaryProgressDto markGroupSummaryProgressDto = markGroupService.summaryGroupProgress(examId, paperNumber);
        return ResultUtil.ok(markGroupSummaryProgressDto);
    }

    /**
     * 班级阅卷进度列表查询
     */
    @ApiOperation(value = "班级阅卷进度列表")
    @RequestMapping(value = "/class/summary", method = RequestMethod.POST)
    public Result classSummary(@ApiParam(value = "考试ID", required = true) @RequestParam Long examId,
                               @ApiParam(value = "试卷编号", required = true) @RequestParam String paperNumber,
                               @ApiParam(value = "班级") @RequestParam(required = false) String className,
                               @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                               @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        IPage<MarkGroupClassProgressDto> markGroupClassProgressDtoIPage = markGroupService.summaryGroupClassProgress(examId, paperNumber, className, pageNumber, pageSize);
        return ResultUtil.ok(markGroupClassProgressDtoIPage);
    }

    /**
     * 评阅题目列表
     */
    @Aac(auth = false)
    @ApiOperation(value = "评阅题目列表")
    @RequestMapping(value = "/list/groupQuestions", method = RequestMethod.POST)
    public Result listGroupQuestions(@ApiParam(value = "考试ID", required = true) @RequestParam Long examId,
                                     @ApiParam(value = "试卷编号", required = true) @RequestParam String paperNumber) {
        List<MarkGroupQuestionsDto> markGroupQuestionsDtoList = markGroupService.listGroupQuestions(examId, paperNumber);
        return ResultUtil.ok(markGroupQuestionsDtoList);
    }

}
