package org.dromara.inspection.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.transaction.annotation.Transactional;
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.inspection.domain.vo.OpsInspectionNodeVo;
import org.dromara.inspection.domain.bo.OpsInspectionNodeBo;
import org.dromara.inspection.service.IOpsInspectionNodeService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 运维-巡检-通用节点
 * 前端访问路由地址为:/inspection/node
 *
 * @author LionLi
 * @date 2025-09-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/node")
public class OpsInspectionNodeController extends BaseController {

    private final IOpsInspectionNodeService opsInspectionNodeService;

    /**
     * 查询运维-巡检-通用节点列表
     */
    @SaCheckPermission("inspection:node:list")
    @GetMapping("/list")
    public TableDataInfo<OpsInspectionNodeVo> list(OpsInspectionNodeBo bo, PageQuery pageQuery) {
        return opsInspectionNodeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出运维-巡检-通用节点列表
     */
    @SaCheckPermission("inspection:node:export")
    @Log(title = "运维-巡检-通用节点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OpsInspectionNodeBo bo, HttpServletResponse response) {
        List<OpsInspectionNodeVo> list = opsInspectionNodeService.queryList(bo);
        ExcelUtil.exportExcel(list, "运维-巡检-通用节点", OpsInspectionNodeVo.class, response);
    }

    /**
     * 获取运维-巡检-通用节点详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("inspection:node:query")
    @GetMapping("/{id}")
    public R<OpsInspectionNodeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable("id") Long id) {
        return R.ok(opsInspectionNodeService.queryById(id));
    }

    /**
     * 新增运维-巡检-通用节点
     */
    @SaCheckPermission("inspection:node:add")
    @Log(title = "运维-巡检-通用节点", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @Transactional
    public R<String> add(@Validated(AddGroup.class) @RequestBody List<OpsInspectionNodeBo> bo) {
        return opsInspectionNodeService.insertByBo(bo);
    }

    /**
     * 修改运维-巡检-通用节点
     */
    @SaCheckPermission("inspection:node:edit")
    @Log(title = "运维-巡检-通用节点", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody List<OpsInspectionNodeBo> bo) {
        return toAjax(opsInspectionNodeService.updateByBo(bo));
    }

    /**
     * 删除运维-巡检-通用节点
     *
     * @param ids 主键串
     */
    @SaCheckPermission("inspection:node:remove")
    @Log(title = "运维-巡检-通用节点", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("ids") Long[] ids) {
        return toAjax(opsInspectionNodeService.deleteWithValidByIds(List.of(ids), true));
    }
}
