| | |
| | | import cc.mrbird.febs.mall.service.*; |
| | | import cc.mrbird.febs.rabbit.producter.AgentProducer; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.RandomUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import lombok.Data; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | private final MallMemberMapper mallMemberMapper; |
| | | private final MallOrderInfoMapper mallOrderInfoMapper; |
| | | private final MallOrderItemMapper mallOrderItemMapper; |
| | | private final MallGoodsMapper mallGoodsMapper; |
| | | private final IApiMallMemberWalletService walletService; |
| | | private final IMallMoneyFlowService moneyFlowService; |
| | | private final DataDictionaryCustomMapper dataDictionaryCustomMapper; |
| | | private final MallMoneyFlowMapper mallMoneyFlowMapper; |
| | | private final MallAchieveRecordMapper mallAchieveRecordMapper; |
| | | private final RedisUtils redisUtils; |
| | | private final MallMemberWalletMapper mallMemberWalletMapper; |
| | | private final IAgentService iAgentService; |
| | | private final IMallMoneyFlowService mallMoneyFlowService; |
| | | private final MallMqRecordMapper mallMqRecordMapper; |
| | | private final AgentProducer agentProducer; |
| | | private final MallScoreRecordMapper mallScoreRecordMapper; |
| | | private final MallScoreAchieveReleaseMapper mallScoreAchieveReleaseMapper; |
| | | private final MallScoreVoucherMapper mallScoreVoucherMapper; |
| | | |
| | | private final MallSystemSettingMapper mallSystemSettingMapper; |
| | | private final IAdminMallMemberService adminMallMemberService; |
| | | |
| | | /** |
| | | * 直推收益 |
| | |
| | | log.info("积分订单无返利"); |
| | | return; |
| | | } |
| | | BigDecimal indirectPer = BigDecimal.valueOf(0.25); |
| | | BigDecimal indirectPer2 = BigDecimal.valueOf(0.15); |
| | | |
| | | MallMember member = mallMemberMapper.selectById(orderInfo.getMemberId()); |
| | | |
| | |
| | | return; |
| | | } |
| | | |
| | | // 父级会员直推人数 |
| | | Integer directCnt = mallMemberMapper.selectOwnCntByInviteIdAndAccountLevel(parentMember.getInviteId(), parentMember.getAccountLevel()); |
| | | // 非代理推代理,非代理上级拿的收益比例 |
| | | DataDictionaryCustom indirectPerDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.UNAGENT_TO_AGENT_PARENT.getType(), DataDictionaryEnum.UNAGENT_TO_AGENT_PARENT.getCode()); |
| | | |
| | | |
| | | // 非代理推代理,非代理上级拿的收益比例 |
| | | DataDictionaryCustom indirectPerTwoDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.UNAGENT_TO_AGENT_PARENT_TWO.getType(), DataDictionaryEnum.UNAGENT_TO_AGENT_PARENT_TWO.getCode()); |
| | | |
| | | |
| | | // 非代理推代理,非代理的直推收益比例 |
| | | DataDictionaryCustom unAgentDirectDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.UNAGENT_TO_AGENT.getType(), DataDictionaryEnum.UNAGENT_TO_AGENT.getCode()); |
| | | |
| | | // 父级需要拥有 此会员 对应级别的直推数量 |
| | | Integer directCnt = mallMemberMapper.selectOwnCntByInviteIdAndAccountLevel(parentMember.getInviteId(), member.getAccountLevel()); |
| | | List<DataDictionaryCustom> dataDices = dataDictionaryCustomMapper.selectDicByType(DataDictionaryEnum.DIRECT_BONUS_SETTING.getType()); |
| | | |
| | | directCnt = directCnt == null ? 0 : directCnt; |
| | |
| | | return; |
| | | } |
| | | |
| | | // 推荐奖比例 |
| | | DataDictionaryCustom indrectDicPropDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.RECOMMEND_BONUS.getType(), DataDictionaryEnum.RECOMMEND_BONUS.getCode()); |
| | | |
| | | for (MallOrderItem item : items) { |
| | | // 减去成本后算收益 |
| | | BigDecimal amount = item.getPrice().subtract(item.getCostPrice()).multiply(BigDecimal.valueOf(item.getCnt())); |
| | | // 减去成本后算收益 减去积分付款 |
| | | BigDecimal amount = item.getPrice().subtract(item.getCostPrice()).multiply(BigDecimal.valueOf(item.getCnt())).subtract(orderInfo.getScoreAmount()); |
| | | if (amount.compareTo(BigDecimal.ZERO) < 1) { |
| | | continue; |
| | | } |
| | | |
| | | // ----- 直推奖 start ------- |
| | | // 直推返利比例 |
| | | BigDecimal profitPer = BigDecimal.ZERO; |
| | | int isSameLevel = 0; |
| | |
| | | } else { |
| | | |
| | | // 判断上级是否与自己购买的套餐符合,若符合则走3级直推逻辑,若不符合则另外一个 |
| | | if (parentMember.getAccountLevel().equals(item.getGoodsLevel()) || AccountLevelEnums.VIP.getLevel().equals(parentMember.getAccountLevel())) { |
| | | |
| | | if (parentMember.getAccountLevel() == 1 && item.getGoodsLevel() != 1) { |
| | | for (DataDictionaryCustom dataDic : dataDices) { |
| | | JSONObject jsonObject = JSONObject.parseObject(dataDic.getValue()); |
| | | if (directCnt >= jsonObject.getInteger("pushCnt")) { |
| | |
| | | isSameLevel = 1; |
| | | // 非代理推代理/非股东推股东 |
| | | } else { |
| | | profitPer = BigDecimal.valueOf(10); |
| | | profitPer = new BigDecimal(unAgentDirectDic.getValue()); |
| | | isSameLevel = 2; |
| | | } |
| | | } |
| | | |
| | | // 直推奖 |
| | | BigDecimal profit = amount.multiply(profitPer.divide(new BigDecimal(100), 2, RoundingMode.HALF_UP)); |
| | | log.info("直推奖励:{}", profit); |
| | | changeScoreAndCommission(parentMember.getId(), profit, MoneyFlowTypeEnum.DIRECT_BONUS.getValue(), orderInfo.getOrderNo()); |
| | | |
| | | // 代理推代理 |
| | | if (isSameLevel == 1) { |
| | | MallMember doubleParentMember = mallMemberMapper.selectInfoByInviteId(parentMember.getReferrerId()); |
| | | if (doubleParentMember == null) { |
| | | continue; |
| | | } |
| | | |
| | | BigDecimal doubleParentProfit = profit.multiply(indirectPer); |
| | | changeScoreAndCommission(doubleParentMember.getId(), doubleParentProfit, MoneyFlowTypeEnum.DIRECT_BONUS.getValue(), orderInfo.getOrderNo()); |
| | | } |
| | | |
| | | // 非代理推代理 |
| | | if (isSameLevel == 2) { |
| | |
| | | continue; |
| | | } |
| | | |
| | | List<MallMember> mallMembers = mallMemberMapper.selectParentMemberList(StrUtil.split(parentMember.getReferrerIds(), ','), parentMember.getReferrerId(), 2); |
| | | List<MallMember> mallMembers = adminMallMemberService.findParentMemberList(StrUtil.split(parentMember.getReferrerIds(), ','), 2, true); |
| | | if (CollUtil.isEmpty(mallMembers)) { |
| | | continue; |
| | | } |
| | | |
| | | int index = 1; |
| | | for (MallMember mallMember : mallMembers) { |
| | | if (!item.getGoodsLevel().equals(mallMember.getAccountLevel())) { |
| | | break; |
| | | if (mallMember.getAccountLevel() == 1) { |
| | | continue; |
| | | } |
| | | |
| | | BigDecimal doubleParentProfit = profit.multiply(indirectPer2); |
| | | if (index == 2) { |
| | | indirectPerDic = indirectPerTwoDic; |
| | | } |
| | | BigDecimal doubleParentProfit = amount.multiply(new BigDecimal(indirectPerDic.getValue()).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP)); |
| | | changeScoreAndCommission(mallMember.getId(), doubleParentProfit, MoneyFlowTypeEnum.DIRECT_BONUS.getValue(), orderInfo.getOrderNo()); |
| | | index++; |
| | | } |
| | | } |
| | | // ----- 直推奖 end ------- |
| | | |
| | | // =======隔代奖== start ===== |
| | | if (StrUtil.isBlank(parentMember.getReferrerId())) { |
| | | continue; |
| | | } |
| | | |
| | | List<String> parents = StrUtil.split(parentMember.getReferrerIds(), ','); |
| | | List<MallMember> members = adminMallMemberService.findParentMemberList(parents, null, true); |
| | | if (CollUtil.isEmpty(members)) { |
| | | return; |
| | | } |
| | | |
| | | BigDecimal direct = profit; |
| | | for (MallMember parent : members) { |
| | | if (parent.getInviteId().equals(parentMember.getInviteId())) { |
| | | continue; |
| | | } |
| | | |
| | | BigDecimal indrectDicProp = new BigDecimal(indrectDicPropDic.getValue()); |
| | | |
| | | // 隔代推荐奖 收益 |
| | | direct = direct.multiply(indrectDicProp.divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP)); |
| | | |
| | | // direct 收益小于1,则跳出 |
| | | if (direct.compareTo(BigDecimal.valueOf(0.01)) < 1) { |
| | | break; |
| | | } |
| | | int reduceResult = walletService.reduce(direct, parent.getId(), "score"); |
| | | if (reduceResult == 2) { |
| | | continue; |
| | | } |
| | | |
| | | walletService.add(direct, parent.getId(), "balance"); |
| | | moneyFlowService.addMoneyFlow(parent.getId(), direct, MoneyFlowTypeEnum.RECOMMEND_BONUS.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.BALANCE.getValue()); |
| | | moneyFlowService.addMoneyFlow(parent.getId(), direct.negate(), MoneyFlowTypeEnum.RECOMMEND_BONUS.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.SCORE.getValue()); |
| | | } |
| | | // =======隔代奖== end ===== |
| | | } |
| | | } |
| | | |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void agentProfit(Integer type) { |
| | | log.info("#####==代理分红==start==#####"); |
| | | log.info("#####==代理分红--{}==start==#####", type); |
| | | if (type == null) { |
| | | return; |
| | | } |
| | |
| | | BigDecimal hundred = BigDecimal.valueOf(100); |
| | | DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.AGENT_BONUS_RELEASE.getType(), DataDictionaryEnum.AGENT_BONUS_RELEASE.getCode()); |
| | | if (dic == null || StrUtil.isBlank(dic.getValue()) || Integer.parseInt(dic.getValue()) == 0) { |
| | | log.info("不进行全网分红"); |
| | | log.info("不进行代理分红"); |
| | | return; |
| | | } |
| | | |
| | | // 全网分红 |
| | | BigDecimal waitToBonus = systemSetting.getAllBonus().multiply(new BigDecimal(dic.getValue()).divide(hundred, 2, RoundingMode.HALF_UP)); |
| | | BigDecimal bonus = BigDecimal.ZERO; |
| | | if (AccountLevelEnums.VVIP.getLevel().equals(type)) { |
| | | bonus = systemSetting.getAgentBonus(); |
| | | } else { |
| | | bonus = systemSetting.getPartnerBonus(); |
| | | } |
| | | |
| | | // 代理 |
| | | BigDecimal waitToBonus = bonus.multiply(new BigDecimal(dic.getValue()).divide(hundred, 2, RoundingMode.HALF_UP)); |
| | | |
| | | // 代理 |
| | | List<MallMember> mallMembers = mallMemberMapper.selectAgentOrPartnetMemberList(type); |
| | |
| | | changeScoreAndCommission(item.getId(), perBonus, type.equals(AccountLevelEnums.VVIP.getLevel()) ? MoneyFlowTypeEnum.AGENT_BONUS.getValue() : MoneyFlowTypeEnum.PARTNER_BONUS.getValue(), null); |
| | | }); |
| | | |
| | | log.info("#####==代理分红==end==#####"); |
| | | if (type.equals(AccountLevelEnums.VVIP.getLevel())) { |
| | | changeSystemBonus(null, waitToBonus.negate(),null); |
| | | } else { |
| | | changeSystemBonus(null, null, waitToBonus.negate()); |
| | | } |
| | | |
| | | bonusRecord(waitToBonus, type, null, null); |
| | | log.info("#####==代理分红 -- {}==end==#####", type); |
| | | } |
| | | |
| | | private BigDecimal changeScoreAndCommission(Long memberId, BigDecimal income, Integer type, String orderNo) { |
| | |
| | | } |
| | | |
| | | income = map.get("amount"); |
| | | walletService.add(income, memberId, "commission"); |
| | | moneyFlowService.addMoneyFlow(memberId, income, type, orderNo, FlowTypeEnum.COMMISSION.getValue()); |
| | | walletService.add(income, memberId, "balance"); |
| | | moneyFlowService.addMoneyFlow(memberId, income, type, orderNo, FlowTypeEnum.BALANCE.getValue()); |
| | | moneyFlowService.addMoneyFlow(memberId, income.negate(), type, orderNo, FlowTypeEnum.SCORE.getValue()); |
| | | return income; |
| | | } |
| | |
| | | Map<String, List<MallMember>> levelMemberMap = new HashMap<>(); |
| | | // TODO 并发处理 |
| | | mallMembers.forEach(item -> { |
| | | List<MallMember> memberList = levelMemberMap.get(item.getLevel()); |
| | | String level = item.getLevel(); |
| | | |
| | | // 全网分红 -- 代理和股东当作是女王殿下级别 |
| | | if (MemberLevelEnum.getLevelCode(level) > MemberLevelEnum.FOUR_LEVEL.getCode()) { |
| | | level = MemberLevelEnum.FOUR_LEVEL.getType(); |
| | | } |
| | | |
| | | List<MallMember> memberList = levelMemberMap.get(level); |
| | | if (CollUtil.isEmpty(memberList)) { |
| | | memberList = new ArrayList<>(); |
| | | } |
| | | |
| | | memberList.add(item); |
| | | levelMemberMap.put(item.getLevel(), memberList); |
| | | levelMemberMap.put(level, memberList); |
| | | }); |
| | | |
| | | if (levelMemberMap.isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | BigDecimal totalBonus = BigDecimal.ZERO; |
| | | for (Map.Entry<String, List<MallMember>> entry : levelMemberMap.entrySet()) { |
| | | BigDecimal levelBonus = levelBonusMap.get(entry.getKey()); |
| | | List<MallMember> memberList = entry.getValue(); |
| | |
| | | memberList.forEach(item -> { |
| | | changeScoreAndCommission(item.getId(), perBonus, MoneyFlowTypeEnum.ALL_INTERNET_BONUS.getValue(), null); |
| | | }); |
| | | |
| | | totalBonus = totalBonus.add(levelBonus); |
| | | } |
| | | |
| | | changeSystemBonus(totalBonus.negate(), null, null); |
| | | |
| | | bonusRecord(totalBonus, 1, null, null); |
| | | log.info("###全网分红执行--end--###"); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void orderBonus(Long orderId) { |
| | | log.info("###订单分红消息开始执行###"); |
| | | MallOrderInfo orderInfo = mallOrderInfoMapper.selectById(orderId); |
| | | if (orderInfo.getOrderType() == 2) { |
| | | log.info("积分订单无返利"); |
| | | return; |
| | | } |
| | | |
| | | List<MallOrderItem> items = mallOrderInfoMapper.getMallOrderItemByOrderId(orderId); |
| | | |
| | | if (CollUtil.isEmpty(items)) { |
| | | return; |
| | | } |
| | | |
| | | |
| | | log.info("###全网分红执行--end--###"); |
| | | DataDictionaryCustom allInternetBonusDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.ALL_INTERNET_BONUS.getType(), DataDictionaryEnum.ALL_INTERNET_BONUS.getCode()); |
| | | DataDictionaryCustom agentBonusDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.AGENT_ALL_BONUS.getType(), DataDictionaryEnum.AGENT_ALL_BONUS.getCode()); |
| | | DataDictionaryCustom partnerBonusDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.PARNER_ALL_BONUS.getType(), DataDictionaryEnum.PARNER_ALL_BONUS.getCode()); |
| | | |
| | | String id = RandomUtil.randomNumbers(16); |
| | | |
| | | // 全网分红 |
| | | BigDecimal totalBonus = BigDecimal.ZERO; |
| | | |
| | | // 代理分红 |
| | | BigDecimal agentTotalBonus = BigDecimal.ZERO; |
| | | // 股东分红 |
| | | BigDecimal partnerTotalBonus = BigDecimal.ZERO; |
| | | BigDecimal hundred = new BigDecimal(100); |
| | | for (MallOrderItem item : items) { |
| | | // 减去成本后算收益 |
| | | BigDecimal amount = item.getPrice().subtract(item.getCostPrice()).multiply(BigDecimal.valueOf(item.getCnt())).subtract(orderInfo.getScoreAmount()); |
| | | if (amount.compareTo(BigDecimal.ZERO) < 1) { |
| | | continue; |
| | | } |
| | | |
| | | if (item.getIsNormal() == 1) { |
| | | BigDecimal bonus = amount.multiply(BigDecimal.valueOf(item.getNormalBonus()).divide(hundred, 2, RoundingMode.HALF_UP)); |
| | | log.info("{}-普通商品分红:{}, 明细ID:{}", id, bonus, item.getId()); |
| | | |
| | | bonusRecord(bonus, 1, item.getOrderId(), item.getId()); |
| | | totalBonus = totalBonus.add(bonus); |
| | | } else { |
| | | BigDecimal bonus = amount.multiply(new BigDecimal(allInternetBonusDic.getValue()).divide(hundred, 2, RoundingMode.HALF_UP)); |
| | | bonusRecord(bonus, 1, item.getOrderId(), item.getId()); |
| | | log.info("{}-套餐全网分红:{}, 明细ID:{}", id, bonus, item.getId()); |
| | | |
| | | totalBonus = totalBonus.add(bonus); |
| | | |
| | | BigDecimal agentBonus = amount.multiply(new BigDecimal(agentBonusDic.getValue()).divide(hundred, 2, RoundingMode.HALF_UP)); |
| | | bonusRecord(agentBonus, 2, item.getOrderId(), item.getId()); |
| | | log.info("{}-套餐代理分红:{}, 明细ID:{}", id, agentBonus, item.getId()); |
| | | |
| | | BigDecimal partnerBonus = amount.multiply(new BigDecimal(partnerBonusDic.getValue()).divide(hundred, 2, RoundingMode.HALF_UP)); |
| | | bonusRecord(partnerBonus, 3, item.getOrderId(), item.getId()); |
| | | log.info("{}-套餐股东分红:{}, 明细ID:{}", id, partnerBonus, item.getId()); |
| | | |
| | | agentTotalBonus = agentTotalBonus.add(agentBonus); |
| | | partnerTotalBonus = partnerTotalBonus.add(partnerBonus); |
| | | } |
| | | } |
| | | |
| | | changeSystemBonus(totalBonus, agentTotalBonus, partnerTotalBonus); |
| | | log.info("###订单分红消息结束执行###"); |
| | | } |
| | | |
| | | private void changeSystemBonus(BigDecimal totalBonus, BigDecimal agentTotalBonus, BigDecimal partnerBonus) { |
| | | boolean flag = false; |
| | | int index = 0; |
| | | while (!flag) { |
| | | MallSystemSetting setting = mallSystemSettingMapper.selectById(1L); |
| | | if (setting == null) { |
| | | return; |
| | | } |
| | | |
| | | LambdaQueryWrapper<MallSystemSetting> update = new LambdaQueryWrapper<>(); |
| | | update.eq(MallSystemSetting::getId, setting.getId()) |
| | | .eq(MallSystemSetting::getRevision, setting.getRevision()); |
| | | |
| | | setting.setAllBonus(setting.getAllBonus().add(totalBonus == null ? BigDecimal.ZERO : totalBonus)); |
| | | setting.setAgentBonus(setting.getAgentBonus().add(agentTotalBonus == null ? BigDecimal.ZERO : agentTotalBonus)); |
| | | setting.setPartnerBonus(setting.getPartnerBonus().add(partnerBonus == null ? BigDecimal.ZERO : partnerBonus)); |
| | | setting.setRevision(setting.getRevision() + 1); |
| | | int i = mallSystemSettingMapper.update(setting, update); |
| | | if (i > 0 || index > 2) { |
| | | flag = true; |
| | | } |
| | | index++; |
| | | } |
| | | } |
| | | |
| | | private final MallBonusRecordMapper mallBonusRecordMapper; |
| | | |
| | | private void bonusRecord(BigDecimal amount, Integer type, Long orderId, Long orderItemId) { |
| | | MallBonusRecord record = new MallBonusRecord(); |
| | | record.setAmount(amount); |
| | | record.setCreateTime(new Date()); |
| | | record.setType(type); |
| | | record.setOrderId(orderId); |
| | | record.setOrderItemId(orderItemId); |
| | | mallBonusRecordMapper.insert(record); |
| | | } |
| | | } |