package org.dromara.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.domain.vo.TblRulesVo;
import org.dromara.domain.bo.TblRulesBo;
import org.dromara.service.ITblRulesService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 规则
 *
 * @author Lion Li
 * @date 2024-08-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/rules")
public class TblRulesController extends BaseController {

    private final ITblRulesService tblRulesService;

    /**
     * 查询规则列表
     */
    @SaCheckPermission("system:rules:list")
    @GetMapping("/list")
    public TableDataInfo<TblRulesVo> list(TblRulesBo bo, PageQuery pageQuery) {
        return tblRulesService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出规则列表
     */
    @SaCheckPermission("system:rules:export")
    @Log(title = "规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TblRulesBo bo, HttpServletResponse response) {
        List<TblRulesVo> list = tblRulesService.queryList(bo);
        ExcelUtil.exportExcel(list, "规则", TblRulesVo.class, response);
    }

    /**
     * 获取规则详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:rules:query")
    @GetMapping("/{id}")
    public R<TblRulesVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tblRulesService.queryById(id));
    }

    /**
     * 新增规则
     */
    @SaCheckPermission("system:rules:add")
    @Log(title = "规则", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TblRulesBo bo) {
        return toAjax(tblRulesService.insertByBo(bo));
    }

    /**
     * 修改规则
     */
    @SaCheckPermission("system:rules:edit")
    @Log(title = "规则", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TblRulesBo bo) throws Exception {
        return toAjax(tblRulesService.updateByBo(bo));
    }

    /**
     * 删除规则
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:rules:remove")
    @Log(title = "规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tblRulesService.deleteWithValidByIds(List.of(ids), true));
    }
}
