JDK8 Stream API使用心得

自己最近封装的年化收益相关工具类

package com.huajin.tradeserver.service.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;

import com.huajin.exchange.tradeservice.domain.response.biz.BizorderApply;
import com.huajin.exchange.tradeservice.domain.response.biz.BizplanRepay;
import com.huajin.listingserverclient.vo.ListingTradeInvestVo;
import com.huajin.tradeserver.domain.ProjectBaseinfo;
import com.huajin.tradeserver.service.payinvest.param.UserCfmInfo;

/**
 * 年化收益率工具类
 *
 * @author hongwei.lian
 * @date 2018年5月14日 上午11:40:38
 */
public class InvestProfitUtil {
	
	private InvestProfitUtil() {}
	
	/**
	 * 合并订单
	 *
	 * @param applyList 订单列表
	 * @param tradeInvestList 年化收益率列表
	 * @param project 项目信息
	 * @return 
	 * @author hongwei.lian
	 * @date 2018年5月14日 下午1:25:28
	 */
	public static List<UserCfmInfo> mergeBizorderApply(List<BizorderApply> applyList, List<ListingTradeInvestVo> tradeInvestList, ProjectBaseinfo project) {
		Map<String, UserCfmInfo> cfmInfoMap = new HashMap<>();
		
		//-- 1、合并订单规则:key为=投资者的userId + 投资者的付款银行卡Id
		if (CollectionUtils.isNotEmpty(applyList)) {
			cfmInfoMap = applyList.stream()
                                                  .collect(Collectors.toMap(
                                                		  apply -> apply.getOrderUserId() + "_" + apply.getPayUserCardId(),
                                                		  apply -> new UserCfmInfo(apply.getOrderUserId(), apply.getPayUserCardId(), apply.getCfmMoney(), BigDecimal.ZERO,null,null),
						                                  (oldValue, newValue) -> {
						                                	  newValue.setCfmMoney(newValue.getCfmMoney().add(oldValue.getCfmMoney()));
						                                	  return newValue;
						                                  }));
		}
		
		//-- 2、根据确权金额设置对应年化收益率
		cfmInfoMap.values().forEach(cfmInfo -> {
			//-- 每个投资者的确权金额
			BigDecimal cfmMoney = cfmInfo.getCfmMoney();
			//-- 项目年化收益率(存在投资者确权金额小于项目起投金额则按最低年化收益率计算)
			BigDecimal investProfit = project.getInvestProfit();
			//-- 根据认购金额设置对应年化收益率
			if (CollectionUtils.isNotEmpty(tradeInvestList)) {
				Optional<ListingTradeInvestVo> optional = 
						tradeInvestList.stream()
						                        .filter(tradeInvest -> cfmMoney.compareTo(tradeInvest.getInvestAmountMin()) >= 0)
						                        .max((tradeInvest1, tradeInvest2) -> tradeInvest1.getInvestAmountMin().compareTo(tradeInvest2.getInvestAmountMin()));
				if (optional.isPresent()) {
					investProfit = optional.get().getInvestProfit();
				} 
			}
			cfmInfo.setInvestProfit(investProfit);
		});
		
		//-- 3、返回合并后的List集合
		return cfmInfoMap.values()
				                        .stream()
				                        .collect(Collectors.toList());
	}
	
	/**
	 * 按年化收益率进行分组
	 *
	 * @param cfmInfoList 确权信息列表
	 * @return 
	 * @author hongwei.lian
	 * @date 2018年5月14日 下午1:26:04
	 */
	public static Map<BigDecimal, BigDecimal> getInvestProfitMap(List<UserCfmInfo> cfmInfoList) {
		Map<BigDecimal, BigDecimal> investProfitMap = new HashMap<>();
		if (CollectionUtils.isNotEmpty(cfmInfoList)) {
			investProfitMap = cfmInfoList.stream()
					                                        .collect(Collectors.toMap(
																    UserCfmInfo::getInvestProfit,
																    UserCfmInfo::getCfmMoney,
													                (oldValue, newValue) -> {
													            	    return newValue.add(oldValue);
												                     }));;
		}
		return investProfitMap;
	}
	
	/**
	 * 合并还款计划(目前解决方案可以优化)
	 *
	 * @param bizplanRepayMap
	 * @return 
	 * @author hongwei.lian
	 * @date 2018年5月17日 下午1:53:17
	 */
	public static List<BizplanRepay> mergeRepayPalnMap(Map<BigDecimal, List<BizplanRepay>> bizplanRepayMap) {
		List<BizplanRepay> planRepayList = new ArrayList<>();
		if (MapUtils.isNotEmpty(bizplanRepayMap)) {
			//-- 1、获取key的List集合
			List<BigDecimal> keyList = new ArrayList<>(bizplanRepayMap.keySet());
			
			//-- 2、以第一个List为基准合成还款计划
			List<BizplanRepay> bizplanRepayList = bizplanRepayMap.get(keyList.get(0));
			
			//-- 3、组装每期计息本金、本金、利息
			for (int i = 0; i < bizplanRepayList.size(); i++) {
				for (int j = 1; j < keyList.size(); j++) {
					List<BizplanRepay> otherBizplanRepayList = bizplanRepayMap.get(keyList.get(j));
					//-- 3.1、组装计息本金
					bizplanRepayList.get(i).setInterestPrincipal(bizplanRepayList.get(i).getInterestPrincipal().add(otherBizplanRepayList.get(i).getInterestPrincipal()));
					//-- 3.2、组装本金
					bizplanRepayList.get(i).setPrincipal(bizplanRepayList.get(i).getPrincipal().add(otherBizplanRepayList.get(i).getPrincipal()));
					//-- 3.3、组装利息
					bizplanRepayList.get(i).setInterest(bizplanRepayList.get(i).getInterest().add(otherBizplanRepayList.get(i).getInterest()));
				}
			}
			
			//-- 4、合成后的还款计划
			planRepayList = bizplanRepayList;
		}
		return planRepayList;
	}

	/**
	 * 优化后的合并还款计划
	 *
	 * @param bizplanRepayLists
	 * @return 
	 * @author hongwei.lian
	 * @date 2018年5月19日 下午4:27:07
	 */
	public static List<BizplanRepay> mergeRepayPalnList(List<List<BizplanRepay>> bizplanRepayLists) {
		List<BizplanRepay> bizplanRepayList = new ArrayList<>();
		if (CollectionUtils.isNotEmpty(bizplanRepayLists)) {
			bizplanRepayList = bizplanRepayLists.stream()
					                                                     .reduce(
					                                                    		 (bizplanRepayList1, bizplanRepayList2) -> {
																					  for (int i = 0; i < bizplanRepayList1.size(); i++) {
																							//-- 组装计息本金
																							bizplanRepayList1.get(i).setInterestPrincipal(bizplanRepayList1.get(i).getInterestPrincipal()
																									.add(bizplanRepayList2.get(i).getInterestPrincipal()));
																							//-- 组装本金
																							bizplanRepayList1.get(i).setPrincipal(bizplanRepayList1.get(i).getPrincipal()
																									.add(bizplanRepayList2.get(i).getPrincipal()));
																							//-- 组装利息
																							bizplanRepayList1.get(i).setInterest(bizplanRepayList1.get(i).getInterest()
																									.add(bizplanRepayList2.get(i).getInterest()));
																						}
				                                                                        return bizplanRepayList1;})
					                                                     .get();
		}
		return bizplanRepayList;
	}
	
}

猜你喜欢

转载自blog.csdn.net/erlian1992/article/details/80375551