feat:
1. notice module
This commit is contained in:
@@ -32,6 +32,7 @@ import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
||||
import lombok.AllArgsConstructor;
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
import org.springblade.core.redis.debounce.RedisDebounce;
|
||||
import org.springblade.core.secure.BladeUser;
|
||||
import org.springblade.core.secure.annotation.IsAdmin;
|
||||
import org.springblade.core.mp.support.Condition;
|
||||
@@ -164,7 +165,7 @@ public class CommonUserOrderController extends BladeController {
|
||||
@ApiOperationSupport(order = 5)
|
||||
@Operation(summary = "确认收款", description = "传入order")
|
||||
public R confirmPayment(@Valid @RequestBody CommonUserOrderEntity order) {
|
||||
return R.status(orderService.confirmPayment(order));
|
||||
return R.status(orderService.confirmPayment(order.getId()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package org.springblade.modules.common.eums;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public enum BusinessModule {
|
||||
|
||||
SELF_OPERATED_MALL("自营商城", "self_mall", 1),
|
||||
ECOSYSTEM_PRODUCT("生态商品", "eco_product", 2),
|
||||
TRAVEL_GROUP("旅游团", "travel_group", 3);
|
||||
|
||||
@Getter
|
||||
private final String displayName;
|
||||
|
||||
@Getter
|
||||
private final String code;
|
||||
|
||||
@Getter
|
||||
private final int id;
|
||||
|
||||
private static final BusinessModule[] BY_ID;
|
||||
|
||||
static {
|
||||
// 初始化 BY_ID 映射(用于 O(1) 查找)
|
||||
int maxId = 0;
|
||||
for (BusinessModule module : values()) {
|
||||
if (module.id > maxId) maxId = module.id;
|
||||
}
|
||||
BY_ID = new BusinessModule[maxId + 1];
|
||||
for (BusinessModule module : values()) {
|
||||
if (BY_ID[module.id] != null) {
|
||||
throw new IllegalStateException("重复的模块 ID: " + module.id);
|
||||
}
|
||||
BY_ID[module.id] = module;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ========================
|
||||
// 根据 code 查找(忽略大小写)
|
||||
// ========================
|
||||
public static BusinessModule fromCodeIgnoreCase(String code) {
|
||||
BusinessModule module = fromCodeIgnoreCaseOrNull(code);
|
||||
if (module == null) {
|
||||
throw new IllegalArgumentException("未知的业务模块 code: " + code);
|
||||
}
|
||||
return module;
|
||||
}
|
||||
|
||||
public static BusinessModule fromCodeIgnoreCaseOrNull(String code) {
|
||||
if (code == null || code.trim().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
String normalized = code.trim().toLowerCase();
|
||||
for (BusinessModule module : values()) {
|
||||
if (module.code.equals(normalized)) {
|
||||
return module;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// ========================
|
||||
// 根据 int id 查找
|
||||
// ========================
|
||||
public static BusinessModule fromId(int id) {
|
||||
BusinessModule module = fromIdOrNull(id);
|
||||
if (module == null) {
|
||||
throw new IllegalArgumentException("未知的业务模块 id: " + id);
|
||||
}
|
||||
return module;
|
||||
}
|
||||
|
||||
public static BusinessModule fromIdOrNull(int id) {
|
||||
if (id < 1 || id >= BY_ID.length) {
|
||||
return null;
|
||||
}
|
||||
return BY_ID[id];
|
||||
}
|
||||
|
||||
/**
|
||||
* O(1) 快速获取(假设 ID 有效且已初始化)
|
||||
*/
|
||||
public static BusinessModule getByIdFast(int id) {
|
||||
return BY_ID[id]; // 注意:若 id 无效会抛 ArrayIndexOutOfBoundsException
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,14 +10,14 @@ import java.util.stream.Stream;
|
||||
@Getter
|
||||
public enum OrderStatus {
|
||||
|
||||
PENDING_PAYMENT(0, "📌 订单 %s 尚未支付!请尽快完成付款,以免订单自动取消。"),
|
||||
PAID(1, "✅ 订单 %s 已支付成功!商家正在准备发货(或待您使用),请耐心等待。"),
|
||||
CANCELLED(2, "🗑️ 订单 %s 已取消。如有疑问,可联系客服了解详情。"),
|
||||
SHIPPED(3, "🚚 订单 %s 已发货!(或🎫 电子票已出票)请留意物流信息(或按时使用服务)。"),
|
||||
COMPLETED(4, "🎉 订单 %s 已完成!感谢您的信任,期待再次为您服务~"),
|
||||
CLOSED_TIMEOUT(5, "⏰ 订单 %s 因超时未支付已自动关闭。如需购买,请重新下单。"),
|
||||
REFUNDING(6, "🔄 订单 %s 的退款申请已受理,款项将在1-7个工作日内原路退回,请耐心等待。"),
|
||||
REFUNDED(7, "💸 订单 %s 的退款已成功!金额已退至您的原支付账户,请注意查收。");
|
||||
PENDING_PAYMENT(0, "📌 订单 <id>%s</id> 尚未支付!请尽快完成付款,以免订单自动取消。"),
|
||||
PAID(1, "✅ 订单 <id>%s</id> 已支付成功!商家正在准备发货(或待您使用),请耐心等待。"),
|
||||
CANCELLED(2, "🗑️ 订单 <id>%s</id> 已取消。如有疑问,可联系客服了解详情。"),
|
||||
SHIPPED(3, "🚚 订单 <id>%s</id> 已发货!(或🎫 电子票已出票)请留意物流信息(或按时使用服务)。"),
|
||||
COMPLETED(4, "🎉 订单 <id>%s</id> 已完成!感谢您的信任,期待再次为您服务~"),
|
||||
CLOSED_TIMEOUT(5, "⏰ 订单 <id>%s</id> 因超时未支付已自动关闭。如需购买,请重新下单。"),
|
||||
REFUNDING(6, "🔄 订单 <id>%s</id> 的退款申请已受理,款项将在1-7个工作日内原路退回,请耐心等待。"),
|
||||
REFUNDED(7, "💸 订单 <id>%s</id> 的退款已成功!金额已退至您的原支付账户,请注意查收。");
|
||||
|
||||
private final int code;
|
||||
private final String userMessageTemplate;
|
||||
@@ -58,4 +58,5 @@ public enum OrderStatus {
|
||||
public static OrderStatus fromCodeOrNull(int code) {
|
||||
return BY_CODE.get(code);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.springblade.modules.common.event;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import org.springblade.modules.common.eums.BusinessModule;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public class PaymentSuccessEvent {
|
||||
|
||||
private final Long orderId;
|
||||
|
||||
private final Long userId;
|
||||
|
||||
private final BusinessModule businessModule;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.springblade.modules.common.listener;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springblade.modules.common.eums.NoticeType;
|
||||
import org.springblade.modules.common.eums.OrderStatus;
|
||||
import org.springblade.modules.common.event.PaymentSuccessEvent;
|
||||
import org.springblade.modules.common.pojo.entity.CommonUserNoticeEntity;
|
||||
import org.springblade.modules.common.service.ICommonUserNoticeService;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.event.TransactionPhase;
|
||||
import org.springframework.transaction.event.TransactionalEventListener;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@AllArgsConstructor
|
||||
public class NoticeListener {
|
||||
|
||||
private final ICommonUserNoticeService noticeService;
|
||||
|
||||
@Async
|
||||
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
|
||||
public void onPaymentSuccess(PaymentSuccessEvent event) {
|
||||
CommonUserNoticeEntity notice = new CommonUserNoticeEntity();
|
||||
notice.setUserId(event.getUserId());
|
||||
notice.setNoticeType(NoticeType.APP_USER.getCode());
|
||||
notice.setModuleType(event.getBusinessModule().getId());
|
||||
notice.setTitle(event.getBusinessModule().getDisplayName());
|
||||
notice.setContent(OrderStatus.PAID.formatUserMessage(String.valueOf(event.getOrderId())));
|
||||
noticeService.save(notice);
|
||||
}
|
||||
|
||||
@TransactionalEventListener(phase = TransactionPhase.AFTER_ROLLBACK)
|
||||
public void onPaymentFailure(PaymentSuccessEvent event) {
|
||||
log.warn("支付事务回滚,orderId={}", event.getOrderId());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -41,6 +41,10 @@
|
||||
<if test="order.id != null">
|
||||
and uo.id = #{order.id}
|
||||
</if>
|
||||
<if test="order.orderStatus != null">
|
||||
and uo.order_status = #{order.orderStatus}
|
||||
</if>
|
||||
order by create_time desc
|
||||
</select>
|
||||
|
||||
<select id="selectOrderEntityPage" resultType="org.springblade.modules.common.pojo.entity.CommonUserOrderEntity">
|
||||
|
||||
@@ -26,7 +26,6 @@
|
||||
package org.springblade.modules.common.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springblade.modules.api.request.CreateOrderRequest;
|
||||
import org.springblade.modules.api.request.PaymentRequest;
|
||||
import org.springblade.modules.api.response.CreateOrderResponse;
|
||||
@@ -70,5 +69,5 @@ public interface ICommonUserOrderService extends IService<CommonUserOrderEntity>
|
||||
|
||||
IPage<CommonUserOrderApiVO> selectOrderApiPage(IPage<CommonUserOrderApiVO> page, CommonUserOrderApiVO userOrder);
|
||||
|
||||
boolean confirmPayment(CommonUserOrderEntity order);
|
||||
boolean confirmPayment(Long id);
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import io.jsonwebtoken.lang.Assert;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springblade.core.redis.debounce.RedisDebounce;
|
||||
import org.springblade.core.secure.utils.AuthUtil;
|
||||
import org.springblade.modules.api.request.CreateOrderRequest;
|
||||
import org.springblade.modules.api.request.PaymentRequest;
|
||||
@@ -36,8 +37,10 @@ import org.springblade.modules.api.response.CreateOrderResponse;
|
||||
import org.springblade.modules.api.response.PaymentResponse;
|
||||
import org.springblade.modules.api.vo.CommonUserOrderApiVO;
|
||||
import org.springblade.modules.common.convert.AppCommonConvert;
|
||||
import org.springblade.modules.common.eums.BusinessModule;
|
||||
import org.springblade.modules.common.eums.OrderStatus;
|
||||
import org.springblade.modules.common.eums.PaymentStatus;
|
||||
import org.springblade.modules.common.event.PaymentSuccessEvent;
|
||||
import org.springblade.modules.common.pojo.entity.CommonUserOrderEntity;
|
||||
import org.springblade.modules.common.pojo.vo.CommonUserOrderVO;
|
||||
import org.springblade.modules.common.excel.CommonUserOrderExcel;
|
||||
@@ -45,10 +48,12 @@ import org.springblade.modules.common.mapper.CommonUserOrderMapper;
|
||||
import org.springblade.modules.common.service.ICommonUserOrderService;
|
||||
import org.springblade.modules.mall.pojo.entity.MallProductEntity;
|
||||
import org.springblade.modules.mall.service.IMallProductService;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
@@ -69,6 +74,8 @@ public class CommonUserOrderServiceImpl extends ServiceImpl<CommonUserOrderMappe
|
||||
|
||||
private final IMallProductService productService;
|
||||
|
||||
private final ApplicationEventPublisher eventPublisher;
|
||||
|
||||
|
||||
@Override
|
||||
public IPage<CommonUserOrderVO> selectOrderPage(IPage<CommonUserOrderVO> page, CommonUserOrderVO order) {
|
||||
@@ -133,17 +140,31 @@ public class CommonUserOrderServiceImpl extends ServiceImpl<CommonUserOrderMappe
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean confirmPayment(CommonUserOrderEntity order) {
|
||||
return update(Wrappers.<CommonUserOrderEntity>lambdaUpdate()
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@RedisDebounce(key = "order:confirm", param = "#id", interval = 20, includeRemainingTime = true, message = "操作过于频繁")
|
||||
public boolean confirmPayment(Long id) {
|
||||
|
||||
CommonUserOrderEntity order = getById(id);
|
||||
if (order == null || order.getIsDeleted() == 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean updated = update(Wrappers.<CommonUserOrderEntity>lambdaUpdate()
|
||||
.set(CommonUserOrderEntity::getOrderStatus, OrderStatus.PAID.getCode())
|
||||
.set(CommonUserOrderEntity::getPaymentStatus, PaymentStatus.PAID.getCode())
|
||||
.set(CommonUserOrderEntity::getPaymentTime, new Date())
|
||||
.eq(CommonUserOrderEntity::getId, order.getId())
|
||||
.eq(CommonUserOrderEntity::getId, id)
|
||||
.eq(CommonUserOrderEntity::getPaymentType, 99)
|
||||
.eq(CommonUserOrderEntity::getOrderStatus, 0)
|
||||
.eq(CommonUserOrderEntity::getPaymentStatus, 0)
|
||||
.eq(CommonUserOrderEntity::getIsDeleted, 0)
|
||||
);
|
||||
if (!updated) {
|
||||
return false;
|
||||
}
|
||||
//todo:增加用户积分
|
||||
eventPublisher.publishEvent(new PaymentSuccessEvent(id, order.getUserId(), BusinessModule.SELF_OPERATED_MALL));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user