package com.qmth.distributed.print.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.service.TBSyncTaskService;
import com.qmth.teachcloud.common.bean.result.SyncListResult;
import com.qmth.teachcloud.common.bean.result.TaskListResult;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.enums.PushTypeEnum;
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 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.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: 同步任务管理-前端控制器
 * @Author: CaoZixuan
 * @Date: 2021-11-08
 */
@Api(tags = "数据管理-任务管理Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.data}/sync")
@Validated
public class TBSyncTaskController {
    private final static Logger log = LoggerFactory.getLogger(TBSyncTaskController.class);

    @Resource
    TBSyncTaskService tbSyncTaskService;

    @ApiOperation(value = "同步管理查询接口")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "任务管理列表信息", response = TaskListResult.class)})
    public Result syncQuery(@ApiParam(value = "任务状态") @RequestParam(required = false) TaskStatusEnum status,
                            @ApiParam(value = "同步类型") @RequestParam(required = false) PushTypeEnum type,
                            @ApiParam(value = "同步数据结果") @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) {
        Long schoolId = SystemConstant.convertIdToLong(String.valueOf(ServletUtil.getRequestHeaderSchoolId()));
        IPage<SyncListResult> page = tbSyncTaskService.query(schoolId, status, type, result, pageNumber, pageSize);
        return ResultUtil.ok(page);
    }

    /**
     * 重新同步
     *
     * @param id 任务ID
     */
    @ApiOperation(value = "重新同步")
    @RequestMapping(value = "/resync", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "重新同步", response = TaskListResult.class)})
    public Result syncQuery(@ApiParam(value = "任务ID") @RequestParam(required = false) Long id) {
        tbSyncTaskService.resync(id);
        return ResultUtil.ok(true);
    }
}
