package com.qmth.distributed.print.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.params.DownloadPaperFileParam;
import com.qmth.distributed.print.business.service.DownloadService;
import com.qmth.distributed.print.business.service.PrintCommonService;
import com.qmth.distributed.print.business.templete.execute.AsyncDownloadPaperFileBatchService;
import com.qmth.teachcloud.common.bean.result.TaskListResult;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.TBTask;
import com.qmth.teachcloud.common.enums.TaskTypeEnum;
import com.qmth.teachcloud.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.Map;
import java.util.Objects;

/**
 * 下载管理
 */
@Api(tags = "系统管理-下载管理Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.data}/download")
public class DownloadController {

    @Resource
    private DownloadService downloadService;
    @Resource
    private PrintCommonService printCommonService;
    @Resource
    private AsyncDownloadPaperFileBatchService asyncDownloadPaperFileBatchService;

    /**
     * 分页
     *
     * @param semesterId 学期ID
     * @param examId     考试ID
     * @param orgId      机构Id
     * @param courseName 课程名称
     * @param pageNumber 分页页码
     * @param pageSize   分页数
     * @return Object
     */
    @ApiOperation(value = "分页查询")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "分页查询", response = TaskListResult.class)})
    public Object page(@ApiParam(value = "学期ID", required = false) @RequestParam(required = false) Long semesterId,
                       @ApiParam(value = "考试ID", required = false) @RequestParam(required = false) Long examId,
                       @ApiParam(value = "学院ID", required = false) @RequestParam(required = false) Long orgId,
                       @ApiParam(value = "课程名称", required = false) @RequestParam(required = false) String courseName,
                       @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) {

        return ResultUtil.ok(downloadService.page(semesterId, examId, orgId, courseName, pageNumber, pageSize));
    }

    /**
     * 单个科目下载（只下载未曝光试卷）
     *
     * @param id 任务ID
     */
    @ApiOperation(value = "单课程下载")
    @RequestMapping(value = "/download_one", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "单课程下载", response = TaskListResult.class)})
    public void downloadOne(HttpServletResponse response, @ApiParam(value = "任务ID", required = false) @RequestParam(required = false) Long id) throws Exception {
        downloadService.paperDownload(response, id);
    }

    @ApiOperation(value = "批量下载")
    @RequestMapping(value = "/download_batch", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "批量下载", response = TaskListResult.class)})
    public Object downloadBatch(@Valid @RequestBody DownloadPaperFileParam downloadPaperFileParam, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        Map<String, Object> map = printCommonService.saveTask(TaskTypeEnum.DOWNLOAD_PAPER_FILE_BATCH);
        map.put("downloadPaperFileParam", downloadPaperFileParam);
        asyncDownloadPaperFileBatchService.exportTask(map);
        TBTask tbTask = Objects.nonNull(map.get(SystemConstant.TASK)) ? (TBTask) map.get(SystemConstant.TASK) : null;
        return Objects.nonNull(tbTask) ? ResultUtil.ok(tbTask.getId()) : ResultUtil.error("创建任务失败");
    }
}
