package com.qmth.distributed.print.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.params.SyncDataParam;
import com.qmth.distributed.print.business.bean.result.EditResult;
import com.qmth.distributed.print.business.service.ExamPrintPlanService;
import com.qmth.teachcloud.common.contant.SystemConstant;
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.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

/**
 * <p>
 * 印刷计划管理 前端控制器
 * </p>
 *
 * @author xf
 * @since 2021-03-23
 */
@Api(tags = "印刷计划管理Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.exam}/print_sync")
@Validated
public class ExamPrintPlanSyncController {
    @Resource
    private ExamPrintPlanService examPrintPlanService;

    /**
     * 需要合并的计划查询
     *
     * @param printPlanId
     * @param startTime
     * @param endTime
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "印刷计划合并管理-计划查询")
    @RequestMapping(value = "/list_sync", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result findPrintPlanPage(@ApiParam(value = "学期ID") @RequestParam(required = false) Long semesterId,
                                    @ApiParam(value = "考试ID") @RequestParam(required = false) Long examId,
                                    @ApiParam(value = "印刷计划ID") @RequestParam(required = false) Long printPlanId,
                                    @ApiParam(value = "计划创建时间段开始时间") @RequestParam(required = false) Long startTime,
                                    @ApiParam(value = "计划创建时间段结束时间") @RequestParam(required = false) Long endTime,
                                    @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 = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        return ResultUtil.ok(examPrintPlanService.printPlanSyncPage(schoolId, semesterId, examId, printPlanId, startTime, endTime, pageNumber, pageSize));
    }

    /**
     * 已同步过的云阅卷考试列表
     * @param semesterId 学期ID
     * @param examId 考试ID
     */
    @ApiOperation(value = "印刷计划合并管理-合并推送-云阅卷考试ID列表")
    @RequestMapping(value = "/list_relate_ids", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result findPrintPlanPage(@ApiParam(value = "学期ID") @RequestParam(required = false) Long semesterId,
                                    @ApiParam(value = "考试ID") @RequestParam(required = false) Long examId) {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        return ResultUtil.ok(examPrintPlanService.printSyncPlan(schoolId, semesterId, examId));
    }


    /**
     * 多计划合并同步
     *
     * @param syncDataParam
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "印刷计划合并管理-合并推送-多计划合并同步")
    @RequestMapping(value = "/sync_data_merge", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = EditResult.class)})
    public Result syncDataCloud(@RequestBody SyncDataParam syncDataParam) {
        examPrintPlanService.syncDataMerge(syncDataParam);
        return ResultUtil.ok(true);
    }

    /**
     * 批量同步
     *
     * @param syncDataParam
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "印刷计划合并管理-批量同步")
    @RequestMapping(value = "/sync_data_cloud_batch", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = EditResult.class)})
    public Result syncDataCloudBatch(@RequestBody SyncDataParam syncDataParam) {
        examPrintPlanService.syncDataCloudBatch(syncDataParam);
        return ResultUtil.ok(true);
    }

    /**
     * 单个手动同步
     *
     * @param printPlanId
     * @param thirdRelateId
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "印刷计划合并管理-同步失败-单个手动同步")
    @RequestMapping(value = "/sync_data_cloud", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = EditResult.class)})
    public Result syncDataCloud(@RequestParam(value = "printPlanId") Long printPlanId,
                                @RequestParam(value = "thirdRelateId", required = false) Long thirdRelateId) {
        examPrintPlanService.syncDataCloud(printPlanId, thirdRelateId);
        return ResultUtil.ok(true);
    }
}

