Commit 7b7395a1 authored by 钱坤's avatar 钱坤

私募基金模糊查询里优化最新净值的获取

parent 4545a249
......@@ -352,6 +352,25 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!--允许移动生成的文件 -->
<verbose>true</verbose>
<!-- 是否覆盖 -->
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
......
package com.tanpu.fund.entity.generator;
import java.math.BigDecimal;
import java.util.Date;
public class FundLatestNav {
private Long id;
private String fundId;
private Date priceDate;
private BigDecimal nav;
private BigDecimal cumulativeNav;
private BigDecimal cumulativeNavWithdrawal;
private Integer ishighOrLow;
private BigDecimal tohighNavRatio;
private Date createTime;
private Date updateTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFundId() {
return fundId;
}
public void setFundId(String fundId) {
this.fundId = fundId == null ? null : fundId.trim();
}
public Date getPriceDate() {
return priceDate;
}
public void setPriceDate(Date priceDate) {
this.priceDate = priceDate;
}
public BigDecimal getNav() {
return nav;
}
public void setNav(BigDecimal nav) {
this.nav = nav;
}
public BigDecimal getCumulativeNav() {
return cumulativeNav;
}
public void setCumulativeNav(BigDecimal cumulativeNav) {
this.cumulativeNav = cumulativeNav;
}
public BigDecimal getCumulativeNavWithdrawal() {
return cumulativeNavWithdrawal;
}
public void setCumulativeNavWithdrawal(BigDecimal cumulativeNavWithdrawal) {
this.cumulativeNavWithdrawal = cumulativeNavWithdrawal;
}
public Integer getIshighOrLow() {
return ishighOrLow;
}
public void setIshighOrLow(Integer ishighOrLow) {
this.ishighOrLow = ishighOrLow;
}
public BigDecimal getTohighNavRatio() {
return tohighNavRatio;
}
public void setTohighNavRatio(BigDecimal tohighNavRatio) {
this.tohighNavRatio = tohighNavRatio;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
}
\ No newline at end of file
package com.tanpu.fund.job;
import com.tanpu.common.utils.RedisUtils;
import com.tanpu.fund.service.impl.FundLatestNavService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import javax.annotation.Resource;
/**
* @author: zyh
* <p>
*/
@Slf4j
public class FundLatestNavJob {
@Resource
private FundLatestNavService fundLatestNavService;
@Resource
private RedisUtils redisUtils;
@Scheduled(cron = "5 4/10 * * * ?")
public void execute() {
long start = System.currentTimeMillis();
try {
log.info("私募基金最新净值更新job开始");
redisUtils.lockAndExecute("" + this.getClass().getSimpleName(), 1, 200, () -> {
fundLatestNavService.sync();
return 1;
});
} catch (RedisUtils.LockUnacquiredException ee) {
} catch (Exception e) {
log.error("", e);
} finally {
log.info("私募基金最新净值更新job结束, 耗时: {}ms", System.currentTimeMillis() - start);
}
}
}
package com.tanpu.fund.mapper.generator;
import com.tanpu.fund.entity.generator.FundLatestNav;
import com.tanpu.fund.entity.generator.FundLatestNavExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface FundLatestNavMapper {
long countByExample(FundLatestNavExample example);
int deleteByExample(FundLatestNavExample example);
int deleteByPrimaryKey(Long id);
int insert(FundLatestNav record);
int insertSelective(FundLatestNav record);
List<FundLatestNav> selectByExample(FundLatestNavExample example);
FundLatestNav selectByPrimaryKey(Long id);
int updateByExampleSelective(@Param("record") FundLatestNav record, @Param("example") FundLatestNavExample example);
int updateByExample(@Param("record") FundLatestNav record, @Param("example") FundLatestNavExample example);
int updateByPrimaryKeySelective(FundLatestNav record);
int updateByPrimaryKey(FundLatestNav record);
}
\ No newline at end of file
......@@ -2,12 +2,14 @@ package com.tanpu.fund.mapper.generator.custom;
import com.tanpu.common.model.product.resp.FilePreviewResp;
import com.tanpu.common.model.tanpuroom.Type;
import com.tanpu.fund.entity.generator.FundLatestNav;
import com.tanpu.fund.entity.generator.FundNav;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.Date;
import java.util.List;
import java.util.Map;
......@@ -57,4 +59,11 @@ public interface FundInfoCustomMapper {
@Select("select t.id as id,t.fund_short_name as value from fund_info t where t.fund_short_name like #{productName} and t.delete_tag=0")
List<Type> getFundInfoLikeName(@Param("productName") String productName);
/**
* 获取*私募*基金最新净值
*
* @return
*/
List<FundLatestNav> getFundLatestNet(@Param("start") Date start);
}
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));
}
}
......@@ -124,6 +124,8 @@ public class ProductServiceImpl implements ProductService, Constant {
@Resource
private FeignForProduct product;
@Resource
private FundLatestNavService fundLatestNavService;
@Override
public Page<ProductInfoVO> getProductList(ProductInfoReq req) {
......@@ -2503,7 +2505,9 @@ public class ProductServiceImpl implements ProductService, Constant {
}
// 查询基金最新净值
List<FundNav> fundInfoNewNet = fundInfoCustomMapper.getFundInfoNewNet(fundInfoList.stream().map(Type::getId).collect(Collectors.toList()));
List<FundNav> fundInfoNewNet = fundLatestNavService.getLatestNavBy(fundInfoList.stream().map(Type::getId).collect(Collectors.toList()));
// 下面这个sql太慢了,换个新方案
// List<FundNav> fundInfoNewNet = fundInfoCustomMapper.getFundInfoNewNet(fundInfoList.stream().map(Type::getId).collect(Collectors.toList()));
Map<String, FundNav> fundNavMap = null;
if (CollectionUtils.isNotEmpty(fundInfoNewNet)) {
fundNavMap = fundInfoNewNet.stream().collect(Collectors.toMap(FundNav::getFundId, c -> c));
......
......@@ -64,4 +64,23 @@
left join ifa_imported_fund_nav res on t.fund_id = res.fund_id and t.price_date = res.price_date
</select>
<select id="getFundLatestNet" resultType="com.tanpu.fund.entity.generator.FundLatestNav">
select
res.fund_id as fundId,
res.fund_code as fundCode,
res.price_date as priceDate,
res.nav as nav,
res.cumulative_nav as cumulativeNav,
res.cumulative_nav_withdrawal as cumulativeNavWithdrawal,
res.ishigh_or_low as ishighOrLow,
res.tohigh_nav_ratio as tohighNavRatio,
res.create_time as createTime
from
(SELECT fund_id, max(price_date) as price_date FROM fund_nav WHERE
create_time <![CDATA[ >]]> #{start, jdbcType=TIMESTAMP} and delete_tag = 0
and nav is not null and cumulative_nav is not null
GROUP by fund_id) t
left join fund_nav res on t.fund_id = res.fund_id and t.price_date = res.price_date
</select>
</mapper>
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment