package org.dromara.knowledge.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.knowledge.domain.bo.KnowledgeSchemaFieldBo;
import org.dromara.knowledge.domain.vo.KnowledgeSchemaFieldVo;
import org.dromara.knowledge.service.KnowledgeSchemaFieldService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 知识库模式字段控制器
 *
 * @author pengles
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/knowledge/schema/field")
public class KnowledgeSchemaFieldController extends BaseController {

    private final KnowledgeSchemaFieldService knowledgeSchemaFieldService;

    /**
     * 查询知识库模式字段列表
     */
    @SaCheckPermission("knowledge:schema:field:list")
    @GetMapping("/list")
    public TableDataInfo<KnowledgeSchemaFieldVo> list(KnowledgeSchemaFieldBo bo, PageQuery pageQuery) {
        return knowledgeSchemaFieldService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出知识库模式字段列表
     */
    @SaCheckPermission("knowledge:schema:field:export")
    @Log(title = "知识库模式字段", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KnowledgeSchemaFieldBo bo, HttpServletResponse response) {
        List<KnowledgeSchemaFieldVo> list = knowledgeSchemaFieldService.queryList(bo);
        ExcelUtil.exportExcel(list, "知识库模式字段数据", KnowledgeSchemaFieldVo.class, response);
    }

    /**
     * 获取知识库模式字段详细信息
     *
     * @param fieldId 字段ID
     */
    @SaCheckPermission("knowledge:schema:field:query")
    @GetMapping("/{fieldId}")
    public R<KnowledgeSchemaFieldVo> getInfo(@PathVariable Long fieldId) {
        return R.ok(knowledgeSchemaFieldService.queryById(fieldId));
    }

    /**
     * 新增知识库模式字段
     */
    @SaCheckPermission("knowledge:schema:field:add")
    @Log(title = "知识库模式字段", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody KnowledgeSchemaFieldBo bo) {
        return toAjax(knowledgeSchemaFieldService.insertByBo(bo));
    }

    /**
     * 修改知识库模式字段
     */
    @SaCheckPermission("knowledge:schema:field:edit")
    @Log(title = "知识库模式字段", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody KnowledgeSchemaFieldBo bo) {
        return toAjax(knowledgeSchemaFieldService.updateByBo(bo));
    }

    /**
     * 删除知识库模式字段
     *
     * @param fieldIds 字段ID串
     */
    @SaCheckPermission("knowledge:schema:field:remove")
    @Log(title = "知识库模式字段", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fieldIds}")
    public R<Void> remove(@PathVariable Long[] fieldIds) {
        return toAjax(knowledgeSchemaFieldService.deleteWithValidByIds(List.of(fieldIds), true));
    }

    /**
     * 根据ID查询字段信息
     *
     * @param id ID
     */
    @SaCheckPermission("knowledge:schema:field:query")
    @GetMapping("/id/{id}")
    public R<KnowledgeSchemaFieldVo> getById(@PathVariable Long id) {
        return R.ok(knowledgeSchemaFieldService.queryById(id));
    }

    /**
     * 根据模式ID查询字段列表
     *
     * @param schemaId 模式ID
     */
    @SaCheckPermission("knowledge:schema:field:query")
    @GetMapping("/schema/{schemaId}")
    public R<List<KnowledgeSchemaFieldVo>> getBySchemaId(@PathVariable Long schemaId) {
        return R.ok(knowledgeSchemaFieldService.queryListBySchemaId(schemaId));
    }

    /**
     * 根据编码查询字段信息
     *
     * @param code 编码
     */
    @SaCheckPermission("knowledge:schema:field:query")
    @GetMapping("/code/{code}")
    public R<KnowledgeSchemaFieldVo> getByCode(@PathVariable String code) {
        return R.ok(knowledgeSchemaFieldService.queryByCode(code));
    }

    /**
     * 根据模式ID和编码查询字段信息
     *
     * @param schemaId 模式ID
     * @param code     编码
     */
    @SaCheckPermission("knowledge:schema:field:query")
    @GetMapping("/schema/{schemaId}/code/{code}")
    public R<KnowledgeSchemaFieldVo> getBySchemaIdAndCode(@PathVariable Long schemaId, @PathVariable String code) {
        return R.ok(knowledgeSchemaFieldService.queryBySchemaIdAndCode(schemaId, code));
    }
}