分享 领取

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

@@ -1,15 +1,13 @@
package com.czg.controller.user;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.market.dto.MkCarouselDTO;
import com.czg.market.entity.MkCarousel;
import com.czg.market.service.MkCarouselService;
import com.czg.market.service.MkShareBaseService;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@@ -20,19 +18,44 @@ import java.util.List;
* @author ww
*/
@RestController
@RequestMapping("/user/carousel")
@RequestMapping("/user")
public class UCarouselController {
@Resource
private MkCarouselService mkCarouselService;
@Resource
private MkShareBaseService mkShareBaseService;
/**
* 轮播图配置
*/
@GetMapping
@SaAdminCheckPermission(parentName = "轮播图配置", value = "carousel:config", name = "轮播图-列表")
@GetMapping("/carousel")
public CzgResult<List<MkCarousel>> getCarousels(MkCarouselDTO carouselDTO) {
carouselDTO.setShopId(StpKit.USER.getShopId());
return CzgResult.success(mkCarouselService.getCarousels(carouselDTO));
}
/**
* 分享 领取触发
*
* @param tagType
* @param shopId
* @param fromUserId 分享人的shopUserId
* @param toUserId 被分享人的shopUserId
*/
public record ShareClaim(String tagType, Long shopId, Long fromUserId, Long toUserId) {
}
/**
* 分享 领取触发
* 会绑定上下级关系
*/
@PostMapping("/shareClaim")
public CzgResult<Void> shareClaim(@RequestBody ShareClaim shareClaim) {
mkShareBaseService.shareClaim(shareClaim.tagType, shareClaim.shopId, shareClaim.fromUserId, shareClaim.toUserId);
return CzgResult.success();
}
}

View File

@@ -19,7 +19,6 @@ import com.mybatisflex.core.paginate.Page;
import io.seata.core.exception.TransactionException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.NestedExceptionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@@ -107,19 +106,6 @@ public class UDistributionController {
return CzgResult.success(distributionUserService.getInviteCode(shopId, shopUserId));
}
/**
* 分销员中心-获取邀请码
*/
@GetMapping("/autoGetInviteCode")
public CzgResult<String> autoGetInviteCode(@RequestParam Long shopId, @RequestParam Long shopUserId) {
try {
return CzgResult.success(distributionUserService.getInviteCode(shopId, shopUserId));
} catch (Exception e) {
String rootMsg = NestedExceptionUtils.getMostSpecificCause(e).getMessage();
log.error("获取邀请码失败用户:{}:{}", shopUserId, rootMsg);
return CzgResult.success("");
}
}
/**
* 分销员中心-实名认证
@@ -133,20 +119,6 @@ public class UDistributionController {
return CzgResult.success();
}
/**
* 分销员中心-无感-绑定邀请人
*/
@PostMapping("/autoBindInviteUser")
public CzgResult<Map<String, Object>> autoBindInviteUser(@RequestBody MkDistributionUserDTO param) throws TransactionException {
try {
bindInviteUser(param);
} catch (Exception e) {
log.error("无感绑定邀请人失败:{}", param, e);
}
return CzgResult.success();
}
/**
* 分销员中心-绑定邀请人
*/

View File

@@ -60,9 +60,18 @@ public interface MkDistributionUserService extends IService<MkDistributionUser>
/**
* 分销员中心-绑定邀请人
* 通过邀请码
*/
void bindInviteUser(MkDistributionUserDTO param) throws CzgException, ValidateException;
/**
* 分销员中心-绑定邀请人
* 通过邀请人id
* @param fromUserId shopUserId 邀请人
* @param toUserId 被邀请人邀请人
*/
void bindInviteUser(Long fromUserId, Long toUserId, Long shopId) throws CzgException, ValidateException;
/**
* 获取分销员分页列表
*/

View File

@@ -12,4 +12,6 @@ import com.czg.market.entity.MkShareBase;
public interface MkShareBaseService extends IService<MkShareBase> {
MkShareBase getShareBase(Long shopId);
void shareClaim(String tagType, Long shopId, Long fromUserId, Long toUserId);
}

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;
}, "被分享人获取优惠券");
}
}