package com.xcong.excoin.quartz.job; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; import com.xcong.excoin.modules.contract.dao.ContractOrderDao; import com.xcong.excoin.modules.contract.entity.ContractOrderEntity; import com.xcong.excoin.modules.documentary.dao.FollowFollowerOrderRelationDao; import com.xcong.excoin.modules.documentary.dao.FollowFollowerProfitDao; import com.xcong.excoin.modules.documentary.dao.FollowTraderInfoDao; import com.xcong.excoin.modules.documentary.dao.FollowTraderProfitInfoDao; import com.xcong.excoin.modules.documentary.entity.FollowFollowerProfitEntity; import com.xcong.excoin.modules.documentary.entity.FollowTraderInfoEntity; import com.xcong.excoin.modules.documentary.entity.FollowTraderProfitInfoEntity; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.math.BigDecimal; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; /** * * * @author wzy * @date 2020-08-06 **/ @Slf4j @Component @ConditionalOnProperty(prefix = "app", name = "loop-job", havingValue = "true") public class FollowProfitUpdateJob { @Resource private FollowTraderInfoDao followTraderInfoDao; @Resource private FollowFollowerProfitDao followFollowerProfitDao; @Resource private ContractOrderDao contractOrderDao; @Resource private FollowFollowerOrderRelationDao followFollowerOrderRelationDao; @Resource private FollowTraderProfitInfoDao followTraderProfitInfoDao; //@Scheduled(cron = "0 0/30 * * * ? ") @Scheduled(cron = "0/10 * * * * ?") public void traderProfitUpdate() { // 查询所有交易员信息 List allTraders = followTraderInfoDao.selectAllTraderInfo(); if (CollUtil.isNotEmpty(allTraders)) { for (FollowTraderInfoEntity trader : allTraders) { Long tradeMemberId = trader.getMemberId(); //获取交易员的当前跟随者 Map hashMap = new HashMap<>(); hashMap.put("trade_member_id", tradeMemberId); hashMap.put("is_follow", FollowFollowerProfitEntity.IS_FOLLOW_Y); List followFollowerProfitEntityList = followFollowerProfitDao.selectByMap(hashMap); if(CollUtil.isNotEmpty(followFollowerProfitEntityList)) { for(FollowFollowerProfitEntity followFollowerProfitEntity : followFollowerProfitEntityList) { //获取当前跟随者的跟随本金 Long memberId = followFollowerProfitEntity.getMemberId(); BigDecimal sumBondAmountBigDecimal = followFollowerProfitDao.selectSumBondAmountBymemberId(memberId,trader.getId()); sumBondAmountBigDecimal = (sumBondAmountBigDecimal == null?BigDecimal.ZERO:sumBondAmountBigDecimal.setScale(2, BigDecimal.ROUND_DOWN)); followFollowerProfitEntity.setTotalPrincipal(sumBondAmountBigDecimal); //获取当前的盈亏 BigDecimal sumRewardAmountBigDecimal = followFollowerProfitDao.selectSumRewardAmountByMemberId(memberId,trader.getId()); sumRewardAmountBigDecimal = (sumRewardAmountBigDecimal == null?BigDecimal.ZERO:sumRewardAmountBigDecimal.setScale(2, BigDecimal.ROUND_DOWN)); followFollowerProfitEntity.setTotalProfit(sumRewardAmountBigDecimal); followFollowerProfitDao.updateById(followFollowerProfitEntity); } } FollowTraderProfitInfoEntity traderInfoProfit = followTraderProfitInfoDao.selectTraderInfoProfitByMemberId(tradeMemberId); // 累计收益率 BigDecimal ljsyl = contractOrderDao.selectFollowOrderTotalProfitByMemberId(tradeMemberId); BigDecimal totalProfitRatio = (ljsyl == null?BigDecimal.ZERO:ljsyl.setScale(2, BigDecimal.ROUND_DOWN)); traderInfoProfit.setTotalProfitRatio(totalProfitRatio); // 带单总收益,只查询交易员自己的带单总收益 BigDecimal totalProfit = followFollowerOrderRelationDao.selectTraderTotalProfitSelf(tradeMemberId); //BigDecimal totalProfit = followFollowerOrderRelationDao.selectTraderTotalProfit(tradeMemberId); traderInfoProfit.setTotalProfit(totalProfit); // 交易笔数 // List orders = contractOrderDao.selectFollowOrderByMemberId(tradeMemberId); List orders = contractOrderDao.selectFollowOrderListByMemberId(tradeMemberId); traderInfoProfit.setTotalOrderCnt(CollUtil.isNotEmpty(orders) ? orders.size() : 0); // 近三周胜率 BigDecimal winCnt = contractOrderDao.selectFollowOrderCntForWinRate(tradeMemberId, 1); winCnt = (winCnt == null?BigDecimal.ZERO:winCnt.setScale(2, BigDecimal.ROUND_DOWN)); BigDecimal allCnt = contractOrderDao.selectFollowOrderCntForWinRate(tradeMemberId, null); allCnt = (allCnt == null?BigDecimal.ZERO:allCnt.setScale(2, BigDecimal.ROUND_DOWN)); BigDecimal winRate = BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_DOWN); if (allCnt.compareTo(BigDecimal.ZERO) > 0) { winRate = winCnt.divide(allCnt, 2, BigDecimal.ROUND_DOWN).setScale(2, BigDecimal.ROUND_DOWN); } traderInfoProfit.setWinRate(winRate); Date date = new Date(); DateTime offsetDay = DateUtil.offsetDay(new Date(), -30); //30天胜率(30天盈利总单数/30平仓总单数) BigDecimal thirtyTotalCnt = contractOrderDao.selectThirtyTotalCntByMemberId(tradeMemberId,date,offsetDay); BigDecimal thirtyWinCnt = contractOrderDao.selectThirtyWinCntByMemberId(tradeMemberId,date,offsetDay); BigDecimal thirtyTotalCntRatio = (thirtyTotalCnt == null?BigDecimal.ZERO:thirtyTotalCnt.setScale(2, BigDecimal.ROUND_DOWN)); BigDecimal thirtyWinCntRatio = (thirtyWinCnt == null?BigDecimal.ZERO:thirtyWinCnt.setScale(2, BigDecimal.ROUND_DOWN)); BigDecimal thirtyProfitRatio = BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_DOWN);; if(thirtyTotalCnt.compareTo(BigDecimal.ZERO) > 0) { thirtyProfitRatio = thirtyWinCntRatio.divide(thirtyTotalCntRatio, 2, BigDecimal.ROUND_DOWN).setScale(2, BigDecimal.ROUND_DOWN); } traderInfoProfit.setThirtyProfitRatio(thirtyProfitRatio); //30天最大回撤率(30天最高收益率-30天最低收益率) BigDecimal thirtyMaxRatio = contractOrderDao.selectThirtyMaxRatioByMemberId(tradeMemberId,date,offsetDay); BigDecimal thirtyMinRatio = contractOrderDao.selectThirtyMinRatioByMemberId(tradeMemberId,date,offsetDay); BigDecimal thirtyRatio = (thirtyMaxRatio == null?BigDecimal.ZERO:thirtyMaxRatio.setScale(2, BigDecimal.ROUND_DOWN)) .subtract((thirtyMinRatio == null?BigDecimal.ZERO:thirtyMinRatio.setScale(2, BigDecimal.ROUND_DOWN))); traderInfoProfit.setThirtyRatio(thirtyRatio); // 当前跟随者总收益 BigDecimal followerProfit = followFollowerProfitDao.selectAllFollowerProfit(tradeMemberId); traderInfoProfit.setFollowerTotalProfit(followerProfit); // 当前跟随人数 int followerCnt = followFollowerProfitDao.selectFollowerCntByTradeMemberId(tradeMemberId); traderInfoProfit.setTotalFollowerCnt(followerCnt); followTraderProfitInfoDao.updateById(traderInfoProfit); } } } }