package com.qmth.sop.server.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.sop.business.bean.result.TaskListResult;
import com.qmth.sop.business.service.TBTaskService;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.DownloadFileEnum;
import com.qmth.sop.common.enums.TaskResultEnum;
import com.qmth.sop.common.enums.TaskStatusEnum;
import com.qmth.sop.common.enums.TaskTypeEnum;
import com.qmth.sop.common.util.Result;
import com.qmth.sop.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.springframework.util.CollectionUtils;
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;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 导入导出任务表 前端控制器
 * </p>
 *
 * @author wangliang
 * @since 2023-07-17
 */
@Api(tags = "任务管理Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_TASK)
public class TBTaskController {

    @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(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), status, type, result);
        if (Objects.nonNull(taskListResultIPage) && !CollectionUtils.isEmpty(taskListResultIPage.getRecords())) {
            List<TaskListResult> taskListResultList = taskListResultIPage.getRecords();
            for (TaskListResult taskListResult : taskListResultList) {
                // 下载类型
                taskListResult.setDownloadType(taskListResult.getHasImportFile() ? DownloadFileEnum.IMPORT_FILE.name() : DownloadFileEnum.EXPORT_FILE.name());
            }
        }
        return ResultUtil.ok(taskListResultIPage);
    }
}
