分享 领取

This commit is contained in:
2026-01-27 16:22:56 +08:00
parent f2024d5be5
commit 52e8a209fb
6 changed files with 211 additions and 35 deletions

View File

@@ -311,6 +311,92 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
.eq(MkDistributionUser::getShopId, parent.getShopId()));
}
@Override
@GlobalTransactional
public void bindInviteUser(Long fromUserId, Long toUserId, Long shopId) throws CzgException, ValidateException {
ShopUser shopUser = shopUserService.getById(fromUserId);
AssertUtil.isNull(shopUser, "店铺用户不存在");
if (shopUser.getParentUserId() != null) {
throw new CzgException("店铺用户已绑定上级");
}
MkDistributionUser parent = getOne(QueryWrapper.create().eq(MkDistributionUser::getId, toUserId));
AssertUtil.isNull(parent, "邀请人不存在");
if (!parent.getShopId().equals(shopId)) {
throw new CzgException("邀请人不是本店铺的分销员");
}
if (parent.getId().equals(shopUser.getId())) {
throw new CzgException("不能绑定自己为上级");
}
if (parent.getStatus() == 9) {
throw new CzgException("绑定失败该邀请人分销身份已被取消");
}
ShopUser parentShopUser = shopUserService.getById(parent.getId());
AssertUtil.isNull(parentShopUser, "邀请人不存在");
if (parentShopUser.getParentUserId() != null && parentShopUser.getParentUserId().equals(shopUser.getId())) {
throw new CzgException("存在绑定关系,不可绑定");
}
if (parentShopUser.getGradeUserId() != null && parentShopUser.getGradeUserId().equals(shopUser.getId())) {
throw new CzgException("存在绑定关系,不可绑定");
}
//更新自己的上级
shopUser.setParentUserId(parentShopUser.getId());
shopUser.setGradeUserId(parentShopUser.getParentUserId());
//更新自己的下级 的上级的上级 为自己的上级
ShopUser upShopUser1 = new ShopUser();
upShopUser1.setParentUserId(parentShopUser.getId());
upShopUser1.setGradeUserId(parentShopUser.getParentUserId());
shopUserService.update(upShopUser1, QueryWrapper.create().eq(ShopUser::getId, shopUser.getId()));
if (shopUser.getParentUserId() != null) {
//更新自己的下级 的上级的上级 为自己的上级
ShopUser upShopUser = new ShopUser();
upShopUser.setGradeUserId(shopUser.getParentUserId());
shopUserService.update(upShopUser, QueryWrapper.create().eq(ShopUser::getParentUserId, shopUser.getId()));
}
MkDistributionUser newDistributionUser = new MkDistributionUser();
newDistributionUser.setId(parent.getId());
newDistributionUser.setInviteCount(parent.getInviteCount() + 1);
MkDistributionConfig mkDistributionConfig = mkDistributionConfigService.getOne(QueryWrapper.create()
.eq(MkDistributionConfig::getShopId, parent.getShopId()));
if ("自主申请".equals(parent.getOpeningMethod()) && parent.getStatus() == 0 && mkDistributionConfig != null) {
if (newDistributionUser.getInviteCount() >= mkDistributionConfig.getInviteCount()) {
ShopUser parentShopUser1 = new ShopUser();
parentShopUser1.setId(parentShopUser.getId());
parentShopUser1.upDistributionShop(shopId, 1);
shopUserService.updateById(parentShopUser1);
}
}
if (mkDistributionConfig != null && !"not_upgrade".equals(mkDistributionConfig.getUpgradeType()) && parent.getIsAssignLevel() == 0) {
if ("invite".equals(mkDistributionConfig.getUpgradeType())) {
if (mkDistributionConfig.getInviteConsume() == 1) {
long count = orderInfoService.count(QueryWrapper.create()
.eq(OrderInfo::getUserId, shopUser.getUserId())
.eq(OrderInfo::getShopId, parent.getShopId())
.eq(OrderInfo::getStatus, OrderStatusEnums.DONE.getCode()));
if (count < 1) {
throw new CzgException("绑定失败,被邀请人需要成功支付一笔订单");
}
}
MkDistributionLevelConfig levelConfig = levelConfigService.getOne(QueryWrapper.create()
.eq(MkDistributionLevelConfig::getShopId, parent.getShopId())
.le(MkDistributionLevelConfig::getInviteCount, newDistributionUser.getInviteCount())
.orderBy(MkDistributionLevelConfig::getId).desc().limit(1));
if (levelConfig != null) {
newDistributionUser.setDistributionLevelId(levelConfig.getId());
newDistributionUser.setDistributionLevelName(levelConfig.getName());
newDistributionUser.setStatus(1);
}
} else if ("cost".equals(mkDistributionConfig.getUpgradeType())) {
costUpgradeLevel(parent.getId(), parent.getShopId());
}
}
update(newDistributionUser, QueryWrapper.create().eq(MkDistributionUser::getId, parent.getId())
.eq(MkDistributionUser::getShopId, parent.getShopId()));
}
@Override
public void costUpgradeLevelBefore(Long userId, Long shopId) {
ShopUser shopUser = shopUserService.getShopUserInfo(shopId, userId);

View File

@@ -1,7 +1,13 @@
package com.czg.service.market.service.impl;
import cn.hutool.core.util.StrUtil;
import com.czg.market.dto.MkShopCouponGiftDTO;
import com.czg.market.dto.ShopCouponDTO;
import com.czg.market.entity.MkShopCouponRecord;
import com.czg.market.service.MkDistributionUserService;
import com.czg.market.service.MkShopCouponRecordService;
import com.czg.market.service.ShopCouponService;
import com.czg.utils.FunUtils;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.market.entity.MkShareBase;
import com.czg.market.service.MkShareBaseService;
@@ -20,6 +26,10 @@ public class MkShareBaseServiceImpl extends ServiceImpl<MkShareBaseMapper, MkSha
@Resource
private ShopCouponService shopCouponService;
@Resource
private MkDistributionUserService distributionUserService;
@Resource
private MkShopCouponRecordService mkShopCouponRecordService;
@Override
public MkShareBase getShareBase(Long shopId) {
@@ -40,4 +50,78 @@ public class MkShareBaseServiceImpl extends ServiceImpl<MkShareBaseMapper, MkSha
}
return shareBase;
}
@Override
public void shareClaim(String tagType, Long shopId, Long fromUserId, Long toUserId) {
//获取邀请码
String inviteCode = null;
try {
inviteCode = distributionUserService.getInviteCode(shopId, fromUserId);
} catch (Exception _) {
}
//绑定上下级
if (StrUtil.isNotBlank(inviteCode)) {
FunUtils.asyncSafeRunVoid(() -> distributionUserService.bindInviteUser(fromUserId, toUserId, shopId));
}
MkShareBase shareBase = getById(shopId);
if (shareBase == null || shareBase.getIsEnabled().equals(1) || StrUtil.isBlank(shareBase.getRewardSharePages())) {
return;
}
if (!shareBase.getRewardSharePages().contains(tagType)) {
return;
}
FunUtils.safeRun(() -> {
//发放分享人优惠券
if (shareBase.getSharerCouponId() != null) {
boolean grant = true;
if (shareBase.getRewardTimesType().equals(1)) {
boolean exists = mkShopCouponRecordService.exists(query()
.eq(MkShopCouponRecord::getShopId, shopId)
.eq(MkShopCouponRecord::getShopUserId, fromUserId)
.eq(MkShopCouponRecord::getSource, "邀请获得"));
grant = !exists;
}
if (grant) {
MkShopCouponGiftDTO giftDTO = new MkShopCouponGiftDTO();
giftDTO.setShopId(shopId)
.setSourceId(shareBase.getShopId())
.setSourceFlowId(toUserId)
.setShopUserId(fromUserId)
.setCouponId(shareBase.getSharerCouponId())
.setSource("邀请获得");
int receiveNum = 1;
if (shareBase.getSharerCouponNum() != null && shareBase.getSharerCouponNum() > 0) {
receiveNum = shareBase.getSharerCouponNum();
}
mkShopCouponRecordService.receiveCoupon(giftDTO, receiveNum, false);
}
}
return null;
}, "邀请人获取优惠券");
FunUtils.safeRun(() -> {
//发放被分享人优惠券
if (shareBase.getSharedUserCouponId() != null) {
boolean exists = mkShopCouponRecordService.exists(query()
.eq(MkShopCouponRecord::getShopId, shopId)
.eq(MkShopCouponRecord::getShopUserId, toUserId)
.eq(MkShopCouponRecord::getSourceFlowId, fromUserId)
.eq(MkShopCouponRecord::getSource, "参与分享获得"));
if (!exists) {
MkShopCouponGiftDTO giftDTO = new MkShopCouponGiftDTO();
giftDTO.setShopId(shopId)
.setSourceId(shareBase.getShopId())
.setShopUserId(toUserId)
.setSourceFlowId(fromUserId)
.setCouponId(shareBase.getSharedUserCouponId())
.setSource("参与分享获得");
int receiveNum = 1;
if (shareBase.getSharedUserCouponNum() != null && shareBase.getSharedUserCouponNum() > 0) {
receiveNum = shareBase.getSharedUserCouponNum();
}
mkShopCouponRecordService.receiveCoupon(giftDTO, receiveNum, false);
}
}
return null;
}, "被分享人获取优惠券");
}
}