@@ -165,7 +165,7 @@ public class DayCardOrderServiceImpl extends ServiceImpl<DayCardOrderDao, DayCar
order . setChannelOrgId ( channelOrgId ) ;
order . setAmount ( amount ) ;
order . setStatus ( DayCardConstants . ORDER_STATUS_CREATED ) ;
order . setPayChannel ( " MOCK " ) ;
order . setPayChannel ( DayCardConstants . PAY_CHANNEL_WECHAT ) ;
order . setDeleteFlag ( DayCardConstants . DELETE_FLAG_UNDEL ) ;
Date now = new Date ( ) ;
order . setCreatedAt ( now ) ;
@@ -179,35 +179,12 @@ public class DayCardOrderServiceImpl extends ServiceImpl<DayCardOrderDao, DayCar
baseMapper . insert ( order ) ;
// 7. 微信 Native 支付下单
if ( wechatPayService . isConfigured ( ) ) {
try {
String codeUrl = wechatPayService . createNativePay ( orderNo , " 天卡订单 " , amount , channelOrgId , agentOrgId , storeOrgId ) ;
order . setCodeUrl ( codeUrl ) ;
order . setPayChannel ( " WECHAT " ) ;
UpdateWrapper < DayCardOrderEntity > payUpdate = new UpdateWrapper < > ( ) ;
payUpdate . eq ( " id " , order . getId ( ) )
. set ( " code_url " , codeUrl )
. set ( " pay_channel " , " WECHAT " ) ;
baseMapper . update ( null , payUpdate ) ;
} catch ( Exception e ) {
logger . warn ( " 微信下单失败,回退到MOCK模式: orderNo={} " , orderNo , e ) ;
order . setPayChannel ( " MOCK " ) ;
order . setCodeUrl ( " mock://pay?orderNo= " + orderNo ) ;
UpdateWrapper < DayCardOrderEntity > payUpdate = new UpdateWrapper < > ( ) ;
payUpdate . eq ( " id " , order . getId ( ) )
. set ( " code_url " , order . getCodeUrl ( ) )
. set ( " pay_channel " , " MOCK " ) ;
baseMapper . update ( null , payUpdate ) ;
}
} else {
order . setPayChannel ( " MOCK " ) ;
order . setCodeUrl ( " mock://pay?orderNo= " + orderNo ) ;
UpdateWrapper < DayCardOrderEntity > payUpdate = new UpdateWrapper < > ( ) ;
payUpdate . eq ( " id " , order . getId ( ) )
. set ( " code_url " , order . getCodeUrl ( ) )
. set ( " pay_channel " , " MOCK " ) ;
baseMapper . update ( null , payUpdate ) ;
}
String codeUrl = wechatPayService . createNativePay ( orderNo , " 天卡订单 " , amount , channelOrgId , agentOrgId , storeOrgId ) ;
order . setCodeUrl ( codeUrl ) ;
UpdateWrapper < DayCardOrderEntity > payUpdate = new UpdateWrapper < > ( ) ;
payUpdate . eq ( " id " , order . getId ( ) )
. set ( " code_url " , codeUrl ) ;
baseMapper . update ( null , payUpdate ) ;
// 8. SKIP LOCKED 抢卡(通用池,不按 partner 过滤)
DayCardPoolEntity pool = dayCardPoolDao . selectOneForUpdateSkipLocked ( ) ;
@@ -219,7 +196,7 @@ public class DayCardOrderServiceImpl extends ServiceImpl<DayCardOrderDao, DayCar
// 9. 乐观锁更新卡状态 UNUSED -> RESERVED
UpdateWrapper < DayCardPoolEntity > poolUpdate = new UpdateWrapper < > ( ) ;
poolUpdate . eq ( " id " , pool . getId ( ) )
. in ( " status " , DayCardConstants . POOL_STATUS_UNUSED , DayCardConstants . POOL_STATUS_REUSABLE )
. eq ( " status " , DayCardConstants . POOL_STATUS_UNUSED )
. eq ( " delete_flag " , DayCardConstants . DELETE_FLAG_UNDEL )
. set ( " status " , DayCardConstants . POOL_STATUS_RESERVED )
. set ( " order_no " , orderNo )
@@ -298,7 +275,7 @@ public class DayCardOrderServiceImpl extends ServiceImpl<DayCardOrderDao, DayCar
order . setChannelOrgId ( channelOrgId ) ;
order . setAmount ( amount ) ;
order . setStatus ( DayCardConstants . ORDER_STATUS_CREATED ) ;
order . setPayChannel ( " MOCK " ) ;
order . setPayChannel ( DayCardConstants . PAY_CHANNEL_WECHAT ) ;
order . setDeleteFlag ( DayCardConstants . DELETE_FLAG_UNDEL ) ;
Date now = new Date ( ) ;
order . setCreatedAt ( now ) ;
@@ -314,36 +291,12 @@ public class DayCardOrderServiceImpl extends ServiceImpl<DayCardOrderDao, DayCar
baseMapper . insert ( order ) ;
// 6.5 微信 Native 支付下单
if ( wechatPayService . isConfigured ( ) ) {
try {
String codeUrl = wechatPayService . createNativePay ( orderNo , " 天卡订单 " , amount , channelOrgId , agentOrgId , storeOrgId ) ;
order . setCodeUrl ( codeUrl ) ;
order . setPayChannel ( " WECHAT " ) ;
UpdateWrapper < DayCardOrderEntity > payUpdate = new UpdateWrapper < > ( ) ;
payUpdate . eq ( " id " , order . getId ( ) )
. set ( " code_url " , codeUrl )
. set ( " pay_channel " , " WECHAT " ) ;
baseMapper . update ( null , payUpdate ) ;
} catch ( Exception e ) {
logger . warn ( " 微信下单失败,回退到MOCK模式: orderNo={} " , orderNo , e ) ;
order . setPayChannel ( " MOCK " ) ;
order . setCodeUrl ( " mock://pay?orderNo= " + orderNo ) ;
UpdateWrapper < DayCardOrderEntity > payUpdate = new UpdateWrapper < > ( ) ;
payUpdate . eq ( " id " , order . getId ( ) )
. set ( " code_url " , order . getCodeUrl ( ) )
. set ( " pay_channel " , " MOCK " ) ;
baseMapper . update ( null , payUpdate ) ;
}
} else {
// 开发模式:使用 Mock 支付
order . setPayChannel ( " MOCK " ) ;
order . setCodeUrl ( " mock://pay?orderNo= " + orderNo ) ;
UpdateWrapper < DayCardOrderEntity > payUpdate = new UpdateWrapper < > ( ) ;
payUpdate . eq ( " id " , order . getId ( ) )
. set ( " code_url " , order . getCodeUrl ( ) )
. set ( " pay_channel " , " MOCK " ) ;
baseMapper . update ( null , payUpdate ) ;
}
String codeUrl = wechatPayService . createNativePay ( orderNo , " 天卡订单 " , amount , channelOrgId , agentOrgId , storeOrgId ) ;
order . setCodeUrl ( codeUrl ) ;
UpdateWrapper < DayCardOrderEntity > payUpdate = new UpdateWrapper < > ( ) ;
payUpdate . eq ( " id " , order . getId ( ) )
. set ( " code_url " , codeUrl ) ;
baseMapper . update ( null , payUpdate ) ;
// 7. SKIP LOCKED 抢卡
DayCardPoolEntity pool = dayCardPoolDao . selectOneForUpdateSkipLocked ( ) ;
@@ -355,7 +308,7 @@ public class DayCardOrderServiceImpl extends ServiceImpl<DayCardOrderDao, DayCar
// 8. 乐观锁更新卡状态 UNUSED -> RESERVED
UpdateWrapper < DayCardPoolEntity > poolUpdate = new UpdateWrapper < > ( ) ;
poolUpdate . eq ( " id " , pool . getId ( ) )
. in ( " status " , DayCardConstants . POOL_STATUS_UNUSED , DayCardConstants . POOL_STATUS_REUSABLE )
. eq ( " status " , DayCardConstants . POOL_STATUS_UNUSED )
. eq ( " delete_flag " , DayCardConstants . DELETE_FLAG_UNDEL )
. set ( " status " , DayCardConstants . POOL_STATUS_RESERVED )
. set ( " order_no " , orderNo )
@@ -444,12 +397,12 @@ public class DayCardOrderServiceImpl extends ServiceImpl<DayCardOrderDao, DayCar
redisUtils . delete ( DayCardConstants . REDIS_DEVICE_ORDER_PREFIX + order . getDeviceSn ( ) ) ;
}
// 6. 触发分账(异步 )
try {
profitSharingExecuteService . executeSharing ( orderNo ) ;
} catch ( Exception e ) {
logger . error ( " 触发分账失败: orderNo={} " , orderNo , e ) ;
}
// 6. 触发分账(商户号开通分账权限后取消注释启用 )
// try {
// profitSharingExecuteService.executeSharing(orderNo);
// } catch (Exception e) {
// logger.error("触发分账失败: orderNo={}", orderNo, e);
// }
}
/**
@@ -646,7 +599,7 @@ public class DayCardOrderServiceImpl extends ServiceImpl<DayCardOrderDao, DayCar
// 更新订单为CANCELLED
UpdateWrapper < DayCardOrderEntity > orderUpdate = new UpdateWrapper < > ( ) ;
orderUpdate . eq ( " id " , order . getId ( ) )
. eq ( " status " , DayCardConstants . ORDER_STATUS_CREATED )
. in ( " status " , DayCardConstants . ORDER_STATUS_CREATED , DayCardConstants . ORDER_STATUS_PAYING )
. set ( " status " , DayCardConstants . ORDER_STATUS_CANCELLED )
. set ( " cancel_time " , now )
. set ( " updated_at " , now ) ;
@@ -672,6 +625,29 @@ public class DayCardOrderServiceImpl extends ServiceImpl<DayCardOrderDao, DayCar
return baseMapper . selectOne ( wrapper ) ;
}
@Override
@Transactional ( rollbackFor = Exception . class )
public void cancelOrder ( String orderNo , Long deviceId ) {
if ( StringUtils . isBlank ( orderNo ) | | deviceId = = null ) {
throw new ASException ( " 订单号和设备ID不能为空 " ) ;
}
DayCardOrderEntity order = getByOrderNo ( orderNo ) ;
if ( order = = null ) {
throw new ASException ( " 订单不存在: " + orderNo ) ;
}
if ( ! deviceId . equals ( order . getDeviceId ( ) ) ) {
throw new ASException ( " 无权操作此订单 " ) ;
}
if ( DayCardConstants . ORDER_STATUS_PAID . equals ( order . getStatus ( ) ) ) {
throw new ASException ( " 订单已支付,无法取消 " ) ;
}
if ( DayCardConstants . ORDER_STATUS_CANCELLED . equals ( order . getStatus ( ) ) ) {
return ;
}
cancelSingleOrder ( order ) ;
logger . info ( " 设备端取消订单: orderNo={}, deviceId={} " , orderNo , deviceId ) ;
}
@Override
public DayCardPoolEntity getDeviceBenefits ( Long deviceId ) {
QueryWrapper < DayCardPoolEntity > wrapper = new QueryWrapper < > ( ) ;
@@ -706,7 +682,6 @@ public class DayCardOrderServiceImpl extends ServiceImpl<DayCardOrderDao, DayCar
UpdateWrapper < DayCardPoolEntity > update = new UpdateWrapper < > ( ) ;
update . eq ( " id " , pool . getId ( ) )
. eq ( " status " , DayCardConstants . POOL_STATUS_USED )
. set ( " status " , DayCardConstants . POOL_STATUS_REUSABLE )
. set ( " device_id " , null )
. set ( " updated_at " , new Date ( ) ) ;
int updated = dayCardPoolDao . update ( null , update ) ;
@@ -723,17 +698,16 @@ public class DayCardOrderServiceImpl extends ServiceImpl<DayCardOrderDao, DayCar
throw new ASException ( " 设备ID不能为空 " ) ;
}
Date now = new Date ( ) ;
// 1. 释放 设备当前天卡权益(若有):USED -> REUSABLE,清卡上的 device_id
// 1. 解绑 设备当前天卡权益(若有):清除 device_id,卡状态保持 USED 不变
DayCardPoolEntity pool = getDeviceBenefits ( deviceId ) ;
if ( pool ! = null ) {
UpdateWrapper < DayCardPoolEntity > poolUpdate = new UpdateWrapper < > ( ) ;
poolUpdate . eq ( " id " , pool . getId ( ) )
. eq ( " status " , DayCardConstants . POOL_STATUS_USED )
. set ( " status " , DayCardConstants . POOL_STATUS_REUSABLE )
. set ( " device_id " , null )
. set ( " updated_at " , now ) ;
dayCardPoolDao . update ( null , poolUpdate ) ;
logger . info ( " 设备组织解绑-释放 天卡权益: deviceId={}, poolId={}, cardCode={} " ,
logger . info ( " 设备组织解绑-解除 天卡权益绑定 : deviceId={}, poolId={}, cardCode={} " ,
deviceId , pool . getId ( ) , pool . getCardCode ( ) ) ;
}
// 2. 清除设备的门店组织关联(storeOrgId),设备可在新场所用新激活码重新绑定