package com.ruoyi.web.controller.learn;

import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.web.service.CommService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.learn.domain.LearnKnowledgeTree;
import com.ruoyi.learn.service.ILearnKnowledgeTreeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 知识点树Controller
 * 
 * @author ruoyi
 * @date 2025-09-25
 */
@RestController
@RequestMapping("/learn/knowledgeTree")
@Api(tags = "后台-知识点")
public class LearnKnowledgeTreeController extends BaseController
{
    @Autowired
    private ILearnKnowledgeTreeService learnKnowledgeTreeService;
    @Autowired
    private CommService commService;

    /**
     * 查询知识点树列表
     */
    @PreAuthorize("@ss.hasPermi('learn:knowledgeTree:list')")
    @GetMapping("/list")
    public TableDataInfo list(LearnKnowledgeTree learnKnowledgeTree)
    {
        startPage();
        List<LearnKnowledgeTree> list = learnKnowledgeTreeService.selectLearnKnowledgeTreeList(learnKnowledgeTree);
        return getDataTable(list);
    }

    @GetMapping("/tree")
    @ApiOperation("知识点树")
    public AjaxResult knowledges(LearnKnowledgeTree learnKnowledgeTree)
    {
        List<LearnKnowledgeTree> list = learnKnowledgeTreeService.selectLearnKnowledgeTreeList(learnKnowledgeTree);
        return AjaxResult.success(commService.buildTree(list, null, null));
    }

    /**
     * 导出知识点树列表
     */
    @PreAuthorize("@ss.hasPermi('learn:knowledgeTree:export')")
    @Log(title = "知识点树", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LearnKnowledgeTree learnKnowledgeTree)
    {
        List<LearnKnowledgeTree> list = learnKnowledgeTreeService.selectLearnKnowledgeTreeList(learnKnowledgeTree);
        ExcelUtil<LearnKnowledgeTree> util = new ExcelUtil<LearnKnowledgeTree>(LearnKnowledgeTree.class);
        util.exportExcel(response, list, "知识点树数据");
    }

    /**
     * 获取知识点树详细信息
     */
    @PreAuthorize("@ss.hasPermi('learn:knowledgeTree:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(learnKnowledgeTreeService.selectLearnKnowledgeTreeById(id));
    }

    /**
     * 批量获取知识点树详细信息
     */
    @PreAuthorize("@ss.hasPermi('learn:knowledgeTree:query')")
    @PostMapping("/getKnowledgeByIds")
    @ApiOperation("批量获取知识点树详细信息")
    public AjaxResult getKnowledgeByIds(@RequestBody List<Long> ids)
    {
        if (ids == null || ids.isEmpty()) {
            return AjaxResult.error("知识点ID列表不能为空");
        }
        List<LearnKnowledgeTree> result = learnKnowledgeTreeService.selectLearnKnowledgeTreeByIds(ids);
        return AjaxResult.success(result);
    }

    /**
     * 新增知识点树
     */
    @PreAuthorize("@ss.hasPermi('learn:knowledgeTree:add')")
    @Log(title = "知识点树", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LearnKnowledgeTree learnKnowledgeTree)
    {
        return toAjax(learnKnowledgeTreeService.insertLearnKnowledgeTree(learnKnowledgeTree));
    }

    /**
     * 修改知识点树
     */
    @PreAuthorize("@ss.hasPermi('learn:knowledgeTree:edit')")
    @Log(title = "知识点树", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LearnKnowledgeTree learnKnowledgeTree)
    {
        return toAjax(learnKnowledgeTreeService.updateLearnKnowledgeTree(learnKnowledgeTree));
    }

    /**
     * 删除知识点树
     */
    @PreAuthorize("@ss.hasPermi('learn:knowledgeTree:remove')")
    @Log(title = "知识点树", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(learnKnowledgeTreeService.deleteLearnKnowledgeTreeByIds(ids));
    }
}
