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.contant.SystemConstant;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import com.qmth.teachcloud.mark.dto.mark.manage.MarkArbitrateDto;
import com.qmth.teachcloud.mark.dto.mark.manage.MarkArbitrateMarkerDto;
import com.qmth.teachcloud.mark.dto.mark.manage.MarkArbitrateSettingDto;
import com.qmth.teachcloud.mark.dto.mark.manage.Task;
import com.qmth.teachcloud.mark.params.MarkResult;
import com.qmth.teachcloud.mark.service.MarkArbitrateHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
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 + "/arbitrate")
public class MarkArbitrateController {

    @Resource
    private MarkArbitrateHistoryService markArbitrateHistoryService;

    /**
     * 仲裁列表查询
     */
    @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,
                       @ApiParam(value = "分组号") @RequestParam(required = false) Integer groupNumber,
                       @ApiParam(value = "状态") @RequestParam(required = false) String type,
                       @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<MarkArbitrateDto> markArbitrateDtoIPage = markArbitrateHistoryService.pageArbitrate(examId, paperNumber, groupNumber, type, className, pageNumber, pageSize);
        return ResultUtil.ok(markArbitrateDtoIPage);
    }

    /**
     * 清理锁卷缓存
     */
    @ApiOperation(value = "清理锁卷缓存")
    @RequestMapping(value = "/clear", method = RequestMethod.POST)
    public Result clear(@ApiParam(value = "仲裁历史ID") @RequestParam(required = false) Long arbitrateId) {
        markArbitrateHistoryService.clearArbitrate(arbitrateId);
        return ResultUtil.ok(true);
    }

    /**
     * 获取评卷状态
     */
    @ApiOperation(value = "获取评卷状态")
    @RequestMapping(value = "/getStatus", method = RequestMethod.POST)
    public Result getStatus(@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(markArbitrateHistoryService.getArbitrateStatus(examId, paperNumber, groupNumber));
    }

    /**
     * 左边的列表
     */
    @ApiOperation(value = "左边的列表")
    @RequestMapping(value = "/getArbitrationList", method = RequestMethod.POST)
    public Result getArbitrationList(@ApiParam(value = "仲裁历史ID", required = true) @RequestParam Long arbitrateId) {
        List<MarkArbitrateMarkerDto> markArbitrateDtoList = markArbitrateHistoryService.listMarkerArbitrateRecord(arbitrateId);
        return ResultUtil.ok(markArbitrateDtoList);
    }

    /**
     * 获取配置信息
     */
    @ApiOperation(value = "获取配置信息")
    @RequestMapping(value = "/getSetting", method = RequestMethod.POST)
    public Result getSetting(@ApiParam(value = "仲裁历史ID") @RequestParam(required = false) Long arbitrateId,
                             @ApiParam(value = "考试ID") @RequestParam(required = false) Long examId,
                             @ApiParam(value = "试卷编号") @RequestParam(required = false) String paperNumber,
                             @ApiParam(value = "分组号") @RequestParam(required = false) Integer groupNumber) {
        MarkArbitrateSettingDto markArbitrateSettingDto = markArbitrateHistoryService.getArbitrateSetting(arbitrateId, examId, paperNumber, groupNumber);
        return ResultUtil.ok(markArbitrateSettingDto);
    }

    /**
     * 获取单个任务
     */
    @ApiOperation(value = "获取单个任务")
    @RequestMapping(value = "/getTask", method = RequestMethod.POST)
    public Result getTask(@ApiParam(value = "仲裁历史ID") @RequestParam(required = false) Long arbitrateId,
                          @ApiParam(value = "考试ID") @RequestParam(required = false) Long examId,
                          @ApiParam(value = "试卷编号") @RequestParam(required = false) String paperNumber,
                          @ApiParam(value = "分组号") @RequestParam(required = false) Integer groupNumber) {
        Task task = markArbitrateHistoryService.getArbitrateTask(arbitrateId, examId, paperNumber, groupNumber);
        return ResultUtil.ok(task);
    }

    /**
     * 保存任务
     */
    @ApiOperation(value = "保存任务")
    @RequestMapping(value = "/saveTask", method = RequestMethod.POST)
    public Result saveTask(@RequestBody MarkResult markResult) {
        markArbitrateHistoryService.saveArbitrateTask(markResult);
        return ResultUtil.ok(true);
    }

    /**
     * 获取单个任务
     */
    @ApiOperation(value = "获取单个任务")
    @RequestMapping(value = "/getHistory", method = RequestMethod.POST)
    public Result getTask(@ApiParam(value = "考试ID") @RequestParam(required = false) Long examId,
                          @ApiParam(value = "试卷编号") @RequestParam(required = false) String paperNumber,
                          @ApiParam(value = "分组号") @RequestParam(required = false) Integer groupNumber,
                          @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                          @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        List<Task> taskList = markArbitrateHistoryService.getHistory(examId, paperNumber, groupNumber, pageNumber, pageSize);
        return ResultUtil.ok(taskList);
    }
}
