Helius
2020-08-07 3b31e14e178f73fdb2605436cbd5f544ddc2de02
modify
13 files modified
151 ■■■■■ changed files
src/main/java/com/xcong/excoin/modules/contract/dao/ContractOrderDao.java 9 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/documentary/dao/FollowFollowerOrderRelationDao.java 3 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/documentary/dao/FollowFollowerProfitDao.java 4 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/documentary/dao/FollowTraderInfoDao.java 3 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/documentary/dao/FollowTraderProfitInfoDao.java 1 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/documentary/entity/FollowTraderProfitInfoEntity.java 4 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/quartz/job/FollowProfitUpdateJob.java 64 ●●●●● patch | view | raw | blame | history
src/main/resources/mapper/contract/ContractOrderDao.xml 24 ●●●●● patch | view | raw | blame | history
src/main/resources/mapper/documentary/FollowFollowerOrderRelationDao.xml 7 ●●●● patch | view | raw | blame | history
src/main/resources/mapper/documentary/FollowFollowerProfitDao.xml 11 ●●●●● patch | view | raw | blame | history
src/main/resources/mapper/documentary/FollowTraderInfoDao.xml 6 ●●●●● patch | view | raw | blame | history
src/main/resources/mapper/documentary/FollowTraderProfitInfoDao.xml 6 ●●●● patch | view | raw | blame | history
src/test/java/com/xcong/excoin/FollowTest.java 9 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/contract/dao/ContractOrderDao.java
@@ -8,6 +8,9 @@
import com.xcong.excoin.modules.contract.parameter.vo.OrderListVo;
import org.apache.ibatis.annotations.Param;
import java.math.BigDecimal;
import java.util.List;
/**
 * @author helius
 */
@@ -16,4 +19,10 @@
    public IPage<ContractOrderEntity> selectContractOrderInPage(Page<ContractOrderEntity> page, @Param("record") ContractOrderEntity contractOrderEntity);
    public ContractOrderEntity selectOrderDetailByIdAndMemberId(@Param("id") Long id, @Param("memberId") Long memberId);
    List<ContractOrderEntity> selectFollowOrderByMemberId(@Param("memberId") Long memberId);
    BigDecimal selectFollowOrderTotalProfitByMemberId(@Param("memberId") Long memberId);
    Integer selectFollowOrderCntForWinRate(@Param("memberId") Long memberId, @Param("type") Integer type);
}
src/main/java/com/xcong/excoin/modules/documentary/dao/FollowFollowerOrderRelationDao.java
@@ -4,6 +4,7 @@
import com.xcong.excoin.modules.documentary.entity.FollowFollowerOrderRelationEntity;
import org.apache.ibatis.annotations.Param;
import java.math.BigDecimal;
import java.util.List;
/**
@@ -17,4 +18,6 @@
    FollowFollowerOrderRelationEntity selectNowOneByorderId(@Param("orderId")Long orderId);
    List<FollowFollowerOrderRelationEntity> selectFollowHoldOrderByTradeOrderNo(@Param("orderNo") String orderNo);
    BigDecimal selectTraderTotalProfit(@Param("memberId") Long memberId);
}
src/main/java/com/xcong/excoin/modules/documentary/dao/FollowFollowerProfitDao.java
@@ -38,5 +38,9 @@
            @Param("memberId")Long memberId);
    int updateFollowerProfitByTradeMemberId(@Param("principal") BigDecimal principal, @Param("profit") BigDecimal profit, @Param("tradeMemberId") Long tradeMemberId, @Param("memberId") Long memberId);
    int selectFollowerCntByTradeMemberId(@Param("tradeMemberId") Long tradeMemberId);
    BigDecimal selectAllFollowerProfit(@Param("tradeMemberId") Long tradeMemberId);
    
}
src/main/java/com/xcong/excoin/modules/documentary/dao/FollowTraderInfoDao.java
@@ -4,6 +4,8 @@
import com.xcong.excoin.modules.documentary.entity.FollowTraderInfoEntity;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface FollowTraderInfoDao extends BaseMapper<FollowTraderInfoEntity> {
    FollowTraderInfoEntity selectFollowTraderInfoEntityBytreaderId(@Param("traderId")Long traderId);
@@ -12,4 +14,5 @@
    FollowTraderInfoEntity selectTraderInfoByOrderId(@Param("orderId") Long orderId);
    List<FollowTraderInfoEntity> selectAllTraderInfo();
}
src/main/java/com/xcong/excoin/modules/documentary/dao/FollowTraderProfitInfoDao.java
@@ -19,4 +19,5 @@
    FollowTraderProfitInfoVo selectOneByMemberId(@Param("memberId")long memberId);
    FollowTraderProfitInfoEntity selectTraderInfoProfitByMemberId(@Param("memberId") Long memberId);
}
src/main/java/com/xcong/excoin/modules/documentary/entity/FollowTraderProfitInfoEntity.java
@@ -47,11 +47,11 @@
    /**
     * 累计跟随人数
     */
    private BigDecimal totalFollowerCnt;
    private int totalFollowerCnt;
    /**
     * 交易笔数
     */
    private BigDecimal totalOrderCnt;
    private int totalOrderCnt;
    
}
src/main/java/com/xcong/excoin/quartz/job/FollowProfitUpdateJob.java
@@ -1,6 +1,23 @@
package com.xcong.excoin.quartz.job;
import cn.hutool.core.collection.CollUtil;
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.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.List;
/**
 *
@@ -8,11 +25,58 @@
 * @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 * * * ? ")
    public void traderProfitUpdate() {
        log.info("交易员定时任务执行");
        // 查询所有交易员信息
        List<FollowTraderInfoEntity> allTraders = followTraderInfoDao.selectAllTraderInfo();
        if (CollUtil.isNotEmpty(allTraders)) {
            for (FollowTraderInfoEntity trader : allTraders) {
                Long tradeMemberId = trader.getMemberId();
                FollowTraderProfitInfoEntity traderInfoProfit = followTraderProfitInfoDao.selectTraderInfoProfitByMemberId(tradeMemberId);
                // 累计收益率
                BigDecimal totalProfitRatio = contractOrderDao.selectFollowOrderTotalProfitByMemberId(tradeMemberId);
                traderInfoProfit.setTotalProfitRatio(totalProfitRatio);
                // 带单总收益
                BigDecimal totalProfit = followFollowerOrderRelationDao.selectTraderTotalProfit(tradeMemberId);
                traderInfoProfit.setTotalProfit(totalProfit);
                // 交易笔数
                List<ContractOrderEntity> orders = contractOrderDao.selectFollowOrderByMemberId(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 followerProfit = followFollowerProfitDao.selectAllFollowerProfit(tradeMemberId);
                traderInfoProfit.setFollowerTotalProfit(followerProfit);
                // 累计跟随人数
                int followerCnt = followFollowerProfitDao.selectFollowerCntByTradeMemberId(tradeMemberId);
                traderInfoProfit.setTotalFollowerCnt(followerCnt);
                followTraderProfitInfoDao.updateById(traderInfoProfit);
            }
        }
    }
}
src/main/resources/mapper/contract/ContractOrderDao.xml
@@ -53,4 +53,28 @@
        select * from contract_order where id=#{id} and member_id=#{memberId}
    </select>
    <select id="selectFollowOrderByMemberId" resultType="com.xcong.excoin.modules.contract.entity.ContractOrderEntity">
        select *
        from contract_order a, follow_follower_order_relation b
        where a.id=b.order_id and a.member_id=#{memberId}
    </select>
    <select id="selectFollowOrderTotalProfitByMemberId" resultType="java.math.BigDecimal">
        select sum(reward_ratio)*100
        from contract_order
        where member_id=#{memberId} and contract_type=2
    </select>
    <select id="selectFollowOrderCntForWinRate" resultType="java.lang.Integer">
        select count(1)
        from contract_order a
        where member_id=#{memberId}
        and contract_type=2 and order_type in (3,4)
        <if test="type!=null and type==1">
            and reward_amount > 0
        </if>
        <if test="type!=null and type==2">
            and 0 > reward_amount
        </if>
    </select>
</mapper>
src/main/resources/mapper/documentary/FollowFollowerOrderRelationDao.xml
@@ -32,5 +32,10 @@
        where a.trade_member_id=b.trade_member_id and a.trade_order_no=#{orderNo} and b.is_follow=1
        and a.order_type=1
    </select>
    <select id="selectTraderTotalProfit" resultType="java.math.BigDecimal">
        select sum(b.reward_amount)
        from follow_follower_order_relation a, contract_order b
        where a.order_id=b.id and a.trade_member_id=#{memberId}
    </select>
</mapper>
src/main/resources/mapper/documentary/FollowFollowerProfitDao.xml
@@ -90,4 +90,15 @@
        total_profit=total_profit+#{profit}
        where trade_member_id=#{tradeMemberId} and member_id=#{memberId}
    </update>
    <select id="selectFollowerCntByTradeMemberId" resultType="java.lang.Integer">
        select count(1) from follow_follower_profit
        where trade_member_id=#{tradeMemberId}
    </select>
    <select id="selectAllFollowerProfit" resultType="java.math.BigDecimal">
        select sum(total_profit)
        from follow_follower_profit
        where trade_member_id=#{tradeMemberId}
    </select>
</mapper>
src/main/resources/mapper/documentary/FollowTraderInfoDao.xml
@@ -17,4 +17,10 @@
        from follow_trader_info a, follow_follower_order_relation b
        where a.member_id=b.trade_member_id and b.order_id=#{orderId}
    </select>
    <select id="selectAllTraderInfo" resultType="com.xcong.excoin.modules.documentary.entity.FollowTraderInfoEntity">
        select *
        from follow_trader_info
        where verify_status=1
    </select>
</mapper>
src/main/resources/mapper/documentary/FollowTraderProfitInfoDao.xml
@@ -32,5 +32,9 @@
            and closing_type not in (4,5)
        order by opening_time desc
    </select>
    <select id="selectTraderInfoProfitByMemberId" resultType="com.xcong.excoin.modules.documentary.entity.FollowTraderProfitInfoEntity">
        select * from follow_trader_profit_info
        where member_id=#{memberId}
    </select>
</mapper>
src/test/java/com/xcong/excoin/FollowTest.java
@@ -1,6 +1,7 @@
package com.xcong.excoin;
import com.xcong.excoin.modules.documentary.service.FollowOrderOperationService;
import com.xcong.excoin.quartz.job.FollowProfitUpdateJob;
import com.xcong.excoin.utils.ThreadPoolUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
@@ -33,4 +34,12 @@
        System.out.println(symbols.contains(symbol.replace("/USDT", "")));
    }
    @Resource
    private FollowProfitUpdateJob followProfitUpdateJob;
    @Test
    public void updateJobTest() {
        followProfitUpdateJob.traderProfitUpdate();
    }
}