|
|
@@ -121,10 +121,29 @@ public class InviteActivityServiceImpl extends ServiceImpl<InviteActivityMapper,
|
|
|
throw new BusinessException(400, "结束时间必须大于开始时间");
|
|
|
}
|
|
|
|
|
|
- // 校验优惠券模板是否存在且可用
|
|
|
- CouponTemplate template = couponTemplateService.getById(dto.getCouponTemplateId());
|
|
|
- if (template == null) {
|
|
|
- throw new BusinessException(400, "优惠券模板不存在");
|
|
|
+ // 获取双向奖励配置
|
|
|
+ Long inviterCouponTemplateId = dto.getInviterCouponTemplateId();
|
|
|
+ Long inviteeCouponTemplateId = dto.getInviteeCouponTemplateId();
|
|
|
+
|
|
|
+ // 【核心校验】至少需要配置一组奖励(邀请人奖励或被邀请人奖励)
|
|
|
+ if (inviterCouponTemplateId == null && inviteeCouponTemplateId == null) {
|
|
|
+ throw new BusinessException(400, "至少需要配置一组奖励(邀请人奖励或被邀请人奖励)");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 校验邀请人优惠券模板是否存在且可用(如果配置了)
|
|
|
+ if (inviterCouponTemplateId != null) {
|
|
|
+ CouponTemplate inviterTemplate = couponTemplateService.getById(inviterCouponTemplateId);
|
|
|
+ if (inviterTemplate == null) {
|
|
|
+ throw new BusinessException(400, "邀请人优惠券模板不存在");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 校验被邀请人优惠券模板是否存在且可用(如果配置了)
|
|
|
+ if (inviteeCouponTemplateId != null) {
|
|
|
+ CouponTemplate inviteeTemplate = couponTemplateService.getById(inviteeCouponTemplateId);
|
|
|
+ if (inviteeTemplate == null) {
|
|
|
+ throw new BusinessException(400, "被邀请人优惠券模板不存在");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
InviteActivity activity = new InviteActivity();
|
|
|
@@ -133,7 +152,8 @@ public class InviteActivityServiceImpl extends ServiceImpl<InviteActivityMapper,
|
|
|
activity.setStartTime(dto.getStartTime());
|
|
|
activity.setEndTime(dto.getEndTime());
|
|
|
activity.setStatus(ActivityStatusEnum.DRAFT.getCode());
|
|
|
- activity.setCouponTemplateId(dto.getCouponTemplateId());
|
|
|
+ activity.setCouponTemplateId(inviterCouponTemplateId); // 邀请人优惠券模板ID
|
|
|
+ activity.setInviteeCouponTemplateId(inviteeCouponTemplateId); // 被邀请人优惠券模板ID
|
|
|
activity.setDailyLimit(dto.getDailyLimit() != null ? dto.getDailyLimit() : 10);
|
|
|
activity.setTotalLimit(dto.getTotalLimit() != null ? dto.getTotalLimit() : 100);
|
|
|
activity.setRequireFirstOrder(dto.getRequireFirstOrder() != null ? dto.getRequireFirstOrder() : 1);
|
|
|
@@ -150,7 +170,8 @@ public class InviteActivityServiceImpl extends ServiceImpl<InviteActivityMapper,
|
|
|
|
|
|
this.save(activity);
|
|
|
|
|
|
- log.info("创建邀请活动成功: id={}, name={}, creator={}", activity.getId(), activity.getActivityName(), creatorName);
|
|
|
+ log.info("创建邀请活动成功: id={}, name={}, creator={}, inviterCouponTemplateId={}, inviteeCouponTemplateId={}",
|
|
|
+ activity.getId(), activity.getActivityName(), creatorName, inviterCouponTemplateId, inviteeCouponTemplateId);
|
|
|
return activity;
|
|
|
}
|
|
|
|
|
|
@@ -186,9 +207,39 @@ public class InviteActivityServiceImpl extends ServiceImpl<InviteActivityMapper,
|
|
|
if (dto.getEndTime() != null) {
|
|
|
activity.setEndTime(dto.getEndTime());
|
|
|
}
|
|
|
- if (dto.getCouponTemplateId() != null) {
|
|
|
- activity.setCouponTemplateId(dto.getCouponTemplateId());
|
|
|
+
|
|
|
+ // 【双向奖励】处理邀请人优惠券模板ID更新(优先使用 inviterCouponTemplateId,兼容旧的 couponTemplateId)
|
|
|
+ Long newInviterCouponTemplateId = dto.getInviterCouponTemplateId();
|
|
|
+ if (newInviterCouponTemplateId == null) {
|
|
|
+ newInviterCouponTemplateId = dto.getCouponTemplateId(); // 兼容旧字段
|
|
|
}
|
|
|
+ if (newInviterCouponTemplateId != null) {
|
|
|
+ // 校验邀请人优惠券模板是否存在
|
|
|
+ CouponTemplate inviterTemplate = couponTemplateService.getById(newInviterCouponTemplateId);
|
|
|
+ if (inviterTemplate == null) {
|
|
|
+ throw new BusinessException(400, "邀请人优惠券模板不存在");
|
|
|
+ }
|
|
|
+ activity.setCouponTemplateId(newInviterCouponTemplateId);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 【双向奖励】处理被邀请人优惠券模板ID更新
|
|
|
+ Long newInviteeCouponTemplateId = dto.getInviteeCouponTemplateId();
|
|
|
+ if (newInviteeCouponTemplateId != null) {
|
|
|
+ // 校验被邀请人优惠券模板是否存在
|
|
|
+ CouponTemplate inviteeTemplate = couponTemplateService.getById(newInviteeCouponTemplateId);
|
|
|
+ if (inviteeTemplate == null) {
|
|
|
+ throw new BusinessException(400, "被邀请人优惠券模板不存在");
|
|
|
+ }
|
|
|
+ activity.setInviteeCouponTemplateId(newInviteeCouponTemplateId);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 【核心校验】更新后至少需要配置一组奖励
|
|
|
+ Long finalInviterCouponTemplateId = activity.getCouponTemplateId();
|
|
|
+ Long finalInviteeCouponTemplateId = activity.getInviteeCouponTemplateId();
|
|
|
+ if (finalInviterCouponTemplateId == null && finalInviteeCouponTemplateId == null) {
|
|
|
+ throw new BusinessException(400, "至少需要配置一组奖励(邀请人奖励或被邀请人奖励)");
|
|
|
+ }
|
|
|
+
|
|
|
if (dto.getDailyLimit() != null) {
|
|
|
activity.setDailyLimit(dto.getDailyLimit());
|
|
|
}
|
|
|
@@ -213,7 +264,8 @@ public class InviteActivityServiceImpl extends ServiceImpl<InviteActivityMapper,
|
|
|
|
|
|
this.updateById(activity);
|
|
|
|
|
|
- log.info("更新邀请活动成功: id={}, name={}", activity.getId(), activity.getActivityName());
|
|
|
+ log.info("更新邀请活动成功: id={}, name={}, inviterCouponTemplateId={}, inviteeCouponTemplateId={}",
|
|
|
+ activity.getId(), activity.getActivityName(), finalInviterCouponTemplateId, finalInviteeCouponTemplateId);
|
|
|
return activity;
|
|
|
}
|
|
|
|
|
|
@@ -229,6 +281,13 @@ public class InviteActivityServiceImpl extends ServiceImpl<InviteActivityMapper,
|
|
|
throw new BusinessException(400, "当前状态不允许发布");
|
|
|
}
|
|
|
|
|
|
+ // 【发布前校验】检查是否已配置至少一组奖励(邀请人奖励或被邀请人奖励)
|
|
|
+ Long inviterCouponTemplateId = activity.getCouponTemplateId();
|
|
|
+ Long inviteeCouponTemplateId = activity.getInviteeCouponTemplateId();
|
|
|
+ if (inviterCouponTemplateId == null && inviteeCouponTemplateId == null) {
|
|
|
+ throw new BusinessException(400, "发布失败:活动未配置任何奖励,请至少配置邀请人奖励或被邀请人奖励");
|
|
|
+ }
|
|
|
+
|
|
|
LocalDateTime now = LocalDateTime.now();
|
|
|
int newStatus = (now.isAfter(activity.getStartTime()) && now.isBefore(activity.getEndTime()))
|
|
|
? ActivityStatusEnum.ONGOING.getCode()
|
|
|
@@ -237,7 +296,8 @@ public class InviteActivityServiceImpl extends ServiceImpl<InviteActivityMapper,
|
|
|
boolean result = inviteActivityMapper.updateStatus(id, newStatus) > 0;
|
|
|
|
|
|
if (result) {
|
|
|
- log.info("发布邀请活动成功: id={}, status={}", id, newStatus);
|
|
|
+ log.info("发布邀请活动成功: id={}, status={}, inviterCouponTemplateId={}, inviteeCouponTemplateId={}",
|
|
|
+ id, newStatus, inviterCouponTemplateId, inviteeCouponTemplateId);
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
@@ -410,6 +470,20 @@ public class InviteActivityServiceImpl extends ServiceImpl<InviteActivityMapper,
|
|
|
inviterUserId, notifyEx.getMessage(), notifyEx);
|
|
|
}
|
|
|
|
|
|
+ // 8. 向被邀请人即时发放新人优惠券(异步,发券失败不影响绑定关系)
|
|
|
+ try {
|
|
|
+ boolean couponResult = distributeInviteeCoupon(record.getId());
|
|
|
+ if (couponResult) {
|
|
|
+ log.info("被邀请人优惠券发放成功: recordId={}, inviteeUserId={}", record.getId(), inviteeUserId);
|
|
|
+ } else {
|
|
|
+ log.warn("被邀请人优惠券发放失败(已记录,可后续手动补发): recordId={}, inviteeUserId={}",
|
|
|
+ record.getId(), inviteeUserId);
|
|
|
+ }
|
|
|
+ } catch (Exception couponEx) {
|
|
|
+ log.error("向被邀请人发放优惠券异常(不影响绑定关系): recordId={}, inviteeUserId={}, error={}",
|
|
|
+ record.getId(), inviteeUserId, couponEx.getMessage(), couponEx);
|
|
|
+ }
|
|
|
+
|
|
|
log.info("绑定邀请关系成功: recordId={}, inviterUserId={}, inviteeUserId={}, inviteCode={}",
|
|
|
record.getId(), inviterUserId, inviteeUserId, inviteCode);
|
|
|
return true;
|
|
|
@@ -473,39 +547,60 @@ public class InviteActivityServiceImpl extends ServiceImpl<InviteActivityMapper,
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public boolean distributeReward(Long recordId) {
|
|
|
+ log.info("[邀请人奖励] 开始发放邀请人奖励: recordId={}", recordId);
|
|
|
+
|
|
|
// 1. 根据记录ID查询邀请记录
|
|
|
InviteRecord record = inviteRecordMapper.selectById(recordId);
|
|
|
if (record == null) {
|
|
|
- log.error("邀请记录不存在: recordId={}", recordId);
|
|
|
+ log.error("[邀请人奖励] 邀请记录不存在: recordId={}", recordId);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 2. 检查记录状态是否为已激活
|
|
|
if (record.getStatus() != InviteStatusEnum.ACTIVATED.getCode()) {
|
|
|
- log.warn("邀请记录状态不正确,无法发放奖励: recordId={}, status={}", recordId, record.getStatus());
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // 3. 检查是否已经发放过奖励
|
|
|
- InviteReward existingReward = inviteRewardMapper.selectByRecordId(recordId);
|
|
|
- if (existingReward != null && existingReward.getStatus() == InviteRewardStatusEnum.DISTRIBUTED.getCode()) {
|
|
|
- log.warn("奖励已发放,不能重复发放: recordId={}, rewardId={}", recordId, existingReward.getId());
|
|
|
+ log.warn("[邀请人奖励] 邀请记录状态不正确,无法发放奖励: recordId={}, status={}", recordId, record.getStatus());
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- // 4. 获取活动配置的优惠券模板ID
|
|
|
+ // 3. 获取活动配置
|
|
|
InviteActivity activity = this.getById(record.getActivityId());
|
|
|
if (activity == null) {
|
|
|
- log.error("活动不存在: activityId={}", record.getActivityId());
|
|
|
+ log.error("[邀请人奖励] 活动不存在: activityId={}", record.getActivityId());
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- // 5. 创建待发放的奖励记录(先创建记录,再发放优惠券)
|
|
|
+ // 4. 【新增】配置检查:仅在配置了邀请人优惠券模板时才发放(与被邀请人即时发券逻辑解耦)
|
|
|
+ Long inviterCouponTemplateId = activity.getCouponTemplateId(); // couponTemplateId 即为邀请人优惠券模板ID
|
|
|
+ if (inviterCouponTemplateId == null) {
|
|
|
+ log.info("[邀请人奖励] 活动未配置邀请人优惠券模板,跳过发放: activityId={}, recordId={}, inviterUserId={}",
|
|
|
+ activity.getId(), recordId, record.getInviterUserId());
|
|
|
+ return true; // 返回true表示成功(虽然没发券,但这是预期行为,兼容单向奖励模式)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 5. 【优化】防重复发放检查:查询是否已存在该邀请记录的邀请人奖励记录(reward_type=0)
|
|
|
+ InviteReward existingInviterReward = inviteRewardMapper.selectByRecordIdAndRewardType(recordId, 0); // rewardType=0 表示邀请人
|
|
|
+ if (existingInviterReward != null) {
|
|
|
+ if (existingInviterReward.getStatus() == InviteRewardStatusEnum.DISTRIBUTED.getCode()) {
|
|
|
+ log.warn("[邀请人奖励] 邀请人奖励已发放,不能重复发放: recordId={}, rewardId={}, inviterUserId={}",
|
|
|
+ recordId, existingInviterReward.getId(), record.getInviterUserId());
|
|
|
+ return false;
|
|
|
+ } else if (existingInviterReward.getStatus() == InviteRewardStatusEnum.PENDING.getCode()
|
|
|
+ || existingInviterReward.getStatus() == InviteRewardStatusEnum.FAILED.getCode()) {
|
|
|
+ log.warn("[邀请人奖励] 邀请人奖励记录已存在(状态={}),不能重复创建: recordId={}, rewardId={}, status={}",
|
|
|
+ existingInviterReward.getStatus() == InviteRewardStatusEnum.PENDING.getCode() ? "待发放" : "失败",
|
|
|
+ recordId, existingInviterReward.getId(), existingInviterReward.getStatus());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 6. 创建待发放的奖励记录(先创建记录,再发放优惠券)
|
|
|
InviteReward reward = new InviteReward();
|
|
|
reward.setActivityId(record.getActivityId());
|
|
|
reward.setRecordId(recordId);
|
|
|
reward.setInviterUserId(record.getInviterUserId());
|
|
|
- reward.setCouponTemplateId(activity.getCouponTemplateId());
|
|
|
+ reward.setCouponTemplateId(inviterCouponTemplateId); // 使用经过配置检查的模板ID
|
|
|
+ reward.setRewardType(0); // 邀请人奖励(确保与被邀请人奖励rewardType=1区分)
|
|
|
+ reward.setTriggerType(0); // 首单完成触发(确保与被邀请人即时发放triggerType=1区分)
|
|
|
reward.setStatus(InviteRewardStatusEnum.PENDING.getCode()); // 待发放
|
|
|
reward.setUserCouponId(null);
|
|
|
reward.setCouponCode(null);
|
|
|
@@ -517,12 +612,15 @@ public class InviteActivityServiceImpl extends ServiceImpl<InviteActivityMapper,
|
|
|
|
|
|
inviteRewardMapper.insert(reward);
|
|
|
|
|
|
- // 6. 调用UserCouponService发放优惠券给邀请人
|
|
|
+ log.info("[邀请人奖励] 创建邀请人奖励记录成功: recordId={}, rewardId={}, inviterUserId={}, couponTemplateId={}",
|
|
|
+ recordId, reward.getId(), record.getInviterUserId(), inviterCouponTemplateId);
|
|
|
+
|
|
|
+ // 7. 调用UserCouponService发放优惠券给邀请人
|
|
|
try {
|
|
|
CouponDistributeDTO distributeDTO = new CouponDistributeDTO();
|
|
|
distributeDTO.setUserId(record.getInviterUserId());
|
|
|
- distributeDTO.setTemplateId(activity.getCouponTemplateId());
|
|
|
- distributeDTO.setSource("邀请活动奖励");
|
|
|
+ distributeDTO.setTemplateId(inviterCouponTemplateId); // 使用经过配置检查的模板ID
|
|
|
+ distributeDTO.setSource("邀请活动-邀请人奖励");
|
|
|
distributeDTO.setSourceId(String.valueOf(recordId));
|
|
|
|
|
|
userCouponService.distributeCoupon(distributeDTO, null, "系统自动发放");
|
|
|
@@ -530,21 +628,21 @@ public class InviteActivityServiceImpl extends ServiceImpl<InviteActivityMapper,
|
|
|
// TODO: 需要从distributeCoupon返回值中获取userCouponId和couponCode
|
|
|
// 这里暂时设置为占位值,实际应根据返回值更新
|
|
|
Long userCouponId = -1L; // 应从实际发放结果获取
|
|
|
- String couponCode = "AUTO_" + System.currentTimeMillis(); // 应从实际发放结果获取
|
|
|
+ String couponCode = "INVITER_" + System.currentTimeMillis(); // 应从实际发放结果获取
|
|
|
|
|
|
- // 7. 更新奖励记录为已发放
|
|
|
+ // 8. 更新奖励记录为已发放
|
|
|
inviteRewardMapper.updateToDistributed(reward.getId(), userCouponId, couponCode);
|
|
|
|
|
|
- // 8. 更新邀请记录状态为已奖励
|
|
|
+ // 9. 更新邀请记录状态为已奖励
|
|
|
inviteRecordMapper.updateToRewarded(recordId);
|
|
|
|
|
|
- // 9. 更新活动的奖励发放数+1
|
|
|
+ // 10. 更新活动的奖励发放数+1
|
|
|
inviteActivityMapper.incrementRewardCount(record.getActivityId());
|
|
|
|
|
|
- // 10. 发送消息通知
|
|
|
+ // 11. 发送消息通知(仅邀请人奖励通知)
|
|
|
try {
|
|
|
// 获取优惠券模板信息用于通知
|
|
|
- CouponTemplate couponTemplate = couponTemplateService.getById(activity.getCouponTemplateId());
|
|
|
+ CouponTemplate couponTemplate = couponTemplateService.getById(inviterCouponTemplateId);
|
|
|
if (couponTemplate != null) {
|
|
|
notificationService.sendRewardSuccessNotification(
|
|
|
record.getInviterUserId(),
|
|
|
@@ -553,18 +651,117 @@ public class InviteActivityServiceImpl extends ServiceImpl<InviteActivityMapper,
|
|
|
);
|
|
|
}
|
|
|
} catch (Exception notifyEx) {
|
|
|
- log.error("发送奖励通知失败(不影响业务): recordId={}, error={}",
|
|
|
+ log.error("[邀请人奖励] 发送奖励通知失败(不影响业务): recordId={}, error={}",
|
|
|
recordId, notifyEx.getMessage(), notifyEx);
|
|
|
}
|
|
|
|
|
|
- log.info("发放邀请奖励成功: recordId={}, rewardId={}, inviterUserId={}",
|
|
|
- recordId, reward.getId(), record.getInviterUserId());
|
|
|
+ log.info("[邀请人奖励] 发放邀请人奖励成功: recordId={}, rewardId={}, inviterUserId={}, userCouponId={}",
|
|
|
+ recordId, reward.getId(), record.getInviterUserId(), userCouponId);
|
|
|
return true;
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
// 发放失败,更新奖励状态为失败
|
|
|
- log.error("发放优惠券失败: recordId={}, error={}", recordId, e.getMessage(), e);
|
|
|
- inviteRewardMapper.updateToFailed(reward.getId(), "优惠券发放失败: " + e.getMessage());
|
|
|
+ log.error("[邀请人奖励] 发放优惠券失败: recordId={}, inviterUserId={}, couponTemplateId={}, error={}",
|
|
|
+ recordId, record.getInviterUserId(), inviterCouponTemplateId, e.getMessage(), e);
|
|
|
+ inviteRewardMapper.updateToFailed(reward.getId(), "邀请人优惠券发放失败: " + e.getMessage());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public boolean distributeInviteeCoupon(Long recordId) {
|
|
|
+ log.info("开始向被邀请人发放新人优惠券: recordId={}", recordId);
|
|
|
+
|
|
|
+ // 1. 查询邀请记录
|
|
|
+ InviteRecord record = inviteRecordMapper.selectById(recordId);
|
|
|
+ if (record == null) {
|
|
|
+ log.error("邀请记录不存在,无法发放被邀请人优惠券: recordId={}", recordId);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 检查是否已发放过被邀请人奖励(防止重复发放)
|
|
|
+ InviteReward existingInviteeReward = inviteRewardMapper.selectByRecordIdAndRewardType(recordId, 1); // rewardType=1 表示被邀请人
|
|
|
+ if (existingInviteeReward != null && existingInviteeReward.getStatus() == InviteRewardStatusEnum.DISTRIBUTED.getCode()) {
|
|
|
+ log.warn("被邀请人奖励已发放,不能重复发放: recordId={}, rewardId={}", recordId, existingInviteeReward.getId());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 查询活动配置,获取 inviteeCouponTemplateId
|
|
|
+ InviteActivity activity = this.getById(record.getActivityId());
|
|
|
+ if (activity == null) {
|
|
|
+ log.error("活动不存在,无法发放被邀请人优惠券: activityId={}", record.getActivityId());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 如果 inviteeCouponTemplateId 为空,跳过不发券(兼容单向奖励模式)
|
|
|
+ if (activity.getInviteeCouponTemplateId() == null) {
|
|
|
+ log.info("活动未配置被邀请人优惠券模板,跳过发券: activityId={}, recordId={}", activity.getId(), recordId);
|
|
|
+ return true; // 返回true表示成功(虽然没发券,但这是预期行为)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 5. 创建待发放的被邀请人奖励记录
|
|
|
+ InviteReward inviteeReward = new InviteReward();
|
|
|
+ inviteeReward.setActivityId(record.getActivityId());
|
|
|
+ inviteeReward.setRecordId(recordId);
|
|
|
+ inviteeReward.setInviterUserId(record.getInviteeUserId()); // 注意:这里是被邀请人的用户ID
|
|
|
+ inviteeReward.setCouponTemplateId(activity.getInviteeCouponTemplateId());
|
|
|
+ inviteeReward.setRewardType(1); // 被邀请人奖励
|
|
|
+ inviteeReward.setTriggerType(1); // 邀请绑定触发(即时发放)
|
|
|
+ inviteeReward.setStatus(InviteRewardStatusEnum.PENDING.getCode()); // 待发放
|
|
|
+ inviteeReward.setUserCouponId(null);
|
|
|
+ inviteeReward.setCouponCode(null);
|
|
|
+ inviteeReward.setFailReason(null);
|
|
|
+ inviteeReward.setDistributeTime(null);
|
|
|
+ inviteeReward.setRetryCount(0);
|
|
|
+ inviteeReward.setOperatorId(null);
|
|
|
+ inviteeReward.setOperatorName(null);
|
|
|
+
|
|
|
+ inviteRewardMapper.insert(inviteeReward);
|
|
|
+
|
|
|
+ // 6. 调用优惠券服务发放优惠券给被邀请人
|
|
|
+ try {
|
|
|
+ CouponDistributeDTO distributeDTO = new CouponDistributeDTO();
|
|
|
+ distributeDTO.setUserId(record.getInviteeUserId()); // 发放给被邀请人
|
|
|
+ distributeDTO.setTemplateId(activity.getInviteeCouponTemplateId()); // 使用被邀请人优惠券模板
|
|
|
+ distributeDTO.setSource("邀请活动-新人注册奖励");
|
|
|
+ distributeDTO.setSourceId(String.valueOf(recordId));
|
|
|
+
|
|
|
+ userCouponService.distributeCoupon(distributeDTO, null, "系统自动发放");
|
|
|
+
|
|
|
+ // TODO: 需要从distributeCoupon返回值中获取userCouponId和couponCode
|
|
|
+ Long userCouponId = -1L; // 应从实际发放结果获取
|
|
|
+ String couponCode = "INVITEE_" + System.currentTimeMillis(); // 应从实际发放结果获取
|
|
|
+
|
|
|
+ // 7. 更新奖励记录为已发放
|
|
|
+ inviteRewardMapper.updateToDistributed(inviteeReward.getId(), userCouponId, couponCode);
|
|
|
+
|
|
|
+ // 8. 记录日志
|
|
|
+ log.info("向被邀请人发放新人优惠券成功: recordId={}, rewardId={}, inviteeUserId={}, couponTemplateId={}",
|
|
|
+ recordId, inviteeReward.getId(), record.getInviteeUserId(), activity.getInviteeCouponTemplateId());
|
|
|
+
|
|
|
+ // 9. 发送通知给被邀请人(可选)
|
|
|
+ try {
|
|
|
+ CouponTemplate couponTemplate = couponTemplateService.getById(activity.getInviteeCouponTemplateId());
|
|
|
+ if (couponTemplate != null) {
|
|
|
+ notificationService.sendRewardSuccessNotification(
|
|
|
+ record.getInviteeUserId(),
|
|
|
+ couponTemplate.getCouponName(),
|
|
|
+ couponTemplate.getDiscountValue()
|
|
|
+ );
|
|
|
+ }
|
|
|
+ } catch (Exception notifyEx) {
|
|
|
+ log.error("发送被邀请人奖励通知失败(不影响业务): recordId={}, error={}",
|
|
|
+ recordId, notifyEx.getMessage(), notifyEx);
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 10. 发券失败时记录到 InviteReward 表(status=失败, 失败原因),不抛出异常
|
|
|
+ log.error("向被邀请人发放优惠券失败: recordId={}, inviteeUserId={}, error={}",
|
|
|
+ recordId, record.getInviteeUserId(), e.getMessage(), e);
|
|
|
+ inviteRewardMapper.updateToFailed(inviteeReward.getId(), "被邀请人优惠券发放失败: " + e.getMessage());
|
|
|
return false;
|
|
|
}
|
|
|
}
|