| | |
| | | package cc.mrbird.febs.mall.service.impl; |
| | | |
| | | import cc.mrbird.febs.common.enumerates.AgentLevelEnum; |
| | | import cc.mrbird.febs.common.enumerates.DataDictionaryEnum; |
| | | import cc.mrbird.febs.common.enumerates.FlowTypeEnum; |
| | | import cc.mrbird.febs.common.enumerates.MoneyFlowTypeEnum; |
| | | import cc.mrbird.febs.common.utils.AppContants; |
| | | import cc.mrbird.febs.common.enumerates.*; |
| | | import cc.mrbird.febs.common.utils.RedisUtils; |
| | | import cc.mrbird.febs.mall.entity.*; |
| | | import cc.mrbird.febs.mall.mapper.*; |
| | | import cc.mrbird.febs.mall.service.IApiMallMemberWalletService; |
| | | import cc.mrbird.febs.mall.service.IMallMoneyFlowService; |
| | | import cc.mrbird.febs.mall.service.IMemberProfitService; |
| | | import cc.mrbird.febs.mall.service.*; |
| | | import cc.mrbird.febs.rabbit.producter.AgentProducer; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.date.DateField; |
| | | import cn.hutool.core.date.DateUnit; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import cn.hutool.core.util.RandomUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import lombok.Data; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * @author wzy |
| | |
| | | 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 MallSystemSettingMapper mallSystemSettingMapper; |
| | | |
| | | /** |
| | | * 直推收益 |
| | | * |
| | | * 1、直推收益 1:20;2:30;3:40 返利,隔代奖拿直推收益20% |
| | | * 2、若非代理推代理,只拿10%,往上找代理给15%,再往上找代理给15%,往上找连续两层。股东套餐同理 |
| | | * 3、代理推代理按照第1点结算 |
| | | * |
| | | * @param orderId |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void staticProfit() { |
| | | log.info("#####静态分红开始运行:{}#####", new Date()); |
| | | List<MallMember> members = mallMemberMapper.selectList(null); |
| | | if (CollUtil.isEmpty(members)) { |
| | | public void directProfit(Long orderId) { |
| | | log.info("######直推奖励, 订单ID:{}######", orderId); |
| | | MallOrderInfo orderInfo = mallOrderInfoMapper.selectById(orderId); |
| | | if (orderInfo.getOrderType() == 2) { |
| | | log.info("积分订单无返利"); |
| | | return; |
| | | } |
| | | BigDecimal indirectPer = BigDecimal.valueOf(0.25); |
| | | BigDecimal indirectPer2 = BigDecimal.valueOf(0.15); |
| | | |
| | | MallMember member = mallMemberMapper.selectById(orderInfo.getMemberId()); |
| | | |
| | | // 父级会员 |
| | | MallMember parentMember = mallMemberMapper.selectInfoByInviteId(member.getReferrerId()); |
| | | if (parentMember == null) { |
| | | return; |
| | | } |
| | | |
| | | Date profitDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1); |
| | | BigDecimal totalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D"); |
| | | if (totalIncome.compareTo(BigDecimal.ZERO) == 0) { |
| | | if (parentMember.getAccountLevel() == 0 || MemberLevelEnum.ZERO_LEVEL.getType().equals(parentMember.getLevel())) { |
| | | log.info("上级:{}未购买会员套餐,无返利", parentMember.getInviteId()); |
| | | return; |
| | | } |
| | | |
| | | DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.STATIC_BONUS.getType(), DataDictionaryEnum.STATIC_BONUS.getCode()); |
| | | BigDecimal perProfit = totalIncome.divide(new BigDecimal(dic.getValue()), 2, RoundingMode.HALF_UP).divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP); |
| | | // 父级会员直推人数 |
| | | Integer directCnt = mallMemberMapper.selectOwnCntByInviteIdAndAccountLevel(parentMember.getInviteId(), parentMember.getAccountLevel()); |
| | | List<DataDictionaryCustom> dataDices = dataDictionaryCustomMapper.selectDicByType(DataDictionaryEnum.DIRECT_BONUS_SETTING.getType()); |
| | | |
| | | for (MallMember member : members) { |
| | | List<MallGoods> goodsList = mallGoodsMapper.selectOrderGoodsList(member.getId(), profitDate); |
| | | directCnt = directCnt == null ? 0 : directCnt; |
| | | List<MallOrderItem> items = mallOrderInfoMapper.getMallOrderItemByOrderId(orderId); |
| | | |
| | | if (CollUtil.isEmpty(goodsList)) { |
| | | if (CollUtil.isEmpty(items)) { |
| | | return; |
| | | } |
| | | |
| | | for (MallOrderItem item : items) { |
| | | // 减去成本后算收益 |
| | | BigDecimal amount = item.getPrice().subtract(item.getCostPrice()).multiply(BigDecimal.valueOf(item.getCnt())); |
| | | if (amount.compareTo(BigDecimal.ZERO) < 1) { |
| | | continue; |
| | | } |
| | | |
| | | for (MallGoods goods : goodsList) { |
| | | BigDecimal goodsProfit = goods.getStaticProp().multiply(perProfit); |
| | | // 直推返利比例 |
| | | BigDecimal profitPer = BigDecimal.ZERO; |
| | | int isSameLevel = 0; |
| | | // 普通商品 -- 直推上级可拿百分比直推奖励 |
| | | if (item.getIsNormal() == 1) { |
| | | profitPer = new BigDecimal(item.getNormalPer()); |
| | | |
| | | int reduce = walletService.reduce(goodsProfit, member.getId(), "score"); |
| | | if (reduce == 2) { |
| | | break; |
| | | // 套餐商品 |
| | | } else { |
| | | |
| | | // 判断上级是否与自己购买的套餐符合,若符合则走3级直推逻辑,若不符合则另外一个 |
| | | if (parentMember.getAccountLevel().equals(item.getGoodsLevel()) || AccountLevelEnums.VIP.getLevel().equals(parentMember.getAccountLevel())) { |
| | | |
| | | for (DataDictionaryCustom dataDic : dataDices) { |
| | | JSONObject jsonObject = JSONObject.parseObject(dataDic.getValue()); |
| | | if (directCnt >= jsonObject.getInteger("pushCnt")) { |
| | | profitPer = jsonObject.getBigDecimal("prop"); |
| | | } |
| | | } |
| | | isSameLevel = 1; |
| | | // 非代理推代理/非股东推股东 |
| | | } else { |
| | | profitPer = BigDecimal.valueOf(10); |
| | | isSameLevel = 2; |
| | | } |
| | | } |
| | | |
| | | // 直推奖 |
| | | BigDecimal profit = amount.multiply(profitPer.divide(new BigDecimal(100), 2, RoundingMode.HALF_UP)); |
| | | changeScoreAndCommission(parentMember.getId(), profit, MoneyFlowTypeEnum.DIRECT_BONUS.getValue(), orderInfo.getOrderNo()); |
| | | |
| | | // 代理推代理 |
| | | if (isSameLevel == 1) { |
| | | MallMember doubleParentMember = mallMemberMapper.selectInfoByInviteId(parentMember.getReferrerId()); |
| | | if (doubleParentMember == null) { |
| | | continue; |
| | | } |
| | | |
| | | walletService.add(goodsProfit, member.getId(), "commission"); |
| | | moneyFlowService.addMoneyFlow(member.getId(), goodsProfit, MoneyFlowTypeEnum.STATIC_BONUS.getValue(), goods.getOrderNo(), FlowTypeEnum.COMMISSION.getValue()); |
| | | moneyFlowService.addMoneyFlow(member.getId(), goodsProfit.negate(), MoneyFlowTypeEnum.STATIC_BONUS.getValue(), goods.getOrderNo(), FlowTypeEnum.SCORE.getValue()); |
| | | BigDecimal doubleParentProfit = profit.multiply(indirectPer); |
| | | changeScoreAndCommission(doubleParentMember.getId(), doubleParentProfit, MoneyFlowTypeEnum.DIRECT_BONUS.getValue(), orderInfo.getOrderNo()); |
| | | } |
| | | |
| | | // 非代理推代理 |
| | | if (isSameLevel == 2) { |
| | | if (StrUtil.isBlank(parentMember.getReferrerIds())) { |
| | | continue; |
| | | } |
| | | |
| | | List<MallMember> mallMembers = mallMemberMapper.selectParentMemberList(StrUtil.split(parentMember.getReferrerIds(), ','), parentMember.getReferrerId(), 2); |
| | | if (CollUtil.isEmpty(mallMembers)) { |
| | | continue; |
| | | } |
| | | |
| | | for (MallMember mallMember : mallMembers) { |
| | | if (!item.getGoodsLevel().equals(mallMember.getAccountLevel())) { |
| | | break; |
| | | } |
| | | |
| | | BigDecimal doubleParentProfit = profit.multiply(indirectPer2); |
| | | changeScoreAndCommission(mallMember.getId(), doubleParentProfit, MoneyFlowTypeEnum.DIRECT_BONUS.getValue(), orderInfo.getOrderNo()); |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void dynamicProfit(Long orderId) { |
| | | log.info("######直推奖励, 订单ID:{}######", orderId); |
| | | public void dynamicProfit(Long orderId, Integer isNormal) { |
| | | MallOrderInfo orderInfo = mallOrderInfoMapper.selectById(orderId); |
| | | if (orderInfo.getOrderType() == 2) { |
| | | log.info("积分订单无返利"); |
| | |
| | | if (StrUtil.isBlank(member.getReferrerId())) { |
| | | return; |
| | | } |
| | | |
| | | // 直推奖励字典 |
| | | DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.DYNAMIC_BONUS.getType(), DataDictionaryEnum.DYNAMIC_BONUS.getCode()); |
| | | // 隔代字典 |
| | | DataDictionaryCustom indrectDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.INDRECT_BONUS.getType(), DataDictionaryEnum.INDRECT_BONUS.getCode()); |
| | | |
| | | // 直接父级 |
| | | MallMember parent = mallMemberMapper.selectInfoByInviteId(member.getReferrerId()); |
| | | |
| | | List<MallOrderItem> items = mallOrderInfoMapper.getMallOrderItemByOrderId(orderId); |
| | | for (MallOrderItem item : items) { |
| | | item.setHasSettle(1); |
| | | mallOrderItemMapper.updateById(item); |
| | | |
| | | // 减去成本后算收益 |
| | | BigDecimal amount = item.getPrice().subtract(item.getCostPrice()).multiply(BigDecimal.valueOf(item.getCnt())); |
| | | if (amount.compareTo(BigDecimal.ZERO) < 1) { |
| | | continue; |
| | | } |
| | | |
| | | // 判断套餐或者普通商品,结算对应商品的动态分红 |
| | | // if (!Objects.equals(item.getIsNormal(), isNormal)) { |
| | | // continue; |
| | | // } |
| | | |
| | | // =======直推返利== start ===== |
| | | // 直接奖励收益 |
| | | BigDecimal dynamicProfit = amount.divide(new BigDecimal(dic.getValue()), 2, RoundingMode.HALF_UP); |
| | | BigDecimal dynamicProfit = amount.multiply(new BigDecimal(dic.getValue()).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP)); |
| | | |
| | | if (dynamicProfit.compareTo(BigDecimal.ZERO) < 1) { |
| | | continue; |
| | | } |
| | | |
| | | int reduce = walletService.reduce(dynamicProfit, parent.getId(), "score"); |
| | | if (reduce == 2) { |
| | | // int reduce = walletService.reduce(dynamicProfit, parent.getId(), "score"); |
| | | // if (reduce == 2) { |
| | | // continue; |
| | | // } |
| | | // walletService.add(dynamicProfit, parent.getId(), "commission"); |
| | | // moneyFlowService.addMoneyFlow(parent.getId(), dynamicProfit, MoneyFlowTypeEnum.DYNAMIC_ACHIEVE.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.COMMISSION.getValue()); |
| | | // moneyFlowService.addMoneyFlow(parent.getId(), dynamicProfit.negate(), MoneyFlowTypeEnum.DYNAMIC_ACHIEVE.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.SCORE.getValue()); |
| | | |
| | | // dynamicProfit = changeScoreAndCommission(parent.getId(), dynamicProfit, MoneyFlowTypeEnum.DYNAMIC_ACHIEVE.getValue(), orderInfo.getOrderNo()); |
| | | if (dynamicProfit.compareTo(BigDecimal.ZERO) < 1) { |
| | | continue; |
| | | } |
| | | |
| | | walletService.add(dynamicProfit, parent.getId(), "commission"); |
| | | |
| | | moneyFlowService.addMoneyFlow(parent.getId(), dynamicProfit, MoneyFlowTypeEnum.DYNAMIC_ACHIEVE.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.COMMISSION.getValue()); |
| | | moneyFlowService.addMoneyFlow(parent.getId(), dynamicProfit.negate(), MoneyFlowTypeEnum.DYNAMIC_ACHIEVE.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.SCORE.getValue()); |
| | | // =======直推返利== end ===== |
| | | |
| | | // =======隔代奖== start ===== |
| | |
| | | if (CollUtil.isEmpty(members)) { |
| | | return; |
| | | } |
| | | // 隔代比例 |
| | | BigDecimal indrectDicProp = new BigDecimal(indrectDic.getValue()); |
| | | |
| | | // 隔代推荐奖 收益 |
| | | BigDecimal direct = dynamicProfit.divide(indrectDicProp, 2, RoundingMode.HALF_UP); |
| | | |
| | | // direct 收益小于1,则跳出 |
| | | if (direct.compareTo(BigDecimal.ONE) < 1) { |
| | | continue; |
| | | } |
| | | |
| | | BigDecimal direct = dynamicProfit; |
| | | for (MallMember parentMember : members) { |
| | | if (parent.getInviteId().equals(parentMember.getInviteId())) { |
| | | continue; |
| | | } |
| | | |
| | | int reduceResult = walletService.reduce(direct, parentMember.getId(), "score"); |
| | | if (reduceResult == 2) { |
| | | continue; |
| | | // 直推数量 |
| | | Integer directCnt = mallMemberMapper.selectOwnCntByInviteId(parentMember.getInviteId()); |
| | | List<DataDictionaryCustom> dataDices = dataDictionaryCustomMapper.selectDicByType(DataDictionaryEnum.INDIRECT_BONUS_SETTING.getType()); |
| | | |
| | | directCnt = directCnt == null ? 0 : directCnt; |
| | | |
| | | // 隔代比例 |
| | | BigDecimal indrectDicProp = BigDecimal.ZERO; |
| | | for (DataDictionaryCustom dataDic : dataDices) { |
| | | JSONObject jsonObject = JSONObject.parseObject(dataDic.getValue()); |
| | | if (directCnt >= jsonObject.getInteger("pushCnt")) { |
| | | indrectDicProp = jsonObject.getBigDecimal("prop"); |
| | | } |
| | | } |
| | | |
| | | walletService.add(direct, parentMember.getId(), "commission"); |
| | | moneyFlowService.addMoneyFlow(parentMember.getId(), direct, MoneyFlowTypeEnum.RECOMMEND_BONUS.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.COMMISSION.getValue()); |
| | | moneyFlowService.addMoneyFlow(parentMember.getId(), direct.negate(), MoneyFlowTypeEnum.RECOMMEND_BONUS.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.SCORE.getValue()); |
| | | // 隔代推荐奖 收益 |
| | | direct = direct.multiply(indrectDicProp.divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP)); |
| | | |
| | | direct = direct.divide(indrectDicProp, 2, RoundingMode.HALF_UP); |
| | | // direct 收益小于1,则跳出 |
| | | if (direct.compareTo(BigDecimal.ONE) < 1) { |
| | | break; |
| | | } |
| | | // int reduceResult = walletService.reduce(direct, parentMember.getId(), "score"); |
| | | // if (reduceResult == 2) { |
| | | // continue; |
| | | // } |
| | | // |
| | | // walletService.add(direct, parentMember.getId(), "commission"); |
| | | // moneyFlowService.addMoneyFlow(parentMember.getId(), direct, MoneyFlowTypeEnum.RECOMMEND_BONUS.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.COMMISSION.getValue()); |
| | | // moneyFlowService.addMoneyFlow(parentMember.getId(), direct.negate(), MoneyFlowTypeEnum.RECOMMEND_BONUS.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.SCORE.getValue()); |
| | | // changeScoreAndCommission(parentMember.getId(), direct, MoneyFlowTypeEnum.RECOMMEND_BONUS.getValue(), orderInfo.getOrderNo()); |
| | | } |
| | | // =======隔代奖== end ===== |
| | | } |
| | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void agentProfit(Date profitDate) { |
| | | public void agentProfit(Integer type) { |
| | | log.info("#####==代理分红==start==#####"); |
| | | if (profitDate == null) { |
| | | profitDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1); |
| | | if (type == null) { |
| | | return; |
| | | } |
| | | BigDecimal totalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D"); |
| | | |
| | | |
| | | DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.AGENT_BONUS.getType(), DataDictionaryEnum.AGENT_BONUS.getCode()); |
| | | BigDecimal profit = totalIncome.divide(new BigDecimal(dic.getValue()), 2, RoundingMode.HALF_UP); |
| | | BigDecimal preProfit = profit.divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP); |
| | | |
| | | List<MallMember> agentMembers = mallMemberMapper.selectByIdAndNoLevel(null, AgentLevelEnum.FIRST_LEVEL.name()); |
| | | if (CollUtil.isEmpty(agentMembers)) { |
| | | |
| | | MallSystemSetting systemSetting = mallSystemSettingMapper.selectById(1L); |
| | | if (systemSetting == null) { |
| | | log.info("没有系统配置"); |
| | | return; |
| | | } |
| | | |
| | | for (MallMember agentMember : agentMembers) { |
| | | DataDictionaryCustom agentDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(AppContants.AGENT_LEVEL_REQUIRE, agentMember.getLevel()); |
| | | |
| | | if (agentDic == null) { |
| | | continue; |
| | | } |
| | | |
| | | JSONObject jsonObject = JSONObject.parseObject(agentDic.getValue()); |
| | | BigDecimal profitProp = jsonObject.getBigDecimal("profitProp"); |
| | | BigDecimal income = preProfit.multiply(profitProp); |
| | | |
| | | int reduce = walletService.reduce(income, agentMember.getId(), "score"); |
| | | if (reduce == 2) { |
| | | continue; |
| | | } |
| | | |
| | | walletService.add(income, agentMember.getId(), "commission"); |
| | | |
| | | moneyFlowService.addMoneyFlow(agentMember.getId(), income, MoneyFlowTypeEnum.AGENT_BONUS.getValue(), null, FlowTypeEnum.COMMISSION.getValue()); |
| | | moneyFlowService.addMoneyFlow(agentMember.getId(), income.negate(), MoneyFlowTypeEnum.AGENT_BONUS.getValue(), null, FlowTypeEnum.SCORE.getValue()); |
| | | 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("不进行全网分红"); |
| | | return; |
| | | } |
| | | |
| | | // 全网分红 |
| | | BigDecimal waitToBonus = systemSetting.getAllBonus().multiply(new BigDecimal(dic.getValue()).divide(hundred, 2, RoundingMode.HALF_UP)); |
| | | |
| | | // 代理 |
| | | List<MallMember> mallMembers = mallMemberMapper.selectAgentOrPartnetMemberList(type); |
| | | if (CollUtil.isEmpty(mallMembers) || waitToBonus.compareTo(BigDecimal.ZERO) < 1) { |
| | | log.info("待分红金额不足或会员不足"); |
| | | return; |
| | | } |
| | | |
| | | BigDecimal perBonus = waitToBonus.divide(new BigDecimal(mallMembers.size()), 2, RoundingMode.HALF_UP); |
| | | mallMembers.forEach(item -> { |
| | | changeScoreAndCommission(item.getId(), perBonus, type.equals(AccountLevelEnums.VVIP.getLevel()) ? MoneyFlowTypeEnum.AGENT_BONUS.getValue() : MoneyFlowTypeEnum.PARTNER_BONUS.getValue(), null); |
| | | }); |
| | | |
| | | log.info("#####==代理分红==end==#####"); |
| | | } |
| | | |
| | | @Override |
| | | public void rankProfit() { |
| | | log.info("######==排名分红==####"); |
| | | Page<MallMember> page = new Page<>(1, 5); |
| | | MallMember query = new MallMember(); |
| | | query.setQuery("2"); |
| | | Date profitDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1); |
| | | query.setCreatedTime(profitDate); |
| | | IPage<MallMember> rankListInPage = mallMemberMapper.selectRankListInPage(page, query); |
| | | private BigDecimal changeScoreAndCommission(Long memberId, BigDecimal income, Integer type, String orderNo) { |
| | | Map<String, BigDecimal> map = new HashMap<>(); |
| | | map.put("amount", income); |
| | | |
| | | List<MallMember> rankList = rankListInPage.getRecords(); |
| | | if (CollUtil.isEmpty(rankList)) { |
| | | return; |
| | | int reduce = walletService.reduce(income, memberId, "score", map); |
| | | if (reduce == 2) { |
| | | return BigDecimal.ZERO; |
| | | } |
| | | |
| | | BigDecimal totalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "M"); |
| | | |
| | | List<DataDictionaryCustom> rankAward = dataDictionaryCustomMapper.selectDicByType("RANK_AWARD"); |
| | | DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.RANK_BONUS.getType(), DataDictionaryEnum.RANK_BONUS.getCode()); |
| | | |
| | | |
| | | BigDecimal rankBonusTotal = new BigDecimal(dic.getValue()).divide(totalIncome, 2, RoundingMode.HALF_UP); |
| | | BigDecimal preBonus = rankBonusTotal.divide(BigDecimal.valueOf(100),2 , RoundingMode.HALF_UP); |
| | | |
| | | int i = 0; |
| | | for (MallMember member : rankList) { |
| | | DataDictionaryCustom rank = rankAward.get(i); |
| | | i++; |
| | | BigDecimal bonus = preBonus.multiply(new BigDecimal(rank.getValue())); |
| | | int reduce = walletService.reduce(bonus, member.getId(), "score"); |
| | | if (reduce == 2) { |
| | | continue; |
| | | } |
| | | |
| | | walletService.add(bonus, member.getId(), "commission"); |
| | | |
| | | moneyFlowService.addMoneyFlow(member.getId(), bonus, MoneyFlowTypeEnum.RANK_BONUS.getValue(), null, FlowTypeEnum.COMMISSION.getValue()); |
| | | moneyFlowService.addMoneyFlow(member.getId(), bonus.negate(), MoneyFlowTypeEnum.RANK_BONUS.getValue(), null, FlowTypeEnum.SCORE.getValue()); |
| | | } |
| | | income = map.get("amount"); |
| | | walletService.add(income, memberId, "commission"); |
| | | moneyFlowService.addMoneyFlow(memberId, income, type, orderNo, FlowTypeEnum.COMMISSION.getValue()); |
| | | moneyFlowService.addMoneyFlow(memberId, income.negate(), type, orderNo, FlowTypeEnum.SCORE.getValue()); |
| | | return income; |
| | | } |
| | | |
| | | /** |
| | | * 拿推荐人收益(代理,直推,排名)的10%加权平分给下面直推 |
| | | */ |
| | | @Override |
| | | public void thankfulProfit() { |
| | | log.info("######==感恩奖==start==####"); |
| | | List<MallMember> mallMembers = mallMemberMapper.selectMemberListHasChild(); |
| | | if (CollUtil.isEmpty(mallMembers)) { |
| | | public void allInternetProfit() { |
| | | log.info("###全网分红执行--start--###"); |
| | | MallSystemSetting systemSetting = mallSystemSettingMapper.selectById(1L); |
| | | if (systemSetting == null) { |
| | | log.info("没有系统配置"); |
| | | return; |
| | | } |
| | | |
| | | DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.THANKFUL_BONUS.getType(), DataDictionaryEnum.THANKFUL_BONUS.getCode()); |
| | | // 感恩奖比例 |
| | | BigDecimal prop = new BigDecimal(dic.getValue()).divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP); |
| | | BigDecimal hundred = BigDecimal.valueOf(100); |
| | | DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.ALL_INTERNET_BONUS_RELEASE.getType(), DataDictionaryEnum.ALL_INTERNET_BONUS_RELEASE.getCode()); |
| | | if (dic == null || StrUtil.isBlank(dic.getValue()) || Integer.parseInt(dic.getValue()) == 0) { |
| | | log.info("不进行全网分红"); |
| | | return; |
| | | } |
| | | |
| | | Date date = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1); |
| | | for (MallMember mallMember : mallMembers) { |
| | | BigDecimal income = mallMoneyFlowMapper.selectThankfulCommission(date, mallMember.getId()); |
| | | List<MallMember> children = mallMemberMapper.selectMemberDirectForHasLevel(mallMember.getInviteId()); |
| | | if (CollUtil.isEmpty(children)) { |
| | | // 全网分红 |
| | | BigDecimal waitToBonus = systemSetting.getAllBonus().multiply(new BigDecimal(dic.getValue()).divide(hundred, 2, RoundingMode.HALF_UP)); |
| | | List<MallMember> mallMembers = mallMemberMapper.selectMemberAfterLevelList(MemberLevelEnum.FIRST_LEVEL.getType()); |
| | | if (CollUtil.isEmpty(mallMembers) || waitToBonus.compareTo(BigDecimal.ZERO) < 1) { |
| | | log.info("待分红金额不足或会员不足"); |
| | | return; |
| | | } |
| | | |
| | | List<DataDictionaryCustom> dicList = dataDictionaryCustomMapper.selectDicByType(DataDictionaryEnum.INTERNET_LEVEL_BONUS.getType()); |
| | | if (CollUtil.isEmpty(dicList)) { |
| | | return; |
| | | } |
| | | |
| | | Map<String, BigDecimal> levelBonusMap = new HashMap<>(); |
| | | dicList.forEach(item -> { |
| | | BigDecimal levelRatio = new BigDecimal(item.getValue()).divide(hundred, 2, RoundingMode.HALF_UP); |
| | | levelBonusMap.put(item.getCode(), waitToBonus.multiply(levelRatio)); |
| | | }); |
| | | |
| | | Map<String, List<MallMember>> levelMemberMap = new HashMap<>(); |
| | | // TODO 并发处理 |
| | | mallMembers.forEach(item -> { |
| | | List<MallMember> memberList = levelMemberMap.get(item.getLevel()); |
| | | if (CollUtil.isEmpty(memberList)) { |
| | | memberList = new ArrayList<>(); |
| | | } |
| | | |
| | | memberList.add(item); |
| | | levelMemberMap.put(item.getLevel(), memberList); |
| | | }); |
| | | |
| | | if (levelMemberMap.isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | for (Map.Entry<String, List<MallMember>> entry : levelMemberMap.entrySet()) { |
| | | BigDecimal levelBonus = levelBonusMap.get(entry.getKey()); |
| | | List<MallMember> memberList = entry.getValue(); |
| | | |
| | | if (CollUtil.isEmpty(memberList) || levelBonus.compareTo(BigDecimal.ZERO) < 1) { |
| | | continue; |
| | | } |
| | | |
| | | BigDecimal preProfit = income.multiply(prop).divide(BigDecimal.valueOf(children.size()), 2, RoundingMode.HALF_DOWN); |
| | | BigDecimal perBonus = levelBonus.divide(BigDecimal.valueOf(memberList.size()), 2, RoundingMode.HALF_UP); |
| | | memberList.forEach(item -> { |
| | | changeScoreAndCommission(item.getId(), perBonus, MoneyFlowTypeEnum.ALL_INTERNET_BONUS.getValue(), null); |
| | | }); |
| | | } |
| | | |
| | | |
| | | int reduce = walletService.reduce(preProfit.negate(), mallMember.getId(), "commission"); |
| | | if (reduce == 2) { |
| | | log.info("###全网分红执行--end--###"); |
| | | } |
| | | |
| | | @Override |
| | | 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; |
| | | } |
| | | |
| | | |
| | | 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 hundred = new BigDecimal(100); |
| | | for (MallOrderItem item : items) { |
| | | // 减去成本后算收益 |
| | | BigDecimal amount = item.getPrice().subtract(item.getCostPrice()).multiply(BigDecimal.valueOf(item.getCnt())); |
| | | if (amount.compareTo(BigDecimal.ZERO) < 1) { |
| | | continue; |
| | | } |
| | | |
| | | moneyFlowService.addMoneyFlow(mallMember.getId(), income.negate(), MoneyFlowTypeEnum.THANKFUL.getValue(), null, FlowTypeEnum.COMMISSION.getValue()); |
| | | if (item.getIsNormal() == 2) { |
| | | BigDecimal bonus = amount.multiply(BigDecimal.valueOf(item.getNormalBonus()).divide(hundred, 2, RoundingMode.HALF_UP)); |
| | | log.info("{}-普通商品分红:{}, 明细ID:{}", id, bonus, item.getId()); |
| | | |
| | | for (MallMember child : children) { |
| | | int reduce1 = walletService.reduce(preProfit.negate(), child.getId(), "score"); |
| | | if (reduce1 == 2) { |
| | | continue; |
| | | } |
| | | totalBonus = totalBonus.add(bonus); |
| | | } else { |
| | | BigDecimal bonus = amount.multiply(new BigDecimal(allInternetBonusDic.getValue()).divide(hundred, 2, RoundingMode.HALF_UP)); |
| | | log.info("{}-套餐全网分红:{}, 明细ID:{}", id, bonus, item.getId()); |
| | | |
| | | walletService.add(preProfit, child.getId(), "commission"); |
| | | moneyFlowService.addMoneyFlow(child.getId(), preProfit, MoneyFlowTypeEnum.THANKFUL.getValue(), null, mallMember.getId(), FlowTypeEnum.COMMISSION.getValue()); |
| | | moneyFlowService.addMoneyFlow(child.getId(), preProfit.negate(), MoneyFlowTypeEnum.THANKFUL.getValue(), null, mallMember.getId(), FlowTypeEnum.SCORE.getValue()); |
| | | totalBonus = totalBonus.add(bonus); |
| | | |
| | | |
| | | BigDecimal agentBonus = amount.multiply(new BigDecimal(agentBonusDic.getValue()).divide(hundred, 2, RoundingMode.HALF_UP)); |
| | | log.info("{}-套餐代理分红:{}, 明细ID:{}", id, agentBonus, item.getId()); |
| | | |
| | | BigDecimal partnerBonus = amount.multiply(new BigDecimal(partnerBonusDic.getValue()).divide(hundred, 2, RoundingMode.HALF_UP)); |
| | | log.info("{}-套餐股东分红:{}, 明细ID:{}", id, partnerBonus, item.getId()); |
| | | |
| | | agentTotalBonus = agentTotalBonus.add(agentBonus).add(partnerBonus); |
| | | } |
| | | } |
| | | log.info("######==感恩奖==end==####"); |
| | | |
| | | boolean flag = false; |
| | | |
| | | int index = 0; |
| | | while (!flag) { |
| | | MallSystemSetting setting = mallSystemSettingMapper.selectById(1L); |
| | | if (setting == null) { |
| | | return; |
| | | } |
| | | |
| | | setting.setAllBonus(setting.getAllBonus().add(totalBonus)); |
| | | setting.setAgentBonus(setting.getAgentBonus().add(agentTotalBonus)); |
| | | setting.setRevision(setting.getRevision() + 1); |
| | | |
| | | LambdaQueryWrapper<MallSystemSetting> update = new LambdaQueryWrapper<>(); |
| | | update.eq(MallSystemSetting::getId, setting.getId()) |
| | | .eq(MallSystemSetting::getRevision, setting.getRevision()); |
| | | int i = mallSystemSettingMapper.update(setting, update); |
| | | if (i > 0 || index > 2) { |
| | | flag = true; |
| | | } |
| | | index++; |
| | | } |
| | | |
| | | log.info("###订单分红消息结束执行###"); |
| | | } |
| | | } |