@ -45,7 +45,9 @@ import com.hfkj.sysenum.merchant.MerchantStatusEnum;
import com.hfkj.sysenum.order.OrderPayTypeEnum ;
import com.hfkj.sysenum.order.OrderPayTypeEnum ;
import com.hfkj.sysenum.order.OrderRefundStatusEnum ;
import com.hfkj.sysenum.order.OrderRefundStatusEnum ;
import org.apache.commons.lang3.StringUtils ;
import org.apache.commons.lang3.StringUtils ;
import org.springframework.data.redis.core.RedisTemplate ;
import org.springframework.stereotype.Component ;
import org.springframework.stereotype.Component ;
import org.springframework.transaction.annotation.Isolation ;
import org.springframework.transaction.annotation.Propagation ;
import org.springframework.transaction.annotation.Propagation ;
import org.springframework.transaction.annotation.Transactional ;
import org.springframework.transaction.annotation.Transactional ;
@ -54,6 +56,7 @@ import java.math.BigDecimal;
import java.util.* ;
import java.util.* ;
import java.util.concurrent.ExecutorService ;
import java.util.concurrent.ExecutorService ;
import java.util.concurrent.Executors ;
import java.util.concurrent.Executors ;
import java.util.concurrent.TimeUnit ;
import java.util.stream.Collectors ;
import java.util.stream.Collectors ;
/ * *
/ * *
@ -91,12 +94,15 @@ public class ApiGasOrderService {
private BsOrderRefundService orderRefundService ;
private BsOrderRefundService orderRefundService ;
@Resource
@Resource
private BsGasOilPriceService gasOilPriceService ;
private BsGasOilPriceService gasOilPriceService ;
@Resource
private RedisTemplate < String , Object > redisTemplate ;
private final String LOCK_KEY = "AGENT_API_PUSH_ORDER_LOCK_" ;
/ * *
/ * *
* 推送加油订单
* 推送加油订单
* @param request
* @param request
* @return
* @return
* /
* /
@Transactional ( propagation = Propagation . REQUIRES_NEW , rollbackFor = { RuntimeException . class } )
@Transactional ( propagation = Propagation . NEVER )
public ResponseOrderPushModel pushOrder ( Long agentId , RequestOrderPushModel request ) throws Exception {
public ResponseOrderPushModel pushOrder ( Long agentId , RequestOrderPushModel request ) throws Exception {
// 交易油站权限
// 交易油站权限
BsAgentMer agentMer = agentMerService . getDetailByAgent ( agentId , request . getGasNo ( ) ) ;
BsAgentMer agentMer = agentMerService . getDetailByAgent ( agentId , request . getGasNo ( ) ) ;
@ -131,6 +137,23 @@ public class ApiGasOrderService {
if ( agent = = null ) {
if ( agent = = null ) {
throw ErrorHelp . genException ( SysCode . OpenApi , ErrorCode . COMMON_ERROR , "未知的接入方" ) ;
throw ErrorHelp . genException ( SysCode . OpenApi , ErrorCode . COMMON_ERROR , "未知的接入方" ) ;
}
}
return createOrder ( agent , gasDetail , oilPrice , request ) ;
}
/ * *
* 创建订单
* /
@Transactional ( propagation = Propagation . REQUIRES_NEW , rollbackFor = { RuntimeException . class } )
public ResponseOrderPushModel createOrder ( BsAgent agent , BsMerchant gasDetail , GasOilPriceModel oilPrice , RequestOrderPushModel request ) throws InterruptedException {
// 锁编号
String lockKey = LOCK_KEY + agent . getId ( ) ;
// 获取锁
Boolean lock = redisTemplate . opsForValue ( ) . setIfAbsent ( lockKey , "" ) ;
if ( Boolean . TRUE . equals ( lock ) ) {
try {
// 锁超时时间 15秒
redisTemplate . expire ( lockKey , 15 , TimeUnit . SECONDS ) ;
// 创建加油订单
// 创建加油订单
BsGasOrder gasOrder = new BsGasOrder ( ) ;
BsGasOrder gasOrder = new BsGasOrder ( ) ;
gasOrder . setAgentId ( agent . getId ( ) ) ;
gasOrder . setAgentId ( agent . getId ( ) ) ;
@ -197,7 +220,7 @@ public class ApiGasOrderService {
consumeParam . put ( "sourceType" , MerchantAccountRecordSourceTypeEnum . type2 . getType ( ) ) ;
consumeParam . put ( "sourceType" , MerchantAccountRecordSourceTypeEnum . type2 . getType ( ) ) ;
consumeParam . put ( "sourceOrderNo" , gasOrder . getOrderNo ( ) ) ;
consumeParam . put ( "sourceOrderNo" , gasOrder . getOrderNo ( ) ) ;
consumeParam . put ( "sourceOrderContent" , "加油订单:" + gasOrder . getOrderNo ( ) ) ;
consumeParam . put ( "sourceOrderContent" , "加油订单:" + gasOrder . getOrderNo ( ) ) ;
agentApiAccountService . consume ( agentId , gasOrder . getGasAgentSettlePrice ( ) , consumeParam ) ;
agentApiAccountService . consume ( agent . get Id( ) , gasOrder . getGasAgentSettlePrice ( ) , consumeParam ) ;
// 处理支付后的加油业务
// 处理支付后的加油业务
oilHandle ( gasOrder ) ;
oilHandle ( gasOrder ) ;
@ -209,8 +232,26 @@ public class ApiGasOrderService {
response . setOrderNo ( gasOrder . getOrderNo ( ) ) ;
response . setOrderNo ( gasOrder . getOrderNo ( ) ) ;
response . setChannelType ( gasOrder . getChannelType ( ) ) ;
response . setChannelType ( gasOrder . getChannelType ( ) ) ;
response . setChannelOrderNo ( gasOrder . getChannelOrderNo ( ) ) ;
response . setChannelOrderNo ( gasOrder . getChannelOrderNo ( ) ) ;
// 释放锁
redisTemplate . delete ( lockKey ) ;
return response ;
return response ;
} catch ( BaseException e ) {
// 释放锁
redisTemplate . delete ( lockKey ) ;
throw ErrorHelp . genException ( SysCode . OpenApi , ErrorCode . COMMON_ERROR , e . getErrorMsg ( ) ) ;
} catch ( Exception e ) {
// 释放锁
redisTemplate . delete ( lockKey ) ;
throw ErrorHelp . genException ( SysCode . OpenApi , ErrorCode . COMMON_ERROR , "创建订单失败" ) ;
}
} else {
Thread . sleep ( 100 ) ;
createOrder ( agent , gasDetail , oilPrice , request ) ;
}
}
throw ErrorHelp . genException ( SysCode . OpenApi , ErrorCode . COMMON_ERROR , "创建加油订单失败" ) ;
}
/ * *
/ * *
* 退款订单
* 退款订单
@ -218,7 +259,7 @@ public class ApiGasOrderService {
* @return
* @return
* @throws Exception
* @throws Exception
* /
* /
@Transactional ( propagation = Propagation . REQUIRES_NEW , rollbackFor = { RuntimeException . class } )
@Transactional ( propagation = Propagation . REQUIRES_NEW , isolation = Isolation . SERIALIZABLE , rollbackFor = { RuntimeException . class } )
public String refundOrder ( Long agentId , RequestOrderRefundModel request ) {
public String refundOrder ( Long agentId , RequestOrderRefundModel request ) {
// 查询加油订单
// 查询加油订单
BsGasOrder gasOrder = gasOrderService . getDetailByOrderNo ( request . getOrderNo ( ) ) ;
BsGasOrder gasOrder = gasOrderService . getDetailByOrderNo ( request . getOrderNo ( ) ) ;
@ -264,23 +305,9 @@ public class ApiGasOrderService {
singleThreadExecutor . submit ( new Runnable ( ) {
singleThreadExecutor . submit ( new Runnable ( ) {
@Override
@Override
public void run ( ) {
public void run ( ) {
if ( gasOrder . getChannelType ( ) . equals ( MerchantSourceTypeEnum . type2 . getNumber ( ) ) ) {
// 团油
try {
try {
// 请求团油渠道申请退款
// 退款金额
JSONObject object = NewLinkRequestService . refuelingOrderRefund ( gasOrder . getUserPhone ( ) , gasOrder . getOrderNo ( ) , orderRefund . getRefundRemark ( ) ) ;
refundPrice ( gasOrder , orderRefund ) ;
if ( object = = null | | ! object . getString ( "code" ) . equals ( "200" ) ) {
gasOrder . setAbnormal ( true ) ;
gasOrder . setAbnormalContent ( "渠道申请退款失败!原因:" + object . getString ( "message" ) ) ;
gasOrderService . updateGasOrder ( gasOrder ) ;
}
} catch ( Exception e ) {
throw ErrorHelp . genException ( SysCode . OpenApi , ErrorCode . COMMON_ERROR , "请求退款失败" ) ;
}
} else {
try {
// 退款
agentApiAccountService . refund ( gasOrder . getAgentId ( ) , gasOrder . getGasRefuelPrice ( ) , orderRefund ) ;
orderRefund . setRefundStatus ( OrderRefundStatusEnum . status2 . getCode ( ) ) ;
orderRefund . setRefundStatus ( OrderRefundStatusEnum . status2 . getCode ( ) ) ;
orderRefund . setFinishTime ( new Date ( ) ) ;
orderRefund . setFinishTime ( new Date ( ) ) ;
orderRefundService . editData ( orderRefund ) ;
orderRefundService . editData ( orderRefund ) ;
@ -289,6 +316,16 @@ public class ApiGasOrderService {
gasOrder . setRefundTime ( orderRefund . getFinishTime ( ) ) ;
gasOrder . setRefundTime ( orderRefund . getFinishTime ( ) ) ;
gasOrderService . updateGasOrder ( gasOrder ) ;
gasOrderService . updateGasOrder ( gasOrder ) ;
} catch ( BaseException e ) {
orderRefund . setRefundStatus ( OrderRefundStatusEnum . status3 . getCode ( ) ) ;
orderRefund . setRefundFailReason ( "退款失败!" ) ;
orderRefundService . editData ( orderRefund ) ;
gasOrder . setStatus ( OrderOilStatus . STATUS6 . getNumber ( ) ) ;
gasOrder . setReceiptFailRemark ( orderRefund . getRefundFailReason ( ) ) ;
gasOrderService . updateGasOrder ( gasOrder ) ;
throw ErrorHelp . genException ( SysCode . OpenApi , ErrorCode . COMMON_ERROR , e . getErrorMsg ( ) ) ;
} catch ( Exception e ) {
} catch ( Exception e ) {
orderRefund . setRefundStatus ( OrderRefundStatusEnum . status3 . getCode ( ) ) ;
orderRefund . setRefundStatus ( OrderRefundStatusEnum . status3 . getCode ( ) ) ;
orderRefund . setRefundFailReason ( "退款失败!" ) ;
orderRefund . setRefundFailReason ( "退款失败!" ) ;
@ -297,8 +334,11 @@ public class ApiGasOrderService {
gasOrder . setStatus ( OrderOilStatus . STATUS6 . getNumber ( ) ) ;
gasOrder . setStatus ( OrderOilStatus . STATUS6 . getNumber ( ) ) ;
gasOrder . setReceiptFailRemark ( orderRefund . getRefundFailReason ( ) ) ;
gasOrder . setReceiptFailRemark ( orderRefund . getRefundFailReason ( ) ) ;
gasOrderService . updateGasOrder ( gasOrder ) ;
gasOrderService . updateGasOrder ( gasOrder ) ;
}
// 退款通知
throw ErrorHelp . genException ( SysCode . OpenApi , ErrorCode . COMMON_ERROR , "请求退款失败" ) ;
} finally {
// 退款成功通知
if ( gasOrder . getStatus ( ) . equals ( OrderOilStatus . STATUS4 . getNumber ( ) ) ) {
// 代理参数
// 代理参数
BsAgentApiParam apiParam = agentApiParamService . getParamByAppId ( request . getAppId ( ) ) ;
BsAgentApiParam apiParam = agentApiParamService . getParamByAppId ( request . getAppId ( ) ) ;
if ( apiParam ! = null & & StringUtils . isNotBlank ( apiParam . getOrderRefundNotify ( ) ) ) {
if ( apiParam ! = null & & StringUtils . isNotBlank ( apiParam . getOrderRefundNotify ( ) ) ) {
@ -310,7 +350,8 @@ public class ApiGasOrderService {
refundNotify . setRefundResult ( orderRefund . getRefundStatus ( ) = = 2 ? true : false ) ;
refundNotify . setRefundResult ( orderRefund . getRefundStatus ( ) = = 2 ? true : false ) ;
refundNotify . setRefundFailReason ( orderRefund . getRefundFailReason ( ) ) ;
refundNotify . setRefundFailReason ( orderRefund . getRefundFailReason ( ) ) ;
refundNotify . setSign ( SignatureUtil . createSign ( refundNotify , apiParam . getAppSecret ( ) ) ) ;
refundNotify . setSign ( SignatureUtil . createSign ( refundNotify , apiParam . getAppSecret ( ) ) ) ;
HttpsUtils . doPost ( apiParam . getOrderRefundNotify ( ) , JSONObject . parseObject ( JSONObject . toJSONString ( refundNotify ) , JSONObject . class ) ) ;
HttpsUtils . doApiPost ( apiParam . getOrderRefundNotify ( ) , JSONObject . parseObject ( JSONObject . toJSONString ( refundNotify ) , JSONObject . class ) ) ;
}
}
}
}
}
}
}
@ -318,6 +359,49 @@ public class ApiGasOrderService {
return "ok" ;
return "ok" ;
}
}
/ * *
* 退款金额
* @param gasOrder
* @param orderRefund
* @return
* /
private void refundPrice ( BsGasOrder gasOrder , BsOrderRefund orderRefund ) {
if ( gasOrder . getChannelType ( ) . equals ( MerchantSourceTypeEnum . type2 . getNumber ( ) ) ) {
// 团油
try {
// 请求团油渠道申请退款
JSONObject object = NewLinkRequestService . refuelingOrderRefund ( gasOrder . getUserPhone ( ) , gasOrder . getOrderNo ( ) , orderRefund . getRefundRemark ( ) ) ;
if ( object = = null | | ! object . getString ( "code" ) . equals ( "200" ) ) {
gasOrder . setAbnormal ( true ) ;
gasOrder . setAbnormalContent ( "渠道申请退款失败!原因:" + object . getString ( "message" ) ) ;
gasOrderService . updateGasOrder ( gasOrder ) ;
}
} catch ( Exception e ) {
throw ErrorHelp . genException ( SysCode . OpenApi , ErrorCode . COMMON_ERROR , "请求退款失败" ) ;
}
} else {
try {
// 代理商退款退款
agentApiAccountService . refund ( gasOrder . getAgentId ( ) , gasOrder . getGasRefuelPrice ( ) , orderRefund ) ;
// 退款到油站
if ( gasOrder . getChannelType ( ) . equals ( MerchantSourceTypeEnum . type1 . getNumber ( ) ) ) {
merchantAccountService . refund ( gasOrder . getMerNo ( ) , gasOrder . getGasSettlePrice ( ) , orderRefund ) ;
}
orderRefund . setRefundStatus ( OrderRefundStatusEnum . status2 . getCode ( ) ) ;
orderRefund . setFinishTime ( new Date ( ) ) ;
orderRefundService . editData ( orderRefund ) ;
gasOrder . setStatus ( OrderOilStatus . STATUS4 . getNumber ( ) ) ;
gasOrder . setRefundTime ( orderRefund . getFinishTime ( ) ) ;
gasOrderService . updateGasOrder ( gasOrder ) ;
} catch ( Exception e ) {
throw ErrorHelp . genException ( SysCode . OpenApi , ErrorCode . COMMON_ERROR , "请求退款失败" ) ;
}
}
}
/ * *
/ * *
* 查询订单详情
* 查询订单详情
* @param request
* @param request