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
package com.tanpu.fund.service.impl;
import com.tanpu.common.utils.DateUtils;
import com.tanpu.fund.entity.generator.FundLatestNav;
import com.tanpu.fund.entity.generator.FundLatestNavExample;
import com.tanpu.fund.entity.generator.FundNav;
import com.tanpu.fund.mapper.generator.FundLatestNavMapper;
import com.tanpu.fund.mapper.generator.custom.FundInfoCustomMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
@Slf4j
public class FundLatestNavService {
@Resource
private FundLatestNavMapper fundLatestNavMapper;
@Resource
private FundInfoCustomMapper fundInfoCustomMapper;
public List<FundNav> getLatestNavBy(List<String> fundIdList) {
if (CollectionUtils.isEmpty(fundIdList)) {
return new ArrayList<>();
}
FundLatestNavExample example = new FundLatestNavExample();
example.createCriteria().andFundIdIn(fundIdList);
List<FundLatestNav> navList = fundLatestNavMapper.selectByExample(example);
return navList.stream().map(p -> {
FundNav target = new FundNav();
BeanUtils.copyProperties(p, target);
return target;
}).collect(Collectors.toList());
}
public void sync(){
FundLatestNavExample example = new FundLatestNavExample();
example.createCriteria().andFundIdEqualTo("DF17000000110");
List<FundLatestNav> list = fundLatestNavMapper.selectByExample(example);
Date startTime = list.get(0).getCreateTime();
List<FundLatestNav> latestList = fundInfoCustomMapper.getFundLatestNet(startTime);
log.info("私募新净值统计, count: {}, start: {}", latestList.size(), DateUtils.format(startTime));
if (CollectionUtils.isEmpty(latestList)) {
log.info("私募没有新净值数据,结束");
return;
}
int p = 0;
while (p < latestList.size()) {
List<FundLatestNav> batchList = latestList.subList(p, Math.min(latestList.size(), p + 200));
if (CollectionUtils.isEmpty(batchList)) {
break;
}
List<String> idList = batchList.stream().map(FundLatestNav::getFundId).collect(Collectors.toList());
FundLatestNavExample latestNavExample = new FundLatestNavExample();
latestNavExample.createCriteria().andFundIdIn(idList);
Map<String, List<FundLatestNav>> existMap = fundLatestNavMapper.selectByExample(latestNavExample)
.stream().collect(Collectors.groupingBy(FundLatestNav::getFundId));
for (FundLatestNav latestNav : batchList) {
latestNav.setUpdateTime(null);
if (existMap.containsKey(latestNav.getFundId())) {
// update
latestNav.setId(existMap.get(latestNav.getFundId()).get(0).getId());
fundLatestNavMapper.updateByPrimaryKeySelective(latestNav);
} else {
// insert
fundLatestNavMapper.insertSelective(latestNav);
}
}
p += 200;
}
Date newStartTime = latestList.stream().map(FundLatestNav::getCreateTime).max(Date::compareTo).get();
FundLatestNav target = new FundLatestNav();
target.setCreateTime(newStartTime);
int c = fundLatestNavMapper.updateByExampleSelective(target, example);
log.info("私募新净值更新起始时间,affected: {}, start: {}", c, DateUtils.format(newStartTime));
}
}