SparePartServiceImpl.java
4.93 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
package vion.service.impl;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.io.file.FileNameUtil;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.lang.Opt;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.data.id.IdUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.crypto.SecureUtil;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import vion.dto.SparePartDTO;
import vion.mapper.SparePartMapper;
import vion.model.*;
import vion.service.IFileService;
import vion.service.ISparePartService;
import vion.vo.SparePartVO;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
* @author HlQ
* @date 2024/1/19
*/
@Service
@RequiredArgsConstructor
public class SparePartServiceImpl extends MPJBaseServiceImpl<SparePartMapper, SparePart> implements ISparePartService {
private final IFileService fileService;
private final Converter converter;
@Value("${fileUrl:}")
private String fileUrl;
@Override
public Object frontSubmit(SparePartDTO dto) {
dto.setUuid(IdUtil.nanoId());
SparePart sparePart = converter.convert(dto, SparePart.class);
if (this.save(sparePart)) {
Opt.ofNullable(dto.getFiles())
.ifPresent(fileList ->
Arrays.stream(fileList).forEach(infile -> {
//上传url地址
String orgName = infile.getOriginalFilename();
String mainName = FileNameUtil.mainName(orgName);
String fileExt = FileNameUtil.extName(orgName);
String filename = StrUtil.format("{}_{}.{}", mainName, DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS"), fileExt);
String path = fileUrl + FileUtil.FILE_SEPARATOR + "sparePart" + FileUtil.FILE_SEPARATOR + sparePart.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(sparePart.getId());
fileInfo.setSourceType(22);
fileInfo.setName(filename);
fileInfo.setUrl(path);
fileInfo.setType(fileExt);
fileInfo.setSha256(SecureUtil.sha256(file).toUpperCase());
fileInfo.setUploader(sparePart.getContact());
fileService.save(fileInfo);
}));
return MapUtil.<String, Long>builder()
.put("id", sparePart.getId())
.build();
}
return "提交失败";
}
@Override
public List<SparePartVO> list(SparePartDTO dto) {
MPJLambdaWrapper<SparePart> wrapper = new MPJLambdaWrapper<>(converter.convert(dto, SparePart.class))
.selectAll(SparePart.class)
.selectAs(Contract::getName, SparePartVO::getContractName)
.selectCollection(RRepairDevice.class, SparePartVO::getRepairDeviceList)
.leftJoin(Contract.class, Contract::getId, SparePart::getContractId)
.leftJoin(RRepairDevice.class, on -> on.eq(RRepairDevice::getRId, SparePart::getId).eq(RRepairDevice::getRType, 1))
.between(SparePart::getCreateTime, dto.getCreateTimeStart(), dto.getCreateTimeEnd())
.orderByDesc(SparePart::getCreateTime);
return this.selectJoinList(SparePartVO.class, wrapper);
}
@Override
public SparePartVO getSparePartDetail(Long id, String uuid) {
MPJLambdaWrapper<SparePart> wrapper = new MPJLambdaWrapper<SparePart>()
.selectAll(PointInfo.class)
.eq(ObjUtil.isNotEmpty(id), SparePart::getId, id)
.eq(StrUtil.isNotBlank(uuid), SparePart::getUuid, uuid)
.orderByDesc(PointInfo::getCreateTime);
return this.selectJoinOne(SparePartVO.class, wrapper);
}
@Override
public String updById(Long id, SparePartDTO dto) {
return this.updateById(converter.convert(dto, SparePart.class)) ? "更新成功" : "更新失败";
}
}