From dc5be7d38446f70e6ff86df311119c32b41fe7f8 Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期六, 28 十一月 2020 16:37:05 +0800 Subject: [PATCH] 大淘客搜索接口升级 --- fanli/src/main/java/com/yeshi/fanli/service/impl/order/OrderMoneySettleServiceImpl.java | 1515 +++++++++++++++++++++++----------------------------------- 1 files changed, 599 insertions(+), 916 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/order/OrderMoneySettleServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/order/OrderMoneySettleServiceImpl.java index d4d5d56..c035a31 100644 --- a/fanli/src/main/java/com/yeshi/fanli/service/impl/order/OrderMoneySettleServiceImpl.java +++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/order/OrderMoneySettleServiceImpl.java @@ -1,9 +1,7 @@ package com.yeshi.fanli.service.impl.order; import java.math.BigDecimal; -import java.math.RoundingMode; import java.util.ArrayList; -import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.HashSet; @@ -30,1032 +28,717 @@ import com.yeshi.fanli.dto.HongBao; import com.yeshi.fanli.dto.mq.order.OrderTopicTagEnum; import com.yeshi.fanli.dto.mq.order.body.OrderMoneyRecievedMQMsg; +import com.yeshi.fanli.dto.mq.order.body.TeamRewardPreRecieveMQMsg; import com.yeshi.fanli.entity.bus.user.HongBaoV2; import com.yeshi.fanli.entity.bus.user.UserInfo; +import com.yeshi.fanli.entity.money.TeamRewardDebt; import com.yeshi.fanli.entity.money.UserMoneyDetail; import com.yeshi.fanli.entity.order.CommonOrder; import com.yeshi.fanli.entity.order.HongBaoOrder; -import com.yeshi.fanli.entity.order.HongBaoV2SettleTemp; -import com.yeshi.fanli.entity.order.InviteOrderSubsidy; -import com.yeshi.fanli.entity.taobao.TaoBaoOrder; import com.yeshi.fanli.entity.taobao.TaoBaoWeiQuanOrder; -import com.yeshi.fanli.exception.money.InviteOrderSubsidyDebtException; +import com.yeshi.fanli.exception.ParamsException; import com.yeshi.fanli.exception.money.OrderMoneySettleException; +import com.yeshi.fanli.exception.money.TeamEincomeRecordException; +import com.yeshi.fanli.exception.money.TeamRewardDebtException; import com.yeshi.fanli.exception.money.UserMoneyDetailException; import com.yeshi.fanli.exception.order.TaoBaoWeiQuanException; import com.yeshi.fanli.log.LogHelper; import com.yeshi.fanli.service.inter.hongbao.AccountDetailsHongBaoMapService; import com.yeshi.fanli.service.inter.hongbao.HongBaoV2SettleTempService; -import com.yeshi.fanli.service.inter.money.InviteOrderSubsidyDebtService; +import com.yeshi.fanli.service.inter.money.TeamRewardDebtService; import com.yeshi.fanli.service.inter.money.UserMoneyService; import com.yeshi.fanli.service.inter.money.msg.UserMoneyMsgNotificationService; import com.yeshi.fanli.service.inter.money.tb.TaoBaoWeiQuanDrawBackService; import com.yeshi.fanli.service.inter.order.CommonOrderService; -import com.yeshi.fanli.service.inter.order.InviteOrderSubsidyService; import com.yeshi.fanli.service.inter.order.OrderMoneySettleService; -import com.yeshi.fanli.service.inter.order.tb.TaoBaoOrderService; import com.yeshi.fanli.service.inter.user.UserSystemCouponService; +import com.yeshi.fanli.service.manger.msg.RocketMQManager; +import com.yeshi.fanli.service.manger.order.TeamRewardManager; import com.yeshi.fanli.util.Constant; -import com.yeshi.fanli.util.StringUtil; -import com.yeshi.fanli.util.TimeUtil; -import com.yeshi.fanli.util.cmq.HongBaoRecieveCMQManager; +import org.yeshi.utils.TimeUtil; import com.yeshi.fanli.util.factory.UserMoneyDetailFactory; +import com.yeshi.fanli.util.mq.cmq.HongBaoRecieveCMQManager; import com.yeshi.fanli.util.rocketmq.MQMsgBodyFactory; import com.yeshi.fanli.util.rocketmq.MQTopicName; -import com.yeshi.fanli.util.taobao.TaoKeOrderApiUtil; @Service public class OrderMoneySettleServiceImpl implements OrderMoneySettleService { - @Resource - private HongBaoV2Mapper hongBaoV2Mapper; + @Resource + private HongBaoV2Mapper hongBaoV2Mapper; - @Resource - private HongBaoOrderMapper hongBaoOrderMapper; + @Resource + private HongBaoOrderMapper hongBaoOrderMapper; - @Resource - private UserInfoMapper userInfoMapper; + @Resource + private UserInfoMapper userInfoMapper; - @Resource - private TaoBaoWeiQuanOrderMapper taoBaoWeiQuanOrderMapper; + @Resource + private TaoBaoWeiQuanOrderMapper taoBaoWeiQuanOrderMapper; - @Resource - private UserMoneyService userMoneyService; + @Resource + private UserMoneyService userMoneyService; - @Resource - private UserMoneyMsgNotificationService userMoneyMsgNotificationService; + @Resource + private UserMoneyMsgNotificationService userMoneyMsgNotificationService; - @Resource - private TaoBaoWeiQuanDrawBackService taoBaoWeiQuanDrawBackService; + @Resource + private TaoBaoWeiQuanDrawBackService taoBaoWeiQuanDrawBackService; - @Resource - private AccountDetailsHongBaoMapService accountDetailsHongBaoMapService; + @Resource + private AccountDetailsHongBaoMapService accountDetailsHongBaoMapService; - @Resource - private CommonOrderService commonOrderService; + @Resource + private CommonOrderService commonOrderService; - @Resource - private UserSystemCouponService userSystemCouponService; + @Resource + private UserSystemCouponService userSystemCouponService; - @Resource - private TaoBaoOrderService taoBaoOrderService; + @Resource(name = "orderTransactionProducer") + private TransactionProducer orderTransactionProducer; - @Resource(name = "orderTransactionProducer") - private TransactionProducer orderTransactionProducer; + // 閭�璇疯ˉ璐� - // 閭�璇疯ˉ璐� + @Resource + private HongBaoV2SettleTempService hongBaoV2SettleTempService; - @Resource - private InviteOrderSubsidyService inviteOrderSubsidyService; + @Resource + private TeamRewardDebtService teamRewardDebtService; - @Resource - private InviteOrderSubsidyDebtService inviteOrderSubsidyDebtService; + @Resource + private TeamRewardManager teamRewardManager; - @Resource - private HongBaoV2SettleTempService hongBaoV2SettleTempService; + @Resource + private RocketMQManager rocketMQManager; - @Transactional(rollbackFor=Exception.class) - @Override - public void inviteSettleTB(Long uid) throws OrderMoneySettleException { - // 鏌ヨUID鐨勪竴浜岀骇閭�璇疯禋 - int sourceType = Constant.SOURCE_TYPE_TAOBAO; - List<Integer> types = new ArrayList<>(); - types.add(HongBaoV2.TYPE_YIJI); - types.add(HongBaoV2.TYPE_ERJI); - types.add(HongBaoV2.TYPE_SHARE_YIJI); - types.add(HongBaoV2.TYPE_SHARE_ERJI); - long count = hongBaoV2Mapper.countCanBalanceHongBaoByTypeAndUid(types, uid, sourceType); - List<HongBaoV2> hongBaoList = new ArrayList<>(); - // 1000鏉℃暟鎹负1椤� - int page = (int) (count % 1000 == 0 ? count / 1000 : count / 1000 + 1); - for (int i = 0; i < page; i++) { - List<HongBaoV2> tempHongBaoList = hongBaoV2Mapper.listCanBalanceHongBaoByTypeAndUid(types, uid, sourceType, - i * 1000, 1000); - if (tempHongBaoList != null && tempHongBaoList.size() > 0) - hongBaoList.addAll(tempHongBaoList); - } + // 涓嬬骇琚皝绂侊紝绾㈠寘澶辨晥 + private void invalidHongBaoForbidden(Long id) { + HongBaoV2 updateHongBaoV2 = new HongBaoV2(id); + updateHongBaoV2.setState(HongBaoV2.STATE_SHIXIAO); + updateHongBaoV2.setBeizhu("灏佺澶辨晥"); + updateHongBaoV2.setUpdateTime(new Date()); + hongBaoV2Mapper.updateByPrimaryKeySelective(updateHongBaoV2); + } - for (int i = 0; i < hongBaoList.size(); i++) { - HongBaoV2 item = hongBaoList.get(i); - if (item != null && item.getParent() != null) { - if (item != null) { - HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByHongBaoId(item.getParent().getId()); - if (hongBaoOrder != null && hongBaoOrder.getCommonOrder() != null) { - CommonOrder co = hongBaoOrder.getCommonOrder(); - // 涓婄骇鐢ㄦ埛涓嶆槸姝e父鐢ㄦ埛锛岃鍗曞潎涓嶈兘鍒拌处 - UserInfo userInfo = userInfoMapper.selectAvailableByPrimaryKey(co.getUserInfo().getId()); - if (userInfo == null || userInfo.getState() != UserInfo.STATE_NORMAL) { - hongBaoList.remove(i); - i--; - } - } - } - } - } + @Transactional(rollbackFor = Exception.class) + @Override + public void inviteSettleTB(Long uid, Date maxPregetTime) throws OrderMoneySettleException { + // 鏌ヨUID鐨勪竴浜岀骇閭�璇疯禋 + int sourceType = Constant.SOURCE_TYPE_TAOBAO; + inviteSettle(uid,sourceType,maxPregetTime); + } - // 閭�璇疯禋鍒拌处浜嬪姟娑堟伅 - OrderMoneyRecievedMQMsg mqMsg = new OrderMoneyRecievedMQMsg(OrderMoneyRecievedMQMsg.TYPE_INVITE, uid, - sourceType, null, null, new Date(), 0); + @Transactional(rollbackFor = Exception.class) + @Override + public void inviteSettleJD(Long uid, Date maxPregetTime) throws OrderMoneySettleException { + // 鏌ヨUID鐨勪竴浜岀骇閭�璇疯禋 + int sourceType = Constant.SOURCE_TYPE_JD; + inviteSettle(uid,sourceType,maxPregetTime); + } - Message msg = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.orderFanLiActual, mqMsg); - String taskKey = getTaskKey(uid); - msg.setKey(taskKey); - // 娣诲姞浜嬪姟娑堟伅 - try { - orderTransactionProducer.send(msg, new LocalTransactionExecuter() { - @Override - public TransactionStatus execute(Message arg0, Object arg1) { - try { - fanliInviteTB(hongBaoList, uid, taskKey); - } catch (TaoBaoWeiQuanException e) { - return TransactionStatus.RollbackTransaction; - } - return TransactionStatus.CommitTransaction; - } - }, null); - } catch (Exception e) { - LogHelper.mqError(e.getMessage(), msg.getTopic(), msg.getTag(), new Gson().toJson(mqMsg)); - } - System.out.println(new Gson().toJson(mqMsg)); - } + @Transactional(rollbackFor = Exception.class) + @Override + public void inviteSettlePDD(Long uid, Date maxPregetTime) throws OrderMoneySettleException { + // 鏌ヨUID鐨勪竴浜岀骇閭�璇疯禋 + int sourceType = Constant.SOURCE_TYPE_PDD; + inviteSettle(uid,sourceType,maxPregetTime); + } - @Transactional(rollbackFor=Exception.class) - @Override - public void inviteSettleJD(Long uid) throws OrderMoneySettleException { - // 鏌ヨUID鐨勪竴浜岀骇閭�璇疯禋 - int sourceType = Constant.SOURCE_TYPE_JD; - List<Integer> types = new ArrayList<>(); - types.add(HongBaoV2.TYPE_YIJI); - types.add(HongBaoV2.TYPE_ERJI); - types.add(HongBaoV2.TYPE_SHARE_YIJI); - types.add(HongBaoV2.TYPE_SHARE_ERJI); - long count = hongBaoV2Mapper.countCanBalanceHongBaoByTypeAndUid(types, uid, sourceType); - List<HongBaoV2> hongBaoList = new ArrayList<>(); - // 1000鏉℃暟鎹负1椤� - int page = (int) (count % 1000 == 0 ? count / 1000 : count / 1000 + 1); - for (int i = 0; i < page; i++) { - List<HongBaoV2> tempHongBaoList = hongBaoV2Mapper.listCanBalanceHongBaoByTypeAndUid(types, uid, sourceType, - i * 1000, 1000); - if (tempHongBaoList != null && tempHongBaoList.size() > 0) - hongBaoList.addAll(tempHongBaoList); - } + @Transactional(rollbackFor = Exception.class) + @Override + public void inviteSettleVipShop(Long uid, Date maxPregetTime) throws OrderMoneySettleException { + // 鏌ヨUID鐨勪竴浜岀骇閭�璇疯禋 + int sourceType = Constant.SOURCE_TYPE_VIP; + inviteSettle(uid,sourceType,maxPregetTime); + } - for (int i = 0; i < hongBaoList.size(); i++) { - HongBaoV2 item = hongBaoList.get(i); - if (item != null && item.getParent() != null) { - if (item != null) { - HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByHongBaoId(item.getParent().getId()); - if (hongBaoOrder != null && hongBaoOrder.getCommonOrder() != null) { - CommonOrder co = hongBaoOrder.getCommonOrder(); - // 涓婄骇鐢ㄦ埛涓嶆槸姝e父鐢ㄦ埛锛岃鍗曞潎涓嶈兘鍒拌处 - UserInfo userInfo = userInfoMapper.selectAvailableByPrimaryKey(co.getUserInfo().getId()); - if (userInfo == null || userInfo.getState() != UserInfo.STATE_NORMAL) { - hongBaoList.remove(i); - i--; - } - } - } - } - } - // 閭�璇疯禋鍒拌处浜嬪姟娑堟伅 - OrderMoneyRecievedMQMsg mqMsg = new OrderMoneyRecievedMQMsg(OrderMoneyRecievedMQMsg.TYPE_INVITE, uid, - sourceType, null, null, new Date(), 0); - Message msg = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.orderFanLiActual, mqMsg); - String taskKey = getTaskKey(uid); - msg.setKey(taskKey); - // 娣诲姞浜嬪姟娑堟伅 - try { - orderTransactionProducer.send(msg, new LocalTransactionExecuter() { - @Override - public TransactionStatus execute(Message arg0, Object arg1) { - fanliInviteOther(hongBaoList, uid, sourceType, taskKey); - return TransactionStatus.CommitTransaction; - } - }, null); - } catch (Exception e) { - LogHelper.mqError(e.getMessage(), msg.getTopic(), msg.getTag(), new Gson().toJson(mqMsg)); - } - } + @Transactional(rollbackFor = Exception.class) + @Override + public void inviteSettleSuning(Long uid, Date maxPregetTime) throws OrderMoneySettleException { + // 鏌ヨUID鐨勪竴浜岀骇閭�璇疯禋 + int sourceType = Constant.SOURCE_TYPE_SUNING; + inviteSettle(uid,sourceType,maxPregetTime); + } - @Transactional(rollbackFor=Exception.class) - @Override - public void inviteSettlePDD(Long uid) throws OrderMoneySettleException { - // 鏌ヨUID鐨勪竴浜岀骇閭�璇疯禋 - int sourceType = Constant.SOURCE_TYPE_PDD; - List<Integer> types = new ArrayList<>(); - types.add(HongBaoV2.TYPE_YIJI); - types.add(HongBaoV2.TYPE_ERJI); - types.add(HongBaoV2.TYPE_SHARE_YIJI); - types.add(HongBaoV2.TYPE_SHARE_ERJI); - long count = hongBaoV2Mapper.countCanBalanceHongBaoByTypeAndUid(types, uid, sourceType); - List<HongBaoV2> hongBaoList = new ArrayList<>(); - // 1000鏉℃暟鎹负1椤� - int page = (int) (count % 1000 == 0 ? count / 1000 : count / 1000 + 1); - for (int i = 0; i < page; i++) { - List<HongBaoV2> tempHongBaoList = hongBaoV2Mapper.listCanBalanceHongBaoByTypeAndUid(types, uid, sourceType, - i * 1000, 1000); - if (tempHongBaoList != null && tempHongBaoList.size() > 0) - hongBaoList.addAll(tempHongBaoList); - } - for (int i = 0; i < hongBaoList.size(); i++) { - HongBaoV2 item = hongBaoList.get(i); - if (item != null && item.getParent() != null) { - if (item != null) { - HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByHongBaoId(item.getParent().getId()); - if (hongBaoOrder != null && hongBaoOrder.getCommonOrder() != null) { - CommonOrder co = hongBaoOrder.getCommonOrder(); - // 涓婄骇鐢ㄦ埛涓嶆槸姝e父鐢ㄦ埛锛岃鍗曞潎涓嶈兘鍒拌处 - UserInfo userInfo = userInfoMapper.selectAvailableByPrimaryKey(co.getUserInfo().getId()); - if (userInfo == null || userInfo.getState() != UserInfo.STATE_NORMAL) { - hongBaoList.remove(i); - i--; - } - } - } - } - } + private void inviteSettle(Long uid, int sourceType, Date maxPregetTime) throws OrderMoneySettleException { + // 鏌ヨUID鐨勪竴浜岀骇閭�璇疯禋 + List<Integer> types = new ArrayList<>(); + types.add(HongBaoV2.TYPE_YIJI); + types.add(HongBaoV2.TYPE_ERJI); + types.add(HongBaoV2.TYPE_SHARE_YIJI); + types.add(HongBaoV2.TYPE_SHARE_ERJI); + long count = hongBaoV2Mapper.countCanBalanceHongBaoByTypeAndUid(types, uid, sourceType, maxPregetTime); + List<HongBaoV2> hongBaoList = new ArrayList<>(); + // 1000鏉℃暟鎹负1椤� + int page = (int) (count % 1000 == 0 ? count / 1000 : count / 1000 + 1); + for (int i = 0; i < page; i++) { + List<HongBaoV2> tempHongBaoList = hongBaoV2Mapper.listCanBalanceHongBaoByTypeAndUid(types, uid, sourceType, + maxPregetTime, i * 1000, 1000); + if (tempHongBaoList != null && tempHongBaoList.size() > 0) + hongBaoList.addAll(tempHongBaoList); + } - // 閭�璇疯禋鍒拌处浜嬪姟娑堟伅 - OrderMoneyRecievedMQMsg mqMsg = new OrderMoneyRecievedMQMsg(OrderMoneyRecievedMQMsg.TYPE_INVITE, uid, - sourceType, null, null, new Date(), 0); - Message msg = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.orderFanLiActual, mqMsg); - String taskKey = getTaskKey(uid); - msg.setKey(taskKey); - // 娣诲姞浜嬪姟娑堟伅 - try { - orderTransactionProducer.send(msg, new LocalTransactionExecuter() { - @Override - public TransactionStatus execute(Message arg0, Object arg1) { - fanliInviteOther(hongBaoList, uid, sourceType, taskKey); - return TransactionStatus.CommitTransaction; - } - }, null); - } catch (Exception e) { - LogHelper.mqError(e.getMessage(), msg.getTopic(), msg.getTag(), new Gson().toJson(mqMsg)); - } - } + for (int i = 0; i < hongBaoList.size(); i++) { + HongBaoV2 item = hongBaoList.get(i); + if (item != null && item.getParent() != null) { + if (item != null) { + HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByHongBaoId(item.getParent().getId()); + if (hongBaoOrder != null && hongBaoOrder.getCommonOrder() != null) { + CommonOrder co = hongBaoOrder.getCommonOrder(); + // 涓婄骇鐢ㄦ埛涓嶆槸姝e父鐢ㄦ埛锛岃鍗曞潎涓嶈兘鍒拌处 + UserInfo userInfo = userInfoMapper.selectAvailableByPrimaryKey(co.getUserInfo().getId()); + if (userInfo == null || userInfo.getState() != UserInfo.STATE_NORMAL) { + invalidHongBaoForbidden(item.getId()); + hongBaoList.remove(i); + i--; + } + } + } + } + } - @Transactional(rollbackFor=Exception.class) - @Override - public void shareSettleTB(Long uid) throws OrderMoneySettleException { - /** - * 澶勭悊鍒嗕韩璧� - */ - int sourceType = Constant.SOURCE_TYPE_TAOBAO; - List<Integer> types = new ArrayList<>(); + if (hongBaoList.size() == 0) + return; - // 鏌ヨUID鐨勫垎浜禋璁㈠崟 - types.add(HongBaoV2.TYPE_SHARE_GOODS); - long count = hongBaoV2Mapper.countCanBalanceHongBaoByTypeAndUid(types, uid, sourceType); - List<HongBaoV2> hongBaoList = new ArrayList<>(); - // 1000鏉℃暟鎹负1椤� - int page = (int) (count % 1000 == 0 ? count / 1000 : count / 1000 + 1); - for (int i = 0; i < page; i++) { - List<HongBaoV2> tempHongBaoList = hongBaoV2Mapper.listCanBalanceHongBaoByTypeAndUid(types, uid, sourceType, - i * 1000, 1000); - if (tempHongBaoList != null && tempHongBaoList.size() > 0) - hongBaoList.addAll(tempHongBaoList); - } + // 閭�璇疯禋鍒拌处浜嬪姟娑堟伅 + String taskKey = getTaskKey(uid); + TeamRewardPreRecieveMQMsg mqMsg = new TeamRewardPreRecieveMQMsg(taskKey, uid, sourceType, maxPregetTime, + new Date()); + Message msg = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.teamRewardPreRecieved, mqMsg); + msg.setKey(taskKey); + // 娣诲姞浜嬪姟娑堟伅 + try { + orderTransactionProducer.send(msg, new LocalTransactionExecuter() { + @Override + public TransactionStatus execute(Message arg0, Object arg1) { + try { + fanliInvite(hongBaoList, uid, sourceType, taskKey, maxPregetTime); + } catch (Exception e) { + return TransactionStatus.RollbackTransaction; + } + return TransactionStatus.CommitTransaction; + } + }, null); + } catch (Exception e) { + LogHelper.mqError(e.getMessage(), msg.getTopic(), msg.getTag(), new Gson().toJson(mqMsg)); + } + } - OrderMoneyRecievedMQMsg mqMsg = new OrderMoneyRecievedMQMsg(OrderMoneyRecievedMQMsg.TYPE_SHARE, uid, sourceType, - null, null, new Date(), 0); - Message msg = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.orderFanLiActual, mqMsg); - String taskKey = getTaskKey(uid); - msg.setKey(taskKey); - // 娣诲姞浜嬪姟娑堟伅 - try { - orderTransactionProducer.send(msg, new LocalTransactionExecuter() { - @Override - public TransactionStatus execute(Message arg0, Object arg1) { - try { - fanLiShareTB(hongBaoList, uid, taskKey); - } catch (TaoBaoWeiQuanException e) { - return TransactionStatus.RollbackTransaction; - } - return TransactionStatus.CommitTransaction; - } - }, null); - } catch (Exception e) { - LogHelper.mqError(e.getMessage(), msg.getTopic(), msg.getTag(), new Gson().toJson(mqMsg)); - } - } + @Transactional(rollbackFor = Exception.class) + @Override + public void shareSettleTB(Long uid, Date maxPregetTime) throws OrderMoneySettleException { + /** + * 澶勭悊鍒嗕韩璧� + */ + int sourceType = Constant.SOURCE_TYPE_TAOBAO; + List<Integer> types = new ArrayList<>(); - @Transactional(rollbackFor=Exception.class) - @Override - public void shareSettleJD(Long uid) throws OrderMoneySettleException { - /** - * 澶勭悊鍒嗕韩璧� - */ - int sourceType = Constant.SOURCE_TYPE_JD; - List<Integer> types = new ArrayList<>(); + // 鏌ヨUID鐨勫垎浜禋璁㈠崟 + types.add(HongBaoV2.TYPE_SHARE_GOODS); + long count = hongBaoV2Mapper.countCanBalanceHongBaoByTypeAndUid(types, uid, sourceType, maxPregetTime); + List<HongBaoV2> hongBaoList = new ArrayList<>(); + // 1000鏉℃暟鎹负1椤� + int page = (int) (count % 1000 == 0 ? count / 1000 : count / 1000 + 1); + for (int i = 0; i < page; i++) { + List<HongBaoV2> tempHongBaoList = hongBaoV2Mapper.listCanBalanceHongBaoByTypeAndUid(types, uid, sourceType, + maxPregetTime, i * 1000, 1000); + if (tempHongBaoList != null && tempHongBaoList.size() > 0) + hongBaoList.addAll(tempHongBaoList); + } - // 鏌ヨUID鐨勫垎浜禋璁㈠崟 - types.add(HongBaoV2.TYPE_SHARE_GOODS); - long count = hongBaoV2Mapper.countCanBalanceHongBaoByTypeAndUid(types, uid, sourceType); - List<HongBaoV2> hongBaoList = new ArrayList<>(); - // 1000鏉℃暟鎹负1椤� - int page = (int) (count % 1000 == 0 ? count / 1000 : count / 1000 + 1); - for (int i = 0; i < page; i++) { - List<HongBaoV2> tempHongBaoList = hongBaoV2Mapper.listCanBalanceHongBaoByTypeAndUid(types, uid, sourceType, - i * 1000, 1000); - if (tempHongBaoList != null && tempHongBaoList.size() > 0) - hongBaoList.addAll(tempHongBaoList); - } + OrderMoneyRecievedMQMsg mqMsg = new OrderMoneyRecievedMQMsg(OrderMoneyRecievedMQMsg.TYPE_SHARE, uid, sourceType, + null, null, new Date(), 0); + Message msg = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.orderFanLiActual, mqMsg); + String taskKey = getTaskKey(uid); + msg.setKey(taskKey); - OrderMoneyRecievedMQMsg mqMsg = new OrderMoneyRecievedMQMsg(OrderMoneyRecievedMQMsg.TYPE_SHARE, uid, sourceType, - null, null, new Date(), 0); - Message msg = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.orderFanLiActual, mqMsg); - String taskKey = getTaskKey(uid); - msg.setKey(taskKey); - // 娣诲姞浜嬪姟娑堟伅 - try { - orderTransactionProducer.send(msg, new LocalTransactionExecuter() { - @Override - public TransactionStatus execute(Message arg0, Object arg1) { - fanLiShareOther(hongBaoList, uid, sourceType, taskKey); - return TransactionStatus.CommitTransaction; - } - }, null); - } catch (Exception e) { - LogHelper.mqError(e.getMessage(), msg.getTopic(), msg.getTag(), new Gson().toJson(mqMsg)); - } - } + // 娣诲姞浜嬪姟娑堟伅 + try { + orderTransactionProducer.send(msg, new LocalTransactionExecuter() { + @Override + public TransactionStatus execute(Message arg0, Object arg1) { + try { + fanLiShareTB(hongBaoList, uid, taskKey); + } catch (TaoBaoWeiQuanException e) { + return TransactionStatus.RollbackTransaction; + } + return TransactionStatus.CommitTransaction; + } + }, null); + } catch (Exception e) { + LogHelper.mqError(e.getMessage(), msg.getTopic(), msg.getTag(), new Gson().toJson(mqMsg)); + } + } - @Transactional(rollbackFor=Exception.class) - @Override - public void shareSettlePDD(Long uid) throws OrderMoneySettleException { - /** - * 澶勭悊鍒嗕韩璧� - */ - int sourceType = Constant.SOURCE_TYPE_PDD; - List<Integer> types = new ArrayList<>(); + @Transactional(rollbackFor = Exception.class) + @Override + public void shareSettleJD(Long uid, Date maxPregetTime) throws OrderMoneySettleException { + /** + * 澶勭悊鍒嗕韩璧� + */ + int sourceType = Constant.SOURCE_TYPE_JD; + List<Integer> types = new ArrayList<>(); - // 鏌ヨUID鐨勫垎浜禋璁㈠崟 - types.add(HongBaoV2.TYPE_SHARE_GOODS); - long count = hongBaoV2Mapper.countCanBalanceHongBaoByTypeAndUid(types, uid, sourceType); - List<HongBaoV2> hongBaoList = new ArrayList<>(); - // 1000鏉℃暟鎹负1椤� - int page = (int) (count % 1000 == 0 ? count / 1000 : count / 1000 + 1); - for (int i = 0; i < page; i++) { - List<HongBaoV2> tempHongBaoList = hongBaoV2Mapper.listCanBalanceHongBaoByTypeAndUid(types, uid, sourceType, - i * 1000, 1000); - if (tempHongBaoList != null && tempHongBaoList.size() > 0) - hongBaoList.addAll(tempHongBaoList); - } + // 鏌ヨUID鐨勫垎浜禋璁㈠崟 + types.add(HongBaoV2.TYPE_SHARE_GOODS); + long count = hongBaoV2Mapper.countCanBalanceHongBaoByTypeAndUid(types, uid, sourceType, maxPregetTime); + List<HongBaoV2> hongBaoList = new ArrayList<>(); + // 1000鏉℃暟鎹负1椤� + int page = (int) (count % 1000 == 0 ? count / 1000 : count / 1000 + 1); + for (int i = 0; i < page; i++) { + List<HongBaoV2> tempHongBaoList = hongBaoV2Mapper.listCanBalanceHongBaoByTypeAndUid(types, uid, sourceType, + maxPregetTime, i * 1000, 1000); + if (tempHongBaoList != null && tempHongBaoList.size() > 0) + hongBaoList.addAll(tempHongBaoList); + } - OrderMoneyRecievedMQMsg mqMsg = new OrderMoneyRecievedMQMsg(OrderMoneyRecievedMQMsg.TYPE_SHARE, uid, sourceType, - null, null, new Date(), 0); - Message msg = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.orderFanLiActual, mqMsg); - String taskKey = getTaskKey(uid); - msg.setKey(taskKey); - // 娣诲姞浜嬪姟娑堟伅 - try { - orderTransactionProducer.send(msg, new LocalTransactionExecuter() { - @Override - public TransactionStatus execute(Message arg0, Object arg1) { - fanLiShareOther(hongBaoList, uid, sourceType, taskKey); - return TransactionStatus.CommitTransaction; - } - }, null); - } catch (Exception e) { - LogHelper.mqError(e.getMessage(), msg.getTopic(), msg.getTag(), new Gson().toJson(mqMsg)); - } - } + OrderMoneyRecievedMQMsg mqMsg = new OrderMoneyRecievedMQMsg(OrderMoneyRecievedMQMsg.TYPE_SHARE, uid, sourceType, + null, null, new Date(), 0); + Message msg = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.orderFanLiActual, mqMsg); + String taskKey = getTaskKey(uid); + msg.setKey(taskKey); + // 娣诲姞浜嬪姟娑堟伅 + try { + orderTransactionProducer.send(msg, new LocalTransactionExecuter() { + @Override + public TransactionStatus execute(Message arg0, Object arg1) { + fanLiShareOther(hongBaoList, uid, sourceType, taskKey); + return TransactionStatus.CommitTransaction; + } + }, null); + } catch (Exception e) { + LogHelper.mqError(e.getMessage(), msg.getTopic(), msg.getTag(), new Gson().toJson(mqMsg)); + } + } - @Transactional(rollbackFor=Exception.class) - @Override - public void ziGouSettle(String orderId, int sourceType, String taskKey) throws OrderMoneySettleException { - // 鏌ヨ鍚屼竴璁㈠崟鍙风殑绾㈠寘 - List<CommonOrder> orderList = commonOrderService.listBySourceTypeAndOrderId(sourceType, orderId); - List<HongBaoV2> hongBaoList = new ArrayList<>(); - Map<Long, BigDecimal> userMoney = new HashMap<>(); - Map<Long, List<HongBaoV2>> userHongBao = new HashMap<>(); - Map<Long, Integer> userGoodsCount = new HashMap<>(); - for (CommonOrder co : orderList) { - HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(co.getId()); - hongBaoList.add(hongBaoOrder.getHongBaoV2()); - Long uid = co.getUserInfo().getId(); - if (userGoodsCount.get(uid) == null) - userGoodsCount.put(uid, co.getCount()); - else - userGoodsCount.put(uid, Integer.valueOf(co.getCount() + userGoodsCount.get(uid))); - } + @Transactional(rollbackFor = Exception.class) + @Override + public void shareSettlePDD(Long uid, Date maxPregetTime) throws OrderMoneySettleException { + shareSettle(Constant.SOURCE_TYPE_PDD, uid, maxPregetTime); + } - for (HongBaoV2 hb : hongBaoList) { - long currentTimeMillis = System.currentTimeMillis(); - if (hb.getState() != HongBao.STATE_SHIXIAO) { - // 璁剧疆涓哄凡杩斿埄 - // 澶勭悊涓荤孩鍖� - if (hb.getState() != HongBaoV2.STATE_SHIXIAO && hb.getState() != HongBaoV2.STATE_YILINGQU - && hb.getType() == HongBaoV2.TYPE_ZIGOU) { - // 濡傛灉绾㈠寘娌¤棰嗗彇 - Long uid = hb.getUserInfo().getId(); - HongBaoV2 updateHongBao = new HongBaoV2(); - updateHongBao.setId(hb.getId()); - updateHongBao.setState(HongBaoV2.STATE_YILINGQU); - updateHongBao.setGetTime(new Date(currentTimeMillis)); - hongBaoV2Mapper.updateByPrimaryKeySelective(updateHongBao); - if (userMoney.get(uid) == null) - userMoney.put(uid, hb.getMoney()); - else - userMoney.put(uid, userMoney.get(uid).add(hb.getMoney())); + @Transactional(rollbackFor = Exception.class) + @Override + public void shareSettleVipShop(Long uid, Date maxPregetTime) throws OrderMoneySettleException { + shareSettle(Constant.SOURCE_TYPE_VIP, uid, maxPregetTime); + } - // 娣诲姞绾㈠寘鍒扮敤鎴� - if (userHongBao.get(uid) == null) - userHongBao.put(uid, new ArrayList<>()); - userHongBao.get(uid).add(hb); - } - } - } + @Transactional(rollbackFor = Exception.class) + @Override + public void shareSettleSuning(Long uid, Date maxPregetTime) throws OrderMoneySettleException { + shareSettle(Constant.SOURCE_TYPE_SUNING, uid, maxPregetTime); + } - // if (userMoney.isEmpty()) { - // throw new OrderMoneySettleException(1, "娌℃湁闇�瑕佺粨绠楃殑璁㈠崟"); - // } + private void shareSettle(int sourceType, Long uid, Date maxPregetTime) throws OrderMoneySettleException { + /** + * 澶勭悊鍒嗕韩璧� + */ + List<Integer> types = new ArrayList<>(); - Iterator<Long> its = userMoney.keySet().iterator(); + // 鏌ヨUID鐨勫垎浜禋璁㈠崟 + types.add(HongBaoV2.TYPE_SHARE_GOODS); + long count = hongBaoV2Mapper.countCanBalanceHongBaoByTypeAndUid(types, uid, sourceType, maxPregetTime); + List<HongBaoV2> hongBaoList = new ArrayList<>(); + // 1000鏉℃暟鎹负1椤� + int page = (int) (count % 1000 == 0 ? count / 1000 : count / 1000 + 1); + for (int i = 0; i < page; i++) { + List<HongBaoV2> tempHongBaoList = hongBaoV2Mapper.listCanBalanceHongBaoByTypeAndUid(types, uid, sourceType, + maxPregetTime, i * 1000, 1000); + if (tempHongBaoList != null && tempHongBaoList.size() > 0) + hongBaoList.addAll(tempHongBaoList); + } - while (its.hasNext()) { - // 灏嗚嚜宸辨坊鍔犲埌鐢ㄦ埛 - Long uid = its.next(); - BigDecimal money = userMoney.get(uid); - UserMoneyDetail userMoneyDetail = null; - // 鎻掑叆鏂扮増璧勯噾鏄庣粏 - try { - // 姝ゅ涓嶇敤璁㈠崟鍙峰仛鍞竴鎬х储寮� - userMoneyDetail = UserMoneyDetailFactory.createFanLi(uid, orderId, sourceType, null, money); - } catch (UserMoneyDetailException e1) { - try { - LogHelper.errorDetailInfo(e1, "璁㈠崟鍙�:" + orderId, ""); - } catch (Exception e2) { - e2.printStackTrace(); - } - } + OrderMoneyRecievedMQMsg mqMsg = new OrderMoneyRecievedMQMsg(OrderMoneyRecievedMQMsg.TYPE_SHARE, uid, sourceType, + null, null, new Date(), 0); + Message msg = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.orderFanLiActual, mqMsg); + String taskKey = getTaskKey(uid); + msg.setKey(taskKey); + // 娣诲姞浜嬪姟娑堟伅 + try { + orderTransactionProducer.send(msg, new LocalTransactionExecuter() { + @Override + public TransactionStatus execute(Message arg0, Object arg1) { + fanLiShareOther(hongBaoList, uid, sourceType, taskKey); + return TransactionStatus.CommitTransaction; + } + }, null); + } catch (Exception e) { + LogHelper.mqError(e.getMessage(), msg.getTopic(), msg.getTag(), new Gson().toJson(mqMsg)); + } + } - userMoneyService.addUserMoney(uid, money, userMoneyDetail); + @Transactional(rollbackFor = Exception.class) + @Override + public void ziGouSettle(String orderId, int sourceType, String taskKey) throws OrderMoneySettleException { + // 鏌ヨ鍚屼竴璁㈠崟鍙风殑绾㈠寘 + List<CommonOrder> orderList = commonOrderService.listBySourceTypeAndOrderId(sourceType, orderId); + List<HongBaoV2> hongBaoList = new ArrayList<>(); + Map<Long, BigDecimal> userMoney = new HashMap<>(); + Map<Long, List<HongBaoV2>> userHongBao = new HashMap<>(); + Map<Long, Integer> userGoodsCount = new HashMap<>(); + for (CommonOrder co : orderList) { + HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(co.getId()); + hongBaoList.add(hongBaoOrder.getHongBaoV2()); + Long uid = co.getUserInfo().getId(); + if (userGoodsCount.get(uid) == null) + userGoodsCount.put(uid, co.getCount()); + else + userGoodsCount.put(uid, Integer.valueOf(co.getCount() + userGoodsCount.get(uid))); + } - List<Long> hbIdList = new ArrayList<>(); - // 娣诲姞璧勯噾鏄庣粏涓庣孩鍖呯殑鏄犲皠鍏崇郴 - for (HongBaoV2 v2 : userHongBao.get(uid)) { - hbIdList.add(v2.getId()); - accountDetailsHongBaoMapService.saveAccountDetailsHongBaoMap(v2.getId(), userMoneyDetail.getId()); - try { - HongBaoRecieveCMQManager.getInstance().addHongBaoRecieveMsg(v2.getId()); - } catch (Exception e) { - LogHelper.errorDetailInfo(e); - } - } - // 娣诲姞缁撶畻鏄犲皠 - hongBaoV2SettleTempService.addTemp(hbIdList, taskKey); + for (HongBaoV2 hb : hongBaoList) { + long currentTimeMillis = System.currentTimeMillis(); + if (hb.getState() != HongBao.STATE_SHIXIAO) { + // 璁剧疆涓哄凡杩斿埄 + // 澶勭悊涓荤孩鍖� + if (hb.getState() != HongBaoV2.STATE_SHIXIAO && hb.getState() != HongBaoV2.STATE_YILINGQU + && hb.getType() == HongBaoV2.TYPE_ZIGOU) { + // 濡傛灉绾㈠寘娌¤棰嗗彇 + Long uid = hb.getUserInfo().getId(); + HongBaoV2 updateHongBao = new HongBaoV2(); + updateHongBao.setId(hb.getId()); + updateHongBao.setState(HongBaoV2.STATE_YILINGQU); + updateHongBao.setGetTime(new Date(currentTimeMillis)); + hongBaoV2Mapper.updateByPrimaryKeySelective(updateHongBao); + if (userMoney.get(uid) == null) + userMoney.put(uid, hb.getMoney()); + else + userMoney.put(uid, userMoney.get(uid).add(hb.getMoney())); - // 绔欏唴淇�+鎺ㄩ�� - UserInfo user = userInfoMapper.selectByPrimaryKey(uid); + // 娣诲姞绾㈠寘鍒扮敤鎴� + if (userHongBao.get(uid) == null) + userHongBao.put(uid, new ArrayList<>()); + userHongBao.get(uid).add(hb); + } + } + } - // 鏂扮増閫氱煡 - userMoneyMsgNotificationService.fanliOrderReceived(uid, orderId, sourceType, userGoodsCount.get(uid), money, - user.getMyHongBao()); - } - // 閫氱煡鍏嶅崟鍒拌处 - try { - userSystemCouponService.updateStateByArrivalAccount(orderId); - } catch (Exception e) { - e.printStackTrace(); - } + // 鑾峰彇缁存潈璧勯噾 + if (sourceType == Constant.SOURCE_TYPE_TAOBAO) { + try { + Map<Long, BigDecimal> userSubMoneyMap = taoBaoWeiQuanDrawBackService.doWeiQuanFanli(orderId, false); + if (userSubMoneyMap != null) { + for (Iterator<Long> its = userMoney.keySet().iterator(); its.hasNext(); ) { + Long uid = its.next(); + if (userSubMoneyMap.get(uid) != null) { + // 鍑忓幓缁存潈璧勯噾 + userMoney.put(uid, userMoney.get(uid).subtract(userSubMoneyMap.get(uid))); + } + } + } + } catch (Exception e) { + LogHelper.errorDetailInfo(e); + throw new OrderMoneySettleException(2, "缁存潈璁㈠崟鍑洪敊"); + } + } - // 缁存潈鎵f锛�2018-08-05鍚庡紑濮嬪疄琛岋級 + // if (userMoney.isEmpty()) { + // throw new OrderMoneySettleException(1, "娌℃湁闇�瑕佺粨绠楃殑璁㈠崟"); + // } - try { - if (sourceType == Constant.SOURCE_TYPE_TAOBAO) - taoBaoWeiQuanDrawBackService.doWeiQuanFanli(orderId); - } catch (TaoBaoWeiQuanException e) { - if (e.getCode() == TaoBaoWeiQuanException.CODE_NEDD_UPDATE_ORDER) { - List<TaoBaoOrder> olist = taoBaoOrderService.getTaoBaoOrderByOrderId(orderId); - if (olist != null && olist.size() > 0) { - long time = TimeUtil.convertToTimeTemp(olist.get(0).getCreateTime(), "yyyy-MM-dd HH:mm:ss"); - olist = TaoKeOrderApiUtil.getTaoBaoCommonOrderList(time, time + 1000 * 2L, 1); - if (olist != null && olist.size() > 0) { - for (int i = 0; i < olist.size(); i++) { - if (!olist.get(i).getOrderId().equalsIgnoreCase(orderId)) {// 鍒犻櫎鏃犲叧璁㈠崟 - olist.remove(i); - i--; - } - } - taoBaoOrderService.addTaoBaoOrderList(olist); - } - } - } else - throw new OrderMoneySettleException(e.getCode(), "缁存潈澶勭悊鍑洪敊:" + e.getMsg()); - } - - // 娣诲姞缁撶畻鏄犲皠 - - } - - private HongBaoV2 filterWeiQuanINGHongBao(HongBaoV2 hongBao) { - - long hbId = hongBao.getId(); - if (hongBao.getParent() != null) { - hbId = hongBao.getParent().getId(); - } - - HongBaoOrder hongbaoOrder = hongBaoOrderMapper.selectByHongBaoId(hbId); - - if (hongbaoOrder == null) - return null; - - List<TaoBaoWeiQuanOrder> taoBaoWeiQuanList = taoBaoWeiQuanOrderMapper - .selectListByOrderIdAndState(hongbaoOrder.getCommonOrder().getOrderNo(), "缁存潈鍒涘缓"); - - List<TaoBaoWeiQuanOrder> taoBaoWeiQuanList1 = taoBaoWeiQuanOrderMapper - .selectListByOrderIdAndState(hongbaoOrder.getCommonOrder().getOrderNo(), "绛夊緟澶勭悊"); - if ((taoBaoWeiQuanList != null && taoBaoWeiQuanList.size() > 0) - || (taoBaoWeiQuanList1 != null && taoBaoWeiQuanList1.size() > 0)) { - return null; - } - return hongBao; - } - - @Transactional(rollbackFor = Exception.class) - private void fanliInviteTB(List<HongBaoV2> hongBaoList, Long uid, String key) throws TaoBaoWeiQuanException { - List<Long> hbIdList = new ArrayList<>(); - BigDecimal invitemoney = new BigDecimal(0); - Set<String> inviteOrders = new HashSet<>(); - int inviteGoodsCount = 0; - - // 闇�瑕佸垽鏂��娆剧殑璁㈠崟鍙� - Set<String> drawBackOrders = new HashSet<String>(); - for (HongBaoV2 hongBao : hongBaoList) { - hongBao = filterWeiQuanINGHongBao(hongBao); - if (hongBao == null) - continue; - - hongBao = hongBaoV2Mapper.selectByPrimaryKeyForUpdate(hongBao.getId()); - if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU || hongBao.getState() == HongBaoV2.STATE_KELINGQU) { - invitemoney = invitemoney.add(hongBao.getMoney()); - HongBaoV2 updateHongBao = new HongBaoV2(); - updateHongBao.setId(hongBao.getId()); - updateHongBao.setGetTime(new Date()); - updateHongBao.setState(HongBaoV2.STATE_YILINGQU); - updateHongBao.setUpdateTime(new Date()); - hongBaoV2Mapper.updateByPrimaryKeySelective(updateHongBao); - // 娣诲姞鍒扮孩鍖呰繑鍒╄褰曢泦鍚� - hbIdList.add(hongBao.getId()); - - HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByHongBaoId(hongBao.getParent().getId()); - inviteGoodsCount += hongBaoOrder.getCommonOrder().getCount(); - inviteOrders.add(hongBaoOrder.getCommonOrder().getOrderNo()); - - Date balanceTime = hongBaoOrder.getCommonOrder().getSettleTime(); - if (balanceTime != null - && balanceTime.getTime() >= TimeUtil.convertToTimeTemp("2018-08-05", "yyyy-MM-dd")) { - if (!StringUtil.isNullOrEmpty(hongBaoOrder.getCommonOrder().getOrderNo())) - drawBackOrders.add(hongBaoOrder.getCommonOrder().getOrderNo()); - } - } - } - - // 閭�璇疯禋鍒拌处 - if (invitemoney.compareTo(new BigDecimal(0)) > 0) { - - // 娣诲姞鏂扮増璇︽儏璁板綍 - try { - // 鏌ヨ閭�璇疯禋鐨勬湁鏁堣鍗曪紝澶辨晥璁㈠崟锛岀淮鏉冭鍗� - Calendar ca = Calendar.getInstance(); - Date maxDate = new Date(TimeUtil - .convertToTimeTemp(ca.get(Calendar.YEAR) + "-" + (ca.get(Calendar.MONTH) + 1), "yyyy-M")); - ca.add(Calendar.MONTH, -1); - - Date minDate = new Date(TimeUtil - .convertToTimeTemp(ca.get(Calendar.YEAR) + "-" + (ca.get(Calendar.MONTH) + 1), "yyyy-M")); - long validCount = hongBaoV2Mapper.countInviteOrderCountByUidAndSettleTime(uid, CommonOrder.STATE_JS, - minDate, maxDate, Constant.SOURCE_TYPE_TAOBAO); - long weiQuanCount = hongBaoV2Mapper.countInviteOrderCountByUidAndSettleTime(uid, CommonOrder.STATE_WQ, - minDate, maxDate, Constant.SOURCE_TYPE_TAOBAO); - - UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createInvite(uid, Constant.SOURCE_TYPE_TAOBAO, - (int) validCount, (int) weiQuanCount, invitemoney, new Date()); - - // 澧炲姞璧勯噾 - userMoneyService.addUserMoney(uid, invitemoney, userMoneyDetail); - // 娣诲姞鍒扮孩鍖呰繑鍒╄褰曢泦鍚� - accountDetailsHongBaoMapService.saveAccountDetailsHongBaoMap(hbIdList, userMoneyDetail.getId()); - } catch (UserMoneyDetailException e) { - try { - LogHelper.errorDetailInfo(e); - } catch (Exception e1) { - e1.printStackTrace(); - } - } - // 鏂扮増閫氱煡 - userMoneyMsgNotificationService.inviteOrderReceived(uid, Constant.SOURCE_TYPE_TAOBAO, inviteOrders.size(), - inviteGoodsCount, invitemoney, userInfoMapper.selectByPrimaryKey(uid).getMyHongBao()); - for (String orderId : drawBackOrders) - taoBaoWeiQuanDrawBackService.doWeiQuanInvite(orderId); - } - - try { - hongBaoV2SettleTempService.addTemp(hbIdList, key); - } catch (Exception e1) { - throw new TaoBaoWeiQuanException(200, "鎻掑叆杩斿埄涓存椂琛ㄥ嚭閿�"); - } - - // 鎵�鏈夌殑杩斿埄鍒拌处绾㈠寘ID - for (Long hongBaoId : hbIdList) { - try { - HongBaoRecieveCMQManager.getInstance().addHongBaoRecieveMsg(hongBaoId); - } catch (Exception e) { - LogHelper.errorDetailInfo(e); - } - } - } - - @Transactional - private void fanliInviteOther(List<HongBaoV2> hongBaoList, Long uid, int sourceType, String key) { - List<Long> hbIdList = new ArrayList<>(); - BigDecimal invitemoney = new BigDecimal(0); - Set<String> inviteOrders = new HashSet<>(); - int inviteGoodsCount = 0; - - // 闇�瑕佸垽鏂��娆剧殑璁㈠崟鍙� - for (HongBaoV2 hongBao : hongBaoList) { - hongBao = filterWeiQuanINGHongBao(hongBao); - if (hongBao == null) - continue; - hongBao = hongBaoV2Mapper.selectByPrimaryKeyForUpdate(hongBao.getId()); - if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU || hongBao.getState() == HongBaoV2.STATE_KELINGQU) { - invitemoney = invitemoney.add(hongBao.getMoney()); - HongBaoV2 updateHongBao = new HongBaoV2(); - updateHongBao.setId(hongBao.getId()); - updateHongBao.setGetTime(new Date()); - updateHongBao.setState(HongBaoV2.STATE_YILINGQU); - updateHongBao.setUpdateTime(new Date()); - hongBaoV2Mapper.updateByPrimaryKeySelective(updateHongBao); - // 娣诲姞鍒扮孩鍖呰繑鍒╄褰曢泦鍚� - hbIdList.add(hongBao.getId()); - - // 2018-08-05 杩囧悗鐨勮鍗曟墠澶勭悊缁存潈 - - HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByHongBaoId(hongBao.getParent().getId()); - inviteGoodsCount += hongBaoOrder.getCommonOrder().getCount(); - inviteOrders.add(hongBaoOrder.getCommonOrder().getOrderNo()); - } - } + Iterator<Long> its = userMoney.keySet().iterator(); - // 閭�璇疯禋鍒拌处 - if (invitemoney.compareTo(new BigDecimal(0)) > 0) { - // 娣诲姞鏂扮増璇︽儏璁板綍 - try { - // 鏌ヨ閭�璇疯禋鐨勬湁鏁堣鍗曪紝澶辨晥璁㈠崟锛岀淮鏉冭鍗� - Calendar ca = Calendar.getInstance(); - Date maxDate = new Date(TimeUtil - .convertToTimeTemp(ca.get(Calendar.YEAR) + "-" + (ca.get(Calendar.MONTH) + 1), "yyyy-M")); - ca.add(Calendar.MONTH, -1); + while (its.hasNext()) { + // 灏嗚嚜宸辨坊鍔犲埌鐢ㄦ埛 + Long uid = its.next(); + BigDecimal money = userMoney.get(uid); + UserMoneyDetail userMoneyDetail = null; + // 鎻掑叆鏂扮増璧勯噾鏄庣粏 + try { + // 姝ゅ涓嶇敤璁㈠崟鍙峰仛鍞竴鎬х储寮� + userMoneyDetail = UserMoneyDetailFactory.createFanLi(uid, orderId, sourceType, null, money); + } catch (UserMoneyDetailException e1) { + try { + LogHelper.errorDetailInfo(e1, "璁㈠崟鍙�:" + orderId, ""); + } catch (Exception e2) { + e2.printStackTrace(); + } + } - Date minDate = new Date(TimeUtil - .convertToTimeTemp(ca.get(Calendar.YEAR) + "-" + (ca.get(Calendar.MONTH) + 1), "yyyy-M")); - long validCount = hongBaoV2Mapper.countInviteOrderCountByUidAndSettleTime(uid, CommonOrder.STATE_JS, - minDate, maxDate, sourceType); - long weiQuanCount = hongBaoV2Mapper.countInviteOrderCountByUidAndSettleTime(uid, CommonOrder.STATE_WQ, - minDate, maxDate, sourceType); + userMoneyService.addUserMoney(uid, money, userMoneyDetail); - UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createInvite(uid, sourceType, (int) validCount, - (int) weiQuanCount, invitemoney, new Date()); + List<Long> hbIdList = new ArrayList<>(); + // 娣诲姞璧勯噾鏄庣粏涓庣孩鍖呯殑鏄犲皠鍏崇郴 + for (HongBaoV2 v2 : userHongBao.get(uid)) { + hbIdList.add(v2.getId()); + accountDetailsHongBaoMapService.saveAccountDetailsHongBaoMap(v2.getId(), userMoneyDetail.getId()); + try { + HongBaoRecieveCMQManager.getInstance().addHongBaoRecieveMsg(v2.getId()); + } catch (Exception e) { + LogHelper.errorDetailInfo(e); + } + } + // 娣诲姞缁撶畻鏄犲皠 + hongBaoV2SettleTempService.addTemp(hbIdList, taskKey); - // 澧炲姞璧勯噾 - userMoneyService.addUserMoney(uid, invitemoney, userMoneyDetail); - // 娣诲姞鍒扮孩鍖呰繑鍒╄褰曢泦鍚� - accountDetailsHongBaoMapService.saveAccountDetailsHongBaoMap(hbIdList, userMoneyDetail.getId()); - } catch (UserMoneyDetailException e) { - try { - LogHelper.errorDetailInfo(e); - } catch (Exception e1) { - e1.printStackTrace(); - } - } - // 鏂扮増閫氱煡 - userMoneyMsgNotificationService.inviteOrderReceived(uid, sourceType, inviteOrders.size(), inviteGoodsCount, - invitemoney, userInfoMapper.selectByPrimaryKey(uid).getMyHongBao()); - } + // 绔欏唴淇�+鎺ㄩ�� + UserInfo user = userInfoMapper.selectByPrimaryKey(uid); - hongBaoV2SettleTempService.addTemp(hbIdList, key); + // 鏂扮増閫氱煡 - // 鎵�鏈夌殑杩斿埄鍒拌处绾㈠寘ID - for (Long hongBaoId : hbIdList) { - try { - HongBaoRecieveCMQManager.getInstance().addHongBaoRecieveMsg(hongBaoId); - } catch (Exception e) { - LogHelper.errorDetailInfo(e); - } - } - } + userMoneyMsgNotificationService.fanliOrderReceived(uid, orderId, sourceType, userGoodsCount.get(uid), money, + user.getMyHongBao(), orderList.get(0).getThirdCreateTime()); + } + // 閫氱煡鍏嶅崟鍒拌处 + try { + userSystemCouponService.updateStateByArrivalAccount(orderId, sourceType); + } catch (Exception e) { + e.printStackTrace(); + } - /** - * 娣樺疂鍒嗕韩璧氳繑鍒� - * - * @param hongBaoList - * @param uid - * @throws TaoBaoWeiQuanException - */ - private void fanLiShareTB(List<HongBaoV2> hongBaoList, Long uid, String key) throws TaoBaoWeiQuanException { - BigDecimal sharemoney = new BigDecimal(0); - List<Long> hbIdList = new ArrayList<>(); - Set<String> drawBackOrders = new HashSet<String>(); - List<Long> recieveHongBaoIds = new ArrayList<>(); - int shareGoodsCount = 0; - Set<String> shareOrders = new HashSet<>(); + // 娣诲姞缁撶畻鏄犲皠 - for (HongBaoV2 hongBao : hongBaoList) { - hongBao = hongBaoV2Mapper.selectByPrimaryKeyForUpdate(hongBao.getId()); - hongBao = filterWeiQuanINGHongBao(hongBao); - if (hongBao == null) - continue; + } - if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU || hongBao.getState() == HongBaoV2.STATE_KELINGQU) { - sharemoney = sharemoney.add(hongBao.getMoney()); - HongBaoV2 updateHongBao = new HongBaoV2(); - updateHongBao.setId(hongBao.getId()); - updateHongBao.setGetTime(new Date()); - updateHongBao.setState(HongBao.STATE_YILINGQU); - updateHongBao.setUpdateTime(new Date()); - hongBaoV2Mapper.updateByPrimaryKeySelective(updateHongBao); - // 娣诲姞鍒扮孩鍖呰繑鍒╄褰曢泦鍚� - hbIdList.add(hongBao.getId()); - // 2018-08-05 杩囧悗鐨勮鍗曟墠澶勭悊缁存潈 - Date balanceTime = null; - HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByHongBaoId(hongBao.getId()); - if (hongBaoOrder != null) { - balanceTime = hongBaoOrder.getCommonOrder().getSettleTime(); - shareGoodsCount += hongBaoOrder.getCommonOrder().getCount(); - } - shareOrders.add(hongBaoOrder.getCommonOrder().getOrderNo()); - if (balanceTime != null - && balanceTime.getTime() >= TimeUtil.convertToTimeTemp("2018-08-05", "yyyy-MM-dd")) { - drawBackOrders.add(hongBaoOrder.getCommonOrder().getOrderNo()); - } - } - } + private HongBaoV2 filterWeiQuanINGHongBao(HongBaoV2 hongBao) { - /** - * 鍒嗕韩璧氬埌璐� - */ + long hbId = hongBao.getId(); + if (hongBao.getParent() != null) { + hbId = hongBao.getParent().getId(); + } - if (sharemoney.compareTo(new BigDecimal(0)) > 0) { - // 娣诲姞鏂扮増璇︽儏璁板綍 - try { - // 璁$畻鏈湀鐨勬湁鏁堣鍗曪紝澶辨晥璁㈠崟锛岀淮鏉冭鍗� - Calendar ca = Calendar.getInstance(); + HongBaoOrder hongbaoOrder = hongBaoOrderMapper.selectByHongBaoId(hbId); - Date maxDate = new Date(TimeUtil - .convertToTimeTemp(ca.get(Calendar.YEAR) + "-" + (ca.get(Calendar.MONTH) + 1), "yyyy-M")); - ca.add(Calendar.MONTH, -1); + if (hongbaoOrder == null) + return null; - Date minDate = new Date(TimeUtil - .convertToTimeTemp(ca.get(Calendar.YEAR) + "-" + (ca.get(Calendar.MONTH) + 1), "yyyy-M")); - long validCount = hongBaoV2Mapper.countShareOrderCountByUidAndSettleTime(uid, CommonOrder.STATE_JS, - minDate, maxDate, Constant.SOURCE_TYPE_TAOBAO); - long weiQuanCount = hongBaoV2Mapper.countShareOrderCountByUidAndSettleTime(uid, CommonOrder.STATE_WQ, - minDate, maxDate, Constant.SOURCE_TYPE_TAOBAO); + List<TaoBaoWeiQuanOrder> taoBaoWeiQuanList = taoBaoWeiQuanOrderMapper + .selectListByOrderIdAndState(hongbaoOrder.getCommonOrder().getOrderNo(), "缁存潈鍒涘缓"); - UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createShare(uid, Constant.SOURCE_TYPE_TAOBAO, - (int) validCount, (int) weiQuanCount, sharemoney, new Date()); - // 娣诲姞璧勯噾 - userMoneyService.addUserMoney(uid, sharemoney, userMoneyDetail); + List<TaoBaoWeiQuanOrder> taoBaoWeiQuanList1 = taoBaoWeiQuanOrderMapper + .selectListByOrderIdAndState(hongbaoOrder.getCommonOrder().getOrderNo(), "绛夊緟澶勭悊"); + if ((taoBaoWeiQuanList != null && taoBaoWeiQuanList.size() > 0) + || (taoBaoWeiQuanList1 != null && taoBaoWeiQuanList1.size() > 0)) { + return null; + } + return hongBao; + } - // 娣诲姞鍒扮孩鍖呰繑鍒╄褰曢泦鍚� - accountDetailsHongBaoMapService.saveAccountDetailsHongBaoMap(hbIdList, userMoneyDetail.getId()); - if (hbIdList.size() > 0) - recieveHongBaoIds.addAll(hbIdList); - } catch (UserMoneyDetailException e) { - try { - LogHelper.errorDetailInfo(e); - } catch (Exception e1) { - e1.printStackTrace(); - } - } + private void fanliInvite(List<HongBaoV2> hongBaoList, Long uid, int sourceType, String key, Date maxPreGetTime) + throws TeamEincomeRecordException, ParamsException, TeamRewardDebtException { + List<Long> hbIdList = new ArrayList<>(); + BigDecimal invitemoney = new BigDecimal(0); + Set<String> inviteOrders = new HashSet<>(); - // 鏂扮増閫氱煡 - userMoneyMsgNotificationService.shareOrderReceived(uid, Constant.SOURCE_TYPE_TAOBAO, shareOrders.size(), - shareGoodsCount, sharemoney, userInfoMapper.selectAvailableByPrimaryKey(uid).getMyHongBao()); + // 闇�瑕佸垽鏂��娆剧殑璁㈠崟鍙� + for (HongBaoV2 hongBao : hongBaoList) { + hongBao = filterWeiQuanINGHongBao(hongBao); + if (hongBao == null) + continue; + hongBao = hongBaoV2Mapper.selectByPrimaryKeyForUpdate(hongBao.getId()); + if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU || hongBao.getState() == HongBaoV2.STATE_KELINGQU) { + TeamRewardDebt debt = teamRewardDebtService.selectBySourceId(hongBao.getId()); + BigDecimal money = hongBao.getMoney(); + if (debt != null && debt.getLeftMoney().compareTo(new BigDecimal(0)) > 0 + && money.compareTo(debt.getLeftMoney()) >= 0) { + money = money.subtract(debt.getLeftMoney()); + // 杩橀挶 + teamRewardDebtService.repayDebt(debt.getId(), debt.getLeftMoney()); + } - for (String orderId : drawBackOrders) - taoBaoWeiQuanDrawBackService.doWeiQuanShare(orderId); - } + invitemoney = invitemoney.add(money); + HongBaoV2 updateHongBao = new HongBaoV2(); + updateHongBao.setId(hongBao.getId()); + updateHongBao.setGetTime(new Date()); + updateHongBao.setState(HongBaoV2.STATE_LINGQUING); + updateHongBao.setUpdateTime(new Date()); + hongBaoV2Mapper.updateByPrimaryKeySelective(updateHongBao); + // 娣诲姞鍒扮孩鍖呰繑鍒╄褰曢泦鍚� + hbIdList.add(hongBao.getId()); + HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByHongBaoId(hongBao.getParent().getId()); + inviteOrders.add(hongBaoOrder.getCommonOrder().getOrderNo()); + } + } - hongBaoV2SettleTempService.addTemp(recieveHongBaoIds, key); + // 閭�璇疯禋鍒拌处 + if (invitemoney.compareTo(new BigDecimal(0)) > 0) { + // 鑾峰彇褰撳墠鐨勬湀浠� + teamRewardManager.addToEincome(uid, maxPreGetTime, invitemoney, sourceType); + } - // 鎵�鏈夌殑杩斿埄鍒拌处绾㈠寘ID - for (Long hongBaoId : recieveHongBaoIds) { - try { - HongBaoRecieveCMQManager.getInstance().addHongBaoRecieveMsg(hongBaoId); - } catch (Exception e) { - LogHelper.errorDetailInfo(e); - } + hongBaoV2SettleTempService.addTemp(hbIdList, key); - } - } + // 鎵�鏈夌殑杩斿埄鍒拌处绾㈠寘ID + for (Long hongBaoId : hbIdList) { + try { + if (Constant.ENABLE_MQ) + HongBaoRecieveCMQManager.getInstance().addHongBaoRecieveMsg(hongBaoId); + } catch (Exception e) { + LogHelper.errorDetailInfo(e); + } + } + } - /** - * 鍏朵粬骞冲彴鍒嗕韩璧氳繑鍒� - * - * @param hongBaoList - * @param uid - * @param sourceType - */ - private void fanLiShareOther(List<HongBaoV2> hongBaoList, Long uid, int sourceType, String key) { - BigDecimal sharemoney = new BigDecimal(0); - List<Long> hbIdList = new ArrayList<>(); - List<Long> recieveHongBaoIds = new ArrayList<>(); - int shareGoodsCount = 0; - Set<String> shareOrders = new HashSet<>(); + /** + * 娣樺疂鍒嗕韩璧氳繑鍒� + * + * @param hongBaoList + * @param uid + * @throws TaoBaoWeiQuanException + */ + private void fanLiShareTB(List<HongBaoV2> hongBaoList, Long uid, String key) throws TaoBaoWeiQuanException { + BigDecimal sharemoney = new BigDecimal(0); + List<Long> hbIdList = new ArrayList<>(); + Set<String> drawBackOrders = new HashSet<String>(); + List<Long> recieveHongBaoIds = new ArrayList<>(); + Set<String> shareOrders = new HashSet<>(); - for (HongBaoV2 hongBao : hongBaoList) { - hongBao = hongBaoV2Mapper.selectByPrimaryKeyForUpdate(hongBao.getId()); - hongBao = filterWeiQuanINGHongBao(hongBao); - if (hongBao == null) - continue; + for (HongBaoV2 hongBao : hongBaoList) { + hongBao = hongBaoV2Mapper.selectByPrimaryKeyForUpdate(hongBao.getId()); + hongBao = filterWeiQuanINGHongBao(hongBao); + if (hongBao == null) + continue; - if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU || hongBao.getState() == HongBaoV2.STATE_KELINGQU) { - sharemoney = sharemoney.add(hongBao.getMoney()); - HongBaoV2 updateHongBao = new HongBaoV2(); - updateHongBao.setId(hongBao.getId()); - updateHongBao.setGetTime(new Date()); - updateHongBao.setState(HongBao.STATE_YILINGQU); - updateHongBao.setUpdateTime(new Date()); - hongBaoV2Mapper.updateByPrimaryKeySelective(updateHongBao); - // 娣诲姞鍒扮孩鍖呰繑鍒╄褰曢泦鍚� - hbIdList.add(hongBao.getId()); - // 2018-08-05 杩囧悗鐨勮鍗曟墠澶勭悊缁存潈 - HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByHongBaoId(hongBao.getId()); - if (hongBaoOrder != null) { - shareGoodsCount += hongBaoOrder.getCommonOrder().getCount(); - } - shareOrders.add(hongBaoOrder.getCommonOrder().getOrderNo()); - } - } + if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU || hongBao.getState() == HongBaoV2.STATE_KELINGQU) { + sharemoney = sharemoney.add(hongBao.getMoney()); + HongBaoV2 updateHongBao = new HongBaoV2(); + updateHongBao.setId(hongBao.getId()); + updateHongBao.setGetTime(new Date()); + updateHongBao.setState(HongBao.STATE_YILINGQU); + updateHongBao.setUpdateTime(new Date()); + hongBaoV2Mapper.updateByPrimaryKeySelective(updateHongBao); + // 娣诲姞鍒扮孩鍖呰繑鍒╄褰曢泦鍚� + hbIdList.add(hongBao.getId()); + // 2018-08-05 杩囧悗鐨勮鍗曟墠澶勭悊缁存潈 + Date balanceTime = null; + HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByHongBaoId(hongBao.getId()); + if (hongBaoOrder != null) { + balanceTime = hongBaoOrder.getCommonOrder().getSettleTime(); + } + shareOrders.add(hongBaoOrder.getCommonOrder().getOrderNo()); + if (balanceTime != null + && balanceTime.getTime() >= TimeUtil.convertToTimeTemp("2018-08-05", "yyyy-MM-dd")) { + drawBackOrders.add(hongBaoOrder.getCommonOrder().getOrderNo()); + } + } + } - /** - * 鍒嗕韩璧氬埌璐� - */ + /** + * 鍒嗕韩璧氬埌璐� + */ - if (sharemoney.compareTo(new BigDecimal(0)) > 0) { - // 娣诲姞鏂扮増璇︽儏璁板綍 - try { - // 璁$畻鏈湀鐨勬湁鏁堣鍗曪紝澶辨晥璁㈠崟锛岀淮鏉冭鍗� - Calendar ca = Calendar.getInstance(); + if (sharemoney.compareTo(new BigDecimal(0)) > 0) { + // 娣诲姞鏂扮増璇︽儏璁板綍 + try { + UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createShare(uid, Constant.SOURCE_TYPE_TAOBAO, + sharemoney, new Date()); + // 娣诲姞璧勯噾 + userMoneyService.addUserMoney(uid, sharemoney, userMoneyDetail); - Date maxDate = new Date(TimeUtil - .convertToTimeTemp(ca.get(Calendar.YEAR) + "-" + (ca.get(Calendar.MONTH) + 1), "yyyy-M")); - ca.add(Calendar.MONTH, -1); + // 娣诲姞鍒扮孩鍖呰繑鍒╄褰曢泦鍚� + accountDetailsHongBaoMapService.saveAccountDetailsHongBaoMap(hbIdList, userMoneyDetail.getId()); + if (hbIdList.size() > 0) + recieveHongBaoIds.addAll(hbIdList); + } catch (UserMoneyDetailException e) { + try { + LogHelper.errorDetailInfo(e); + } catch (Exception e1) { + e1.printStackTrace(); + } + } - Date minDate = new Date(TimeUtil - .convertToTimeTemp(ca.get(Calendar.YEAR) + "-" + (ca.get(Calendar.MONTH) + 1), "yyyy-M")); - long validCount = hongBaoV2Mapper.countShareOrderCountByUidAndSettleTime(uid, CommonOrder.STATE_JS, - minDate, maxDate, sourceType); - long weiQuanCount = hongBaoV2Mapper.countShareOrderCountByUidAndSettleTime(uid, CommonOrder.STATE_WQ, - minDate, maxDate, sourceType); + // 鏂扮増閫氱煡 + userMoneyMsgNotificationService.shareOrderReceived(uid, Constant.SOURCE_TYPE_TAOBAO, sharemoney, + userInfoMapper.selectAvailableByPrimaryKey(uid).getMyHongBao(), new Date()); - UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createShare(uid, sourceType, (int) validCount, - (int) weiQuanCount, sharemoney, new Date()); - // 娣诲姞璧勯噾 - userMoneyService.addUserMoney(uid, sharemoney, userMoneyDetail); + for (String orderId : drawBackOrders) + taoBaoWeiQuanDrawBackService.doWeiQuanShare(orderId); + } - // 娣诲姞鍒扮孩鍖呰繑鍒╄褰曢泦鍚� - accountDetailsHongBaoMapService.saveAccountDetailsHongBaoMap(hbIdList, userMoneyDetail.getId()); - if (hbIdList.size() > 0) - recieveHongBaoIds.addAll(hbIdList); - } catch (UserMoneyDetailException e) { - try { - LogHelper.errorDetailInfo(e); - } catch (Exception e1) { - e1.printStackTrace(); - } - } + hongBaoV2SettleTempService.addTemp(recieveHongBaoIds, key); - // 鏂扮増閫氱煡 - userMoneyMsgNotificationService.shareOrderReceived(uid, Constant.SOURCE_TYPE_TAOBAO, shareOrders.size(), - shareGoodsCount, sharemoney, userInfoMapper.selectAvailableByPrimaryKey(uid).getMyHongBao()); + // 鎵�鏈夌殑杩斿埄鍒拌处绾㈠寘ID + for (Long hongBaoId : recieveHongBaoIds) { + try { + if (Constant.ENABLE_MQ) + HongBaoRecieveCMQManager.getInstance().addHongBaoRecieveMsg(hongBaoId); + } catch (Exception e) { + LogHelper.errorDetailInfo(e); + } - } + } + } - hongBaoV2SettleTempService.addTemp(recieveHongBaoIds, key); + /** + * 鍏朵粬骞冲彴鍒嗕韩璧氳繑鍒� + * + * @param hongBaoList + * @param uid + * @param sourceType + */ + private void fanLiShareOther(List<HongBaoV2> hongBaoList, Long uid, int sourceType, String key) { + BigDecimal sharemoney = new BigDecimal(0); + List<Long> hbIdList = new ArrayList<>(); + List<Long> recieveHongBaoIds = new ArrayList<>(); + Set<String> shareOrders = new HashSet<>(); - // 鎵�鏈夌殑杩斿埄鍒拌处绾㈠寘ID - for (Long hongBaoId : recieveHongBaoIds) { - try { - HongBaoRecieveCMQManager.getInstance().addHongBaoRecieveMsg(hongBaoId); - } catch (Exception e) { - LogHelper.errorDetailInfo(e); - } - } - } + for (HongBaoV2 hongBao : hongBaoList) { + hongBao = hongBaoV2Mapper.selectByPrimaryKeyForUpdate(hongBao.getId()); + hongBao = filterWeiQuanINGHongBao(hongBao); + if (hongBao == null) + continue; - /** - * 閭�璇疯ˉ璐寸粨绠� - * - * @param orderId - * @param sourceType - */ - @Transactional(rollbackFor=Exception.class) - @Override - public void inviteSubsidySettle(Long uid, String taskKey) throws OrderMoneySettleException { - // 鏌ヨkey - List<HongBaoV2SettleTemp> list = hongBaoV2SettleTempService.listByKey(taskKey); - if (list != null && list.size() > 0) { + if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU || hongBao.getState() == HongBaoV2.STATE_KELINGQU) { + sharemoney = sharemoney.add(hongBao.getMoney()); + HongBaoV2 updateHongBao = new HongBaoV2(); + updateHongBao.setId(hongBao.getId()); + updateHongBao.setGetTime(new Date()); + updateHongBao.setState(HongBao.STATE_YILINGQU); + updateHongBao.setUpdateTime(new Date()); + hongBaoV2Mapper.updateByPrimaryKeySelective(updateHongBao); + // 娣诲姞鍒扮孩鍖呰繑鍒╄褰曢泦鍚� + hbIdList.add(hongBao.getId()); + // 2018-08-05 杩囧悗鐨勮鍗曟墠澶勭悊缁存潈 + HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByHongBaoId(hongBao.getId()); + shareOrders.add(hongBaoOrder.getCommonOrder().getOrderNo()); + } + } - Integer sourceType = null; - List<InviteOrderSubsidy> subsidyList = new ArrayList<>(); - for (HongBaoV2SettleTemp temp : list) { - Long hongBaoId = temp.getHongBaoId(); - HongBaoV2 hongBao = hongBaoV2Mapper.selectByPrimaryKey(hongBaoId); - if (hongBao != null) - sourceType = hongBao.getOrderType(); - if (hongBao.getParent() != null) - hongBao = hongBao.getParent(); - HongBaoOrder order = hongBaoOrderMapper.selectByHongBaoId(hongBao.getId()); - if (order != null && order.getCommonOrder() != null) { - InviteOrderSubsidy subSidy = inviteOrderSubsidyService.getByOrderNoAndTypeForUpdate(uid, - order.getCommonOrder().getOrderNo(), order.getCommonOrder().getSourceType()); - if (subSidy != null && subSidy.getState() == InviteOrderSubsidy.STATE_VALID) { - subsidyList.add(subSidy); - } - } - } + /** + * 鍒嗕韩璧氬埌璐� + */ - if (sourceType == null) - throw new OrderMoneySettleException(1, "璁㈠崟绫诲瀷鏈幏鍙栧埌"); + if (sharemoney.compareTo(new BigDecimal(0)) > 0) { + // 娣诲姞鏂扮増璇︽儏璁板綍 + try { + UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createShare(uid, sourceType, sharemoney, + new Date()); + // 娣诲姞璧勯噾 + userMoneyService.addUserMoney(uid, sharemoney, userMoneyDetail); - if (subsidyList.size() > 0) { - BigDecimal debtMoney = inviteOrderSubsidyDebtService.getTotalDebtMoney(uid); - BigDecimal originalDebtMoney = new BigDecimal(debtMoney.toString());// 鎬荤殑娆犳 - List<InviteOrderSubsidy> copySubsidyList = new ArrayList<>(); - copySubsidyList.addAll(subsidyList); - while (debtMoney.compareTo(new BigDecimal(0)) > 0 && copySubsidyList.size() > 0) {// 鏈夋瑺娆撅紝杩樻湁杩樼殑 - debtMoney = kouKuan(copySubsidyList, debtMoney); - } - // 杩樻 - BigDecimal repayMoney = originalDebtMoney.subtract(debtMoney); - BigDecimal bannce = null; - if (repayMoney.compareTo(new BigDecimal(0)) > 0) { - try { - bannce = inviteOrderSubsidyDebtService.repayDebtByUid(uid, repayMoney); - // 鍓╀綑鐨勮ˉ璐撮噾棰� - } catch (InviteOrderSubsidyDebtException e) { - throw new OrderMoneySettleException(2, "杩樻寮傚父"); - } - } - if (bannce != null && bannce.compareTo(new BigDecimal(0)) > 0) { - // 濡傛灉鏈夎繕鍓╀笅鐨勯挶灏卞姞鍒扮涓�涓笂闈� - subsidyList.get(0).setMoney(subsidyList.get(0).getMoney().add(bannce)); - } + // 娣诲姞鍒扮孩鍖呰繑鍒╄褰曢泦鍚� + accountDetailsHongBaoMapService.saveAccountDetailsHongBaoMap(hbIdList, userMoneyDetail.getId()); + if (hbIdList.size() > 0) + recieveHongBaoIds.addAll(hbIdList); + } catch (UserMoneyDetailException e) { + try { + LogHelper.errorDetailInfo(e); + } catch (Exception e1) { + e1.printStackTrace(); + } + } - BigDecimal totalMoney = new BigDecimal(0); - for (InviteOrderSubsidy subsidy : subsidyList) { - inviteOrderSubsidyService.settleById(subsidy.getId(), subsidy.getMoney()); - totalMoney = totalMoney.add(subsidy.getMoney()); - } + userMoneyMsgNotificationService.shareOrderReceived(uid, sourceType, sharemoney, + userInfoMapper.selectAvailableByPrimaryKey(uid).getMyHongBao(), new Date()); + } - // 鍒拌处 - UserMoneyDetail userMoneyDetail = null; - try { - userMoneyDetail = UserMoneyDetailFactory.createInviteSubsidy(uid, sourceType, totalMoney, - new Date()); - } catch (UserMoneyDetailException e) { - throw new OrderMoneySettleException(1, "璐︽埛鏄庣粏鏋勫缓鍑洪敊"); - } - // 娣诲姞璧勯噾 - userMoneyService.addUserMoney(uid, totalMoney, userMoneyDetail); - // 娣诲姞娑堟伅 - userMoneyMsgNotificationService.inviteOrderSubSidy(uid, new Date(), "绯荤粺鑷姩涓嬪彂", totalMoney, - userInfoMapper.selectAvailableByPrimaryKey(uid).getMyHongBao()); - } - } - } + hongBaoV2SettleTempService.addTemp(recieveHongBaoIds, key); - private String getTaskKey(Long uid) { - return uid + "-" + UUID.randomUUID().toString(); - } + // 鎵�鏈夌殑杩斿埄鍒拌处绾㈠寘ID + for (Long hongBaoId : recieveHongBaoIds) { + try { + if (Constant.ENABLE_MQ) + HongBaoRecieveCMQManager.getInstance().addHongBaoRecieveMsg(hongBaoId); + } catch (Exception e) { + LogHelper.errorDetailInfo(e); + } + } + } - private BigDecimal kouKuan(List<InviteOrderSubsidy> subsidyList, BigDecimal debtMoney) { - BigDecimal average = debtMoney.divide(new BigDecimal(subsidyList.size()), 2, RoundingMode.UP); - for (int i = 0; i < subsidyList.size(); i++) { - InviteOrderSubsidy sidy = subsidyList.get(i); - BigDecimal subMoney = null; - if (average.compareTo(sidy.getMoney()) >= 0) { - subMoney = sidy.getMoney(); - } else { - subMoney = average; - } - if (subMoney.compareTo(debtMoney) > 0) - subMoney = debtMoney; - debtMoney = debtMoney.subtract(subMoney); - sidy.setMoney(sidy.getMoney().subtract(subMoney)); + private String getTaskKey(Long uid) { + return uid + "-" + UUID.randomUUID().toString(); + } - if (sidy.getMoney().compareTo(new BigDecimal(0)) == 0) { - subsidyList.remove(i); - i--; - } - } - return debtMoney; - } } -- Gitblit v1.8.0