|
@@ -3,7 +3,6 @@ package com.fire.order.service.impl;
|
|
|
|
|
|
import com.alibaba.nacos.api.utils.StringUtils;
|
|
import com.alibaba.nacos.api.utils.StringUtils;
|
|
import com.alibaba.nacos.common.utils.CollectionUtils;
|
|
import com.alibaba.nacos.common.utils.CollectionUtils;
|
|
-import com.alibaba.nacos.common.utils.MD5Utils;
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fire.common.redis.RedisAmountScript;
|
|
import com.fire.common.redis.RedisAmountScript;
|
|
@@ -21,7 +20,10 @@ import com.fire.param.HeaderDto;
|
|
import com.fire.utils.date.DateUtils;
|
|
import com.fire.utils.date.DateUtils;
|
|
import com.fire.utils.string.FireStringUtils;
|
|
import com.fire.utils.string.FireStringUtils;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
|
|
|
+import org.springframework.messaging.support.MessageBuilder;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.util.DigestUtils;
|
|
import redis.clients.jedis.JedisCluster;
|
|
import redis.clients.jedis.JedisCluster;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
@@ -37,6 +39,8 @@ import static com.fire.dto.enums.OrderStatus.ORDER_SENT;
|
|
import static com.fire.dto.enums.PriceCheck.CHECK;
|
|
import static com.fire.dto.enums.PriceCheck.CHECK;
|
|
import static com.fire.dto.enums.Province.QG_ALL;
|
|
import static com.fire.dto.enums.Province.QG_ALL;
|
|
import static com.fire.dto.enums.RedisKey.*;
|
|
import static com.fire.dto.enums.RedisKey.*;
|
|
|
|
+import static com.fire.dto.enums.RocketQueue.ORDER_QUEUE;
|
|
|
|
+import static com.fire.dto.enums.RocketTopic.MAKE_ORDER_TOPIC;
|
|
import static com.fire.dto.enums.Status.*;
|
|
import static com.fire.dto.enums.Status.*;
|
|
import static com.fire.dto.enums.ValidStatus.INVALID;
|
|
import static com.fire.dto.enums.ValidStatus.INVALID;
|
|
|
|
|
|
@@ -55,6 +59,8 @@ public class MakeOrderServiceImpl implements MakeOrderService {
|
|
private RedisOrderIdScript redisOrderIdScript;
|
|
private RedisOrderIdScript redisOrderIdScript;
|
|
@Resource
|
|
@Resource
|
|
private RedisAmountScript redisAmountScript;
|
|
private RedisAmountScript redisAmountScript;
|
|
|
|
+ @Resource
|
|
|
|
+ private RocketMQTemplate rocketMQTemplate;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 下单方法
|
|
* 下单方法
|
|
@@ -71,15 +77,15 @@ public class MakeOrderServiceImpl implements MakeOrderService {
|
|
responseDto.setMsgBody(msgBody);
|
|
responseDto.setMsgBody(msgBody);
|
|
//判断请求参数是否为空
|
|
//判断请求参数是否为空
|
|
if (null == orderRequestParam) {
|
|
if (null == orderRequestParam) {
|
|
- orderResp.setRCode(FAILURE_SERVER.status());
|
|
|
|
- orderResp.setRMsg("请求参数为空");
|
|
|
|
|
|
+ orderResp.setRCode(PARAM_LOSS.status());
|
|
|
|
+ orderResp.setRMsg(PARAM_LOSS.message());
|
|
return responseDto;
|
|
return responseDto;
|
|
}
|
|
}
|
|
HeaderDto header = orderRequestParam.getHeader();
|
|
HeaderDto header = orderRequestParam.getHeader();
|
|
//判断头信息是否为空
|
|
//判断头信息是否为空
|
|
if (null == header) {
|
|
if (null == header) {
|
|
- orderResp.setRCode(FAILURE_SERVER.status());
|
|
|
|
- orderResp.setRMsg("请求参数头信息为空");
|
|
|
|
|
|
+ orderResp.setRCode(HEADER_LOSS.status());
|
|
|
|
+ orderResp.setRMsg(HEADER_LOSS.message());
|
|
return responseDto;
|
|
return responseDto;
|
|
}
|
|
}
|
|
responseDto.setHeader(header);
|
|
responseDto.setHeader(header);
|
|
@@ -87,14 +93,14 @@ public class MakeOrderServiceImpl implements MakeOrderService {
|
|
long now = System.currentTimeMillis();
|
|
long now = System.currentTimeMillis();
|
|
String orderTime = header.getTimestamp();
|
|
String orderTime = header.getTimestamp();
|
|
if (null == orderTime) {
|
|
if (null == orderTime) {
|
|
- orderResp.setRCode(FAILURE_SERVER.status());
|
|
|
|
- orderResp.setRMsg("时间戳为空");
|
|
|
|
|
|
+ orderResp.setRCode(DATE_ERROR.status());
|
|
|
|
+ orderResp.setRMsg(DATE_ERROR.message());
|
|
return responseDto;
|
|
return responseDto;
|
|
}
|
|
}
|
|
long orderT = DateUtils.String2Long(orderTime);
|
|
long orderT = DateUtils.String2Long(orderTime);
|
|
if (orderT == 0) {
|
|
if (orderT == 0) {
|
|
- orderResp.setRCode(FAILURE_SERVER.status());
|
|
|
|
- orderResp.setRMsg("时间戳格式错误");
|
|
|
|
|
|
+ orderResp.setRCode(DATE_ERROR.status());
|
|
|
|
+ orderResp.setRMsg(DATE_ERROR.message());
|
|
return responseDto;
|
|
return responseDto;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -106,22 +112,22 @@ public class MakeOrderServiceImpl implements MakeOrderService {
|
|
//校验seqNo是否为空
|
|
//校验seqNo是否为空
|
|
String seqNo = header.getSeqNo();
|
|
String seqNo = header.getSeqNo();
|
|
if (StringUtils.isEmpty(seqNo)) {
|
|
if (StringUtils.isEmpty(seqNo)) {
|
|
- orderResp.setRCode(OVER_TIME.status());
|
|
|
|
- orderResp.setRMsg("流水号为空");
|
|
|
|
|
|
+ orderResp.setRCode(PARAM_LOSS.status());
|
|
|
|
+ orderResp.setRMsg(seqNo + PARAM_LOSS.message());
|
|
return responseDto;
|
|
return responseDto;
|
|
}
|
|
}
|
|
//校验消息体
|
|
//校验消息体
|
|
OrderRequestMsgBody requestMsgBody = orderRequestParam.getMsgBody();
|
|
OrderRequestMsgBody requestMsgBody = orderRequestParam.getMsgBody();
|
|
if (null == requestMsgBody) {
|
|
if (null == requestMsgBody) {
|
|
orderResp.setRCode(FAILURE_SERVER.status());
|
|
orderResp.setRCode(FAILURE_SERVER.status());
|
|
- orderResp.setRMsg("请求消息体为空");
|
|
|
|
|
|
+ orderResp.setRMsg("body" + PARAM_LOSS.message());
|
|
return responseDto;
|
|
return responseDto;
|
|
}
|
|
}
|
|
//参数正文校验
|
|
//参数正文校验
|
|
OrderRequestContent content = requestMsgBody.getContent();
|
|
OrderRequestContent content = requestMsgBody.getContent();
|
|
if (null == content) {
|
|
if (null == content) {
|
|
orderResp.setRCode(FAILURE_SERVER.status());
|
|
orderResp.setRCode(FAILURE_SERVER.status());
|
|
- orderResp.setRMsg("请求参数正文为空");
|
|
|
|
|
|
+ orderResp.setRMsg("content" + PARAM_LOSS.message());
|
|
return responseDto;
|
|
return responseDto;
|
|
}
|
|
}
|
|
//校验packageId是否为空
|
|
//校验packageId是否为空
|
|
@@ -134,8 +140,8 @@ public class MakeOrderServiceImpl implements MakeOrderService {
|
|
//校验orderType是否为空
|
|
//校验orderType是否为空
|
|
String orderType = content.getOrderType();
|
|
String orderType = content.getOrderType();
|
|
if (StringUtils.isEmpty(orderType)) {
|
|
if (StringUtils.isEmpty(orderType)) {
|
|
- orderResp.setRCode(FAILURE_SERVER.status());
|
|
|
|
- orderResp.setRMsg("订单类型为空");
|
|
|
|
|
|
+ orderResp.setRCode(PARAM_LOSS.status());
|
|
|
|
+ orderResp.setRMsg(orderType + PARAM_LOSS.message());
|
|
return responseDto;
|
|
return responseDto;
|
|
}
|
|
}
|
|
//校验订单号是否为空
|
|
//校验订单号是否为空
|
|
@@ -171,16 +177,16 @@ public class MakeOrderServiceImpl implements MakeOrderService {
|
|
//校验appId
|
|
//校验appId
|
|
String appId = header.getAppId();
|
|
String appId = header.getAppId();
|
|
if (StringUtils.isEmpty(appId)) {
|
|
if (StringUtils.isEmpty(appId)) {
|
|
- orderResp.setRCode(FAILURE_SERVER.status());
|
|
|
|
- orderResp.setRMsg("APPID为空");
|
|
|
|
|
|
+ orderResp.setRCode(APP_ID_LOSE.status());
|
|
|
|
+ orderResp.setRMsg(APP_ID_LOSE.message());
|
|
return responseDto;
|
|
return responseDto;
|
|
}
|
|
}
|
|
|
|
|
|
//appId是否存在
|
|
//appId是否存在
|
|
FlowAppInfo flowAppInfo = DataPool.flowAppInfoMap.get(appId);
|
|
FlowAppInfo flowAppInfo = DataPool.flowAppInfoMap.get(appId);
|
|
if (flowAppInfo == null) {
|
|
if (flowAppInfo == null) {
|
|
- orderResp.setRCode(FAILURE_SERVER.status());
|
|
|
|
- orderResp.setRMsg("APPID不存在");
|
|
|
|
|
|
+ orderResp.setRCode(APP_ID_LOSE.status());
|
|
|
|
+ orderResp.setRMsg(APP_ID_LOSE.message());
|
|
return responseDto;
|
|
return responseDto;
|
|
}
|
|
}
|
|
//appId是否失效
|
|
//appId是否失效
|
|
@@ -190,7 +196,7 @@ public class MakeOrderServiceImpl implements MakeOrderService {
|
|
return responseDto;
|
|
return responseDto;
|
|
}
|
|
}
|
|
String secretStr = header.getTimestamp() + seqNo + appId + flowAppInfo.getAppKey();
|
|
String secretStr = header.getTimestamp() + seqNo + appId + flowAppInfo.getAppKey();
|
|
- String secretKey = MD5Utils.encodeHexString(secretStr.getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
|
+ String secretKey = DigestUtils.md5DigestAsHex(secretStr.getBytes(StandardCharsets.UTF_8));
|
|
//secretKey校验
|
|
//secretKey校验
|
|
if (!secretKey.equals(header.getSecretKey())) {
|
|
if (!secretKey.equals(header.getSecretKey())) {
|
|
orderResp.setRCode(SECRET_KEY_ERROR.status());
|
|
orderResp.setRCode(SECRET_KEY_ERROR.status());
|
|
@@ -198,7 +204,7 @@ public class MakeOrderServiceImpl implements MakeOrderService {
|
|
return responseDto;
|
|
return responseDto;
|
|
}
|
|
}
|
|
String signStr = flowAppInfo.getAppKey() + phoneNo + packageId + orderType + extOrder;
|
|
String signStr = flowAppInfo.getAppKey() + phoneNo + packageId + orderType + extOrder;
|
|
- String sign = MD5Utils.encodeHexString(signStr.getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
|
+ String sign = DigestUtils.md5DigestAsHex(signStr.getBytes(StandardCharsets.UTF_8));
|
|
//sign校验
|
|
//sign校验
|
|
if (!sign.equals(content.getSign())) {
|
|
if (!sign.equals(content.getSign())) {
|
|
orderResp.setRCode(SIGN_FAIL.status());
|
|
orderResp.setRCode(SIGN_FAIL.status());
|
|
@@ -219,8 +225,8 @@ public class MakeOrderServiceImpl implements MakeOrderService {
|
|
return responseDto;
|
|
return responseDto;
|
|
}
|
|
}
|
|
//校验产品是否有效
|
|
//校验产品是否有效
|
|
- Integer isEffective = product.getIsValid();
|
|
|
|
- if (INVALID.status().equals(isEffective)) {
|
|
|
|
|
|
+ Integer isValid = product.getIsValid();
|
|
|
|
+ if (INVALID.status().equals(isValid)) {
|
|
orderResp.setRCode(PACKAGE_ERROR.status());
|
|
orderResp.setRCode(PACKAGE_ERROR.status());
|
|
orderResp.setRMsg(PACKAGE_ERROR.message());
|
|
orderResp.setRMsg(PACKAGE_ERROR.message());
|
|
return responseDto;
|
|
return responseDto;
|
|
@@ -229,19 +235,25 @@ public class MakeOrderServiceImpl implements MakeOrderService {
|
|
//根据号码前七位,获取归属地信息
|
|
//根据号码前七位,获取归属地信息
|
|
String start7 = phoneNo.substring(0, 7);
|
|
String start7 = phoneNo.substring(0, 7);
|
|
PhoneZone phoneZone = DataPool.phoneZoneMap.get(start7);
|
|
PhoneZone phoneZone = DataPool.phoneZoneMap.get(start7);
|
|
|
|
+ if (phoneZone == null) {
|
|
|
|
+ orderResp.setRCode(PHONE_ZONE_ERROR.status());
|
|
|
|
+ orderResp.setRMsg("归属地信息未取到,需要供应商初始化归属信息");
|
|
|
|
+ return responseDto;
|
|
|
|
+ }
|
|
|
|
+ //如果归属不一样,那么不通过校验
|
|
if (!QG_ALL.getCode().equals(product.getAreaNum())) {
|
|
if (!QG_ALL.getCode().equals(product.getAreaNum())) {
|
|
//如果归属不一样,那么不通过校验
|
|
//如果归属不一样,那么不通过校验
|
|
- if (phoneZone == null || !phoneZone.getProvinceCode().equals(product.getAreaNum())) {
|
|
|
|
- orderResp.setRCode(FAILURE_SERVER.status());
|
|
|
|
- orderResp.setRMsg("号码归属和所选产品归属不一致");
|
|
|
|
|
|
+ if (!phoneZone.getProvinceCode().equals(product.getAreaNum())) {
|
|
|
|
+ orderResp.setRCode(PHONE_ZONE_ERROR.status());
|
|
|
|
+ orderResp.setRMsg(PHONE_ZONE_ERROR.message());
|
|
return responseDto;
|
|
return responseDto;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
//如果要求校验结算价格
|
|
//如果要求校验结算价格
|
|
CustomerInfo customerInfo = flowAppInfo.getCustomerInfo();
|
|
CustomerInfo customerInfo = flowAppInfo.getCustomerInfo();
|
|
if (customerInfo == null || CHECK.status().equals(customerInfo.getPriceCheck()) && !product.getPrice().equals(content.getPrice())) {
|
|
if (customerInfo == null || CHECK.status().equals(customerInfo.getPriceCheck()) && !product.getPrice().equals(content.getPrice())) {
|
|
- orderResp.setRCode(FAILURE_SERVER.status());
|
|
|
|
- orderResp.setRMsg("结算价格不一致");
|
|
|
|
|
|
+ orderResp.setRCode(PRICE_ERROR.status());
|
|
|
|
+ orderResp.setRMsg(PRICE_ERROR.message());
|
|
return responseDto;
|
|
return responseDto;
|
|
}
|
|
}
|
|
//校验维护信息
|
|
//校验维护信息
|
|
@@ -283,12 +295,15 @@ public class MakeOrderServiceImpl implements MakeOrderService {
|
|
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+
|
|
//自增id 取自redis,长度17位,后面加两位00 为主订单号,后面分发订单号后两位从 01开始
|
|
//自增id 取自redis,长度17位,后面加两位00 为主订单号,后面分发订单号后两位从 01开始
|
|
Long orderId = jedisCluster.incr(ORDER_ID_INCR.key()) * 100;
|
|
Long orderId = jedisCluster.incr(ORDER_ID_INCR.key()) * 100;
|
|
|
|
|
|
//客户订单id校验 这里订单id拼接上客户id,保证唯一
|
|
//客户订单id校验 这里订单id拼接上客户id,保证唯一
|
|
String customerOrder = content.getExtOrder() + "." + flowAppInfo.getCustomerId();
|
|
String customerOrder = content.getExtOrder() + "." + flowAppInfo.getCustomerId();
|
|
String checkOrder = redisOrderIdScript.checkOrderId(customerOrder, String.valueOf(orderId));
|
|
String checkOrder = redisOrderIdScript.checkOrderId(customerOrder, String.valueOf(orderId));
|
|
|
|
+
|
|
//0表示订单号重复
|
|
//0表示订单号重复
|
|
if ("0".equals(checkOrder)) {
|
|
if ("0".equals(checkOrder)) {
|
|
orderResp.setRCode(ORDER_ID_ERROR.status());
|
|
orderResp.setRCode(ORDER_ID_ERROR.status());
|
|
@@ -331,12 +346,20 @@ public class MakeOrderServiceImpl implements MakeOrderService {
|
|
return responseDto;
|
|
return responseDto;
|
|
}
|
|
}
|
|
//扣减
|
|
//扣减
|
|
- redisAmountScript.changeAmount(CUSTOMER_AMOUNT.key(), flowAppInfo.getCustomerId(), SUB.oper(), product.getPrice(), customerInfo.getCreditAmount());
|
|
|
|
|
|
+ Long afterAmount = redisAmountScript.changeAmount(CUSTOMER_AMOUNT.key(), flowAppInfo.getCustomerId(), SUB.oper(), product.getPrice(), customerInfo.getCreditAmount());
|
|
|
|
+ if (afterAmount == null) {
|
|
|
|
+ orderResp.setRCode(BALANCE_NOT_ENOUGH.status());
|
|
|
|
+ orderResp.setRMsg(BALANCE_NOT_ENOUGH.message());
|
|
|
|
+ return responseDto;
|
|
|
|
+ }
|
|
|
|
+
|
|
try {
|
|
try {
|
|
//订单入redis
|
|
//订单入redis
|
|
|
|
+
|
|
jedisCluster.hset(ORDER_INFO.key(), String.valueOf(orderId), orderStr);
|
|
jedisCluster.hset(ORDER_INFO.key(), String.valueOf(orderId), orderStr);
|
|
- //订单入队列
|
|
|
|
|
|
|
|
|
|
+ //订单入队列
|
|
|
|
+ rocketMQTemplate.syncSendOrderly(MAKE_ORDER_TOPIC.topic(), MessageBuilder.withPayload(orderStr).build(), ORDER_QUEUE.queue());
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
//其他异常 扣减补偿
|
|
//其他异常 扣减补偿
|
|
log.error("订单存储或者发送到队列失败", e);
|
|
log.error("订单存储或者发送到队列失败", e);
|
|
@@ -345,7 +368,6 @@ public class MakeOrderServiceImpl implements MakeOrderService {
|
|
orderResp.setRMsg(FAILURE_SERVER.message());
|
|
orderResp.setRMsg(FAILURE_SERVER.message());
|
|
return responseDto;
|
|
return responseDto;
|
|
}
|
|
}
|
|
-
|
|
|
|
return responseDto;
|
|
return responseDto;
|
|
}
|
|
}
|
|
|
|
|