feat(预算功能新增)
1、实现月度预算功能,每个用户每个月可以设置一个预算金额。在首页展示预算相关信息,并提供预算调整功能。每月首次打开应用时,自动结算上月预算并弹窗通知用户,同时开启本月预算。
This commit is contained in:
parent
3df3aea533
commit
2ca71b6982
@ -0,0 +1,67 @@
|
|||||||
|
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<BudgetResponse> 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<BudgetSettlementResponse> getBudgetSettlement(Authentication authentication) {
|
||||||
|
Long userId = getUserId(authentication);
|
||||||
|
BudgetSettlementResponse response = budgetService.getLastMonthSettlement(userId);
|
||||||
|
return ResponseEntity.ok(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "设置/更新预算")
|
||||||
|
@PutMapping
|
||||||
|
public ResponseEntity<BudgetResponse> 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<User>()
|
||||||
|
.eq(User::getUsername, username)
|
||||||
|
);
|
||||||
|
return user != null ? user.getId() : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
15
src/main/java/com/accounting/dto/BudgetRequest.java
Normal file
15
src/main/java/com/accounting/dto/BudgetRequest.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package com.accounting.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class BudgetRequest {
|
||||||
|
private Integer year;
|
||||||
|
|
||||||
|
private Integer month;
|
||||||
|
|
||||||
|
private BigDecimal amount;
|
||||||
|
}
|
||||||
|
|
||||||
23
src/main/java/com/accounting/dto/BudgetResponse.java
Normal file
23
src/main/java/com/accounting/dto/BudgetResponse.java
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package com.accounting.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class BudgetResponse {
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private Integer year;
|
||||||
|
|
||||||
|
private Integer month;
|
||||||
|
|
||||||
|
private BigDecimal amount; // 预算金额
|
||||||
|
|
||||||
|
private BigDecimal usedAmount; // 已用金额(本月支出)
|
||||||
|
|
||||||
|
private BigDecimal remainingAmount; // 剩余预算
|
||||||
|
|
||||||
|
private BigDecimal remainingDaily; // 剩余日均
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
package com.accounting.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class BudgetSettlementResponse {
|
||||||
|
private Integer year; // 年份
|
||||||
|
|
||||||
|
private Integer month; // 月份
|
||||||
|
|
||||||
|
private BigDecimal budgetAmount; // 预算金额
|
||||||
|
|
||||||
|
private BigDecimal actualExpense; // 实际支出
|
||||||
|
|
||||||
|
private Boolean isOverBudget; // 是否超支
|
||||||
|
|
||||||
|
private BigDecimal overAmount; // 超支金额(如果超支)
|
||||||
|
|
||||||
|
private BigDecimal completionRate; // 完成率(实际支出/预算金额,如果预算为0则为null)
|
||||||
|
}
|
||||||
|
|
||||||
29
src/main/java/com/accounting/entity/Budget.java
Normal file
29
src/main/java/com/accounting/entity/Budget.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package com.accounting.entity;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@TableName("budget")
|
||||||
|
public class Budget {
|
||||||
|
@TableId(type = IdType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private Long userId;
|
||||||
|
|
||||||
|
private Integer year;
|
||||||
|
|
||||||
|
private Integer month;
|
||||||
|
|
||||||
|
private BigDecimal amount;
|
||||||
|
|
||||||
|
private LocalDateTime createTime;
|
||||||
|
|
||||||
|
private LocalDateTime updateTime;
|
||||||
|
}
|
||||||
|
|
||||||
10
src/main/java/com/accounting/mapper/BudgetMapper.java
Normal file
10
src/main/java/com/accounting/mapper/BudgetMapper.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package com.accounting.mapper;
|
||||||
|
|
||||||
|
import com.accounting.entity.Budget;
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface BudgetMapper extends BaseMapper<Budget> {
|
||||||
|
}
|
||||||
|
|
||||||
191
src/main/java/com/accounting/service/BudgetService.java
Normal file
191
src/main/java/com/accounting/service/BudgetService.java
Normal file
@ -0,0 +1,191 @@
|
|||||||
|
package com.accounting.service;
|
||||||
|
|
||||||
|
import com.accounting.dto.BudgetRequest;
|
||||||
|
import com.accounting.dto.BudgetResponse;
|
||||||
|
import com.accounting.dto.BudgetSettlementResponse;
|
||||||
|
import com.accounting.dto.StatisticsResponse;
|
||||||
|
import com.accounting.entity.Budget;
|
||||||
|
import com.accounting.mapper.BudgetMapper;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.math.RoundingMode;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class BudgetService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private BudgetMapper budgetMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private StatisticsService statisticsService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取或创建预算(如果不存在则创建默认0)
|
||||||
|
*/
|
||||||
|
@Transactional
|
||||||
|
public Budget getOrCreateBudget(Long userId, int year, int month) {
|
||||||
|
Budget budget = budgetMapper.selectOne(
|
||||||
|
new LambdaQueryWrapper<Budget>()
|
||||||
|
.eq(Budget::getUserId, userId)
|
||||||
|
.eq(Budget::getYear, year)
|
||||||
|
.eq(Budget::getMonth, month)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (budget == null) {
|
||||||
|
budget = new Budget();
|
||||||
|
budget.setUserId(userId);
|
||||||
|
budget.setYear(year);
|
||||||
|
budget.setMonth(month);
|
||||||
|
budget.setAmount(BigDecimal.ZERO);
|
||||||
|
budgetMapper.insert(budget);
|
||||||
|
}
|
||||||
|
|
||||||
|
return budget;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取预算
|
||||||
|
*/
|
||||||
|
public Budget getBudget(Long userId, int year, int month) {
|
||||||
|
return budgetMapper.selectOne(
|
||||||
|
new LambdaQueryWrapper<Budget>()
|
||||||
|
.eq(Budget::getUserId, userId)
|
||||||
|
.eq(Budget::getYear, year)
|
||||||
|
.eq(Budget::getMonth, month)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置/更新预算
|
||||||
|
*/
|
||||||
|
@Transactional
|
||||||
|
public BudgetResponse setBudget(Long userId, BudgetRequest request) {
|
||||||
|
int year = request.getYear();
|
||||||
|
int month = request.getMonth();
|
||||||
|
BigDecimal amount = request.getAmount();
|
||||||
|
|
||||||
|
if (amount == null || amount.compareTo(BigDecimal.ZERO) < 0) {
|
||||||
|
throw new RuntimeException("预算金额必须大于等于0");
|
||||||
|
}
|
||||||
|
|
||||||
|
Budget budget = getOrCreateBudget(userId, year, month);
|
||||||
|
budget.setAmount(amount);
|
||||||
|
budgetMapper.updateById(budget);
|
||||||
|
|
||||||
|
return getBudgetWithStatistics(userId, year, month);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取预算及统计信息
|
||||||
|
*/
|
||||||
|
public BudgetResponse getBudgetWithStatistics(Long userId, int year, int month) {
|
||||||
|
Budget budget = getOrCreateBudget(userId, year, month);
|
||||||
|
|
||||||
|
BudgetResponse response = new BudgetResponse();
|
||||||
|
response.setId(budget.getId());
|
||||||
|
response.setYear(budget.getYear());
|
||||||
|
response.setMonth(budget.getMonth());
|
||||||
|
response.setAmount(budget.getAmount());
|
||||||
|
|
||||||
|
// 获取本月支出统计
|
||||||
|
StatisticsResponse stats = statisticsService.getMonthlyStatistics(userId, year, month);
|
||||||
|
BigDecimal usedAmount = stats.getTotalExpense() != null ? stats.getTotalExpense() : BigDecimal.ZERO;
|
||||||
|
response.setUsedAmount(usedAmount);
|
||||||
|
|
||||||
|
// 计算剩余预算
|
||||||
|
BigDecimal remainingAmount = budget.getAmount().subtract(usedAmount);
|
||||||
|
response.setRemainingAmount(remainingAmount);
|
||||||
|
|
||||||
|
// 计算剩余日均
|
||||||
|
LocalDate today = LocalDate.now();
|
||||||
|
LocalDate monthStart = LocalDate.of(year, month, 1);
|
||||||
|
LocalDate monthEnd = monthStart.withDayOfMonth(monthStart.lengthOfMonth());
|
||||||
|
|
||||||
|
int remainingDays = 0;
|
||||||
|
if (year == today.getYear() && month == today.getMonthValue()) {
|
||||||
|
// 当前月份
|
||||||
|
remainingDays = monthEnd.getDayOfMonth() - today.getDayOfMonth() + 1;
|
||||||
|
if (remainingDays < 0) {
|
||||||
|
remainingDays = 0;
|
||||||
|
}
|
||||||
|
} else if (year < today.getYear() || (year == today.getYear() && month < today.getMonthValue())) {
|
||||||
|
// 过去的月份
|
||||||
|
remainingDays = 0;
|
||||||
|
} else {
|
||||||
|
// 未来的月份
|
||||||
|
remainingDays = monthEnd.getDayOfMonth();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (remainingDays > 0) {
|
||||||
|
BigDecimal remainingDaily = remainingAmount.divide(BigDecimal.valueOf(remainingDays), 2, RoundingMode.HALF_UP);
|
||||||
|
response.setRemainingDaily(remainingDaily);
|
||||||
|
} else {
|
||||||
|
response.setRemainingDaily(BigDecimal.ZERO);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取上月预算结算信息
|
||||||
|
*/
|
||||||
|
public BudgetSettlementResponse getLastMonthSettlement(Long userId) {
|
||||||
|
LocalDate today = LocalDate.now();
|
||||||
|
LocalDate lastMonth = today.minusMonths(1);
|
||||||
|
int year = lastMonth.getYear();
|
||||||
|
int month = lastMonth.getMonthValue();
|
||||||
|
|
||||||
|
Budget budget = getBudget(userId, year, month);
|
||||||
|
BigDecimal budgetAmount = budget != null && budget.getAmount() != null ? budget.getAmount() : BigDecimal.ZERO;
|
||||||
|
|
||||||
|
// 获取上月支出统计
|
||||||
|
StatisticsResponse stats = statisticsService.getMonthlyStatistics(userId, year, month);
|
||||||
|
BigDecimal actualExpense = stats.getTotalExpense() != null ? stats.getTotalExpense() : BigDecimal.ZERO;
|
||||||
|
|
||||||
|
BudgetSettlementResponse response = new BudgetSettlementResponse();
|
||||||
|
response.setYear(year);
|
||||||
|
response.setMonth(month);
|
||||||
|
response.setBudgetAmount(budgetAmount);
|
||||||
|
response.setActualExpense(actualExpense);
|
||||||
|
|
||||||
|
// 判断是否超支
|
||||||
|
boolean isOverBudget = actualExpense.compareTo(budgetAmount) > 0;
|
||||||
|
response.setIsOverBudget(isOverBudget);
|
||||||
|
|
||||||
|
if (isOverBudget) {
|
||||||
|
BigDecimal overAmount = actualExpense.subtract(budgetAmount);
|
||||||
|
response.setOverAmount(overAmount);
|
||||||
|
} else {
|
||||||
|
response.setOverAmount(BigDecimal.ZERO);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 计算完成率
|
||||||
|
if (budgetAmount.compareTo(BigDecimal.ZERO) > 0) {
|
||||||
|
BigDecimal completionRate = actualExpense.divide(budgetAmount, 4, RoundingMode.HALF_UP)
|
||||||
|
.multiply(BigDecimal.valueOf(100));
|
||||||
|
response.setCompletionRate(completionRate);
|
||||||
|
} else {
|
||||||
|
response.setCompletionRate(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查并创建本月预算(如果不存在则创建默认0)
|
||||||
|
*/
|
||||||
|
@Transactional
|
||||||
|
public void checkAndCreateCurrentMonthBudget(Long userId) {
|
||||||
|
LocalDate today = LocalDate.now();
|
||||||
|
int year = today.getYear();
|
||||||
|
int month = today.getMonthValue();
|
||||||
|
|
||||||
|
getOrCreateBudget(userId, year, month);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -67,6 +67,22 @@ CREATE TABLE IF NOT EXISTS `account` (
|
|||||||
UNIQUE KEY `uk_user_account` (`user_id`) COMMENT '每个用户只有一个账户'
|
UNIQUE KEY `uk_user_account` (`user_id`) COMMENT '每个用户只有一个账户'
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='账户表';
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='账户表';
|
||||||
|
|
||||||
|
-- 预算表
|
||||||
|
CREATE TABLE IF NOT EXISTS `budget` (
|
||||||
|
`id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '预算ID',
|
||||||
|
`user_id` BIGINT NOT NULL COMMENT '用户ID',
|
||||||
|
`year` INT NOT NULL COMMENT '年份',
|
||||||
|
`month` INT NOT NULL COMMENT '月份(1-12)',
|
||||||
|
`amount` DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT '预算金额',
|
||||||
|
`create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||||||
|
`update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
INDEX `idx_user_id` (`user_id`),
|
||||||
|
INDEX `idx_year_month` (`year`, `month`),
|
||||||
|
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE,
|
||||||
|
UNIQUE KEY `uk_user_year_month` (`user_id`, `year`, `month`) COMMENT '每个用户每个月只有一个预算'
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='预算表';
|
||||||
|
|
||||||
-- OCR记录表
|
-- OCR记录表
|
||||||
CREATE TABLE IF NOT EXISTS `ocr_record` (
|
CREATE TABLE IF NOT EXISTS `ocr_record` (
|
||||||
`id` BIGINT NOT NULL AUTO_INCREMENT COMMENT 'OCR记录ID',
|
`id` BIGINT NOT NULL AUTO_INCREMENT COMMENT 'OCR记录ID',
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user