From ee5c8055b1c0dd7c67a9025a76b10bd287c93d9a Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期三, 15 四月 2020 09:52:27 +0800
Subject: [PATCH] 资金明细增加团队分红

---
 fanli/src/main/java/com/yeshi/fanli/service/manger/order/HongBaoV2AddManager.java |  304 ++++++++++++++++++++++++++------------------------
 1 files changed, 158 insertions(+), 146 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/service/manger/order/HongBaoV2AddManager.java b/fanli/src/main/java/com/yeshi/fanli/service/manger/order/HongBaoV2AddManager.java
index 6fbed48..cdf0ecb 100644
--- a/fanli/src/main/java/com/yeshi/fanli/service/manger/order/HongBaoV2AddManager.java
+++ b/fanli/src/main/java/com/yeshi/fanli/service/manger/order/HongBaoV2AddManager.java
@@ -19,7 +19,6 @@
 import com.yeshi.fanli.dao.mybatis.order.CommonOrderGoodsMapper;
 import com.yeshi.fanli.dao.mybatis.order.HongBaoOrderMapper;
 import com.yeshi.fanli.dto.HongBao;
-import com.yeshi.fanli.dto.order.OrderHongBaoRateParams;
 import com.yeshi.fanli.entity.bus.user.HongBaoV2;
 import com.yeshi.fanli.entity.bus.user.ThreeSale;
 import com.yeshi.fanli.entity.bus.user.UserInfo;
@@ -27,9 +26,14 @@
 import com.yeshi.fanli.entity.order.CommonOrder;
 import com.yeshi.fanli.entity.order.CommonOrderGoods;
 import com.yeshi.fanli.entity.order.HongBaoOrder;
+import com.yeshi.fanli.exception.order.CommonOrderException;
 import com.yeshi.fanli.exception.order.HongBaoException;
+import com.yeshi.fanli.exception.order.InviteOrderSubsidyException;
 import com.yeshi.fanli.exception.user.UserAccountException;
+import com.yeshi.fanli.log.LogHelper;
 import com.yeshi.fanli.service.inter.order.CommonOrderService;
+import com.yeshi.fanli.service.inter.order.InviteOrderSubsidyService;
+import com.yeshi.fanli.service.inter.order.OrderHongBaoMoneyComputeService;
 import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
 import com.yeshi.fanli.service.inter.order.msg.UserOrderMsgNotificationService;
 import com.yeshi.fanli.service.inter.user.UserInfoService;
@@ -42,6 +46,7 @@
 import com.yeshi.fanli.util.MoneyBigDecimalUtil;
 import com.yeshi.fanli.util.TaoBaoConstant;
 import com.yeshi.fanli.util.TimeUtil;
+import com.yeshi.fanli.util.user.UserLevelUtil;
 
 @Component
 public class HongBaoV2AddManager {
@@ -82,6 +87,12 @@
 	@Resource
 	private UserLevelManager userLevelManager;
 
+	@Resource
+	private InviteOrderSubsidyService inviteOrderSubsidyService;
+
+	@Resource
+	private OrderHongBaoMoneyComputeService orderHongBaoMoneyComputeService;
+
 	/**
 	 * 娣诲姞绾㈠寘淇℃伅
 	 * 
@@ -90,17 +101,29 @@
 	 * @return
 	 * @throws HongBaoException
 	 * @throws UserAccountException
+	 * @throws CommonOrderException 
+	 * @throws OrderTeamRewardException 
+	 * @throws InviteOrderSubsidyException 
 	 */
 	public int addHongBao(List<CommonOrder> commonOrderList, int type) throws HongBaoException, UserAccountException {
 
 		if (commonOrderList != null && commonOrderList.size() > 0) {
 			int orderType = commonOrderList.get(0).getSourceType();
+			String orderId = commonOrderList.get(0).getOrderNo();
 
 			Map<Integer, HongBaoOrder> notificationMap = new HashMap<>();
 			int goodsCount = 0;
 			boolean hasAdd = false;
 			boolean hasUpdate = false;
 			Set<Integer> stateSet = new HashSet<>();// 璁㈠崟鐘舵�丼et
+
+			UserLevelEnum userLevel = UserLevelUtil.getByOrderRank(commonOrderList.get(0).getUrank());
+
+			if (userLevel == null)
+				userLevel = UserLevelEnum.daRen;
+
+			Date placeOrderDate = commonOrderList.get(0).getThirdCreateTime();
+
 			for (CommonOrder commonOrder : commonOrderList) {
 				stateSet.add(commonOrder.getState());
 				goodsCount += commonOrder.getCount();
@@ -108,19 +131,46 @@
 					throw new HongBaoException(1, "璁㈠崟淇℃伅涓嶅畬鏁�");
 				HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId());
 				if (hongBaoOrder == null) {
-					saveHongBao(commonOrder, type, notificationMap);
+					saveHongBao(commonOrder, type, notificationMap, userLevel, placeOrderDate);
 					hasAdd = true;
 				} else {
-					boolean update = updateHongBao(hongBaoOrder, commonOrder, type, notificationMap);
-					if (update)
+					boolean update = updateHongBao(hongBaoOrder, commonOrder, type, notificationMap, placeOrderDate);
+					if (update) {
 						hasUpdate = true;
+					}
 				}
+			}
+
+			if (hasAdd) {
+				try {
+					inviteOrderSubsidyService.addOrUpdateByOrder(orderId, orderType);
+				} catch (InviteOrderSubsidyException e) {
+					throw new HongBaoException(201, "娣诲姞琛ヨ创鍑洪敊");
+				} catch (CommonOrderException e) {
+					throw new HongBaoException(202, "娣诲姞琛ヨ创鍑洪敊");
+				}
+			}
+
+			int state = getOrderState(stateSet);
+
+			if (hasAdd || hasUpdate) {
+				// 琛ヨ创澶辨晥
+				if (state == CommonOrder.STATE_SX)
+					inviteOrderSubsidyService.invalidByOrderIdAndSourceType(orderId, orderType);
+				else
+					try {
+						inviteOrderSubsidyService.addOrUpdateByOrder(orderId, orderType);
+					} catch (InviteOrderSubsidyException e) {
+						LogHelper.errorDetailInfo(e, "娣樺疂璁㈠崟琛ヨ创鏇存柊澶辫触", orderId);
+					} catch (CommonOrderException e) {
+						LogHelper.errorDetailInfo(e, "娣樺疂璁㈠崟琛ヨ创鏇存柊澶辫触", orderId);
+					}
 			}
 
 			/**
 			 * 閫氱煡鐢ㄦ埛鐨勮繑鍒╂儏鍐�
 			 */
-			int state = getOrderState(stateSet);
+
 			if (!Constant.IS_TEST) {
 				if (hasAdd) {
 					Iterator<Integer> its = notificationMap.keySet().iterator();
@@ -128,75 +178,49 @@
 						Integer t = its.next();
 						HongBaoV2 notify = notificationMap.get(t).getHongBaoV2();
 						CommonOrder commonOrder = notificationMap.get(t).getCommonOrder();
-						String orderId = commonOrderList.get(0).getOrderNo();
 						Long uid = notify.getUserInfo().getId();
 						BigDecimal money = notify.getMoney();
 						switch (t) {
 						case HongBaoV2.TYPE_ZIGOU:
 							userOrderMsgNotificationService.orderFanLiStatistic(uid, orderId, orderType,
-									commonOrder.getPayment(), money, goodsCount, commonOrder.getState(),
+									commonOrder.getPayment(), money, goodsCount, state,
 									commonOrder.getThirdCreateTime());
 
 							break;
 						case HongBaoV2.TYPE_SHARE_GOODS:
 
 							userOrderMsgNotificationService.orderShareStatistic(uid, orderId, orderType,
-									commonOrder.getPayment(), money, goodsCount, commonOrder.getState(),
+									commonOrder.getPayment(), money, goodsCount, state,
 									commonOrder.getThirdCreateTime());
 
 							break;
 						case HongBaoV2.TYPE_YIJI:
 							userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
-									commonOrder.getPayment(), money, goodsCount, commonOrder.getState(),
+									commonOrder.getPayment(), money, goodsCount, state,
 									commonOrder.getThirdCreateTime());
 
 							break;
 						case HongBaoV2.TYPE_ERJI:
-						
-							userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType, 	commonOrder.getPayment(), money,
-									goodsCount,  commonOrder.getState(), commonOrder.getThirdCreateTime());
+
+							userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
+									commonOrder.getPayment(), money, goodsCount, state,
+									commonOrder.getThirdCreateTime());
 
 							break;
 
 						case HongBaoV2.TYPE_SHARE_YIJI:
-							userOrderMsgNotificationService.orderShareFirstLevelStatistic(uid, orderId, orderType,
-									commonOrder.getPayment(), money, goodsCount, state, notify.getBeizhu());
+							userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
+									commonOrder.getPayment(), money, goodsCount, state,
+									commonOrder.getThirdCreateTime());
 							break;
-						}
-					}
-				}
+						case HongBaoV2.TYPE_SHARE_ERJI:
 
-				// 娣诲姞鏂扮増鏈�氱煡
+							userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
+									commonOrder.getPayment(), money, goodsCount, state,
+									commonOrder.getThirdCreateTime());
 
-				if (!hasAdd) {
-					Iterator<Integer> its = notificationMap.keySet().iterator();
-					while (its.hasNext()) {
-						Integer t = its.next();
-						HongBaoV2 notify = notificationMap.get(t).getHongBaoV2();
-						CommonOrder commonOrder = notificationMap.get(t).getCommonOrder();
-						String orderId = commonOrderList.get(0).getOrderNo();
-						Long uid = notify.getUserInfo().getId();
-						BigDecimal money = notify.getMoney();
-						switch (t) {
-						case HongBaoV2.TYPE_ZIGOU:
-							userOrderMsgNotificationService.orderFanLiStateChanged(uid, orderId, orderType,
-									commonOrder.getPayment(), money, state);
 							break;
-						case HongBaoV2.TYPE_SHARE_GOODS:
-							userOrderMsgNotificationService.orderShareStateChanged(uid, orderId, orderType,
-									commonOrder.getPayment(), money, state);
-							break;
-						case HongBaoV2.TYPE_YIJI:
-							userOrderMsgNotificationService.orderInviteStateChanged(uid, orderId, orderType,
-									commonOrder.getPayment(), money, state);
-							break;
-						case HongBaoV2.TYPE_ERJI:
-							userOrderMsgNotificationService.orderInviteStateChanged(uid, orderId, orderType,
-									commonOrder.getPayment(), money, state);
-							break;
-						case HongBaoV2.TYPE_SHARE_YIJI:
-							userOrderMsgNotificationService.orderShareFirstLevelStateChanged(uid, orderId, orderType,
-									commonOrder.getPayment(), money, state);
+
 						}
 					}
 				}
@@ -228,21 +252,24 @@
 		return CommonOrder.STATE_SX;
 	}
 
-	@Transactional(rollbackFor = Exception.class)
 	private boolean updateHongBao(HongBaoOrder hongBaoOrder, CommonOrder commonOrder, int type,
-			Map<Integer, HongBaoOrder> notificationMap) throws HongBaoException, UserAccountException {
+			Map<Integer, HongBaoOrder> notificationMap, Date placeOrderTime)
+			throws HongBaoException, UserAccountException {
 		System.out.println(commonOrder.getOrderNo());
 		// 閿佽
 		HongBaoV2 oldHongBao = hongBaoV2Mapper.selectByPrimaryKeyForUpdate(hongBaoOrder.getHongBaoV2().getId());
 		if (oldHongBao == null)
 			throw new HongBaoException(10, "绾㈠寘瀵硅薄涓嶅瓨鍦�");
+		UserLevelEnum buyerLevel = UserLevelUtil.getByOrderRank(oldHongBao.getUrank());
+		if (buyerLevel == null)
+			throw new HongBaoException(201, "鐢ㄦ埛绛夌骇涓虹┖");
+
 		// 宸茬粡澶辨晥锛屽凡缁忛鍙栵紝鏂拌�佺姸鎬佷竴鑷寸殑绾㈠寘涓嶅仛澶勭悊
 		if (oldHongBao.getState() == HongBaoV2.STATE_SHIXIAO || oldHongBao.getState() == HongBaoV2.STATE_YILINGQU)
 			return false;
 
 		if (type == HongBaoV2.TYPE_ZIGOU) {// 鑾峰彇鑷喘鐨勮繑鍒╂瘮渚�
-			BigDecimal fanliRate = hongBaoManageService.getRate(new OrderHongBaoRateParams(0, type, false,
-					oldHongBao.getUrank() == 100, commonOrder.getThirdCreateTime()));
+			BigDecimal money = orderHongBaoMoneyComputeService.computeFanliMoney(commonOrder, buyerLevel);
 			// 鍏嶅崟澶勭悊
 			boolean mianDan = false;
 			List<CommonOrder> orderList = commonOrderService.listBySourceTypeAndOrderId(Constant.SOURCE_TYPE_TAOBAO,
@@ -258,7 +285,7 @@
 				}
 			}
 
-			HongBaoV2 hongBao = createFanLiUpdateHongBao(commonOrder, fanliRate, hongBaoOrder.getHongBaoV2().getId(),
+			HongBaoV2 hongBao = createFanLiUpdateHongBao(commonOrder, money, hongBaoOrder.getHongBaoV2().getId(),
 					mianDan);
 
 			// 鏂拌�佺孩鍖呯姸鎬佷竴鑷翠笉澶勭悊
@@ -285,12 +312,14 @@
 				tempHongBao.setMoney(hongBao.getMoney());
 				CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
 				tempCommonOrder.setPayment(commonOrder.getPayment());
+				tempCommonOrder.setThirdCreateTime(commonOrder.getThirdCreateTime());
 				notificationMap.put(type, new HongBaoOrder(tempCommonOrder, tempHongBao));
 			} else {
 				// 澧炲姞浠樻閲戦涓庤祫閲�
 				HongBaoOrder tempHongBaoOrder = notificationMap.get(type);
 				tempHongBaoOrder.getCommonOrder()
 						.setPayment(tempHongBaoOrder.getCommonOrder().getPayment().add(commonOrder.getPayment()));
+
 				tempHongBaoOrder.getHongBaoV2()
 						.setMoney(tempHongBaoOrder.getHongBaoV2().getMoney().add(hongBao.getMoney()));
 				notificationMap.put(type, tempHongBaoOrder);
@@ -302,17 +331,14 @@
 				for (HongBaoV2 child : children) {
 					if (child.getState() == HongBaoV2.STATE_YILINGQU)
 						continue;
-					BigDecimal rate = null;
+
 					if (child.getType() == HongBaoV2.TYPE_YIJI) {
-						rate = hongBaoManageService.getRate(new OrderHongBaoRateParams(1, oldHongBao.getType(), false,
-								oldHongBao.getUrank() == 100, commonOrder.getThirdCreateTime()));
+						money = orderHongBaoMoneyComputeService.computeFirstInviteMoney(commonOrder, buyerLevel);
 					} else if (child.getType() == HongBaoV2.TYPE_ERJI) {
-						rate = hongBaoManageService.getRate(new OrderHongBaoRateParams(2, oldHongBao.getType(), false,
-								oldHongBao.getUrank() == 100, commonOrder.getThirdCreateTime()));
+						money = orderHongBaoMoneyComputeService.computeSecondInviteMoney(commonOrder, buyerLevel);
 					}
 
-					HongBaoV2 childUpdate = createInviteUpdateHongBao(child, hongBao.getState(),
-							MoneyBigDecimalUtil.mul(hongBao.getMoney(), rate.divide(new BigDecimal(100))), commonOrder);
+					HongBaoV2 childUpdate = createInviteUpdateHongBao(child, hongBao.getState(), money, commonOrder);
 					hongBaoV2Mapper.updateByPrimaryKeySelective(childUpdate);
 
 					// 鍔犲叆閫氱煡
@@ -322,6 +348,7 @@
 						tempHongBao.setMoney(child.getMoney());
 						CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
 						tempCommonOrder.setPayment(commonOrder.getPayment());
+						tempCommonOrder.setThirdCreateTime(commonOrder.getThirdCreateTime());
 						notificationMap.put(child.getType(), new HongBaoOrder(tempCommonOrder, tempHongBao));
 					} else {
 						// 澧炲姞浠樻閲戦涓庤祫閲�
@@ -337,7 +364,7 @@
 				if (!mianDan) {
 					ThreeSale threeSale = threeSaleSerivce.selectByWorkerId(commonOrder.getUserInfo().getId());
 					if (threeSale != null && threeSale.getSucceedTime() != null
-							&& threeSale.getSucceedTime() <= commonOrder.getThirdCreateTime().getTime()) {
+							&& threeSale.getSucceedTime() <= placeOrderTime.getTime()) {
 						UserInfo boss = threeSaleSerivce.getBoss(commonOrder.getUserInfo().getId());
 						if (boss != null) {
 							boss = userInfoService.selectAvailableByPrimaryKey(boss.getId());
@@ -345,12 +372,11 @@
 
 						if (boss != null) {
 							// 鎻掑叆涓�绾у瓙绾㈠寘
-							BigDecimal firstRate = hongBaoManageService.getRate(new OrderHongBaoRateParams(1, type,
-									false, oldHongBao.getUrank() == 100, commonOrder.getThirdCreateTime()));
-							if (firstRate != null && firstRate.compareTo(new BigDecimal(0)) > 0) {
+							money = orderHongBaoMoneyComputeService.computeFirstInviteMoney(commonOrder, buyerLevel);
+							UserLevelEnum bossUserLevel = userLevelManager.getUserLevel(boss.getId());
+							if (money != null) {
 								HongBaoV2 firstHongbao = createInviteHongBao(boss.getId(), oldHongBao, commonOrder,
-										HongBaoV2.TYPE_YIJI, MoneyBigDecimalUtil.mul(hongBao.getMoney(),
-												firstRate.divide(new BigDecimal(100))));
+										HongBaoV2.TYPE_YIJI, money, bossUserLevel);
 								if (firstHongbao != null)
 									addInviteHongBao(firstHongbao, null, notificationMap, commonOrder);
 							}
@@ -359,13 +385,12 @@
 								boss = userInfoService.selectAvailableByPrimaryKey(boss.getId());
 							}
 							if (boss != null) {
-								// 浜岀骇绾㈠寘
-								BigDecimal secondRate = hongBaoManageService.getRate(new OrderHongBaoRateParams(2, type,
-										false, oldHongBao.getUrank() == 100, commonOrder.getThirdCreateTime()));
-								if (secondRate != null && secondRate.compareTo(new BigDecimal(0)) > 0) {
+								money = orderHongBaoMoneyComputeService.computeSecondInviteMoney(commonOrder,
+										buyerLevel);
+								bossUserLevel = userLevelManager.getUserLevel(boss.getId());
+								if (money != null) {
 									HongBaoV2 secondHongbao = createInviteHongBao(boss.getId(), oldHongBao, commonOrder,
-											HongBaoV2.TYPE_ERJI, MoneyBigDecimalUtil.mul(hongBao.getMoney(),
-													secondRate.divide(new BigDecimal(100))));
+											HongBaoV2.TYPE_ERJI, money, bossUserLevel);
 									if (secondHongbao != null)
 										addInviteHongBao(secondHongbao, null, notificationMap, commonOrder);
 								}
@@ -375,10 +400,10 @@
 				}
 			}
 		} else if (type == HongBaoV2.TYPE_SHARE_GOODS) {
-			BigDecimal fanliRate = hongBaoManageService.getRate(new OrderHongBaoRateParams(0, type, false,
-					oldHongBao.getUrank() == 100, commonOrder.getThirdCreateTime()));
 
-			HongBaoV2 hongBao = createShareUpdateHongBao(commonOrder, fanliRate, oldHongBao.getId());
+			BigDecimal money = orderHongBaoMoneyComputeService.computeShareMoney(commonOrder, buyerLevel);
+
+			HongBaoV2 hongBao = createShareUpdateHongBao(commonOrder, money, oldHongBao.getId());
 			hongBaoV2Mapper.updateByPrimaryKeySelective(hongBao);
 			// 閫氱煡
 			if (notificationMap.get(type) == null) {
@@ -387,6 +412,7 @@
 				tempHongBao.setMoney(hongBao.getMoney());
 				CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
 				tempCommonOrder.setPayment(commonOrder.getPayment());
+				tempCommonOrder.setThirdCreateTime(commonOrder.getThirdCreateTime());
 				notificationMap.put(type, new HongBaoOrder(tempCommonOrder, tempHongBao));
 			} else {
 				// 澧炲姞浠樻閲戦涓庤祫閲�
@@ -405,16 +431,12 @@
 					if (child.getState() == HongBaoV2.STATE_YILINGQU)
 						continue;
 
-					BigDecimal rate = null;
 					if (child.getType() == HongBaoV2.TYPE_SHARE_YIJI) {
-						rate = hongBaoManageService.getRate(new OrderHongBaoRateParams(1, oldHongBao.getType(), false,
-								oldHongBao.getUrank() == 100, commonOrder.getThirdCreateTime()));
+						money = orderHongBaoMoneyComputeService.computeFirstInviteMoney(commonOrder, buyerLevel);
 					} else if (child.getType() == HongBaoV2.TYPE_SHARE_ERJI) {
-						rate = hongBaoManageService.getRate(new OrderHongBaoRateParams(2, oldHongBao.getType(), false,
-								oldHongBao.getUrank() == 100, commonOrder.getThirdCreateTime()));
+						money = orderHongBaoMoneyComputeService.computeSecondInviteMoney(commonOrder, buyerLevel);
 					}
-					HongBaoV2 childUpdate = createInviteUpdateHongBao(child, hongBao.getState(),
-							MoneyBigDecimalUtil.mul(hongBao.getMoney(), rate.divide(new BigDecimal(100))), commonOrder);
+					HongBaoV2 childUpdate = createInviteUpdateHongBao(child, hongBao.getState(), money, commonOrder);
 					hongBaoV2Mapper.updateByPrimaryKeySelective(childUpdate);
 					// 娣诲姞閫氱煡
 					if (notificationMap.get(child.getType()) == null) {
@@ -424,6 +446,7 @@
 								.setMoney(childUpdate.getMoney() == null ? new BigDecimal(0) : childUpdate.getMoney());
 						CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
 						tempCommonOrder.setPayment(commonOrder.getPayment());
+						tempCommonOrder.setThirdCreateTime(commonOrder.getThirdCreateTime());
 						notificationMap.put(child.getType(), new HongBaoOrder(tempCommonOrder, tempHongBao));
 					} else {
 						// 澧炲姞浠樻閲戦涓庤祫閲�
@@ -445,13 +468,10 @@
 	}
 
 	@Transactional(rollbackFor = Exception.class)
-	private void saveHongBao(CommonOrder commonOrder, int type, Map<Integer, HongBaoOrder> notificationMap)
-			throws HongBaoException, UserAccountException {
+	private void saveHongBao(CommonOrder commonOrder, int type, Map<Integer, HongBaoOrder> notificationMap,
+			UserLevelEnum buyerLevel, Date placeOrderDate) throws HongBaoException, UserAccountException {
 
-		boolean vip = commonOrder.getUrank() != null && commonOrder.getUrank() == 100;
-		if (type == HongBaoV2.TYPE_ZIGOU) {// 鑾峰彇鑷喘鐨勮繑鍒╂瘮渚�
-			BigDecimal fanliRate = hongBaoManageService
-					.getRate(new OrderHongBaoRateParams(0, type, false, vip, commonOrder.getThirdCreateTime()));
+		if (type == HongBaoV2.TYPE_ZIGOU) {
 			// 鏌ヨ鏄惁鏈夊厤鍗曡鍒�
 			BigDecimal mianDanMoney = null;
 			if (commonOrder.getState() == CommonOrder.STATE_JS || commonOrder.getState() == CommonOrder.STATE_FK) {
@@ -461,7 +481,6 @@
 					BigDecimal payMent = commonOrder.getPayment();
 					if (commonOrder.getState() == CommonOrder.STATE_JS)
 						payMent = commonOrder.getSettlement();
-
 					CommonOrderGoods goods = commonOrderGoodsMapper
 							.selectByPrimaryKey(commonOrder.getCommonOrderGoods().getId());
 					if (goods != null) {
@@ -486,33 +505,34 @@
 				}
 			}
 
-			HongBaoV2 hongBao = createFanLiHongBao(commonOrder, fanliRate, mianDanMoney);
+			HongBaoV2 hongBao = createFanLiHongBao(commonOrder,
+					orderHongBaoMoneyComputeService.computeFanliMoney(commonOrder, buyerLevel), mianDanMoney,
+					buyerLevel);
 			if (hongBao == null)
 				return;
 			addFanLiOrShareHongBao(hongBao, commonOrder, notificationMap);
 			UserInfo boss = threeSaleSerivce.getBoss(hongBao.getUserInfo().getId());
 			if (boss != null && hongBao.getState() != HongBaoV2.STATE_SHIXIAO && mianDanMoney == null) {// 1绾OSS瀛樺湪涓旂孩鍖呮湭澶辨晥,鍏嶅崟涓嶆敮鎸佸绾у垎閿�
-				// 鎻掑叆涓�绾у瓙绾㈠寘
-				BigDecimal firstRate = hongBaoManageService
-						.getRate(new OrderHongBaoRateParams(1, type, false, vip, commonOrder.getThirdCreateTime()));
-				if (firstRate.compareTo(new BigDecimal(0)) <= 0)// 杩斿埄姣斾緥涓�0鐨勪笉鍒嗘垚
+				BigDecimal money = orderHongBaoMoneyComputeService.computeFirstInviteMoney(commonOrder, buyerLevel);
+				if (money == null)// 杩斿埄璧勯噾涓虹┖涓嶅弬涓庡垎鎴�
 					return;
 
+				UserLevelEnum bossLevel = userLevelManager.getUserLevel(boss.getId());
+
 				HongBaoV2 firstHongbao = createInviteHongBao(boss.getId(), hongBao, commonOrder, HongBaoV2.TYPE_YIJI,
-						MoneyBigDecimalUtil.mul(hongBao.getMoney(), firstRate.divide(new BigDecimal(100))));
+						money, bossLevel);
 
 				addInviteHongBao(firstHongbao, null, notificationMap, commonOrder);
 
 				// 鎻掑叆浜岀骇瀛愮孩鍖�
 				boss = threeSaleSerivce.getBoss(boss.getId());
 				if (boss != null) {// 浜岀骇BOSS瀛樺湪涓旀槸闈炰細鍛樿鍗�
-					BigDecimal secondRate = hongBaoManageService
-							.getRate(new OrderHongBaoRateParams(2, type, false, vip, commonOrder.getThirdCreateTime()));
-					if (secondRate.compareTo(new BigDecimal(0)) <= 0)
+					money = orderHongBaoMoneyComputeService.computeSecondInviteMoney(commonOrder, buyerLevel);
+					bossLevel = userLevelManager.getUserLevel(boss.getId());
+					if (money == null)
 						return;
 					HongBaoV2 secondHongbao = createInviteHongBao(boss.getId(), hongBao, commonOrder,
-							HongBaoV2.TYPE_ERJI,
-							MoneyBigDecimalUtil.mul(hongBao.getMoney(), secondRate.divide(new BigDecimal(100))));
+							HongBaoV2.TYPE_ERJI, money, bossLevel);
 					addInviteHongBao(secondHongbao, null, notificationMap, commonOrder);
 				}
 			}
@@ -522,45 +542,48 @@
 			if (commonOrder.getState() == CommonOrder.STATE_SX || commonOrder.getState() == CommonOrder.STATE_WQ)
 				return;
 			// 鍒嗕韩璧�
-			BigDecimal shareRate = null;
+			BigDecimal money = null;
 			if (commonOrder.getSourceType() == Constant.SOURCE_TYPE_TAOBAO
 					&& commonOrder.getSourcePosition().equalsIgnoreCase(TaoBaoConstant.TAOBAO_TLJ_RELATION_PID_DEFAULT))// 鏉ヨ嚜浜庢窐绀奸噾鐨勫垎浜�
-				shareRate = hongBaoManageService.getTLJShareRate(commonOrder.getCreateTime().getTime());
-			else
-				shareRate = hongBaoManageService
-						.getRate(new OrderHongBaoRateParams(0, type, false, vip, commonOrder.getThirdCreateTime()));
+			{
+				BigDecimal shareRate = hongBaoManageService.getTLJShareRate(commonOrder.getCreateTime().getTime());
+				if (commonOrder.getState() == CommonOrder.STATE_JS || commonOrder.getState() == CommonOrder.STATE_WQ)
+					money = MoneyBigDecimalUtil.mul(commonOrder.geteIncome(),
+							MoneyBigDecimalUtil.div(shareRate, new BigDecimal(100)));
+				else
+					money = MoneyBigDecimalUtil.mul(commonOrder.getEstimate(),
+							MoneyBigDecimalUtil.div(shareRate, new BigDecimal(100)));
+			} else
+				money = orderHongBaoMoneyComputeService.computeShareMoney(commonOrder, buyerLevel);
 
-			if (shareRate.compareTo(new BigDecimal(0)) <= 0)
+			if (money == null || money.compareTo(new BigDecimal(0)) <= 0)
 				return;
-			HongBaoV2 hongBao = createShareHongBao(commonOrder, shareRate, vip);
+			HongBaoV2 hongBao = createShareHongBao(commonOrder, money, buyerLevel);
 			if (hongBao == null)
 				return;
 			addFanLiOrShareHongBao(hongBao, commonOrder, notificationMap);
 			// 4鏈�17鏃ュ悗鎵嶆湁涓�绾у垎浜禋
-			if (commonOrder.getThirdCreateTime().getTime() > TimeUtil.convertToTimeTemp("2019-04-17", "yyyy-MM-dd")) {
+			if (placeOrderDate.getTime() > TimeUtil.convertToTimeTemp("2019-04-17", "yyyy-MM-dd")) {
 				UserInfo boss = threeSaleSerivce.getBoss(hongBao.getUserInfo().getId());
 				if (boss != null) {
-					BigDecimal firstLevelRate = hongBaoManageService
-							.getRate(new OrderHongBaoRateParams(1, type, false, vip, commonOrder.getThirdCreateTime()));
-					if (firstLevelRate == null || firstLevelRate.compareTo(new BigDecimal("0")) == 0)
+					money = orderHongBaoMoneyComputeService.computeFirstInviteMoney(commonOrder, buyerLevel);
+					if (money == null)
 						return;
-
+					UserLevelEnum bossLevel = userLevelManager.getUserLevel(boss.getId());
 					HongBaoV2 firstHongbao = createInviteHongBao(boss.getId(), hongBao, commonOrder,
-							HongBaoV2.TYPE_SHARE_YIJI,
-							MoneyBigDecimalUtil.mul(hongBao.getMoney(), firstLevelRate.divide(new BigDecimal(100))));
+							HongBaoV2.TYPE_SHARE_YIJI, money, bossLevel);
 
 					addInviteHongBao(firstHongbao, hongBao.getUserInfo().getNickName(), notificationMap, commonOrder);
 
 					// 浜岀骇鍒嗕韩璧�
 					boss = threeSaleSerivce.getBoss(boss.getId());
 					if (boss != null) {
-						BigDecimal secondLevelRate = hongBaoManageService.getRate(
-								new OrderHongBaoRateParams(2, type, false, vip, commonOrder.getThirdCreateTime()));
-						if (secondLevelRate.compareTo(new BigDecimal(0)) <= 0)// 杩斿埄姣斾緥涓�0灏变笉缁熻
+						money = orderHongBaoMoneyComputeService.computeSecondInviteMoney(commonOrder, buyerLevel);
+						if (money == null)// 杩斿埄姣斾緥涓�0灏变笉缁熻
 							return;
+						bossLevel = userLevelManager.getUserLevel(boss.getId());
 						HongBaoV2 secondChild = createInviteHongBao(boss.getId(), hongBao, commonOrder,
-								HongBaoV2.TYPE_SHARE_ERJI, MoneyBigDecimalUtil.mul(hongBao.getMoney(),
-										secondLevelRate.divide(new BigDecimal(100))));
+								HongBaoV2.TYPE_SHARE_ERJI, money, bossLevel);
 						addInviteHongBao(secondChild, null, notificationMap, commonOrder);
 					}
 				}
@@ -613,6 +636,7 @@
 
 				CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
 				tempCommonOrder.setPayment(commonOrder.getPayment());
+				tempCommonOrder.setThirdCreateTime(commonOrder.getThirdCreateTime());
 				notificationMap.put(child.getType(), new HongBaoOrder(tempCommonOrder, tempHongBao));
 			} else {
 				// 澧炲姞浠樻閲戦涓庤祫閲�
@@ -637,7 +661,7 @@
 	 * @return
 	 */
 	private HongBaoV2 createInviteHongBao(Long uid, HongBaoV2 parent, CommonOrder commonOrder, int hongBaoType,
-			BigDecimal money) {
+			BigDecimal money, UserLevelEnum userLevel) {
 		if (parent == null)
 			return null;
 		if (parent.getState() == HongBaoV2.STATE_SHIXIAO)
@@ -645,8 +669,6 @@
 		UserInfo user = userInfoService.selectAvailableByPrimaryKey(uid);
 		if (user == null)
 			return null;
-
-		UserLevelEnum level = userLevelManager.getUserLevel(uid);
 
 		HongBaoV2 child = new HongBaoV2();
 		child.setParent(parent);
@@ -665,7 +687,7 @@
 		}
 
 		child.setUserInfo(user);
-		child.setUrank(level.getOrderRank());
+		child.setUrank(userLevel.getOrderRank());
 		child.setVersion(2);
 		child.setCreateTime(new Date());
 		child.setOrderType(commonOrder.getSourceType());
@@ -682,22 +704,24 @@
 	 *            -杩斿埄姣斾緥
 	 * @param mianDanMoney
 	 *            鍏嶅崟閲戦
+	 * @param isVIP-鏄惁涓篤IP璁㈠崟
 	 * @return
 	 * @throws HongBaoException
 	 * @throws UserAccountException
 	 */
-	private HongBaoV2 createFanLiHongBao(CommonOrder commonOrder, BigDecimal fanliRate, BigDecimal mianDanMoney) throws HongBaoException, UserAccountException {
+	private HongBaoV2 createFanLiHongBao(CommonOrder commonOrder, BigDecimal money, BigDecimal mianDanMoney,
+			UserLevelEnum userLevel) throws HongBaoException, UserAccountException {
 		HongBaoV2 hongBao = new HongBaoV2();
 		hongBao.setUserInfo(commonOrder.getUserInfo());
 		hongBao.setCreateTime(new Date());
 		hongBao.setType(HongBaoV2.TYPE_ZIGOU);
 		hongBao.setVersion(2);
+		hongBao.setMoney(money);
 		if (commonOrder.getState() == CommonOrder.STATE_FK) {
 			hongBao.setState(HongBaoV2.STATE_BUKELINGQU);
-			hongBao.setMoney(MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), fanliRate.divide(new BigDecimal(100))));
+
 		} else if (commonOrder.getState() == CommonOrder.STATE_JS || CommonOrder.STATE_WQ == commonOrder.getState()) {
 			hongBao.setState(HongBaoV2.STATE_KELINGQU);
-			hongBao.setMoney(MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), fanliRate.divide(new BigDecimal(100))));
 			if (commonOrder.getSourceType() == Constant.SOURCE_TYPE_PDD)// 鎷煎澶�15澶╁崐鍒拌处
 				hongBao.setPreGetTime(new Date(
 						commonOrder.getSettleTime().getTime() + 1000 * 60 * 60 * 24 * 15L + 1000 * 60 * 60 * 12L));
@@ -716,30 +740,24 @@
 		UserInfo user = userInfoService.getUserById(commonOrder.getUserInfo().getId());
 		if (user == null)
 			throw new UserAccountException(1001, "鐢ㄦ埛涓嶅瓨鍦�/琚皝绂�");
-		UserLevelEnum userLevel = userLevelManager.getUserLevel(commonOrder.getUserInfo().getId(),
-				commonOrder.getThirdCreateTime());
-		if (userLevel != null)
-			hongBao.setUrank(userLevel.getOrderRank());
-		else {
-			hongBao.setUrank(user.getRank());
-		}
+		hongBao.setUrank(userLevel.getOrderRank());
 		hongBao.setOrderType(commonOrder.getSourceType());
 		return hongBao;
 	}
 
-	private HongBaoV2 createFanLiUpdateHongBao(CommonOrder commonOrder, BigDecimal fanliRate, Long hongBaoId,
+	private HongBaoV2 createFanLiUpdateHongBao(CommonOrder commonOrder, BigDecimal money, Long hongBaoId,
 			boolean mianDan) throws HongBaoException, UserAccountException {
 		HongBaoV2 hongBao = new HongBaoV2(hongBaoId);
 		hongBao.setUpdateTime(new Date());
+		hongBao.setMoney(money);
 		// 鏇存敼鐘舵�佷笌璧勯噾
 		if (commonOrder.getState() == CommonOrder.STATE_FK) {
 			hongBao.setState(HongBaoV2.STATE_BUKELINGQU);
-			hongBao.setMoney(MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), fanliRate.divide(new BigDecimal(100))));
+
 			if (mianDan)
 				hongBao.setMoney(commonOrder.getPayment());
 		} else if (commonOrder.getState() == CommonOrder.STATE_JS || commonOrder.getState() == CommonOrder.STATE_WQ) {
 			hongBao.setState(HongBaoV2.STATE_KELINGQU);
-			hongBao.setMoney(MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), fanliRate.divide(new BigDecimal(100))));
 			if (commonOrder.getSourceType() == Constant.SOURCE_TYPE_PDD)// 鎷煎澶�15澶╄繃1涓皬鏃跺埌璐�
 				hongBao.setPreGetTime(new Date(
 						commonOrder.getSettleTime().getTime() + 1000 * 60 * 60 * 24 * 15L + 1000 * 60 * 60 * 1L));
@@ -766,17 +784,16 @@
 	 * @throws HongBaoException
 	 * @throws UserAccountException
 	 */
-	private HongBaoV2 createShareUpdateHongBao(CommonOrder commonOrder, BigDecimal fanliRate, Long hongBaoId)
+	private HongBaoV2 createShareUpdateHongBao(CommonOrder commonOrder, BigDecimal money, Long hongBaoId)
 			throws HongBaoException, UserAccountException {
 		HongBaoV2 hongBao = new HongBaoV2(hongBaoId);
 		hongBao.setUpdateTime(new Date());
+		hongBao.setMoney(money);
 		// 鏇存敼鐘舵�佷笌璧勯噾
 		if (commonOrder.getState() == CommonOrder.STATE_FK) {
 			hongBao.setState(HongBaoV2.STATE_BUKELINGQU);
-			hongBao.setMoney(MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), fanliRate.divide(new BigDecimal(100))));
 		} else if (commonOrder.getState() == CommonOrder.STATE_JS || commonOrder.getState() == CommonOrder.STATE_WQ) {
 			hongBao.setState(HongBaoV2.STATE_KELINGQU);
-			hongBao.setMoney(MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), fanliRate.divide(new BigDecimal(100))));
 			Calendar calendar = Calendar.getInstance();
 			calendar.setTime(commonOrder.getSettleTime());
 			calendar.add(Calendar.MONTH, 1);
@@ -822,6 +839,7 @@
 			tempHongBao.setMoney(hongBao.getMoney());
 			CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
 			tempCommonOrder.setPayment(commonOrder.getPayment());
+			tempCommonOrder.setThirdCreateTime(commonOrder.getThirdCreateTime());
 			notificationMap.put(hongBao.getType(), new HongBaoOrder(tempCommonOrder, tempHongBao));
 		} else {
 			// 澧炲姞浠樻閲戦涓庤祫閲�
@@ -843,18 +861,17 @@
 	 * @throws HongBaoException
 	 * @throws UserAccountException
 	 */
-	private HongBaoV2 createShareHongBao(CommonOrder commonOrder, BigDecimal shareRate, boolean isVip)
+	private HongBaoV2 createShareHongBao(CommonOrder commonOrder, BigDecimal money, UserLevelEnum userLevel)
 			throws HongBaoException, UserAccountException {
 		HongBaoV2 hongBao = new HongBaoV2();
 		hongBao.setCreateTime(new Date());
 		hongBao.setType(HongBaoV2.TYPE_SHARE_GOODS);
 		hongBao.setVersion(2);
+		hongBao.setMoney(money);
 		if (commonOrder.getState() == CommonOrder.STATE_FK) {
 			hongBao.setState(HongBaoV2.STATE_BUKELINGQU);
-			hongBao.setMoney(MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), shareRate.divide(new BigDecimal(100))));
 		} else if (commonOrder.getState() == CommonOrder.STATE_JS || commonOrder.getState() == CommonOrder.STATE_WQ) {
 			hongBao.setState(HongBaoV2.STATE_KELINGQU);
-			hongBao.setMoney(MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), shareRate.divide(new BigDecimal(100))));
 			Calendar calendar = Calendar.getInstance();
 			calendar.setTime(commonOrder.getSettleTime());
 			calendar.add(Calendar.MONTH, 1);
@@ -864,12 +881,7 @@
 		UserInfo user = userInfoService.getUserById(commonOrder.getUserInfo().getId());
 		if (user == null)
 			throw new UserAccountException(1001, "鐢ㄦ埛涓嶅瓨鍦�/琚皝绂�");
-		UserLevelEnum userLevel = userLevelManager.getUserLevel(commonOrder.getUserInfo().getId(),
-				commonOrder.getThirdCreateTime());
-		if (userLevel != null)
-			hongBao.setUrank(userLevel.getOrderRank());
-		else
-			hongBao.setUrank(user.getRank());
+		hongBao.setUrank(userLevel.getOrderRank());
 		hongBao.setUserInfo(user);
 		hongBao.setOrderType(commonOrder.getSourceType());
 		return hongBao;

--
Gitblit v1.8.0