|
@@ -4,10 +4,12 @@ package com.fire.get.order.service.impl;
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.fire.common.auth.SupplierAuth;
|
|
|
+import com.fire.common.redis.RedisAmountScript;
|
|
|
import com.fire.common.redis.RedisPriorityQueueScript;
|
|
|
import com.fire.dto.ChannelSupplier;
|
|
|
import com.fire.dto.FlowOrderInfo;
|
|
|
import com.fire.dto.MobileFlowDispatchRec;
|
|
|
+import com.fire.dto.TransactionFlow;
|
|
|
import com.fire.dto.enums.BusinessStatus;
|
|
|
import com.fire.dto.enums.SupplierOrderStatus;
|
|
|
import com.fire.get.order.data.DataPool;
|
|
@@ -22,19 +24,19 @@ import org.springframework.web.client.RestTemplate;
|
|
|
import redis.clients.jedis.JedisCluster;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-
|
|
|
import java.util.Date;
|
|
|
|
|
|
import static com.fire.common.constants.RocketTags.FAIL_TAG;
|
|
|
import static com.fire.common.constants.RocketTags.SUCCESS_TAG;
|
|
|
-import static com.fire.common.constants.RocketTopic.CHILD_ORDER_TOPIC;
|
|
|
-import static com.fire.common.constants.RocketTopic.ORDER_TOPIC;
|
|
|
+import static com.fire.common.constants.RocketTopic.*;
|
|
|
+import static com.fire.dto.enums.AmountOper.ADD;
|
|
|
import static com.fire.dto.enums.BusinessStatus.BUSINESS_REPORT_ERROR;
|
|
|
import static com.fire.dto.enums.BusinessStatus.BUSINESS_SUCCESS;
|
|
|
import static com.fire.dto.enums.OrderStatus.ORDER_FAIL;
|
|
|
import static com.fire.dto.enums.OrderStatus.ORDER_SUCCESS;
|
|
|
-import static com.fire.dto.enums.RedisKey.CHILD_ORDER_INFO;
|
|
|
-import static com.fire.dto.enums.RedisKey.ORDER_INFO;
|
|
|
+import static com.fire.dto.enums.RedisKey.*;
|
|
|
+import static com.fire.dto.enums.RelationType.RELATION_SUPPLIER;
|
|
|
+import static com.fire.dto.enums.ServiceType.SERVICE_ORDER_ADD;
|
|
|
import static com.fire.dto.enums.SupplierOrderStatus.SUPPLIER_ORDER_PROCESS;
|
|
|
|
|
|
/**
|
|
@@ -56,10 +58,13 @@ public class GetOrderSupplierServiceImpl implements GetOrderSupplierService {
|
|
|
private RocketMQTemplate rocketMQTemplate;
|
|
|
@Resource
|
|
|
private SupplierAuth supplierAuth;
|
|
|
+ @Resource
|
|
|
+ private RedisAmountScript redisAmountScript;
|
|
|
|
|
|
@Override
|
|
|
@Async
|
|
|
public void getOrder(MobileFlowDispatchRec childOrder) {
|
|
|
+
|
|
|
//根据通道提取供应商信息
|
|
|
ChannelSupplier supplier = DataPool.supplierHashMap.get(childOrder.getChannelId());
|
|
|
//组装请求参数
|
|
@@ -105,27 +110,71 @@ public class GetOrderSupplierServiceImpl implements GetOrderSupplierService {
|
|
|
|
|
|
//如果充值中不操作 所以非充值中都需要更改状态
|
|
|
if (!supplierOrderStatus.equals(SUPPLIER_ORDER_PROCESS)) {
|
|
|
- //分发记录修改 只要不是充值中,子订单就进入终态 todo redis中获取分发记录
|
|
|
+ //分发记录修改 只要不是充值中,子订单就进入终态
|
|
|
+ ObjectMapper om = new ObjectMapper();
|
|
|
+ //获取一个实时的子订单状态
|
|
|
+ Long recId = childOrder.getRecId();
|
|
|
+ long hKey = recId / 10000000;
|
|
|
+
|
|
|
+ try {
|
|
|
+ String newChildOrderStr = jedisCluster.hget(CHILD_ORDER_INFO.key() + hKey, String.valueOf(recId));
|
|
|
+ MobileFlowDispatchRec newChildOrder = om.readValue(newChildOrderStr, MobileFlowDispatchRec.class);
|
|
|
+ //如果当前是最终态 直接返回
|
|
|
+ if (ORDER_FAIL.status().equals(newChildOrder.getSendStatus()) || ORDER_SUCCESS.status().equals(newChildOrder.getSendStatus())) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("实时子订单信息获取异常:", e);
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
childOrder.setGwErrorMsg(businessStatus.message());
|
|
|
childOrder.setGwErrorCode(String.valueOf(businessStatus.status()));
|
|
|
+ String tags = null;
|
|
|
switch (supplierOrderStatus) {
|
|
|
- case SUPPLIER_ORDER_FAILED -> childOrder.setSendStatus(ORDER_FAIL.status());
|
|
|
- case SUPPLIER_ORDER_SUCCESS -> childOrder.setSendStatus(ORDER_SUCCESS.status());
|
|
|
+ case SUPPLIER_ORDER_FAILED -> {
|
|
|
+ childOrder.setSendStatus(ORDER_FAIL.status());
|
|
|
+ tags = FAIL_TAG;
|
|
|
+ //子订单失败 如果原来已经是最终态
|
|
|
+ Long afterAmount = redisAmountScript.changeAmount(SUPPLIER_AMOUNT.key(), supplier.getSupplierId(), ADD.oper(), childOrder.getOperatorBalancePrice(), 100000000000L);
|
|
|
+ Long transSeq = jedisCluster.incr(GLOBAL_ID_INCR.key());
|
|
|
+ TransactionFlow transaction = TransactionFlow.builder()
|
|
|
+ .afterAmount(afterAmount)
|
|
|
+ .beforeAmount(afterAmount + childOrder.getOperatorBalancePrice())
|
|
|
+ .createTime(new Date())
|
|
|
+ .distinguish(RELATION_SUPPLIER.type())
|
|
|
+ .extorderId(String.valueOf(childOrder.getRecId()))
|
|
|
+ .orderId(childOrder.getOrderId())
|
|
|
+ .name(supplier.getSupplierName())
|
|
|
+ .note("订单失败退款")
|
|
|
+ .operatorName("system")
|
|
|
+ .operatingAmount(childOrder.getOperatorBalancePrice())
|
|
|
+ .serviceType(SERVICE_ORDER_ADD.type())
|
|
|
+ .relationId(supplier.getSupplierId())
|
|
|
+ .seqNo(transSeq)
|
|
|
+ .build();
|
|
|
+ try {
|
|
|
+ //流水入队列
|
|
|
+ String transactionStr = om.writeValueAsString(transaction);
|
|
|
+ rocketMQTemplate.syncSend(TRANSACTION_TOPIC, MessageBuilder.withPayload(transactionStr).build());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("供应商下单扣减异常:", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ case SUPPLIER_ORDER_SUCCESS -> {
|
|
|
+ childOrder.setSendStatus(ORDER_SUCCESS.status());
|
|
|
+ tags = SUCCESS_TAG;
|
|
|
+ }
|
|
|
}
|
|
|
- Long recId = childOrder.getRecId();
|
|
|
+
|
|
|
String childOrderStr;
|
|
|
- ObjectMapper om = new ObjectMapper();
|
|
|
+
|
|
|
try {
|
|
|
childOrderStr = om.writeValueAsString(childOrder);
|
|
|
//订单转换为json更新入redis
|
|
|
- long hKey = recId / 10000000;
|
|
|
+
|
|
|
jedisCluster.hset(CHILD_ORDER_INFO.key() + hKey, String.valueOf(recId), childOrderStr);
|
|
|
- String tags = null;
|
|
|
- switch (supplierOrderStatus) {
|
|
|
- case SUPPLIER_ORDER_FAILED -> tags = FAIL_TAG;
|
|
|
- case SUPPLIER_ORDER_SUCCESS -> tags = SUCCESS_TAG;
|
|
|
- }
|
|
|
+
|
|
|
//子订单入队列
|
|
|
rocketMQTemplate.syncSendOrderly(CHILD_ORDER_TOPIC + ":" + tags, MessageBuilder.withPayload(childOrderStr).build(), String.valueOf(recId));
|
|
|
} catch (JsonProcessingException e) {
|
|
@@ -136,9 +185,9 @@ public class GetOrderSupplierServiceImpl implements GetOrderSupplierService {
|
|
|
|
|
|
long orderId = childOrder.getOrderId();
|
|
|
//订单转换为json更新入redis
|
|
|
- long hKey = orderId / 10000000;
|
|
|
String orderStr = jedisCluster.hget(ORDER_INFO.key() + hKey, String.valueOf(orderId));
|
|
|
|
|
|
+
|
|
|
//子订单成功 那么主订单就成功了 子订单成功 那么主订单重入队列
|
|
|
switch (supplierOrderStatus) {
|
|
|
case SUPPLIER_ORDER_SUCCESS -> {
|