ContractServiceImpl.java
5.12 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
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.ObjUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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 vion.dto.ContractDTO;
import vion.mapper.ContractMapper;
import vion.mapper.RContractStoreMapper;
import vion.model.Contract;
import vion.model.FileInfo;
import vion.model.RContractStore;
import vion.service.IContractPaymentService;
import vion.service.IContractService;
import vion.service.IFileService;
import vion.vo.ContractVO;
import vion.vo.UserVO;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author HlQ
* @date 2023/11/29
*/
@Service
@RequiredArgsConstructor
public class ContractServiceImpl extends MPJBaseServiceImpl<ContractMapper, Contract> implements IContractService {
private final IFileService fileService;
private final IContractPaymentService contractPaymentService;
private final Converter converter;
private final RContractStoreMapper rContractStoreMapper;
@Value("${fileUrl:}")
private String fileUrl;
@Override
public Page<ContractVO> list(ContractDTO dto) {
Contract contract = converter.convert(dto, new Contract());
String name = contract.getName();
contract.setName(null);
List<Long> ids = null;
if (dto.getStoreId() != null) {
ids = rContractStoreMapper.selectList(Wrappers.lambdaQuery(RContractStore.class)
.select(RContractStore::getContractId)
.eq(RContractStore::getStoreId, dto.getStoreId()))
.stream().map(RContractStore::getContractId).collect(Collectors.toList());
if (ids.isEmpty()) {
ids.add(-1L);
}
}
Page<Contract> contractList = this.lambdaQuery(contract)
.in(CollectionUtils.isNotEmpty(ids), Contract::getId, ids)
.like(StringUtils.isNotBlank(name), Contract::getName, name)
.page(Page.of(dto.getPageNum(), dto.getPageSize()));
List<ContractVO> contractVOList = converter.convert(contractList.getRecords(), ContractVO.class);
return Page.<ContractVO>of(contractList.getCurrent(), contractList.getSize(), contractList.getTotal()).setRecords(contractVOList);
}
@Override
public String updateById(Long id, ContractDTO dto) {
Contract exitContract = this.getById(id);
Contract contract = converter.convert(dto, new Contract());
contract.setId(id);
if ((ObjUtil.notEqual(contract.getStatus(), exitContract.getStatus()) && contract.getStatus() != null) || dto.getPaidAmount() != null) {
contractPaymentService.calMoney(exitContract, contract);
}
this.updateById(contract);
UserVO userVO = (UserVO) StpUtil.getTokenSession().get("curLoginUser");
Opt.ofNullable(dto.getFiles())
.ifPresent(fileList ->
Arrays.stream(fileList).forEach(infile -> {
//上传url地址
String orgName = infile.getOriginalFilename();
String fileName = orgName.substring(0, orgName.lastIndexOf("."));
String fileExt = orgName.substring(orgName.lastIndexOf("."));
String filename = fileName + "_" + DateUtil.format(new Date(), "yyyyMMdd_HHmmss") + fileExt;
String path = fileUrl + FileUtil.FILE_SEPARATOR + "contract" + FileUtil.FILE_SEPARATOR + contract.getId() + 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(-1L);
fileInfo.setSourceId(contract.getId());
fileInfo.setSourceType(5);
fileInfo.setName(filename);
fileInfo.setUrl(path);
fileInfo.setType(FileUtil.extName(file));
fileInfo.setSha256(SecureUtil.sha256(file).toUpperCase());
fileInfo.setUploader(userVO.getUsername());
fileService.save(fileInfo);
}));
return "更新成功";
}
}