安化黑茶系统开发之团队奖励和晋升部分源码记录
由于系统较为复杂,我们将分模块设计,主要包括:
- 会员模块(Member):处理会员的注册、推荐关系、层级关系等。
- 奖励模块(Reward):处理推荐奖励、团队奖励的计算和发放。
- 晋升模块(Promotion):处理会员的晋升逻辑
1. 核心实体类设计
首先定义系统所需的实体类,用于数据建模和业务逻辑处理:
// 会员实体类 public class Member { private Long id; private String username; private String realName; // 实名认证 private String phone; private Long referrerId; // 推荐人ID private Integer memberLevel; // 会员等级:1-普通会员,2-组长,3-经理等 private String currentPlate; // 当前所在盘:BLACK/RED private Integer platePosition; // 在盘中的位置 private BigDecimal totalBonus; // 累计奖金 private Date registrationDate; private Integer status; // 状态:0-无效,1-有效 // 构造函数、getter、setter省略 } // 奖金记录实体 public class BonusRecord { private Long id; private Long memberId; private String bonusType; // 奖金类型:RECOMMEND/TEAM/PROMOTION private BigDecimal amount; private String description; private Date createTime; private Integer status; // 0-未发放,1-已发放 // 构造函数、getter、setter省略 } // 团队关系实体 public class TeamRelation { private Long id; private Long memberId; private Long parentId; // 上级ID private Integer depth; // 层级深度 private String path; // 路径索引,如:1.2.3. // 构造函数、getter、setter省略 }
2. 数据访问层(DAO)
定义数据访问接口
@Mapper public interface MemberMapper { Member selectById(@Param("id") Long id); int updateMemberLevel(@Param("id") Long id, @Param("level") Integer level); int updatePlateInfo(@Param("id") Long id, @Param("plate") String plate, @Param("position") Integer position); List<Member> selectDirectReferrals(@Param("referrerId") Long referrerId); List<Member> selectTeamMembersByPath(@Param("path") String path); } @Mapper public interface BonusRecordMapper { int insertBonusRecord(BonusRecord record); BigDecimal selectTotalBonusByMemberId(@Param("memberId") Long memberId); } @Mapper public interface TeamRelationMapper { int insertTeamRelation(TeamRelation relation); TeamRelation selectRelationByMemberId(@Param("memberId") Long memberId); List<TeamRelation> selectSubordinatesByPath(@Param("path") String path); }
3. 服务层核心实现
3.1 推荐奖励服务
@Service @Transactional public class ReferralBonusService { @Autowired private MemberMapper memberMapper; @Autowired private BonusRecordMapper bonusRecordMapper; private static final BigDecimal REFERRAL_BONUS_RATE = new BigDecimal("0.10"); // 推荐奖励10% /** * 处理推荐奖励 * @param referrerId 推荐人ID * @param newMemberId 新会员ID * @param orderAmount 订单金额 * @return 奖励金额 */ public BigDecimal processReferralBonus(Long referrerId, Long newMemberId, BigDecimal orderAmount) { // 参数验证 if (referrerId == null || newMemberId == null || orderAmount == null) { throw new IllegalArgumentException("推荐奖励参数不能为空"); } // 计算推荐奖励 BigDecimal bonusAmount = orderAmount.multiply(REFERRAL_BONUS_RATE); // 创建奖金记录 BonusRecord bonusRecord = new BonusRecord(); bonusRecord.setMemberId(referrerId); bonusRecord.setBonusType("RECOMMEND"); bonusRecord.setAmount(bonusAmount); bonusRecord.setDescription("推荐新会员奖励"); bonusRecord.setCreateTime(new Date()); bonusRecord.setStatus(1); int result = bonusRecordMapper.insertBonusRecord(bonusRecord); if (result > 0) { // 记录日志 System.out.println("推荐奖励发放成功:推荐人-" + referrerId + ", 金额-" + bonusAmount); return bonusAmount; } else { throw new RuntimeException("推荐奖励记录失败"); } } /** * 获取会员的推荐奖励总额 */ public BigDecimal getTotalReferralBonus(Long memberId) { return bonusRecordMapper.selectTotalBonusByMemberIdAndType(memberId, "RECOMMEND"); } }
3.2 团队奖励服务
@Service @Transactional public class TeamBonusService { @Autowired private TeamRelationMapper teamRelationMapper; @Autowired private BonusRecordMapper bonusRecordMapper; @Autowired private MemberMapper memberMapper; private static final int MAX_TEAM_DEPTH = 10; // 最大团队层级 private static final BigDecimal[] TEAM_BONUS_RATES = { new BigDecimal("0.05"), // 一级5% new BigDecimal("0.03"), // 二级3% new BigDecimal("0.02") // 三级2% }; /** * 计算团队奖励 * @param memberId 当前会员ID * @param teamPerformance 团队业绩 */ public BigDecimal calculateTeamBonus(Long memberId, BigDecimal teamPerformance) { TeamRelation relation = teamRelationMapper.selectRelationByMemberId(memberId); if (relation == null) { throw new RuntimeException("会员团队关系不存在"); } BigDecimal totalBonus = BigDecimal.ZERO; // 根据层级计算不同比例的奖励 for (int i = 0; i < TEAM_BONUS_RATES.length; i++) { BigDecimal levelBonus = calculateLevelBonus(relation.getPath(), i + 1, teamPerformance); totalBonus = totalBonus.add(levelBonus); } return totalBonus; } /** * 发放团队奖励 */ public void distributeTeamBonus(Long activatorId, BigDecimal orderAmount) { TeamRelation activatorRelation = teamRelationMapper.selectRelationByMemberId(activatorId); if (activatorRelation == null) return; String path = activatorRelation.getPath(); String[] parentIds = path.split("\\."); for (int i = 0; i < Math.min(parentIds.length, TEAM_BONUS_RATES.length); i++) { Long parentId = Long.valueOf(parentIds[parentIds.length - 1 - i]); BigDecimal rate = TEAM_BONUS_RATES[i]; BigDecimal bonusAmount = orderAmount.multiply(rate); // 创建团队奖励记录 BonusRecord bonusRecord = new BonusRecord(); bonusRecord.setMemberId(parentId); bonusRecord.setBonusType("TEAM"); bonusRecord.setAmount(bonusAmount); bonusRecord.setDescription(i + 1 + "代团队奖励"); bonusRecord.setCreateTime(new Date()); bonusRecord.setStatus(1); bonusRecordMapper.insertBonusRecord(bonusRecord); } } private BigDecimal calculateLevelBonus(String path, int level, BigDecimal performance) { // 实现特定层级的奖金计算逻辑 return performance.multiply(TEAM_BONUS_RATES[level - 1]); } }
3.3 会员晋升服务
@Service @Transactional public class MemberPromotionService { @Autowired private MemberMapper memberMapper; @Autowired private TeamRelationMapper teamRelationMapper; @Autowired private PlateAllocationService plateAllocationService; /** * 检查并执行会员晋升 */ public PromotionResult checkAndPromoteMember(Long memberId) { Member member = memberMapper.selectById(memberId); if (member == null) { throw new RuntimeException("会员不存在"); } // 检查晋升条件 PromotionCriteria criteria = checkPromotionCriteria(member); if (criteria.isQualified()) { return executePromotion(member, criteria); } return new PromotionResult(false, "未满足晋升条件", null); } /** * 检查晋升条件 */ private PromotionCriteria checkPromotionCriteria(Member member) { PromotionCriteria criteria = new PromotionCriteria(); // 检查直接推荐人数 List<Member> directReferrals = memberMapper.selectDirectReferrals(member.getId()); criteria.setDirectReferralCount(directReferrals.size()); // 检查团队规模 TeamRelation relation = teamRelationMapper.selectRelationByMemberId(member.getId()); List<TeamRelation> teamMembers = teamRelationMapper.selectSubordinatesByPath(relation.getPath()); criteria.setTeamSize(teamMembers.size()); // 检查团队业绩 BigDecimal teamPerformance = calculateTeamPerformance(teamMembers); criteria.setTeamPerformance(teamPerformance); // 设置晋升标准(根据实际业务调整) criteria.setRequiredDirectReferrals(5); criteria.setRequiredTeamSize(20); criteria.setRequiredPerformance(new BigDecimal("50000")); return criteria; } /** * 执行晋升逻辑 */ private PromotionResult executePromotion(Member member, PromotionCriteria criteria) { try { // 1. 提升会员等级 int newLevel = member.getMemberLevel() + 1; memberMapper.updateMemberLevel(member.getId(), newLevel); // 2. 处理盘位晋升逻辑(黑盘到红盘) if ("BLACK".equals(member.getCurrentPlate())) { plateAllocationService.promoteToRedPlate(member.getId()); } // 3. 发放晋升奖励 BigDecimal promotionBonus = calculatePromotionBonus(newLevel); if (promotionBonus.compareTo(BigDecimal.ZERO) > 0) { distributePromotionBonus(member.getId(), promotionBonus, "等级晋升奖励"); } return new PromotionResult(true, "晋升成功", newLevel); } catch (Exception e) { throw new RuntimeException("会员晋升失败: " + e.getMessage()); } } /** * 盘位分配服务 */ @Service public class PlateAllocationService { public static final int PLATE_CAPACITY = 8; // 每个盘8个点位 /** * 将黑盘组长晋升到红盘 */ public void promoteToRedPlate(Long memberId) { Member promoter = memberMapper.selectById(memberId); // 检查是否是黑盘组长 if (!"BLACK".equals(promoter.getCurrentPlate()) || promoter.getPlatePosition() != 1) { throw new RuntimeException("只有黑盘组长可以晋升到红盘"); } // 分配红盘位置 allocateRedPlatePosition(promoter); // 重组黑盘逻辑 reorganizeBlackPlate(promoter); } private void allocateRedPlatePosition(Member promoter) { // 实现红盘位置分配逻辑 // 这里需要根据实际业务规则实现 memberMapper.updatePlateInfo(promoter.getId(), "RED", 1); } private void reorganizeBlackPlate(Member promoter) { // 实现黑盘重组逻辑 // 包括重新分配组长、副组长等 } } // 晋升条件封装类 private class PromotionCriteria { private int directReferralCount; private int teamSize; private BigDecimal teamPerformance; private int requiredDirectReferrals; private int requiredTeamSize; private BigDecimal requiredPerformance; public boolean isQualified() { return directReferralCount >= requiredDirectReferrals && teamSize >= requiredTeamSize && teamPerformance.compareTo(requiredPerformance) >= 0; } // getter和setter方法 } // 晋升结果封装类 public class PromotionResult { private boolean success; private String message; private Integer newLevel; public PromotionResult(boolean success, String message, Integer newLevel) { this.success = success; this.message = message; this.newLevel = newLevel; } // getter和setter方法 } }
4. 控制器层(Controller)
@RestController @RequestMapping("/api/bonus") public class BonusController { @Autowired private ReferralBonusService referralBonusService; @Autowired private TeamBonusService teamBonusService; @Autowired private MemberPromotionService promotionService; /** * 处理新会员注册奖励 */ @PostMapping("/referral") public ResponseEntity<Map<String, Object>> handleReferralBonus( @RequestParam Long referrerId, @RequestParam Long newMemberId, @RequestParam BigDecimal orderAmount) { try { BigDecimal bonus = referralBonusService.processReferralBonus(referrerId, newMemberId, orderAmount); Map<String, Object> result = new HashMap<>(); result.put("success", true); result.put("bonusAmount", bonus); result.put("message", "推荐奖励发放成功"); return ResponseEntity.ok(result); } catch (Exception e) { Map<String, Object> error = new HashMap<>(); error.put("success", false); error.put("message", e.getMessage()); return ResponseEntity.badRequest().body(error); } } /** * 触发团队奖励计算 */ @PostMapping("/team") public ResponseEntity<Map<String, Object>> triggerTeamBonus( @RequestParam Long memberId, @RequestParam BigDecimal performance) { try { teamBonusService.distributeTeamBonus(memberId, performance); Map<String, Object> result = new HashMap<>(); result.put("success", true); result.put("message", "团队奖励计算完成"); return ResponseEntity.ok(result); } catch (Exception e) { Map<String, Object> error = new HashMap<>(); error.put("success", false); error.put("message", e.getMessage()); return ResponseEntity.badRequest().body(error); } } /** * 检查并执行晋升 */ @PostMapping("/promotion/check") public ResponseEntity<Map<String, Object>> checkPromotion(@RequestParam Long memberId) { try { MemberPromotionService.PromotionResult result = promotionService.checkAndPromoteMember(memberId); Map<String, Object> response = new HashMap<>(); response.put("success", result.isSuccess()); response.put("message", result.getMessage()); response.put("newLevel", result.getNewLevel()); return ResponseEntity.ok(response); } catch (Exception e) { Map<String, Object> error = new HashMap<>(); error.put("success", false); error.put("message", e.getMessage()); return ResponseEntity.badRequest().body(error); } } }
5. 配置和工具类
// 奖金计算配置类 @Configuration @ConfigurationProperties(prefix = "bonus") @Data public class BonusConfig { private Map<String, BigDecimal> levelRates = new HashMap<>(); private Map<Integer, BigDecimal> promotionBonus = new HashMap<>(); private int maxGenerationLevel = 10; // 可以从application.yml中加载配置 } // 奖金计算工具类 @Component public class BonusCalculator { public BigDecimal calculateBonus(BigDecimal amount, BigDecimal rate) { if (amount == null || rate == null) { return BigDecimal.ZERO; } return amount.multiply(rate).setScale(2, RoundingMode.HALF_UP); } public boolean isQualifiedForPromotion(int actual, int required) { return actual >= required; } }
微信号:tieniu6636
浙公网安备 33010602011771号