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.ReportCate;
import com.ruoyi.cai.service.ReportCateService;
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 2023-12-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cai/reportCate")
public class ReportCateController extends BaseController {

    private final ReportCateService reportCateService;

    /**
     * 查询举报分类列表
     */
    @SaCheckPermission("cai:reportCate:list")
    @GetMapping("/list")
    public TableDataInfo<ReportCate> list(ReportCate bo, PageQuery pageQuery) {
        Page<ReportCate> page = reportCateService.page(pageQuery.build(), Wrappers.lambdaQuery(bo));
        return TableDataInfo.build(page);
    }


    /**
     * 获取举报分类详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("cai:reportCate:query")
    @GetMapping("/{id}")
    public R<ReportCate> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Integer id) {
        return R.ok(reportCateService.getById(id));
    }

    /**
     * 新增举报分类
     */
    @SaCheckPermission("cai:reportCate:add")
    @Log(title = "举报分类", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ReportCate bo) {
        return toAjax(reportCateService.save(bo));
    }

    /**
     * 修改举报分类
     */
    @SaCheckPermission("cai:reportCate:edit")
    @Log(title = "举报分类", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ReportCate bo) {
        return toAjax(reportCateService.updateById(bo));
    }

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