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.teachcloud.common.bean.result.TaskListResult;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.enums.DownloadFileEnum;
import com.qmth.teachcloud.common.enums.TaskResultEnum;
import com.qmth.teachcloud.common.enums.TaskStatusEnum;
import com.qmth.teachcloud.common.enums.TaskTypeEnum;
import com.qmth.teachcloud.common.service.TBTaskService;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 导入导出任务表 前端控制器
 * </p>
 *
 * @author wangliang
 * @since 2021-03-19
 */
@Api(tags = "数据管理-任务管理Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_DATA + "/task")
@Validated
public class TBTaskController {
    private final static Logger log = LoggerFactory.getLogger(TBTaskController.class);

    @Resource
    TBTaskService tbTaskService;

    @ApiOperation(value = "任务管理查询接口")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "任务管理列表信息", response = TaskListResult.class)})
    public Result taskQuery(@ApiParam(value = "学期", required = false) @RequestParam(value = "semesterId", required = false) Long semesterId,
                            @ApiParam(value = "考试", required = false) @RequestParam(value = "examId", required = false) Long examId,
                            @ApiParam(value = "课程代码", required = false) @RequestParam(value = "courseCode", required = false) String courseCode,
                            @ApiParam(value = "试着编号", required = false) @RequestParam(value = "paperNumber", required = false) String paperNumber,
                            @ApiParam(value = "印刷计划id", required = false) @RequestParam(required = false) String printPlanId,
                            @ApiParam(value = "任务状态", required = false) @RequestParam(required = false) TaskStatusEnum status,
                            @ApiParam(value = "任务类型", required = false) @RequestParam(required = false) TaskTypeEnum type,
                            @ApiParam(value = "任务数据结果", required = false) @RequestParam(required = false) TaskResultEnum result,
                            @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) {
        IPage<TaskListResult> taskListResultIPage = tbTaskService.query(new Page<>(pageNumber, pageSize), semesterId, examId, courseCode, paperNumber, Objects.nonNull(ServletUtil.getRequestHeaderSchoolId()) ? Long.parseLong(ServletUtil.getRequestHeaderSchoolId().toString()) : null, SystemConstant.convertIdToLong(printPlanId), status, type, result);
        if (Objects.nonNull(taskListResultIPage) && Objects.nonNull(taskListResultIPage.getRecords()) && taskListResultIPage.getRecords().size() > 0) {
            List<TaskListResult> taskListResultList = taskListResultIPage.getRecords();
            for (TaskListResult taskListResult : taskListResultList) {
                if (Objects.equals(taskListResult.getStatus(), TaskStatusEnum.FINISH.getTitle()) && Objects.equals(taskListResult.getResult(), TaskResultEnum.ERROR.getTitle())
//                        && taskListResult.getResetCount() >= dictionaryConfig.sysDomain().getAutoCreatePdfResetMaxCount()
                        && Objects.isNull(taskListResult.getErrorMessage())) {
                    taskListResult.setResetCreatePdf(true);
                }
                // 下载类型
                taskListResult.setDownloadType(taskListResult.getHasImportFile() ? DownloadFileEnum.IMPORT_FILE.name() : DownloadFileEnum.RESULT.name());
            }
        }
        return ResultUtil.ok(taskListResultIPage);
    }

    @ApiOperation(value = "任务管理查询接口")
    @PostMapping("/query_result")
    @ApiResponses({@ApiResponse(code = 200, message = "任务管理列表信息", response = TaskListResult.class)})
    public Result taskQuery(@ApiParam(value = "学期", required = false) @RequestParam(value = "semesterId", required = false) Long semesterId,
                            @ApiParam(value = "考试", required = false) @RequestParam(value = "examId", required = false) Long examId,
                            @ApiParam(value = "课程代码", required = false) @RequestParam(value = "courseCode", required = false) String courseCode,
                            @ApiParam(value = "试着编号", required = false) @RequestParam(value = "paperNumber", required = false) String paperNumber,
                            @ApiParam(value = "任务状态", required = false) @RequestParam(required = false) TaskStatusEnum status,
                            @ApiParam(value = "任务类型", required = false) @RequestParam(required = false) TaskTypeEnum type,
                            @ApiParam(value = "任务数据结果", required = false) @RequestParam(required = false) TaskResultEnum result,
                            @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) {
        IPage<TaskListResult> taskListResultIPage = tbTaskService.queryResult(new Page<>(pageNumber, pageSize), semesterId, examId, courseCode, paperNumber, status, type, result);
        if (Objects.nonNull(taskListResultIPage) && Objects.nonNull(taskListResultIPage.getRecords()) && taskListResultIPage.getRecords().size() > 0) {
            List<TaskListResult> taskListResultList = taskListResultIPage.getRecords();
            for (TaskListResult taskListResult : taskListResultList) {
                if (Objects.equals(taskListResult.getStatus(), TaskStatusEnum.FINISH.getTitle()) && Objects.equals(taskListResult.getResult(), TaskResultEnum.ERROR.getTitle())
                        && Objects.isNull(taskListResult.getErrorMessage())) {
                    taskListResult.setResetCreatePdf(true);
                }
                // 下载类型
                taskListResult.setDownloadType(taskListResult.getHasImportFile() ? DownloadFileEnum.IMPORT_FILE.name() : DownloadFileEnum.RESULT.name());
            }
        }
        return ResultUtil.ok(taskListResultIPage);
    }
}
