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.Black;
import com.ruoyi.cai.service.BlackService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
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.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
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-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cai/black")
public class BlackController extends BaseController {

    private final BlackService blackService;

    /**
     * 查询黑名单列表
     */
    @SaCheckPermission("cai:black:list")
    @GetMapping("/list")
    public TableDataInfo<Black> list(Black bo, PageQuery pageQuery) {
        Page<Black> page = blackService.page(pageQuery.build(), Wrappers.lambdaQuery(bo).orderByDesc(Black::getCreateTime));
        return TableDataInfo.build(page);
    }

    /**
     * 获取黑名单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("cai:black:query")
    @GetMapping("/{id}")
    public R<Black> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(blackService.getById(id));
    }

    /**
     * 新增黑名单
     */
    @SaCheckPermission("cai:black:add")
    @Log(title = "黑名单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody Black bo) {
        return toAjax(blackService.addBlack(bo));
    }

    /**
     * 修改黑名单
     */
    @SaCheckPermission("cai:black:edit")
    @Log(title = "黑名单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody Black bo) {
        return toAjax(blackService.updateBlackStatus(bo.getId(),bo.getEnableStatus()));
    }

    /**
     * 删除黑名单
     */
    @SaCheckPermission("cai:black:remove")
    @Log(title = "黑名单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> remove(@NotNull(message = "主键不能为空")
                          @PathVariable Long id) {
        return toAjax(blackService.deleteBlack(id));
    }
}
