|
|
@@ -4,6 +4,10 @@ package com.fire.order.service.impl;
|
|
|
import com.alibaba.nacos.api.utils.StringUtils;
|
|
|
import com.alibaba.nacos.common.utils.CollectionUtils;
|
|
|
import com.alibaba.nacos.common.utils.MD5Utils;
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fire.common.redisscript.RedisAmountScript;
|
|
|
+import com.fire.common.redisscript.RedisOrderIdScript;
|
|
|
import com.fire.dto.*;
|
|
|
import com.fire.order.data.DataPool;
|
|
|
import com.fire.order.request.OrderRequestContent;
|
|
|
@@ -16,6 +20,7 @@ import com.fire.order.service.MakeOrderService;
|
|
|
import com.fire.param.HeaderDto;
|
|
|
import com.fire.utils.date.DateUtils;
|
|
|
import com.fire.utils.string.FireStringUtils;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import redis.clients.jedis.JedisCluster;
|
|
|
|
|
|
@@ -25,9 +30,10 @@ import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+import static com.fire.dto.enums.AmountOper.SUB;
|
|
|
import static com.fire.dto.enums.PriceCheck.CHECK;
|
|
|
import static com.fire.dto.enums.Province.QG_ALL;
|
|
|
-import static com.fire.dto.enums.RedisKey.ORDER_ID_INCR;
|
|
|
+import static com.fire.dto.enums.RedisKey.*;
|
|
|
import static com.fire.dto.enums.Status.*;
|
|
|
import static com.fire.dto.enums.ValidStatus.INVALID;
|
|
|
|
|
|
@@ -37,10 +43,15 @@ import static com.fire.dto.enums.ValidStatus.INVALID;
|
|
|
* @author ZJQ 2021年5月14日17:13:54
|
|
|
*/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class MakeOrderServiceImpl implements MakeOrderService {
|
|
|
|
|
|
@Resource
|
|
|
private JedisCluster jedisCluster;
|
|
|
+ @Resource
|
|
|
+ private RedisOrderIdScript redisOrderIdScript;
|
|
|
+ @Resource
|
|
|
+ private RedisAmountScript redisAmountScript;
|
|
|
|
|
|
/**
|
|
|
* 下单方法
|
|
|
@@ -225,7 +236,7 @@ public class MakeOrderServiceImpl implements MakeOrderService {
|
|
|
}
|
|
|
//如果要求校验结算价格
|
|
|
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("结算价格不一致");
|
|
|
return responseDto;
|
|
|
@@ -269,13 +280,40 @@ public class MakeOrderServiceImpl implements MakeOrderService {
|
|
|
|
|
|
}
|
|
|
}
|
|
|
+ //自增id 取自redis,长度17位,后面加两位00 为主订单号,后面分发订单号后两位从 01开始
|
|
|
+ Long orderId = jedisCluster.incr(ORDER_ID_INCR.key()) * 100;
|
|
|
|
|
|
- jedisCluster.incr(ORDER_ID_INCR.key());
|
|
|
+ //客户订单id校验 这里订单id拼接上客户id,保证唯一
|
|
|
+ String customerOrder = content.getExtOrder() + "." + flowAppInfo.getCustomerId();
|
|
|
+ String checkOrder = redisOrderIdScript.checkOrderId(customerOrder, String.valueOf(orderId));
|
|
|
+ //0表示订单号重复
|
|
|
+ if ("0".equals(checkOrder)) {
|
|
|
+ orderResp.setRCode(ORDER_ID_ERROR.status());
|
|
|
+ orderResp.setRMsg(ORDER_ID_ERROR.message());
|
|
|
+ return responseDto;
|
|
|
+ }
|
|
|
|
|
|
//初始化订单详情
|
|
|
FlowOrderInfo order = new FlowOrderInfo();
|
|
|
order.setFlowAppId(appId);
|
|
|
- order.setOrderId("待生成");
|
|
|
+ order.setOrderId(orderId);
|
|
|
+
|
|
|
+ //订单转换为json入redis
|
|
|
+ String orderStr;
|
|
|
+ ObjectMapper om = new ObjectMapper();
|
|
|
+ try {
|
|
|
+ orderStr = om.writeValueAsString(order);
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
+ //订单转换的其他异常 如果执行到这里说明系统有bug
|
|
|
+ log.error("订单转换成json失败", e);
|
|
|
+ orderResp.setRCode(FAILURE_SERVER.status());
|
|
|
+ orderResp.setRMsg(FAILURE_SERVER.message());
|
|
|
+ return responseDto;
|
|
|
+ }
|
|
|
+ //扣减
|
|
|
+ redisAmountScript.changeAmount(CUSTOMER_AMOUNT.key(),CUSTOMER_AMOUNT.key()+flowAppInfo.getCustomerId(),SUB.oper(),String.valueOf(product.getPrice()),String.valueOf(customerInfo.getCreditAmount()));
|
|
|
+
|
|
|
+ jedisCluster.hset(ORDER_INFO.key(), String.valueOf(orderId), orderStr);
|
|
|
|
|
|
return responseDto;
|
|
|
}
|