Compare commits
2 Commits
910fec8e65
...
9641dcaf9a
| Author | SHA1 | Date | |
|---|---|---|---|
| 9641dcaf9a | |||
| 8784556b6a |
@ -2,6 +2,7 @@ package com.accounting.controller;
|
|||||||
|
|
||||||
import com.accounting.dto.BillRequest;
|
import com.accounting.dto.BillRequest;
|
||||||
import com.accounting.dto.BillResponse;
|
import com.accounting.dto.BillResponse;
|
||||||
|
import com.accounting.dto.BatchBillRequest;
|
||||||
import com.accounting.entity.User;
|
import com.accounting.entity.User;
|
||||||
import com.accounting.mapper.UserMapper;
|
import com.accounting.mapper.UserMapper;
|
||||||
import com.accounting.service.BillService;
|
import com.accounting.service.BillService;
|
||||||
@ -37,6 +38,14 @@ public class BillController {
|
|||||||
return ResponseEntity.ok(response);
|
return ResponseEntity.ok(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "批量创建账单")
|
||||||
|
@PostMapping("/batch")
|
||||||
|
public ResponseEntity<List<BillResponse>> createBills(@Valid @RequestBody BatchBillRequest request, Authentication authentication) {
|
||||||
|
Long userId = getUserId(authentication);
|
||||||
|
List<BillResponse> responses = billService.createBills(request, userId);
|
||||||
|
return ResponseEntity.ok(responses);
|
||||||
|
}
|
||||||
|
|
||||||
@Operation(summary = "更新账单")
|
@Operation(summary = "更新账单")
|
||||||
@PutMapping("/{id}")
|
@PutMapping("/{id}")
|
||||||
public ResponseEntity<BillResponse> updateBill(@PathVariable Long id, @Valid @RequestBody BillRequest request, Authentication authentication) {
|
public ResponseEntity<BillResponse> updateBill(@PathVariable Long id, @Valid @RequestBody BillRequest request, Authentication authentication) {
|
||||||
|
|||||||
19
src/main/java/com/accounting/dto/BatchBillRequest.java
Normal file
19
src/main/java/com/accounting/dto/BatchBillRequest.java
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package com.accounting.dto;
|
||||||
|
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class BatchBillRequest {
|
||||||
|
@NotEmpty(message = "账单列表不能为空")
|
||||||
|
@Valid
|
||||||
|
private List<BillRequest> bills;
|
||||||
|
|
||||||
|
public List<BillRequest> getBills() {
|
||||||
|
return bills;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBills(List<BillRequest> bills) {
|
||||||
|
this.bills = bills;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -2,6 +2,7 @@ package com.accounting.service;
|
|||||||
|
|
||||||
import com.accounting.dto.BillRequest;
|
import com.accounting.dto.BillRequest;
|
||||||
import com.accounting.dto.BillResponse;
|
import com.accounting.dto.BillResponse;
|
||||||
|
import com.accounting.dto.BatchBillRequest;
|
||||||
import com.accounting.entity.Account;
|
import com.accounting.entity.Account;
|
||||||
import com.accounting.entity.Bill;
|
import com.accounting.entity.Bill;
|
||||||
import com.accounting.entity.Category;
|
import com.accounting.entity.Category;
|
||||||
@ -14,6 +15,7 @@ import org.springframework.stereotype.Service;
|
|||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@ -56,6 +58,39 @@ public class BillService {
|
|||||||
return convertToResponse(bill, category);
|
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
|
@Transactional
|
||||||
public BillResponse updateBill(Long id, BillRequest request, Long userId) {
|
public BillResponse updateBill(Long id, BillRequest request, Long userId) {
|
||||||
// 验证账单是否存在且属于当前用户
|
// 验证账单是否存在且属于当前用户
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user