package com.qmth.distributed.print.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.service.ExamTaskService;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.log.OperationTypeEnum;
import com.qmth.teachcloud.common.service.BasicOperationLogService;
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.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: 2022-11-29
 */
@Api(tags = "日志管理controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_BASIC + "/operation_log")
public class BasicOperationLogController {
    @Resource
    private BasicOperationLogService basicOperationLogService;

    @Resource
    private ExamTaskService examTaskService;

    @ApiOperation(value = "日志管理-分页查询")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result findOperationLogPage(@ApiParam(value = "操作类型") @RequestParam(required = false) OperationTypeEnum operationType,
                                       @ApiParam(value = "操作时间（起始）") @RequestParam(required = false) Long startTime,
                                       @ApiParam(value = "操作时间（终止）") @RequestParam(required = false) Long endTime,
                                       @ApiParam(value = "操作人姓名/工号") @RequestParam(required = false) String operatorName,
                                       @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) {
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(basicOperationLogService.findOperationLogPage(operationType, startTime, endTime, operatorName, requestUser, pageNumber, pageSize));
    }

    @ApiOperation(value = "日志管理-试卷、题卡预览接口保存")
    @RequestMapping(value = "/save_preview_log", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result savePreviewLog(@ApiParam(value = "命题任务ID") @RequestParam(value = "examTaskId") Long examTaskId,
                                 @ApiParam(value = "卷型") @RequestParam(required = false) String paperType,
                                 @ApiParam(value = "类型") @RequestParam(required = false) String type) {
        return ResultUtil.ok(examTaskService.savePreviewLog(examTaskId, paperType, type));
    }
}
