| | |
| | | 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.configure.FebsConfigure; |
| | | import cc.mrbird.febs.common.enumerates.*; |
| | | import cc.mrbird.febs.common.utils.AppContants; |
| | | import cc.mrbird.febs.common.utils.MallUtils; |
| | | import cc.mrbird.febs.mall.entity.*; |
| | | import cc.mrbird.febs.mall.mapper.*; |
| | | import cc.mrbird.febs.mall.service.AsyncService; |
| | | 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.DateTime; |
| | | import cn.hutool.core.date.DateUnit; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | 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 com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import lombok.RequiredArgsConstructor; |
| | |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @author wzy |
| | |
| | | private final IMallMoneyFlowService moneyFlowService; |
| | | private final DataDictionaryCustomMapper dataDictionaryCustomMapper; |
| | | private final MallMoneyFlowMapper mallMoneyFlowMapper; |
| | | private final MallAchieveRecordMapper mallAchieveRecordMapper; |
| | | private final MallMemberCouponMapper mallMemberCouponMapper; |
| | | private final RunVipMapper runVipMapper; |
| | | private final MallMemberWalletMapper mallMemberWalletMapper; |
| | | private final RunNodeSetMapper runNodeSetMapper; |
| | | private final MallChargeMapper mallChargeMapper; |
| | | private final IMallMoneyFlowService mallMoneyFlowService; |
| | | |
| | | private final AsyncService asyncService; |
| | | private final FebsConfigure febsConfigure; |
| | | private final RunVipGrowMapper runVipGrowMapper; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void staticProfit() { |
| | | public void staticProfit(Date profitDate) { |
| | | log.info("#####静态分红开始运行:{}#####", new Date()); |
| | | List<MallMember> members = mallMemberMapper.selectList(null); |
| | | if (CollUtil.isEmpty(members)) { |
| | | return; |
| | | } |
| | | |
| | | Date profitDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1); |
| | | BigDecimal totalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D"); |
| | | if (profitDate == null) { |
| | | profitDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1); |
| | | } |
| | | // 套餐业绩 |
| | | // BigDecimal tcIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 2); |
| | | // 普通商品业绩 |
| | | // BigDecimal normalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 1); |
| | | BigDecimal totalIncome = mallAchieveRecordMapper.selectAchieveTotal("D", profitDate); |
| | | |
| | | if (totalIncome.compareTo(BigDecimal.ZERO) == 0) { |
| | | 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); |
| | | BigDecimal perProfit = totalIncome.multiply(new BigDecimal(dic.getValue()).divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP)); |
| | | |
| | | Map<Long, List<Map<String, Object>>> map = new HashMap<>(); |
| | | Map<Long, BigDecimal> goodsProp = new HashMap<>(); |
| | | for (MallMember member : members) { |
| | | List<MallGoods> goodsList = mallGoodsMapper.selectOrderGoodsList(member.getId(), profitDate); |
| | | |
| | |
| | | } |
| | | |
| | | for (MallGoods goods : goodsList) { |
| | | BigDecimal goodsProfit = goods.getStaticProp().multiply(perProfit); |
| | | |
| | | int reduce = walletService.reduce(goodsProfit, member.getId(), "score"); |
| | | if (reduce == 2) { |
| | | break; |
| | | if (goods.getIsNormal() == 1) { |
| | | 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()); |
| | | List<Map<String, Object>> list = map.get(goods.getId()); |
| | | if (CollUtil.isEmpty(list)) { |
| | | list = new ArrayList<>(); |
| | | } |
| | | |
| | | Map<String, Object> listItem = new HashMap<>(); |
| | | listItem.put("memberId", member.getId()); |
| | | listItem.put("orderNo", goods.getOrderNo()); |
| | | list.add(listItem); |
| | | |
| | | map.put(goods.getId(), list); |
| | | goodsProp.put(goods.getId(), goods.getStaticProp()); |
| | | } |
| | | } |
| | | |
| | | if (!map.isEmpty()) { |
| | | for (Map.Entry<Long, List<Map<String, Object>>> entry : map.entrySet()) { |
| | | List<Map<String, Object>> list = entry.getValue(); |
| | | BigDecimal staticProfit = goodsProp.get(entry.getKey()).divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP).multiply(perProfit); |
| | | BigDecimal preStaticProfit = staticProfit.divide(new BigDecimal(list.size()), 2, RoundingMode.HALF_UP); |
| | | |
| | | for (Map<String, Object> item : list) { |
| | | Long memberId = (Long) item.get("memberId"); |
| | | String orderNo = (String) item.get("orderNo"); |
| | | |
| | | // int reduce = walletService.reduce(preStaticProfit, memberId, "score"); |
| | | // if (reduce == 2) { |
| | | // break; |
| | | // } |
| | | // |
| | | // walletService.add(preStaticProfit, memberId, "commission"); |
| | | // moneyFlowService.addMoneyFlow(memberId, preStaticProfit, MoneyFlowTypeEnum.STATIC_BONUS.getValue(), orderNo, FlowTypeEnum.COMMISSION.getValue()); |
| | | // moneyFlowService.addMoneyFlow(memberId, preStaticProfit.negate(), MoneyFlowTypeEnum.STATIC_BONUS.getValue(), orderNo, FlowTypeEnum.SCORE.getValue()); |
| | | changeScoreAndCommission(memberId, preStaticProfit, MoneyFlowTypeEnum.STATIC_BONUS.getValue(), orderNo); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void dynamicProfit(Long orderId) { |
| | | dynamicProfit(orderId, 2); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void dynamicProfit(Long orderId) { |
| | | public void dynamicProfit(Long orderId, Integer isNormal) { |
| | | log.info("######直推奖励, 订单ID:{}######", orderId); |
| | | MallOrderInfo orderInfo = mallOrderInfoMapper.selectById(orderId); |
| | | if (orderInfo.getOrderType() == 2) { |
| | |
| | | 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(); |
| | | // 直推奖励字典 |
| | | DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.DYNAMIC_BONUS.getType(), DataDictionaryEnum.DYNAMIC_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())); |
| | | |
| | | BigDecimal dynamicProfit = amount.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; |
| | | if (amount.compareTo(BigDecimal.ZERO) < 1) { |
| | | continue; |
| | | } |
| | | |
| | | // 隔代比例 |
| | | 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; |
| | | } |
| | | |
| | | if (direct.compareTo(BigDecimal.ONE) < 1) { |
| | | break; |
| | | } |
| | | |
| | | int reduce = walletService.reduce(direct, parentMember.getId(), "score"); |
| | | if (reduce == 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()); |
| | | |
| | | direct = direct.divide(indrectDicProp, 2, RoundingMode.HALF_UP); |
| | | // 判断套餐或者普通商品,结算对应商品的动态分红 |
| | | if (!Objects.equals(item.getIsNormal(), isNormal)) { |
| | | continue; |
| | | } |
| | | |
| | | // =======直推返利== start ===== |
| | | // 直接奖励收益 |
| | | 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, member.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; |
| | | } |
| | | // =======直推返利== end ===== |
| | | |
| | | // =======隔代奖== start ===== |
| | | if (StrUtil.isBlank(parent.getReferrerId())) { |
| | | continue; |
| | | } |
| | | |
| | | walletService.add(dynamicProfit, member.getId(), "commission"); |
| | | List<String> parents = StrUtil.split(parent.getReferrerIds(), ','); |
| | | List<MallMember> members = mallMemberMapper.selectByInviteIds(parents); |
| | | if (CollUtil.isEmpty(members)) { |
| | | return; |
| | | } |
| | | |
| | | 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()); |
| | | BigDecimal direct = dynamicProfit; |
| | | for (MallMember parentMember : members) { |
| | | if (parent.getInviteId().equals(parentMember.getInviteId())) { |
| | | 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"); |
| | | } |
| | | } |
| | | |
| | | // 隔代推荐奖 收益 |
| | | direct = direct.multiply(indrectDicProp.divide(new BigDecimal("100"), 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) { |
| | | log.info("#####==代理分红==start==#####"); |
| | | if (profitDate == null) { |
| | | profitDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1); |
| | | } |
| | | BigDecimal totalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D"); |
| | | |
| | | // 套餐业绩 |
| | | // BigDecimal tcIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 2); |
| | | // 普通商品业绩 |
| | | // BigDecimal normalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 1); |
| | | BigDecimal totalIncome = mallAchieveRecordMapper.selectAchieveTotal("D", profitDate); |
| | | |
| | | 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); |
| | | BigDecimal profit = totalIncome.multiply(new BigDecimal(dic.getValue()).divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP)); |
| | | |
| | | List<MallMember> agentMembers = mallMemberMapper.selectByIdAndNoLevel(null, AgentLevelEnum.FIRST_LEVEL.name()); |
| | | if (CollUtil.isEmpty(agentMembers)) { |
| | | return; |
| | | } |
| | | |
| | | for (MallMember agentMember : agentMembers) { |
| | | DataDictionaryCustom agentDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(AppContants.AGENT_LEVEL_REQUIRE, agentMember.getLevel()); |
| | | |
| | | if (agentDic == null) { |
| | | continue; |
| | | } |
| | | |
| | | List<DataDictionaryCustom> dics = dataDictionaryCustomMapper.selectDicByType(AppContants.AGENT_LEVEL_REQUIRE); |
| | | for (DataDictionaryCustom agentDic : dics) { |
| | | JSONObject jsonObject = JSONObject.parseObject(agentDic.getValue()); |
| | | BigDecimal profitProp = jsonObject.getBigDecimal("profitProp"); |
| | | BigDecimal income = preProfit.multiply(profitProp); |
| | | BigDecimal profitProp = jsonObject.getBigDecimal("profitProp").divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP); |
| | | |
| | | int reduce = walletService.reduce(income, agentMember.getId(), "score"); |
| | | if (reduce == 2) { |
| | | List<MallMember> agentMemberList = mallMemberMapper.selectMemberWithLevel(agentDic.getCode()); |
| | | if (CollUtil.isEmpty(agentMemberList)) { |
| | | continue; |
| | | } |
| | | |
| | | walletService.add(income, agentMember.getId(), "commission"); |
| | | BigDecimal income = profit.multiply(profitProp).divide(BigDecimal.valueOf(agentMemberList.size()), 2, RoundingMode.HALF_UP); |
| | | if (income.compareTo(BigDecimal.ZERO) < 1) { |
| | | continue; |
| | | } |
| | | |
| | | 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()); |
| | | for (MallMember agentMember : agentMemberList) { |
| | | // 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()); |
| | | changeScoreAndCommission(agentMember.getId(), income, MoneyFlowTypeEnum.AGENT_BONUS.getValue(), null); |
| | | } |
| | | } |
| | | log.info("#####==代理分红==end==#####"); |
| | | } |
| | | |
| | | private BigDecimal changeScoreAndCommission(Long memberId, BigDecimal income, Integer type, String orderNo) { |
| | | Map<String, BigDecimal> map = new HashMap<>(); |
| | | map.put("amount", income); |
| | | |
| | | int reduce = walletService.reduce(income, memberId, "score", map); |
| | | if (reduce == 2) { |
| | | return BigDecimal.ZERO; |
| | | } |
| | | |
| | | log.info("#####==代理分红==end==#####"); |
| | | 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; |
| | | } |
| | | |
| | | @Override |
| | |
| | | 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); |
| | |
| | | return; |
| | | } |
| | | |
| | | BigDecimal totalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "M"); |
| | | // 套餐业绩 |
| | | // BigDecimal tcIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "M", 2); |
| | | // 普通商品业绩 |
| | | // BigDecimal normalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "M", 1); |
| | | BigDecimal totalIncome = mallAchieveRecordMapper.selectAchieveTotal("M", profitDate); |
| | | |
| | | 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); |
| | | BigDecimal rankBonusTotal = totalIncome.multiply(new BigDecimal(dic.getValue()).divide(new BigDecimal("100"), 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; |
| | | } |
| | | // 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()); |
| | | |
| | | 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()); |
| | | changeScoreAndCommission(member.getId(), bonus, MoneyFlowTypeEnum.RANK_BONUS.getValue(), null); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 拿推荐人收益(代理,直推,排名)的10%加权平分给下面直推 |
| | | * 拿推荐人的收益(代理,直推,排名)的10%加权平分给下面直推 |
| | | */ |
| | | @Override |
| | | public void thankfulProfit() { |
| | | public void thankfulProfit(Date date) { |
| | | log.info("######==感恩奖==start==####"); |
| | | List<MallMember> mallMembers = mallMemberMapper.selectMemberListHasChild(); |
| | | if (CollUtil.isEmpty(mallMembers)) { |
| | | return; |
| | | } |
| | | if (date == null) { |
| | | date = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1); |
| | | } |
| | | |
| | | 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); |
| | | |
| | | 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()); |
| | |
| | | continue; |
| | | } |
| | | |
| | | BigDecimal preProfit = income.multiply(prop).divide(BigDecimal.valueOf(children.size()), 2, RoundingMode.HALF_DOWN); |
| | | |
| | | |
| | | int reduce = walletService.reduce(preProfit.negate(), mallMember.getId(), "commission"); |
| | | if (reduce == 2) { |
| | | BigDecimal reduceProfit = income.multiply(prop); |
| | | if (reduceProfit.compareTo(BigDecimal.ZERO) < 1) { |
| | | continue; |
| | | } |
| | | |
| | | moneyFlowService.addMoneyFlow(mallMember.getId(), income.negate(), MoneyFlowTypeEnum.THANKFUL.getValue(), null, FlowTypeEnum.COMMISSION.getValue()); |
| | | int reduce = walletService.reduce(reduceProfit, mallMember.getId(), "commission"); |
| | | if (reduce == 2) { |
| | | continue; |
| | | } |
| | | moneyFlowService.addMoneyFlow(mallMember.getId(), reduceProfit.negate(), MoneyFlowTypeEnum.THANKFUL.getValue(), null, FlowTypeEnum.COMMISSION.getValue()); |
| | | |
| | | for (MallMember child : children) { |
| | | int reduce1 = walletService.reduce(preProfit.negate(), child.getId(), "score"); |
| | | if (reduce1 == 2) { |
| | | BigDecimal preProfit = income.multiply(prop).divide(BigDecimal.valueOf(children.size()), 2, RoundingMode.HALF_DOWN); |
| | | if (preProfit.compareTo(BigDecimal.ZERO) < 1) { |
| | | continue; |
| | | } |
| | | |
| | | 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()); |
| | | // int reduce1 = walletService.reduce(preProfit, child.getId(), "score"); |
| | | // if (reduce1 == 2) { |
| | | // continue; |
| | | // } |
| | | // 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()); |
| | | changeScoreAndCommission(child.getId(), preProfit, MoneyFlowTypeEnum.THANKFUL.getValue(), null); |
| | | } |
| | | } |
| | | log.info("######==感恩奖==end==####"); |
| | | } |
| | | |
| | | @Override |
| | | public void storeAndDirectorProfit(Date profitDate) { |
| | | log.info("#####==店补/董事==start==######"); |
| | | if (profitDate == null) { |
| | | profitDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1); |
| | | } |
| | | // 套餐业绩 |
| | | // BigDecimal tcIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 2); |
| | | // 普通商品业绩 |
| | | // BigDecimal normalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 1); |
| | | BigDecimal totalIncome = mallAchieveRecordMapper.selectAchieveTotal("D", profitDate); |
| | | |
| | | // 董事 |
| | | DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.DIRECTOR_BONUS.getType(), DataDictionaryEnum.DIRECTOR_BONUS.getCode()); |
| | | List<MallMember> directors = mallMemberMapper.selectDirectorsOrStoreMaster(1); |
| | | if (CollUtil.isNotEmpty(directors)) { |
| | | BigDecimal total = totalIncome.multiply(new BigDecimal(dic.getValue()).divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP)); |
| | | BigDecimal pre = total.divide(new BigDecimal(directors.size()), 2, RoundingMode.HALF_UP); |
| | | |
| | | for (MallMember director : directors) { |
| | | if (pre.compareTo(BigDecimal.ZERO) < 1) { |
| | | continue; |
| | | } |
| | | |
| | | // int reduce = walletService.reduce(pre, director.getId(), "score"); |
| | | // if (reduce == 2) { |
| | | // continue; |
| | | // } |
| | | // walletService.add(pre, director.getId(), "commission"); |
| | | // moneyFlowService.addMoneyFlow(director.getId(), pre, MoneyFlowTypeEnum.DIRECTOR_BONUS.getValue(), null, FlowTypeEnum.COMMISSION.getValue()); |
| | | // moneyFlowService.addMoneyFlow(director.getId(), pre.negate(), MoneyFlowTypeEnum.DIRECTOR_BONUS.getValue(), null, FlowTypeEnum.SCORE.getValue()); |
| | | changeScoreAndCommission(director.getId(), pre, MoneyFlowTypeEnum.DIRECTOR_BONUS.getValue(), null); |
| | | } |
| | | } |
| | | |
| | | // 店补 |
| | | dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.COMMUNITY_BONUS.getType(), DataDictionaryEnum.COMMUNITY_BONUS.getCode()); |
| | | List<MallMember> storeMasters = mallMemberMapper.selectDirectorsOrStoreMaster(2); |
| | | if (CollUtil.isNotEmpty(storeMasters)) { |
| | | BigDecimal total = totalIncome.multiply(new BigDecimal(dic.getValue()).divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP)); |
| | | BigDecimal pre = total.divide(new BigDecimal(storeMasters.size()), 2, RoundingMode.HALF_UP); |
| | | |
| | | for (MallMember storeMaster : storeMasters) { |
| | | if (pre.compareTo(BigDecimal.ZERO) < 1) { |
| | | continue; |
| | | } |
| | | |
| | | // int reduce = walletService.reduce(pre, storeMaster.getId(), "score"); |
| | | // if (reduce == 2) { |
| | | // continue; |
| | | // } |
| | | // walletService.add(pre, storeMaster.getId(), "commission"); |
| | | // moneyFlowService.addMoneyFlow(storeMaster.getId(), pre, MoneyFlowTypeEnum.COMMUNITY_BONUS.getValue(), null, FlowTypeEnum.COMMISSION.getValue()); |
| | | // moneyFlowService.addMoneyFlow(storeMaster.getId(), pre.negate(), MoneyFlowTypeEnum.COMMUNITY_BONUS.getValue(), null, FlowTypeEnum.SCORE.getValue()); |
| | | changeScoreAndCommission(storeMaster.getId(), pre, MoneyFlowTypeEnum.COMMUNITY_BONUS.getValue(), null); |
| | | } |
| | | } |
| | | log.info("#####==店补/董事==end==######"); |
| | | } |
| | | |
| | | @Override |
| | | public void getOrderCoupon(Long orderId) { |
| | | List<MallOrderItem> mallOrderItemList = mallOrderItemMapper.selectListByOrderId(orderId); |
| | | if(CollUtil.isNotEmpty(mallOrderItemList)){ |
| | | List<Long> memberCouponIds = mallOrderItemList.stream().map(MallOrderItem::getMemberCouponId).collect(Collectors.toList()); |
| | | if(CollUtil.isNotEmpty(memberCouponIds)){ |
| | | for(Long memberCouponId : memberCouponIds){ |
| | | MallMemberCoupon mallMemberCoupon = mallMemberCouponMapper.selectById(memberCouponId); |
| | | mallMemberCoupon.setState(2); |
| | | mallMemberCouponMapper.updateById(mallMemberCoupon); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 更新会员等级,将所有等级过期的会员等级重置为最低等级 |
| | | * 查询 RunVip 表中按顺序号升序排列的第一条记录,获取最低会员等级。 |
| | | * 构建查询条件,查询 MallMember 表中会员等级过期且当前等级不是最低等级的用户。 |
| | | * 如果查询结果不为空,遍历每个用户,调用 updateLevel 方法将会员等级更新为最低等级。 |
| | | * 处理完所有用户后,结束方法 |
| | | */ |
| | | @Override |
| | | public void updateMemberLevel() { |
| | | |
| | | // 更新会员等级的逻辑 |
| | | try { |
| | | List<RunVip> runVips = runVipMapper.selectList(new LambdaQueryWrapper<RunVip>().orderByAsc(RunVip::getOrderNumber)); |
| | | RunVip runVip = runVips.get(0); |
| | | |
| | | // 获取过期时间小于等于当前时间,并且会员等级不是最小等级游客的所有用户 |
| | | LambdaQueryWrapper<MallMember> mallMemberLambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | | mallMemberLambdaQueryWrapper.le(MallMember::getVipLevelTime, DateUtil.date()); |
| | | mallMemberLambdaQueryWrapper.ne(MallMember::getLevel, runVip.getVipCode()); |
| | | List<MallMember> mallMembers = mallMemberMapper.selectList(mallMemberLambdaQueryWrapper); |
| | | if (CollUtil.isNotEmpty(mallMembers)) { |
| | | mallMembers.forEach( |
| | | item -> { |
| | | try { |
| | | mallMemberMapper.updateLevel(item.getId(), runVip.getVipCode()); |
| | | } catch (Exception e) { |
| | | log.error("Error updating member level for user {}: ", item.getId(), e); |
| | | } |
| | | } |
| | | ); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("Error fetching or updating VIP levels: ", e); |
| | | } |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | |
| | | //计算当前时间是否是最后一次 |
| | | String lastDateTimeStr = DateUtil.formatDate(DateUtil.date())+" 20:00:00"; |
| | | DateTime lastDateTime = DateUtil.parseDateTime(lastDateTimeStr); |
| | | System.out.println(DateUtil.compare(DateUtil.date(),lastDateTime) > 0); |
| | | System.out.println(DateUtil.parseTime(DateUtil.date().toTimeStr())); |
| | | } |
| | | |
| | | /** |
| | | * 获取全部会员 |
| | | * 根据会员等级的最大每日获得去给用户新增碳积分 |
| | | * 更新钱包数据 |
| | | * 生成对应的流水数据 |
| | | * 释放规则 |
| | | * 当前时间的小时 H1 / 2,整除时 |
| | | * 每个会员等级每日总释放量 T1 / 总时间间隔 C1 = 每次释放量上限 V1 |
| | | * 每次释放量上线 V1 / 2 = 每次释放量下限 V2 |
| | | * 每次释放量 V3 = 随机 [V2,V1) |
| | | * 当前时间的小时 H1 / 2,不整除时 |
| | | * 每个会员等级每日总释放量 T1 / 总时间间隔 C1 = 每次释放量上限 V1 |
| | | * 每次释放量 V3 = 每次释放量上限 V1 + (每次释放量上限 V1 - 上一次释放的数量 U1) |
| | | * 如果上一次没有释放,则 V3 = 每次释放量上限 [V1,V1*2) |
| | | */ |
| | | @Override |
| | | public void updateRunScore() { |
| | | DateTime nowTimeStr = DateUtil.date(); |
| | | String startTimeStr = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( |
| | | RunVipDataDictionaryEnum.RUN_START_TIME.getType(), |
| | | RunVipDataDictionaryEnum.RUN_START_TIME.getCode() |
| | | ).getValue(); |
| | | String endTimeStr = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( |
| | | RunVipDataDictionaryEnum.RUN_END_TIME.getType(), |
| | | RunVipDataDictionaryEnum.RUN_END_TIME.getCode() |
| | | ).getValue(); |
| | | |
| | | DateTime nowTime = DateUtil.parseTime(nowTimeStr.toTimeStr()); |
| | | DateTime startTime = DateUtil.parseTime(startTimeStr); |
| | | DateTime endTime = DateUtil.parseTime(endTimeStr); |
| | | //判断时间是否在时间范围内 |
| | | if(DateUtil.compare(nowTime,startTime) < 0 || DateUtil.compare(nowTime,endTime) > 0){ |
| | | return; |
| | | } |
| | | |
| | | List<RunVip> runVips = runVipMapper.selectList(null); |
| | | Map<String, RunVip> runVipMap = runVips.stream() |
| | | .collect(Collectors.toMap(RunVip::getVipCode, runVip -> runVip)); |
| | | List<MallMember> mallMembers = mallMemberMapper.selectList(null); |
| | | if(CollUtil.isNotEmpty(mallMembers)){ |
| | | /** |
| | | * 释放规则 |
| | | * 当前时间的小时 H1 / 2,整除时 |
| | | * 每个会员等级每日总释放量 T1 / 总时间间隔 C1 = 每次释放量上限 V1 |
| | | * 每次释放量上线 V1 / 2 = 每次释放量下限 V2 |
| | | * 每次释放量 V3 = 随机 [V2,V1) |
| | | * 当前时间的小时 H1 / 2,不整除时 |
| | | * 每个会员等级每日总释放量 T1 / 总时间间隔 C1 = 每次释放量下限 V1 |
| | | * 如果上一次有释放 |
| | | * 每次释放量上限 V2 = 每次释放量上限 V1 + (每次释放量上限 V1 - 上一次释放的数量 U1) |
| | | * 如果上一次没有释放 |
| | | * 每次释放量上限 V2 = 每次释放量上限 V1 * 2 |
| | | * 每次释放量 V3 = 随机 [V1,U2) |
| | | */ |
| | | int hour = DateUtil.hour(nowTime,true); |
| | | boolean flag = isDivisibleByTwo(hour); |
| | | |
| | | int C1 = Integer.parseInt(String.valueOf(DateUtil.between(startTime, endTime, DateUnit.HOUR))); |
| | | for (MallMember item : mallMembers) { |
| | | RunVip runVip = runVipMap.get(item.getLevel()); |
| | | if(ObjectUtil.isEmpty(runVip)){ |
| | | //未匹配到,直接跳过 |
| | | continue; |
| | | } |
| | | //每次释放的碳积分 |
| | | //每一个等级每日释放最大值 |
| | | BigDecimal T1 = new BigDecimal(runVip.getGrowthCnt()); |
| | | BigDecimal V1 = T1.divide(new BigDecimal(C1), 0, RoundingMode.DOWN); |
| | | BigDecimal V2 = BigDecimal.ZERO; |
| | | BigDecimal V3 = BigDecimal.ZERO; |
| | | if (flag) { |
| | | V2 = V1.divide(new BigDecimal("2"), 0, RoundingMode.DOWN); |
| | | if(V1.intValue() <= V2.intValue()){ |
| | | V3 = new BigDecimal(RandomUtil.randomInt(V1.intValue(),V2.intValue()+1)).setScale(0,RoundingMode.DOWN); |
| | | }else{ |
| | | V3 = new BigDecimal(RandomUtil.randomInt(V2.intValue(),V1.intValue())).setScale(0,RoundingMode.DOWN); |
| | | } |
| | | }else{ |
| | | List<MallMoneyFlow> mallMoneyFlows = mallMoneyFlowMapper.selectList( |
| | | new LambdaQueryWrapper<MallMoneyFlow>() |
| | | .eq(MallMoneyFlow::getMemberId, item.getId()) |
| | | .eq(MallMoneyFlow::getFlowType, FlowTypeEnum.SCORE.getValue()) |
| | | .eq(MallMoneyFlow::getType, RunVipMoneyFlowTypeEnum.GET_SCORE.getValue()) |
| | | .ge(MallMoneyFlow::getCreatedTime, DateUtil.beginOfDay(nowTimeStr)) |
| | | .orderByDesc(MallMoneyFlow::getCreatedTime) |
| | | .last("LIMIT 1") |
| | | ); |
| | | if(CollUtil.isNotEmpty(mallMoneyFlows)){ |
| | | BigDecimal U1 = mallMoneyFlows.get(0).getAmount().setScale(0,RoundingMode.DOWN); |
| | | V2 = V1.add(V1.subtract(U1)).setScale(0,RoundingMode.DOWN); |
| | | }else{ |
| | | V2 = V1.multiply(new BigDecimal("2")).setScale(0,RoundingMode.DOWN); |
| | | } |
| | | if(V1.intValue() < V2.intValue()){ |
| | | V3 = new BigDecimal(RandomUtil.randomInt(V1.intValue(),V2.intValue())).setScale(0,RoundingMode.DOWN); |
| | | }else if(V1.intValue() == V2.intValue()){ |
| | | V3 = new BigDecimal(RandomUtil.randomInt(V1.intValue(),V2.intValue()+1)).setScale(0,RoundingMode.DOWN); |
| | | }else{ |
| | | V3 = new BigDecimal(RandomUtil.randomInt(V2.intValue(),V1.intValue())).setScale(0,RoundingMode.DOWN); |
| | | } |
| | | } |
| | | //会员权益的收益 |
| | | RunVipGrow runVipGrow = runVipGrowMapper.selectList( |
| | | new LambdaQueryWrapper<RunVipGrow>() |
| | | .eq(RunVipGrow::getMemberId, item.getId()) |
| | | .eq(RunVipGrow::getLevelNow, runVip.getVipCode()) |
| | | .orderByDesc(RunVipGrow::getId) |
| | | ).stream().findFirst().orElse(null); |
| | | if(null != runVipGrow){ |
| | | BigDecimal amountNow = runVipGrow.getAmountNow(); |
| | | RunVip runVipNext = runVips.stream().filter(vipItem -> vipItem.getOrderNumber() == runVip.getOrderNumber() + 1).findFirst().orElse(null); |
| | | if(runVipNext != null){ |
| | | |
| | | BigDecimal presentPrice = runVip.getPresentPrice(); |
| | | amountNow = amountNow.subtract(presentPrice); |
| | | |
| | | BigDecimal presentPriceNext = runVipNext.getPresentPrice(); |
| | | BigDecimal subtract = presentPriceNext.subtract(presentPrice); |
| | | |
| | | BigDecimal divide = amountNow.divide(subtract, 4, RoundingMode.DOWN); |
| | | |
| | | Integer growthCnt = runVip.getGrowthCnt(); |
| | | Integer growthCntNext = runVipNext.getGrowthCnt(); |
| | | int growthNum = growthCntNext - growthCnt; |
| | | |
| | | BigDecimal growthAmountTotal = divide.multiply(new BigDecimal(growthNum)); |
| | | BigDecimal growthAmount = growthAmountTotal.divide(new BigDecimal(C1), 0, RoundingMode.DOWN); |
| | | if(growthAmount.intValue() > 0){ |
| | | V3 = V3.add(growthAmount); |
| | | } |
| | | } |
| | | } |
| | | //更新钱包数据 |
| | | if(V3.intValue() <= 0){ |
| | | continue; |
| | | } |
| | | //异步线程池去释放积分操作 |
| | | asyncService.releaseScore(V3,item.getId()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void updateMemberCoin() { |
| | | try { |
| | | // 操作时间 |
| | | DateTime operationDate = DateUtil.parseDate(DateUtil.today()); |
| | | |
| | | // 获取会员等级 |
| | | List<RunVip> runVips = runVipMapper.selectList(null); |
| | | if (CollUtil.isEmpty(runVips)) { |
| | | log.error("未找到任何会员等级"); |
| | | return; |
| | | } |
| | | Map<String, RunVip> runVipMap = runVips.stream() |
| | | .collect(Collectors.toMap(RunVip::getVipCode, runVip -> runVip)); |
| | | |
| | | // 获取最低级别的会员等级 |
| | | Optional<RunVip> optionalRunVip = runVips.stream().min(Comparator.comparingInt(RunVip::getOrderNumber)); |
| | | RunVip minRunVip = optionalRunVip.orElse(null); |
| | | if (minRunVip == null) { |
| | | log.error("最低级别的会员等级未找到"); |
| | | return; |
| | | } |
| | | |
| | | // 获取全部等级不是游客的用户,返回一个set集合 |
| | | List<MallMember> mallMembers = mallMemberMapper.selectList( |
| | | new LambdaQueryWrapper<MallMember>() |
| | | .select(MallMember::getId, MallMember::getLevel, MallMember::getInviteId, MallMember::getReferrerId) |
| | | .ne(MallMember::getLevel,minRunVip.getVipCode()) |
| | | ); |
| | | if (CollUtil.isEmpty(mallMembers)) { |
| | | log.info("没有符合条件的会员"); |
| | | return; |
| | | } |
| | | |
| | | Set<Long> memberIds = mallMembers.stream() |
| | | .map(MallMember::getId) |
| | | .collect(Collectors.toSet()); |
| | | if (CollUtil.isEmpty(memberIds)) { |
| | | log.info("没有有效的会员ID"); |
| | | return; |
| | | } |
| | | |
| | | |
| | | // 构建成员ID到MallMember对象的映射 |
| | | Map<Long, MallMember> memberMap = mallMembers.stream() |
| | | .collect(Collectors.toMap(MallMember::getId, member -> member)); |
| | | // 缓存直推成员 |
| | | Map<String, Set<MallMember>> directMembersCache = mallMembers.stream() |
| | | .filter(member -> StrUtil.isNotBlank(member.getReferrerId())) |
| | | .collect(Collectors.groupingBy(MallMember::getReferrerId, Collectors.toSet())); |
| | | |
| | | |
| | | for (Long memberId : memberIds) { |
| | | MallMember mallMember = memberMap.get(memberId); |
| | | if (mallMember == null || minRunVip.getVipCode().equals(mallMember.getLevel())) { |
| | | continue; |
| | | } |
| | | |
| | | // 获取所有购买了会员等级的直推成员 |
| | | Set<MallMember> directMembers = directMembersCache.get(mallMember.getInviteId()); |
| | | if (CollUtil.isEmpty(directMembers)) { |
| | | continue; |
| | | } |
| | | |
| | | RunVip memberRunVip = runVipMap.get(mallMember.getLevel()); |
| | | if (memberRunVip == null) { |
| | | log.warn("会员等级 {} 不存在", mallMember.getLevel()); |
| | | continue; |
| | | } |
| | | for (MallMember item : directMembers) { |
| | | BigDecimal realScore = calculateDirectScore(memberRunVip, item, operationDate, runVipMap); |
| | | if(BigDecimal.ZERO.compareTo(realScore) >= 0){ |
| | | return; |
| | | } |
| | | walletService.addScore(realScore, memberId); |
| | | String orderNo = MallUtils.getOrderNum("ZLS"); |
| | | mallMoneyFlowService.runVipMoneyFlowAdd( |
| | | memberId, |
| | | item.getId(), |
| | | orderNo, |
| | | FlowTypeEnum.SCORE.getValue(), |
| | | RunVipMoneyFlowTypeEnum.DIRECT_SCORE.getValue(), |
| | | realScore, |
| | | StrUtil.format(RunVipMoneyFlowTypeEnum.DIRECT_SCORE.getDescription(), realScore), |
| | | YesOrNoEnum.YES.getValue() |
| | | ); |
| | | // CompletableFuture.runAsync(() -> { |
| | | // BigDecimal realScore = calculateDirectScore(memberRunVip, item, operationDate, runVipMap); |
| | | // if(BigDecimal.ZERO.compareTo(realScore) >= 0){ |
| | | // return; |
| | | // } |
| | | // walletService.addScore(realScore, memberId); |
| | | // String orderNo = MallUtils.getOrderNum("ZLS"); |
| | | // mallMoneyFlowService.runVipMoneyFlowAdd( |
| | | // memberId, |
| | | // item.getId(), |
| | | // orderNo, |
| | | // FlowTypeEnum.SCORE.getValue(), |
| | | // RunVipMoneyFlowTypeEnum.DIRECT_SCORE.getValue(), |
| | | // realScore, |
| | | // StrUtil.format(RunVipMoneyFlowTypeEnum.DIRECT_SCORE.getDescription(), realScore), |
| | | // YesOrNoEnum.YES.getValue() |
| | | // ); |
| | | // },febsConfigure.asyncThreadPoolTaskExecutor()); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("更新会员积分时发生异常", e); |
| | | throw new RuntimeException("更新会员积分时发生异常", e); // 根据业务需求选择是否抛出异常 |
| | | } |
| | | } |
| | | |
| | | |
| | | private BigDecimal calculateDirectScore(RunVip memberRunVip, MallMember directMember, Date operationDate, Map<String, RunVip> runVipMap) { |
| | | // 实际助力 |
| | | BigDecimal realScore = BigDecimal.ZERO; |
| | | |
| | | try { |
| | | RunVip directRunVip = runVipMap.get(directMember.getLevel()); |
| | | if (directRunVip == null) { |
| | | return realScore; |
| | | } |
| | | // 获取每一个会员的前一日碳积分总和 |
| | | LambdaQueryWrapper<MallMoneyFlow> mallMoneyFlowLambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | | mallMoneyFlowLambdaQueryWrapper.ge(MallMoneyFlow::getCreatedTime, DateUtil.offsetDay(operationDate, -1)); |
| | | mallMoneyFlowLambdaQueryWrapper.lt(MallMoneyFlow::getCreatedTime, operationDate); |
| | | mallMoneyFlowLambdaQueryWrapper.eq(MallMoneyFlow::getFlowType, FlowTypeEnum.SCORE.getValue()); |
| | | mallMoneyFlowLambdaQueryWrapper.eq(MallMoneyFlow::getType, RunVipMoneyFlowTypeEnum.GET_SCORE.getValue()); |
| | | mallMoneyFlowLambdaQueryWrapper.eq(MallMoneyFlow::getMemberId, directMember.getId()); |
| | | |
| | | List<MallMoneyFlow> mallMoneyFlows = mallMoneyFlowMapper.selectList(mallMoneyFlowLambdaQueryWrapper); |
| | | if (CollUtil.isEmpty(mallMoneyFlows)) { |
| | | return realScore; |
| | | } |
| | | |
| | | // 如果直推小于会员本身的会员等级,则全部助力 |
| | | if (memberRunVip.getOrderNumber() >= directRunVip.getOrderNumber()) { |
| | | BigDecimal rebatePercent = directRunVip.getRebatePercent(); |
| | | BigDecimal totalScore = mallMoneyFlows.stream() |
| | | .map(MallMoneyFlow::getAmount) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | realScore = totalScore.multiply(rebatePercent).setScale(0, RoundingMode.DOWN); |
| | | } else { |
| | | Integer growthCnt = memberRunVip.getGrowthCnt(); |
| | | if (growthCnt == null || growthCnt <= 0) { |
| | | return realScore; |
| | | } |
| | | BigDecimal rebatePercent1 = memberRunVip.getRebatePercent(); |
| | | if (rebatePercent1 == null || rebatePercent1.compareTo(BigDecimal.ZERO) <= 0) { |
| | | return realScore; |
| | | } |
| | | realScore = new BigDecimal(growthCnt).multiply(rebatePercent1).setScale(0, RoundingMode.DOWN); |
| | | } |
| | | } catch (Exception e) { |
| | | // 记录日志并返回默认值 |
| | | log.error("Error calculating direct score", e); |
| | | return BigDecimal.ZERO; |
| | | } |
| | | |
| | | return realScore; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | public boolean isDivisibleByTwo(int number) { |
| | | return number % 2 == 0; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 更新会员钱包中的积分,将所有积分大于0的会员钱包积分清零 |
| | | * 初始化最大尝试次数 maxAttempts 和当前尝试次数 attemptCount。 |
| | | * 使用 while 循环,每次查询1000条积分大于0的会员钱包记录。 |
| | | * 如果查询结果为空,设置标志位 flag 为 false,退出循环。 |
| | | * 否则,提取记录ID并调用 updateScore 方法将积分更新为0。 |
| | | * 增加尝试次数,继续下一次循环,直到达到最大尝试次数或查询结果为空。 |
| | | */ |
| | | @Override |
| | | public void updateMemberScore() { |
| | | // 更新会员钱包积分的逻辑 |
| | | int maxAttempts = 100; // 最大尝试次数 |
| | | int attemptCount = 0; |
| | | boolean flag = true; |
| | | while (flag && attemptCount < maxAttempts) { |
| | | try { |
| | | Page<MallMemberWallet> page = new Page<>(0, 1000); |
| | | IPage<MallMemberWallet> result = mallMemberWalletMapper.selectPage( |
| | | page, |
| | | new LambdaQueryWrapper<MallMemberWallet>() |
| | | .gt(MallMemberWallet::getScore, 0) |
| | | ); |
| | | List<MallMemberWallet> mallMemberWallets = result.getRecords(); |
| | | if (CollUtil.isEmpty(mallMemberWallets)) { |
| | | flag = false; |
| | | } else { |
| | | List<Long> ids = mallMemberWallets.stream().map(MallMemberWallet::getId).collect(Collectors.toList()); |
| | | mallMemberWalletMapper.updateScore(ids); |
| | | } |
| | | attemptCount++; |
| | | } catch (Exception e) { |
| | | log.error("Error updating member level: ", e); |
| | | flag = false; // 发生异常时退出循环 |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void updateNodeScore() { |
| | | /** |
| | | * 获取全部的节点 |
| | | */ |
| | | List<MallMember> mallMembers = mallMemberMapper.selectList( |
| | | new LambdaQueryWrapper<MallMember>() |
| | | .eq(MallMember::getDirector, YesOrNoEnum.YES.getValue()) |
| | | ); |
| | | if(CollUtil.isEmpty(mallMembers)){ |
| | | return; |
| | | } |
| | | //获取节点设置 |
| | | List<RunNodeSet> runNodeSets = runNodeSetMapper.selectList(null); |
| | | |
| | | //获取节点设置runNodeSets中orderNumber最小的节点 |
| | | RunNodeSet minRunNodeSet = runNodeSets.stream().min(Comparator.comparingInt(RunNodeSet::getOrderNumber)).orElse(null); |
| | | if(null == minRunNodeSet){ |
| | | return; |
| | | } |
| | | |
| | | DateTime endTime = DateUtil.date(); |
| | | for (MallMember item : mallMembers) { |
| | | //获取总业绩 |
| | | BigDecimal achieve = getDirectAchieve(item.getInviteId(),item.getDirectorTime(),endTime); |
| | | //判断总业绩是否小于最小业绩 |
| | | if(minRunNodeSet.getMinAchieve().compareTo(achieve) > 0){ |
| | | item.setDirector(YesOrNoEnum.NO.getValue()); |
| | | mallMemberMapper.updateById(item); |
| | | continue; |
| | | } |
| | | BigDecimal nodePercent = getNodePercent(runNodeSets, achieve); |
| | | if(BigDecimal.ZERO.compareTo(nodePercent) >= 0){ |
| | | continue; |
| | | } |
| | | asyncService.releaseNodeCoin(achieve,nodePercent,item.getId()); |
| | | item.setDirectorTime(endTime); |
| | | mallMemberMapper.updateById(item); |
| | | } |
| | | } |
| | | |
| | | private BigDecimal getDirectAchieve(String inviteId,Date startTime,Date endTime) { |
| | | |
| | | try { |
| | | // 获取直推成员和团队成员的ID集合 |
| | | Set<Long> memberIds = getTeamMemberIds(inviteId); |
| | | |
| | | if (CollUtil.isEmpty(memberIds)) { |
| | | return BigDecimal.ZERO; |
| | | } |
| | | |
| | | // 获取团队业绩(不包含本人业绩) |
| | | List<MallCharge> mallCharges = mallChargeMapper.selectList( |
| | | new LambdaQueryWrapper<MallCharge>() |
| | | .in(MallCharge::getMemberId, memberIds) |
| | | .eq(MallCharge::getState, YesOrNoEnum.YES.getValue()) |
| | | .ge(MallCharge::getCreatedTime, startTime) |
| | | .lt(MallCharge::getCreatedTime, endTime) |
| | | ); |
| | | |
| | | if (CollUtil.isEmpty(mallCharges)) { |
| | | return BigDecimal.ZERO; |
| | | } |
| | | |
| | | return mallCharges.stream() |
| | | .map(MallCharge::getAmount) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | } catch (Exception e) { |
| | | // 异常处理 |
| | | log.error("Error occurred while calculating direct achieve for member: {}", inviteId, e); |
| | | return BigDecimal.ZERO; |
| | | } |
| | | } |
| | | |
| | | private Set<Long> getTeamMemberIds(String inviteId) { |
| | | Set<Long> memberIds = new HashSet<>(); |
| | | // 获取直推成员 |
| | | List<MallMember> directMembers = mallMemberMapper.selectList( |
| | | new LambdaQueryWrapper<MallMember>() |
| | | .eq(MallMember::getReferrerId, inviteId) |
| | | ); |
| | | if(CollUtil.isEmpty(directMembers)){ |
| | | return memberIds; |
| | | } |
| | | memberIds.addAll(directMembers.stream().map(MallMember::getId).collect(Collectors.toSet())); |
| | | |
| | | // 获取团队成员 |
| | | List<MallMember> teamMembers = mallMemberMapper.selectList( |
| | | new LambdaQueryWrapper<MallMember>() |
| | | .in(MallMember::getReferrerId, directMembers.stream().map(MallMember::getInviteId).collect(Collectors.toSet())) |
| | | ); |
| | | |
| | | if(CollUtil.isEmpty(teamMembers)){ |
| | | return memberIds; |
| | | } |
| | | memberIds.addAll(teamMembers.stream().map(MallMember::getId).collect(Collectors.toSet())); |
| | | |
| | | return memberIds; |
| | | } |
| | | |
| | | |
| | | private BigDecimal getNodePercent(List<RunNodeSet> runNodeSets, BigDecimal directAchieve) { |
| | | BigDecimal percent = BigDecimal.ZERO; |
| | | //获取directAchieve在runNodeSets中哪一个位置 |
| | | for(RunNodeSet item : runNodeSets){ |
| | | BigDecimal minAchieve = item.getMinAchieve(); |
| | | BigDecimal maxAchieve = item.getMaxAchieve(); |
| | | BigDecimal nodePercent = new BigDecimal(item.getNodePercent()); |
| | | if(directAchieve.compareTo(minAchieve) >= 0 && directAchieve.compareTo(maxAchieve) < 0){ |
| | | percent = nodePercent; |
| | | break; |
| | | } |
| | | } |
| | | return percent; |
| | | } |
| | | } |