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.TblRulesNodeVo;
import org.dromara.domain.bo.TblRulesNodeBo;
import org.dromara.service.ITblRulesNodeService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 规则节点
 *
 * @author Lion Li
 * @date 2024-08-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/rulesNode")
public class TblRulesNodeController extends BaseController {

    private final ITblRulesNodeService tblRulesNodeService;

    /**
     * 查询规则节点列表
     */
    @SaCheckPermission("system:rulesNode:list")
    @GetMapping("/list")
    public TableDataInfo<TblRulesNodeVo> list(TblRulesNodeBo bo, PageQuery pageQuery) {
        return tblRulesNodeService.queryPageList(bo, pageQuery);
    }

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

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

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

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

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