package com.emoon.knowledge.controller;

import java.util.List;

import com.emoon.common.core.domain.R;
import com.emoon.common.core.validate.validate.AddGroup;
import com.emoon.common.core.validate.validate.EditGroup;
import com.emoon.common.excel.utils.ExcelUtil;
import com.emoon.common.idempotent.annotation.RepeatSubmit;
import com.emoon.common.log.annotation.Log;
import com.emoon.common.log.enums.BusinessType;
import com.emoon.common.mybatis.core.page.PageQuery;
import com.emoon.common.mybatis.core.page.TableDataInfo;
import com.emoon.common.web.core.BaseController;
import com.emoon.knowledge.domain.bo.ThinkApiBo;
import com.emoon.knowledge.domain.vo.ThinkApiVo;
import com.emoon.knowledge.service.IThinkApiService;
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;


/**
 * API配置管理
 *
 * @author wangkang
 * @date 2025-11-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/apiConfig")
public class ThinkApiController extends BaseController {

    private final IThinkApiService thinkApiService;

    /**
     * 查询API配置列表
     */
    @SaCheckPermission("system:apiConfig:list")
    @GetMapping("/list")
    public TableDataInfo<ThinkApiVo> list(ThinkApiBo bo, PageQuery pageQuery) {
        return thinkApiService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出API配置列表
     */
    @SaCheckPermission("system:apiConfig:export")
    @Log(title = "API配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ThinkApiBo bo, HttpServletResponse response) {
        List<ThinkApiVo> list = thinkApiService.queryList(bo);
        ExcelUtil.exportExcel(list, "API配置", ThinkApiVo.class, response);
    }

    /**
     * 获取API配置详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:apiConfig:query")
    @GetMapping("/{id}")
    public R<ThinkApiVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(thinkApiService.queryById(id));
    }

    /**
     * 新增API配置
     */
    @SaCheckPermission("system:apiConfig:add")
    @Log(title = "API配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ThinkApiBo bo) {
        return toAjax(thinkApiService.insertByBo(bo));
    }

    /**
     * 修改API配置
     */
    @SaCheckPermission("system:apiConfig:edit")
    @Log(title = "API配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ThinkApiBo bo) {
        return toAjax(thinkApiService.updateByBo(bo));
    }

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



