package com.guo.learningprogresstracker.controller; import com.guo.learningprogresstracker.dto.AiUsageBalance; import com.guo.learningprogresstracker.dto.AiUsageBreakdownItem; import com.guo.learningprogresstracker.dto.AiUsagePermission; import com.guo.learningprogresstracker.dto.AiUsageSeriesPoint; import com.guo.learningprogresstracker.dto.AiUsageSummary; import com.guo.learningprogresstracker.dto.request.AiUsageQuery; import com.guo.learningprogresstracker.entity.CommonResult; import com.guo.learningprogresstracker.exception.ErrorParameterException; import com.guo.learningprogresstracker.service.AiUsageService; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * 「AI 用量统计」面板查询接口(契约 2)。 * *

用量记录是系统级数据,只有白名单用户能看:{@code permission} 用 {@code allowed=false} * 让前端静默隐藏入口,其余接口直接拒绝,避免入口藏起来了、接口却谁都能拉。

*/ @RestController @RequestMapping("/ai/usage") @RequiredArgsConstructor public class AiUsageController { private static final String NO_PERMISSION_MESSAGE = "没有查看权限"; private final AiUsageService aiUsageService; /** 当前用户能否查看用量面板;非白名单返回 200 + allowed=false */ @GetMapping("/permission") public CommonResult permission() { return CommonResult.success(new AiUsagePermission(aiUsageService.isAdmin())); } @GetMapping("/summary") public CommonResult summary(AiUsageQuery query) { if (!aiUsageService.isAdmin()) { return CommonResult.error(NO_PERMISSION_MESSAGE); } return CommonResult.success(aiUsageService.summary(query)); } @GetMapping("/series") public CommonResult> series(AiUsageQuery query) throws ErrorParameterException { if (!aiUsageService.isAdmin()) { return CommonResult.error(NO_PERMISSION_MESSAGE); } return CommonResult.success(aiUsageService.series(query)); } @GetMapping("/breakdown") public CommonResult> breakdown(AiUsageQuery query) throws ErrorParameterException { if (!aiUsageService.isAdmin()) { return CommonResult.error(NO_PERMISSION_MESSAGE); } return CommonResult.success(aiUsageService.breakdown(query)); } /** 上游余额;lpt-ai 未配置或查询失败时返回 {@code configured=false},不算错误 */ @GetMapping("/balance") public CommonResult balance() { if (!aiUsageService.isAdmin()) { return CommonResult.error(NO_PERMISSION_MESSAGE); } return CommonResult.success(aiUsageService.balance()); } }