InvoiceServiceImpl.java
8.63 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package vion.service.impl;
import cn.dev33.satoken.stp.StpUtil;
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.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.lang.Opt;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.math.NumberUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.json.JSONObject;
import org.dromara.hutool.json.JSONUtil;
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.model.RContractUser;
import vion.service.IContractService;
import vion.service.IInvoiceService;
import vion.service.IRContractUserService;
import vion.third.DingMod;
import vion.vo.InvoiceVO;
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.function.Function;
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 IRContractUserService contractUserService;
private final DingMod dingMod;
private final Converter converter;
@Override
public Page<InvoiceVO> list(InvoiceDTO 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<Invoice> wrapper = new MPJLambdaWrapper<>(converter.convert(dto, Invoice.class))
.selectAll(Invoice.class)
.leftJoin(Contract.class, Contract::getContractNo, Invoice::getContractNo)
.eq(StrUtil.isNotBlank(saleName), Contract::getSaleName, saleName)
.between(ArrayUtil.isAllNotNull(dto.getInvoicingTimeStart(), dto.getInvoicingTimeEnd()), Invoice::getInvoicingTime, dto.getInvoicingTimeStart(), dto.getInvoicingTimeEnd());
Page<InvoiceVO> page = Page.of(dto.getPageNum(), dto.getPageSize());
Opt.ofNullable(dto.getOrderItem())
.ifPresent(page::addOrder);
return this.selectJoinListPage(page, InvoiceVO.class, wrapper);
}
@Override
@Transactional(rollbackFor = Exception.class)
public String save(List<InvoiceDTO> dto) {
List<Invoice> invoiceList = converter.convert(dto, Invoice.class);
this.saveBatch(invoiceList);
List<String> noList = invoiceList.stream().map(Invoice::getContractNo).collect(Collectors.toList());
Map<String, Contract> no2ContractMap = Opt.ofEmptyAble(contractService.lambdaQuery()
.in(Contract::getContractNo, noList).list())
.map(l -> l.stream().collect(Collectors.toMap(Contract::getContractNo, Function.identity())))
.orElse(MapUtil.empty());
Map<String, String> no2UseridMap = Opt.ofEmptyAble(contractUserService.lambdaQuery()
.in(RContractUser::getContractNo, noList).list())
.map(l -> l.stream().collect(Collectors.groupingBy(RContractUser::getContractNo, Collectors.mapping(RContractUser::getUserId, Collectors.joining(",")))))
.orElse(MapUtil.empty());
// 推送钉钉消息
invoiceList.forEach(i -> {
String contractNo = i.getContractNo();
Contract existContract = no2ContractMap.get(contractNo);
if (ObjUtil.isNull(existContract)) {
return;
}
Opt.ofBlankAble(no2UseridMap.get(contractNo))
.ifPresent(useridStr -> dingMod.workMsg(buildMsg(useridStr, i, existContract)));
});
Map<String, List<Invoice>> no2InvoiceMap = invoiceList.stream().collect(Collectors.groupingBy(Invoice::getContractNo));
List<String> failList = new ArrayList<>();
no2InvoiceMap.forEach((no, list) -> {
BigDecimal sum = list.stream().map(Invoice::getInvoiceAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
// 根据合同编号查合同
Contract existContract = no2ContractMap.get(no);
if (ObjUtil.isNull(existContract)) {
failList.add(no);
return;
}
contractService.lambdaUpdate().eq(Contract::getContractNo, no).set(Contract::getInvoiceAmount, NumberUtil.add(sum, existContract.getInvoiceAmount())).update(new Contract());
});
if (CollUtil.isNotEmpty(failList)) {
return StrUtil.format("合同编号为{}的合同不存在", failList.toString());
}
return "成功";
}
@Override
public String update(Long id, InvoiceDTO dto) {
Invoice invoice = converter.convert(dto, Invoice.class);
if (this.updateById(invoice)) {
List<Invoice> invoiceList = this.lambdaQuery()
.in(Invoice::getContractNo, dto.getContractNo())
.list();
BigDecimal sumAmount = invoiceList.stream().map(Invoice::getInvoiceAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
contractService.lambdaUpdate()
.eq(Contract::getContractNo, dto.getContractNo())
.set(Contract::getInvoiceAmount, sumAmount)
.update(new Contract());
return "更新成功";
}
return "更新失败";
}
@Override
@Transactional(rollbackFor = Exception.class)
public String delById(Long id) {
Invoice invoice = this.getById(id);
String contractNo = invoice.getContractNo();
contractService.lambdaUpdate()
.eq(Contract::getContractNo, contractNo)
.setSql("invoice_amount = invoice_amount - " + invoice.getInvoiceAmount())
.update(new Contract());
this.removeById(id);
return "成功";
}
@Override
public String calMoney(List<String> noList) {
Map<String, BigDecimal> no2SumMap = this.lambdaQuery()
.in(ArrayUtil.isNotEmpty(noList), Invoice::getContractNo, noList)
.list()
.stream().collect(Collectors.groupingBy(Invoice::getContractNo, Collectors.reducing(BigDecimal.ZERO, Invoice::getInvoiceAmount, BigDecimal::add)));
no2SumMap.forEach((no, sum) -> {
Contract existContract = contractService.lambdaQuery().eq(Contract::getContractNo, no).one();
if (ObjUtil.isNull(existContract)) {
return;
}
contractService.lambdaUpdate()
.eq(Contract::getContractNo, no)
.set(Contract::getInvoiceAmount, sum)
.update(new Contract());
});
return "金额计算成功";
}
JSONObject buildMsg(String userid, Invoice invoice, Contract contract) {
var jsonObj = JSONUtil.ofObj()
.set("agent_id", 2358374016L)
.set("userid_list", userid);
var msg = JSONUtil.ofObj();
var content = JSONUtil.ofObj().set("title", "开票提醒");
String template = """
#### 开票提醒
#### 合同编号:**{}**
#### 合同名称:**{}**
#### 发票编号:{}
#### 发票金额:{}
#### 开票时间:{}
#### 备注:{}
#### 发送时间:{}
""";
String markdown = StrUtil.format(template,
contract.getContractNo(), contract.getName(), invoice.getInvoiceNo(), invoice.getInvoiceAmount(), DateUtil.formatDate(invoice.getInvoicingTime()), invoice.getRemark(), DateUtil.now());
content.set("text", markdown);
msg.set("msgtype", "markdown").set("markdown", content);
jsonObj.set("msg", msg);
return jsonObj;
}
}