ソースを参照

邀请代码优化

zhangchen 1 ヶ月 前
コミット
99dc87ab2d

+ 4 - 0
alien-entity/src/main/java/shop/alien/entity/store/ActivityInviteLog.java

@@ -68,4 +68,8 @@ public class ActivityInviteLog {
     @TableField("delete_flag")
     @TableLogic
     private Integer deleteFlag;
+
+    @ApiModelProperty(value = "发放状态, 0:未发放, 1:已发放")
+    @TableField("distribution_status")
+    private Integer distributionStatus;
 }

+ 2 - 0
alien-store/src/main/java/shop/alien/store/service/ActivityInviteConfigService.java

@@ -43,4 +43,6 @@ public interface ActivityInviteConfigService extends IService<ActivityInviteConf
 
     String bindInviteCode(Integer invitedUserId, String inviteCode);
 
+    boolean distributionInviteReward(String invitedUserId, int inviteCondition);
+
 }

+ 79 - 10
alien-store/src/main/java/shop/alien/store/service/impl/ActivityInviteConfigServiceImpl.java

@@ -3,6 +3,7 @@ package shop.alien.store.service.impl;
 import com.alibaba.nacos.common.utils.CollectionUtils;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import lombok.RequiredArgsConstructor;
@@ -30,6 +31,8 @@ import java.time.ZoneId;
 import java.util.Collections;
 import java.util.Date;
 import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
 
 @Slf4j
 @Service
@@ -283,18 +286,19 @@ public class ActivityInviteConfigServiceImpl extends ServiceImpl<ActivityInviteC
                    activityInviteLog.setInvitedRewardCoupon(activityInviteConfig.getInvitedRewardCoupon());
                    activityInviteLog.setInvitedRewardPoint(activityInviteConfig.getInvitedRewardPoint());
                    activityInviteLog.setInviteTime(Date.from(Instant.now()));
+                   activityInviteLog.setDistributionStatus(0);
                    activityInviteLogMapper.insert(activityInviteLog);
 
-                   if(activityInviteConfig.getInviteRewardType() == 2){
-                       userPointService.addPoint(inviteUserId, activityInviteConfig.getInviteRewardPoint());
-                   } else if(activityInviteConfig.getInviteRewardType() == 1 && activityInviteConfig.getInviteRewardCoupon() > 0){
-                       lifeDiscountCouponService.issuePlatformCoupon(Collections.singletonList(inviteUserId), activityInviteConfig.getInviteRewardCoupon());
-                   }
-                   if(activityInviteConfig.getInvitedRewardType() == 2){
-                       userPointService.addPoint(invitedUserId, activityInviteConfig.getInvitedRewardPoint());
-                   } else if (activityInviteConfig.getInvitedRewardType() == 1 && activityInviteConfig.getInvitedRewardCoupon() > 0) {
-                       lifeDiscountCouponService.issuePlatformCoupon(Collections.singletonList(invitedUserId), activityInviteConfig.getInvitedRewardCoupon());
-                   }
+//                   if(activityInviteConfig.getInviteRewardType() == 2){
+//                       userPointService.addPoint(inviteUserId, activityInviteConfig.getInviteRewardPoint());
+//                   } else if(activityInviteConfig.getInviteRewardType() == 1 && activityInviteConfig.getInviteRewardCoupon() > 0){
+//                       lifeDiscountCouponService.issuePlatformCoupon(Collections.singletonList(inviteUserId), activityInviteConfig.getInviteRewardCoupon());
+//                   }
+//                   if(activityInviteConfig.getInvitedRewardType() == 2){
+//                       userPointService.addPoint(invitedUserId, activityInviteConfig.getInvitedRewardPoint());
+//                   } else if (activityInviteConfig.getInvitedRewardType() == 1 && activityInviteConfig.getInvitedRewardCoupon() > 0) {
+//                       lifeDiscountCouponService.issuePlatformCoupon(Collections.singletonList(invitedUserId), activityInviteConfig.getInvitedRewardCoupon());
+//                   }
 
                    LifeUser updateLifeUser = new LifeUser();
                    updateLifeUser.setId(invitedLifeUser.getId());
@@ -308,4 +312,69 @@ public class ActivityInviteConfigServiceImpl extends ServiceImpl<ActivityInviteC
         }
         return "参数异常";
     }
+
+    public boolean distributionInviteReward(String invitedUserId, int inviteCondition){
+
+        LambdaQueryWrapper<ActivityInviteLog> activityInviteLogLambdaQueryWrapper = new LambdaQueryWrapper<>();
+        activityInviteLogLambdaQueryWrapper.eq(ActivityInviteLog::getInvitedUserId, invitedUserId);
+        activityInviteLogLambdaQueryWrapper.eq(ActivityInviteLog::getDeleteFlag, 0);
+        activityInviteLogLambdaQueryWrapper.eq(ActivityInviteLog::getDistributionStatus, 0);
+
+        // 未发放的邀请记录
+        List<ActivityInviteLog> activityInviteLogList = activityInviteLogMapper.selectList(activityInviteLogLambdaQueryWrapper);
+        if(CollectionUtils.isEmpty(activityInviteLogList)){
+            return false;
+        }
+
+        List<Integer> logIdList = activityInviteLogList.stream()
+                .map(ActivityInviteLog::getId)
+                .collect(Collectors.toList());
+        if(CollectionUtils.isEmpty(logIdList)){
+            return false;
+        }
+
+        // 邀请记录对应的活动
+        List<Integer> activityIdList = activityInviteLogList.stream()
+                .map(ActivityInviteLog::getActivityId)
+                .collect(Collectors.toList());
+        if(CollectionUtils.isEmpty(activityIdList)){
+            return false;
+        }
+        List<ActivityInviteConfig> activityInviteConfigList =  activityInviteConfigMapper.selectBatchIds(activityIdList);
+        if(CollectionUtils.isEmpty(activityInviteConfigList)){
+            return false;
+        }
+
+        Map<Integer, Integer> inviteConditionMap = activityInviteConfigList.stream()
+                .collect(Collectors.toMap(
+                        ActivityInviteConfig::getId,
+                        ActivityInviteConfig::getInviteCondition
+                ));
+
+        for(ActivityInviteLog activityInviteLog : activityInviteLogList){
+            Integer activityId = activityInviteLog.getActivityId();
+            if(inviteConditionMap.containsKey(activityId)){
+                Integer inviteConditionValue = inviteConditionMap.get(activityId);
+                if(inviteConditionValue != null && !inviteConditionValue.equals(inviteCondition)){
+                    continue;
+                }
+               if(activityInviteLog.getInviteRewardType() == 2){
+                   userPointService.addPoint(activityInviteLog.getInviteUserId(), activityInviteLog.getInviteRewardPoint());
+               } else if(activityInviteLog.getInviteRewardType() == 1 && activityInviteLog.getInviteRewardCoupon() > 0){
+                   lifeDiscountCouponService.issuePlatformCoupon(Collections.singletonList(activityInviteLog.getInviteUserId()), activityInviteLog.getInviteRewardCoupon());
+               }
+               if(activityInviteLog.getInvitedRewardType() == 2){
+                   userPointService.addPoint(activityInviteLog.getInvitedUserId(), activityInviteLog.getInvitedRewardPoint());
+               } else if (activityInviteLog.getInvitedRewardType() == 1 && activityInviteLog.getInvitedRewardCoupon() > 0) {
+                   lifeDiscountCouponService.issuePlatformCoupon(Collections.singletonList(activityInviteLog.getInvitedUserId()), activityInviteLog.getInvitedRewardCoupon());
+               }
+
+            }
+        }
+        LambdaUpdateWrapper<ActivityInviteLog> activityInviteLogLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
+        activityInviteLogLambdaUpdateWrapper.in(ActivityInviteLog::getId, logIdList);
+        activityInviteLogLambdaUpdateWrapper.set(ActivityInviteLog::getDistributionStatus, 1);
+        activityInviteLogMapper.update(null, activityInviteLogLambdaUpdateWrapper);
+        return true;
+    }
 }

+ 18 - 0
alien-store/src/main/java/shop/alien/store/service/impl/LifeCouponServiceImpl.java

@@ -18,6 +18,7 @@ import shop.alien.entity.store.dto.LifeDiscountCouponStoreFriendDto;
 import shop.alien.entity.store.vo.LifeCouponStatusVo;
 import shop.alien.mapper.*;
 import shop.alien.store.config.BaseRedisService;
+import shop.alien.store.service.ActivityInviteConfigService;
 import shop.alien.store.service.LifeCouponService;
 import shop.alien.store.service.LifeDiscountCouponStoreFriendService;
 import shop.alien.util.common.UniqueRandomNumGenerator;
@@ -65,6 +66,8 @@ public class LifeCouponServiceImpl extends ServiceImpl<LifeCouponMapper, LifeCou
     private final BaseRedisService baseRedisService;
     private final LifeDiscountCouponMapper lifeDiscountCouponMapper;
 
+    private final ActivityInviteConfigService activityInviteConfigService;
+
     @Override
     public LifeCoupon addOrUpdateCoupon(LifeCoupon lifeCoupon) {
 
@@ -350,6 +353,21 @@ public class LifeCouponServiceImpl extends ServiceImpl<LifeCouponMapper, LifeCou
 
                 resultMap.put("code", "true");
                 resultMap.put("message", "核销成功");
+
+                // 如果用户首次支付并核销,则发放邀请奖励
+                try{
+                    String userId = lifeUserOrder.getUserId();
+                    LambdaQueryWrapper<LifeUserOrder> lifeUserOrderLambdaQueryWrapper = new LambdaQueryWrapper<>();
+                    lifeUserOrderLambdaQueryWrapper.eq(LifeUserOrder::getUserId, userId);
+                    lifeUserOrderLambdaQueryWrapper.eq(LifeUserOrder::getStatus, 7);
+                    int completeOrderCount = lifeUserOrderMapper.selectCount(lifeUserOrderLambdaQueryWrapper);
+                    if(completeOrderCount == 1){
+                        activityInviteConfigService.distributionInviteReward(userId, 3);
+                    }
+                } catch(Exception e1){
+                    log.error("发放奖励邀请失败");
+                }
+
             } else {
                 resultMap.put("code", "false");
                 resultMap.put("message", "核销失败");