| | |
| | | package cc.mrbird.febs.mall.service.impl; |
| | | |
| | | 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.mall.entity.MallGoods; |
| | | import cc.mrbird.febs.mall.entity.MallMember; |
| | | import cc.mrbird.febs.mall.mapper.MallGoodsMapper; |
| | | import cc.mrbird.febs.mall.mapper.MallMemberMapper; |
| | | import cc.mrbird.febs.mall.mapper.MallOrderInfoMapper; |
| | | import cc.mrbird.febs.mall.mapper.MallOrderItemMapper; |
| | | 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 cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.date.DateField; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | private final MallGoodsMapper mallGoodsMapper; |
| | | private final IApiMallMemberWalletService walletService; |
| | | private final IMallMoneyFlowService moneyFlowService; |
| | | private final DataDictionaryCustomMapper dataDictionaryCustomMapper; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | |
| | | if (totalIncome.compareTo(BigDecimal.ZERO) == 0) { |
| | | return; |
| | | } |
| | | BigDecimal perProfit = totalIncome.divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP); |
| | | |
| | | 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); |
| | | |
| | | for (MallMember member : members) { |
| | | List<MallGoods> goodsList = mallGoodsMapper.selectOrderGoodsList(member.getId(), profitDate); |
| | |
| | | for (MallGoods goods : goodsList) { |
| | | BigDecimal goodsProfit = goods.getStaticProp().multiply(perProfit); |
| | | |
| | | walletService.add(goodsProfit, member.getId(), "score", "total_score"); |
| | | moneyFlowService.addMoneyFlow(member.getId(), goodsProfit, MoneyFlowTypeEnum.STATIC_BONUS.getValue(), goods.getOrderNo(), FlowTypeEnum.SCORE.getValue()); |
| | | try { |
| | | walletService.reduce(goodsProfit, member.getId(), "score"); |
| | | } catch (Exception e) { |
| | | log.info("减少积分失败异常:{}, {}", goodsProfit, member.getId()); |
| | | break; |
| | | } |
| | | |
| | | 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()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void dynamicProfit(Long orderId) { |
| | | log.info("######直推奖励, 订单ID:{}######", orderId); |
| | | MallOrderInfo orderInfo = mallOrderInfoMapper.selectById(orderId); |
| | | MallMember member = mallMemberMapper.selectById(orderInfo.getMemberId()); |
| | | |
| | | 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()); |
| | | |
| | | String parent = member.getReferrerId(); |
| | | List<MallOrderItem> items = mallOrderInfoMapper.getMallOrderItemByOrderId(orderId); |
| | | for (MallOrderItem item : items) { |
| | | BigDecimal dynamicProfit = item.getAmount().multiply(BigDecimal.valueOf(item.getCnt())).divide(new BigDecimal(dic.getValue()), 2, RoundingMode.HALF_UP); |
| | | |
| | | List<String> parents = StrUtil.split(member.getReferrerIds(), ','); |
| | | List<MallMember> members = mallMemberMapper.selectByInviteIds(parents); |
| | | if (CollUtil.isEmpty(members)) { |
| | | return; |
| | | } |
| | | |
| | | BigDecimal indrectDicProp = new BigDecimal(indrectDic.getValue()); |
| | | BigDecimal direct = dynamicProfit.divide(indrectDicProp, 2, RoundingMode.HALF_UP); |
| | | for (MallMember parentMember : members) { |
| | | if (parent.equals(parentMember.getInviteId())) { |
| | | continue; |
| | | } |
| | | |
| | | try { |
| | | walletService.reduce(direct, parentMember.getId(), "score"); |
| | | } catch (Exception e) { |
| | | log.info("减少积分失败异常:{}, {}", direct, parentMember.getId()); |
| | | 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()); |
| | | direct = direct.divide(indrectDicProp, 2, RoundingMode.HALF_UP); |
| | | } |
| | | |
| | | try { |
| | | walletService.reduce(dynamicProfit, member.getId(), "score"); |
| | | } catch (Exception e) { |
| | | log.info("减少积分失败异常:{}, {}", dynamicProfit, member.getId()); |
| | | continue; |
| | | } |
| | | walletService.add(dynamicProfit, member.getId(), "commission"); |
| | | |
| | | moneyFlowService.addMoneyFlow(member.getId(), dynamicProfit, MoneyFlowTypeEnum.DYNAMIC_ACHIEVE.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.COMMISSION.getValue()); |
| | | moneyFlowService.addMoneyFlow(member.getId(), dynamicProfit.negate(), MoneyFlowTypeEnum.DYNAMIC_ACHIEVE.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.SCORE.getValue()); |
| | | } |
| | | } |
| | | |