SettlementDiffServiceImpl.java
5.87 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
package vion.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
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.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vion.dto.SettlementDiffDTO;
import vion.mapper.SettlementDiffMapper;
import vion.model.Contract;
import vion.model.FileInfo;
import vion.model.SettlementDiff;
import vion.service.IContractPaymentService;
import vion.service.IContractService;
import vion.service.IFileService;
import vion.service.ISettlementDiffService;
import vion.vo.SettlementDiffVO;
import vion.vo.UserVO;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
* @author HlQ
* @date 2023/12/25
*/
@Service
@RequiredArgsConstructor
public class SettlementDiffServiceImpl extends MPJBaseServiceImpl<SettlementDiffMapper, SettlementDiff> implements ISettlementDiffService {
private final IContractService contractService;
private final IContractPaymentService contractPaymentService;
private final IFileService fileService;
private final Converter converter;
@Value("${fileUrl:}")
private String fileUrl;
@Override
public Page<SettlementDiffVO> list(String no, SettlementDiffDTO dto) {
Page<SettlementDiff> diffPage = this.lambdaQuery(converter.convert(dto, SettlementDiff.class))
.eq(SettlementDiff::getContractNo, no)
.page(Page.of(dto.getPageNum(), dto.getPageSize()));
List<SettlementDiffVO> voList = converter.convert(diffPage.getRecords(), SettlementDiffVO.class);
return Page.<SettlementDiffVO>of(diffPage.getCurrent(), diffPage.getSize(), diffPage.getTotal()).setRecords(voList);
}
@Override
public SettlementDiffVO getVOById(Long id) {
return converter.convert(this.getById(id), SettlementDiffVO.class);
}
@Override
@Transactional(rollbackFor = Exception.class)
public String save(SettlementDiffDTO dto) {
SettlementDiff settlementDiff = converter.convert(dto, SettlementDiff.class);
if (this.save(settlementDiff)) {
calMoney(settlementDiff);
Long id = settlementDiff.getId();
handleFile(dto, id);
return "新增成功";
}
return "新增失败";
}
@Override
@Transactional(rollbackFor = Exception.class)
public String update(Long id, SettlementDiffDTO dto) {
SettlementDiff settlementDiff = converter.convert(dto, SettlementDiff.class);
if (this.updateById(settlementDiff)) {
SettlementDiff existDiff = this.getById(id);
calMoney(existDiff);
handleFile(dto, id);
return "更新成功";
}
return "更新失败";
}
@Override
@Transactional(rollbackFor = Exception.class)
public String delById(Long id) {
Opt.ofEmptyAble(fileService.lambdaQuery()
.eq(FileInfo::getSourceId, id)
.eq(FileInfo::getSourceType, 15)
.list())
.ifPresent(l -> {
l.forEach(f -> FileUtil.del(f.getUrl()));
fileService.removeByIds(l);
});
SettlementDiff existDiff = this.getById(id);
this.removeById(id);
calMoney(existDiff);
return "删除成功";
}
private void calMoney(SettlementDiff existDiff) {
Contract existContract = contractService.lambdaQuery().eq(Contract::getContractNo, existDiff.getContractNo()).one();
Contract contractDto = new Contract();
contractDto.setId(existContract.getId());
contractPaymentService.calMoney(existContract, contractDto);
contractService.updateById(contractDto);
}
private void handleFile(SettlementDiffDTO dto, Long id) {
Opt.ofNullable(dto.getFiles())
.ifPresent(fileList -> {
UserVO userVO = (UserVO) StpUtil.getTokenSession().get("curLoginUser");
Arrays.stream(fileList).forEach(infile -> {
String orgName = infile.getOriginalFilename();
String mainName = FileUtil.mainName(orgName);
String fileExt = FileUtil.extName(orgName);
String filename = StrUtil.format("{}_{}.{}", mainName, DateUtil.format(new Date(), "yyyyMMdd_HHmmss"), fileExt);
String path = fileUrl + FileUtil.FILE_SEPARATOR + "settlementDiff" + FileUtil.FILE_SEPARATOR + dto.getContractNo() + FileUtil.FILE_SEPARATOR + filename;
File file = FileUtil.touch(path);
try {
infile.transferTo(file);
} catch (IOException e) {
log.error("保存文件出错", e);
}
FileInfo fileInfo = new FileInfo();
fileInfo.setStoreId(0L);
fileInfo.setSourceId(id);
fileInfo.setContractId(dto.getContractId());
fileInfo.setSourceType(15);
fileInfo.setName(filename);
fileInfo.setUrl(path);
fileInfo.setType(FileUtil.extName(file));
fileInfo.setSha256(SecureUtil.sha256(file).toUpperCase());
fileInfo.setUploader(userVO.getUsername());
fileService.save(fileInfo);
});
});
}
}