From 02d8f36a2a51a2ffcacdb619bee34077da61b328 Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期四, 10 一月 2019 15:23:42 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/hongbao/HongBaoV2ServiceImpl.java |  241 ++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 210 insertions(+), 31 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 634c724..c268891 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
@@ -5,7 +5,10 @@
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
 import javax.annotation.Resource;
 
@@ -13,6 +16,7 @@
 import org.springframework.transaction.annotation.Transactional;
 
 import com.yeshi.fanli.dao.mybatis.HongBaoV2Mapper;
+import com.yeshi.fanli.dao.mybatis.PidOrderMapper;
 import com.yeshi.fanli.dao.mybatis.hongbao.HongBaoMapper;
 import com.yeshi.fanli.dao.mybatis.order.CommonOrderGoodsMapper;
 import com.yeshi.fanli.dao.mybatis.order.CommonOrderMapper;
@@ -21,20 +25,26 @@
 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.dto.HongBaoDTO;
 import com.yeshi.fanli.entity.bus.user.HongBao;
 import com.yeshi.fanli.entity.bus.user.HongBaoV2;
 import com.yeshi.fanli.entity.bus.user.UserInfo;
 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.entity.taobao.PidOrder;
 import com.yeshi.fanli.entity.taobao.TaoBaoOrder;
 import com.yeshi.fanli.entity.taobao.TaoBaoWeiQuanOrder;
 import com.yeshi.fanli.exception.HongBaoException;
+import com.yeshi.fanli.exception.share.UserShareGoodsRecordException;
+import com.yeshi.fanli.log.LogHelper;
 import com.yeshi.fanli.service.inter.hongbao.HongBaoManageService;
 import com.yeshi.fanli.service.inter.hongbao.HongBaoV2Service;
 import com.yeshi.fanli.service.inter.hongbao.ThreeSaleSerivce;
 import com.yeshi.fanli.service.inter.order.CommonOrderService;
 import com.yeshi.fanli.service.inter.user.UserInfoService;
+import com.yeshi.fanli.service.inter.user.UserNotificationService;
+import com.yeshi.fanli.service.inter.user.UserShareGoodsGroupService;
 import com.yeshi.fanli.util.Constant;
 import com.yeshi.fanli.util.MoneyBigDecimalUtil;
 import com.yeshi.fanli.util.TimeUtil;
@@ -75,6 +85,12 @@
 
 	@Resource
 	private TaoBaoWeiQuanOrderMapper taoBaoWeiQuanOrderMapper;
+
+	@Resource
+	private UserShareGoodsGroupService userShareGoodsGroupService;
+
+	@Resource
+	private PidOrderMapper pidOrderMapper;
 
 	@Override
 	public int insert(HongBaoV2 record) {
@@ -119,12 +135,22 @@
 		if (hb.getParent() != null)
 			v2.setParent(new HongBaoV2(hb.getParent().getId()));
 
-		if (hb.getPreGettime() == null || hb.getPreGettime() == 0)
-			v2.setPreGetTime(null);
-		else
-			v2.setPreGetTime(new Date(hb.getPreGettime()));
+		if (hb.getType() == HongBao.TYPE_SHARE_GOODS) {
+			if (hb.getBalanceTime() != null)
+				v2.setPreGetTime(new Date(hb.getPreGettime()));
+			else
+				v2.setPreGetTime(null);
+		} else {
+			if (hb.getPreGettime() == null || hb.getPreGettime() == 0)
+				v2.setPreGetTime(null);
+			else
+				v2.setPreGetTime(new Date(hb.getPreGettime()));
+		}
 
 		v2.setState(hb.getState());
+		if (hb.getState() == 1 && hb.getBalanceTime() != null) {
+			v2.setState(HongBaoV2.STATE_KELINGQU);
+		}
 		v2.setType(type);
 		v2.setUpdateTime(new Date(hb.getCreatetime()));
 		v2.setUrank(hb.getUrank());
@@ -192,6 +218,14 @@
 
 				if (list.size() <= 0)
 					return;
+				if (list.get(0).getType() == 20)
+					for (HongBao hb1 : list) {
+						PidOrder pidOrder = pidOrderMapper.getPidOrderByHongBaoId(hb1.getId());
+						if (pidOrder != null)
+							hb1.setAuctionId(pidOrder.getAuctionId());
+
+					}
+
 				List<TaoBaoOrder> orderList = taoBaoOrderMapper.selectTaoBaoOrderByOrderId(orderId);
 
 				if (orderList == null || orderList.size() < list.size()) {
@@ -206,12 +240,24 @@
 
 				// 鎸夌収auctionId,payMoney鎺掑簭
 				Comparator<HongBao> cm = new Comparator<HongBao>() {
-
 					@Override
 					public int compare(HongBao o1, HongBao o2) {
-						return o1.getAuctionId() == o2.getAuctionId()
-								? (o1.getPayMoney().subtract(o2.getPayMoney()).compareTo(new BigDecimal(0)))
-								: (int) (o1.getAuctionId() - o2.getAuctionId());
+						if (o1.getAuctionId().longValue() < o2.getAuctionId().longValue())
+							return -3;
+						else if (o1.getAuctionId().longValue() > o2.getAuctionId().longValue()) {
+							return 3;
+						} else {
+							if (o1.getState() - o2.getState() > 0)
+								return 2;
+							else if (o1.getState() - o2.getState() < 0)
+								return -2;
+							else {
+								if (o1.getPayMoney().compareTo(o2.getPayMoney()) >= 0)
+									return 1;
+								else
+									return -1;
+							}
+						}
 					}
 				};
 
@@ -221,13 +267,24 @@
 
 					@Override
 					public int compare(TaoBaoOrder o1, TaoBaoOrder o2) {
-						return o1.getAuctionId() == o2.getAuctionId()
-								? (o1.getPayment().subtract(o2.getPayment()).compareTo(new BigDecimal(0)))
-								: (int) (o1.getAuctionId() - o2.getAuctionId());
+
+						if (o1.getAuctionId().longValue() < o2.getAuctionId().longValue())
+							return -3;
+						else if (o1.getAuctionId().longValue() > o2.getAuctionId().longValue()) {
+							return 3;
+						} else {
+							if (o1.getPayment().compareTo(o2.getPayment()) < 0)
+								return 2;
+							else
+								return -2;
+						}
 					}
 				};
 
 				Collections.sort(orderList, cmOrder);
+
+				for (TaoBaoOrder t : orderList)
+					System.out.println(t.getAuctionId());
 
 				for (int i = 0; i < list.size(); i++) {
 					CommonOrder commonOrder = TaoBaoOrderUtil.convert(orderList.get(i));
@@ -295,18 +352,51 @@
 	@Resource
 	private ThreeSaleSerivce threeSaleSerivce;
 
+	@Resource
+	private UserNotificationService userNotificationService;
+
 	@Transactional
 	@Override
-	public void addHongBao(CommonOrder commonOrder, int type) throws HongBaoException {
-		if (commonOrder == null || commonOrder.getId() == null || commonOrder.getUserInfo() == null)
-			throw new HongBaoException(1, "璁㈠崟淇℃伅涓嶅畬鏁�");
-		HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId());
-		if (hongBaoOrder == null) {
-			saveHongBao(commonOrder, type);
-		} else {
-			updateHongBao(hongBaoOrder, commonOrder, type);
-		}
+	public void addHongBao(List<CommonOrder> commonOrderList, int type) throws HongBaoException {
 
+		if (commonOrderList != null && commonOrderList.size() > 0) {
+			Map<Integer, HongBaoV2> notificationMap = new HashMap<>();
+			for (CommonOrder commonOrder : commonOrderList) {
+				if (commonOrder == null || commonOrder.getId() == null || commonOrder.getUserInfo() == null)
+					throw new HongBaoException(1, "璁㈠崟淇℃伅涓嶅畬鏁�");
+				HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId());
+				if (hongBaoOrder == null) {
+					saveHongBao(commonOrder, type, notificationMap);
+				} else {
+					updateHongBao(hongBaoOrder, commonOrder, type);
+				}
+			}
+			/**
+			 * 閫氱煡鐢ㄦ埛鐨勮繑鍒╂儏鍐�
+			 */
+			Iterator<Integer> its = notificationMap.keySet().iterator();
+			while (its.hasNext()) {
+				Integer t = its.next();
+				HongBaoV2 notify = notificationMap.get(t);
+				String orderId = commonOrderList.get(0).getOrderNo();
+				Long uid = notify.getUserInfo().getId();
+				BigDecimal money = notify.getMoney();
+				switch (t) {
+				case HongBaoV2.TYPE_ZIGOU:
+					userNotificationService.orderFanliStatisticed(uid, orderId);
+					break;
+				case HongBaoV2.TYPE_SHARE_GOODS:
+					userNotificationService.orderShareStatisticed(uid, orderId, money);
+					break;
+				case HongBaoV2.TYPE_YIJI:
+					userNotificationService.orderInviteStatisticed(uid, orderId, money);
+					break;
+				case HongBaoV2.TYPE_ERJI:
+					userNotificationService.orderInviteStatisticed(uid, orderId, money);
+					break;
+				}
+			}
+		}
 	}
 
 	@Transactional
@@ -358,7 +448,7 @@
 					// 浠ュ疄闄呮敹鍏ヤ负鍑嗚绠楅浼版敹鐩�
 					if (hongBao.getState() == HongBaoV2.STATE_KELINGQU) {
 						childUpdate.setMoney(
-								MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), rate.divide(new BigDecimal(100))));
+								MoneyBigDecimalUtil.mul(hongBao.getMoney(), rate.divide(new BigDecimal(100))));
 						Calendar calendar = Calendar.getInstance();
 						calendar.setTime(commonOrder.getSettleTime());
 						calendar.add(Calendar.MONTH, 1);
@@ -367,7 +457,7 @@
 								"yyyy-M-dd")));
 					} else if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU) {
 						childUpdate.setMoney(
-								MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), rate.divide(new BigDecimal(100))));
+								MoneyBigDecimalUtil.mul(hongBao.getMoney(), rate.divide(new BigDecimal(100))));
 					}
 					hongBaoV2Mapper.updateByPrimaryKeySelective(childUpdate);
 				}
@@ -416,7 +506,7 @@
 					// 浠ュ疄闄呮敹鍏ヤ负鍑嗚绠楅浼版敹鐩�
 					if (CommonOrder.STATE_JS == commonOrder.getState()) {
 						childUpdate.setMoney(
-								MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), rate.divide(new BigDecimal(100))));
+								MoneyBigDecimalUtil.mul(hongBao.getMoney(), rate.divide(new BigDecimal(100))));
 						Calendar calendar = Calendar.getInstance();
 						calendar.setTime(commonOrder.getSettleTime());
 						calendar.add(Calendar.MONTH, 1);
@@ -428,7 +518,7 @@
 							System.out.println("");
 
 						childUpdate.setMoney(
-								MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), rate.divide(new BigDecimal(100))));
+								MoneyBigDecimalUtil.mul(hongBao.getMoney(), rate.divide(new BigDecimal(100))));
 					}
 					hongBaoV2Mapper.updateByPrimaryKeySelective(childUpdate);
 				}
@@ -437,7 +527,8 @@
 	}
 
 	@Transactional
-	private void saveHongBao(CommonOrder commonOrder, int type) throws HongBaoException {
+	private void saveHongBao(CommonOrder commonOrder, int type, Map<Integer, HongBaoV2> notificationMap)
+			throws HongBaoException {
 		if (type == HongBaoV2.TYPE_ZIGOU) {// 鑾峰彇鑷喘鐨勮繑鍒╂瘮渚�
 			BigDecimal fanliRate = hongBaoManageService.getFanLiRate(commonOrder.getCreateTime().getTime());
 			HongBaoV2 hongBao = new HongBaoV2();
@@ -469,6 +560,12 @@
 			hongBaoOrder.setCreateTime(new Date());
 			hongBaoOrder.setHongBaoV2(hongBao);
 			hongBaoOrderMapper.insertSelective(hongBaoOrder);
+			// 鍔犲叆閫氱煡
+			if (notificationMap.get(type) == null)
+				notificationMap.put(type, hongBao);
+			else {
+				notificationMap.get(type).setMoney(notificationMap.get(type).getMoney().add(hongBao.getMoney()));
+			}
 
 			UserInfo boss = threeSaleSerivce.getBoss(user.getId());
 			if (boss != null && hongBao.getState() != HongBaoV2.STATE_SHIXIAO) {// 1绾OSS瀛樺湪涓旂孩鍖呮湭澶辨晥
@@ -489,7 +586,7 @@
 
 				if (hongBao.getState() == HongBaoV2.STATE_KELINGQU) {
 					firstHongbao.setMoney(
-							MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), firstRate.divide(new BigDecimal(100))));
+							MoneyBigDecimalUtil.mul(hongBao.getMoney(), firstRate.divide(new BigDecimal(100))));
 					Calendar calendar = Calendar.getInstance();
 					calendar.setTime(commonOrder.getSettleTime());
 					calendar.add(Calendar.MONTH, 1);
@@ -498,9 +595,16 @@
 							"yyyy-M-dd")));
 				} else if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU) {
 					firstHongbao.setMoney(
-							MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), firstRate.divide(new BigDecimal(100))));
+							MoneyBigDecimalUtil.mul(hongBao.getMoney(), firstRate.divide(new BigDecimal(100))));
 				}
 				hongBaoV2Mapper.insertSelective(firstHongbao);
+				// 鐢ㄦ埛閫氱煡
+				if (notificationMap.get(HongBaoV2.TYPE_YIJI) == null)
+					notificationMap.put(HongBaoV2.TYPE_YIJI, firstHongbao);
+				else {
+					notificationMap.get(HongBaoV2.TYPE_YIJI)
+							.setMoney(notificationMap.get(HongBaoV2.TYPE_YIJI).getMoney().add(firstHongbao.getMoney()));
+				}
 
 				// 鎻掑叆浜岀骇瀛愮孩鍖�
 				boss = threeSaleSerivce.getBoss(boss.getId());
@@ -518,8 +622,8 @@
 					secondHongbao.setVersion(2);
 					secondHongbao.setState(hongBao.getState());
 					if (hongBao.getState() == HongBaoV2.STATE_KELINGQU) {
-						firstHongbao.setMoney(MoneyBigDecimalUtil.mul(commonOrder.geteIncome(),
-								secondRate.divide(new BigDecimal(100))));
+						secondHongbao.setMoney(
+								MoneyBigDecimalUtil.mul(hongBao.getMoney(), secondRate.divide(new BigDecimal(100))));
 						Calendar calendar = Calendar.getInstance();
 						calendar.setTime(commonOrder.getSettleTime());
 						calendar.add(Calendar.MONTH, 1);
@@ -527,10 +631,18 @@
 								calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
 								"yyyy-M-dd")));
 					} else if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU) {
-						secondHongbao.setMoney(MoneyBigDecimalUtil.mul(commonOrder.getEstimate(),
-								secondRate.divide(new BigDecimal(100))));
+						secondHongbao.setMoney(
+								MoneyBigDecimalUtil.mul(hongBao.getMoney(), secondRate.divide(new BigDecimal(100))));
 					}
 					hongBaoV2Mapper.insertSelective(secondHongbao);
+
+					// 鐢ㄦ埛閫氱煡
+					if (notificationMap.get(HongBaoV2.TYPE_ERJI) == null)
+						notificationMap.put(HongBaoV2.TYPE_ERJI, secondHongbao);
+					else {
+						notificationMap.get(HongBaoV2.TYPE_ERJI).setMoney(
+								notificationMap.get(HongBaoV2.TYPE_ERJI).getMoney().add(secondHongbao.getMoney()));
+					}
 				}
 
 			}
@@ -569,7 +681,74 @@
 			hongBaoOrder.setCreateTime(new Date());
 			hongBaoOrder.setHongBaoV2(hongBao);
 			hongBaoOrderMapper.insertSelective(hongBaoOrder);
+
+			if (notificationMap.get(type) == null)
+				notificationMap.put(type, hongBao);
+			else {
+				notificationMap.get(type).setMoney(notificationMap.get(type).getMoney().add(hongBao.getMoney()));
+			}
+
+			// 淇敼缁熻鏁版嵁
+			Long goodsId = commonOrder.getCommonOrderGoods().getId();
+			// 鏌ヨ鍟嗗搧
+			CommonOrderGoods goods = commonOrderGoodsMapper.selectByPrimaryKey(goodsId);
+
+			HongBao hb = new HongBao();
+			if (goods != null)
+				hb.setAuctionId(Long.parseLong(goods.getGoodsId()));
+			hb.setUserInfo(hongBao.getUserInfo());
+			hb.setMoney(hongBao.getMoney());
+
+			try {
+				userShareGoodsGroupService.updateOrderRecord(hb);
+			} catch (UserShareGoodsRecordException e) {
+				try {
+					LogHelper.errorDetailInfo(e);
+				} catch (Exception e1) {
+					e1.printStackTrace();
+				}
+			}
+
 		} else
 			throw new HongBaoException(2, "type閿欒");
 	}
+
+	@Override
+	public List<HongBaoDTO> listJiangJinByUid(Long uid, int page, int pageSize) {
+		return hongBaoV2Mapper.listJiangJinByUid(uid, (page - 1) * pageSize, pageSize);
+	}
+
+	@Override
+	public long countJiangJinByUid(Long uid) {
+		Long count = hongBaoV2Mapper.countJiangJinByUid(uid);
+		return count == null ? 0 : count;
+	}
+
+	@Override
+	public BigDecimal getTotalTiChengMoney(Long uid) {
+
+		return hongBaoV2Mapper.getTotalTiChengMoney(uid);
+	}
+
+	@Override
+	public int getTotalTiChengCount(Long uid) {
+		return hongBaoV2Mapper.getTotalTiChengCount(uid);
+	}
+
+	@Override
+	public BigDecimal getUnGetTiChengMoney(Long uid) {
+		return hongBaoV2Mapper.getUnGetTiChengMoney(uid);
+	}
+
+	@Override
+	public BigDecimal getTotalFanLiMoney(Long uid) {
+		BigDecimal money = hongBaoV2Mapper.getTotalFanLiMoney(uid);
+		return money == null ? new BigDecimal(0) : money;
+	}
+
+	@Override
+	public BigDecimal getUnRecievedFanLiMoney(Long uid) {
+		BigDecimal money = hongBaoV2Mapper.getUnRecievedFanLiMoney(uid);
+		return money == null ? new BigDecimal(0) : money;
+	}
 }

--
Gitblit v1.8.0