|
@@ -3,18 +3,19 @@ package com.xs.core.service.Impl.team;
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.util.ObjUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.xs.core.common.enums.CoinTransactionCategoryEnum;
|
|
|
+import com.xs.core.common.enums.CoinTransactionTypeEnum;
|
|
|
+import com.xs.core.common.exception.BusinessException;
|
|
|
+import com.xs.core.common.validation.CheckUtils;
|
|
|
import com.xs.core.model.coin.msg.TeamShareMessage;
|
|
|
-import com.xs.core.model.team.entity.TeamInviteConfig;
|
|
|
-import com.xs.core.model.team.entity.TeamInviteRewardRecord;
|
|
|
-import com.xs.core.model.team.entity.TeamShareRewardRecord;
|
|
|
-import com.xs.core.model.team.resp.TeamUserResp;
|
|
|
-import com.xs.core.model.team.resp.TeamsInfoResp;
|
|
|
-import com.xs.core.model.user.entity.TgUser;
|
|
|
-import com.xs.core.service.team.ITeamInviteConfigService;
|
|
|
-import com.xs.core.service.team.ITeamInviteRewardRecordService;
|
|
|
-import com.xs.core.service.team.ITeamShareRewardRecordService;
|
|
|
-import com.xs.core.service.team.TeamShareService;
|
|
|
-import com.xs.core.service.user.ITgUserService;
|
|
|
+import com.xs.core.model.team.entity.*;
|
|
|
+import com.xs.core.model.team.req.TeamShareReq;
|
|
|
+import com.xs.core.model.team.resp.*;
|
|
|
+import com.xs.core.model.user.entity.User;
|
|
|
+import com.xs.core.service.team.*;
|
|
|
+import com.xs.core.service.user.IUserService;
|
|
|
+import com.xs.core.service.wallet.IUserWalletService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -25,13 +26,17 @@ import java.math.RoundingMode;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.Optional;
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
@Service
|
|
|
@Slf4j
|
|
|
public class TeamShareGoldCoinSettlementServiceImpl implements TeamShareService {
|
|
|
@Autowired
|
|
|
- private ITgUserService userService;
|
|
|
+ private IUserService userService;
|
|
|
|
|
|
@Autowired
|
|
|
private ITeamInviteConfigService teamInviteConfigService;
|
|
@@ -42,13 +47,22 @@ public class TeamShareGoldCoinSettlementServiceImpl implements TeamShareService
|
|
|
@Autowired
|
|
|
private ITeamShareRewardRecordService teamShareRewardRecordService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private IUserWalletService walletService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IInviteRewardsRuleService inviteRewardsRuleService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IInviteTaskRewardsFetchRecordService inviteRewardsFetchRecordService;
|
|
|
+
|
|
|
@Override
|
|
|
@Transactional
|
|
|
public void teamShareGoldCoinSettlement(TeamShareMessage teamShareMessage) {
|
|
|
Long userId = teamShareMessage.getUserId();
|
|
|
//获取用户的上一级邀请人 即a队
|
|
|
- TgUser inviteA = userService.getInviterByUserId(userId);
|
|
|
- TgUser inviteB = null;
|
|
|
+ User inviteA = userService.getInviterByUserId(userId);
|
|
|
+ User inviteB = null;
|
|
|
if (inviteA != null && StrUtil.isNotBlank(inviteA.getReferrerId())) {
|
|
|
//获取a队的上一级邀请人 即b队
|
|
|
inviteB = userService.getById(inviteA.getReferrerId());
|
|
@@ -104,29 +118,135 @@ public class TeamShareGoldCoinSettlementServiceImpl implements TeamShareService
|
|
|
|
|
|
@Override
|
|
|
public TeamsInfoResp getMyTeamsInfo(Long id) {
|
|
|
- List<TeamUserResp> teamUserList = getMyTeamUserList(id, null);
|
|
|
- TeamsInfoResp teamsInfoResp = new TeamsInfoResp();
|
|
|
- TeamInviteConfig config = teamInviteConfigService.getTeamInviteConfig();
|
|
|
- if (CollUtil.isNotEmpty(teamUserList)) {
|
|
|
- teamsInfoResp.setTotal(teamUserList.size());
|
|
|
- long countA = teamUserList.stream().filter(t -> "A".equals(t.getTeamType())).count();
|
|
|
- long countB = teamUserList.stream().filter(t -> "B".equals(t.getTeamType())).count();
|
|
|
- teamsInfoResp.setTeamACount(countA);
|
|
|
- teamsInfoResp.setTeamBCount(countB);
|
|
|
- } else {
|
|
|
- teamsInfoResp.setTotal(0);
|
|
|
- teamsInfoResp.setTeamACount(0L);
|
|
|
- teamsInfoResp.setTeamBCount(0L);
|
|
|
+ CompletableFuture<List<TeamUserResp>> teamUserListFuture = CompletableFuture.supplyAsync(() -> userService.getMyTeamUserList(id, null));
|
|
|
+ CompletableFuture<List<TeamShareRewardRecord>> shareRewardRecordListAFuture = CompletableFuture.supplyAsync(() -> teamShareRewardRecordService.getNoClaimTeamShareRewardRecordByType(id, "A"));
|
|
|
+ CompletableFuture<List<TeamShareRewardRecord>> shareRewardRecordListBFuture = CompletableFuture.supplyAsync(() -> teamShareRewardRecordService.getNoClaimTeamShareRewardRecordByType(id, "B"));
|
|
|
+ CompletableFuture<TeamInviteConfig> configFuture = CompletableFuture.supplyAsync(() -> teamInviteConfigService.getTeamInviteConfig());
|
|
|
+ CompletableFuture<List<TeamInviteRewardsResp>> teamInviteRewardRecordListFuture = CompletableFuture.supplyAsync(() -> teamInviteRewardRecordService.getTeamInviteRewardRecordList(id));
|
|
|
+ CompletableFuture.allOf(teamUserListFuture, shareRewardRecordListAFuture, shareRewardRecordListBFuture, configFuture, teamInviteRewardRecordListFuture);
|
|
|
+ return new TeamsInfoResp(teamInviteRewardRecordListFuture.join(), teamUserListFuture.join(), shareRewardRecordListAFuture.join(), shareRewardRecordListBFuture.join(), configFuture.join());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public TeamShareInfoResp getTeamShareUserInfoByType(Long userId, TeamShareReq req) {
|
|
|
+ //获取团队用户信息
|
|
|
+ List<TeamShareUserResp> teamShareUserRespList = teamShareRewardRecordService.getTeamShareUserInfoByType(userId, req.getType());
|
|
|
+ if (CollUtil.isNotEmpty(teamShareUserRespList)) {
|
|
|
+ TeamShareInfoResp teamShareInfoResp = new TeamShareInfoResp();
|
|
|
+ teamShareInfoResp.setUsers(teamShareUserRespList);
|
|
|
+ BigDecimal totalTeamRewardAmount = teamShareUserRespList.stream()
|
|
|
+ .map(TeamShareUserResp::getTeamRewardAmount)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+ teamShareInfoResp.setTotal(totalTeamRewardAmount.setScale(2, RoundingMode.HALF_UP));
|
|
|
+ return teamShareInfoResp;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void claimTeamShareReward(Long id, TeamShareReq req) {
|
|
|
+ List<TeamShareRewardRecord> noClaimTeamShareRewardRecordList = teamShareRewardRecordService.getNoClaimTeamShareRewardRecordByType(id, req.getType());
|
|
|
+ CheckUtils.throwIfEmpty(noClaimTeamShareRewardRecordList, "error.team.share.reward.empty");
|
|
|
+ if (CollUtil.isNotEmpty(noClaimTeamShareRewardRecordList)) {
|
|
|
+ //计算总奖励
|
|
|
+ BigDecimal totalRewardAmount = noClaimTeamShareRewardRecordList.stream()
|
|
|
+ .map(TeamShareRewardRecord::getRewardAmount)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+ //更新用户金币
|
|
|
+ walletService.coinTransaction(CoinTransactionTypeEnum.ADD, CoinTransactionCategoryEnum.TEAM_SHARING, "团队产出收益", "团队产出收益", totalRewardAmount, id);
|
|
|
+ //更新为已领取
|
|
|
+ noClaimTeamShareRewardRecordList.forEach(t -> {
|
|
|
+ t.setClaimStatus(1);
|
|
|
+ t.setClaimTime(LocalDateTime.now());
|
|
|
+ });
|
|
|
+ teamShareRewardRecordService.updateBatchById(noClaimTeamShareRewardRecordList);
|
|
|
}
|
|
|
- if (ObjUtil.isNotNull(config)) {
|
|
|
- teamsInfoResp.setProportionA(config.getTeamAScale());
|
|
|
- teamsInfoResp.setProportionB(config.getTeamBScale());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<TeamInviteRewardsResp> getTeamInviteRewardsList(Long id) {
|
|
|
+ //获取团队邀请奖励记录
|
|
|
+ return teamInviteRewardRecordService.getTeamInviteRewardRecordList(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public TeamInviteAvailableRewardsResp getTeamInviteAvailableRewardsRules(Long id) {
|
|
|
+ //获取团队邀请奖励规则
|
|
|
+ List<InviteRewardsRule> allRules = inviteRewardsRuleService.getAllByOrderByRewardsLevelAsc();
|
|
|
+ InviteTaskRewardsFetchRecord userRewards = inviteRewardsFetchRecordService.getLastByUserId(id);
|
|
|
+ //查询用户邀请的人数
|
|
|
+ Long inviterCount = userService.queryInviterCount(id);
|
|
|
+ List<InviteRewardsRule> collect = allRules.stream()
|
|
|
+ .filter(rule -> rule.getRewardsLevel() > (ObjUtil.isNotNull(userRewards.getRewardsLevel()) ? userRewards.getRewardsLevel() : 0))
|
|
|
+ .toList();
|
|
|
+ return new TeamInviteAvailableRewardsResp(inviterCount, collect);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void claimTeamInviteReward(Long id) {
|
|
|
+ //获取团队邀请奖励记录
|
|
|
+ LambdaQueryWrapper<TeamInviteRewardRecord> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(TeamInviteRewardRecord::getReceiverId, id);
|
|
|
+ queryWrapper.eq(TeamInviteRewardRecord::getClaimStatus, 0);
|
|
|
+ List<TeamInviteRewardRecord> list = teamInviteRewardRecordService.list(queryWrapper);
|
|
|
+ CheckUtils.throwIfEmpty(list, "error.team.invite.reward.empty");
|
|
|
+ if (CollUtil.isNotEmpty(list)) {
|
|
|
+ //计算总奖励
|
|
|
+ BigDecimal totalRewardAmount = list.stream()
|
|
|
+ .map(TeamInviteRewardRecord::getRewardAmount)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+ //更新用户金币
|
|
|
+ walletService.coinTransaction(CoinTransactionTypeEnum.ADD, CoinTransactionCategoryEnum.SHARE_REWARDS, "团队邀请收益", "团队邀请收益,邀请一人奖励1000", totalRewardAmount, id);
|
|
|
+ //更新为已领取
|
|
|
+ list.forEach(t -> {
|
|
|
+ t.setClaimStatus(1);
|
|
|
+ t.setClaimTime(LocalDateTime.now());
|
|
|
+ });
|
|
|
+ teamInviteRewardRecordService.updateBatchById(list);
|
|
|
}
|
|
|
- return teamsInfoResp;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<TeamUserResp> getMyTeamUserList(Long userId, String teamType) {
|
|
|
- return userService.getMyTeamUserList(userId, teamType);
|
|
|
+ @Transactional
|
|
|
+ public void claimTeamInviteTaskReward(Long id, String ruleId) {
|
|
|
+ //查询用户邀请的人数
|
|
|
+ Long inviterCount = userService.queryInviterCount(id);
|
|
|
+ InviteTaskRewardsFetchRecord exist = inviteRewardsFetchRecordService.getByRuleId(ruleId);
|
|
|
+ CheckUtils.throwIf(ObjUtil.isNotNull(exist), "error.team.invite.reward.task.claim.exists");
|
|
|
+ List<InviteRewardsRule> allRules = inviteRewardsRuleService.getAllByOrderByRewardsLevelAsc();
|
|
|
+ //查询待领取规则
|
|
|
+ InviteRewardsRule claimRule = inviteRewardsRuleService.getById(ruleId);
|
|
|
+ CheckUtils.throwIf(ObjUtil.isNull(claimRule), "error.team.invite.reward.task.claim.not.found");
|
|
|
+ CheckUtils.throwIf(inviterCount < claimRule.getInviteNum(), "error.team.invite.reward.task.claim.not.enough");
|
|
|
+ //查询用户已领取的奖励等级
|
|
|
+ InviteTaskRewardsFetchRecord userRewards = Optional.ofNullable(inviteRewardsFetchRecordService.getLastByUserId(id)).orElse(new InviteTaskRewardsFetchRecord(0, 0));
|
|
|
+ int nextIndex = 0;
|
|
|
+ //按顺序遍历
|
|
|
+ for (int i = 0; i < allRules.size(); i++) {
|
|
|
+ InviteRewardsRule rule = allRules.get(i);
|
|
|
+ if (rule.getId().equals(userRewards.getId())) {
|
|
|
+ //找到下一等级待领取的规则
|
|
|
+ nextIndex = i + 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ CheckUtils.throwIf(nextIndex >= allRules.size(), "error.team.invite.reward.task.claim.done");
|
|
|
+ InviteRewardsRule nextRule = allRules.get(nextIndex);
|
|
|
+ CheckUtils.throwIf(claimRule.getRewardsLevel() > nextRule.getRewardsLevel(), "error.team.invite.reward.task.claim.previous.not.claim");
|
|
|
+
|
|
|
+ //添加领取记录
|
|
|
+ InviteTaskRewardsFetchRecord record = new InviteTaskRewardsFetchRecord();
|
|
|
+ record.setUserId(id);
|
|
|
+ record.setRewardsLevel(claimRule.getRewardsLevel());
|
|
|
+ record.setCreatedTime(LocalDateTime.now());
|
|
|
+ record.setRuleId(claimRule.getId());
|
|
|
+ record.setGoldCoinNum(claimRule.getGoldCoinNum());
|
|
|
+ inviteRewardsFetchRecordService.save(record);
|
|
|
+ //更新用户金币
|
|
|
+ walletService.coinTransaction(CoinTransactionTypeEnum.ADD, CoinTransactionCategoryEnum.SHARE_REWARDS, "团队邀请任务奖励", "团队邀请任务奖励", new BigDecimal(claimRule.getGoldCoinNum()), id);
|
|
|
}
|
|
|
}
|