InvoiceServiceImpl.java
2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package vion.service.impl;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vion.dto.InvoiceDTO;
import vion.mapper.InvoiceMapper;
import vion.model.Contract;
import vion.model.Invoice;
import vion.service.IContractService;
import vion.service.IInvoiceService;
import vion.vo.InvoiceVO;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author HlQ
* @date 2023/12/5
*/
@Service
@RequiredArgsConstructor
public class InvoiceServiceImpl extends MPJBaseServiceImpl<InvoiceMapper, Invoice> implements IInvoiceService {
private final IContractService contractService;
private final Converter converter;
@Override
public Page<InvoiceVO> list(InvoiceDTO dto) {
Page<Invoice> recPage = this.lambdaQuery(converter.convert(dto, new Invoice())).page(Page.of(dto.getPageNum(), dto.getPageSize()));
return Page.<InvoiceVO>of(recPage.getCurrent(), recPage.getSize(), recPage.getTotal()).setRecords(converter.convert(recPage.getRecords(), InvoiceVO.class));
}
@Override
@Transactional(rollbackFor = Exception.class)
public String save(List<InvoiceDTO> dto) {
List<Invoice> invoiceList = converter.convert(dto, Invoice.class);
this.saveBatch(invoiceList);
Map<String, List<Invoice>> no2InvoiceMap = invoiceList.stream().collect(Collectors.groupingBy(Invoice::getContractNo));
no2InvoiceMap.forEach((no, list) -> {
BigDecimal sum = list.stream().map(Invoice::getInvoiceAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
// 根据合同编号查合同
Contract existContract = contractService.lambdaQuery().eq(Contract::getContractNo, no).one();
contractService.lambdaUpdate().eq(Contract::getContractNo, no).set(Contract::getInvoiceAmount, NumberUtil.add(sum, existContract.getInvoiceAmount())).update();
});
return "成功";
}
}