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.annotation.Aac;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.result.EditResult;
import com.qmth.distributed.print.business.enums.ExamDetailStatusEnum;
import com.qmth.distributed.print.business.service.ExamDetailService;
import com.qmth.distributed.print.business.service.TBTaskPdfService;
import com.qmth.teachcloud.common.bean.result.TaskListResult;
import com.qmth.teachcloud.common.bean.result.TaskPdfResult;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.enums.CreatePdfTypeEnum;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.enums.TaskResultEnum;
import com.qmth.teachcloud.common.enums.TaskStatusEnum;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;

/**
 * <p>
 * pdf生成 前端控制器
 * </p>
 */
@Api(tags = "PDF生成管理Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_DATA + "/task")
@Validated
public class TBTaskPdfController {
    private final static Logger log = LoggerFactory.getLogger(TBTaskPdfController.class);

    @Resource
    TBTaskPdfService tbTaskPdfService;

    @Resource
    ExamDetailService examDetailService;

    @Aac(auth = false)
    @ApiOperation(value = "PDF生成管理查询接口")
    @RequestMapping(value = "/page_pdf_create", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "PDF生成管理列表信息", response = TaskListResult.class)})
    public Result taskQuery(@ApiParam(value = "学期ID", required = true) @RequestParam(value = "semesterId", required = false) Long semesterId,
                            @ApiParam(value = "考试ID", required = true) @RequestParam(value = "examId", required = false) Long examId,
                            @ApiParam(value = "印刷计划ID") @RequestParam(required = false) String printPlanId,
                            @ApiParam(value = "课程代码") @RequestParam(value = "courseCode", required = false) String courseCode,
                            @ApiParam(value = "试着编号") @RequestParam(value = "paperNumber", required = false) String paperNumber,
                            @ApiParam(value = "任务状态") @RequestParam(value = "status", required = false) TaskStatusEnum status,
                            @ApiParam(value = "任务数据结果") @RequestParam(value = "result", required = false) TaskResultEnum result,
                            @ApiParam(value = "考点") @RequestParam(value = "examPlace", required = false) String examPlace,
                            @ApiParam(value = "考场") @RequestParam(value = "examRoom", required = false) String examRoom,
                            @ApiParam(value = "考试时间（开始）") @RequestParam(value = "examStartTime", required = false) Long examStartTime,
                            @ApiParam(value = "考试时间（结束）") @RequestParam(value = "examEndTime", required = false) Long examEndTime,
                            @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 (semesterId == null) {
            throw ExceptionResultEnum.SEMESTER_ID_IS_NULL.exception();
        }
        if (examId == null) {
            throw ExceptionResultEnum.EXAM_ID_IS_NULL.exception();
        }
        IPage<TaskPdfResult> taskListResultIPage = tbTaskPdfService.pageData(new Page<>(pageNumber, pageSize), examId, printPlanId, courseCode, paperNumber, status, result, examPlace, examRoom, examStartTime, examEndTime);
        return ResultUtil.ok(taskListResultIPage);
    }

    @ApiOperation(value = "重新生成pdf")
    @RequestMapping(value = "/reset_create_pdf", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "重新生成pdf", response = EditResult.class)})
    public Result resetCreatePdf(@ApiParam(value = "任务id", required = true) @RequestParam Long id,
                                 @ApiParam(value = "pdf生成类型") @RequestParam(defaultValue = "ALL") CreatePdfTypeEnum type) {
        examDetailService.resetExamDetail(id, type, ExamDetailStatusEnum.NEW, false);
        tbTaskPdfService.resetRunningCreatePdf(id, type);
        return ResultUtil.ok(new EditResult());
    }

}
