package com.ruoyi.cai.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.cai.domain.Rank;
import com.ruoyi.cai.dto.admin.vo.RankAdminVo;
import com.ruoyi.cai.service.RankService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 榜单
 *
 * @author 77
 * @date 2024-01-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cai/rank")
public class RankController extends BaseController {

    private final RankService rankService;

    /**
     * 查询榜单列表
     */
    @SaCheckPermission("cai:rank:list")
    @GetMapping("/list")
    public TableDataInfo<RankAdminVo> list(RankAdminVo bo, PageQuery pageQuery) {
        Page<RankAdminVo> page = rankService.pageAdmin(pageQuery, bo);
        return TableDataInfo.build(page);
    }

    /**
     * 获取榜单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("cai:rank:query")
    @GetMapping("/{id}")
    public R<Rank> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Integer id) {
        return R.ok(rankService.getById(id));
    }

    /**
     * 删除榜单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("cai:rank:remove")
    @Log(title = "榜单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Integer[] ids) {
        return toAjax(rankService.removeBatchByIds(Arrays.asList(ids)));
    }
}
