• zp's avatar
    init · f9a4de2a
    zp authored
    f9a4de2a
FundCombineInfoServiceImpl.java 8.36 KB
package com.tanpu.fund.service.impl;

import com.github.pagehelper.page.PageMethod;
import com.tanpu.common.enums.Constant;
import com.tanpu.common.model.Page;
import com.tanpu.common.model.Pageable;
import com.tanpu.common.model.product.resp.Net;
import com.tanpu.common.model.product.resp.ProductLabel;
import com.tanpu.common.resp.CommonResp;
import com.tanpu.common.utils.BigDecimalUtil;
import com.tanpu.fund.api.model.FundInfoVO;
import com.tanpu.fund.api.model.resp.FundGroupVO;
import com.tanpu.fund.api.model.resp.FundSetListVO;
import com.tanpu.fund.entity.generator.*;
import com.tanpu.fund.enums.ProStrategyEnums;
import com.tanpu.fund.enums.ProTypeEnums;
import com.tanpu.fund.feign.user.FeignClientForFatools;
import com.tanpu.fund.mapper.generator.*;
import com.tanpu.fund.service.FundCombineInfoService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.tanpu.common.utils.BigDecimalUtil.multiply100;

@Service
public class FundCombineInfoServiceImpl implements FundCombineInfoService {

    @Resource
    private FundInfoMapper fundInfoMapper;
    @Resource
    private FundNavMapper fundNavMapper;
    @Resource
    private FundPerformanceMapper fundPerformanceMapper;
    @Resource
    private FundCountMapper fundCountMapper;
    @Resource
    private FeignClientForFatools feignClientForFatools;
    @Resource
    private FundCombineInfoMapper fundCombineInfoMapper;

    @Override
    public Page<FundSetListVO> queryFundCombines(Integer strategyType, Pageable pageable) {

        // 分页查询
        com.github.pagehelper.Page<FundSetListVO> page = PageMethod.startPage(pageable.getPageNumber(), pageable.getPageSize());

        FundCountExample example = new FundCountExample();
        example.createCriteria().andStrategyEqualTo(strategyType).andDataSourcesEqualTo(Constant.ONE_NUM)
                .andStatusEqualTo(1).andDeleteTagEqualTo(Constant.ZERO_NUM);
        example.setOrderByClause("sort desc,create_time desc");
        List<FundCount> fundCounts = this.fundCountMapper.selectByExample(example);

        List<String> fundIds = fundCounts.stream().map(FundCount::getFundId).collect(Collectors.toList());

        //查询基金信息
        Map<String, FundInfo> fundMap;
        {
            if (CollectionUtils.isEmpty(fundIds)) {
                fundMap = new HashMap<>(0);
            } else {
                FundInfoExample fundInfoExample = new FundInfoExample();
                fundInfoExample.createCriteria().andIdIn(fundIds).andDeleteTagEqualTo(Constant.ZERO_NUM);
                List<FundInfo> fundInfoList = this.fundInfoMapper.selectByExample(fundInfoExample);

                fundMap = fundInfoList.stream().collect(Collectors.toMap(FundInfo::getId, c -> c));
            }
        }

        //基金收益
        Map<String, FundCount> performanceMap = fundCounts.stream().collect(Collectors.toMap(FundCount::getFundId, c -> c));

        //组装返回结果
        List<FundSetListVO> resList = fundCounts.stream()
                .filter(f -> fundMap.get(f.getFundId()) != null && performanceMap.get(f.getFundId()) != null)
                .map(item -> {
                    FundCount fundCount = performanceMap.get(item.getFundId());

                    FundInfo fundInfo = fundMap.get(item.getFundId());
                    return FundSetListVO.builder()
                            .id(item.getFundId())
                            .name(fundInfo.getFundShortName())
                            .strategy(fundCount == null ? null : fundCount.getStrategy())
                            .strategyName(ProStrategyEnums.getName(fundCount == null ? null : fundCount.getStrategy()))
                            .desc(fundInfo.getDescInfo())
                            .ratioId(fundInfo.getPrimaryBenchmarkId())
                            .foundProfit(multiply100(fundCount.getRetIncep()))
                            .build();
                }).collect(Collectors.toList());

        return new Page<>(pageable, page.getTotal(), resList);
    }

    @Override
    public FundGroupVO detail(String id) {

        //查询组合基金详情
        FundInfo fundInfo = this.fundInfoMapper.selectByPrimaryKey(id);

        FundGroupVO resItem = FundGroupVO.builder()
                .productName(fundInfo.getFundShortName())
                .groupTarget(fundInfo.getCombineTarget())
                .customerGroup(fundInfo.getFitGroup())
                .groupComment(fundInfo.getCombineComment())
                .transferComment(fundInfo.getTransferComment())
                .build();

        resItem.setRiskLevel(fundInfo.getRiskLevel());

        //查询组合基金标签
        {
            CommonResp<List<ProductLabel>> resp = this.feignClientForFatools.querytagbyrelid(id);
            if (resp.isSuccess()) {
                resItem.setProductLabels(resp.getAttributes());
            }
        }

        //查询最新净值
        {
            FundNavExample example = new FundNavExample();
            example.createCriteria().andFundIdEqualTo(id).andDeleteTagEqualTo(Constant.ZERO_NUM);
            example.setOrderByClause(FundNav.Column.priceDate.desc());
            List<FundNav> fundNavs = this.fundNavMapper.selectByExampleWithRowbounds(example, new RowBounds(0, 1));
            if (!CollectionUtils.isEmpty(fundNavs)) {
                FundNav fundNav = fundNavs.get(0);

                resItem.setNet(Net.builder()
                        .netDate(fundNav.getPriceDate().getTime())
                        .netValue(BigDecimalUtil.toString(fundNav.getNav(),4))
                        .build());
            }
        }

        //查询最近收益
        {

            FundCountExample example = new FundCountExample();
            example.createCriteria().andFundIdEqualTo(id).andStatusEqualTo(Constant.ONE_NUM).andDeleteTagEqualTo(Constant.ZERO_NUM);
            List<FundCount> fundCounts = this.fundCountMapper.selectByExample(example);
            if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(fundCounts)) {
                FundCount fundCount = fundCounts.get(0);
                resItem.setNear1Month(multiply100(fundCount.getRet1m()));
                resItem.setNear1YearRatio(multiply100(fundCount.getRet1y()));
                resItem.setRetIncep(multiply100(fundCount.getRetIncep()));
            }
        }

        //查询基金占比
        {
            FundCombineInfoExample example = new FundCombineInfoExample();
            example.createCriteria().andPFundIdEqualTo(id).andFlagEqualTo(Constant.ZERO_NUM).andDeleteTagEqualTo(Constant.ZERO_NUM);
            List<FundCombineInfo> fundCombineInfos = fundCombineInfoMapper.selectByExample(example);

            if (!CollectionUtils.isEmpty(fundCombineInfos)) {

                List<String> fundIdList = fundCombineInfos.stream().map(FundCombineInfo::getFundId).collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(fundIdList)) {
                    FundInfoExample infoExample = new FundInfoExample();
                    infoExample.createCriteria().andIdIn(fundIdList).andDeleteTagEqualTo(Constant.ZERO_NUM);
                    List<FundInfo> fundInfos = fundInfoMapper.selectByExample(infoExample);

                    if (!CollectionUtils.isEmpty(fundInfos)) {
                        Map<String, FundInfo> map = fundInfos.stream().collect(Collectors.toMap(FundInfo::getId, c -> c));

                        List<FundInfoVO> fundInfoVOList = fundCombineInfos.stream().filter(item -> map.get(item.getFundId()) != null).map(item -> {
                            FundInfo info = map.get(item.getFundId());

                            FundInfoVO p = FundInfoVO.builder()
                                    .fundName(info.getFundShortName())
                                    .ratio(multiply100(item.getProportionRatio()))
                                    .bonusTypeName(ProTypeEnums.getName(info.getFundType()))
                                    .bonusType(info.getFundType())
                                    .build();
                            return p;
                        }).collect(Collectors.toList());

                        resItem.setFundInfoVOList(fundInfoVOList);
                    }
                }
            }
        }

        return resItem;
    }
}