package com.qmth.distributed.print.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.result.WorkResult;
import com.qmth.distributed.print.business.bean.result.analyze.GradeInitResult;
import com.qmth.distributed.print.business.entity.ExamPaperStructure;
import com.qmth.distributed.print.business.enums.ExamPaperStructureStatusEnum;
import com.qmth.distributed.print.business.service.ExamPaperStructureService;
import com.qmth.distributed.print.business.service.ExamTaskService;
import com.qmth.distributed.print.business.service.GradeBatchPaperService;
import com.qmth.distributed.print.business.service.GradeInitializeService;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysUser;
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.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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;

/**
 * @Description: 工作台 前端控制器
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2021/4/8
 */
@Api(tags = "工作台Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.work}")
//@Aac(auth = BOOL.FALSE, strict = BOOL.FALSE)
@Validated
public class WorkController {

    @Resource
    ExamTaskService examTaskService;

    @Autowired
    ExamPaperStructureService examPaperStructureService;

    @Resource
    GradeInitializeService gradeInitializeService;

    @ApiOperation(value = "获取待办接口")
    @RequestMapping(value = "/exam/task/ready", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = WorkResult.class)})
    public Result getFlowTaskReady(@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) {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(examTaskService.getFlowTaskReadyList(new Page<>(pageNumber, pageSize), SystemConstant.getHeadOrUserSchoolId(), sysUser.getOrgId(), sysUser.getId()));
    }

    @ApiOperation(value = "获取待办计数接口")
    @RequestMapping(value = "/exam/task/ready_count", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = WorkResult.class)})
    public Result getFlowTaskReadyCount() {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(examTaskService.getFlowTaskReadyList(new Page<>(SystemConstant.PAGE_NUMBER, SystemConstant.PAGE_SIZE), SystemConstant.getHeadOrUserSchoolId(), sysUser.getOrgId(), sysUser.getId()).getRecords().size());
    }

    /**
     * 阅卷待办任务-数量
     *
     * @return
     */
    @ApiOperation(value = "阅卷待办任务-数量")
    @RequestMapping(value = "/structure/task/ready_count", method = RequestMethod.POST)
    public Result countStructure() {
        long count = examPaperStructureService.countByPropositionTeacherId();
        return ResultUtil.ok(count);
    }

    /**
     * 阅卷待办任务-数量
     *
     * @return
     */
    @ApiOperation(value = "阅卷待办任务-列表")
    @RequestMapping(value = "/structure/task/ready", method = RequestMethod.POST)
    public Result list(@RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                       @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        String[] structureStatusEnums = new String[]{ExamPaperStructureStatusEnum.FINISH.name()};
        IPage<ExamPaperStructure> examPaperStructureIPage = examPaperStructureService.listByPropositionTeacherId(null, null, pageNumber, pageSize, structureStatusEnums);
        return ResultUtil.ok(examPaperStructureIPage);
    }

    /**
     * 阅卷待办任务-数量
     */
    @ApiOperation(value = "教研待办任务-数量")
    @RequestMapping(value = "/grade/task/ready_count", method = RequestMethod.POST)
    public Result countReport() {
        long count = gradeInitializeService.countByPropositionTeacherId();
        return ResultUtil.ok(count);
    }

    /**
     * 阅卷待办任务
     */
    @ApiOperation(value = "阅卷待办任务-列表")
    @RequestMapping(value = "/grade/task/ready", method = RequestMethod.POST)
    public Result gradeList(@RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                            @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        IPage<GradeInitResult> needAnalyzedPaper = gradeInitializeService.listNeedAnalyzedPaperToWork(requestUser, pageNumber, pageSize);
        return ResultUtil.ok(needAnalyzedPaper);
    }
}

