java8 集合嵌套性能流处理快的一笔

原创
2019/04/14 02:35
阅读数 44

 

        BigDecimal rate = nod.getValue();//普通用户比例
        BigDecimal rated = d.getValue();// done比例
        int index = 0;
        int count = total%10000==0?total / 10000 :total/10000+ 1;
        int size = 10000;

        while (index < count) {
            int start = index*size;
            // 查询所有团队信息
            List<IncomeCoinDTO> runs = incomeCoinService.selectRewardList(maxId, begin, end, start, size);//Collectors.reducing(BigDecimal.ZERO,BigDecimal::add)
            if (runs == null || runs.isEmpty()) {
                return;
            }


            List<Integer> tids = runs.stream().map(IncomeCoinDTO::getParentTeamId).distinct().collect(Collectors.toList());
            List<TeamUserDTO> teams = teamService.selectParentTeamList(tids);
//此处关键
            Map<Integer, TeamUserDTO> teamMap = teams.stream().collect(
                    Collectors.toMap(t -> t.getTeamId(),
                            t -> t));
            List<IncomeCoinDTO> list = runs.stream().map(r -> {
                return convert(r, teamMap.get(r.getParentTeamId()), rated, rate);
            }).filter(o->o!=null).collect(Collectors.toList());
//注意去空
            list.stream().sorted(Comparator.comparing(IncomeCoinDTO::getSportCoin));
            incomeCoinService.insertList(runs);
            Map<Long, IncomeCoinDTO> map = list.stream().collect(Collectors.toMap(IncomeCoinDTO::getUid, Function.identity(), IncomeCoinDTO.convert));
//累计加加
            Set<Long> keys = map.keySet();
            List<Long> uids = new ArrayList<>(keys);
            this.executeReward(uids, map);

            index++;
        }






   private IncomeCoinDTO convert(IncomeCoinDTO coin, TeamUserDTO team, BigDecimal rated, BigDecimal rate) {
        if (team == null) return null;
        coin.setType(11);
        coin.setStatu(1);
        coin.setDistance(BigDecimal.ZERO);
        coin.setDone(team.getDone());
        coin.setPhone(team.getMobile());
        coin.setUid(team.getUid());
        if (team.getDone() > 0) {
            coin.setSportCoin(BigDecimalUtil.multiply(coin.getSportCoin(), rated, 5));
        } else {
            coin.setSportCoin(BigDecimalUtil.multiply(coin.getSportCoin(), rate, 5));
        }
        return coin;
    }


public class IncomeCoinDTO implements Serializable {
    private static final long serialVersionUID = 1L;

    private Integer id;
    private String tradeNo;
    private Long uid;
    private String phone;
    private BigDecimal sportCoin;
    private BigDecimal distance;
    private Long inviteUid;
    private String invitePhone;
    private String inviteName;
    private Integer type;
    private Integer statu;
    private Integer done;
    private Integer parentTeamId;

    public static  final BinaryOperator<IncomeCoinDTO> convert=(o1, o2)->{
        o1.setSportCoin(BigDecimalUtil.add(o1.getSportCoin(),o2.getSportCoin()));
        return  o1;
    };
}


public class TeamUserDTO {
    private Long id;
    private Long uid;
    private String name;
    private String mobile;
    private Integer done;
    private Integer teamId;


}

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部