PaymentServiceImpl.java
5.29 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package vion.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vion.dto.PaymentDTO;
import vion.mapper.PaymentMapper;
import vion.model.Contract;
import vion.model.Payment;
import vion.service.IContractPaymentService;
import vion.service.IContractService;
import vion.service.IPaymentService;
import vion.vo.PaymentVO;
import vion.vo.RoleVO;
import vion.vo.UserVO;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author HlQ
* @date 2023/12/5
*/
@Service
@RequiredArgsConstructor
public class PaymentServiceImpl extends MPJBaseServiceImpl<PaymentMapper, Payment> implements IPaymentService {
private final IContractService contractService;
private final IContractPaymentService contractPaymentService;
private final Converter converter;
@Override
public Page<PaymentVO> list(PaymentDTO dto) {
UserVO userVO = (UserVO) StpUtil.getTokenSession().get("curLoginUser");
String saleName = Opt.ofEmptyAble(userVO.getRoleVOList())
.map(l -> l.stream().map(RoleVO::getCode).collect(Collectors.toList()))
.map(roleCodeList -> {
if (!roleCodeList.contains("admin") && !roleCodeList.contains("shangwu") && !roleCodeList.contains("caiwu")) {
return userVO.getUsername();
} else {
return "";
}
}).orElse("未知");
MPJLambdaWrapper<Payment> wrapper = new MPJLambdaWrapper<>(converter.convert(dto, Payment.class))
.selectAll(Payment.class)
.leftJoin(Contract.class, Contract::getContractNo, Payment::getContractNo)
.eq(StrUtil.isNotBlank(saleName), Contract::getSaleName, saleName)
.between(ArrayUtil.isAllNotNull(dto.getCollectionTimeStart(), dto.getCollectionTimeEnd()), Payment::getCollectionTime, dto.getCollectionTimeStart(), dto.getCollectionTimeEnd())
.orderByDesc(Payment::getCreateTime);
return this.selectJoinListPage(Page.of(dto.getPageNum(), dto.getPageSize()), PaymentVO.class, wrapper);
}
@Override
@Transactional(rollbackFor = Exception.class)
public String save(List<PaymentDTO> dto) {
List<Payment> paymentList = converter.convert(dto, Payment.class);
paymentList.forEach(p -> {
Payment payment = this.lambdaQuery().eq(Payment::getSerialNo, p.getSerialNo()).one();
if (ObjUtil.isNull(payment)) {
this.save(p);
} else {
p.setId(payment.getId());
this.updateById(p);
}
});
List<String> noList = paymentList.stream().map(Payment::getContractNo).collect(Collectors.toList());
Map<String, List<Payment>> no2PaymentMap = this.lambdaQuery()
.in(Payment::getContractNo, noList)
.list()
.stream().collect(Collectors.groupingBy(Payment::getContractNo));
List<String> failList = new ArrayList<>();
no2PaymentMap.forEach((no, list) -> {
BigDecimal sumAmount = list.stream().map(Payment::getPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
Contract existContract = contractService.lambdaQuery().eq(Contract::getContractNo, no).one();
if (ObjUtil.isNull(existContract)) {
failList.add(no);
return;
}
// 最新的应收款去计算
Contract updDto = new Contract();
updDto.setId(existContract.getId());
updDto.setPaidAmount(sumAmount);
contractPaymentService.calMoney(existContract, updDto);
contractService.updateById(updDto);
});
if (CollUtil.isNotEmpty(failList)) {
return StrUtil.format("合同编号为{}的合同不存在", failList.toString());
}
return "成功";
}
@Override
@Transactional(rollbackFor = Exception.class)
public String delById(Long id, String contractNo) {
Contract existContract = contractService.lambdaQuery().eq(Contract::getContractNo, contractNo).one();
Assert.notNull(existContract, "该合同编号对应的合同不存在");
this.removeById(id);
// 最新的应收款去计算
BigDecimal sumAmount = this.lambdaQuery().eq(Payment::getContractNo, contractNo).list()
.stream().map(Payment::getPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
Contract updDto = new Contract();
updDto.setId(existContract.getId());
updDto.setPaidAmount(sumAmount);
contractPaymentService.calMoney(existContract, updDto);
contractService.updateById(updDto);
return "删除成功";
}
}