1. 完成确认收货逻辑
This commit is contained in:
2026-03-20 17:50:52 +08:00
parent a51369ad85
commit 2d89f30925
12 changed files with 180 additions and 20 deletions

View File

@@ -0,0 +1,26 @@
package org.springblade.modules.common.eums;
import lombok.Getter;
@Getter
public enum UserCommissionFlowType {
EARNED_FROM_REFERRAL(1, "好友通过您的邀请下单,您已获得 ¥%s 推广奖励!佣金已到账,快去查看吧!"),
TEAM_SETTLEMENT_REWARD(1, "🎉 恭喜!您的团队本日达成目标,您获得 ¥%s 团队结算奖励,已发放至佣金账户!"),
WITHDRAW_SUCCESS(2, "您已成功提现 ¥%s 邀请奖励,款项将在1-3个工作日内到账,请注意查收!");
private final int code;
private final String userMessageTemplate;
UserCommissionFlowType(int code, String userMessageTemplate) {
this.code = code;
this.userMessageTemplate = userMessageTemplate;
}
public String format(String amount) {
return String.format(userMessageTemplate, amount);
}
}

View File

@@ -28,7 +28,7 @@ public enum UserPointFlowType {
private final int code; private final int code;
private final String userMessageTemplate; private final String userMessageTemplate;
private UserPointFlowType(int code, String userMessageTemplate) { UserPointFlowType(int code, String userMessageTemplate) {
this.code = code; this.code = code;
this.userMessageTemplate = userMessageTemplate; this.userMessageTemplate = userMessageTemplate;
} }

View File

@@ -0,0 +1,19 @@
package org.springblade.modules.common.event;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.math.BigDecimal;
@AllArgsConstructor
@Getter
public class EarnedCommissionEvent {
private final Long orderId;
private final Long userId;
private final BigDecimal commission;
}

View File

@@ -4,11 +4,9 @@ import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springblade.modules.common.eums.NoticeType; import org.springblade.modules.common.eums.NoticeType;
import org.springblade.modules.common.eums.OrderStatus; import org.springblade.modules.common.eums.OrderStatus;
import org.springblade.modules.common.eums.UserCommissionFlowType;
import org.springblade.modules.common.eums.UserPointFlowType; import org.springblade.modules.common.eums.UserPointFlowType;
import org.springblade.modules.common.event.FillExpressEvent; import org.springblade.modules.common.event.*;
import org.springblade.modules.common.event.GrantUserPointEvent;
import org.springblade.modules.common.event.PaymentSuccessEvent;
import org.springblade.modules.common.event.SpendUserPointEvent;
import org.springblade.modules.common.pojo.entity.CommonUserNoticeEntity; import org.springblade.modules.common.pojo.entity.CommonUserNoticeEntity;
import org.springblade.modules.common.service.ICommonUserNoticeService; import org.springblade.modules.common.service.ICommonUserNoticeService;
import org.springblade.modules.market.event.MarketUpgradeNoticeEvent; import org.springblade.modules.market.event.MarketUpgradeNoticeEvent;
@@ -144,6 +142,22 @@ public class NoticeListener {
@TransactionalEventListener(phase = TransactionPhase.AFTER_ROLLBACK) @TransactionalEventListener(phase = TransactionPhase.AFTER_ROLLBACK)
public void onFillExpressFailure(FillExpressEvent event) { public void onFillExpressFailure(FillExpressEvent event) {
log.warn("支付事务回滚,orderId={}", event.getOrderId()); log.warn("发货事务回滚,orderId={}", event.getOrderId());
}
@Async
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void onEarnedCommission(EarnedCommissionEvent event) {
CommonUserNoticeEntity notice = new CommonUserNoticeEntity();
notice.setUserId(event.getUserId());
notice.setNoticeType(NoticeType.MARKET_USER.getCode());
notice.setTitle("佣金通知");
notice.setContent(UserCommissionFlowType.EARNED_FROM_REFERRAL.format(String.valueOf(event.getCommission())));
noticeService.save(notice);
}
@TransactionalEventListener(phase = TransactionPhase.AFTER_ROLLBACK)
public void onEarnedCommissionFailure(EarnedCommissionEvent event) {
log.warn("奖励佣金事务回滚,orderId={}", event.getOrderId());
} }
} }

View File

@@ -31,7 +31,6 @@ import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springblade.common.utils.date.DateUtil; import org.springblade.common.utils.date.DateUtil;
import org.springblade.core.log.exception.ServiceException; import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.redis.debounce.RedisDebounce;
import org.springblade.core.secure.utils.AuthUtil; import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.api.request.ConfirmOrderRequest; import org.springblade.modules.api.request.ConfirmOrderRequest;
import org.springblade.modules.api.request.CreateOrderRequest; import org.springblade.modules.api.request.CreateOrderRequest;
@@ -55,6 +54,9 @@ import org.springblade.modules.common.service.ICommonUserOrderService;
import org.springblade.modules.common.service.ICommonUserPointService; import org.springblade.modules.common.service.ICommonUserPointService;
import org.springblade.modules.mall.pojo.entity.MallProductEntity; import org.springblade.modules.mall.pojo.entity.MallProductEntity;
import org.springblade.modules.mall.service.IMallProductService; import org.springblade.modules.mall.service.IMallProductService;
import org.springblade.modules.market.pojo.entity.MarketInfoEntity;
import org.springblade.modules.market.service.IMarketInfoService;
import org.springblade.modules.market.service.IMarketUserCommissionService;
import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.Wrapper;
@@ -85,6 +87,10 @@ public class CommonUserOrderServiceImpl extends ServiceImpl<CommonUserOrderMappe
private final ICommonUserPointService userPointService; private final ICommonUserPointService userPointService;
private final IMarketUserCommissionService commissionService;
private final IMarketInfoService marketInfoService;
@Override @Override
public IPage<CommonUserOrderVO> selectOrderPage(IPage<CommonUserOrderVO> page, CommonUserOrderVO order) { public IPage<CommonUserOrderVO> selectOrderPage(IPage<CommonUserOrderVO> page, CommonUserOrderVO order) {
@@ -150,8 +156,12 @@ public class CommonUserOrderServiceImpl extends ServiceImpl<CommonUserOrderMappe
@Override @Override
public PaymentResponse pay(PaymentRequest request) { public PaymentResponse pay(PaymentRequest request) {
CommonUserOrderEntity order = getById(Long.valueOf(request.getOrderId())); CommonUserOrderEntity order = getById(Long.valueOf(request.getOrderId()));
Assert.notNull(order, "无效订单"); if (order == null) {
Assert.isTrue(order.getPaymentType() != 99, "线下订单不能重复支付"); throw new ServiceException("无效订单");
}
if (order.getPaymentType().equals(PaymentType.OFFLINE.getCode())) {
throw new ServiceException("无效订单");
}
//todo: 继续支付逻辑 //todo: 继续支付逻辑
return null; return null;
} }
@@ -194,7 +204,6 @@ public class CommonUserOrderServiceImpl extends ServiceImpl<CommonUserOrderMappe
if (order == null) { if (order == null) {
throw new IllegalStateException("订单更新成功但查询不到,id=" + id); throw new IllegalStateException("订单更新成功但查询不到,id=" + id);
} }
// userPointService.grantPointsByUserOrder(order);
eventPublisher.publishEvent(new PaymentSuccessEvent(id, order.getUserId(), BusinessModule.SELF_OPERATED_MALL)); eventPublisher.publishEvent(new PaymentSuccessEvent(id, order.getUserId(), BusinessModule.SELF_OPERATED_MALL));
@@ -225,7 +234,30 @@ public class CommonUserOrderServiceImpl extends ServiceImpl<CommonUserOrderMappe
} }
@Override @Override
@Transactional(rollbackFor = Exception.class)
public boolean confirm(ConfirmOrderRequest request) { public boolean confirm(ConfirmOrderRequest request) {
return false; CommonUserOrderEntity order = getById(request.getId());
if (order == null) {
throw new ServiceException("无效订单");
}
boolean updated = update(Wrappers.<CommonUserOrderEntity>lambdaUpdate()
.set(CommonUserOrderEntity::getOrderStatus, OrderStatus.COMPLETED.getCode())
.eq(CommonUserOrderEntity::getId, order.getId())
.eq(CommonUserOrderEntity::getOrderStatus, OrderStatus.SHIPPED.getCode()));
if (!updated) {
throw new ServiceException("无效订单");
}
if (order.getPaymentType().equals(PaymentType.OFFLINE.getCode())) {
return true;
}
if (order.getOrderType().equalsIgnoreCase(BusinessModule.SELF_OPERATED_MALL.getCode())) {
userPointService.grantPointsByUserOrder(order);
MarketInfoEntity marketInfo = marketInfoService.getById(AuthUtil.getUserId());
Assert.notNull(marketInfo, "无效用户");
if (marketInfo.getPid() != null) {
commissionService.earnedCommissionByUserOrder(order, marketInfo.getPid());
}
}
return true;
} }
} }

View File

@@ -58,15 +58,9 @@ public class CommonUserPointServiceImpl extends BaseServiceImpl<CommonUserPointM
public void grantPointsByUserOrder(CommonUserOrderEntity order) { public void grantPointsByUserOrder(CommonUserOrderEntity order) {
Assert.notNull(order, "无效订单"); Assert.notNull(order, "无效订单");
Assert.isTrue(order.getId() != null && order.getUserId() != null && order.getActualAmount() != null && order.getActualAmount().compareTo(BigDecimal.ZERO) > 0, "无效订单"); Assert.isTrue(order.getId() != null && order.getUserId() != null && order.getActualAmount() != null && order.getActualAmount().compareTo(BigDecimal.ZERO) > 0, "无效订单");
BigDecimal pointsAmount; Assert.isTrue(order.getOrderType().equalsIgnoreCase(SELF_OPERATED_MALL.getCode()), "无效类型");
String sourceType; BigDecimal pointsAmount = order.getActualAmount().multiply(BigDecimal.valueOf(0.3));
switch (BusinessModule.fromCodeIgnoreCaseOrNull(order.getOrderType())) { String sourceType = SELF_OPERATED_MALL.getCode();
case SELF_OPERATED_MALL -> {
pointsAmount = order.getActualAmount().multiply(BigDecimal.valueOf(0.3));
sourceType = SELF_OPERATED_MALL.getCode();
}
default -> throw new ServiceException("无效订单类型");
}
CommonUserPointEntity userPoint = getById(order.getUserId()); CommonUserPointEntity userPoint = getById(order.getUserId());
BigDecimal lastAmount = userPoint.getAmount(); BigDecimal lastAmount = userPoint.getAmount();
int i = baseMapper.addUserPoint(order.getUserId(), pointsAmount, userPoint.getVersion()); int i = baseMapper.addUserPoint(order.getUserId(), pointsAmount, userPoint.getVersion());

View File

@@ -29,6 +29,8 @@ import org.springblade.modules.market.pojo.entity.MarketUserCommissionEntity;
import org.springblade.modules.market.pojo.vo.MarketUserCommissionVO; import org.springblade.modules.market.pojo.vo.MarketUserCommissionVO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import java.math.BigDecimal;
import java.util.List; import java.util.List;
/** /**
@@ -49,4 +51,5 @@ public interface MarketUserCommissionMapper extends BaseMapper<MarketUserCommiss
List<MarketUserCommissionVO> selectUserCommissionPage(IPage page, MarketUserCommissionVO userCommission); List<MarketUserCommissionVO> selectUserCommissionPage(IPage page, MarketUserCommissionVO userCommission);
int addUserCommission(Long userId, BigDecimal amount, Integer version);
} }

View File

@@ -30,5 +30,11 @@
order by uc.id desc order by uc.id desc
</select> </select>
<update id="addUserCommission">
update
user_commission
set amount = amount + #{amount} , version = version + 1
where id = #{userId} and version = #{version}
</update>
</mapper> </mapper>

View File

@@ -25,11 +25,14 @@
*/ */
package org.springblade.modules.market.service; package org.springblade.modules.market.service;
import org.springblade.modules.market.pojo.entity.MarketUserCommissionEntity;
import org.springblade.modules.market.pojo.entity.MarketUserCommissionLogEntity; import org.springblade.modules.market.pojo.entity.MarketUserCommissionLogEntity;
import org.springblade.modules.market.pojo.vo.MarketUserCommissionLogVO; import org.springblade.modules.market.pojo.vo.MarketUserCommissionLogVO;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseService; import org.springblade.core.mp.base.BaseService;
import java.math.BigDecimal;
/** /**
* 用户佣金流水表 服务类 * 用户佣金流水表 服务类
* *
@@ -47,4 +50,5 @@ public interface IMarketUserCommissionLogService extends BaseService<MarketUserC
IPage<MarketUserCommissionLogVO> selectUserCommissionLogPage(IPage<MarketUserCommissionLogVO> page, MarketUserCommissionLogVO userCommissionLog); IPage<MarketUserCommissionLogVO> selectUserCommissionLogPage(IPage<MarketUserCommissionLogVO> page, MarketUserCommissionLogVO userCommissionLog);
boolean recordCommissionChangeLog(MarketUserCommissionEntity userCommission, BigDecimal lastAmount, String sourceType, BigDecimal amount, Integer type, Long sourceId, String sourceDetail);
} }

View File

@@ -25,6 +25,7 @@
*/ */
package org.springblade.modules.market.service; package org.springblade.modules.market.service;
import org.springblade.modules.common.pojo.entity.CommonUserOrderEntity;
import org.springblade.modules.market.pojo.entity.MarketUserCommissionEntity; import org.springblade.modules.market.pojo.entity.MarketUserCommissionEntity;
import org.springblade.modules.market.pojo.vo.MarketUserCommissionVO; import org.springblade.modules.market.pojo.vo.MarketUserCommissionVO;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -47,4 +48,5 @@ public interface IMarketUserCommissionService extends BaseService<MarketUserComm
IPage<MarketUserCommissionVO> selectUserCommissionPage(IPage<MarketUserCommissionVO> page, MarketUserCommissionVO userCommission); IPage<MarketUserCommissionVO> selectUserCommissionPage(IPage<MarketUserCommissionVO> page, MarketUserCommissionVO userCommission);
void earnedCommissionByUserOrder(CommonUserOrderEntity order, Long userId);
} }

View File

@@ -25,6 +25,8 @@
*/ */
package org.springblade.modules.market.service.impl; package org.springblade.modules.market.service.impl;
import org.springblade.common.utils.date.DateUtil;
import org.springblade.modules.market.pojo.entity.MarketUserCommissionEntity;
import org.springblade.modules.market.pojo.entity.MarketUserCommissionLogEntity; import org.springblade.modules.market.pojo.entity.MarketUserCommissionLogEntity;
import org.springblade.modules.market.pojo.vo.MarketUserCommissionLogVO; import org.springblade.modules.market.pojo.vo.MarketUserCommissionLogVO;
import org.springblade.modules.market.mapper.MarketUserCommissionLogMapper; import org.springblade.modules.market.mapper.MarketUserCommissionLogMapper;
@@ -33,6 +35,8 @@ import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl; import org.springblade.core.mp.base.BaseServiceImpl;
import java.math.BigDecimal;
/** /**
* 用户佣金流水表 服务实现类 * 用户佣金流水表 服务实现类
* *
@@ -47,5 +51,20 @@ public class MarketUserCommissionLogServiceImpl extends BaseServiceImpl<MarketUs
return page.setRecords(baseMapper.selectUserCommissionLogPage(page, userCommissionLog)); return page.setRecords(baseMapper.selectUserCommissionLogPage(page, userCommissionLog));
} }
@Override
public boolean recordCommissionChangeLog(MarketUserCommissionEntity userCommission, BigDecimal lastAmount, String sourceType, BigDecimal amount, Integer type, Long sourceId, String sourceDetail) {
MarketUserCommissionLogEntity commissionLog = new MarketUserCommissionLogEntity();
commissionLog.setType(type);
commissionLog.setUserId(userCommission.getId());
commissionLog.setAmount(amount);
commissionLog.setSourceType(sourceType);
commissionLog.setSourceId(sourceId);
commissionLog.setSourceDetail(sourceDetail);
commissionLog.setIsComplete(1);
commissionLog.setStatisticalDate(DateUtil.getToday());
commissionLog.setLastAmount(lastAmount);
return save(commissionLog);
}
} }

View File

@@ -25,13 +25,27 @@
*/ */
package org.springblade.modules.market.service.impl; package org.springblade.modules.market.service.impl;
import com.alibaba.fastjson.JSON;
import io.jsonwebtoken.lang.Assert;
import lombok.AllArgsConstructor;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.modules.common.eums.UserCommissionFlowType;
import org.springblade.modules.common.event.EarnedCommissionEvent;
import org.springblade.modules.common.pojo.entity.CommonUserOrderEntity;
import org.springblade.modules.market.pojo.entity.MarketUserCommissionEntity; import org.springblade.modules.market.pojo.entity.MarketUserCommissionEntity;
import org.springblade.modules.market.pojo.vo.MarketUserCommissionVO; import org.springblade.modules.market.pojo.vo.MarketUserCommissionVO;
import org.springblade.modules.market.mapper.MarketUserCommissionMapper; import org.springblade.modules.market.mapper.MarketUserCommissionMapper;
import org.springblade.modules.market.service.IMarketUserCommissionLogService;
import org.springblade.modules.market.service.IMarketUserCommissionService; import org.springblade.modules.market.service.IMarketUserCommissionService;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl; import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import static org.springblade.modules.common.eums.BusinessModule.SELF_OPERATED_MALL;
/** /**
* 用户佣金表 服务实现类 * 用户佣金表 服务实现类
@@ -40,12 +54,39 @@ import org.springblade.core.mp.base.BaseServiceImpl;
* @since 2026-02-14 * @since 2026-02-14
*/ */
@Service @Service
@AllArgsConstructor
public class MarketUserCommissionServiceImpl extends BaseServiceImpl<MarketUserCommissionMapper, MarketUserCommissionEntity> implements IMarketUserCommissionService { public class MarketUserCommissionServiceImpl extends BaseServiceImpl<MarketUserCommissionMapper, MarketUserCommissionEntity> implements IMarketUserCommissionService {
private final IMarketUserCommissionLogService commissionLogService;
private final ApplicationEventPublisher eventPublisher;
@Override @Override
public IPage<MarketUserCommissionVO> selectUserCommissionPage(IPage<MarketUserCommissionVO> page, MarketUserCommissionVO userCommission) { public IPage<MarketUserCommissionVO> selectUserCommissionPage(IPage<MarketUserCommissionVO> page, MarketUserCommissionVO userCommission) {
return page.setRecords(baseMapper.selectUserCommissionPage(page, userCommission)); return page.setRecords(baseMapper.selectUserCommissionPage(page, userCommission));
} }
@Override
@Transactional(rollbackFor = Exception.class)
public void earnedCommissionByUserOrder(CommonUserOrderEntity order, Long userId) {
Assert.notNull(order, "无效订单");
Assert.isTrue(order.getId() != null && order.getUserId() != null && order.getActualAmount() != null && order.getActualAmount().compareTo(BigDecimal.ZERO) > 0, "无效订单");
Assert.isTrue(order.getOrderType().equalsIgnoreCase(SELF_OPERATED_MALL.getCode()), "无效类型");
BigDecimal commissionAmount = order.getActualAmount().multiply(BigDecimal.valueOf(0.1));
String sourceType = SELF_OPERATED_MALL.getCode();
MarketUserCommissionEntity userCommission = getById(userId);
BigDecimal lastAmount = userCommission.getAmount();
int i = baseMapper.addUserCommission(userId, commissionAmount, userCommission.getVersion());
if (i > 0) {
if (commissionLogService.recordCommissionChangeLog(userCommission, lastAmount, sourceType, commissionAmount, UserCommissionFlowType.EARNED_FROM_REFERRAL.getCode(), order.getId(), JSON.toJSONString(order))) {
eventPublisher.publishEvent(new EarnedCommissionEvent(order.getId(), userId, commissionAmount));
return;
}
}
throw new ServiceException("直推人奖励佣金失败");
}
} }