AI-accounting-soft/src/main/java/com/accounting/service/BillService.java
ni ziyi 8784556b6a fix(ocr批量入库)
1、新增ocr识别批量入库功能
2025-12-25 14:55:40 +08:00

205 lines
7.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.accounting.service;
import com.accounting.dto.BillRequest;
import com.accounting.dto.BillResponse;
import com.accounting.dto.BatchBillRequest;
import com.accounting.entity.Account;
import com.accounting.entity.Bill;
import com.accounting.entity.Category;
import com.accounting.mapper.BillMapper;
import com.accounting.mapper.CategoryMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class BillService {
@Autowired
private BillMapper billMapper;
@Autowired
private CategoryMapper categoryMapper;
@Autowired
private AccountService accountService;
@Transactional
public BillResponse createBill(BillRequest request, Long userId) {
// 验证分类是否存在
Category category = categoryMapper.selectById(request.getCategoryId());
if (category == null) {
throw new RuntimeException("分类不存在");
}
// 获取或创建账户
Account account = accountService.getOrCreateAccount(userId);
// 创建账单
Bill bill = new Bill();
bill.setUserId(userId);
bill.setAccountId(account.getId()); // 自动关联账户
bill.setCategoryId(request.getCategoryId());
bill.setAmount(request.getAmount());
bill.setDescription(request.getDescription());
bill.setBillDate(request.getBillDate() != null ? request.getBillDate() : LocalDate.now());
bill.setImageUrl(request.getImageUrl());
bill.setType(request.getType()); // 设置账单类型
billMapper.insert(bill);
return convertToResponse(bill, category);
}
@Transactional
public List<BillResponse> createBills(BatchBillRequest request, Long userId) {
List<BillResponse> responses = new ArrayList<>();
// 获取或创建账户
Account account = accountService.getOrCreateAccount(userId);
for (BillRequest billRequest : request.getBills()) {
// 验证分类是否存在
Category category = categoryMapper.selectById(billRequest.getCategoryId());
if (category == null) {
throw new RuntimeException("分类不存在分类ID: " + billRequest.getCategoryId());
}
// 创建账单
Bill bill = new Bill();
bill.setUserId(userId);
bill.setAccountId(account.getId()); // 自动关联账户
bill.setCategoryId(billRequest.getCategoryId());
bill.setAmount(billRequest.getAmount());
bill.setDescription(billRequest.getDescription());
bill.setBillDate(billRequest.getBillDate() != null ? billRequest.getBillDate() : LocalDate.now());
bill.setImageUrl(billRequest.getImageUrl());
bill.setType(billRequest.getType()); // 设置账单类型
billMapper.insert(bill);
responses.add(convertToResponse(bill, category));
}
return responses;
}
@Transactional
public BillResponse updateBill(Long id, BillRequest request, Long userId) {
// 验证账单是否存在且属于当前用户
Bill bill = billMapper.selectOne(
new LambdaQueryWrapper<Bill>()
.eq(Bill::getId, id)
.eq(Bill::getUserId, userId)
);
if (bill == null) {
throw new RuntimeException("账单不存在或无权限");
}
// 验证分类是否存在
Category category = categoryMapper.selectById(request.getCategoryId());
if (category == null) {
throw new RuntimeException("分类不存在");
}
// 如果账单没有关联账户,则自动关联
if (bill.getAccountId() == null) {
Account account = accountService.getOrCreateAccount(userId);
bill.setAccountId(account.getId());
}
// 更新账单
bill.setCategoryId(request.getCategoryId());
bill.setAmount(request.getAmount());
bill.setDescription(request.getDescription());
if (request.getBillDate() != null) {
bill.setBillDate(request.getBillDate());
}
if (request.getImageUrl() != null) {
bill.setImageUrl(request.getImageUrl());
}
bill.setType(request.getType()); // 更新账单类型
billMapper.updateById(bill);
return convertToResponse(bill, category);
}
@Transactional
public void deleteBill(Long id, Long userId) {
Bill bill = billMapper.selectOne(
new LambdaQueryWrapper<Bill>()
.eq(Bill::getId, id)
.eq(Bill::getUserId, userId)
);
if (bill == null) {
throw new RuntimeException("账单不存在或无权限");
}
billMapper.deleteById(id);
}
public BillResponse getBill(Long id, Long userId) {
Bill bill = billMapper.selectOne(
new LambdaQueryWrapper<Bill>()
.eq(Bill::getId, id)
.eq(Bill::getUserId, userId)
);
if (bill == null) {
throw new RuntimeException("账单不存在或无权限");
}
Category category = categoryMapper.selectById(bill.getCategoryId());
return convertToResponse(bill, category);
}
public List<BillResponse> getBills(Long userId, LocalDate startDate, LocalDate endDate) {
LambdaQueryWrapper<Bill> wrapper = new LambdaQueryWrapper<Bill>()
.eq(Bill::getUserId, userId)
.orderByDesc(Bill::getBillDate)
.orderByDesc(Bill::getCreateTime);
if (startDate != null) {
wrapper.ge(Bill::getBillDate, startDate);
}
if (endDate != null) {
wrapper.le(Bill::getBillDate, endDate);
}
List<Bill> bills = billMapper.selectList(wrapper);
return bills.stream().map(bill -> {
Category category = categoryMapper.selectById(bill.getCategoryId());
return convertToResponse(bill, category);
}).collect(Collectors.toList());
}
private BillResponse convertToResponse(Bill bill, Category category) {
BillResponse response = new BillResponse();
BeanUtils.copyProperties(bill, response);
if (category != null) {
response.setCategoryName(category.getName());
response.setCategoryIcon(category.getIcon());
// 如果账单有自己的类型,则使用账单的类型,否则使用分类的类型
if (response.getType() == null) {
response.setType(category.getType());
}
}
return response;
}
}