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.bean.dto.BasicPrintConfigDto;
import com.qmth.teachcloud.common.entity.BasicPrintConfig;
import com.qmth.distributed.print.business.service.BasicPrintConfigService;
import com.qmth.teachcloud.common.contant.SystemConstant;
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;
import java.util.List;

/**
 * <p>
 * 印品配置 前端控制器
 * </p>
 *
 * @author xf
 * @since 2022-02-10
 */
@Api(tags = "印品配置Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${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", required = false) 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<BasicPrintConfigDto> printConfigDtoIPage = basicPrintConfigService.listPage(examId, enable, createStartTime, createEndTime, pageNumber, pageSize);
        return ResultUtil.ok(printConfigDtoIPage);
    }

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

    /**
     * 启用/禁用
     *
     */
    @ApiOperation(value = "启用/禁用")
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    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);
    }

    /**
     *
     * @param id 印品配置ID
     */
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result delete(@RequestParam(value = "id", required = false) Long id) {
        boolean isSuccess = basicPrintConfigService.delete(id);
        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,
                             @RequestParam(value = "orgId") Long orgId) {
        BasicPrintConfig basicPrintConfig = basicPrintConfigService.getByExamIdAndOrgId(examId, orgId);
        return ResultUtil.ok(basicPrintConfig);
    }

    /**
     *  查询考试下设置印品的机构集合
     * @param examId 考试ID
     */
    @ApiOperation(value = "查询考试下设置印品的机构集合")
    @RequestMapping(value = "/list_org_id_by_exam_id", method = RequestMethod.POST)
    public Result listOrgIdsByExamId(@RequestParam(value = "id", required = false) Long id,
                             @RequestParam(value = "examId") Long examId) {
        List<String> orgIds = basicPrintConfigService.listOrgIdsByExamId(id, examId);
        return ResultUtil.ok(orgIds);
    }
}

