|  |  |  | 
|---|
|  |  |  | package cc.mrbird.febs.mall.service.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import cc.mrbird.febs.common.enumerates.AgentLevelEnum; | 
|---|
|  |  |  | import cc.mrbird.febs.common.enumerates.MoneyFlowTypeEnum; | 
|---|
|  |  |  | import cc.mrbird.febs.common.enumerates.*; | 
|---|
|  |  |  | import cc.mrbird.febs.common.utils.AppContants; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.entity.AgentInfo; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.entity.DataDictionaryCustom; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.entity.MallMember; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.entity.MallOrderInfo; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.mapper.DataDictionaryCustomMapper; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.mapper.MallMemberMapper; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.mapper.MallMemberWalletMapper; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.mapper.MallOrderInfoMapper; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.service.IAgentService; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.service.IApiMallMemberService; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.service.IApiMallMemberWalletService; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.entity.*; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.mapper.*; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.service.*; | 
|---|
|  |  |  | import cn.hutool.core.collection.CollUtil; | 
|---|
|  |  |  | import cn.hutool.core.util.ObjectUtil; | 
|---|
|  |  |  | import cn.hutool.core.util.StrUtil; | 
|---|
|  |  |  | import cn.hutool.json.JSONUtil; | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import jdk.nashorn.internal.ir.IfNode; | 
|---|
|  |  |  | import lombok.RequiredArgsConstructor; | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private final DataDictionaryCustomMapper dataDictionaryCustomMapper; | 
|---|
|  |  |  | private final MallMemberMapper memberMapper; | 
|---|
|  |  |  | private final MallOrderInfoMapper orderInfoMapper; | 
|---|
|  |  |  | private final MallOrderInfoMapper mallOrderInfoMapper; | 
|---|
|  |  |  | private final IMallMoneyFlowService mallMoneyFlowService; | 
|---|
|  |  |  | private final IApiMallMemberWalletService memberWalletService; | 
|---|
|  |  |  | private final IApiMallMemberService memberService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | 
|---|
|  |  |  | List<String> ids = StrUtil.split(member.getReferrerIds(), ','); | 
|---|
|  |  |  | List<MallMember> parentMembers = memberMapper.selectByInviteIds(ids); | 
|---|
|  |  |  | for (MallMember parent : parentMembers) { | 
|---|
|  |  |  | // 未激活用户无法升级 | 
|---|
|  |  |  | if (AgentLevelEnum.ZERO_LEVEL.name().equals(member.getLevel())) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | DataDictionaryCustom nextLevel = dataDictionaryCustomMapper.selectNextAgentLevelInfo(parent.getLevel()); | 
|---|
|  |  |  | if (nextLevel == null) { | 
|---|
|  |  |  | log.info("当前层级无下一级:{}", parent.getLevel()); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | AgentInfo agentInfo = JSONObject.parseObject(nextLevel.getValue(), AgentInfo.class); | 
|---|
|  |  |  | if (!orderCntFinish(parent, agentInfo)) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | if (!directMemberCnt(parent, agentInfo)) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!agentCntFinish(parent, agentInfo)) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!teamIncome(parent, agentInfo)) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | parent.setLevel(nextLevel.getCode()); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 判断用户直推下单数量或团队下单数量是否达标 | 
|---|
|  |  |  | * 判断直推人数是否达标 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param member | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private boolean orderCntFinish(MallMember member, AgentInfo agentInfo) { | 
|---|
|  |  |  | Integer cnt = orderInfoMapper.selectCntDirectOrTeam(agentInfo.getOrderType(), member.getInviteId()); | 
|---|
|  |  |  | private boolean directMemberCnt(MallMember member, AgentInfo agentInfo) { | 
|---|
|  |  |  | List<MallMember> childs = memberMapper.selectByRefererId(member.getInviteId()); | 
|---|
|  |  |  | if (CollUtil.isEmpty(childs)) { | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (cnt >= agentInfo.getOrderCnt()) { | 
|---|
|  |  |  | if (childs.size() >= agentInfo.getDirectCnt()) { | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | log.info("用户{}订单未达标, 当前数量为:{},要求数量:{}, 当前等级为:{}", member.getAccount(), cnt, agentInfo.getOrderCnt(), member.getLevel()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | log.info("用户:{}直推数量未达标, 当前等级:{}, 当前数量:{}, 目标数量:{}", member.getPhone(), member.getLevel(), childs.size(), agentInfo.getLastAgentCnt()); | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private boolean agentCntFinish(MallMember member, AgentInfo agentInfo) { | 
|---|
|  |  |  | if (agentInfo.getLastCnt() == null) { | 
|---|
|  |  |  | if (agentInfo.getLastAgentCnt() == null || agentInfo.getLastAgentCnt() == 0) { | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (i >= agentInfo.getLastCnt()) { | 
|---|
|  |  |  | if (i >= agentInfo.getLastAgentCnt()) { | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | log.info("用户:{}代理数量未达标, 当前等级:{}, 当前数量:{}, 目标数量:{}", member.getAccount(), member.getLevel(), i, agentInfo.getLastCnt()); | 
|---|
|  |  |  | log.info("用户:{}代理数量未达标, 当前等级:{}, 当前数量:{}, 目标数量:{}", member.getPhone(), member.getLevel(), i, agentInfo.getLastAgentCnt()); | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 团队业绩是否达标 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param agentInfo | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private boolean teamIncome(MallMember member, AgentInfo agentInfo) { | 
|---|
|  |  |  | BigDecimal totalIncome = memberMapper.selectAchieveByMemberId(member.getInviteId(), 2); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | BigDecimal targetIncome = agentInfo.getTeamIncome().multiply(new BigDecimal("10000")); | 
|---|
|  |  |  | if (totalIncome.compareTo(targetIncome) >= 0) { | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | log.info("用户:{}团队业绩未达标, 当前等级:{}, 当前数量:{}, 目标数量:{}", member.getPhone(), member.getLevel(), totalIncome, targetIncome); | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | public void returnMoneyToAgent(Long orderId) { | 
|---|
|  |  |  | MallOrderInfo orderInfo = orderInfoMapper.selectById(orderId); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | MallMember member = memberMapper.selectById(orderInfo.getMemberId()); | 
|---|
|  |  |  | if (StrUtil.isBlank(member.getReferrerIds())) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 直推奖励 | 
|---|
|  |  |  | directReturnMoney(member, orderInfo); | 
|---|
|  |  |  | // 团队奖励 | 
|---|
|  |  |  | teamReturnMoney(member, orderInfo); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 直推奖励 | 
|---|
|  |  |  | * @param member | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private void directReturnMoney(MallMember member, MallOrderInfo orderInfo) { | 
|---|
|  |  |  | MallMember parent = memberMapper.selectInfoByInviteId(member.getReferrerId()); | 
|---|
|  |  |  | if (AgentLevelEnum.FIRST_LEVEL.name().equals(parent.getLevel())) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void rankReturnMoney(Long orderId) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | AgentInfo agentInfo = getAgentInfo(parent.getLevel()); | 
|---|
|  |  |  | if (agentInfo == null) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | BigDecimal returnMoney = agentInfo.getDirectIncome().divide(BigDecimal.valueOf(100), RoundingMode.UP).multiply(orderInfo.getAmount()); | 
|---|
|  |  |  | memberWalletService.addBalance(returnMoney, parent.getId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | memberService.addMoneyFlow(parent.getId(), returnMoney, MoneyFlowTypeEnum.BONUS.getValue(), orderInfo.getOrderNo(), null, null, orderInfo.getMemberId(), null); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 团队奖励 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void teamReturnMoney(MallMember member, MallOrderInfo orderInfo) { | 
|---|
|  |  |  | List<String> inviteIds = StrUtil.split(member.getReferrerIds(), ','); | 
|---|
|  |  |  | List<MallMember> agentList = memberMapper.selectMemberParentAgentList(inviteIds); | 
|---|
|  |  |  | if (CollUtil.isEmpty(agentList)) { | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public void directReward(Long directRewardId) { | 
|---|
|  |  |  | log.info("消费补贴奖励、直推返利、分享奖励直推上级、分享奖励直推上级的直推、团队管理补贴消息,ID:{}", directRewardId); | 
|---|
|  |  |  | MallOrderInfo orderInfo = mallOrderInfoMapper.selectById(directRewardId); | 
|---|
|  |  |  | if(ObjectUtil.isEmpty(orderInfo)){ | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Integer status = orderInfo.getStatus(); | 
|---|
|  |  |  | if(OrderStatusEnum.FINISH.getValue() != status){ | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //下单补贴金额 | 
|---|
|  |  |  | BigDecimal subsidyAmount = orderInfo.getSubsidyAmount(); | 
|---|
|  |  |  | //下单人 | 
|---|
|  |  |  | Long memberId = orderInfo.getMemberId(); | 
|---|
|  |  |  | MallMember mallMember = memberMapper.selectById(memberId); | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 生成补贴的流水记录 | 
|---|
|  |  |  | *  1、待生效 | 
|---|
|  |  |  | *  2、返利对象,下单人 | 
|---|
|  |  |  | *  3、金额,补贴金额 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | Long subsidyAmountFlowId = mallMoneyFlowService.addMoneyFlow( | 
|---|
|  |  |  | mallMember.getId(), | 
|---|
|  |  |  | orderInfo.getOrderNo(), | 
|---|
|  |  |  | subsidyAmount.setScale(4, BigDecimal.ROUND_DOWN), | 
|---|
|  |  |  | MallMoneyFlowTypeEnum.MARKET_SUBSIDIES.getCode(), | 
|---|
|  |  |  | MallMoneyFlow.STATUS_SUCCESS, | 
|---|
|  |  |  | MallMoneyFlow.IS_RETURN_Y, | 
|---|
|  |  |  | mallMember.getId(), | 
|---|
|  |  |  | FlowTypeEnum.BALANCE.getValue(), | 
|---|
|  |  |  | MallMoneyFlowTypeEnum.MARKET_SUBSIDIES.getName() | 
|---|
|  |  |  | ); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Map<String, Map<Long, BigDecimal>> needReturn = new HashMap<>(); | 
|---|
|  |  |  | for (MallMember mallMember : agentList) { | 
|---|
|  |  |  | String level = mallMember.getLevel(); | 
|---|
|  |  |  | memberWalletService.addBalance(subsidyAmount.setScale(2, BigDecimal.ROUND_DOWN),mallMember.getId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Map<Long, BigDecimal> amount = needReturn.get(level); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (amount != null) { | 
|---|
|  |  |  | //                amount = new HashMap<>(); | 
|---|
|  |  |  | //                AgentInfo agentInfo = getAgentInfo(level); | 
|---|
|  |  |  | //                if (agentInfo == null) { | 
|---|
|  |  |  | //                    continue; | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 生成直推返利的流水记录 | 
|---|
|  |  |  | *  1、待生效 | 
|---|
|  |  |  | *  2、返利对象:下单人的直推上级 | 
|---|
|  |  |  | *  3、金额:返利比例乘以补贴金额 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 生成分享奖励的流水记录 | 
|---|
|  |  |  | *  1、待生效 | 
|---|
|  |  |  | *  2、返利对象:下单人的直推上级 10%,直推的直推 5% | 
|---|
|  |  |  | *  3、金额:补贴金额乘以分享奖励的比例 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | if(StrUtil.isEmpty(mallMember.getReferrerId())){ | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //下单人的直推上级 | 
|---|
|  |  |  | MallMember mallMemberOne = memberMapper.selectInfoByInviteId(mallMember.getReferrerId()); | 
|---|
|  |  |  | //生成直推返利的流水记录 | 
|---|
|  |  |  | if(ObjectUtil.isNotEmpty(mallMemberOne)){ | 
|---|
|  |  |  | //            DataDictionaryCustom rewardPercentDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( | 
|---|
|  |  |  | //                    DataDictionaryEnum.REWARD_PERCENT.getType(), | 
|---|
|  |  |  | //                    DataDictionaryEnum.REWARD_PERCENT.getCode() | 
|---|
|  |  |  | //            ); | 
|---|
|  |  |  | //            BigDecimal rewardPercent = new BigDecimal(rewardPercentDic.getValue()).multiply(new BigDecimal(0.01)).setScale(2,BigDecimal.ROUND_DOWN); | 
|---|
|  |  |  | //            BigDecimal directRewardAmount = subsidyAmount.multiply(rewardPercent).setScale(4, BigDecimal.ROUND_DOWN); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                BigDecimal returnMoney; | 
|---|
|  |  |  | //                if (AgentInfo.TEAM_INCOME_TYPE_AMOUNT.equals(agentInfo.getTeamIncomeType())) { | 
|---|
|  |  |  | //                    returnMoney = agentInfo.getTeamIncome(); | 
|---|
|  |  |  | //                } else { | 
|---|
|  |  |  | //                    returnMoney = agentInfo.getTeamIncome().divide(BigDecimal.valueOf(100), RoundingMode.UP).multiply(orderInfo.getAmount()); | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | //                amount.put(mallMember.getId(), returnMoney); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | amount = new HashMap<>(); | 
|---|
|  |  |  | AgentInfo agentInfo = getAgentInfo(level); | 
|---|
|  |  |  | if (agentInfo == null) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | BigDecimal returnMoney; | 
|---|
|  |  |  | if (AgentInfo.TEAM_INCOME_TYPE_AMOUNT.equals(agentInfo.getTeamIncomeType())) { | 
|---|
|  |  |  | returnMoney = agentInfo.getTeamIncome(); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | returnMoney = agentInfo.getTeamIncome().divide(BigDecimal.valueOf(100), RoundingMode.UP).multiply(orderInfo.getAmount()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | amount.put(mallMember.getId(), returnMoney); | 
|---|
|  |  |  | needReturn.put(level, amount); | 
|---|
|  |  |  | //            Long marketSubsidyFlowId = mallMoneyFlowService.addMoneyFlow( | 
|---|
|  |  |  | //                    mallMember.getId(), | 
|---|
|  |  |  | //                    orderInfo.getOrderNo(), | 
|---|
|  |  |  | //                    directRewardAmount, | 
|---|
|  |  |  | //                    MallMoneyFlowTypeEnum.DIRECT_REWARD.getCode(), | 
|---|
|  |  |  | //                    MallMoneyFlow.STATUS_SUCCESS, | 
|---|
|  |  |  | //                    MallMoneyFlow.IS_RETURN_Y, | 
|---|
|  |  |  | //                    mallMemberOne.getId(), | 
|---|
|  |  |  | //                    FlowTypeEnum.BALANCE.getValue(), | 
|---|
|  |  |  | //                    MallMoneyFlowTypeEnum.DIRECT_REWARD.getName() | 
|---|
|  |  |  | //            ); | 
|---|
|  |  |  | //            memberWalletService.addBalance(directRewardAmount,mallMemberOne.getId()); | 
|---|
|  |  |  | DataDictionaryCustom shareOneDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( | 
|---|
|  |  |  | DataDictionaryEnum.LEVEL_ONE.getType(), | 
|---|
|  |  |  | DataDictionaryEnum.LEVEL_ONE.getCode() | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | //生成分享奖励的流水记录 | 
|---|
|  |  |  | BigDecimal shareOne = new BigDecimal(shareOneDic.getValue()).multiply(new BigDecimal(0.01)).setScale(2,BigDecimal.ROUND_DOWN); | 
|---|
|  |  |  | BigDecimal shareOneAmount = subsidyAmount.multiply(shareOne).setScale(4, BigDecimal.ROUND_DOWN); | 
|---|
|  |  |  | Long ShareOneFlowId = mallMoneyFlowService.addMoneyFlow( | 
|---|
|  |  |  | mallMember.getId(), | 
|---|
|  |  |  | orderInfo.getOrderNo(), | 
|---|
|  |  |  | shareOneAmount, | 
|---|
|  |  |  | MallMoneyFlowTypeEnum.SHARE_REWARD_ONE.getCode(), | 
|---|
|  |  |  | MallMoneyFlow.STATUS_SUCCESS, | 
|---|
|  |  |  | MallMoneyFlow.IS_RETURN_Y, | 
|---|
|  |  |  | mallMemberOne.getId(), | 
|---|
|  |  |  | FlowTypeEnum.BALANCE.getValue(), | 
|---|
|  |  |  | MallMoneyFlowTypeEnum.SHARE_REWARD_ONE.getName() | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | memberWalletService.addBalance(shareOneAmount,mallMemberOne.getId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(needReturn.isEmpty()) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | if(StrUtil.isEmpty(mallMemberOne.getReferrerId())){ | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //下单人的直推上级的直推 | 
|---|
|  |  |  | MallMember mallMemberTwo = memberMapper.selectInfoByInviteId(mallMemberOne.getReferrerId()); | 
|---|
|  |  |  | if(ObjectUtil.isNotEmpty(mallMemberTwo)){ | 
|---|
|  |  |  | DataDictionaryCustom shareTwoDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( | 
|---|
|  |  |  | DataDictionaryEnum.LEVEL_TWO.getType(), | 
|---|
|  |  |  | DataDictionaryEnum.LEVEL_TWO.getCode() | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | BigDecimal shareTwo = new BigDecimal(shareTwoDic.getValue()).multiply(new BigDecimal(0.01)).setScale(2,BigDecimal.ROUND_DOWN); | 
|---|
|  |  |  | BigDecimal shareTwoAmount = subsidyAmount.multiply(shareTwo).setScale(4, BigDecimal.ROUND_DOWN); | 
|---|
|  |  |  | Long shareTwoFlowId = mallMoneyFlowService.addMoneyFlow( | 
|---|
|  |  |  | mallMember.getId(), | 
|---|
|  |  |  | orderInfo.getOrderNo(), | 
|---|
|  |  |  | shareTwoAmount, | 
|---|
|  |  |  | MallMoneyFlowTypeEnum.SHARE_REWARD_TWO.getCode(), | 
|---|
|  |  |  | MallMoneyFlow.STATUS_SUCCESS, | 
|---|
|  |  |  | MallMoneyFlow.IS_RETURN_Y, | 
|---|
|  |  |  | mallMemberTwo.getId(), | 
|---|
|  |  |  | FlowTypeEnum.BALANCE.getValue(), | 
|---|
|  |  |  | MallMoneyFlowTypeEnum.SHARE_REWARD_TWO.getName() | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | memberWalletService.addBalance(shareTwoAmount,mallMemberTwo.getId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 团队管理补贴 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | for (Map.Entry<String, Map<Long, BigDecimal>> entry : needReturn.entrySet()) { | 
|---|
|  |  |  | for (Map.Entry<Long, BigDecimal> amount : entry.getValue().entrySet()) { | 
|---|
|  |  |  | memberWalletService.addBalance(amount.getValue(), amount.getKey()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | memberService.addMoneyFlow(amount.getKey(), amount.getValue(), MoneyFlowTypeEnum.ACHIEVE.getValue(), orderInfo.getOrderNo(), null, null, orderInfo.getMemberId(), null); | 
|---|
|  |  |  | if(StrUtil.isEmpty(mallMember.getReferrerIds())){ | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //下单人所有的上级 | 
|---|
|  |  |  | String referrerIds = mallMember.getReferrerIds(); | 
|---|
|  |  |  | List<String> refererIdList = StrUtil.split(referrerIds, ",", -1, true, true); | 
|---|
|  |  |  | //下单人所有符合条件的上级-邀请码 | 
|---|
|  |  |  | ArrayList<String> refererIdListUp = new ArrayList<>(); | 
|---|
|  |  |  | //初始级别即当前下单人的级别 | 
|---|
|  |  |  | String levelStart = mallMember.getLevel(); | 
|---|
|  |  |  | if(CollUtil.isNotEmpty(refererIdList)){ | 
|---|
|  |  |  | for(String inviteId : refererIdList){ | 
|---|
|  |  |  | MallMember mallMemberUp = memberMapper.selectInfoByInviteId(inviteId); | 
|---|
|  |  |  | String levelUp = mallMemberUp.getLevel(); | 
|---|
|  |  |  | //团队补贴从区代开始 | 
|---|
|  |  |  | if(!MemberAgentLevelEnum.AGENT.getName().equals(levelUp)){ | 
|---|
|  |  |  | //比较两个代理级别,同级别或者大于下单人的级别都保留 | 
|---|
|  |  |  | int compareLevel = MemberAgentLevelEnum.AGENT_ONE.compareLevel(levelUp,levelStart); | 
|---|
|  |  |  | if(0 < compareLevel){ | 
|---|
|  |  |  | levelStart = levelUp; | 
|---|
|  |  |  | refererIdListUp.add(inviteId); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(CollUtil.isNotEmpty(refererIdListUp)){ | 
|---|
|  |  |  | levelStart = mallMember.getLevel(); | 
|---|
|  |  |  | for(String inviteId : refererIdList){ | 
|---|
|  |  |  | MallMember mallMemberUp = memberMapper.selectInfoByInviteId(inviteId); | 
|---|
|  |  |  | String levelUp = mallMemberUp.getLevel(); | 
|---|
|  |  |  | //获取团队分享比例 | 
|---|
|  |  |  | BigDecimal manageSubsidyPercent = getManageSubsidyPercent(levelStart, levelUp); | 
|---|
|  |  |  | if(manageSubsidyPercent.compareTo(BigDecimal.ZERO) > 0){ | 
|---|
|  |  |  | BigDecimal teamManageAmount = subsidyAmount.multiply(manageSubsidyPercent).setScale(4, BigDecimal.ROUND_DOWN); | 
|---|
|  |  |  | //生成团队分享流水 | 
|---|
|  |  |  | Long teamManageFlowId = mallMoneyFlowService.addMoneyFlow( | 
|---|
|  |  |  | mallMember.getId(), | 
|---|
|  |  |  | orderInfo.getOrderNo(), | 
|---|
|  |  |  | teamManageAmount, | 
|---|
|  |  |  | MallMoneyFlowTypeEnum.TEAM_REWARD.getCode(), | 
|---|
|  |  |  | MallMoneyFlow.STATUS_SUCCESS, | 
|---|
|  |  |  | MallMoneyFlow.IS_RETURN_Y, | 
|---|
|  |  |  | mallMemberUp.getId(), | 
|---|
|  |  |  | FlowTypeEnum.BALANCE.getValue(), | 
|---|
|  |  |  | MallMoneyFlowTypeEnum.TEAM_REWARD.getName() | 
|---|
|  |  |  | ); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | memberWalletService.addBalance(teamManageAmount,mallMemberUp.getId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | levelStart = levelUp; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private AgentInfo getAgentInfo(String level) { | 
|---|
|  |  |  | DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(AppContants.AGENT_LEVEL_REQUIRE, level); | 
|---|
|  |  |  | if (dic == null) { | 
|---|
|  |  |  | log.info("当前用户父级等级:{}", level); | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 根据传入的两个级别,比较之后获取对应的团队管理奖励比例 | 
|---|
|  |  |  | * @param levelStart | 
|---|
|  |  |  | * @param levelUp | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private BigDecimal getManageSubsidyPercent(String levelStart,String levelUp){ | 
|---|
|  |  |  | BigDecimal manageSubsidyPercent = BigDecimal.ZERO; | 
|---|
|  |  |  | String agentCodeStart = MemberAgentLevelEnum.AGENT_ONE.getCodeByName(levelStart); | 
|---|
|  |  |  | String agentCodeUp = MemberAgentLevelEnum.AGENT_ONE.getCodeByName(levelUp); | 
|---|
|  |  |  | int compareLevel = MemberAgentLevelEnum.AGENT_ONE.compareLevel(levelUp, levelStart); | 
|---|
|  |  |  | //如果是平级 | 
|---|
|  |  |  | if(compareLevel == 2){ | 
|---|
|  |  |  | manageSubsidyPercent = getDicSubsidyPercent(agentCodeStart); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return JSONObject.parseObject(dic.getValue(), AgentInfo.class); | 
|---|
|  |  |  | //如果不是平级 | 
|---|
|  |  |  | if(compareLevel == 1){ | 
|---|
|  |  |  | //1、初始级别不能是会员 | 
|---|
|  |  |  | //2、当前级别的上一个级别是否是levelStart, | 
|---|
|  |  |  | // 满足,需要减去levelStart的团队管理奖励 | 
|---|
|  |  |  | String minLevel = MemberAgentLevelEnum.AGENT_ONE.minLevel(agentCodeUp); | 
|---|
|  |  |  | if(levelStart.equals(minLevel) | 
|---|
|  |  |  | && !MemberAgentLevelEnum.AGENT.getName().equals(levelStart)){ | 
|---|
|  |  |  | //                    && MemberAgentLevelEnum.AGENT.getName()levelStart){ | 
|---|
|  |  |  | BigDecimal dicManageSubsidyPercentUp = getDicManageSubsidyPercent(agentCodeUp); | 
|---|
|  |  |  | BigDecimal dicManageSubsidyPercentStart = getDicManageSubsidyPercent(agentCodeStart); | 
|---|
|  |  |  | manageSubsidyPercent = dicManageSubsidyPercentUp.subtract(dicManageSubsidyPercentStart); | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | manageSubsidyPercent = getDicManageSubsidyPercent(agentCodeUp); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return manageSubsidyPercent; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取对应的团队管理奖励比例 | 
|---|
|  |  |  | * @param agentCode | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private BigDecimal getDicManageSubsidyPercent(String agentCode){ | 
|---|
|  |  |  | BigDecimal manageSubsidyPercent = BigDecimal.ZERO; | 
|---|
|  |  |  | DataDictionaryCustom agentDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( | 
|---|
|  |  |  | DataDictionaryEnum.AGENT_ONE.getType(), | 
|---|
|  |  |  | agentCode | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(agentDic.getValue()); | 
|---|
|  |  |  | String manageSubsidyPercentStr = jsonObject.get("manageSubsidyPercent").toString(); | 
|---|
|  |  |  | manageSubsidyPercent =  new BigDecimal(manageSubsidyPercentStr) | 
|---|
|  |  |  | .multiply(new BigDecimal(0.01)) | 
|---|
|  |  |  | .setScale(2,BigDecimal.ROUND_DOWN); | 
|---|
|  |  |  | return manageSubsidyPercent; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取对应的平级团队管理奖励比例 | 
|---|
|  |  |  | * @param agentCode | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private BigDecimal getDicSubsidyPercent(String agentCode){ | 
|---|
|  |  |  | BigDecimal subsidyPercent = BigDecimal.ZERO; | 
|---|
|  |  |  | DataDictionaryCustom agentDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( | 
|---|
|  |  |  | DataDictionaryEnum.AGENT_ONE.getType(), | 
|---|
|  |  |  | agentCode | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(agentDic.getValue()); | 
|---|
|  |  |  | String subsidyPercentStr = jsonObject.get("manageSubsidyPercent").toString(); | 
|---|
|  |  |  | subsidyPercent =  new BigDecimal(subsidyPercentStr) | 
|---|
|  |  |  | .multiply(new BigDecimal(0.01)) | 
|---|
|  |  |  | .setScale(2,BigDecimal.ROUND_DOWN); | 
|---|
|  |  |  | return subsidyPercent; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|