Compare commits
No commits in common. "9641dcaf9a37f62c62a009276dd095b4f3379d2c" and "910fec8e65886b3474262d8b20d9dcca7fe56371" have entirely different histories.
9641dcaf9a
...
910fec8e65
@ -2,7 +2,6 @@ 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;
|
||||||
@ -38,14 +37,6 @@ 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) {
|
||||||
|
|||||||
@ -1,19 +0,0 @@
|
|||||||
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,7 +2,6 @@ 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;
|
||||||
@ -15,7 +14,6 @@ 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;
|
||||||
|
|
||||||
@ -58,39 +56,6 @@ 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