| | |
| | | |
| | | private final MallSystemSettingMapper mallSystemSettingMapper; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void staticProfit(Date profitDate) { |
| | | log.info("#####静态分红开始运行:{}#####", new Date()); |
| | | List<MallMember> members = mallMemberMapper.selectList(null); |
| | | if (CollUtil.isEmpty(members)) { |
| | | return; |
| | | } |
| | | |
| | | /** |
| | | * |
| | | */ |
| | | 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.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); |
| | | |
| | | if (CollUtil.isEmpty(goodsList)) { |
| | | continue; |
| | | } |
| | | |
| | | for (MallGoods goods : goodsList) { |
| | | if (goods.getIsNormal() == 1) { |
| | | continue; |
| | | } |
| | | |
| | | 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"); |
| | | changeAllScoreAndCommission(preStaticProfit,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); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | public void changeAllScoreAndCommission(BigDecimal preStaticProfit,String orderNo){ |
| | | List<MallMember> members = mallMemberMapper.selectList(null); |
| | | for(MallMember mallMember : members){ |
| | | changeScoreAndCommission(mallMember.getId(), preStaticProfit, MoneyFlowTypeEnum.STATIC_BONUS.getValue(), orderNo); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void dynamicProfit(Long orderId) { |
| | | // return; |
| | | dynamicProfit(orderId, 2); |
| | | } |
| | | |
| | | /** |
| | | * 直推收益 |
| | | * |
| | |
| | | |
| | | // 直推奖 |
| | | BigDecimal profit = amount.multiply(profitPer.divide(new BigDecimal(100), 2, RoundingMode.HALF_UP)); |
| | | changeScoreAndCommission(parentMember.getId(), profit, MoneyFlowTypeEnum.DYNAMIC_ACHIEVE.getValue(), orderInfo.getOrderNo()); |
| | | changeScoreAndCommission(parentMember.getId(), profit, MoneyFlowTypeEnum.DIRECT_BONUS.getValue(), orderInfo.getOrderNo()); |
| | | |
| | | // 代理推代理 |
| | | if (isSameLevel == 1) { |
| | |
| | | } |
| | | |
| | | BigDecimal doubleParentProfit = profit.multiply(indirectPer); |
| | | changeScoreAndCommission(doubleParentMember.getId(), doubleParentProfit, MoneyFlowTypeEnum.RECOMMEND_BONUS.getValue(), orderInfo.getOrderNo()); |
| | | changeScoreAndCommission(doubleParentMember.getId(), doubleParentProfit, MoneyFlowTypeEnum.DIRECT_BONUS.getValue(), orderInfo.getOrderNo()); |
| | | } |
| | | |
| | | // 非代理推代理 |
| | |
| | | } |
| | | |
| | | BigDecimal doubleParentProfit = profit.multiply(indirectPer2); |
| | | changeScoreAndCommission(mallMember.getId(), doubleParentProfit, MoneyFlowTypeEnum.RECOMMEND_BONUS.getValue(), orderInfo.getOrderNo()); |
| | | changeScoreAndCommission(mallMember.getId(), doubleParentProfit, MoneyFlowTypeEnum.DIRECT_BONUS.getValue(), orderInfo.getOrderNo()); |
| | | } |
| | | } |
| | | } |
| | |
| | | // 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()); |
| | | // dynamicProfit = changeScoreAndCommission(parent.getId(), dynamicProfit, MoneyFlowTypeEnum.DYNAMIC_ACHIEVE.getValue(), orderInfo.getOrderNo()); |
| | | if (dynamicProfit.compareTo(BigDecimal.ZERO) < 1) { |
| | | 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()); |
| | | // changeScoreAndCommission(parentMember.getId(), direct, MoneyFlowTypeEnum.RECOMMEND_BONUS.getValue(), orderInfo.getOrderNo()); |
| | | } |
| | | // =======隔代奖== end ===== |
| | | } |
| | |
| | | moneyFlowService.addMoneyFlow(memberId, income, type, orderNo, FlowTypeEnum.COMMISSION.getValue()); |
| | | moneyFlowService.addMoneyFlow(memberId, income.negate(), type, orderNo, FlowTypeEnum.SCORE.getValue()); |
| | | return income; |
| | | } |
| | | |
| | | @Override |
| | | public void scorePool() { |
| | | log.info("#####==积分池==start==######"); |
| | | |
| | | String redisKey = "scorePool"; |
| | | // List<MallOrderInfo> mallOrderInfos = mallOrderInfoMapper.selectList(new QueryWrapper<>()); |
| | | // if(CollUtil.isEmpty(mallOrderInfos)){ |
| | | // redisUtils.set(redisKey,0,0L); |
| | | // return; |
| | | // } |
| | | /** |
| | | * 后一天2点,结算前一天的所有消费 |
| | | * 积分池增加每日消费的10% |
| | | * 每天释放1%,给全体用户 |
| | | */ |
| | | //前一天的所有消费的10% |
| | | DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.SCORE_POOR_RADIO.getType(), DataDictionaryEnum.SCORE_POOR_RADIO.getCode()); |
| | | if(ObjectUtil.isEmpty(dic)){ |
| | | return; |
| | | } |
| | | ScorePoorDto scorePoorDto = JSONUtil.toBean(dic.getValue(), ScorePoorDto.class); |
| | | BigDecimal everydayRatio = scorePoorDto.getEverydayRatio().multiply(new BigDecimal(0.01)); |
| | | BigDecimal dividEquallyRatio = scorePoorDto.getDividEquallyRatio().multiply(new BigDecimal(0.01)); |
| | | |
| | | Date date = DateUtil.offsetDay(new Date(), -1); |
| | | BigDecimal totalAmount = mallOrderInfoMapper.selectTotalAmountByPayDoneAndDataString(1, date); |
| | | BigDecimal multiplyTotalAmount = totalAmount.multiply(everydayRatio); |
| | | String scorePoolRedis = redisUtils.getString(redisKey); |
| | | if(StrUtil.isBlank(scorePoolRedis)){ |
| | | redisUtils.set(redisKey,0,0L); |
| | | } |
| | | |
| | | BigDecimal scorePoolOld = new BigDecimal(redisUtils.getString(redisKey)); |
| | | BigDecimal scorePoolNew = scorePoolOld.add(multiplyTotalAmount); |
| | | |
| | | //每天释放1%给全体用户 |
| | | BigDecimal scorePoolToDay = scorePoolNew.multiply(dividEquallyRatio); |
| | | |
| | | redisUtils.set(redisKey,scorePoolNew.subtract(scorePoolToDay).intValue(),0L); |
| | | |
| | | QueryWrapper<MallMember> formalMember = new QueryWrapper<>(); |
| | | formalMember.ne("level", AgentLevelEnum.ZERO_LEVEL.name()); |
| | | List<MallMember> mallMembers = mallMemberMapper.selectList(formalMember); |
| | | int cnt = 0; |
| | | if(CollUtil.isNotEmpty(mallMembers)){ |
| | | BigDecimal income = new BigDecimal(scorePoolToDay.divide(new BigDecimal(mallMembers.size()), 2, RoundingMode.HALF_UP).intValue()); |
| | | if(income.compareTo(BigDecimal.ZERO) < 1){ |
| | | return; |
| | | } |
| | | for(MallMember mallMember : mallMembers){ |
| | | /* |
| | | 平分成佣金,一比一减少赠送积分 |
| | | 如果没有赠送积分,则平分0 |
| | | */ |
| | | BigDecimal outCome = BigDecimal.ZERO; |
| | | MallMemberWallet mallMemberWallet = mallMemberWalletMapper.selectWalletByMemberId(mallMember.getId()); |
| | | if(mallMemberWallet.getScore().compareTo(BigDecimal.ZERO) < 1){ |
| | | continue; |
| | | } |
| | | if(mallMemberWallet.getScore().compareTo(income) < 0){ |
| | | outCome = new BigDecimal(mallMemberWallet.getScore().intValue()); |
| | | }else{ |
| | | outCome = income; |
| | | } |
| | | walletService.add(outCome, mallMember.getId(), "commission"); |
| | | walletService.reduce(outCome, mallMember.getId(), "score"); |
| | | moneyFlowService.addMoneyFlow(mallMember.getId(), outCome, MoneyFlowTypeEnum.SCORE_POOL.getValue(), null, FlowTypeEnum.COMMISSION.getValue()); |
| | | moneyFlowService.addMoneyFlow(mallMember.getId(), outCome.negate(), MoneyFlowTypeEnum.SCORE_POOL.getValue(), null, FlowTypeEnum.SCORE.getValue()); |
| | | cnt ++; |
| | | } |
| | | } |
| | | log.info("#####==积分池==end==######;" + |
| | | "今日新增积分:" + multiplyTotalAmount.setScale(2, RoundingMode.HALF_UP) |
| | | +";平分积分数量:"+scorePoolToDay.setScale(2, RoundingMode.HALF_UP) |
| | | +";人数:"+cnt); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void selaHalfVoucher(String price) { |
| | | /** |
| | | * 获取所有凭证大于0的用户 |
| | | * 当前价格卖出账户的一半凭证 |
| | | */ |
| | | BigDecimal scorePrice = new BigDecimal(price == null ? "0" : price); |
| | | if(scorePrice.compareTo(BigDecimal.ZERO) <= 0){ |
| | | return; |
| | | } |
| | | List<MallMemberWallet> mallMemberWallets = mallMemberWalletMapper.selectList(null); |
| | | if(CollUtil.isNotEmpty(mallMemberWallets)){ |
| | | List<MallMemberWallet> wallets = mallMemberWallets |
| | | .stream() |
| | | .filter(mallMemberWallet -> mallMemberWallet.getVoucherCnt().compareTo(BigDecimal.ZERO) > 0) |
| | | .collect(Collectors.toList()); |
| | | if(CollUtil.isNotEmpty(wallets)){ |
| | | BigDecimal scorePoolCntAdd = BigDecimal.ZERO; |
| | | //绿色积分剩余数量 |
| | | for(MallMemberWallet mallMemberWallet : wallets){ |
| | | //增加账户的凭证金额,减少当前的一半的凭证数量 |
| | | BigDecimal voucherCnt = mallMemberWallet.getVoucherCnt().divide(new BigDecimal(2), 2, BigDecimal.ROUND_DOWN) |
| | | .setScale(2,BigDecimal.ROUND_DOWN); |
| | | BigDecimal voucherAmountAdd = scorePrice.multiply(voucherCnt) |
| | | .setScale(2,BigDecimal.ROUND_DOWN); |
| | | mallMemberWalletMapper.addVorCherAmountAndCntById(voucherAmountAdd,voucherCnt,mallMemberWallet.getId()); |
| | | |
| | | scorePoolCntAdd = scorePoolCntAdd.add(voucherCnt); |
| | | |
| | | DataDictionaryCustom surplusCntDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( |
| | | GreenScoreEnum.SURPLUS_CNT.getType(), |
| | | GreenScoreEnum.SURPLUS_CNT.getCode() |
| | | ); |
| | | //增加绿色积分剩余数量 |
| | | BigDecimal surplusCnt = new BigDecimal(surplusCntDic.getValue() == null ? "0" : surplusCntDic.getValue()); |
| | | BigDecimal voucherCntAdd = surplusCnt.add(voucherCnt).setScale(2,BigDecimal.ROUND_DOWN); |
| | | surplusCntDic.setValue(voucherCntAdd.toString()); |
| | | dataDictionaryCustomMapper.updateById(surplusCntDic); |
| | | |
| | | /** |
| | | * 生成一条卖出记录 |
| | | */ |
| | | String voucherNo = MallUtils.getOrderNum("VS"); |
| | | MallScoreVoucher mallScoreVoucher = new MallScoreVoucher(); |
| | | mallScoreVoucher.setVoucherNo(voucherNo); |
| | | mallScoreVoucher.setMemberId(mallMemberWallet.getMemberId()); |
| | | mallScoreVoucher.setVoucherCnt(voucherCnt); |
| | | mallScoreVoucher.setPrice(scorePrice); |
| | | mallScoreVoucher.setVoucherAmount(voucherAmountAdd); |
| | | mallScoreVoucher.setType("S"); |
| | | mallScoreVoucherMapper.insert(mallScoreVoucher); |
| | | |
| | | //产生一条流水记录 |
| | | mallMoneyFlowService.addMoneyFlow( |
| | | mallMemberWallet.getMemberId(), |
| | | voucherAmountAdd, |
| | | MoneyFlowTypeEnum.VOUCHER_SALE.getValue(), |
| | | voucherNo, |
| | | FlowTypeEnum.BALANCE.getValue()); |
| | | } |
| | | |
| | | //增加积分凭证池的凭证数量 |
| | | DataDictionaryCustom scorePoolCntDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( |
| | | GreenScoreEnum.SCORE_POOL_CNT.getType(), |
| | | GreenScoreEnum.SCORE_POOL_CNT.getCode() |
| | | ); |
| | | //增加绿色积分剩余数量 |
| | | BigDecimal scorePoolCnt = new BigDecimal(scorePoolCntDic.getValue() == null ? "0" : scorePoolCntDic.getValue()); |
| | | scorePoolCnt = scorePoolCnt.add(scorePoolCntAdd); |
| | | scorePoolCntDic.setValue(scorePoolCnt.toString()); |
| | | dataDictionaryCustomMapper.updateById(scorePoolCntDic); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 给用户的增加凭证数据,并且增加流水 |
| | | * @param memberAchieveRelease 释放数量 |
| | | * @param memberId 用户ID |
| | | * @param Type 流水类型 |
| | | * @param flowNo 流水编号 |
| | | */ |
| | | private void releaseToMemberAndAddFlow(BigDecimal memberAchieveRelease,Long memberId,int Type,String flowNo){ |
| | | //释放到用户的账户 |
| | | mallMemberWalletMapper.addVorCherCntByMemberId(memberAchieveRelease,memberId); |
| | | //产生一条流水记录 TODO 需要去掉注释 |
| | | mallMoneyFlowService.addMoneyFlow( |
| | | memberId, |
| | | memberAchieveRelease, |
| | | Type, |
| | | flowNo, |
| | | FlowTypeEnum.VOUCHER_CNT.getValue()); |
| | | } |
| | | |
| | | /** |
| | | * 根据星级等级和占比,给该星级的用户释放凭证 |
| | | * @param levelAchieveRelease |
| | | * @param level |
| | | * @param percent |
| | | * @return |
| | | */ |
| | | private BigDecimal releaseByLevel(BigDecimal levelAchieveRelease,String level,BigDecimal percent,String scoreNo){ |
| | | //实际支付的凭证数量 |
| | | BigDecimal levelReleaseReal = BigDecimal.ZERO; |
| | | //当前星级的人数 |
| | | List<MallMember> levelMembers = mallMemberMapper.selectMemberWithLevel(level); |
| | | if(CollUtil.isNotEmpty(levelMembers)){ |
| | | //这个星级该获得的凭证数量 = 总数 * 比例 |
| | | BigDecimal levelReleaseCnt = levelAchieveRelease.multiply(percent).setScale(2, BigDecimal.ROUND_DOWN); |
| | | if(levelReleaseCnt.compareTo(BigDecimal.ZERO) > 0){ |
| | | //每个人获得的数量 |
| | | BigDecimal levelRelease = levelReleaseCnt.divide(new BigDecimal(levelMembers.size()), 2, BigDecimal.ROUND_DOWN); |
| | | if(levelRelease.compareTo(BigDecimal.ZERO) > 0){ |
| | | for(MallMember mallMember : levelMembers){ |
| | | releaseToMemberAndAddFlow(levelRelease,mallMember.getId(),MoneyFlowTypeEnum.ACHIEVE_RELEASE_SCORE_XJ.getValue(),scoreNo); |
| | | levelReleaseReal.add(levelRelease); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | return levelReleaseReal; |
| | | } |
| | | |
| | | /** |
| | | * 获取每日最大产生的凭证数量 |
| | | * @return |
| | | */ |
| | | private BigDecimal getAchieveDailyRelease(){ |
| | | DataDictionaryCustom achieveReleasePercentDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( |
| | | GreenScoreEnum.ACHIEVE_RELEASE_PERCENT.getType(), |
| | | GreenScoreEnum.ACHIEVE_RELEASE_PERCENT.getCode() |
| | | ); |
| | | BigDecimal achieveReleasePercent = new BigDecimal(achieveReleasePercentDic.getValue() == null ? "0" : achieveReleasePercentDic.getValue()) |
| | | .multiply(new BigDecimal(0.0001)); |
| | | |
| | | |
| | | DataDictionaryCustom totalCntDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( |
| | | GreenScoreEnum.TOTAL_CNT.getType(), |
| | | GreenScoreEnum.TOTAL_CNT.getCode() |
| | | ); |
| | | //总凭证数量 |
| | | BigDecimal totalCnt = new BigDecimal(totalCntDic.getValue() == null ? "0" : totalCntDic.getValue()).multiply(new BigDecimal(0.9)); |
| | | |
| | | //获取每日最大产生的凭证数量 |
| | | BigDecimal achieveRelease = totalCnt.multiply(achieveReleasePercent); |
| | | return achieveRelease; |
| | | } |
| | | |
| | | @Override |