Commit c1e137db by 李幸鑫

Merge branch 'new-pay' of git.lijinqi.com:new-computility/api into new-pay

parents 4a90c71d 3dde2664
package com.luhu.computility.module.apihub.service.expire;
import com.luhu.computility.module.apihub.dal.mysql.apiorder.ApiOrderMapper;
import com.luhu.computility.module.apihub.dal.dataobject.apiorder.ApiOrderDO;
import com.luhu.computility.module.apihub.enums.ApihubOrderStatusEnum;
import com.luhu.computility.module.pay.enums.OrderBusinessTypeEnum;
import com.luhu.computility.module.pay.service.expire.BusinessOrderExpireService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
/**
* APIHub订单过期服务实现类
*
* @author jony
*/
@Service
@Slf4j
public class ApiHubOrderExpireServiceImpl implements BusinessOrderExpireService {
@Resource
private ApiOrderMapper apiOrderMapper;
@Override
public OrderBusinessTypeEnum getSupportedBusinessType() {
return OrderBusinessTypeEnum.API_HUB;
}
@Override
public boolean expireBusinessOrder(Long businessOrderId) {
try {
// 查询订单
ApiOrderDO order = apiOrderMapper.selectById(businessOrderId);
if (order == null) {
log.warn("[expireBusinessOrder] API订单不存在,订单ID: {}", businessOrderId);
return false;
}
// 检查订单状态,只有待支付的订单才能过期
if (!ApihubOrderStatusEnum.UNPAID.getValue().equals(order.getStatus())) {
log.warn("[expireBusinessOrder] API订单状态不是待支付,无法过期,订单ID: {}, 当前状态: {}",
businessOrderId, order.getStatus());
return false;
}
// 更新订单状态为已取消
ApiOrderDO updateOrder = new ApiOrderDO();
updateOrder.setId(businessOrderId);
updateOrder.setStatus(ApihubOrderStatusEnum.CANCELED.getValue());
updateOrder.setCancelTime(LocalDateTime.now());
int updateCount = apiOrderMapper.updateById(updateOrder);
if (updateCount > 0) {
log.info("[expireBusinessOrder] API订单过期成功,订单ID: {}", businessOrderId);
return true;
} else {
log.error("[expireBusinessOrder] API订单过期失败,更新数据库失败,订单ID: {}", businessOrderId);
return false;
}
} catch (Exception e) {
log.error("[expireBusinessOrder] 过期API订单异常,订单ID: {}", businessOrderId, e);
return false;
}
}
}
\ No newline at end of file
package com.luhu.computility.module.compute.service.expire;
import com.luhu.computility.module.compute.dal.mysql.resourceorder.ResourceOrderMapper;
import com.luhu.computility.module.compute.dal.dataobject.resourceorder.ResourceOrderDO;
import com.luhu.computility.module.compute.enums.ResourceOrderStatus;
import com.luhu.computility.module.pay.enums.OrderBusinessTypeEnum;
import com.luhu.computility.module.pay.service.expire.BusinessOrderExpireService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
/**
* 算力资源订单过期服务实现类
*
* @author jony
*/
@Service
@Slf4j
public class ComputeOrderExpireServiceImpl implements BusinessOrderExpireService {
@Resource
private ResourceOrderMapper resourceOrderMapper;
@Override
public OrderBusinessTypeEnum getSupportedBusinessType() {
return OrderBusinessTypeEnum.COMPUTE_RESOURCE;
}
@Override
public boolean expireBusinessOrder(Long businessOrderId) {
try {
// 查询订单
ResourceOrderDO order = resourceOrderMapper.selectById(businessOrderId);
if (order == null) {
log.warn("[expireBusinessOrder] 算力资源订单不存在,订单ID: {}", businessOrderId);
return false;
}
// 检查订单状态,只有待支付的订单才能过期
if (!ResourceOrderStatus.UNPAID.getValue().equals(order.getStatus())) {
log.warn("[expireBusinessOrder] 算力资源订单状态不是待支付,无法过期,订单ID: {}, 当前状态: {}",
businessOrderId, order.getStatus());
return false;
}
// 更新订单状态为已取消
ResourceOrderDO updateOrder = new ResourceOrderDO();
updateOrder.setId(businessOrderId);
updateOrder.setStatus(ResourceOrderStatus.CANCELED.getValue());
updateOrder.setCancelTime(LocalDateTime.now());
int updateCount = resourceOrderMapper.updateById(updateOrder);
if (updateCount > 0) {
log.info("[expireBusinessOrder] 算力资源订单过期成功,订单ID: {}", businessOrderId);
return true;
} else {
log.error("[expireBusinessOrder] 算力资源订单过期失败,更新数据库失败,订单ID: {}", businessOrderId);
return false;
}
} catch (Exception e) {
log.error("[expireBusinessOrder] 过期算力资源订单异常,订单ID: {}", businessOrderId, e);
return false;
}
}
}
\ No newline at end of file
......@@ -31,6 +31,18 @@
<artifactId>computility-spring-boot-starter-biz-tenant</artifactId>
</dependency>
<!-- 业务模块API依赖 -->
<dependency>
<groupId>com.luhu</groupId>
<artifactId>computility-module-compute-api</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.luhu</groupId>
<artifactId>computility-module-apihub-api</artifactId>
<version>${revision}</version>
</dependency>
<!-- Web 相关 -->
<dependency>
<groupId>com.luhu</groupId>
......
package com.luhu.computility.module.pay.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* 订单业务类型枚举
*
* @author jony
*/
@Getter
@AllArgsConstructor
public enum OrderBusinessTypeEnum {
COMPUTE_RESOURCE(1, "算力资源订单"),
API_HUB(2, "API订单");
private final Integer value;
private final String name;
/**
* 根据值获取枚举
*/
public static OrderBusinessTypeEnum fromValue(Integer value) {
for (OrderBusinessTypeEnum type : values()) {
if (type.getValue().equals(value)) {
return type;
}
}
return null;
}
/**
* 根据值获取名称
*/
public static String getNameByValue(Integer value) {
OrderBusinessTypeEnum typeEnum = fromValue(value);
return typeEnum != null ? typeEnum.getName() : null;
}
/**
* 判断是否是算力资源订单
*/
public static boolean isComputeResource(Integer businessType) {
return COMPUTE_RESOURCE.getValue().equals(businessType);
}
/**
* 判断是否是API订单
*/
public static boolean isApiHub(Integer businessType) {
return API_HUB.getValue().equals(businessType);
}
}
package com.luhu.computility.module.pay.service.expire;
import com.luhu.computility.module.pay.enums.OrderBusinessTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
/**
* 业务订单过期管理器
*
* @author jony
*/
@Service
@Slf4j
public class BusinessOrderExpireManager {
@Autowired
private ApplicationContext applicationContext;
private Map<Integer, BusinessOrderExpireService> expireServiceMap = new HashMap<>();
@PostConstruct
public void init() {
// 获取所有BusinessOrderExpireService的实现
Map<String, BusinessOrderExpireService> services = applicationContext.getBeansOfType(BusinessOrderExpireService.class);
for (BusinessOrderExpireService service : services.values()) {
OrderBusinessTypeEnum supportedType = service.getSupportedBusinessType();
if (supportedType != null) {
expireServiceMap.put(supportedType.getValue(), service);
log.info("[init] 注册业务订单过期服务: {} -> {}",
supportedType.getName(), service.getClass().getSimpleName());
}
}
log.info("[init] 业务订单过期服务注册完成,共注册 {} 个服务", expireServiceMap.size());
}
/**
* 根据业务类型过期业务订单
*
* @param businessType 业务类型
* @param businessOrderId 业务订单ID
* @return 是否过期成功
*/
public boolean expireBusinessOrder(Integer businessType, Long businessOrderId) {
BusinessOrderExpireService service = expireServiceMap.get(businessType);
if (service == null) {
log.error("[expireBusinessOrder] 未找到业务类型 {} 对应的过期服务", businessType);
return false;
}
try {
log.info("[expireBusinessOrder] 开始过期业务订单,业务类型: {}, 订单ID: {}", businessType, businessOrderId);
boolean success = service.expireBusinessOrder(businessOrderId);
if (success) {
log.info("[expireBusinessOrder] 业务订单过期成功,业务类型: {}, 订单ID: {}", businessType, businessOrderId);
} else {
log.error("[expireBusinessOrder] 业务订单过期失败,业务类型: {}, 订单ID: {}", businessType, businessOrderId);
}
return success;
} catch (Exception e) {
log.error("[expireBusinessOrder] 过期业务订单异常,业务类型: {}, 订单ID: {}", businessType, businessOrderId, e);
return false;
}
}
}
\ No newline at end of file
package com.luhu.computility.module.pay.service.expire;
import com.luhu.computility.module.pay.enums.OrderBusinessTypeEnum;
/**
* 业务订单过期服务接口
*
* @author jony
*/
public interface BusinessOrderExpireService {
/**
* 获取支持的业务类型
*
* @return 业务类型
*/
OrderBusinessTypeEnum getSupportedBusinessType();
/**
* 过期业务订单
*
* @param businessOrderId 业务订单ID
* @return 是否过期成功
*/
boolean expireBusinessOrder(Long businessOrderId);
}
\ No newline at end of file
......@@ -8,6 +8,7 @@ import com.luhu.computility.module.pay.dal.dataobject.wpgj.PayOrderWpgjDO;
import com.luhu.computility.module.pay.dal.mysql.wpgj.PayOrderWpgjMapper;
import com.luhu.computility.module.pay.enums.WpgjOrderStatusEnum;
import com.luhu.computility.module.pay.framework.pay.core.client.impl.wpgj.WpgjPayProperties;
import com.luhu.computility.module.pay.service.expire.BusinessOrderExpireManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
......@@ -33,6 +34,9 @@ public class PayOrderWpgjServiceImpl implements PayOrderWpgjService {
@Resource
private WpgjPayProperties wpgjPayProperties;
@Resource
private BusinessOrderExpireManager businessOrderExpireManager;
@Override
public Long saveOrder(WpgjPayNotifyDTO notifyDTO) {
// 查询是否已存在相同商户订单ID的记录
......@@ -91,10 +95,14 @@ public class PayOrderWpgjServiceImpl implements PayOrderWpgjService {
return 0;
}
// 2. 遍历执行,将过期的待支付订单标记为已取消
// 2. 遍历执行,将过期的待支付订单标记为已取消,并同时过期对应的业务订单
int count = 0;
for (PayOrderWpgjDO order : orders) {
count += expireOrder(order) ? 1 : 0;
if (expireOrder(order)) {
count++;
// 同时过期对应的业务订单
expireBusinessOrder(order);
}
}
return count;
}
......@@ -126,4 +134,35 @@ public class PayOrderWpgjServiceImpl implements PayOrderWpgjService {
}
}
/**
* 过期业务订单
*
* @param payOrder WPGJ支付订单
*/
private void expireBusinessOrder(PayOrderWpgjDO payOrder) {
try {
Integer businessType = payOrder.getBusinessType();
String merOrderId = payOrder.getMerOrderId();
if (businessType == null || merOrderId == null) {
log.warn("[expireBusinessOrder] 业务类型或商户订单ID为空,无法过期业务订单,WPGJ订单ID: {}",
payOrder.getOrderId());
return;
}
// 调用业务订单过期管理器过期业务订单
boolean success = businessOrderExpireManager.expireBusinessOrder(businessType, Long.valueOf(merOrderId));
if (success) {
log.info("[expireBusinessOrder] 业务订单过期成功,业务类型: {}, 订单ID: {}, WPGJ订单ID: {}",
businessType, merOrderId, payOrder.getOrderId());
} else {
log.error("[expireBusinessOrder] 业务订单过期失败,业务类型: {}, 订单ID: {}, WPGJ订单ID: {}",
businessType, merOrderId, payOrder.getOrderId());
}
} catch (Exception e) {
log.error("[expireBusinessOrder] 过期业务订单异常,WPGJ订单ID: {}", payOrder.getOrderId(), e);
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment