package com.aegis.web.controller.knowledge;

import com.aegis.common.annotation.Log;
import com.aegis.common.core.controller.BaseController;
import com.aegis.common.core.domain.AjaxResult;
import com.aegis.common.enums.BusinessType;
import com.aegis.knowledge.domain.KnowledgeCategory;
import com.aegis.knowledge.service.IKnowledgeCategoryService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 知识库分类管理 Controller
 */
@RestController
@RequestMapping("/api/knowledge/categories")
public class KnowledgeCategoryController extends BaseController {

    @Autowired
    private IKnowledgeCategoryService categoryService;

    /**
     * 查询分类列表（树形结构）
     */
    @PreAuthorize("@ss.hasPermi('knowledge:category:list')")
    @GetMapping
    public AjaxResult list() {
        List<KnowledgeCategory> list = categoryService.listTree();
        return success(list);
    }

    /**
     * 根据父分类ID查询子分类
     */
    @PreAuthorize("@ss.hasPermi('knowledge:category:list')")
    @GetMapping("/parent/{parentId}")
    public AjaxResult listByParentId(@PathVariable Long parentId) {
        List<KnowledgeCategory> list = categoryService.listByParentId(parentId);
        return success(list);
    }

    /**
     * 获取分类详情
     */
    @PreAuthorize("@ss.hasPermi('knowledge:category:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        KnowledgeCategory category = categoryService.getById(id);
        if (category == null || "2".equals(category.getDelFlag())) {
            return error("分类不存在或已删除");
        }
        return success(category);
    }

    /**
     * 新增分类
     */
    @PreAuthorize("@ss.hasPermi('knowledge:category:add')")
    @Log(title = "知识库分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KnowledgeCategory category) {
        return toAjax(categoryService.save(category));
    }

    /**
     * 修改分类
     */
    @PreAuthorize("@ss.hasPermi('knowledge:category:edit')")
    @Log(title = "知识库分类管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    public AjaxResult edit(@PathVariable Long id, @RequestBody KnowledgeCategory category) {
        category.setId(id);
        return toAjax(categoryService.updateById(category));
    }

    /**
     * 删除分类
     */
    @PreAuthorize("@ss.hasPermi('knowledge:category:remove')")
    @Log(title = "知识库分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        KnowledgeCategory category = categoryService.getById(id);
        if (category == null || "2".equals(category.getDelFlag())) {
            return error("分类不存在或已删除");
        }
        // 检查是否有子分类
        List<KnowledgeCategory> children = categoryService.listByParentId(id);
        if (children != null && !children.isEmpty()) {
            return error("该分类下存在子分类，无法删除");
        }
        // 逻辑删除
        category.setDelFlag("2");
        return toAjax(categoryService.updateById(category));
    }
}

