package com.accounting.controller; import com.accounting.dto.BudgetRequest; import com.accounting.dto.BudgetResponse; import com.accounting.dto.BudgetSettlementResponse; import com.accounting.entity.User; import com.accounting.mapper.UserMapper; import com.accounting.service.BudgetService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.web.bind.annotation.*; import java.time.LocalDate; @Tag(name = "预算管理", description = "预算管理接口") @RestController @RequestMapping("/api/budgets") public class BudgetController { @Autowired private BudgetService budgetService; @Autowired private UserMapper userMapper; @Operation(summary = "获取当前月份的预算信息") @GetMapping public ResponseEntity getBudget(Authentication authentication) { Long userId = getUserId(authentication); LocalDate today = LocalDate.now(); BudgetResponse response = budgetService.getBudgetWithStatistics(userId, today.getYear(), today.getMonthValue()); return ResponseEntity.ok(response); } @Operation(summary = "获取上月预算结算信息") @GetMapping("/settlement") public ResponseEntity getBudgetSettlement(Authentication authentication) { Long userId = getUserId(authentication); BudgetSettlementResponse response = budgetService.getLastMonthSettlement(userId); return ResponseEntity.ok(response); } @Operation(summary = "设置/更新预算") @PutMapping public ResponseEntity setBudget( @RequestBody BudgetRequest request, Authentication authentication) { Long userId = getUserId(authentication); BudgetResponse response = budgetService.setBudget(userId, request); return ResponseEntity.ok(response); } private Long getUserId(Authentication authentication) { UserDetails userDetails = (UserDetails) authentication.getPrincipal(); String username = userDetails.getUsername(); User user = userMapper.selectOne( new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper() .eq(User::getUsername, username) ); return user != null ? user.getId() : null; } }