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)); } }