Helius
2021-04-06 e94d7f8203bc1a6f7e1d8d7585fbd8e9821160ee
Merge branch 'activity' of http://120.27.238.55:7000/r/exchange into activity
1 files added
3 files modified
202 ■■■■■ changed files
src/main/java/com/xcong/excoin/modules/contract/dao/ContractOrderDao.java 2 ●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/quartz/job/FollowProfitUpdateJob.java 16 ●●●●● patch | view | raw | blame | history
src/main/resources/mapper/contract/ContractOrderDao.xml 10 ●●●●● patch | view | raw | blame | history
src/test/java/com/xcong/excoin/LocalTest.java 174 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/contract/dao/ContractOrderDao.java
@@ -28,7 +28,7 @@
    BigDecimal selectFollowOrderTotalProfitByMemberId(@Param("memberId") Long memberId);
    Integer selectFollowOrderCntForWinRate(@Param("memberId") Long memberId, @Param("type") Integer type);
    BigDecimal selectFollowOrderCntForWinRate(@Param("memberId") Long memberId, @Param("type") Integer type);
    List<ContractOrderEntity> selectWholeOpenOrderByOrderNo(@Param("orderNo") String orderNo);
src/main/java/com/xcong/excoin/quartz/job/FollowProfitUpdateJob.java
@@ -93,16 +93,18 @@
                List<ContractOrderEntity> orders = contractOrderDao.selectFollowOrderListByMemberId(tradeMemberId);
                traderInfoProfit.setTotalOrderCnt(CollUtil.isNotEmpty(orders) ? orders.size() : 0);
                // 近三周胜率
                Integer winCnt = contractOrderDao.selectFollowOrderCntForWinRate(tradeMemberId, 1);
                Integer allCnt = contractOrderDao.selectFollowOrderCntForWinRate(tradeMemberId, null);
                if (winCnt != null && allCnt != null && allCnt!=0) {
                    BigDecimal winRate = BigDecimal.valueOf(winCnt).divide(BigDecimal.valueOf(allCnt), 4, BigDecimal.ROUND_DOWN);
                    traderInfoProfit.setWinRate(winRate);
                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平仓总张数)
                //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));
src/main/resources/mapper/contract/ContractOrderDao.xml
@@ -84,11 +84,12 @@
        where member_id=#{memberId} and contract_type=2
    </select>
    <select id="selectFollowOrderCntForWinRate" resultType="java.lang.Integer">
    <select id="selectFollowOrderCntForWinRate" resultType="java.math.BigDecimal">
        select count(1)
        from contract_order a
        where member_id=#{memberId}
        and contract_type=2 and order_type in (3,4)
        and contract_type=2
          and closing_time is not null
        <if test="type!=null and type==1">
            and reward_amount > 0
        </if>
@@ -128,16 +129,17 @@
    </select>
    <select id="selectThirtyTotalCntByMemberId" resultType="java.math.BigDecimal">
        select sum(symbol_cnt)/2
        select count(id)
        from contract_order
        where
              member_id=#{memberId}
          and contract_type=2
          and closing_time is not null
          and create_time BETWEEN #{offsetDay} and #{date}
    </select>
    <select id="selectThirtyWinCntByMemberId" resultType="java.math.BigDecimal">
        select sum(symbol_cnt)/2
        select count(id)
        from contract_order
        where
        member_id=#{memberId}
src/test/java/com/xcong/excoin/LocalTest.java
New file
@@ -0,0 +1,174 @@
package com.xcong.excoin;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.xcong.excoin.modules.contract.entity.ContractOrderEntity;
import org.springframework.boot.test.context.SpringBootTest;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xcong.excoin.common.LoginUserUtils;
import com.xcong.excoin.common.response.Result;
import com.xcong.excoin.modules.coin.dao.OrderCoinsDao;
import com.xcong.excoin.modules.contract.dao.ContractHoldOrderDao;
import com.xcong.excoin.modules.contract.dao.ContractOrderDao;
import com.xcong.excoin.modules.documentary.dao.FollowFollowerNoticeDao;
import com.xcong.excoin.modules.documentary.dao.FollowFollowerOrderRelationDao;
import com.xcong.excoin.modules.documentary.dao.FollowFollowerProfitDao;
import com.xcong.excoin.modules.documentary.dao.FollowFollowerSettingDao;
import com.xcong.excoin.modules.documentary.dao.FollowTraderInfoDao;
import com.xcong.excoin.modules.documentary.dao.FollowTraderLabelDao;
import com.xcong.excoin.modules.documentary.dao.FollowTraderProfitDetailDao;
import com.xcong.excoin.modules.documentary.dao.FollowTraderProfitInfoDao;
import com.xcong.excoin.modules.documentary.dto.FollowTraderProfitInfoDto;
import com.xcong.excoin.modules.documentary.entity.FollowFollowerProfitEntity;
import com.xcong.excoin.modules.documentary.entity.FollowFollowerSettingEntity;
import com.xcong.excoin.modules.documentary.entity.FollowTraderInfoEntity;
import com.xcong.excoin.modules.documentary.entity.FollowTraderLabelEntity;
import com.xcong.excoin.modules.documentary.entity.FollowTraderProfitInfoEntity;
import com.xcong.excoin.modules.documentary.vo.FollowTraderProfitInfoVo;
import com.xcong.excoin.modules.documentary.vo.TradeProfitInfoVo;
import com.xcong.excoin.modules.member.dao.MemberDao;
import com.xcong.excoin.modules.member.dao.MemberLevelRateDao;
import com.xcong.excoin.modules.member.dao.MemberSettingDao;
import com.xcong.excoin.modules.member.dao.MemberWalletContractDao;
import com.xcong.excoin.modules.member.entity.MemberEntity;
import com.xcong.excoin.utils.CacheSettingUtils;
import com.xcong.excoin.utils.RedisUtils;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
@Slf4j
@SpringBootTest
public class LocalTest {
    @Resource
    private MemberDao memberDao;
    @Resource
    private FollowTraderInfoDao followTraderInfoDao;
    @Resource
    private FollowTraderProfitDetailDao followTraderProfitDetailDao;
    @Resource
    private FollowTraderProfitInfoDao followTraderProfitInfoDao;
    @Resource
    private FollowFollowerProfitDao followFollowerProfitDao;
    @Resource
    private OrderCoinsDao orderCoinsDao;
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private CacheSettingUtils cacheSettingUtils;
    @Resource
    private ContractHoldOrderDao contractHoldOrderDao;
    @Resource
    private ContractOrderDao contractOrderDao;
    @Resource
    private MemberLevelRateDao memberLevelRateDao;
    @Resource
    private MemberWalletContractDao memberWalletContractDao;
    @Resource
    private FollowFollowerSettingDao followFollowerSettingDao;
    @Resource
    private FollowFollowerOrderRelationDao followFollowerOrderRelationDao;
    @Resource
    private FollowFollowerNoticeDao followFollowerNoticeDao;
    @Resource
    private FollowTraderLabelDao followTraderLabelDao;
    @Resource
    private MemberSettingDao memberSettingDao;
    @Test
    public void traderProfitUpdate() {
        log.info("交易员定时任务执行");
        // 查询所有交易员信息
        List<FollowTraderInfoEntity> allTraders = followTraderInfoDao.selectAllTraderInfo();
        if (CollUtil.isNotEmpty(allTraders)) {
            for (FollowTraderInfoEntity trader : allTraders) {
                Long tradeMemberId = trader.getMemberId();
                //获取交易员的当前跟随者
                Map<String, Object> hashMap = new HashMap<>();
                hashMap.put("trade_member_id", tradeMemberId);
                hashMap.put("is_follow", FollowFollowerProfitEntity.IS_FOLLOW_Y);
                List<FollowFollowerProfitEntity> 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<ContractOrderEntity> orders = contractOrderDao.selectFollowOrderByMemberId(tradeMemberId);
                List<ContractOrderEntity> orders = contractOrderDao.selectFollowOrderListByMemberId(tradeMemberId);
                traderInfoProfit.setTotalOrderCnt(CollUtil.isNotEmpty(orders) ? orders.size() : 0);
                // 近三周胜率
                Integer winCnt = contractOrderDao.selectFollowOrderCntForWinRate(tradeMemberId, 1);
                Integer allCnt = contractOrderDao.selectFollowOrderCntForWinRate(tradeMemberId, null);
                if (winCnt != null && allCnt != null && allCnt!=0) {
                    BigDecimal winRate = BigDecimal.valueOf(winCnt).divide(BigDecimal.valueOf(allCnt), 4, 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);
            }
        }
    }
}