package com.example.controller;

import com.example.common.Result;
import com.example.entity.ResourceCategory;
import com.example.service.ResourceCategoryService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资源分类控制器
 */
@RestController
@RequestMapping("/resourceCategory")
public class ResourceCategoryController {

    @Resource
    private ResourceCategoryService resourceCategoryService;
    
    /**
     * 获取所有分类
     */
    @GetMapping
    public Result findAll() {
        List<ResourceCategory> list = resourceCategoryService.findAll();
        return Result.success(list);
    }
    
    /**
     * 根据父分类ID获取子分类
     */
    @GetMapping("/parent/{parentId}")
    public Result findByParentId(@PathVariable Integer parentId) {
        List<ResourceCategory> list = resourceCategoryService.findByParentId(parentId);
        return Result.success(list);
    }
    
    /**
     * 根据ID获取分类
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        ResourceCategory category = resourceCategoryService.findById(id);
        return Result.success(category);
    }
    
    /**
     * 新增分类
     */
    @PostMapping
    public Result add(@RequestBody ResourceCategory category) {
        resourceCategoryService.add(category);
        return Result.success();
    }
    
    /**
     * 更新分类
     */
    @PutMapping
    public Result update(@RequestBody ResourceCategory category) {
        resourceCategoryService.update(category);
        return Result.success();
    }
    
    /**
     * 删除分类
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        resourceCategoryService.delete(id);
        return Result.success();
    }
    
    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "10") Integer pageSize,
                            ResourceCategory category) {
        PageInfo<ResourceCategory> pageInfo = resourceCategoryService.selectPage(category, pageNum, pageSize);
        return Result.success(pageInfo);
    }
} 