package com.diagnose.service; import com.diagnose.common.handler.BizException; import com.diagnose.common.result.ResponseStatus; import com.diagnose.mapper.DiagnoseMapper; import com.diagnose.mapper.FinanceMapper; import com.diagnose.mapper.StockMapper; import com.diagnose.vo.*; import org.springframework.stereotype.Service; import javax.annotation.Resource; import javax.validation.constraints.NotNull; import java.math.BigDecimal; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; @Service public class StockService { private static final long cacheExpires = 60 * 60 * 1000; private static long cacheTime = 0; private static final int kLineMonth = 3; private List stockListCache = null; private Map stockMapCache = null; // @Resource private StockMapper stockMapper; @Resource private FinanceMapper financeMapper; @Resource private DiagnoseMapper diagnoseMapper; public List selectAllStock() { return stockMapper.selectAllStock(); } public synchronized List selectALlStockCache () { long now = System.currentTimeMillis(); if (stockListCache == null || now - cacheTime > cacheExpires) { stockListCache = selectAllStock(); cacheTime = now; } return stockListCache; } public StockVO getStock(Long stkUniCode) { if (stockMapCache == null) { stockMapCache = selectALlStockCache().stream().collect(Collectors.toMap(StockVO::getSecUniCode, Function.identity())); } return stockMapCache.get(stkUniCode); } // 得分排名 public DiagnoseRankVO rank(StockVO stock) { DiagnoseRankVO diagnoseRankVO = diagnoseMapper.selectRank(stock.getSecUniCode()); if (diagnoseRankVO == null) { return null; } Integer totalInduCount = diagnoseMapper.selectTotalInduCount(stock.getPlateUniCode()); Integer totalMktCount = diagnoseMapper.selectTotalMktCount(); diagnoseRankVO.setTotalInduCount(totalInduCount); diagnoseRankVO.setTotalMktCount(totalMktCount); return diagnoseRankVO; } // 综合评分 public ComprehensiveEvaluationVO comprehensiveEvaluation(Long stkUniCode) { StockVO stock = getStock(stkUniCode); if (stock == null) { return null; } ComprehensiveEvaluationVO vo = new ComprehensiveEvaluationVO(); vo.setStock(stock); DiagnoseRankVO diagnoseRankVO = rank(stock); if (diagnoseRankVO == null) { return null; } vo.setRank(diagnoseRankVO); BigDecimal star = diagnoseRankVO.getTotalStar(); if (star == null) { return vo; } List backTestList = diagnoseMapper.selectBackTest(star); vo.setBackTestList(backTestList); return vo; } // 财务估值 public FinancialValuationVO financialValuation(Long stkUniCode) { StockVO stock = getStock(stkUniCode); if (stock == null) { return null; } Long secUniCode = stock.getSecUniCode(); String secCode = stock.getSecCode(); Integer mktNum = stock.getMktNum(); // 总结性机器语 & 财务估值得分 FinancialValuationVO vo = financeMapper.selectFinancialValuation(secCode, mktNum); if (vo == null) { return null; } // 财务分析行业排名 FinanceRankVO financeRank = financeMapper.selectFinanceRank(secUniCode); vo.setFinanceRank(financeRank); // 估值分析 List diagnoseValuationList = diagnoseMapper.selectDiagnoseValuation(secUniCode); vo.setDiagnoseValuationList(diagnoseValuationList); // 行情估值 String tableName = stock.getPriceTableName(); StockPriceVO stockPrice = stockMapper.selectLatestPrice(tableName, secUniCode); vo.setStockPrice(stockPrice); return vo; } // 财务估值-更多 public FinancialValuationExtendVO financialValuationExtend(Long stkUniCode) { StockVO stock = getStock(stkUniCode); if (stock == null) { return null; } String secCode = stock.getSecCode(); Integer mktNum = stock.getMktNum(); Integer secMarPar = stock.getSecMarPar(); // 总结性机器语 & 财务估值得分 FinancialValuationVO baseVO = financeMapper.selectFinancialValuation(secCode, mktNum); if (baseVO == null) { return null; } FinancialValuationExtendVO vo = new FinancialValuationExtendVO(); vo.setFinSumMac(baseVO.getFinSumMac()); vo.setScore(baseVO.getScore()); // 财务指标分析列表 List financeIndexAnalysisList = financeMapper.selectFinanceIndexAnalysis(secCode, secMarPar); financeIndexAnalysisList.forEach(fiaVO -> { if (fiaVO.getEndDate() != null && fiaVO.getEndDate().length() > 10) { fiaVO.setEndDate(fiaVO.getEndDate().substring(0, 10)); } }); vo.setFinanceIndexAnalysisList(financeIndexAnalysisList); // 现金流量列表 List financeCashShortList = financeMapper.selectFinanceCashShort(secCode, secMarPar); financeCashShortList.forEach(fcsVO -> { if (fcsVO.getEndDate() != null && fcsVO.getEndDate().length() > 10) { fcsVO.setEndDate(fcsVO.getEndDate().substring(0, 10)); } }); vo.setFinanceCashShortList(financeCashShortList); return vo; } // 估值分析 public List financialValuationAnalysis(Long stkUniCode, Integer timeSpan, Integer type) { StockVO stock = getStock(stkUniCode); if (stock == null) { return null; } String tableName = stock.getPriceTableName(); String stockValueColumn = getStockValueColumn(type); Long plateUniCode = stock.getPlateUniCode(); // 估值分析 List list = stockMapper.selectFinancialValuationAnalysis(tableName, stockValueColumn, stkUniCode, plateUniCode, timeSpan, type); return list; } // 量化分析 public QuantitativeAnalysisVO quantitativeAnalysis(Long stkUniCode) { StockVO stock = getStock(stkUniCode); if (stock == null) { return null; } String secCode = stock.getSecCode(); Integer mktNum = stock.getMktNum(); // 量化分析 QuantitativeAnalysisVO vo = diagnoseMapper.selectQuantitativeAnalysis(secCode, mktNum); if (vo == null) { return null; } String tableName = stock.getPriceTableName(); // K线 List kLineList = stockMapper.selectKLine(tableName, stkUniCode, kLineMonth); vo.setkLineList(kLineList); return vo; } // 市场热度 public MarketSentimentVO marketSentiment(@NotNull Long stkUniCode) { StockVO stock = getStock(stkUniCode); if (stock == null) { return null; } Long secUniCode = stock.getSecUniCode(); String secCode = stock.getSecCode(); Integer mktNum = stock.getMktNum(); Integer secMarPar = stock.getSecMarPar(); // 市场热度分析 MarketSentimentVO vo = diagnoseMapper.selectMarketSentiment(secCode, mktNum); if (vo == null) { return null; } // 热度趋势 List sentimentTrendList = diagnoseMapper.selectSentimentTrend(secUniCode); vo.setSentimentTrendList(sentimentTrendList); // 机构评级 List institutionalRatingList = diagnoseMapper.selectInstitutionalRating(secCode, secMarPar); vo.setInstitutionalRatingList(institutionalRatingList); return vo; } private String getStockValueColumn(Integer type) { if (type == 2) { return "stk_per_ttm"; } else if (type == 3) { return "price_bookv_ratio"; } throw new BizException(ResponseStatus.PARM_ERROR, "类型错误"); } public InstitutionalActivityVO institutionalActivity(Long stkUniCode) { StockVO stock = getStock(stkUniCode); if (stock == null) { return null; } String secCode = stock.getSecCode(); Integer mktNum = stock.getMktNum(); Integer secMarPar = stock.getSecMarPar(); Long secUniCode = stock.getSecUniCode(); String tableName = stock.getPriceTableName(); InstitutionalActivityVO vo = diagnoseMapper.selectInstitutionalActivity(secCode, mktNum); if (vo == null) { return null; } List institutionalFundFlowList = stockMapper.selectInstitutionalFundFlow(tableName, secUniCode, secCode, secMarPar); vo.setInstitutionalFundFlowList(institutionalFundFlowList); List shareholderList = stockMapper.selectShareholderList(secCode, secMarPar); vo.setShareholderList(shareholderList); return vo; } }