StoreServiceImpl.java
4.45 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
package vion.service.impl;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Opt;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import vion.Global;
import vion.dto.StatusDTO;
import vion.dto.StoreDTO;
import vion.mapper.StoreMapper;
import vion.model.Account;
import vion.model.FileInfo;
import vion.model.Store;
import vion.service.IAccountService;
import vion.service.IFileService;
import vion.service.IStoreService;
import vion.vo.StoreVO;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements IStoreService {
private final IAccountService accountService;
private final IFileService fileService;
private final Converter converter;
@Value("${fileUrl:}")
private String fileUrl;
@Override
public Page<StoreVO> getStoreList(StoreDTO data) {
Page<Store> storeList = this.lambdaQuery(converter.convert(data, new Store()))
.between(data.getStartdate() != null && data.getEnddate() != null, Store::getOrderdate, data.getStartdate(), data.getEnddate())
.page(Page.of(data.getPageNum(), data.getPageSize()));
// todo 缓存
List<Account> accountList = accountService.list();
List<FileInfo> fileInfoList = fileService.lambdaQuery().eq(FileInfo::getSourceType, 1).list();
Map<Long, Long> store2CntMap = fileInfoList.stream().collect(Collectors.groupingBy(FileInfo::getStoreId, Collectors.counting()));
List<StoreVO> storeVOList = new ArrayList<>();
storeList.getRecords().forEach(item -> {
StoreVO storeVO = converter.convert(item, new StoreVO());
storeVO.setAccountName(accountList.stream().filter(v -> v.getId().equals(item.getAccountId())).map(Account::getName).findFirst().orElse("--"));
storeVO.setFileNum(store2CntMap.getOrDefault(item.getId(), 0L));
storeVOList.add(storeVO);
});
return Page.<StoreVO>of(data.getPageNum(), data.getPageSize(), storeList.getTotal()).setRecords(storeVOList);
}
@Override
public String updateStoreStage(StatusDTO statusDTO, String token) {
this.update(Wrappers.<Store>lambdaUpdate()
.set(Store::getProjectStage, statusDTO.getProjectStage())
.eq(Store::getId, statusDTO.getSourceId()));
Opt.ofNullable(statusDTO.getFiles()).ifPresent(tmpFiles -> {
for (MultipartFile infile : tmpFiles) {
//上传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 + statusDTO.getStoreId() + FileUtil.FILE_SEPARATOR + statusDTO.getSourceId() + FileUtil.FILE_SEPARATOR + filename;
File file = FileUtil.touch(path);
try {
infile.transferTo(file);
} catch (IOException e) {
log.error("保存文件出错", e);
}
FileInfo tempFileInfo = new FileInfo();
tempFileInfo.setStoreId(statusDTO.getStoreId());
tempFileInfo.setSourceId(statusDTO.getSourceId());
tempFileInfo.setSourceType(statusDTO.getSourceType());
tempFileInfo.setName(filename);
tempFileInfo.setUrl(path);
tempFileInfo.setType(FileUtil.extName(file));
tempFileInfo.setSha256(SecureUtil.sha256(file).toUpperCase());
tempFileInfo.setUploader(Global.USERNAME_MAP.get(token).getUsername());
fileService.save(tempFileInfo);
}
});
return "更新成功";
}
}