From b3007afcb9b8ca4302497a2c1c3e0d990b10a388 Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期四, 03 一月 2019 14:52:40 +0800
Subject: [PATCH] 新版订单与返利

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/hongbao/HongBaoV2ServiceImpl.java |  123 +++++++++++++++++++++++++++++++---------
 1 files changed, 95 insertions(+), 28 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/hongbao/HongBaoV2ServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/hongbao/HongBaoV2ServiceImpl.java
index 9c32806..634c724 100644
--- a/fanli/src/main/java/com/yeshi/fanli/service/impl/hongbao/HongBaoV2ServiceImpl.java
+++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/hongbao/HongBaoV2ServiceImpl.java
@@ -20,6 +20,7 @@
 import com.yeshi.fanli.dao.mybatis.order.OrderItemMapper;
 import com.yeshi.fanli.dao.mybatis.order.OrderMapper;
 import com.yeshi.fanli.dao.mybatis.taobao.TaoBaoOrderMapper;
+import com.yeshi.fanli.dao.mybatis.taobao.TaoBaoWeiQuanOrderMapper;
 import com.yeshi.fanli.entity.bus.user.HongBao;
 import com.yeshi.fanli.entity.bus.user.HongBaoV2;
 import com.yeshi.fanli.entity.bus.user.UserInfo;
@@ -27,6 +28,7 @@
 import com.yeshi.fanli.entity.order.CommonOrderGoods;
 import com.yeshi.fanli.entity.order.HongBaoOrder;
 import com.yeshi.fanli.entity.taobao.TaoBaoOrder;
+import com.yeshi.fanli.entity.taobao.TaoBaoWeiQuanOrder;
 import com.yeshi.fanli.exception.HongBaoException;
 import com.yeshi.fanli.service.inter.hongbao.HongBaoManageService;
 import com.yeshi.fanli.service.inter.hongbao.HongBaoV2Service;
@@ -70,6 +72,9 @@
 
 	@Resource
 	private UserInfoService userInfoService;
+
+	@Resource
+	private TaoBaoWeiQuanOrderMapper taoBaoWeiQuanOrderMapper;
 
 	@Override
 	public int insert(HongBaoV2 record) {
@@ -141,7 +146,6 @@
 			if (hb.getOrder() != null) {
 				orderId = hb.getOrder().getOrderId();
 				// 鏌ヨ鍚屼竴璁㈠崟鍙风殑闈炲瓙绾㈠寘
-
 			} else
 				orderId = hb.getOrderId();
 
@@ -189,9 +193,15 @@
 				if (list.size() <= 0)
 					return;
 				List<TaoBaoOrder> orderList = taoBaoOrderMapper.selectTaoBaoOrderByOrderId(orderId);
+
 				if (orderList == null || orderList.size() < list.size()) {
 					System.out.println("绾㈠寘鏁颁笌璁㈠崟鏁颁笉瀵瑰簲");
 					return;
+				}
+
+				// 鍔犲叆鎺掑簭鍊�
+				for (int i = 0; i < orderList.size(); i++) {
+					orderList.get(i).setOrderBy(i + 1);
 				}
 
 				// 鎸夌収auctionId,payMoney鎺掑簭
@@ -237,6 +247,11 @@
 					commonOrder.setCreateTime(new Date(list.get(i).getCreatetime()));
 
 					// TODO 缁存潈鍒ゆ柇
+					List<TaoBaoWeiQuanOrder> weiQuanList = taoBaoWeiQuanOrderMapper.selectListByOrderIdAndState(orderId,
+							"缁存潈鎴愬姛");
+					if (weiQuanList != null && weiQuanList.size() > 0)
+						commonOrder.setState(CommonOrder.STATE_WQ);
+
 					commonOrderMapper.insertSelective(commonOrder);
 
 					// 鎻掑叆绾㈠寘
@@ -287,21 +302,30 @@
 			throw new HongBaoException(1, "璁㈠崟淇℃伅涓嶅畬鏁�");
 		HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId());
 		if (hongBaoOrder == null) {
-
+			saveHongBao(commonOrder, type);
 		} else {
-
+			updateHongBao(hongBaoOrder, commonOrder, type);
 		}
 
 	}
 
 	@Transactional
 	private void updateHongBao(HongBaoOrder hongBaoOrder, CommonOrder commonOrder, int type) throws HongBaoException {
+		System.out.println(commonOrder.getOrderNo());
+		HongBaoV2 oldHongBao = hongBaoV2Mapper.selectByPrimaryKey(hongBaoOrder.getHongBaoV2().getId());
+		if (oldHongBao == null)
+			throw new HongBaoException(10, "绾㈠寘瀵硅薄涓嶅瓨鍦�");
+		// 宸茬粡澶辨晥鎴栬�呭凡缁忛鍙栫殑绾㈠寘涓嶅仛澶勭悊
+		if (oldHongBao.getState() == HongBaoV2.STATE_SHIXIAO || oldHongBao.getState() == HongBaoV2.STATE_YILINGQU)
+			return;
+
 		if (type == HongBaoV2.TYPE_ZIGOU) {// 鑾峰彇鑷喘鐨勮繑鍒╂瘮渚�
 			BigDecimal fanliRate = hongBaoManageService.getFanLiRate(commonOrder.getCreateTime().getTime());
 			HongBaoV2 hongBao = new HongBaoV2(hongBaoOrder.getHongBaoV2().getId());
 			hongBao.setUpdateTime(new Date());
+			// 鏇存敼鐘舵�佷笌璧勯噾
 			if (commonOrder.getState() == CommonOrder.STATE_FK) {
-				hongBao.setState(HongBaoV2.STATE_BUFENSHIXIAO);
+				hongBao.setState(HongBaoV2.STATE_BUKELINGQU);
 				hongBao.setMoney(
 						MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), fanliRate.divide(new BigDecimal(100))));
 			} else if (commonOrder.getState() == CommonOrder.STATE_JS) {
@@ -311,24 +335,27 @@
 				hongBao.setPreGetTime(new Date(commonOrder.getSettleTime().getTime() + 1000 * 60 * 60 * 24 * 15L));
 			} else if (commonOrder.getState() == CommonOrder.STATE_SX) {
 				hongBao.setState(HongBaoV2.STATE_SHIXIAO);
+				hongBao.setMoney(new BigDecimal(0));
 			}
 			hongBaoV2Mapper.updateByPrimaryKeySelective(hongBao);
+
 			// 鑾峰彇瀛愮孩鍖�
-			List<HongBaoV2> children = hongBaoV2Mapper.listChildrenById(hongBaoOrder.getId());
+			List<HongBaoV2> children = hongBaoV2Mapper.listChildrenById(hongBao.getId());
 			if (children != null)
 				for (HongBaoV2 child : children) {
 					HongBaoV2 childUpdate = new HongBaoV2(child.getId());
 					childUpdate.setState(hongBao.getState());
 					childUpdate.setUpdateTime(new Date());
 					BigDecimal rate = null;
-					if (child.getType() == HongBaoV2.TYPE_YIJI) {
+					if (child.getType() == HongBaoV2.TYPE_YIJI) {// 涓�绾у垎浜禋
 						rate = hongBaoManageService.getFirstInviteRate(child.getUrank(),
 								commonOrder.getCreateTime().getTime());
-					} else if (child.getType() == HongBaoV2.TYPE_ERJI) {
+					} else if (child.getType() == HongBaoV2.TYPE_ERJI) {// 浜岀骇鍒嗕韩璧�
 						rate = hongBaoManageService.getSecondInviteRate(child.getUrank(),
 								commonOrder.getCreateTime().getTime());
 					}
 
+					// 浠ュ疄闄呮敹鍏ヤ负鍑嗚绠楅浼版敹鐩�
 					if (hongBao.getState() == HongBaoV2.STATE_KELINGQU) {
 						childUpdate.setMoney(
 								MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), rate.divide(new BigDecimal(100))));
@@ -346,36 +373,65 @@
 				}
 
 		} else if (type == HongBaoV2.TYPE_SHARE_GOODS) {
-			// TODO 鍒嗕韩璧氱殑浜岀骇鏇存柊
-			// 鍒嗕韩璧�
-			BigDecimal shareRate = hongBaoManageService.getShareRate(commonOrder.getCreateTime().getTime());
-			HongBaoV2 hongBao = new HongBaoV2();
-			hongBao.setCreateTime(new Date());
-			hongBao.setType(HongBaoV2.TYPE_SHARE_GOODS);
-			hongBao.setVersion(2);
+			BigDecimal fanliRate = hongBaoManageService.getFanLiRate(commonOrder.getCreateTime().getTime());
+			HongBaoV2 hongBao = new HongBaoV2(hongBaoOrder.getHongBaoV2().getId());
+			hongBao.setUpdateTime(new Date());
+			// 鏇存敼鐘舵�佷笌璧勯噾
 			if (commonOrder.getState() == CommonOrder.STATE_FK) {
-				hongBao.setState(HongBaoV2.STATE_BUFENSHIXIAO);
+				hongBao.setState(HongBaoV2.STATE_BUKELINGQU);
 				hongBao.setMoney(
-						MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), shareRate.divide(new BigDecimal(100))));
+						MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), fanliRate.divide(new BigDecimal(100))));
 			} else if (commonOrder.getState() == CommonOrder.STATE_JS) {
 				hongBao.setState(HongBaoV2.STATE_KELINGQU);
 				hongBao.setMoney(
-						MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), shareRate.divide(new BigDecimal(100))));
+						MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), fanliRate.divide(new BigDecimal(100))));
 				Calendar calendar = Calendar.getInstance();
 				calendar.setTime(commonOrder.getSettleTime());
 				calendar.add(Calendar.MONTH, 1);
 				hongBao.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
 						calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25", "yyyy-M-dd")));
+			} else if (commonOrder.getState() == CommonOrder.STATE_SX) {
+				hongBao.setState(HongBaoV2.STATE_SHIXIAO);
+				hongBao.setMoney(new BigDecimal(0));
 			}
-			UserInfo user = userInfoService.getUserById(commonOrder.getUserInfo().getId());
-			hongBao.setUrank(user.getRank());
-			hongBaoV2Mapper.insertSelective(hongBao);
-			// 鎻掑叆绾㈠寘涓庤鍗曟槧灏�
-			HongBaoOrder hongBaoOrder = new HongBaoOrder();
-			hongBaoOrder.setCommonOrder(commonOrder);
-			hongBaoOrder.setCreateTime(new Date());
-			hongBaoOrder.setHongBaoV2(hongBao);
-			hongBaoOrderMapper.insertSelective(hongBaoOrder);
+			hongBaoV2Mapper.updateByPrimaryKeySelective(hongBao);
+
+			// 鑾峰彇瀛愮孩鍖�
+			List<HongBaoV2> children = hongBaoV2Mapper.listChildrenById(hongBao.getId());
+			if (children != null)
+				for (HongBaoV2 child : children) {
+					HongBaoV2 childUpdate = new HongBaoV2(child.getId());
+					// 缁熶竴璁剧疆鐘舵��
+					childUpdate.setState(hongBao.getState());
+					childUpdate.setUpdateTime(new Date());
+					BigDecimal rate = null;
+					if (child.getType() == HongBaoV2.TYPE_SHARE_YIJI) {// 涓�绾у垎浜禋
+						rate = hongBaoManageService.getFirstShareRate(child.getUrank(),
+								commonOrder.getCreateTime().getTime());
+					} else if (child.getType() == HongBaoV2.TYPE_SHARE_ERJI) {// 浜岀骇鍒嗕韩璧�
+						rate = hongBaoManageService.getSecondShareRate(child.getUrank(),
+								commonOrder.getCreateTime().getTime());
+					}
+
+					// 浠ュ疄闄呮敹鍏ヤ负鍑嗚绠楅浼版敹鐩�
+					if (CommonOrder.STATE_JS == commonOrder.getState()) {
+						childUpdate.setMoney(
+								MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), rate.divide(new BigDecimal(100))));
+						Calendar calendar = Calendar.getInstance();
+						calendar.setTime(commonOrder.getSettleTime());
+						calendar.add(Calendar.MONTH, 1);
+						childUpdate.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
+								calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
+								"yyyy-M-dd")));
+					} else if (CommonOrder.STATE_FK == commonOrder.getState()) {
+						if (commonOrder.getOrderNo().equalsIgnoreCase("314134177659155087"))
+							System.out.println("");
+
+						childUpdate.setMoney(
+								MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), rate.divide(new BigDecimal(100))));
+					}
+					hongBaoV2Mapper.updateByPrimaryKeySelective(childUpdate);
+				}
 		} else
 			throw new HongBaoException(2, "type閿欒");
 	}
@@ -385,11 +441,12 @@
 		if (type == HongBaoV2.TYPE_ZIGOU) {// 鑾峰彇鑷喘鐨勮繑鍒╂瘮渚�
 			BigDecimal fanliRate = hongBaoManageService.getFanLiRate(commonOrder.getCreateTime().getTime());
 			HongBaoV2 hongBao = new HongBaoV2();
+			hongBao.setUserInfo(commonOrder.getUserInfo());
 			hongBao.setCreateTime(new Date());
 			hongBao.setType(HongBaoV2.TYPE_ZIGOU);
 			hongBao.setVersion(2);
 			if (commonOrder.getState() == CommonOrder.STATE_FK) {
-				hongBao.setState(HongBaoV2.STATE_BUFENSHIXIAO);
+				hongBao.setState(HongBaoV2.STATE_BUKELINGQU);
 				hongBao.setMoney(
 						MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), fanliRate.divide(new BigDecimal(100))));
 			} else if (commonOrder.getState() == CommonOrder.STATE_JS) {
@@ -419,7 +476,11 @@
 				// 鎻掑叆涓�绾у瓙绾㈠寘
 				BigDecimal firstRate = hongBaoManageService
 						.getFirstInviteRate(boss.getRank() == null ? 0 : boss.getRank());
+				if (firstRate.compareTo(new BigDecimal(0)) <= 0)
+					return;
 				HongBaoV2 firstHongbao = new HongBaoV2();
+				firstHongbao.setUserInfo(boss);
+				firstHongbao.setUrank(boss.getRank());
 				firstHongbao.setParent(hongBao);
 				firstHongbao.setCreateTime(new Date());
 				firstHongbao.setType(HongBaoV2.TYPE_YIJI);
@@ -446,7 +507,11 @@
 				if (boss != null) {// 浜岀骇BOSS瀛樺湪
 					BigDecimal secondRate = hongBaoManageService
 							.getSecondInviteRate(boss.getRank() == null ? 0 : boss.getRank());
+					if (secondRate.compareTo(new BigDecimal(0)) <= 0)
+						return;
 					HongBaoV2 secondHongbao = new HongBaoV2();
+					secondHongbao.setUserInfo(boss);
+					secondHongbao.setUrank(boss.getRank());
 					secondHongbao.setParent(hongBao);
 					secondHongbao.setCreateTime(new Date());
 					secondHongbao.setType(HongBaoV2.TYPE_ERJI);
@@ -467,6 +532,7 @@
 					}
 					hongBaoV2Mapper.insertSelective(secondHongbao);
 				}
+
 			}
 
 		} else if (type == HongBaoV2.TYPE_SHARE_GOODS) {
@@ -480,7 +546,7 @@
 			hongBao.setType(HongBaoV2.TYPE_SHARE_GOODS);
 			hongBao.setVersion(2);
 			if (commonOrder.getState() == CommonOrder.STATE_FK) {
-				hongBao.setState(HongBaoV2.STATE_BUFENSHIXIAO);
+				hongBao.setState(HongBaoV2.STATE_BUKELINGQU);
 				hongBao.setMoney(
 						MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), shareRate.divide(new BigDecimal(100))));
 			} else if (commonOrder.getState() == CommonOrder.STATE_JS) {
@@ -495,6 +561,7 @@
 			}
 			UserInfo user = userInfoService.getUserById(commonOrder.getUserInfo().getId());
 			hongBao.setUrank(user.getRank());
+			hongBao.setUserInfo(user);
 			hongBaoV2Mapper.insertSelective(hongBao);
 			// 鎻掑叆绾㈠寘涓庤鍗曟槧灏�
 			HongBaoOrder hongBaoOrder = new HongBaoOrder();

--
Gitblit v1.8.0