From d550a51bb832c158899a437923a7a6456474a2d8 Mon Sep 17 00:00:00 2001 From: tianfeng <769204422@qq.com> Date: Tue, 21 Oct 2025 18:55:11 +0800 Subject: [PATCH] =?UTF-8?q?=E9=82=80=E8=AF=B7=E7=BB=91=E5=AE=9A=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E6=8E=A5=E5=8F=A3=E6=96=B0=E5=A2=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../InviteUserActivityRestController.java | 34 +++++++- .../command/activity/BindInviteCodeExe.java | 82 +++++++++++++++++++ .../command/activity/GetMyInviteCodeExe.java | 70 ++++++++++++++++ .../InviteUserActivityServiceImpl.java | 16 ++++ .../clientobject/activity/InviteCodeCO.java | 44 ++++++++++ .../dto/cmd/activity/BindInviteCodeCmd.java | 21 +++++ .../activity/InviteUserActivityService.java | 12 +++ 7 files changed, 277 insertions(+), 2 deletions(-) create mode 100644 rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/BindInviteCodeExe.java create mode 100644 rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/GetMyInviteCodeExe.java create mode 100644 rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/clientobject/activity/InviteCodeCO.java create mode 100644 rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/cmd/activity/BindInviteCodeCmd.java diff --git a/rc-service/rc-service-other/other-adapter/src/main/java/com/red/circle/other/adapter/app/activity/InviteUserActivityRestController.java b/rc-service/rc-service-other/other-adapter/src/main/java/com/red/circle/other/adapter/app/activity/InviteUserActivityRestController.java index 0bc352bd..0ad4b3c2 100644 --- a/rc-service/rc-service-other/other-adapter/src/main/java/com/red/circle/other/adapter/app/activity/InviteUserActivityRestController.java +++ b/rc-service/rc-service-other/other-adapter/src/main/java/com/red/circle/other/adapter/app/activity/InviteUserActivityRestController.java @@ -2,8 +2,6 @@ package com.red.circle.other.adapter.app.activity; import com.red.circle.common.business.dto.cmd.AppExtCommand; import com.red.circle.common.business.dto.cmd.app.AppIdLongCmd; -import com.red.circle.framework.core.asserts.ResponseAssert; -import com.red.circle.framework.core.response.CommonErrorCode; import com.red.circle.other.app.dto.clientobject.activity.ActivityUserRewardPropsCO; import com.red.circle.other.app.dto.clientobject.activity.InviteActivityCO; import com.red.circle.other.app.dto.clientobject.activity.InviteActivityCO.InviteActivityInfoCO; @@ -12,6 +10,12 @@ import com.red.circle.other.app.dto.clientobject.activity.InviteUserCO; import com.red.circle.other.app.dto.clientobject.activity.InviteUserCommissionDetailsCO; import com.red.circle.other.app.dto.cmd.activity.InviteUserActivityLimitCmd; import com.red.circle.other.app.service.activity.InviteUserActivityService; +import com.red.circle.other.app.dto.cmd.activity.BindInviteCodeCmd; +import com.red.circle.other.app.dto.clientobject.activity.InviteCodeCO; + +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import java.util.List; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -142,5 +146,31 @@ public class InviteUserActivityRestController { // ResponseAssert.failure(CommonErrorCode.API_DEPRECATED); inviteUserActivityService.receiveAward(cmd); } + /** + * 获取我的邀请码. + * + * @eo.name 获取我的邀请码 + * @eo.url /my/invite/code + * @eo.method get + * @eo.request-type formdata + */ + @GetMapping("/my/invite/code") + public InviteCodeCO getMyInviteCode(AppExtCommand cmd) { + InviteCodeCO inviteCode = inviteUserActivityService.getMyInviteCode(cmd); + return inviteCode; + } + + /** + * 绑定邀请码. + * + * @eo.name 绑定邀请码 + * @eo.url /bind/invite/code + * @eo.method post + * @eo.request-type json + */ + @PostMapping("/bind/invite/code") + public void bindInviteCode(@Validated @RequestBody BindInviteCodeCmd cmd) { + inviteUserActivityService.bindInviteCode(cmd); + } } diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/BindInviteCodeExe.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/BindInviteCodeExe.java new file mode 100644 index 00000000..c2317000 --- /dev/null +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/BindInviteCodeExe.java @@ -0,0 +1,82 @@ +package com.red.circle.other.app.command.activity; + +import com.red.circle.other.app.dto.cmd.activity.BindInviteCodeCmd; +import com.red.circle.other.domain.gateway.user.UserProfileGateway; +import com.red.circle.other.domain.model.user.UserProfile; +import com.red.circle.other.infra.database.rds.entity.user.user.UserInviteUser; +import com.red.circle.other.infra.database.rds.service.user.user.UserInviteUserService; +import com.red.circle.tool.core.date.TimestampUtils; +import com.red.circle.tool.core.sequence.IdWorkerUtils; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.Objects; + +/** + * 绑定邀请码执行器 + * + * @author system + * @date 2025-01-21 + */ +@Slf4j +@Component +@RequiredArgsConstructor +public class BindInviteCodeExe { + + private final UserProfileGateway userProfileGateway; + private final UserInviteUserService userInviteUserService; + + /** + * 执行绑定邀请码 + * + * @param cmd 绑定命令 + */ + @Transactional(rollbackFor = Exception.class) + public void execute(BindInviteCodeCmd cmd) { + Long userId = cmd.getReqUserId(); + String inviteCode = cmd.getInviteCode(); + String sysOrigin = cmd.getReqSysOrigin().getOrigin(); + + log.info("开始绑定邀请码, userId={}, inviteCode={}", userId, inviteCode); + + // 1. 校验用户是否存在 + UserProfile userProfile = userProfileGateway.getByUserId(userId); + if (userProfile == null) { + throw new RuntimeException("The user does not exist."); + } + + // 2. 校验是否已绑定过邀请人 + List existingInvite = userInviteUserService.query().eq(UserInviteUser::getUserId, userId).list(); + if (existingInvite != null && !existingInvite.isEmpty()) { + throw new RuntimeException("The inviter has been bound before and cannot be bound again"); + } + + // 3. 根据邀请码查找邀请人(邀请码即为用户账号) + UserProfile inviterProfile = userProfileGateway.getByAccount(sysOrigin, inviteCode); + if (inviterProfile == null) { + throw new RuntimeException("The invitation code is invalid and the corresponding user was not found"); + } + + // 4. 校验不能邀请自己 + if (Objects.equals(userId, inviterProfile.getId())) { + throw new RuntimeException("You cannot bind your own invitation code"); + } + + // 5. 保存邀请关系 + UserInviteUser inviteUser = new UserInviteUser(); + inviteUser.setId(IdWorkerUtils.getId()); + inviteUser.setSysOrigin(sysOrigin); + inviteUser.setUserId(userId); + inviteUser.setInviteUserId(inviterProfile.getId()); + inviteUser.setCreateTime(TimestampUtils.now()); + inviteUser.setUpdateTime(TimestampUtils.now()); + + userInviteUserService.save(inviteUser); + + log.info("绑定邀请码成功, userId={}, inviterUserId={}, inviteCode={}", + userId, inviterProfile.getId(), inviteCode); + } +} diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/GetMyInviteCodeExe.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/GetMyInviteCodeExe.java new file mode 100644 index 00000000..c12f0ea3 --- /dev/null +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/GetMyInviteCodeExe.java @@ -0,0 +1,70 @@ +package com.red.circle.other.app.command.activity; + +import com.red.circle.common.business.dto.cmd.AppExtCommand; +import com.red.circle.other.app.dto.clientobject.activity.InviteCodeCO; +import com.red.circle.other.domain.gateway.user.UserProfileGateway; +import com.red.circle.other.domain.model.user.UserProfile; +import com.red.circle.other.infra.database.rds.dao.user.user.UserInviteUserDAO; +import com.red.circle.other.infra.database.rds.entity.user.user.UserInviteUser; +import com.red.circle.other.infra.database.rds.service.user.user.UserInviteUserService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * 获取我的邀请码执行器 + * + * @author system + * @date 2025-01-21 + */ +@Slf4j +@Component +@RequiredArgsConstructor +public class GetMyInviteCodeExe { + + private final UserProfileGateway userProfileGateway; + private final UserInviteUserService userInviteUserService; + + /** + * 执行获取我的邀请码 + * + * @param cmd 命令 + * @return 邀请码信息 + */ + public InviteCodeCO execute(AppExtCommand cmd) { + Long userId = cmd.getReqUserId(); + + // 获取用户信息 + UserProfile userProfile = userProfileGateway.getByUserId(userId); + if (userProfile == null) { + log.warn("用户不存在, userId={}", userId); + return null; + } + + InviteCodeCO inviteCodeCO = new InviteCodeCO(); + inviteCodeCO.setUserId(userId); + inviteCodeCO.setInviteCode(userProfile.getAccount()); // 使用用户账号作为邀请码 + + // 查询是否已绑定邀请人 + List inviteUserList = userInviteUserService.query().eq(UserInviteUser::getUserId, userId).list(); + + if (inviteUserList != null && !inviteUserList.isEmpty()) { + UserInviteUser inviteUser = inviteUserList.get(0); + inviteCodeCO.setHasBound(true); + inviteCodeCO.setInviterUserId(inviteUser.getInviteUserId()); + + // 获取邀请人信息 + UserProfile inviterProfile = userProfileGateway.getByUserId(inviteUser.getInviteUserId()); + if (inviterProfile != null) { + inviteCodeCO.setInviterAccount(inviterProfile.getAccount()); + } + } else { + inviteCodeCO.setHasBound(false); + } + + log.info("获取邀请码成功, userId={}, inviteCode={}", userId, inviteCodeCO.getInviteCode()); + return inviteCodeCO; + } +} diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/activity/InviteUserActivityServiceImpl.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/activity/InviteUserActivityServiceImpl.java index 51071506..153ff935 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/activity/InviteUserActivityServiceImpl.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/activity/InviteUserActivityServiceImpl.java @@ -16,6 +16,10 @@ import com.red.circle.other.app.dto.clientobject.activity.InviteActivityCO.Invit import com.red.circle.other.app.dto.clientobject.activity.InviteUserActivitySummaryCO; import com.red.circle.other.app.dto.clientobject.activity.InviteUserCO; import com.red.circle.other.app.dto.clientobject.activity.InviteUserCommissionDetailsCO; +import com.red.circle.other.app.command.activity.GetMyInviteCodeExe; +import com.red.circle.other.app.command.activity.BindInviteCodeExe; +import com.red.circle.other.app.dto.cmd.activity.BindInviteCodeCmd; +import com.red.circle.other.app.dto.clientobject.activity.InviteCodeCO; import com.red.circle.other.app.dto.cmd.activity.InviteUserActivityLimitCmd; import java.util.List; import lombok.RequiredArgsConstructor; @@ -41,6 +45,8 @@ public class InviteUserActivityServiceImpl implements InviteUserActivityService private final InviteUserCommissionDetailsExe inviteUserCommissionDetailsExe; private final InviteUserQuantityAwardQueryExe inviteUserQuantityAwardQueryExe; private final InviteActivityExtractedListCmdExe inviteActivityExtractedListCmdExe; + private final GetMyInviteCodeExe getMyInviteCodeExe; + private final BindInviteCodeExe bindInviteCodeExe; @Override public InviteUserActivitySummaryCO getSummary(AppExtCommand cmd) { @@ -82,4 +88,14 @@ public class InviteUserActivityServiceImpl implements InviteUserActivityService public List extractList(AppExtCommand cmd) { return inviteActivityExtractedListCmdExe.execute(cmd); } + + @Override + public InviteCodeCO getMyInviteCode(AppExtCommand cmd) { + return getMyInviteCodeExe.execute(cmd); + } + + @Override + public void bindInviteCode(BindInviteCodeCmd cmd) { + bindInviteCodeExe.execute(cmd); + } } diff --git a/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/clientobject/activity/InviteCodeCO.java b/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/clientobject/activity/InviteCodeCO.java new file mode 100644 index 00000000..6ff7f970 --- /dev/null +++ b/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/clientobject/activity/InviteCodeCO.java @@ -0,0 +1,44 @@ +package com.red.circle.other.app.dto.clientobject.activity; + +import lombok.Data; +import lombok.experimental.Accessors; + +import java.io.Serializable; + +/** + * 邀请码客户端对象 + * + * @author system + * @date 2025-01-21 + */ +@Data +@Accessors(chain = true) +public class InviteCodeCO implements Serializable { + + private static final long serialVersionUID = 1L; + + /** + * 用户ID + */ + private Long userId; + + /** + * 邀请码 + */ + private String inviteCode; + + /** + * 是否已绑定邀请人 + */ + private Boolean hasBound; + + /** + * 绑定的邀请人ID + */ + private Long inviterUserId; + + /** + * 绑定的邀请人账号 + */ + private String inviterAccount; +} diff --git a/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/cmd/activity/BindInviteCodeCmd.java b/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/cmd/activity/BindInviteCodeCmd.java new file mode 100644 index 00000000..3b0ad71d --- /dev/null +++ b/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/cmd/activity/BindInviteCodeCmd.java @@ -0,0 +1,21 @@ +package com.red.circle.other.app.dto.cmd.activity; + +import com.red.circle.common.business.dto.cmd.AppExtCommand; +import jakarta.validation.constraints.NotBlank; +import lombok.Data; +import lombok.EqualsAndHashCode; + + +/** + * 绑定邀请码命令 + * + * @author system + * @date 2025-01-21 + */ +@EqualsAndHashCode(callSuper = true) +@Data +public class BindInviteCodeCmd extends AppExtCommand { + + @NotBlank(message = "邀请码不能为空") + private String inviteCode; +} diff --git a/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/service/activity/InviteUserActivityService.java b/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/service/activity/InviteUserActivityService.java index 57acdbf6..31718fc1 100644 --- a/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/service/activity/InviteUserActivityService.java +++ b/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/service/activity/InviteUserActivityService.java @@ -10,6 +10,8 @@ import com.red.circle.other.app.dto.clientobject.activity.InviteUserActivitySumm import com.red.circle.other.app.dto.clientobject.activity.InviteUserCO; import com.red.circle.other.app.dto.clientobject.activity.InviteUserCommissionDetailsCO; import com.red.circle.other.app.dto.cmd.activity.InviteUserActivityLimitCmd; +import com.red.circle.other.app.dto.cmd.activity.BindInviteCodeCmd; +import com.red.circle.other.app.dto.clientobject.activity.InviteCodeCO; import java.util.List; /** @@ -58,4 +60,14 @@ public interface InviteUserActivityService { * 已提取列表. */ List extractList(AppExtCommand cmd); + + /** + * 获取我的邀请码. + */ + InviteCodeCO getMyInviteCode(AppExtCommand cmd); + + /** + * 绑定邀请码. + */ + void bindInviteCode(BindInviteCodeCmd cmd); }