package com.pingan.rbpfundval3.service;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.pingan.rbpfundval3.dao.FundReportDao;
import com.pingan.rbpfundval3.model.FundRealtimeValuationSeason;
import com.pingan.rbpfundval3.model.FundValuationSeasonTime;
import com.pingan.rbpfundval3.model.KlCihdquote;
import com.pingan.rbpfundval3.model.KlNav;
import com.pingan.rbpfundval3.model.KlSholding;
import com.pingan.rbpfundval3.model.TradeDay;
public class Process3 {
private static Logger logger = LoggerFactory.getLogger(Process3.class);
private static SqlSession session;
private static FundReportDao fundReportDao;
private static String resource = "mybatis-config.xml";
private static Map<String, List<TradeDay>> timelistMap = new HashMap<String, List<TradeDay>>();
private static Map<String, String> yesterlistMap = new HashMap<String,String>();
//检测有无更新数据
private static List<KlSholding> fvstlist = new ArrayList<KlSholding>();
//当前基金详情
private static List<KlSholding> KlSholdingList = null;
static {
try {
Properties properties = new Properties();
//测试
String userPath = System.getProperty("user.home");
userPath = userPath + "/java/fundvaluation2/";
//本地
//String userPath = "D:\\fundValuation3-1\\fundValuation3\\src\\";
String appFilePath = userPath + "application.properties";
logger.info("user.home:" + userPath);
logger.info("appFilePath:" + appFilePath);
BufferedReader bufferedReader = new BufferedReader(new FileReader(appFilePath));
properties.load(bufferedReader);
} catch (IOException e) {
logger.error(e.getMessage());
}
}
public static void main(String[] args) throws IOException {
init();
releaseResource();
}
private static void init() throws IOException {
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
session = sqlSessionFactory.openSession();
fundReportDao = session.getMapper(FundReportDao.class);
check4UpdateDate();
}
//检测数据
private static void check4UpdateDate(){
fvstlist = fundReportDao.checkInfo();
if(fvstlist == null && fvstlist.size() < 0){
logger.info("No new record of season report.");
}else{
checkQualification(fvstlist);
}
}
//检测资格
private static void checkQualification(List<KlSholding> fvstlist){
BigDecimal num = new BigDecimal("100");
BigDecimal num1 = new BigDecimal("1");
List<TradeDay> dateList = new ArrayList<TradeDay>();
String yesterday = new String();
List<FundValuationSeasonTime> fvstList = new ArrayList<FundValuationSeasonTime>();
List<FundRealtimeValuationSeason> frvsList = new ArrayList<FundRealtimeValuationSeason>();
logger.info("total count is "+ fvstlist.size());
for(int i = 0;i<fvstlist.size();i++){
Double nav = null;
Double nav1 = null;
String symbol = fvstlist.get(i).getSymbol();
String reportDate = fvstlist.get(i).getReportdate();
String publishDate = fvstlist.get(i).getPublishdate();
FundValuationSeasonTime fundValuationSeasonTime = new FundValuationSeasonTime();
fundValuationSeasonTime.setSymbol(symbol);
fundValuationSeasonTime.setReportDate(reportDate);
fundValuationSeasonTime.setPublishDate(publishDate);
fundValuationSeasonTime.setCreateTime(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
fundValuationSeasonTime.setStatus("0");
logger.info("symbol="+ symbol + ", reportDate=" + reportDate + ", publishDate=" + publishDate+" --- "+(i+1)+" of "+ fvstlist.size());
String sum = fundReportDao.countSholding7(symbol,reportDate);
if(sum != null){
double sumSholding7 = Double.valueOf(sum);
if(sumSholding7 > 40){
BigDecimal expanded = num.divide(BigDecimal.valueOf(sumSholding7), 4, BigDecimal.ROUND_HALF_UP);
if(timelistMap.containsKey(reportDate)){
dateList = timelistMap.get(reportDate);
}else{
dateList = findRound4ReportDate(reportDate);
timelistMap.put(reportDate, dateList);
}
for(int j = 0;j<dateList.size();j++){
String presentday = String.valueOf(dateList.get(j).gettDate());
if(yesterlistMap.containsKey(presentday)){
yesterday = yesterlistMap.get(presentday);
}else{
yesterday = fundReportDao.getPreTradeDay(dateList.get(j).gettDate());
yesterlistMap.put(presentday, yesterday);
}
KlNav klNav1 = fundReportDao.selectNav(symbol,presentday);
KlNav klNav = fundReportDao.selectNav(symbol,yesterday);
if(klNav != null && klNav1 != null ){
if(klNav.getNav1() != null && !"".equals(klNav.getNav1())){
BigDecimal retsum = new BigDecimal("0");
nav = Double.valueOf(String.valueOf(klNav.getNav1()));
nav1 = Double.valueOf(String.valueOf(klNav1.getNav1()));
KlSholdingList = findklSholdingInfo(symbol, reportDate, publishDate);
for(int k = 0;k<KlSholdingList.size();k++){
Double sholding = Double.valueOf(String.valueOf(KlSholdingList.get(k).getSholding7()));
BigDecimal sholding7 = BigDecimal.valueOf(sholding).divide(num);
String esymbol = KlSholdingList.get(k).getEsymbol();
KlCihdquote presentklCihdquote = findLclose(esymbol,presentday);
KlCihdquote yesterklCihdquote = findLclose(esymbol,yesterday);
if(presentklCihdquote != null && yesterklCihdquote!= null){
BigDecimal presentLclose = presentklCihdquote.getLclose();
BigDecimal yesterLclose = yesterklCihdquote.getLclose();
//(当日收盘-昨日收盘)/昨日收盘 *持仓占比*扩大系数
BigDecimal ret = (presentLclose.subtract(yesterLclose)).divide(yesterLclose, 4, BigDecimal.ROUND_HALF_UP)
.multiply(sholding7).multiply(expanded);
retsum = retsum.add(ret);
}else{
continue;
}
}
//(retsum+1)*nav1
BigDecimal fv = (retsum.add(num1)).multiply(BigDecimal.valueOf(nav)).setScale(4,BigDecimal.ROUND_HALF_UP);
//(val_value – nav)/nav devi_percent
BigDecimal percent = (fv.subtract(BigDecimal.valueOf(nav1))).divide(BigDecimal.valueOf(nav1), 4, BigDecimal.ROUND_HALF_UP);
Double fundValuation = Double.valueOf(String.valueOf(fv));
Double deviPercent = Double.valueOf(String.valueOf(percent));
FundRealtimeValuationSeason fundRealtimeValuationSeason = new FundRealtimeValuationSeason();
fundRealtimeValuationSeason.setFundId(symbol);
fundRealtimeValuationSeason.setReportDate(reportDate);
fundRealtimeValuationSeason.setPublishDate(publishDate);
fundRealtimeValuationSeason.setValDate(presentday);
fundRealtimeValuationSeason.setValValue(fundValuation);
fundRealtimeValuationSeason.setNav(nav1);
fundRealtimeValuationSeason.setDeviPercent(deviPercent);
fundRealtimeValuationSeason.setCreateTime(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
frvsList.add(fundRealtimeValuationSeason);
}
}
}
if(nav != null){
fundValuationSeasonTime.setIsQulified("1");
}else{
fundValuationSeasonTime.setIsQulified("0");
}
}else{
fundValuationSeasonTime.setIsQulified("0");
}
}else{
fundValuationSeasonTime.setIsQulified("0");
}
fvstList.add(fundValuationSeasonTime);
}
if(frvsList != null && frvsList.size()>0){
batchInsertValuation(frvsList,2000);
logger.info("Valuation size is "+ frvsList.size());
}
if(fvstList != null && fvstList.size()>0){
batchInsertValuationSeasonTime(fvstList,2000);
logger.info("ValuationSeasonTime size is "+ fvstList.size());
}
}
private static void batchInsertValuation(List<FundRealtimeValuationSeason> frvsList, int batchCount) {
int count = frvsList.size() / batchCount;
int mode = frvsList.size() % batchCount;
int i = 0;
for (; i < count; i++) {
fundReportDao.insertFundRealtimeValuationSeason(frvsList.subList(i * batchCount, (i + 1) * batchCount));
session.commit();
}
if (mode > 0) {
fundReportDao.insertFundRealtimeValuationSeason(frvsList.subList(i * batchCount, frvsList.size()));
session.commit();
}
}
private static void batchInsertValuationSeasonTime(List<FundValuationSeasonTime> fvstList, int batchCount) {
int count = fvstList.size() / batchCount;
int mode = fvstList.size() % batchCount;
int i = 0;
for (; i < count; i++) {
fundReportDao.insertFundValuationSeasonTime(fvstList.subList(i * batchCount, (i + 1) * batchCount));
session.commit();
}
if (mode > 0) {
fundReportDao.insertFundValuationSeasonTime(fvstList.subList(i * batchCount, fvstList.size()));
session.commit();
}
}
private static KlCihdquote findLclose(String esymbol, String tdate){
KlCihdquote klCihdquote = fundReportDao.findKlCihdquoteInfo(esymbol, tdate);
return klCihdquote;
}
//查询reportdate 前后15天
private static List<TradeDay> findRound4ReportDate(String reportDate){
List<TradeDay> dateList = fundReportDao.getRound30PreTradeDay(reportDate);
return dateList;
}
//查询当前基金下所有的股票详情
private static List<KlSholding> findklSholdingInfo(String symbol, String reportDate,String publishDate){
List<KlSholding> KlSholdingList= fundReportDao.selectSholdingInfo(symbol, reportDate, publishDate);
return KlSholdingList;
}
private static void releaseResource() {
session.close();
}
}
转载于:https://blog.51cto.com/7952376/2366319