GlobalService.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package com.smppw.analysis.application.service.info;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import cn.hutool.core.collection.ListUtil;
  4. import cn.hutool.core.map.MapUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import com.smppw.analysis.application.dto.info.*;
  7. import com.smppw.analysis.domain.dto.info.HeadInfoParams;
  8. import com.smppw.analysis.domain.dto.info.HeadInfoVO;
  9. import com.smppw.analysis.domain.dto.info.PrivatelyFundHeadInfoVO;
  10. import com.smppw.analysis.domain.dto.info.PubliclyFundHeadInfoVO;
  11. import com.smppw.analysis.domain.dto.performance.IndicatorParams;
  12. import com.smppw.analysis.domain.manager.info.HeadInfoConstants;
  13. import com.smppw.analysis.domain.manager.info.HeadInfoFactory;
  14. import com.smppw.analysis.domain.manager.performance.Performance;
  15. import com.smppw.analysis.domain.manager.performance.PerformanceConstants;
  16. import com.smppw.analysis.domain.manager.performance.PerformanceFactory;
  17. import com.smppw.analysis.domain.service.BaseInfoService;
  18. import com.smppw.analysis.domain.service.NavService;
  19. import com.smppw.analysis.infrastructure.exception.APIException;
  20. import com.smppw.common.pojo.IStrategy;
  21. import com.smppw.common.pojo.ValueLabelVO;
  22. import com.smppw.common.pojo.dto.DateValue;
  23. import com.smppw.common.pojo.dto.calc.IndicatorCalcTimeRangeDto;
  24. import com.smppw.common.pojo.enums.*;
  25. import com.smppw.constants.SecType;
  26. import com.smppw.core.IndicatorService;
  27. import com.smppw.utils.StrategyHandleUtils;
  28. import org.apache.commons.lang3.StringUtils;
  29. import org.springframework.stereotype.Service;
  30. import java.util.*;
  31. import java.util.stream.Collectors;
  32. @Service
  33. public class GlobalService {
  34. private final NavService navService;
  35. private final BaseInfoService baseInfoService;
  36. private final HeadInfoFactory headInfoFactory;
  37. private final PerformanceFactory performanceFactory;
  38. public GlobalService(NavService navService,
  39. BaseInfoService baseInfoService,
  40. HeadInfoFactory headInfoFactory,
  41. PerformanceFactory performanceFactory) {
  42. this.navService = navService;
  43. this.baseInfoService = baseInfoService;
  44. this.headInfoFactory = headInfoFactory;
  45. this.performanceFactory = performanceFactory;
  46. }
  47. public HeadInfoVO headInfo(HeadInfoReq req) {
  48. String secType = this.baseInfoService.getSecType(req.getSecId());
  49. String type = HeadInfoConstants.SEC_TYPE_HEAD_MAP.get(secType);
  50. HeadInfoParams params = req.convert();
  51. params.setSecType(secType);
  52. return this.headInfoFactory.getInstance(type).get(params);
  53. }
  54. public CommonInfoVO commonInfo(CommonInfoReq params) {
  55. String secType = this.baseInfoService.getSecType(params.getSecId());
  56. CommonInfoVO res = new CommonInfoVO();
  57. String secId = params.getSecId();
  58. List<Frequency> result = CollectionUtil.newArrayList();
  59. String type = HeadInfoConstants.SEC_TYPE_HEAD_MAP.get(secType);
  60. HeadInfoVO fundHeadInfo = this.headInfoFactory.getInstance(type).get(new HeadInfoParams(secId, type));
  61. if (fundHeadInfo == null) {
  62. throw new APIException("");
  63. }
  64. String raiseType = fundHeadInfo.getRaiseType();
  65. result.add(this.baseInfoService.getNavFrequency(secId));
  66. // 将频率转换一道 取最小频率
  67. List<Frequency> frequencies = this.toFrequencyList(result);
  68. if (Objects.equals("2", raiseType)) {
  69. frequencies.removeIf(p -> p.getId() == Frequency.Default.getId());
  70. }
  71. List<ValueLabelVO> frequencyValueLabel = this.trans2ValueLabel(frequencies);
  72. res.setFrequency(frequencyValueLabel);
  73. // 净值类型
  74. List<ValueLabelVO> navType = Arrays.stream(NavType.values())
  75. .filter(p -> !NavType.All.equals(p) && !NavType.UnitAndCumulativeNav.equals(p))
  76. .map(p -> new ValueLabelVO(p.name(), p.getDesc())).collect(Collectors.toList());
  77. res.setNavType(navType);
  78. handleTimeRange(res, secId);
  79. // 获取常用指数
  80. List<ValueLabelVO> benchmarkList = baseInfoService.getCommonIndexList();
  81. handleBenchmark(res, secType, benchmarkList, fundHeadInfo);
  82. return res;
  83. }
  84. @SuppressWarnings("unchecked")
  85. public HeadIndicatorVO headIndicator(HeadIndicatorReq req) {
  86. IndicatorParams params = req.convert();
  87. Performance<IndicatorParams, Map<String, Object>> instance = this.performanceFactory.getInstance(PerformanceConstants.INDICATOR);
  88. Map<String, Object> dataset = instance.execute(params);
  89. Map<String, Object> indicatorMap = MapUtil.get(dataset, req.getSecId(), Map.class);
  90. HeadIndicatorVO vo = new HeadIndicatorVO();
  91. vo.setAnnualReturn(MapUtil.getStr(indicatorMap, Indicator.AnnualReturn.name()));
  92. vo.setAnnualStdDev(MapUtil.getStr(indicatorMap, Indicator.AnnualStdDev.name()));
  93. vo.setMaxDrawdown(MapUtil.getStr(indicatorMap, Indicator.MaxDrawdown.name()));
  94. vo.setSharpeRatio(MapUtil.getStr(indicatorMap, Indicator.SharpeRatio.name()));
  95. return vo;
  96. }
  97. private List<Frequency> toFrequencyList(List<Frequency> value) {
  98. if (CollectionUtil.isEmpty(value))
  99. return CollectionUtil.newArrayList(Frequency.Default);
  100. if (value.contains(Frequency.Daily))
  101. return CollectionUtil.newArrayList(Frequency.Default, Frequency.Daily, Frequency.Weekly, Frequency.Monthly);
  102. if (value.contains(Frequency.Weekly))
  103. return CollectionUtil.newArrayList(Frequency.Default, Frequency.Weekly, Frequency.Monthly);
  104. if (value.contains(Frequency.Monthly))
  105. return CollectionUtil.newArrayList(Frequency.Default, Frequency.Monthly);
  106. return CollectionUtil.newArrayList(Frequency.Default);
  107. }
  108. private List<ValueLabelVO> trans2ValueLabel(List<Frequency> frequencies) {
  109. List<ValueLabelVO> res = CollectionUtil.newArrayList();
  110. for (Frequency frequency : frequencies) {
  111. res.add(new ValueLabelVO(frequency.name(), frequency.getDesc()));
  112. }
  113. return res;
  114. }
  115. private void handleTimeRange(CommonInfoVO vo, String secId) {
  116. List<String> refIds = ListUtil.toLinkedList(secId);
  117. String endDate = null;
  118. String startDate = null;
  119. Frequency navFrequency = this.baseInfoService.getNavFrequency(secId);
  120. Map<String, List<DateValue>> allNavMap = this.navService.getSecIdDateValueNavListMapByDb(refIds, null, null, Visibility.Both, NavType.CumulativeNav);
  121. List<IndicatorCalcTimeRangeDto> secTimeRanges = IndicatorService.getInstance().getSecTimeRange(secId, navFrequency, allNavMap);
  122. Map<String, String> timeRangeMap = MapUtil.newHashMap();
  123. for (IndicatorCalcTimeRangeDto timeRange : secTimeRanges) {
  124. if (StringUtils.isEmpty(endDate)) {
  125. endDate = timeRange.getEndDate();
  126. }
  127. if (TimeRange.FromSetup == timeRange.getTimeRange()) {
  128. startDate = timeRange.getStartDate();
  129. }
  130. timeRangeMap.put(timeRange.getTimeRange().name(), timeRange.getStartDate());
  131. }
  132. vo.setTimeRangeMap(timeRangeMap);
  133. vo.setEndDate(endDate);
  134. vo.setStartDate(startDate);
  135. }
  136. private void handleBenchmark(CommonInfoVO res, String fundType, List<ValueLabelVO> benchmarkList, HeadInfoVO headInfoVO) {
  137. String benchmarkId = null;
  138. ValueLabelVO valueLabelVO;
  139. // 基金有对应策略的指数
  140. if (fundType.equals(SecType.PRIVATELY_OFFERED_FUND) || fundType.equals(SecType.PRIVATE_FUND)) {
  141. PrivatelyFundHeadInfoVO fundHeadInfo = (PrivatelyFundHeadInfoVO) headInfoVO;
  142. benchmarkId = fundHeadInfo.getPrimaryBenchmarkId();
  143. valueLabelVO = new ValueLabelVO(benchmarkId, fundHeadInfo.getPrimaryBenchmarkName());
  144. } else if (fundType.equals(SecType.PUBLICLY_OFFERED_FUNDS)) {
  145. // 公募需要特殊处理
  146. PubliclyFundHeadInfoVO fundHeadInfo = (PubliclyFundHeadInfoVO) headInfoVO;
  147. Integer subStrategyId = fundHeadInfo.getSubstrategyId();
  148. IStrategy iStrategy = StrategyHandleUtils.getStrategyById(subStrategyId);
  149. benchmarkId = iStrategy.getBenchmark();
  150. if (StrUtil.isBlank(benchmarkId)) {
  151. benchmarkId = fundHeadInfo.getPrimaryBenchmarkId();
  152. valueLabelVO = new ValueLabelVO(benchmarkId, fundHeadInfo.getPrimaryBenchmarkName());
  153. } else {
  154. Map<String, String> querySecName = this.baseInfoService.querySecName(ListUtil.of(benchmarkId));
  155. String indexName = querySecName.get(benchmarkId);
  156. valueLabelVO = new ValueLabelVO(benchmarkId, indexName);
  157. }
  158. } else {
  159. valueLabelVO = new ValueLabelVO();
  160. }
  161. if (CollectionUtil.isNotEmpty(benchmarkList)) {
  162. // 如果常用指数里面没有基准,则把基准放在常用指数的第一位
  163. Optional<ValueLabelVO> contains = benchmarkList.stream().filter(p -> p.getValue().equals(valueLabelVO.getValue())).findFirst();
  164. if (contains.isEmpty()) {
  165. benchmarkList.add(0, valueLabelVO);
  166. }
  167. } else {
  168. benchmarkList.add(valueLabelVO);
  169. }
  170. res.setBenchmarkId(benchmarkId);
  171. res.setUsefulBenchmark(benchmarkList);
  172. }
  173. }