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.BasicPrintConfigService;
import com.qmth.teachcloud.common.annotation.OperationLogDetail;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.BasicPrintConfig;
import com.qmth.teachcloud.common.enums.log.OperationTypeEnum;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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 2022-02-10
 */
@Api(tags = "印品管理Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_BASIC + "/print_config")
@Validated
public class BasicPrintConfigController {

    @Resource
    private BasicPrintConfigService basicPrintConfigService;

    /**
     * 分页查询
     *
     * @param enable          启用/禁用
     * @param createStartTime 创建开始时间
     * @param createEndTime   创建结束时间
     * @param pageNumber      分页页码
     * @param pageSize        分页数量
     */
    @ApiOperation(value = "分页查询")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Result list(@RequestParam(value = "examId") Long examId,
                       @RequestParam(value = "enable", required = false) Boolean enable,
                       @RequestParam(value = "createStartTime", required = false) Long createStartTime,
                       @RequestParam(value = "createEndTime", required = false) Long createEndTime,
                       @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                       @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        IPage<BasicPrintConfig> printConfigIPage = basicPrintConfigService.listPage(examId, enable, createStartTime, createEndTime, pageNumber, pageSize);
        return ResultUtil.ok(printConfigIPage);
    }

    /**
     * 新增/修改
     *
     * @param basicPrintConfig 参数
     */
    @ApiOperation(value = "新增/修改")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.SAVE)
    public Result save(@RequestBody BasicPrintConfig basicPrintConfig) {
        return ResultUtil.ok(basicPrintConfigService.savePrintConfig(basicPrintConfig));
    }

    /**
     * 启用/禁用
     */
    @ApiOperation(value = "启用/禁用")
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.UPDATE)
    public Result enable(@RequestParam(value = "id", required = false) Long id,
                         @RequestParam(value = "enable", required = false) Boolean enable) {
        boolean isSuccess = basicPrintConfigService.enable(id, enable);
        return ResultUtil.ok(isSuccess);
    }

    /**
     * 启用/禁用
     */
    @ApiOperation(value = "查询orgId印品")
    @RequestMapping(value = "/get_by_exam_id_and_org_id", method = RequestMethod.POST)
    public Result getByOrgId(@RequestParam(value = "examId") Long examId) {
        BasicPrintConfig basicPrintConfig = basicPrintConfigService.getByExamId(examId);
        return ResultUtil.ok(basicPrintConfig);
    }

}

