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.SenClusterNodesVo;
import org.dromara.domain.bo.SenClusterNodesBo;
import org.dromara.service.ISenClusterNodesService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 集群配置
 *
 * @author luogang
 * @date 2024-09-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/manage/clusterNodes")
public class SenClusterNodesController extends BaseController {

    private final ISenClusterNodesService senClusterNodesService;

    /**
     * 查询集群配置列表
     */
    @SaCheckPermission("manage:clusterNodes:list")
    @GetMapping("/list")
    public TableDataInfo<SenClusterNodesVo> list(SenClusterNodesBo bo, PageQuery pageQuery) {
        return senClusterNodesService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出集群配置列表
     */
    @SaCheckPermission("manage:clusterNodes:export")
    @Log(title = "集群配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SenClusterNodesBo bo, HttpServletResponse response) {
        List<SenClusterNodesVo> list = senClusterNodesService.queryList(bo);
        ExcelUtil.exportExcel(list, "集群配置", SenClusterNodesVo.class, response);
    }

    /**
     * 获取集群配置详细信息
     *
     * @param nodeId 主键
     */
    @SaCheckPermission("manage:clusterNodes:query")
    @GetMapping("/{nodeId}")
    public R<SenClusterNodesVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long nodeId) {
        return R.ok(senClusterNodesService.queryById(nodeId));
    }

    /**
     * 新增集群配置
     */
    @SaCheckPermission("manage:clusterNodes:add")
    @Log(title = "集群配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SenClusterNodesBo bo) {
        return toAjax(senClusterNodesService.insertByBo(bo));
    }

    /**
     * 修改集群配置
     */
    @SaCheckPermission("manage:clusterNodes:edit")
    @Log(title = "集群配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SenClusterNodesBo bo) {
        return toAjax(senClusterNodesService.updateByBo(bo));
    }

    /**
     * 删除集群配置
     *
     * @param nodeIds 主键串
     */
    @SaCheckPermission("manage:clusterNodes:remove")
    @Log(title = "集群配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{nodeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] nodeIds) {
        return toAjax(senClusterNodesService.deleteWithValidByIds(List.of(nodeIds), true));
    }
}
