FeatureController.java 4.6 KB
package com.aigeo.ai.controller;

import com.aigeo.ai.entity.Feature;
import com.aigeo.ai.service.FeatureService;
import com.aigeo.common.Result;
import com.aigeo.common.ResultPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * AI功能模块控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/features")
@Tag(name = "AI功能管理", description = "AI功能模块相关接口")
public class FeatureController {
    
    @Autowired
    private FeatureService featureService;
    
    @PostMapping
    @Operation(summary = "创建AI功能", description = "创建新的AI功能")
    public Result<Feature> createFeature(@RequestBody Feature feature) {
        try {
            Feature savedFeature = featureService.save(feature);
            return Result.success("功能创建成功", savedFeature);
        } catch (Exception e) {
            log.error("创建AI功能失败", e);
            return Result.error("功能创建失败");
        }
    }
    
    @GetMapping("/{id}")
    @Operation(summary = "获取AI功能详情", description = "根据ID获取AI功能详情")
    public Result<Feature> getFeatureById(@PathVariable Integer id) {
        try {
            return featureService.findById(id)
                    .map(feature -> Result.success("查询成功", feature))
                    .orElse(Result.error("功能不存在"));
        } catch (Exception e) {
            log.error("获取AI功能详情失败, id: {}", id, e);
            return Result.error("查询失败");
        }
    }
    
    @GetMapping
    @Operation(summary = "获取AI功能列表", description = "获取所有AI功能列表")
    public Result<List<Feature>> getAllFeatures() {
        try {
            List<Feature> features = featureService.findAll();
            return Result.success("查询成功", features);
        } catch (Exception e) {
            log.error("获取AI功能列表失败", e);
            return Result.error("查询失败");
        }
    }
    
    @GetMapping("/active")
    @Operation(summary = "获取启用的AI功能列表", description = "获取所有启用的AI功能列表")
    public Result<List<Feature>> getActiveFeatures() {
        try {
            List<Feature> features = featureService.findActiveFeatures();
            return Result.success("查询成功", features);
        } catch (Exception e) {
            log.error("获取启用的AI功能列表失败", e);
            return Result.error("查询失败");
        }
    }
    
    @GetMapping("/category/{category}")
    @Operation(summary = "根据分类获取AI功能列表", description = "根据分类获取AI功能列表")
    public Result<List<Feature>> getFeaturesByCategory(@PathVariable String category) {
        try {
            List<Feature> features = featureService.findByCategory(category);
            return Result.success("查询成功", features);
        } catch (Exception e) {
            log.error("根据分类获取AI功能列表失败, category: {}", category, e);
            return Result.error("查询失败");
        }
    }
    
    @PutMapping("/{id}")
    @Operation(summary = "更新AI功能", description = "更新AI功能信息")
    public Result<Feature> updateFeature(@PathVariable Integer id, @RequestBody Feature feature) {
        try {
            if (!featureService.findById(id).isPresent()) {
                return Result.error("功能不存在");
            }
            feature.setId(id);
            Feature updatedFeature = featureService.save(feature);
            return Result.success("功能更新成功", updatedFeature);
        } catch (Exception e) {
            log.error("更新AI功能失败, id: {}", id, e);
            return Result.error("功能更新失败");
        }
    }
    
    @DeleteMapping("/{id}")
    @Operation(summary = "删除AI功能", description = "删除指定ID的AI功能")
    public Result<String> deleteFeature(@PathVariable Integer id) {
        try {
            if (!featureService.findById(id).isPresent()) {
                return Result.error("功能不存在");
            }
            featureService.deleteById(id);
            return Result.success("功能删除成功");
        } catch (Exception e) {
            log.error("删除AI功能失败, id: {}", id, e);
            return Result.error("功能删除失败");
        }
    }
}