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


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.teachcloud.common.annotation.OperationLogDetail;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
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.dto.mark.manage.MarkUserGroupProgressDto;
import com.qmth.teachcloud.mark.entity.MarkUserGroup;
import com.qmth.teachcloud.mark.service.MarkUserGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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 + "/marker")
public class MarkUserGroupController extends BaseController {

    @Resource
    private MarkUserGroupService markUserGroupService;

    /**
     * 评卷员管理列表查询
     */
    @ApiOperation(value = "评卷员管理列表查询")
    @RequestMapping(value = "/list", 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) Integer groupNumber,
                               @ApiParam(value = "评卷员") @RequestParam(required = false) String loginName,
                               @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<MarkUserGroupProgressDto> markGroupClassProgressDtoIPage = markUserGroupService.pageGroupUserByExamIdAndPaperNumberAndGroupNumber(examId, paperNumber, groupNumber, loginName, className, pageNumber, pageSize);
        return ResultUtil.ok(markGroupClassProgressDtoIPage);
    }

    /**
     * 评卷员管理列表查询
     */
    @ApiOperation(value = "评卷员管理列表查询")
    @RequestMapping(value = "/list_bind_marker", method = RequestMethod.POST)
    public Result listBindMarker(@ApiParam(value = "考试ID", required = true) @RequestParam Long examId,
                                 @ApiParam(value = "试卷编号", required = true) @RequestParam String paperNumber,
                                 @ApiParam(value = "分组号", required = true) @RequestParam Integer groupNumber) {
        return ResultUtil.ok(markUserGroupService.listBindMarkers(examId, paperNumber, groupNumber));
    }

    /**
     * 绑定评卷员
     */
    @ApiOperation(value = "绑定评卷员")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.UPDATE, detail = "绑定评卷员操作，考试ID：{{examId}}、试卷编号：{{paperNumber}}、分组号：{{groupNumber}}、评卷员ID集合：{{userIds}}")
    public Result addMarker(@ApiParam(value = "考试ID", required = true) @RequestParam Long examId,
                            @ApiParam(value = "试卷编号", required = true) @RequestParam String paperNumber,
                            @ApiParam(value = "分组号", required = true) @RequestParam Integer groupNumber,
                            @ApiParam(value = "评卷员ID集合", required = true) @RequestParam List<Long> userIds) {
        validMarkPaperForMark(examId, paperNumber);
        markUserGroupService.addMarkUserGroup(examId, paperNumber, groupNumber, userIds);
        return ResultUtil.ok(true);
    }

    /**
     * 重置评卷员
     */
    @ApiOperation(value = "重置评卷员")
    @RequestMapping(value = "/reset", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.UPDATE, detail = "重置评卷员操作，用户分组ID：{{markUserGroupId}}")
    public Result resetMarker(@ApiParam(value = "用户分组ID", required = true) @RequestParam Long markUserGroupId) {
        MarkUserGroup markUserGroup = markUserGroupService.getById(markUserGroupId);
        if (markUserGroup == null) {
            throw ExceptionResultEnum.ERROR.exception("评卷员不存在");
        }
        validMarkPaperForMark(markUserGroup.getExamId(), markUserGroup.getPaperNumber());
        return ResultUtil.ok(markUserGroupService.resetMarker(markUserGroup));
    }

    /**
     * 解绑评卷员
     */
    @ApiOperation(value = "解绑评卷员")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.UPDATE, detail = "解绑评卷员操作，用户分组ID：{{markUserGroupId}}")
    public Result deleteMarker(@ApiParam(value = "用户分组ID", required = true) @RequestParam Long markUserGroupId) {
        MarkUserGroup markUserGroup = markUserGroupService.getById(markUserGroupId);
        if (markUserGroup == null) {
            throw ExceptionResultEnum.ERROR.exception("评卷员不存在");
        }
        validMarkPaperForMark(markUserGroup.getExamId(), markUserGroup.getPaperNumber());
        markUserGroupService.deleteMarker(markUserGroupId);
        return ResultUtil.ok(true);
    }

    /**
     * 回收评卷员正在评卷的试卷
     */
    @ApiOperation(value = "回收正在评卷的试卷")
    @RequestMapping(value = "/release", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.UPDATE, detail = "回收正在评卷的试卷操作，用户分组ID集合：{{markUserGroupIds}}")
    public Result releaseMarker(@ApiParam(value = "用户分组ID集合", required = true) @RequestParam List<Long> markUserGroupIds) {
        MarkUserGroup markUserGroup = markUserGroupService.getById(markUserGroupIds.get(0));
        if (markUserGroup == null) {
            throw ExceptionResultEnum.ERROR.exception("评卷员不存在");
        }
        validMarkPaperForMark(markUserGroup.getExamId(), markUserGroup.getPaperNumber());
        markUserGroupService.releaseMarker(markUserGroupIds);
        return ResultUtil.ok(true);
    }

    /**
     * 设置评卷任务
     */
    @ApiOperation(value = "设置评卷任务")
    @RequestMapping(value = "/setTaskCount", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.UPDATE, detail = "设置评卷任务操作，用户分组ID集合：{{markUserGroupIds}}、评卷任务数：{{topCount}}")
    public Result setTaskCount(@ApiParam(value = "用户分组ID集合", required = true) @RequestParam List<Long> markUserGroupIds,
                               @ApiParam(value = "评卷任务数", required = true) @RequestParam Integer topCount) {
        MarkUserGroup markUserGroup = markUserGroupService.getById(markUserGroupIds.get(0));
        if (markUserGroup == null) {
            throw ExceptionResultEnum.ERROR.exception("评卷员不存在");
        }
        validMarkPaperForMark(markUserGroup.getExamId(), markUserGroup.getPaperNumber());
        markUserGroupService.setTopCount(markUserGroupIds, topCount);
        return ResultUtil.ok(true);
    }

}
