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.entity.BasicCardRule;
import com.qmth.distributed.print.business.service.BasicCardRuleService;
import com.qmth.teachcloud.common.annotation.OperationLogDetail;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
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;
import java.util.List;

/**
 * <p>
 * 题卡规则 前端控制器
 * </p>
 *
 * @author xf
 * @since 2021-03-23
 */
@Api(tags = "题卡规则Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_BASIC + "/card_rule")
@Validated
public class BasicCardRuleController {

    @Resource
    private BasicCardRuleService basicCardRuleService;

    /**
     * 查询
     *
     * @param enable
     * @param name
     * @param createStartTime
     * @param createEndTime
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result list(@RequestParam(value = "enable", required = false) Boolean enable,
                       @RequestParam(value = "name", required = false) String name,
                       @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<BasicCardRule> cardRuleDtoIPage = basicCardRuleService.list(enable, name, createStartTime, createEndTime, pageNumber, pageSize);
        return ResultUtil.ok(cardRuleDtoIPage);
    }

    /**
     * 模糊查询
     *
     * @param param
     * @return
     */
    @ApiOperation(value = "模糊查询")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public Result list(@RequestParam(value = "param", required = false) String param) {
        List<BasicCardRule> cardRules = basicCardRuleService.list(param);
        return ResultUtil.ok(cardRules);
    }

    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "查询详情")
    @RequestMapping(value = "/get_one", method = RequestMethod.POST)
    public Result getOne(@RequestParam(value = "id", required = false) Long id) {
        if (id == null) {
            throw ExceptionResultEnum.ERROR.exception("传参有误，题卡规则ID为空");
        }
        return ResultUtil.ok(basicCardRuleService.getById(id));
    }

    /**
     * 新增/修改
     *
     * @param cardRule
     * @return
     */
    @ApiOperation(value = "新增/修改")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.SAVE, detail = "新增/修改操作，题卡规则ID：{{cardRule.id}}、题卡规则名称：{{cardRule.name}}")
    public Result save(@RequestBody BasicCardRule cardRule) {
        return ResultUtil.ok(basicCardRuleService.saveCardRule(cardRule));
    }

    /**
     * 删除
     *
     * @param cardRule
     * @return
     */
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.DELETE, detail = "删除操作，题卡规则ID：{{cardRule.id}}")
    public Result delete(@RequestBody BasicCardRule cardRule) {
        return ResultUtil.ok(basicCardRuleService.delete(cardRule));
    }

    /**
     * 启用/禁用
     */
    @ApiOperation(value = "启用/禁用")
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.ENABLE, detail = "启用/禁用操作，题卡规则ID：{{basicCardRule.id}}、启用/状态：{{basicCardRule.enable}}")
    public Result enable(@RequestBody BasicCardRule basicCardRule) {
        return ResultUtil.ok(basicCardRuleService.enable(basicCardRule.getId(), basicCardRule.getEnable()));
    }
}

